]>
Commit | Line | Data |
---|---|---|
36c3082a | 1 | #!/bin/bash -e |
dc24f4e1 | 2 | |
35fd42a7 | 3 | bootstrap() { |
deeffdc9 | 4 | [[ $(id -u) -eq 0 ]] || exec sudo env -uUNSHARE HOME="$HOME" "$0" "$@" |
a5307b94 | 5 | |
35fd42a7 MF |
6 | if [[ -z ${UNSHARE} ]] ; then |
7 | mount_args= | |
8 | if type -P unshare >&/dev/null ; then | |
65050245 | 9 | test_arg() { unshare "$@" -- true >&/dev/null && uargs+=( "$@" ) || :; } |
be7880e1 | 10 | uargs=( -m ) |
587fdaa2 MF |
11 | test_arg -u |
12 | test_arg -i | |
13 | test_arg -p -f --mount-proc | |
14 | test_arg --propagation=private | |
be7880e1 | 15 | UNSHARE=true exec unshare "${uargs[@]}" -- "$0" "$@" |
35fd42a7 MF |
16 | fi |
17 | else | |
18 | mount_args='-n' | |
f2ff891e | 19 | fi |
35fd42a7 | 20 | unset UNSHARE |
a5307b94 MF |
21 | } |
22 | ||
7f474309 MF |
23 | is_mounted() { |
24 | local dst=$1 | |
25 | grep -sq "${dst}" /proc/mounts | |
26 | } | |
27 | ||
dc24f4e1 | 28 | maybe_mount() { |
a5307b94 MF |
29 | local src=/$1 dst=${chroot}/${2:-$1} |
30 | [[ -d ${src} ]] || return 0 | |
35fd42a7 MF |
31 | if ! mkdir -p "${dst}" ; then |
32 | [[ -w ${chroot} ]] && exit 1 || return 0 | |
33 | fi | |
7f474309 | 34 | is_mounted "${dst}" || mount ${mount_args} --bind "${src}" "${dst}" |
dc24f4e1 | 35 | } |
dc24f4e1 MF |
36 | |
37 | get_type() { | |
38 | case $(file "$1") in | |
1b460979 MF |
39 | *x86-64*) echo x86_64;; |
40 | *"Intel 80386"*) echo i386;; | |
41 | *32-bit*PowerPC*MSB*) echo ppc;; | |
42 | *64-bit*PowerPC*MSB*) echo ppc64;; | |
43 | *32-bit*PowerPC*LSB*) echo ppcle;; | |
44 | *64-bit*PowerPC*LSB*) echo ppc64le;; | |
45 | *32-bit*S/390*) echo s390;; | |
46 | *64-bit*S/390*) echo s390x;; | |
47 | *64-bit*MIPS*) echo mips64;; | |
48 | *32-bit*MIPS*N32*) echo mips64;; | |
49 | *32-bit*MIPS*) echo mips;; | |
50 | *32-bit*PA-RISC*) echo parisc;; | |
51 | *64-bit*PA-RISC*) echo parisc64;; | |
52 | *32-bit*SPARC*) echo sparc;; | |
53 | *64-bit*SPARC*) echo sparc64;; | |
dc24f4e1 MF |
54 | esac |
55 | } | |
35fd42a7 MF |
56 | |
57 | init_chroot() { | |
58 | [[ -w . ]] || return 0 | |
59 | ||
60 | if [[ ! -L etc/mtab ]] ; then | |
9ebe6122 | 61 | ln -sfT /proc/mounts etc/mtab |
35fd42a7 | 62 | fi |
9ebe6122 | 63 | local f dst |
35fd42a7 MF |
64 | local etc=( |
65 | hosts | |
66 | locale.gen | |
67 | localtime | |
68 | resolv.conf | |
69 | ) | |
70 | local home=( | |
71 | .inputrc | |
72 | .gdbinit | |
73 | .gitconfig | |
74 | .nanorc | |
75 | ) | |
76 | for f in \ | |
77 | $(printf 'etc/%s ' "${etc[@]}") \ | |
78 | ; do | |
9ebe6122 MF |
79 | if [[ -e /${f} ]] ; then |
80 | cp "/${f}" "${f}" | |
35fd42a7 MF |
81 | fi |
82 | done | |
83 | for f in "${home[@]}" ; do | |
9ebe6122 MF |
84 | df="root/${f}" |
85 | f="${HOME}/${f}" | |
86 | if [[ -e ${f} ]] ; then | |
87 | cp "${f}" "${df}" | |
88 | fi | |
35fd42a7 | 89 | done |
9ebe6122 | 90 | |
584a84ce MF |
91 | if [[ ! -d root/.git ]] ; then |
92 | f="${HOME}/.profile.d/aliases.sh" | |
93 | if [[ -e ${f} ]] ; then | |
94 | cat "${f}" > root/.bash_profile | |
95 | fi | |
9ebe6122 | 96 | fi |
35fd42a7 MF |
97 | } |
98 | ||
99 | usage() { | |
100 | cat <<-EOF | |
101 | Usage: ${0##*/} [options] [program to run] | |
102 | ||
103 | Sets up common mount points and then chroots in and runs a program. | |
104 | If no program is specified, then launch a login shell. | |
105 | ||
106 | Options: | |
107 | -u Unmount all paths in the chroot | |
108 | -m <path> Add path to mount list | |
109 | -d <dir> Use <dir> as chroot (defaults to ${0%/*}) | |
110 | -h This help screen | |
111 | EOF | |
112 | exit | |
113 | } | |
114 | ||
115 | main() { | |
116 | bootstrap "$@" | |
117 | ||
8c29b1d8 MF |
118 | local mounts=( |
119 | proc sys tmp dev dev/shm run | |
120 | usr/portage usr/portage/distfiles | |
121 | var/db/repos/gentoo var/cache/distfiles | |
122 | usr/local/src | |
123 | ) | |
35fd42a7 MF |
124 | |
125 | local chroot=${0%/*} | |
126 | case ${chroot} in | |
127 | .) chroot=${PWD} ;; | |
128 | ./*) chroot=${PWD}/${chroot#./} ;; | |
129 | esac | |
130 | ||
131 | local cmd | |
132 | while [[ -n $1 ]] ; do | |
133 | case $1 in | |
134 | -u) cmd='umount' ;; | |
135 | -m) mounts+=( "$2" ); shift ;; | |
136 | -d) chroot=$(realpath "$2"); shift ;; | |
8b6a28cd | 137 | --help|-h) usage ;; |
35fd42a7 MF |
138 | -*) echo "${0##*/}: unknown option $1"; exit 1 ;; |
139 | *) break ;; | |
140 | esac | |
141 | shift | |
142 | done | |
143 | cd "${chroot}" | |
144 | ||
145 | case ${cmd} in | |
146 | umount) exec "${0%/*}/umount-tree" -y "${chroot}" ;; | |
147 | esac | |
148 | ||
149 | local m | |
150 | for m in "${mounts[@]}" ; do | |
1e623aa8 | 151 | maybe_mount "${m}" |
35fd42a7 | 152 | done |
7f474309 MF |
153 | # Handle special mounts that we don't want to just bind mount. |
154 | if ! is_mounted "${chroot}/dev/pts" ; then | |
0e51bf73 MF |
155 | # Option order matters: on older kernels that don't recognize newinstance, |
156 | # parsing stops as soon as it hits that. | |
7f474309 | 157 | mount -t devpts devpts "${chroot}/dev/pts" \ |
0e51bf73 | 158 | -o nosuid,noexec,mode=0620,gid=5,ptmxmode=0666,newinstance |
7f474309 | 159 | fi |
35fd42a7 MF |
160 | |
161 | init_chroot | |
162 | ||
163 | local setarch | |
164 | if type -P setarch &>/dev/null ; then | |
165 | local bin_dst=$(get_type bin/bash) | |
166 | if [[ -n ${bin_dst} ]] ; then | |
167 | setarch="setarch ${bin_dst}" | |
168 | fi | |
d1eee076 | 169 | fi |
35fd42a7 | 170 | |
917d2f98 MF |
171 | # Doubtful these settings we want to leak into the chroot. |
172 | unset ROOT PORTAGE_CONFIGROOT LD_LIBRARY_PATH | |
35fd42a7 MF |
173 | unset LS_COLORS # format changes over time |
174 | [[ $# -eq 0 ]] && set -- env HOME=/root /bin/bash -l | |
175 | exec \ | |
176 | ${setarch} \ | |
177 | chroot "${chroot}" \ | |
178 | "$@" | |
179 | } | |
180 | ||
181 | main "$@" |