Last active
April 1, 2019 22:56
-
-
Save maxwellb/01100e2463dd1a3e9a279c596658cb4e to your computer and use it in GitHub Desktop.
Gentoo-Prefix chroot for Chrome OS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
user=$(id -run) | |
group=$(id -grn) | |
dir=/usr/local/${user} | |
forcerel=${1:-""} | |
assert_base_dir() { | |
[ -d ${dir} ] || sudo mkdir -pv ${dir} | |
sudo chown --from=root ${user}:${group} ${dir} | |
} | |
assert_bind_mount() { | |
local mount="${1}" | |
local mountpoint="${dir}/gentoo${mount}" | |
check_mount ${mount} || do_mount ${mount} ${mountpoint} | |
} | |
assert_bind_mounts() { | |
assert_bind_mount /dev | |
assert_bind_mount /tmp/gentoo | |
assert_bind_mount /proc | |
assert_bind_mount /sys | |
assert_bind_mount $HOME | |
assert_bind_mount /etc/resolv.conf | |
assert_bind_mount /etc/passwd | |
assert_bind_mount /etc/group | |
assert_bind_mount /etc/hosts | |
} | |
assert_chroot_prep() { | |
sudo mkdir -pv ${dir}/gentoo/dev | |
sudo mkdir -pv ${dir}/gentoo/tmp/gentoo | |
sudo mkdir -pv ${dir}/gentoo/proc | |
sudo mkdir -pv ${dir}/gentoo${HOME} | |
sudo mkdir -pv ${dir}/gentoo/sys | |
if [ -L ${dir}/gentoo/dev ]; then | |
echo "${dir}/gentoo/dev cannot be a symlink" | |
exit 1 | |
fi | |
if [ -L ${dir}/gentoo/tmp/gentoo ]; then | |
echo "${dir}/gentoo/tmp/gentoo cannot be a symlink" | |
exit 1 | |
fi | |
if [ -L ${dir}/gentoo/proc ]; then | |
echo "${dir}/gentoo/proc cannot be a symlink" | |
exit 1 | |
fi | |
if [ -L ${dir}/gentoo/sys ]; then | |
echo "${dir}/gentoo/sys cannot be a symlink" | |
exit 1 | |
fi | |
assert_bind_mounts | |
} | |
assert_distribution() { | |
[ -d $dir/gentoo ] || extract_remote_release | |
} | |
assert_tmp_link() { | |
ln -sf ${dir}/gentoo /tmp/gentoo || sudo ln -sf ${dir}/gentoo /tmp/gentoo | |
# And make sure it is relinked when we run the shell again (after reboot) | |
assert_tmp_link_in_bashrc | |
} | |
assert_tmp_link_in_bashrc() { | |
grep "ln .*${dir}/gentoo" ~/.bashrc >/dev/zero || echo "ln -sf ${dir}/gentoo /tmp/gentoo" >> ~/.bashrc | |
} | |
check_mount() { | |
local mount="${1}" | |
[ ! -e ${mount} ] && true || grep "${dir}/gentoo" /proc/mounts | awk '{ print $2 }' | grep "^${dir}/gentoo${mount}$" >/dev/zero | |
} | |
display_chroot_help() { | |
echo " " | |
echo "Run the following command to enter the prefix at ${dir}/gentoo within a chroot:" | |
echo " sudo -E chroot --userspec=${user}:${group} ${dir}/gentoo '/startprefix_original'" | |
echo " " | |
} | |
do_mount() { | |
local mount="${1}" | |
local mountpoint="${2}" | |
if [ "x" != "x${mount}" -a "x" != "x${mountpoint}" ]; then | |
echo "Mounting ${mount} at ${mountpoint}" | |
if [ -f ${mount} ]; then | |
sudo cp -fv ${mount} ${mountpoint} | |
else | |
sudo mount --bind "${mount}" "${mountpoint}" | |
fi | |
fi | |
} | |
extract_remote_release() { | |
if [ -L ${dir} ]; then | |
echo "${dir} should not be a symlink" | |
exit 1 | |
fi | |
local release="https://github.com$(curl -sL https://github.com/awesomebytes/gentoo_prefix_ci/releases/latest | grep -E 'href=".*?/archive/.*?\.tar\.gz"' | sed -e 's;.*href="\(.*\)" rel.*;\1;')" | |
release=${forcerel:-release} | |
if [ "${release}" > "http" -a "${release}" < "httq" ]; then | |
curl -sL "${release}" | tar --directory=${dir} -zxv | |
else | |
tar --directory=${dir} -zxvf ${release} | |
fi | |
} | |
# Ensure we're not in a chroot | |
if [ ! -d /mnt/stateful_partition ]; then | |
exit 0; | |
fi | |
# Get the release | |
assert_base_dir | |
assert_distribution | |
assert_tmp_link | |
# Convenience link in $HOME | |
[ -e ~/gentoo ] || ln -sf ${dir}/gentoo ~/gentoo | |
assert_chroot_prep | |
display_chroot_help | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment