]> git.wh0rd.org - home.git/blob - .bin/custom-chroot
chroot helper
[home.git] / .bin / custom-chroot
1 !/bin/bash -e
2
3 [[ -w / ]] || exec sudo "$0" "$@"
4 [[ $# -eq 0 ]] && set -- /bin/bash -l
5 mounts="proc sys dev dev/pts"
6
7 chroot=${0%/*}
8 cd "${chroot}"
9
10 maybe_mount() {
11 local src=$1 dst=${2:-$1}
12 mkdir -p $d/$m
13 grep -sq "${chroot}/${dst}" /proc/mounts || mount --bind "/${src}" "${chroot}/${dst}"
14 }
15 for m in ${mounts} ; do
16 maybe_mount ${m}
17 done
18
19 get_type() {
20 case $(file "$1") in
21 *x86-64*) echo x86_64;;
22 *"Intel 80386"*) echo i386;;
23 esac
24 }
25 bin_dst=$(get_type bin/bash)
26 bin_src=$(get_type /bin/bash)
27 if [[ ${bin_dst} == ${bin_src} ]] ; then
28 setarch=
29 else
30 setarch="setarch ${bin_dst}"
31 fi
32
33 if [[ ! -L /etc/mtab ]] ; then
34 rm -f etc/mtab
35 ln -sf /proc/mounts etc/mtab
36 fi
37 etc="
38 hosts
39 locale.gen
40 localtime
41 resolv.conf
42 "
43 root="
44 .inputrc
45 .nanorc
46 "
47 for f in \
48 $(printf 'etc/%s ' ${etc}) \
49 $(printf 'root/%s ' ${root}) \
50 ; do
51 cp /${f} ${f}
52 done
53
54 unset LS_COLORS # format changes over time
55 exec ${setarch} chroot "${chroot}" "$@"