2 # Copyright 2011 CyberLeo, All Rights Reserved
3 # http://wiki.cyberleo.net/wiki/CyberLeo/COPYRIGHT
5 # Need root beyond here
6 [ "$(id -u)" -eq 0 ] || exec sudo env J_BASE=${J_BASE} J_NAME=${J_NAME} J_USER=${J_USER:-${USER}} "${0}" "${@}"
8 meh() { printf " \033[1;32m*\033[0m %s%s\n" "${jname:+${jname}: }" "${*}"; }
9 omg() { printf " \033[1;33m*\033[0m %s%s\n" "${jname:+${jname}: }" "${*}"; }
10 wtf() { printf " \033[1;31m*\033[0m %s%s\n" "${jname:+${jname}: }" "${*}"; exit 1; }
12 [ "${*}" ] && printf "%s\n\n" "${*}"
15 <command> <name> [arguments]
17 ls list available chroots
19 status show jail status
21 start prepare an existing chroot for use
23 stop undo what 'start' did
26 shell spawn a shell or command within the chroot
28 eval evaluate a shell command line within the chroot
34 cmd="$(basename "${0}")"
35 jbase="${J_BASE:-$(realpath "$(dirname "${0}")/../")}"
36 jname="${J_NAME:-$(basename "${1}")}" #"
39 # Remove chroot name from argument stack, if passed in
40 [ "${J_NAME}" ] || shift
42 # Propagate certain environment variables; sterilize the rest of the environment
49 # List available jails
51 ( cd "${jbase}"; ls -1 ) | while read jname
57 # Create a new chroot, somehow
59 # Either a debian chroot with debootstrap or a gentoo chroot with stage3 + portage tarballs
60 DEBOOTSTRAP_DIR="$(base)/debootstrap" "${DEBOOTSTRAP_DIR}/debootstrap" --arch=amd64 squeeze "${jdir}"
63 # Figure out and set chroot parameters; needed for all functions that follow
67 # Make sure jname is not empty
70 printf "jerror='%s'\n" "jname empty"
74 # Given a chroot name, find and set up the chroot dir
75 jdir="${jbase}/${jname}"
78 printf "jerror='%s'\n" "not a directory"
83 for shell in /bin/bash /usr/bin/bash /usr/local/bin/bash /bin/sh
85 if [ -f "${jdir}/${shell}" ]
93 printf "jerror='%s'\n", "unable to locate usable shell; is this a jail?"
97 printf "jname='%s' jdir='%s' jshell='%s'\n" "${jname}" "${jdir}" "${jshell}"
101 # Chroot is 'up' if /dev/pts and /proc are mounted
103 jname="${1:-${jname}}"
104 eval "$(j_params "${jname}")"
105 [ "${jerror}" ] && wtf "${jerror}"
106 grep -q "^devpts ${jdir}/dev/pts devpts" /proc/mounts || return 1
107 grep -q "^proc ${jdir}/proc proc" /proc/mounts || return 1
112 [ -z "${1}" ] && set - $(l_ls)
115 j_up "${1}" && meh "$(printf '\033[1;32mup\033[0m')" || meh "$(printf '\033[1;31mdown\033[0m')"
120 # Mount /dev/pts and /proc in the chroot
122 jname="${1:-${jname}}"
123 j_up "${jname}" && return 0
124 eval "$(j_params "${jname}")"
125 meh "starting ${jname} ..."
126 mount -t devpts devpts "${jdir}/dev/pts"
127 mount -t proc proc "${jdir}/proc"
130 # Execute command in chroot
132 jname="${1:-${jname}}"
133 j_up "${jname}" || wtf "chroot not running"
134 eval "$(j_params "${jname}")"
136 env -i ${jenv} /usr/bin/chroot "${jdir}" /bin/su "${juser:-${USER}}" -c "${*}"
140 jname="${1:-${jname}}"
141 eval "$(j_params "${jname}")"
142 j_eval "${jname}" "${jshell} -l"
145 # Unmount /dev/pts and /proc in the chroot
147 jname="${1:-${jname}}"
148 eval "$(j_params "${jname}")"
149 j_up "${jname}" || return 0
150 meh "stopping ${jname} ..."
151 umount "${jdir}/proc"
152 umount "${jdir}/dev/pts"
156 status) j_status "${jname}" "${@}" ;;
157 start) j_start "${jname}" ;;
158 shell|enter) j_shell "${jname}" ;;
159 eval) j_eval "${jname}" "${*}" ;;
160 stop) j_stop "${jname}" ;;