]> git.wh0rd.org - home.git/blame - .bin/custom-chroot
custom-chroot: handle parsic/sparc ABIs
[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;;
95ef1dd2
MF
38 *64-bit*MIPS*) echo mips64;;
39 *32-bit*MIPS*N32*)echo mips64;;
40 *32-bit*MIPS*) echo mips;;
0e502db0
MF
41 *32-bit*PA-RISC*) echo parisc;;
42 *64-bit*PA-RISC*) echo parisc64;;
43 *32-bit*SPARC*) echo sparc;;
44 *64-bit*SPARC*) echo sparc64;;
dc24f4e1
MF
45 esac
46}
35fd42a7
MF
47
48init_chroot() {
49 [[ -w . ]] || return 0
50
51 if [[ ! -L etc/mtab ]] ; then
9ebe6122 52 ln -sfT /proc/mounts etc/mtab
35fd42a7 53 fi
9ebe6122 54 local f dst
35fd42a7
MF
55 local etc=(
56 hosts
57 locale.gen
58 localtime
59 resolv.conf
60 )
61 local home=(
62 .inputrc
63 .gdbinit
64 .gitconfig
65 .nanorc
66 )
67 for f in \
68 $(printf 'etc/%s ' "${etc[@]}") \
69 ; do
9ebe6122
MF
70 if [[ -e /${f} ]] ; then
71 cp "/${f}" "${f}"
35fd42a7
MF
72 fi
73 done
74 for f in "${home[@]}" ; do
9ebe6122
MF
75 df="root/${f}"
76 f="${HOME}/${f}"
77 if [[ -e ${f} ]] ; then
78 cp "${f}" "${df}"
79 fi
35fd42a7 80 done
9ebe6122 81
584a84ce
MF
82 if [[ ! -d root/.git ]] ; then
83 f="${HOME}/.profile.d/aliases.sh"
84 if [[ -e ${f} ]] ; then
85 cat "${f}" > root/.bash_profile
86 fi
9ebe6122 87 fi
35fd42a7
MF
88}
89
90usage() {
91 cat <<-EOF
92 Usage: ${0##*/} [options] [program to run]
93
94 Sets up common mount points and then chroots in and runs a program.
95 If no program is specified, then launch a login shell.
96
97 Options:
98 -u Unmount all paths in the chroot
99 -m <path> Add path to mount list
100 -d <dir> Use <dir> as chroot (defaults to ${0%/*})
101 -h This help screen
102 EOF
103 exit
104}
105
106main() {
107 bootstrap "$@"
108
c3e618ce 109 local mounts=( proc sys tmp dev dev/pts dev/shm run usr/portage usr/portage/distfiles usr/local/src )
35fd42a7
MF
110
111 local chroot=${0%/*}
112 case ${chroot} in
113 .) chroot=${PWD} ;;
114 ./*) chroot=${PWD}/${chroot#./} ;;
115 esac
116
117 local cmd
118 while [[ -n $1 ]] ; do
119 case $1 in
120 -u) cmd='umount' ;;
121 -m) mounts+=( "$2" ); shift ;;
122 -d) chroot=$(realpath "$2"); shift ;;
8b6a28cd 123 --help|-h) usage ;;
35fd42a7
MF
124 -*) echo "${0##*/}: unknown option $1"; exit 1 ;;
125 *) break ;;
126 esac
127 shift
128 done
129 cd "${chroot}"
130
131 case ${cmd} in
132 umount) exec "${0%/*}/umount-tree" -y "${chroot}" ;;
133 esac
134
135 local m
136 for m in "${mounts[@]}" ; do
137 maybe_mount ${m}
138 done
139
140 init_chroot
141
142 local setarch
143 if type -P setarch &>/dev/null ; then
144 local bin_dst=$(get_type bin/bash)
145 if [[ -n ${bin_dst} ]] ; then
146 setarch="setarch ${bin_dst}"
147 fi
d1eee076 148 fi
35fd42a7 149
917d2f98
MF
150 # Doubtful these settings we want to leak into the chroot.
151 unset ROOT PORTAGE_CONFIGROOT LD_LIBRARY_PATH
35fd42a7
MF
152 unset LS_COLORS # format changes over time
153 [[ $# -eq 0 ]] && set -- env HOME=/root /bin/bash -l
154 exec \
155 ${setarch} \
156 chroot "${chroot}" \
157 "$@"
158}
159
160main "$@"