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