aboutsummaryrefslogtreecommitdiff
path: root/alpine-chroot-activate
blob: 31ab90eff308eb9444174fbf9285e10ca61f7804 (plain) (blame)
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
35
36
37
38
39
40
#!/bin/sh

err() {
	echo "$@" >&2
	exit 1
}

if [ "$(id -u)" -ne 0 ]; then
	err 'Please run as root'
fi

if [ -z "$1" ]
then
	err 'Specify path as the first argument'
fi

ensure() {  
	{ set +x; } 2>/dev/null
        [ $# -lt 1 ] && return 0
	$@ || err failed executing: "$@"
	set -x
}

set -xu

umask 022

ensure export chroot_dir="$(ensure realpath -- "$1")"

umount "${chroot_dir}/dev"
umount "${chroot_dir}/proc"
umount "${chroot_dir}/sys"
ensure mount -o bind /dev "${chroot_dir}/dev"
ensure mount -t proc none "${chroot_dir}/proc"
ensure mount -o bind /sys "${chroot_dir}/sys"

{ set +x; } 2>/dev/null

printf '\n\nNow, type the following to enter the chroot:\n\n'
printf "chroot '%s' /bin/ash -l\n\n" "${chroot_dir}"