]> git.wh0rd.org - home.git/blob - .bin/custom-chroot
chroot: support uts/pid namespaces too
[home.git] / .bin / custom-chroot
1 #!/bin/bash -e
2
3 bootstrap() {
4 [[ $(id -u) -eq 0 ]] || exec sudo env -uUNSHARE HOME="$HOME" "$0" "$@"
5
6 if [[ -z ${UNSHARE} ]] ; then
7 mount_args=
8 if type -P unshare >&/dev/null ; then
9 uargs=( -m )
10 unshare -u -- true >&/dev/null && uargs+=( -u )
11 unshare -p -- true >&/dev/null && uargs+=( -p -f --mount-proc )
12 UNSHARE=true exec unshare "${uargs[@]}" -- "$0" "$@"
13 fi
14 else
15 mount_args='-n'
16 fi
17 unset UNSHARE
18 }
19
20 maybe_mount() {
21 local src=/$1 dst=${chroot}/${2:-$1}
22 [[ -d ${src} ]] || return 0
23 if ! mkdir -p "${dst}" ; then
24 [[ -w ${chroot} ]] && exit 1 || return 0
25 fi
26 grep -sq "${dst}" /proc/mounts || mount ${mount_args} --bind "${src}" "${dst}"
27 }
28
29 get_type() {
30 case $(file "$1") in
31 *x86-64*) echo x86_64;;
32 *"Intel 80386"*) echo i386;;
33 *32-bit*PowerPC*) echo ppc;;
34 *64-bit*PowerPC*) echo ppc64;;
35 *32-bit*S/390*) echo s390;;
36 *64-bit*S/390*) echo s390x;;
37 esac
38 }
39
40 init_chroot() {
41 [[ -w . ]] || return 0
42
43 if [[ ! -L etc/mtab ]] ; then
44 ln -sfT /proc/mounts etc/mtab
45 fi
46 local f dst
47 local etc=(
48 hosts
49 locale.gen
50 localtime
51 resolv.conf
52 )
53 local home=(
54 .inputrc
55 .gdbinit
56 .gitconfig
57 .nanorc
58 )
59 for f in \
60 $(printf 'etc/%s ' "${etc[@]}") \
61 ; do
62 if [[ -e /${f} ]] ; then
63 cp "/${f}" "${f}"
64 fi
65 done
66 for f in "${home[@]}" ; do
67 df="root/${f}"
68 f="${HOME}/${f}"
69 if [[ -e ${f} ]] ; then
70 cp "${f}" "${df}"
71 fi
72 done
73
74 f="${HOME}/.profile.d/aliases.sh"
75 if [[ -e ${f} ]] ; then
76 cat "${f}" > root/.bash_profile
77 fi
78 }
79
80 usage() {
81 cat <<-EOF
82 Usage: ${0##*/} [options] [program to run]
83
84 Sets up common mount points and then chroots in and runs a program.
85 If no program is specified, then launch a login shell.
86
87 Options:
88 -u Unmount all paths in the chroot
89 -m <path> Add path to mount list
90 -d <dir> Use <dir> as chroot (defaults to ${0%/*})
91 -h This help screen
92 EOF
93 exit
94 }
95
96 main() {
97 bootstrap "$@"
98
99 local mounts=( proc sys tmp dev dev/pts dev/shm usr/portage usr/portage/distfiles usr/local/src )
100
101 local chroot=${0%/*}
102 case ${chroot} in
103 .) chroot=${PWD} ;;
104 ./*) chroot=${PWD}/${chroot#./} ;;
105 esac
106
107 local cmd
108 while [[ -n $1 ]] ; do
109 case $1 in
110 -u) cmd='umount' ;;
111 -m) mounts+=( "$2" ); shift ;;
112 -d) chroot=$(realpath "$2"); shift ;;
113 -h) usage ;;
114 -*) echo "${0##*/}: unknown option $1"; exit 1 ;;
115 *) break ;;
116 esac
117 shift
118 done
119 cd "${chroot}"
120
121 case ${cmd} in
122 umount) exec "${0%/*}/umount-tree" -y "${chroot}" ;;
123 esac
124
125 local m
126 for m in "${mounts[@]}" ; do
127 maybe_mount ${m}
128 done
129
130 init_chroot
131
132 local setarch
133 if type -P setarch &>/dev/null ; then
134 local bin_dst=$(get_type bin/bash)
135 if [[ -n ${bin_dst} ]] ; then
136 setarch="setarch ${bin_dst}"
137 fi
138 fi
139
140 # Doubtful these settings we want to leak into the chroot.
141 unset ROOT PORTAGE_CONFIGROOT LD_LIBRARY_PATH
142 unset LS_COLORS # format changes over time
143 [[ $# -eq 0 ]] && set -- env HOME=/root /bin/bash -l
144 exec \
145 ${setarch} \
146 chroot "${chroot}" \
147 "$@"
148 }
149
150 main "$@"