]>
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 | |
be7880e1 MF |
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" "$@" | |
35fd42a7 MF |
13 | fi |
14 | else | |
15 | mount_args='-n' | |
f2ff891e | 16 | fi |
35fd42a7 | 17 | unset UNSHARE |
a5307b94 MF |
18 | } |
19 | ||
dc24f4e1 | 20 | maybe_mount() { |
a5307b94 MF |
21 | local src=/$1 dst=${chroot}/${2:-$1} |
22 | [[ -d ${src} ]] || return 0 | |
35fd42a7 MF |
23 | if ! mkdir -p "${dst}" ; then |
24 | [[ -w ${chroot} ]] && exit 1 || return 0 | |
25 | fi | |
f2ff891e | 26 | grep -sq "${dst}" /proc/mounts || mount ${mount_args} --bind "${src}" "${dst}" |
dc24f4e1 | 27 | } |
dc24f4e1 MF |
28 | |
29 | get_type() { | |
30 | case $(file "$1") in | |
35fd42a7 MF |
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;; | |
dc24f4e1 MF |
37 | esac |
38 | } | |
35fd42a7 MF |
39 | |
40 | init_chroot() { | |
41 | [[ -w . ]] || return 0 | |
42 | ||
43 | if [[ ! -L etc/mtab ]] ; then | |
9ebe6122 | 44 | ln -sfT /proc/mounts etc/mtab |
35fd42a7 | 45 | fi |
9ebe6122 | 46 | local f dst |
35fd42a7 MF |
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 | |
9ebe6122 MF |
62 | if [[ -e /${f} ]] ; then |
63 | cp "/${f}" "${f}" | |
35fd42a7 MF |
64 | fi |
65 | done | |
66 | for f in "${home[@]}" ; do | |
9ebe6122 MF |
67 | df="root/${f}" |
68 | f="${HOME}/${f}" | |
69 | if [[ -e ${f} ]] ; then | |
70 | cp "${f}" "${df}" | |
71 | fi | |
35fd42a7 | 72 | done |
9ebe6122 MF |
73 | |
74 | f="${HOME}/.profile.d/aliases.sh" | |
75 | if [[ -e ${f} ]] ; then | |
76 | cat "${f}" > root/.bash_profile | |
77 | fi | |
35fd42a7 MF |
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 | ||
5aba3aea | 99 | local mounts=( proc sys tmp dev dev/pts dev/shm usr/portage usr/portage/distfiles usr/local/src ) |
35fd42a7 MF |
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 | |
d1eee076 | 138 | fi |
35fd42a7 | 139 | |
917d2f98 MF |
140 | # Doubtful these settings we want to leak into the chroot. |
141 | unset ROOT PORTAGE_CONFIGROOT LD_LIBRARY_PATH | |
35fd42a7 MF |
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 "$@" |