]> CyberLeo.Net >> Repos - CDN/j.git/blob - j
j: ensure j_params works when called for different chroots in the same invocation
[CDN/j.git] / j
1 #!/bin/sh
2 # Copyright 2011 CyberLeo, All Rights Reserved
3 # http://wiki.cyberleo.net/wiki/CyberLeo/COPYRIGHT
4
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}" "${@}"
7
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; }
11 pebkac() {
12   [ "${*}" ] && printf "%s\n\n" "${*}"
13   cat <<EOF
14 Usage:
15   <command> <name> [arguments]
16   list
17   ls      list available chroots
18
19   status  show jail status
20
21   start   prepare an existing chroot for use
22
23   stop    undo what 'start' did
24
25   enter
26   shell   spawn a shell or command within the chroot
27
28   eval    evaluate a shell command line within the chroot
29
30 EOF
31   exit 1
32 }
33
34 cmd="$(basename "${0}")"
35 jbase="${J_BASE:-$(realpath "$(dirname "${0}")/../")}"
36 jname="${J_NAME:-$(basename "${1}")}" #"
37 juser="${J_USER}"
38
39 # Remove chroot name from argument stack, if passed in
40 [ "${J_NAME}" ] || shift
41
42 # Propagate certain environment variables; sterilize the rest of the environment
43 jenv="
44   LANG=${LANG}
45   TERM=${TERM}
46   USER=${USER}
47 "
48
49 # List available jails
50 j_ls() {
51   ( cd "${jbase}"; ls -1 ) | while read jname
52   do
53     something
54   done
55 }
56
57 # Create a new chroot, somehow
58 j_init() {
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}"
61 }
62
63 # Figure out and set chroot parameters; needed for all functions that follow
64 j_params() {
65   ( jname="${1:-jname}"
66
67     # Make sure jname is not empty
68     if [ -z "${jname}" ]
69     then
70       printf "jerror='%s'\n" "jname empty"
71       return 1
72     fi
73
74     # Given a chroot name, find and set up the chroot dir
75     jdir="${jbase}/${jname}"
76     if [ ! -d "${jdir}" ]
77     then
78       printf "jerror='%s'\n" "not a directory"
79       return 1
80     fi
81
82     # Where is the shell?
83     jshell=""
84     for shell in /bin/bash /usr/bin/bash /usr/local/bin/bash /bin/sh
85     do
86       if [ -f "${jdir}/${shell}" ]
87       then
88         jshell=${shell}
89         break
90       fi
91     done
92     if [ -z "${jshell}" ]
93     then
94       printf "jerror='%s'\n" "unable to locate usable shell; is this a jail?"
95       return 1
96     fi
97
98     printf "jerror='' jname='%s' jdir='%s' jshell='%s'\n" "${jname}" "${jdir}" "${jshell}"
99   )
100 }
101
102 # Chroot is 'up' if /dev/pts and /proc are mounted
103 j_up() {
104   jname="${1:-${jname}}"
105   eval "$(j_params "${jname}")"
106   [ "${jerror}" ] && wtf "${jerror}"
107   grep -q "^devpts ${jdir}/dev/pts devpts" /proc/mounts || return 1
108   grep -q "^proc ${jdir}/proc proc" /proc/mounts || return 1
109   return 0
110 }
111
112 j_status() {
113   [ -z "${1}" ] && set - $(l_ls)
114   while [ "${1}" ]
115   do
116     j_up "${1}" && meh "$(printf '\033[1;32mup\033[0m')" || meh "$(printf '\033[1;31mdown\033[0m')"
117     shift
118   done
119 }
120
121 # Mount /dev/pts and /proc in the chroot
122 j_start() {
123   jname="${1:-${jname}}"
124   j_up "${jname}" && return 0
125   eval "$(j_params "${jname}")"
126   meh "starting ${jname} ..."
127   mount -t devpts devpts "${jdir}/dev/pts"
128   mount -t proc proc "${jdir}/proc"
129 }
130
131 # Execute command in chroot
132 j_eval() {
133   jname="${1:-${jname}}"
134   j_up "${jname}" || wtf "chroot not running"
135   eval "$(j_params "${jname}")"
136   shift
137   env -i ${jenv} /usr/bin/chroot "${jdir}" /bin/su "${juser:-${USER}}" -c "${*}"
138 }
139
140 j_shell() {
141   jname="${1:-${jname}}"
142   eval "$(j_params "${jname}")"
143   j_eval "${jname}" "${jshell} -l"
144 }
145
146 # Unmount /dev/pts and /proc in the chroot
147 j_stop() {
148   jname="${1:-${jname}}"
149   eval "$(j_params "${jname}")"
150   j_up "${jname}" || return 0
151   meh "stopping ${jname} ..."
152   umount "${jdir}/proc"
153   umount "${jdir}/dev/pts"
154 }
155
156 case "${cmd}" in
157 status) j_status "${jname}" "${@}" ;;
158 start) j_start "${jname}" ;;
159 shell|enter) j_shell "${jname}" ;;
160 eval) j_eval "${jname}" "${*}" ;;
161 stop) j_stop "${jname}" ;;
162 *) pebkac ;;
163 esac