#!/bin/sh # Copyright 2011 CyberLeo, All Rights Reserved # http://wiki.cyberleo.net/wiki/CyberLeo/COPYRIGHT meh() { printf " \033[1;32m*\033[0m %s%s\n" "${jail:+${jail}: }" "${*}"; } omg() { printf " \033[1;33m*\033[0m %s%s\n" "${jail:+${jail}: }" "${*}"; } wtf() { printf " \033[1;31m*\033[0m %s%s\n" "${jail:+${jail}: }" "${*}"; exit 1; } pebkac() { [ "${*}" ] && printf "%s\n\n" "${*}" cat < EOF exit 1 } cmd="$(basename "${0}")" base="${J_BASE:-$(realpath "$(dirname "${0}")/../")}" jail="${J_NAME:-$(basename "${1:-DebianChroot}")}" #" jdir="${base}/${jail}" jail_shell="" # Propagate certain environment variables; sterilize the rest of the environment jail_env=" TERM=${TERM} USER=${USER} " # Create a new jail (Will not work, since a new jail will not exist and will not pass the 'not a jail' check above) jail_new() { DEBOOTSTRAP_DIR="$(base)/debootstrap" "${DEBOOTSTRAP_DIR}/debootstrap" --arch=amd64 squeeze "${jdir}" } # Figure out jail parameters jail_params() { # Where is the shell? for shell in /bin/bash /usr/bin/bash /usr/local/bin/bash /bin/sh do if [ -f "${jdir}/${shell}" ] then jail_shell=${shell} break fi wtf "cannot locate usable shell; is this a real jail?" done } # Jail is 'up' if /dev/pts and /proc are mounted jail_up() { grep -q "^devpts ${jdir}/dev/pts devpts" /proc/mounts || return 1 grep -q "^proc ${jdir}/proc proc" /proc/mounts || return 1 return 0 } jail_status() { jail_up && meh "$(printf '\033[1;32mup\033[0m')" || meh "$(printf '\033[1;31mdown\033[0m')" } # Mount /dev/pts and /proc in the jail jail_start() { jail_up && return 0 meh "starting ${jail} ..." mount -t devpts devpts "${jdir}/dev/pts" mount -t proc proc "${jdir}/proc" } # Enter jail jail_enter() { jail_up || wtf "jail not up" meh "entering ${jail} ..." env -i ${jail_env} /usr/bin/chroot "${jdir}" /bin/su "${USER}" -c "${jail_shell}" -l } # Unmount /dev/pts and /proc in the jail jail_stop() { jail_up || return 0 meh "stopping ${jail} ..." umount "${jdir}/proc" umount "${jdir}/dev/pts" } # Need root beyond here [ "$(id -u)" -eq 0 ] || exec sudo env ${jail_env} "${0}" "${@}" jail_params case "${cmd}" in status) jail_status ;; start) jail_start ;; enter) jail_enter ;; stop) jail_stop ;; *) pebkac ;; esac