2 # Copyright 2011 CyberLeo, All Rights Reserved
3 # http://wiki.cyberleo.net/wiki/CyberLeo/COPYRIGHT
5 meh() { printf "%s\n" "${*}"; }
6 omg() { printf "%s\n" "${*}"; }
7 wtf() { printf "%s\n" "${*}"; exit 1; }
9 [ "${*}" ] && printf "%s\n\n" "${*}"
11 Usage: <start|enter|stop> <jailname>
16 cmd="$(basename "${0}")"
17 base="$(realpath "$(dirname "${0}")")"
18 jail="$(basename "${1:-DebianChroot}")"
19 jdir="${base}/${jail}"
21 [ -d "${jdir}" ] || wtf "${jail}: not a jail?"
23 # Propagate certain environment variables; sterilize the rest of the environment
29 # Create a new jail (Will not work, since a new jail will not exist and will not pass the 'not a jail' check above)
31 DEBOOTSTRAP_DIR="$(base)/debootstrap" "${DEBOOTSTRAP_DIR}/debootstrap" --arch=amd64 squeeze "${jdir}"
34 # Jail is 'up' if /dev/pts and /proc are mounted
36 grep -q "^devpts ${jdir}/dev/pts devpts" /proc/mounts || return 1
37 grep -q "^proc ${jdir}/proc proc" /proc/mounts || return 1
41 # Mount /dev/pts and /proc in the jail
44 meh "starting ${jail} ..."
45 mount -t devpts devpts "${jdir}/dev/pts"
46 mount -t proc proc "${jdir}/proc"
51 jail_up || wtf "jail not up"
52 meh "entering ${jail} ..."
53 env -i ${env} /usr/bin/chroot "${jdir}" /bin/su "${USER}" -c /bin/bash -l
56 # Unmount /dev/pts and /proc in the jail
59 meh "stopping ${jail} ..."
61 umount "${jdir}/dev/pts"
64 # Need root beyond here
65 [ "$(id -u)" -eq 0 ] || exec sudo env ${env} "${0}" "${@}"