Skip to content

Instantly share code, notes, and snippets.

@JL2210
Last active June 27, 2020 13:15
Show Gist options
  • Save JL2210/f51a3d5a7a57900983b706949eba9f74 to your computer and use it in GitHub Desktop.
Save JL2210/f51a3d5a7a57900983b706949eba9f74 to your computer and use it in GitHub Desktop.
A script to bootstrap Gentoo on a Chromebook
#!/bin/bash
set -e
if [ "${UID}" = "0" ]; then
echo "Error: ${0} must not be run as root.";
exit 3;
fi
TMPDIR="$(mktemp -d)"
UNAME_M="$(uname -m)"
MIRROR="${MIRROR:-http://distfiles.gentoo.org/releases}"
INSTALL_DIR="${INSTALL_DIR:-/usr/local}"
PREFIX="${PREFIX:-${INSTALL_DIR}/gentoo}"
EPREFIX="${EPREFIX:-${INSTALL_DIR}/prefix}"
if [ "${UNAME_M}" = "x86_64" ] || [ "${UNAME_M}" = "aarch64" ]; then
read -p "Do you want to install a 32-bit Prefix to run old applications? [Y/n] ";
case "${REPLY}" in
[nN]* )
echo "OK, that's fine. Let's continue.";
;;
[yY]* )
case "${UNAME_M}" in
x86_64)
UNAME_M=i686;
;;
aarch64)
UNAME_M=armv7l;
;;
esac
PREFIX="${PREFIX}32";
EPREFIX="${EPREFIX}32";
echo "Got it. Your 32-bit Prefix will be installed in ${EPREFIX}.";
;;
*)
echo "I'm sorry, I must've made a mistake somewhere. Did you say something other than yes or no?"
exit 1;;
esac
fi
GITWEB_SCRIPTS="${GITWEB_SCRIPTS:-https://gitweb.gentoo.org/repo/proj/prefix.git/plain/scripts}"
case "${UNAME_M}" in
x86_64)
GENTOO_ARCH=amd64;
BITS=64;
ARCH="${GENTOO_ARCH}";
;;
i686)
GENTOO_ARCH=x86;
BITS=32;
ARCH="${UNAME_M}";
;;
armv7l)
GENTOO_ARCH=arm;
BITS=32;
ARCH=armv7a_hardfp;
;;
aarch64)
GENTOO_ARCH=arm64;
BITS=64;
ARCH="${GENTOO_ARCH}";
MIRROR="http://distfiles.gentoo.org/experimental";
DIST_URL="${MIRROR}/${GENTOO_ARCH}";
STAGE3_URL="${DIST_URL}/old/stage3-${ARCH}-20190115.tar.bz2";
;;
*)
echo "Bootstrapping Gentoo Prefix on your Chromebook is not supported.";
exit 1;;
esac
if [ "${UNAME_M}" != "aarch64" ]; then
DIST_URL="${MIRROR}/${GENTOO_ARCH}/autobuilds"
STAGE3_URL="${DIST_URL}/$(curl -Ls ${DIST_URL}/latest-stage3-${ARCH}.txt | sed '/^#/d' | cut -d' ' -f1)"
fi
STAGE3="${STAGE3_URL##*/}"
OUTPUT="${TMPDIR}/${STAGE3}"
echo "Downloading stage 3..."
curl --progress-bar --output "${OUTPUT}" "${STAGE3_URL}"
echo "Done. Unpacking stage 3..."
echo -e "Warning: All data in ${PREFIX} and ${EPREFIX} will be lost. To stop this, please press ^C in the next 15 seconds.\a"
sleep 15
echo -e "\aContinuing..."
sudo umount -Rl "${PREFIX}/proc" 2>/dev/null || :
sudo umount -Rl "${PREFIX}/dev" 2>/dev/null || :
sudo umount -Rl "${PREFIX}/sys" 2>/dev/null || :
sudo rm -rf "${PREFIX}" "${EPREFIX}"
sudo mkdir -p "${PREFIX}/${EPREFIX}"
sudo chown chronos:chronos "${PREFIX}/${EPREFIX}"
sudo tar xphf "${OUTPUT}" --xattrs-include='*.*' --numeric-owner -C "${PREFIX}"
rm -rf "${TMPDIR}"
echo "Downloading bootstrap script..."
sudo curl --progress-bar --output "${PREFIX}/bootstrap-prefix.sh" -L "${GITWEB_SCRIPTS}/bootstrap-prefix.sh"
sudo chmod a+x "${PREFIX}/bootstrap-prefix.sh"
echo "Creating wrapper script..."
echo "#! /bin/sh
set -e
useradd -u 1000 -s /bin/bash chronos
export CHOST=\"\$(gcc -dumpmachine | sed -i 's,armv7a-unknown-linux-gnueabihf,armv7l-pc-linux-gnu,g')\"
emerge-webrsync
ACCEPT_KEYWORDS=\"~${GENTOO_ARCH}\" emerge -1 glibc
setarch linux${BITS} su chronos -c \"/bootstrap-prefix.sh \${1} \${2}\"
" | sudo tee "${PREFIX}/bootstrap-wrapper.sh" >/dev/null
sudo chmod a+x "${PREFIX}/bootstrap-wrapper.sh"
echo "Running wrapper script..."
sudo mount -t proc procfs "${PREFIX}/proc"
sudo mount --rbind /dev "${PREFIX}/dev"
sudo mount --make-rslave "${PREFIX}/dev"
sudo mount --rbind /sys "${PREFIX}/sys"
sudo mount --make-rslave "${PREFIX}/sys"
sudo cp -L /etc/resolv.conf "${PREFIX}/etc/"
sudo chroot "${PREFIX}" /bootstrap-wrapper.sh "${EPREFIX}" "noninteractive"
sudo mv "${PREFIX}/${EPREFIX}" "${INSTALL_DIR}/"
sudo umount -Rl "${PREFIX}/proc" || :
sudo umount -Rl "${PREFIX}/dev" || :
sudo umount -Rl "${PREFIX}/sys" || :
sudo rm -rf "${PREFIX}"
echo -e "\nGentoo Prefix has been installed; to run it, use this command: ${EPREFIX}/startprefix"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment