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