From 122cf8bf2c8f978214750a94093ba295556ae381 Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Thu, 28 Apr 2011 10:44:02 -0500 Subject: [PATCH] enter -> j: rename; add command symlinks --- enter | 88 +---------------------------------------------------------- j | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ start | 1 + stop | 1 + 4 files changed, 90 insertions(+), 87 deletions(-) mode change 100755 => 120000 enter create mode 100755 j create mode 120000 start create mode 120000 stop diff --git a/enter b/enter deleted file mode 100755 index 73ef325..0000000 --- a/enter +++ /dev/null @@ -1,87 +0,0 @@ -#!/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="$(realpath "$(dirname "${0}")")" -jail="$(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 -} - -# 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 -start) jail_start ;; -enter) jail_enter ;; -stop) jail_stop ;; -*) pebkac ;; -esac diff --git a/enter b/enter new file mode 120000 index 0000000..0fe2fa5 --- /dev/null +++ b/enter @@ -0,0 +1 @@ +j \ No newline at end of file diff --git a/j b/j new file mode 100755 index 0000000..73ef325 --- /dev/null +++ b/j @@ -0,0 +1,87 @@ +#!/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="$(realpath "$(dirname "${0}")")" +jail="$(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 +} + +# 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 +start) jail_start ;; +enter) jail_enter ;; +stop) jail_stop ;; +*) pebkac ;; +esac diff --git a/start b/start new file mode 120000 index 0000000..0fe2fa5 --- /dev/null +++ b/start @@ -0,0 +1 @@ +j \ No newline at end of file diff --git a/stop b/stop new file mode 120000 index 0000000..0fe2fa5 --- /dev/null +++ b/stop @@ -0,0 +1 @@ +j \ No newline at end of file -- 2.42.0