about summary refs log tree commit diff
path: root/xchroot
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2018-02-01 19:26:14 +0100
committerLeah Neukirchen <leah@vuxu.org>2018-02-01 19:26:14 +0100
commite9c011077d578aeb0e1f2c85b566b2769ecee160 (patch)
treef65ac6c26ddd71d14c3c46f361cbe39fc1d8c7aa /xchroot
parentb6fcc0fe8f58fcf03407b92ad6346be2eb6b1a7f (diff)
downloadxtools-e9c011077d578aeb0e1f2c85b566b2769ecee160.tar.gz
xtools-e9c011077d578aeb0e1f2c85b566b2769ecee160.tar.xz
xtools-e9c011077d578aeb0e1f2c85b566b2769ecee160.zip
add xchroot
Diffstat (limited to 'xchroot')
-rwxr-xr-xxchroot41
1 files changed, 41 insertions, 0 deletions
diff --git a/xchroot b/xchroot
new file mode 100755
index 0000000..2e76f42
--- /dev/null
+++ b/xchroot
@@ -0,0 +1,41 @@
+#!/bin/sh -e
+# xchroot DIR [CMD...] - chroot into a Void (or other Linux) installation
+
+fail() {
+	printf '%s\n' "$1" 1>&2
+	exit 1
+}
+
+if [ "$(id -u)" -ne 0 ]; then
+	fail 'xchroot needs to run as root'
+fi
+
+case "$1" in
+	/*) CHROOT=$1; shift;;
+	*) fail 'no absolute chroot dir given';;
+esac
+
+[ -d "$CHROOT" ] || fail 'not a directory'
+[ -d "$CHROOT/dev" ] || fail 'no /dev in chroot'
+[ -d "$CHROOT/proc" ] || fail 'no /proc in chroot'
+[ -d "$CHROOT/sys" ] || fail 'no /sys in chroot'
+
+mount --rbind /dev "$CHROOT/dev"
+mount --rbind /proc "$CHROOT/proc"
+mount --rbind /sys "$CHROOT/sys"
+
+cleanup() {
+	umount -R "$CHROOT/dev" "$CHROOT/proc" "$CHROOT/sys"
+}
+
+trap cleanup EXIT INT
+
+if [ -f "$CHROOT/$SHELL" ]; then
+	INNER_SHELL="$SHELL"
+elif [ -f "$CHROOT/bin/bash" ]; then
+	INNER_SHELL="/bin/bash"
+else
+	INNER_SHELL="/bin/sh"
+fi
+
+chroot "$CHROOT" ${@:-$INNER_SHELL}