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