2 # Copyright 2011 CyberLeo, All Rights Reserved
3 # http://wiki.cyberleo.net/wiki/CyberLeo/COPYRIGHT
5 meh() { printf " \033[1;32m*\033[0m %s%s\n" "${jail:+${jail}: }" "${*}"; }
6 omg() { printf " \033[1;33m*\033[0m %s%s\n" "${jail:+${jail}: }" "${*}"; }
7 wtf() { printf " \033[1;31m*\033[0m %s%s\n" "${jail:+${jail}: }" "${*}"; exit 1; }
9 [ "${*}" ] && printf "%s\n\n" "${*}"
11 Usage: <start|enter|stop> <jailname>
16 cmd="$(basename "${0}")"
17 base="${J_BASE:-$(realpath "$(dirname "${0}")/../")}"
18 jail="${J_NAME:-$(basename "${1:-DebianChroot}")}" #"
19 jdir="${base}/${jail}"
22 # Propagate certain environment variables; sterilize the rest of the environment
28 # Create a new jail (Will not work, since a new jail will not exist and will not pass the 'not a jail' check above)
30 DEBOOTSTRAP_DIR="$(base)/debootstrap" "${DEBOOTSTRAP_DIR}/debootstrap" --arch=amd64 squeeze "${jdir}"
33 # Figure out jail parameters
36 for shell in /bin/bash /usr/bin/bash /usr/local/bin/bash /bin/sh
38 if [ -f "${jdir}/${shell}" ]
43 wtf "cannot locate usable shell; is this a real jail?"
47 # Jail is 'up' if /dev/pts and /proc are mounted
49 grep -q "^devpts ${jdir}/dev/pts devpts" /proc/mounts || return 1
50 grep -q "^proc ${jdir}/proc proc" /proc/mounts || return 1
55 jail_up && meh "$(printf '\033[1;32mup\033[0m')" || meh "$(printf '\033[1;31mdown\033[0m')"
58 # Mount /dev/pts and /proc in the jail
61 meh "starting ${jail} ..."
62 mount -t devpts devpts "${jdir}/dev/pts"
63 mount -t proc proc "${jdir}/proc"
68 jail_up || wtf "jail not up"
69 meh "entering ${jail} ..."
70 env -i ${jail_env} /usr/bin/chroot "${jdir}" /bin/su "${USER}" -c "${jail_shell}" -l
73 # Unmount /dev/pts and /proc in the jail
76 meh "stopping ${jail} ..."
78 umount "${jdir}/dev/pts"
81 # Need root beyond here
82 [ "$(id -u)" -eq 0 ] || exec sudo env ${jail_env} "${0}" "${@}"
87 status) jail_status ;;