From 83998bbd5176a6944291acdeaed96b13d86bf987 Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Thu, 28 Apr 2011 10:34:24 -0500 Subject: [PATCH] Init --- .gitignore | 0 enter | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 .gitignore create mode 100755 enter diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/enter b/enter new file mode 100755 index 0000000..fa7e52e --- /dev/null +++ b/enter @@ -0,0 +1,88 @@ +#!/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 ${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 ${env} "${0}" "${@}" + +jail_params + +case "${cmd}" in +start) jail_start ;; +enter) jail_enter ;; +stop) jail_stop ;; +*) pebkac ;; +esac -- 2.42.0