#!/bin/sh # Copyright 2011 CyberLeo, All Rights Reserved # http://wiki.cyberleo.net/wiki/CyberLeo/COPYRIGHT # Need root beyond here [ "$(id -u)" -eq 0 ] || exec sudo env J_BASE=${J_BASE} J_NAME=${J_NAME} J_USER=${J_USER:-${USER}} "${0}" "${@}" meh() { printf " \033[1;32m*\033[0m %s%s\n" "${jname:+${jname}: }" "${*}"; } omg() { printf " \033[1;33m*\033[0m %s%s\n" "${jname:+${jname}: }" "${*}"; } wtf() { printf " \033[1;31m*\033[0m %s%s\n" "${jname:+${jname}: }" "${*}"; exit 1; } pebkac() { [ "${*}" ] && printf "%s\n\n" "${*}" cat < [arguments] list ls list available chroots status show jail status start prepare an existing chroot for use stop undo what 'start' did enter shell spawn a shell or command within the chroot eval evaluate a shell command line within the chroot EOF exit 1 } cmd="$(basename "${0}")" jbase="${J_BASE:-$(realpath "$(dirname "${0}")/../")}" jname="${J_NAME:-$(basename "${1}")}" #" juser="${J_USER}" # Remove chroot name from argument stack, if passed in [ "${J_NAME}" ] || shift # Propagate certain environment variables; sterilize the rest of the environment jenv=" LANG=${LANG} TERM=${TERM} USER=${USER} " # List available jails j_ls() { ( cd "${jbase}"; ls -1 ) | while read jname do something done } # Create a new chroot, somehow j_init() { # Either a debian chroot with debootstrap or a gentoo chroot with stage3 + portage tarballs DEBOOTSTRAP_DIR="$(base)/debootstrap" "${DEBOOTSTRAP_DIR}/debootstrap" --arch=amd64 squeeze "${jdir}" } # Figure out and set chroot parameters; needed for all functions that follow j_params() { ( jname="${1:-jname}" # Make sure jname is not empty if [ -z "${jname}" ] then printf "jerror='%s'\n" "jname empty" return 1 fi # Given a chroot name, find and set up the chroot dir jdir="${jbase}/${jname}" if [ ! -d "${jdir}" ] then printf "jerror='%s'\n" "not a directory" return 1 fi # Where is the shell? jshell="" for shell in /bin/bash /usr/bin/bash /usr/local/bin/bash /bin/sh do if [ -f "${jdir}/${shell}" ] then jshell=${shell} break fi done if [ -z "${jshell}" ] then printf "jerror='%s'\n" "unable to locate usable shell; is this a jail?" return 1 fi printf "jerror='' jname='%s' jdir='%s' jshell='%s'\n" "${jname}" "${jdir}" "${jshell}" ) } # Chroot is 'up' if /dev/pts and /proc are mounted j_up() { jname="${1:-${jname}}" eval "$(j_params "${jname}")" [ "${jerror}" ] && wtf "${jerror}" grep -q "^devpts ${jdir}/dev/pts devpts" /proc/mounts || return 1 grep -q "^proc ${jdir}/proc proc" /proc/mounts || return 1 return 0 } j_status() { [ -z "${1}" ] && set - $(l_ls) while [ "${1}" ] do j_up "${1}" && meh "$(printf '\033[1;32mup\033[0m')" || meh "$(printf '\033[1;31mdown\033[0m')" shift done } # Mount /dev/pts and /proc in the chroot j_start() { jname="${1:-${jname}}" j_up "${jname}" && return 0 eval "$(j_params "${jname}")" meh "starting ${jname} ..." mount -t devpts devpts "${jdir}/dev/pts" mount -t proc proc "${jdir}/proc" } # Execute command in chroot j_eval() { jname="${1:-${jname}}" j_up "${jname}" || wtf "chroot not running" eval "$(j_params "${jname}")" shift env -i ${jenv} /usr/bin/chroot "${jdir}" /bin/su "${juser:-${USER}}" -c "${*}" } j_shell() { jname="${1:-${jname}}" eval "$(j_params "${jname}")" j_eval "${jname}" "${jshell} -l" } # Unmount /dev/pts and /proc in the chroot j_stop() { jname="${1:-${jname}}" eval "$(j_params "${jname}")" j_up "${jname}" || return 0 meh "stopping ${jname} ..." umount "${jdir}/proc" umount "${jdir}/dev/pts" } case "${cmd}" in status) j_status "${jname}" "${@}" ;; start) j_start "${jname}" ;; shell|enter) j_shell "${jname}" ;; eval) j_eval "${jname}" "${*}" ;; stop) j_stop "${jname}" ;; *) pebkac ;; esac