]> git.wh0rd.org Git - home.git/blob - .bin/custom-chroot
update chroot more
[home.git] / .bin / custom-chroot
1 #!/bin/bash -e
2
3 [[ -w / ]] || exec sudo "$0" "$@"
4
5 mounts="proc sys dev dev/pts usr/portage usr/portage/distfiles"
6
7 chroot=${0%/*}
8 case ${chroot} in
9         .) chroot=${PWD} ;;
10         ./*) chroot=${PWD}/${chroot#./} ;;
11 esac
12 cd "${chroot}"
13
14 do_umount() {
15         mounts=$(mount | grep ${chroot} | awk '{print $3}' | tac)
16         for m in ${mounts} ; do
17                 echo "unmounting $m"
18                 umount $m
19         done
20         if [[ -z ${mounts} ]] ; then
21                 echo "nothing mounted in ${chroot}"
22         fi
23         exit 0
24 }
25
26 while [[ -n $1 ]] ; do
27         case $1 in
28                 -u) do_umount ;;
29                 -*) echo "unknown option $1"; exit 1 ;;
30                 *)  break ;;
31         esac
32 done
33
34 maybe_mount() {
35         local src=/$1 dst=${chroot}/${2:-$1}
36         [[ -d ${src} ]] || return 0
37         mkdir -p "${dst}"
38         grep -sq "${dst}" /proc/mounts || mount --bind "${src}" "${dst}"
39 }
40 for m in ${mounts} ; do
41         maybe_mount ${m}
42 done
43
44 get_type() {
45         case $(file "$1") in
46                 *x86-64*)        echo x86_64;;
47                 *"Intel 80386"*) echo i386;;
48         esac
49 }
50 bin_dst=$(get_type bin/bash)
51 bin_src=$(get_type /bin/bash)
52 if [[ ${bin_dst} == ${bin_src} ]] ; then
53         setarch=
54 else
55         setarch="setarch ${bin_dst}"
56 fi
57
58 if [[ ! -L etc/mtab ]] ; then
59         rm -f etc/mtab
60         ln -sf /proc/mounts etc/mtab
61 fi
62 etc="
63         hosts
64         locale.gen
65         localtime
66         resolv.conf
67 "
68 home="
69         .inputrc
70         .nanorc
71 "
72 for f in \
73         $(printf 'etc/%s ' ${etc}) \
74 ; do
75         cp /${f} ${f}
76 done
77 for f in ${home} ; do
78         cp ~/${f} root/${f}
79 done
80
81 unset LS_COLORS # format changes over time
82 [[ $# -eq 0 ]] && set -- env HOME=/root /bin/bash -l
83 exec \
84         ${setarch} \
85         chroot "${chroot}" \
86         "$@"