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