Skip to content

Instantly share code, notes, and snippets.

@jdowner
Last active August 29, 2015 14:13
Show Gist options
  • Save jdowner/4792d1ca626d3543eb6e to your computer and use it in GitHub Desktop.
Save jdowner/4792d1ca626d3543eb6e to your computer and use it in GitHub Desktop.
chroot
#!/bin/bash
set -o pipefail
CHROOT_PATH="${HOME}/scm/chroot"
SRC_PATH="${HOME}/scm/rift"
die() {
echo
echo "ERROR: $*"
echo
exit 1
}
mounted() {
mount 2>/dev/null | cut -d' ' -f 3 | grep -q "^${CHROOT_PATH}${1}$"
}
bind_mount() {
local dest=${CHROOT_PATH}${1:-${2}}
[ ! -d "${dest}" ] && sudo mkdir -p "${dest}"
sudo mount -o bind ${1} "${dest}" || die "Bind mount of ${1} to ${dest} failed"
}
chroot_init() {
if ! mounted "/dev"; then
sudo mount -t devtmpfs devtmpfs ${CHROOT_PATH}/dev
fi
if ! mounted "/dev/shm"; then
sudo mount -t tmpfs tmpfs ${CHROOT_PATH}/dev/shm
fi
if ! mounted "/proc"; then
sudo mount -t proc proc ${CHROOT_PATH}/proc
fi
if ! mounted "/sys"; then
sudo mount -t sysfs sysfs ${CHROOT_PATH}/sys
fi
if ! mounted "/tmp"; then
sudo mount -t tmpfs tmpfs ${CHROOT_PATH}/tmp
fi
if ! mounted "${SRC_PATH}"; then
bind_mount "${SRC_PATH}"
fi
if ! mounted "/dev/pts"; then
bind_mount "/dev/pts"
fi
mkdir -p ${CHROOT_PATH}/home/${USER}
sudo chown ${USER}:${GROUP} ${CHROOT_PATH}/home/${USER}
rsync -qrLptgo \
--exclude='*.swp' --exclude='chromium/*' --exclude='google-chrome/*' --exclude='.ccache*' --exclude='.cache*' \
--exclude='.mozilla/*' --exclude='.wine/*' --exclude='.macromedia/*' --exclude='.config/libreoffice/*' \
--exclude='.ant/*' \
/home/${USER}/.[^.]* ${CHROOT_PATH}/home/${USER}
sudo cp /etc/{passwd,group,shadow,sudoers,resolv.conf} ${CHROOT_PATH}/etc
sudo touch ${CHROOT_PATH}/rift-chroot
}
chroot_enter() {
chroot_init
sudo chroot ${CHROOT_PATH} su ${USER} -l -s "${SRC_PATH}/rift-shell"
}
chroot_deinit() {
local mps=$(mount | grep "${CHROOT_PATH}" | sort -u | cut -d ' ' -f 3)
local mp
for mp in ${mps}; do
sudo umount -f ${mp} || echo "WARNING: failed to umount ${mp}"
done
}
usage() {
cat <<- EOF
$(basename ${0}) [ARGUMENTS]
ARGUMENTS:
-h, --help This screen
-c, --chroot [PATH] Root of the chroot [${CHROOT_PATH}]
-s, --source [PATH] Root of the Rift source tree [${SRC_PATH}]
-i, --init Initialize the chroot
-e, --enter Initialize and enter the chroot
-d, --deinit Deinitialize the chroot
Building a chroot:
# Build a VM image with developer files include
sudo scripts/cloud/mkvmimg -d -o disk-image
# Create a source and destination mountpoint (outside of your checkout)
mkdir -p ~/tmp ${CHROOT_PATH}
# Sync the chroot to the image.
sudo mount -o bind scripts/cloud/rift-root-aaaaaaa.img ~/tmp
sudo rsync -av --delete-after ~/tmp/ ${CHROOT_PATH}/
sudo umount ~/tmp
EOF
}
ACTION=
while [ $# -gt 0 ]; do
case "${1}" in
-h|--help)
usage
exit 0
;;
-c|--chroot)
CHROOT_PATH=${2}
shift;;
-s|--source)
SRC_PATH=${2}
shift;;
-i|--init)
ACTION="init"
;;
-e|--enter)
ACTION="enter"
;;
-d|--deinit)
ACTION="deinit"
;;
esac
shift
done
if [ "${ACTION}" == "init" ]; then
chroot_init
elif [ "${ACTION}" == "enter" ]; then
chroot_enter
elif [ "${ACTION}" == "deinit" ]; then
chroot_deinit
else
die "No action specified, see --help"
fi
# vim: noet sw=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment