summaryrefslogtreecommitdiff
path: root/scripts/uchroot
blob: b11981a18bb1b28f532a79011147837f93d8e886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/sh
set -e
[ $# -ge 1 ] || {
	echo "Usage: uchroot ROOT [USER] [CMD]" >&2
	exit 1
}

if [ "$(id -u)" != "0" ]; then
	exec uroot "$0" "$@"
fi
###############################################################

ROOT="$1"
USR="${2:-root}"
shift 2
[ $# -lt 1 ] && CMD="" || CMD="-c '$@'"

[ -d "$ROOT" ] || {
	echo "There is no such directory: $ROOT" >&2
	exit 1
}
[ -d "$ROOT/proc" -a -d "$ROOT/dev" -a -d "$ROOT/sys" ] || {
	echo "Root seems to not contain valid rootfs: $ROOT" >&2
	exit 1
}

mount --rbind "$ROOT" "$ROOT"
mount -t proc none "$ROOT/proc"
mount --rbind /dev "$ROOT/dev"
mount --rbind /sys "$ROOT/sys"
cp -f /etc/resolv.conf "$ROOT/etc/resolv.conf"
cp -f /etc/hosts "$ROOT/etc/hosts"

exec chroot "$ROOT" /bin/sh -c ". /etc/profile && exec su $CMD -l $USR" "$0"