summaryrefslogtreecommitdiff
path: root/scripts/uchroot
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/uchroot')
-rwxr-xr-xscripts/uchroot35
1 files changed, 29 insertions, 6 deletions
diff --git a/scripts/uchroot b/scripts/uchroot
index b11981a..b94e14f 100755
--- a/scripts/uchroot
+++ b/scripts/uchroot
@@ -1,7 +1,11 @@
#!/bin/sh
set -e
[ $# -ge 1 ] || {
- echo "Usage: uchroot ROOT [USER] [CMD]" >&2
+ echo "Usage: uchroot USER [CMD] [ARG].." >&2
+ echo "Configuration has to be passed trough envrionment."
+ echo " ROOT: path to directory to chroot in"
+ echo " ARCH: optional target architecture (if qemu should be used)"
+ echo " MOUNT: pairs of two paths separated by colon (LOCAL:INROOT)"
exit 1
}
@@ -10,10 +14,17 @@ if [ "$(id -u)" != "0" ]; then
fi
###############################################################
-ROOT="$1"
-USR="${2:-root}"
-shift 2
-[ $# -lt 1 ] && CMD="" || CMD="-c '$@'"
+[ -n "$ROOT" ] || {
+ echo "Variable ROOT has to be defined in environment."
+ exit 1
+}
+
+USR="${1:-root}"
+shift
+[ $# -lt 1 ] && CMD="" || {
+ CMD="-c \"$1\""
+ shift
+}
[ -d "$ROOT" ] || {
echo "There is no such directory: $ROOT" >&2
@@ -24,11 +35,23 @@ shift 2
exit 1
}
+EXECUTOR=
+
mount --rbind "$ROOT" "$ROOT"
mount -t proc none "$ROOT/proc"
mount --rbind /dev "$ROOT/dev"
mount --rbind /sys "$ROOT/sys"
+while IFS=: read src target; do
+ [ -n "$src" -a -n "$target" ] || continue
+ mount --rbind "$src" "$ROOT/$target"
+done <<<"$MOUNT"
+
cp -f /etc/resolv.conf "$ROOT/etc/resolv.conf"
cp -f /etc/hosts "$ROOT/etc/hosts"
+if [ -n "$ARCH" -a "$ARCH" != "$(arch)" ]; then
+ cp -f "$(which "qemu-$ARCH")" "$ROOT/qemu"
+ EXECUTOR=/qemu
+fi
-exec chroot "$ROOT" /bin/sh -c ". /etc/profile && exec su $CMD -l $USR" "$0"
+exec chroot "$ROOT" $EXECUTOR /bin/sh -c \
+ ". /etc/profile && exec su -l $USR $CMD \"\$@\"" -- "$@"