From 3687eb638dfcdc641b77e46f44814638d5b9bf81 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sat, 17 Sep 2016 15:16:06 -0700 Subject: initial commit --- chrootfs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 chrootfs (limited to 'chrootfs') diff --git a/chrootfs b/chrootfs new file mode 100755 index 0000000..0b42070 --- /dev/null +++ b/chrootfs @@ -0,0 +1,76 @@ +#!/bin/bash +set -e + +# Directory in which the root filesystem is contained +# +ROOTFS=$(pwd)/cb5-rootfs + +print_usage() { + cat 1>&2 <&2 + exit 1 + esac + shift +done + +# Process last argument, the root file system location +if [ -z "$1" ] || [ "$1" = -h ] || [ "$1" = --help ]; then + print_usage + exit 1 +fi +if [ "$EUID" -ne 0 ]; then + echo "This must be run as root." 1>&2 + exit 1 +fi +export ROOTFS="$1" + +# Unmount and remove any temporary files +# +cleanup() { + set +e + + # Identify and kill all processes still using files + echo "Killing processes using mount point" 1>&2 + fuser -k "${ROOTFS}" + sleep 3 + fuser -9 -k -v "${ROOTFS}" + + # Clean up all temporary mount points + echo "Removing temporary mount points" 1>&2 + umount -l "${ROOTFS}/proc" 2> /dev/null + umount -l "${ROOTFS}/sys" 2> /dev/null + umount -l "${ROOTFS}/dev/pts" 2> /dev/null + umount -l "${ROOTFS}/dev" 2> /dev/null + trap - 0 1 2 3 6 +} +trap cleanup 0 1 2 3 6 + +# Prepare filesystem to be chrooted +# +echo "Creating temporary system mount points in rootfs" 1>&2 +mount -t proc chproc "$ROOTFS/proc" +mount -t sysfs chsys "$ROOTFS/sys" +mount -t devtmpfs chdev "$ROOTFS/dev" +mount -t devpts chpts "$ROOTFS/dev/pts" + +echo "Starting shell in rootfs" 1>&2 +LANG=C LC_ALL=C DEBIAN_FRONTEND=noninteractive chroot ${ROOTFS} + +cleanup +echo "Cleaned up" -- cgit v1.2.3