diff options
author | Jakob Odersky <jakob@odersky.com> | 2016-12-11 00:19:47 -0800 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2016-12-11 00:19:47 -0800 |
commit | 234361053f5cb1c6f058c181f460ce7f254729ca (patch) | |
tree | 7b4fe97d6c1e42e62a0efde20b64437b43fa8e5d | |
parent | 75ae14cf2b8078c39f574b43a269d9b40928d792 (diff) | |
download | dotfiles-234361053f5cb1c6f058c181f460ce7f254729ca.tar.gz dotfiles-234361053f5cb1c6f058c181f460ce7f254729ca.tar.bz2 dotfiles-234361053f5cb1c6f058c181f460ce7f254729ca.zip |
Add chrootfs utility script
-rwxr-xr-x | home/bin/chrootfs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/home/bin/chrootfs b/home/bin/chrootfs new file mode 100755 index 0000000..876a03f --- /dev/null +++ b/home/bin/chrootfs @@ -0,0 +1,41 @@ +#!/bin/bash +# Utility script to chroot into a root filesystem of a foreign +# architecture +set -e + +fail() { + echo "$1" >&2 + exit 1 +} + +rootfs="$1" + +[ -n "$rootfs" ] || fail "Usage: $(basename $0) <directory>" +[ -e "$rootfs" ] || fail "$rootfs does not exist" +[ "$EUID" -eq 0 ] || fail "$(basename $0) must be run as root" + +# Unmount and remove any temporary files +# +cleanup() { + set +e + fuser --kill -SIGTERM "$rootfs" + sleep 2 + fuser --kill -SIGKILL --verbose "$rootfs" + 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 + +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 "chrootfs: chrooting into $rootfs" 1>&2 +LANG=C LC_ALL=C chroot "$rootfs" +echo "chrootfs: cleaning up" 1>&2 +cleanup +echo "chrootfs: done" 1>&2 |