Skip to content

Instantly share code, notes, and snippets.

@ryao
Last active February 3, 2016 04:57
Show Gist options
  • Save ryao/3c345f206b19c9795109 to your computer and use it in GitHub Desktop.
Save ryao/3c345f206b19c9795109 to your computer and use it in GitHub Desktop.
A script to simplify entering a "chroot"-style container in the Gentoo install process
#!/bin/sh
HOSTNAME=
BIND=
SHELL=bash
while getopts "b:h:s:" opt; do
case $opt in
b)
BIND="${BIND} ${OPTARG}"
;;
h)
HOSTNAME="${OPTARG}"
;;
s)
SHELL="${OPTARG}"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $(($OPTIND - 1))
ROOT="$*"
# Use a mount namespace (replaces chroot method)
exec unshare -ipmuf /bin/sh -c "
pivot_root \"${ROOT}\" \"${ROOT}/tmp\"
mount --rbind /tmp/dev /dev
mount --rbind /tmp/sys /sys
mount -t proc none /proc
if [ -n \"${BIND}\" ]
then
echo \"${BIND}\" | while IFS=: read src dest opts; do
mkdir -p \"\${dest}\"
mount --bind \"/tmp\${src# }\" \"\${dest}\"
[ -n \"\${opts}\" ] && mount -o \"remount,\${opts}\" \"\${dest}\"
done
fi
umount -l /tmp
hostname ${HOSTNAME} > /dev/null
cd
exec ${SHELL}
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment