]> git.wh0rd.org - home.git/blob - .bin/custom-chroot
copy over gdb/git files
[home.git] / .bin / custom-chroot
1 #!/bin/bash -e
2
3 [[ -w / ]] || exec sudo env HOME="$HOME" "$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 *32-bit*PowerPC*) echo ppc;;
49 *64-bit*PowerPC*) echo ppc64;;
50 esac
51 }
52 bin_dst=$(get_type bin/bash)
53 if [[ -n ${bin_dst} ]] && type -P setarch &>/dev/null ; then
54 setarch="setarch ${bin_dst}"
55 fi
56
57 if [[ ! -L etc/mtab ]] ; then
58 rm -f etc/mtab
59 ln -sf /proc/mounts etc/mtab
60 fi
61 etc="
62 hosts
63 locale.gen
64 localtime
65 resolv.conf
66 "
67 home="
68 .inputrc
69 .gdbinit
70 .gitconfig
71 .nanorc
72 "
73 for f in \
74 $(printf 'etc/%s ' ${etc}) \
75 ; do
76 if [ -e "/${f}" ] ; then
77 cp /${f} ${f}
78 fi
79 done
80 for f in ${home} ; do
81 cp ~/${f} root/${f}
82 done
83
84 unset LS_COLORS # format changes over time
85 [[ $# -eq 0 ]] && set -- env HOME=/root /bin/bash -l
86 exec \
87 ${setarch} \
88 chroot "${chroot}" \
89 "$@"