]> git.wh0rd.org - home.git/blame - .bin/custom-chroot
ssh: switch to ed25519 keys
[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
MF
74
75 f="${HOME}/.profile.d/aliases.sh"
76 if [[ -e ${f} ]] ; then
77 cat "${f}" > root/.bash_profile
78 fi
35fd42a7
MF
79}
80
81usage() {
82 cat <<-EOF
83 Usage: ${0##*/} [options] [program to run]
84
85 Sets up common mount points and then chroots in and runs a program.
86 If no program is specified, then launch a login shell.
87
88 Options:
89 -u Unmount all paths in the chroot
90 -m <path> Add path to mount list
91 -d <dir> Use <dir> as chroot (defaults to ${0%/*})
92 -h This help screen
93 EOF
94 exit
95}
96
97main() {
98 bootstrap "$@"
99
c3e618ce 100 local mounts=( proc sys tmp dev dev/pts dev/shm run usr/portage usr/portage/distfiles usr/local/src )
35fd42a7
MF
101
102 local chroot=${0%/*}
103 case ${chroot} in
104 .) chroot=${PWD} ;;
105 ./*) chroot=${PWD}/${chroot#./} ;;
106 esac
107
108 local cmd
109 while [[ -n $1 ]] ; do
110 case $1 in
111 -u) cmd='umount' ;;
112 -m) mounts+=( "$2" ); shift ;;
113 -d) chroot=$(realpath "$2"); shift ;;
8b6a28cd 114 --help|-h) usage ;;
35fd42a7
MF
115 -*) echo "${0##*/}: unknown option $1"; exit 1 ;;
116 *) break ;;
117 esac
118 shift
119 done
120 cd "${chroot}"
121
122 case ${cmd} in
123 umount) exec "${0%/*}/umount-tree" -y "${chroot}" ;;
124 esac
125
126 local m
127 for m in "${mounts[@]}" ; do
128 maybe_mount ${m}
129 done
130
131 init_chroot
132
133 local setarch
134 if type -P setarch &>/dev/null ; then
135 local bin_dst=$(get_type bin/bash)
136 if [[ -n ${bin_dst} ]] ; then
137 setarch="setarch ${bin_dst}"
138 fi
d1eee076 139 fi
35fd42a7 140
917d2f98
MF
141 # Doubtful these settings we want to leak into the chroot.
142 unset ROOT PORTAGE_CONFIGROOT LD_LIBRARY_PATH
35fd42a7
MF
143 unset LS_COLORS # format changes over time
144 [[ $# -eq 0 ]] && set -- env HOME=/root /bin/bash -l
145 exec \
146 ${setarch} \
147 chroot "${chroot}" \
148 "$@"
149}
150
151main "$@"