]> git.wh0rd.org - home.git/blame - .bin/custom-chroot
git-repack: new repack helper
[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
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
dc24f4e1 23maybe_mount() {
a5307b94
MF
24 local src=/$1 dst=${chroot}/${2:-$1}
25 [[ -d ${src} ]] || return 0
35fd42a7
MF
26 if ! mkdir -p "${dst}" ; then
27 [[ -w ${chroot} ]] && exit 1 || return 0
28 fi
f2ff891e 29 grep -sq "${dst}" /proc/mounts || mount ${mount_args} --bind "${src}" "${dst}"
dc24f4e1 30}
dc24f4e1
MF
31
32get_type() {
33 case $(file "$1") in
35fd42a7
MF
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;;
95ef1dd2
MF
40 *64-bit*MIPS*) echo mips64;;
41 *32-bit*MIPS*N32*)echo mips64;;
42 *32-bit*MIPS*) echo mips;;
0e502db0
MF
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;;
dc24f4e1
MF
47 esac
48}
35fd42a7
MF
49
50init_chroot() {
51 [[ -w . ]] || return 0
52
53 if [[ ! -L etc/mtab ]] ; then
9ebe6122 54 ln -sfT /proc/mounts etc/mtab
35fd42a7 55 fi
9ebe6122 56 local f dst
35fd42a7
MF
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
9ebe6122
MF
72 if [[ -e /${f} ]] ; then
73 cp "/${f}" "${f}"
35fd42a7
MF
74 fi
75 done
76 for f in "${home[@]}" ; do
9ebe6122
MF
77 df="root/${f}"
78 f="${HOME}/${f}"
79 if [[ -e ${f} ]] ; then
80 cp "${f}" "${df}"
81 fi
35fd42a7 82 done
9ebe6122 83
584a84ce
MF
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
9ebe6122 89 fi
35fd42a7
MF
90}
91
92usage() {
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
108main() {
109 bootstrap "$@"
110
c3e618ce 111 local mounts=( proc sys tmp dev dev/pts dev/shm run usr/portage usr/portage/distfiles usr/local/src )
35fd42a7
MF
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 ;;
8b6a28cd 125 --help|-h) usage ;;
35fd42a7
MF
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
d1eee076 150 fi
35fd42a7 151
917d2f98
MF
152 # Doubtful these settings we want to leak into the chroot.
153 unset ROOT PORTAGE_CONFIGROOT LD_LIBRARY_PATH
35fd42a7
MF
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
162main "$@"