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