summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xezjail-admin136
1 files changed, 72 insertions, 64 deletions
diff --git a/ezjail-admin b/ezjail-admin
index 23b54f5..f83c347 100755
--- a/ezjail-admin
+++ b/ezjail-admin
@@ -7,9 +7,8 @@ ezjail_share=${ezjail_prefix}/share/ezjail
7ezjail_examples=${ezjail_prefix}/share/examples/ezjail 7ezjail_examples=${ezjail_prefix}/share/examples/ezjail
8ezjail_jailcfgs=${ezjail_etc}/ezjail 8ezjail_jailcfgs=${ezjail_etc}/ezjail
9 9
10if [ -f ${ezjail_etc}/ezjail.conf ]; then 10# read user config
11 . ${ezjail_etc}/ezjail.conf; 11[ -f ${ezjail_etc}/ezjail.conf ] && . ${ezjail_etc}/ezjail.conf
12fi
13 12
14# set defaults 13# set defaults
15ezjail_jaildir=${ezjail_jaildir:-"/usr/jails"} 14ezjail_jaildir=${ezjail_jaildir:-"/usr/jails"}
@@ -26,6 +25,7 @@ ezjail_devfs_ruleset=${ezjail_devfs_ruleset:-"devfsrules_jail"}
26ezjail_procfs_enable=${ezjail_procfs_enable:-"YES"} 25ezjail_procfs_enable=${ezjail_procfs_enable:-"YES"}
27ezjail_fdescfs_enable=${ezjail_fdescfs_enable:-"YES"} 26ezjail_fdescfs_enable=${ezjail_fdescfs_enable:-"YES"}
28 27
28# define our bail out shortcut
29exerr () { echo $*; exit 1; } 29exerr () { echo $*; exit 1; }
30 30
31# check for command 31# check for command
@@ -36,7 +36,7 @@ case "$1" in
36create) 36create)
37 shift 37 shift
38 args=`getopt xf:r: $*` 38 args=`getopt xf:r: $*`
39 [ $? = 0 ] || exerr 'Usage: ezjail create [-f flavour] [-r jailroot] [-x] jailname jailip'; 39 [ $? = 0 ] || exerr 'Usage: ezjail create [-f flavour] [-r jailroot] [-x] jailname jailip'
40 40
41 newjail_root= 41 newjail_root=
42 newjail_flavour= 42 newjail_flavour=
@@ -52,58 +52,62 @@ create)
52 --) shift; break;; 52 --) shift; break;;
53 esac 53 esac
54 done; 54 done;
55 newjail_name=$1; newjail_ip=$2; 55 newjail_name=$1; newjail_ip=$2
56 56
57 # we need at least a name and an ip for new jail 57 # we need at least a name and an ip for new jail
58 [ "${newjail_name}" -a "${newjail_ip}" -a $# = 2 ] || exerr 'Usage: ezjail create [-f flavour] [-r jailroot] [-x] jailname jailip' 58 [ "${newjail_name}" -a "${newjail_ip}" -a $# = 2 ] || exerr 'Usage: ezjail create [-f flavour] [-r jailroot] [-x] jailname jailip'
59 59
60 # check, whether ezjail-update has been called. existence of 60 # check, whether ezjail-update has been called. existence of
61 # ezjail_jailbase is our indicator 61 # ezjail_jailbase is our indicator
62 [ -d ${ezjail_jailbase} ] || exerr "Error: base jail does not exist. Please run 'ezjail-admin update' first" 62 [ -d ${ezjail_jailbase} ] || exerr "Error: base jail does not exist. Please run 'ezjail-admin update' first."
63 63
64 # relative paths don't make sense in rc.scripts 64 # relative paths don't make sense in rc.scripts
65 [ ${ezjail_jaildir#/} = ${ezjail_jaildir} ] && exerr "Error: Need an absolute path in ezjail_jaildir, it currently is set to: $ezjail_jaildir" 65 [ ${ezjail_jaildir#/} = ${ezjail_jaildir} ] && exerr "Error: Need an absolute path in ezjail_jaildir, it currently is set to: ${ezjail_jaildir}."
66 66
67 # jail names must not have names that irritate file systems, 67 # jail names must not irritate file systems, excluding dots
68 # excluding dots from this list was done intentionally to 68 # from this list was done intentionally to permit foo.com
69 # allow foo.com style directory names, however, the jail 69 # style directory names, however, the jail name will be
70 # name will be foo_com in most scripts 70 # foo_com in most scripts
71 newjail_name=`echo $newjail_name | tr /~ __`; 71 newjail_name=`echo ${newjail_name} | tr /~ __`
72 newjail_nname=`echo ${newjail_name} | tr . _`
72 newjail_root=${newjail_root:-"${ezjail_jaildir}/${newjail_name}"} 73 newjail_root=${newjail_root:-"${ezjail_jaildir}/${newjail_name}"}
73 newjail_nname=`echo $newjail_name | tr . _`;
74 74
75 # if jail root specified on command line is not absolute, 75 # if jail root specified on command line is not absolute,
76 # make it absolute inside our jail directory 76 # make it absolute inside our jail directory
77 [ ${newjail_root#/} = ${newjail_root} ] && newjail_root=$ezjail_jaildir/$newjail_root 77 [ ${newjail_root#/} = ${newjail_root} ] && newjail_root=${ezjail_jaildir}/${newjail_root}
78 78
79 # if jail root specified on command line does not lie 79 # if jail root specified on command line does not lie
80 # within our jail directory, we need to create a softlink 80 # within our jail directory, we need to create a softlink
81 if [ ${newjail_root##${ezjail_jaildir}} = $newjail_root ]; then 81 if [ ${newjail_root##${ezjail_jaildir}} = ${newjail_root} ]; then
82 newjail_softlink=$ezjail_jaildir/`basename $newjail_root` 82 newjail_softlink=${ezjail_jaildir}/`basename ${newjail_root}`
83 [ -e $newjail_softlink -a $newjail_fill = "YES" ] && exerr "Error: an ezjail already exists at $newjail_softlink" 83 [ -e ${newjail_softlink} -a ${newjail_fill} = "YES" ] && exerr "Error: an ezjail already exists at ${newjail_softlink}."
84 fi 84 fi
85 85
86 # do some sanity checks on the selected flavour (if any) 86 # do some sanity checks on the selected flavour (if any)
87 if [ "${newjail_flavour}" ]; then 87 if [ "${newjail_flavour}" ]; then
88 [ -d ${ezjail_flavours}/${newjail_flavour}/ ] || exerr "Error: Flavour config directory ${ezjail_flavours}/${newjail_flavour} not found" 88 [ -d ${ezjail_flavours}/${newjail_flavour}/ ] || exerr "Error: Flavour config directory ${ezjail_flavours}/${newjail_flavour} not found."
89 fi 89 fi
90 90
91 # All sanity checks that may lead to errors are hopefully
92 # passed here
93
91 # now take a copy of our template jail 94 # now take a copy of our template jail
92 if [ "$newjail_fill" = "YES" ]; then 95 if [ "$newjail_fill" = "YES" ]; then
93 mkdir -p ${newjail_root} && cd ${ezjail_jailtemplate} && \ 96 mkdir -p ${newjail_root} && cd ${ezjail_jailtemplate} && \
94 find * | cpio -p -v ${newjail_root} > /dev/null 97 find * | cpio -p -v ${newjail_root} > /dev/null
98 [ $? = 0 ] || exerr 'Error: Could not copy template jail'
95 fi 99 fi
96 100
97 # if a soft link is necessary, create it now 101 # if a soft link is necessary, create it now
98 [ "$newjail_softlink" ] && ln -s $newjail_root $newjail_softlink 102 [ "${newjail_softlink}" ] && ln -s ${newjail_root} ${newjail_softlink}
99 103
100 # if the automount feature is not disabled, create an 104 # if the automount feature is not disabled, this
101 # fstab entry for new jail 105 # fstab entry for new jail will be obeyed
102 echo $ezjail_jailbase $newjail_root/basejail nullfs ro 0 0 > /etc/fstab.$newjail_nname 106 echo ${ezjail_jailbase} ${newjail_root}/basejail nullfs ro 0 0 > /etc/fstab.${newjail_nname}
103 107
104 # now, where everything seems to have gone right, 108 # now, where everything seems to have gone right,
105 # create control file in ezjails config dir 109 # create control file in ezjails config dir
106 mkdir -p $ezjail_jailcfgs 110 mkdir -p ${ezjail_jailcfgs}
107 echo export jail_${newjail_nname}_hostname=\"${newjail_name}\" > ${ezjail_jailcfgs}/${newjail_nname} 111 echo export jail_${newjail_nname}_hostname=\"${newjail_name}\" > ${ezjail_jailcfgs}/${newjail_nname}
108 echo export jail_${newjail_nname}_ip=\"${newjail_ip}\" >> ${ezjail_jailcfgs}/${newjail_nname} 112 echo export jail_${newjail_nname}_ip=\"${newjail_ip}\" >> ${ezjail_jailcfgs}/${newjail_nname}
109 echo export jail_${newjail_nname}_rootdir=\"${newjail_root}\" >> ${ezjail_jailcfgs}/${newjail_nname} 113 echo export jail_${newjail_nname}_rootdir=\"${newjail_root}\" >> ${ezjail_jailcfgs}/${newjail_nname}
@@ -122,28 +126,30 @@ create)
122 find * | cpio -p -v ${newjail_root} > /dev/null 126 find * | cpio -p -v ${newjail_root} > /dev/null
123 127
124 install -o root -g wheel -m 0755 ${ezjail_share}/ezjail-config.sh ${newjail_root}/etc/rc.d/ezjail-config.sh 128 install -o root -g wheel -m 0755 ${ezjail_share}/ezjail-config.sh ${newjail_root}/etc/rc.d/ezjail-config.sh
125 echo "Note: Shell scripts installed, flavourizing on jails first startup" 129 echo "Note: Shell scripts installed, flavourizing on jails first startup."
126 fi 130 fi
127 131
132 # For user convenience, some scenarios commonly causing
133 # headaches are checked
134
128 # check, whether IP is configured on a local interface, warn if it isnt 135 # check, whether IP is configured on a local interface, warn if it isnt
129 ping -c 1 -m 1 -t 1 -q $newjail_ip > /dev/null 136 ping -c 1 -m 1 -t 1 -q ${newjail_ip} > /dev/null
130 [ $? = 0 ] || echo "Warning: IP $newjail_ip not configured on a local interface" 137 [ $? = 0 ] || echo "Warning: IP ${newjail_ip} not configured on a local interface."
131 138
132 # check, whether some host system services do listen on the Jails IP 139 # check, whether some host system services do listen on the Jails IP
133 TIFS=$IFS; IFS=_ 140 TIFS=$IFS; IFS=_
134 newjail_listener=`sockstat -4 -l | grep $newjail_ip:[[:digit:]]` 141 newjail_listener=`sockstat -4 -l | grep ${newjail_ip}:[[:digit:]]`
135 if [ $? = 0 ]; then 142 if [ $? = 0 ]; then
136 echo "Warning: Some services already seem to be listening on IP $newjail_ip" 143 echo "Warning: Some services already seem to be listening on IP ${newjail_ip}"
137 echo " This may cause some confusion, here they are:" 144 echo " This may cause some confusion, here they are:"
138 echo $newjail_listener 145 echo ${newjail_listener}
139 fi 146 fi
140
141 newjail_listener=`sockstat -4 -l | grep \*:[[:digit:]]` 147 newjail_listener=`sockstat -4 -l | grep \*:[[:digit:]]`
142 if [ $? = 0 ]; then 148 if [ $? = 0 ]; then
143 echo "Warning: Some services already seem to be listening on all IPs" 149 echo "Warning: Some services already seem to be listening on all IPs."
144 echo " (including $newjail_ip)" 150 echo " (including ${newjail_ip})"
145 echo " This may cause some confusion, here they are:" 151 echo " This may cause some confusion, here they are:"
146 echo $newjail_listener 152 echo ${newjail_listener}
147 fi 153 fi
148 IFS=$TIFS 154 IFS=$TIFS
149 155
@@ -154,8 +160,8 @@ delete)
154 args=`getopt w $*` 160 args=`getopt w $*`
155 [ $? = 0 ] || exerr 'Usage: ezjail delete [-w] jailname'; 161 [ $? = 0 ] || exerr 'Usage: ezjail delete [-w] jailname';
156 162
157 oldjail_wipe="NO" 163 oldjail_wipe=
158 164
159 set -- $args 165 set -- $args
160 for arg do 166 for arg do
161 case $arg in 167 case $arg in
@@ -166,13 +172,13 @@ delete)
166 oldjail_name=$1; 172 oldjail_name=$1;
167 173
168 # we only need name of jail to vanish 174 # we only need name of jail to vanish
169 [ "$oldjail_name" -a $# = 1 ] || exerr 'Usage: ezjail delete [-w] jailname' 175 [ "${oldjail_name}" -a $# = 1 ] || exerr 'Usage: ezjail delete [-w] jailname'
170 176
171 # tidy up jail name the ezjail way 177 # tidy up jail name the ezjail way
172 oldjail_nname=`echo $oldjail_name | tr /~. ___`; 178 oldjail_nname=`echo ${oldjail_name} | tr /~. ___`;
173 179
174 # check for existence of jail in our records 180 # check for existence of jail in our records
175 [ -f ${ezjail_jailcfgs}/${oldjail_nname} ] || exerr "Error: Nothing known about jail $oldjail_name" 181 [ -f ${ezjail_jailcfgs}/${oldjail_nname} ] || exerr "Error: Nothing known about jail ${oldjail_name}."
176 182
177 # fetch information about the jail to be gone 183 # fetch information about the jail to be gone
178 # by parsing our records 184 # by parsing our records
@@ -181,7 +187,7 @@ delete)
181 187
182 # if jail is still running, refuse to go any further 188 # if jail is still running, refuse to go any further
183 if [ -f /var/run/jail_${oldjail_nname}.id ]; then 189 if [ -f /var/run/jail_${oldjail_nname}.id ]; then
184 echo "Error: Jail appears to be still running, stop it first" 190 echo "Error: Jail appears to be still running, stop it first."
185 echo "(/var/run/jail_${oldjail_nname}.id exists)" 191 echo "(/var/run/jail_${oldjail_nname}.id exists)"
186 exit 1; 192 exit 1;
187 fi 193 fi
@@ -191,25 +197,25 @@ delete)
191 rm -f ${ezjail_jailcfgs}/${oldjail_nname} 197 rm -f ${ezjail_jailcfgs}/${oldjail_nname}
192 198
193 # delete fstab.JAILNAME 199 # delete fstab.JAILNAME
194 rm -f /etc/fstab.$oldjail_nname 200 rm -f /etc/fstab.${oldjail_nname}
195 201
196 # if there is a soft link pointing to the jail root, remove it 202 # if there is a soft link pointing to the jail root, remove it
197 oldjail_softlink=$ezjail_jaildir/`basename $oldjail_rootdir` 203 oldjail_softlink=${ezjail_jaildir}/`basename ${oldjail_rootdir}`
198 [ -L $oldjail_softlink ] && rm $oldjail_softlink 204 [ -L ${oldjail_softlink} ] && rm ${oldjail_softlink}
199 205
200 # if wiping the jail was requested, remove it 206 # if wiping the jail was requested, remove it
201 [ $oldjail_wipe = "YES" ] && rm -rf $oldjail_rootdir 207 [ ${oldjail_wipe} = "YES" ] && rm -rf ${oldjail_rootdir}
202 208
203 ;; 209 ;;
204######################## ezjail-admin LIST ######################## 210######################## ezjail-admin LIST ########################
205 list) 211 list)
206 jail_list=`ls $ezjail_jailcfgs` 212 jail_list=`ls ${ezjail_jailcfgs}`
207 for jail in $jail_list; do 213 for jail in ${jail_list}; do
208 . ${ezjail_jailcfgs}/$jail 214 . ${ezjail_jailcfgs}/${jail}
209 eval jail_ip=\"\$jail_${jail}_ip\" 215 eval jail_ip=\"\$jail_${jail}_ip\"
210 eval jail_hostname=\"\$jail_${jail}_hostname\" 216 eval jail_hostname=\"\$jail_${jail}_hostname\"
211 eval jail_rootdir=\"\$jail_${jail}_rootdir\" 217 eval jail_rootdir=\"\$jail_${jail}_rootdir\"
212 printf "%-15s %-28s %s\\n" $jail_ip $jail_hostname $jail_rootdir 218 printf "%-15s %-28s %s\\n" ${jail_ip} ${jail_hostname} ${jail_rootdir}
213 done 219 done
214 220
215 ;; 221 ;;
@@ -233,21 +239,21 @@ setup|update)
233 esac 239 esac
234 done; 240 done;
235 241
236 if [ $updatejail_installaction = "none" ]; then 242 if [ ${updatejail_installaction} = "none" ]; then
237 # check, whether ezjail-update has been called. existence of 243 # check, whether ezjail-update has been called. existence of
238 # ezjail_jailbase is our indicator 244 # ezjail_jailbase is our indicator
239 [ -d ${ezjail_jailbase} ] || exerr "Error: base jail does not exist. You cannot fill base jails ports tree before creating it. Please run 'ezjail-admin update' first" 245 [ -d ${ezjail_jailbase} ] || exerr "Error: base jail does not exist. You cannot fill base jails ports tree before creating it. Please run 'ezjail-admin update' first."
240 else 246 else
241 # Bump the user for some of the most common errors 247 # Bump the user for some of the most common errors
242 [ -d ${ezjail_sourcetree} ] || exerr "Cannot find your copy of the FreeBSD source tree in $ezjail_sourcetree." 248 [ -d ${ezjail_sourcetree} ] || exerr "Cannot find your copy of the FreeBSD source tree in ${ezjail_sourcetree}."
243 [ -f ${ezjail_sourcetree}/Makefile ] || exerr "Your source tree in $ezjail_sourcetree seems to be incomplete (Makefile missing)." 249 [ -e ${ezjail_sourcetree}/Makefile ] || exerr "Your source tree in ${ezjail_sourcetree} seems to be incomplete (Makefile missing)."
244 250
245 cd ${ezjail_sourcetree}
246 # Normally fulljail should be renamed by past ezjail-admin commands 251 # Normally fulljail should be renamed by past ezjail-admin commands
247 # However those may have failed 252 # However those may have failed
248 rm -rf ${ezjail_jailfull}; mkdir -p ${ezjail_jailfull} 253 rm -rf ${ezjail_jailfull}; mkdir -p ${ezjail_jailfull}
249 254
250 # make our world 255 # make our world
256 cd ${ezjail_sourcetree}
251 make ${updatejail_installaction} DESTDIR=${ezjail_jailfull} 257 make ${updatejail_installaction} DESTDIR=${ezjail_jailfull}
252 [ $? = 0 ] || exerr "make ${updatejail_installaction} failed" 258 [ $? = 0 ] || exerr "make ${updatejail_installaction} failed"
253 259
@@ -257,9 +263,9 @@ setup|update)
257 263
258 cd ${ezjail_jailfull} 264 cd ${ezjail_jailfull}
259 # Fill basejail from installed world 265 # Fill basejail from installed world
260 mkdir -p ${ezjail_jailbase}/usr ${ezjail_jailbase}/config/pkg 266 mkdir -p ${ezjail_jailbase}/usr
261 for a in bin lib libexec sbin usr/bin usr/include usr/lib usr/libexec usr/sbin usr/src usr/share; do 267 for a in bin lib libexec sbin usr/bin usr/include usr/lib usr/libexec usr/sbin usr/src usr/share; do
262 find ${a} | cpio -d -p -v ${ezjail_jailbase}; 268 find ${a} | cpio -d -p -v ${ezjail_jailbase}
263 [ $? = 0 ] || exerr "Installation of ${a} failed." 269 [ $? = 0 ] || exerr "Installation of ${a} failed."
264 chflags -R noschg ${a}; rm -r ${a}; ln -s /basejail/${a} ${a} 270 chflags -R noschg ${a}; rm -r ${a}; ln -s /basejail/${a} ${a}
265 done 271 done
@@ -283,25 +289,27 @@ setup|update)
283 if [ -f ${ezjail_jailbase}/usr/ports/CVS/Root ]; then 289 if [ -f ${ezjail_jailbase}/usr/ports/CVS/Root ]; then
284 echo -n "Updating ports from "; cat ${ezjail_jailbase}/usr/ports/CVS/Root 290 echo -n "Updating ports from "; cat ${ezjail_jailbase}/usr/ports/CVS/Root
285 echo "Gathering local information may take a while." 291 echo "Gathering local information may take a while."
286 cd ${ezjail_jailbase}/usr/ports/; cvs up -Pd; 292 cd ${ezjail_jailbase}/usr/ports/; cvs up -Pd
287 else 293 else
288 echo "Checking out ports from ${ezjail_portscvsroot}" 294 echo "Checking out ports from ${ezjail_portscvsroot}"
289 mkdir -p ${ezjail_jailbase}/usr/ports/ 295 mkdir -p ${ezjail_jailbase}/usr/ports/
290 cd ${ezjail_jailbase}/usr/; cvs -d ${ezjail_portscvsroot} co ports; 296 cd ${ezjail_jailbase}/usr/; cvs -d ${ezjail_portscvsroot} co ports
291 fi 297 fi
292
293 [ $? = 0 ] || exerr "Updating ports failed." 298 [ $? = 0 ] || exerr "Updating ports failed."
294 [ -e ${ezjail_jailtemplate}/usr/ports ] || ln -s /basejail/usr/ports ${ezjail_jailtemplate}/usr/ports
295 299
296 fi 300 fi
297 301
298 # A ports collection inside jails is hardly useful w/o an appropriate /etc/make.conf 302 # A ports collection inside jails is hardly useful w/o an appropriate /etc/make.conf
299 # if we find basejail/usr/ports/Makefile, assume that the admin wants a working make.conf in new jails 303 # if we find basejail/usr/ports/Makefile, assume that the admin wants a working make.conf in new jails
300 if [ -f ${ezjail_jailbase}/usr/ports/Makefile -a -f ${ezjail_examples}/default/etc/make.conf -a \ 304 # also a softlink to the ports in basejail is provided
301 ! -f ${ezjail_jailtemplate}/etc/make.conf ]; then 305 if [ -f ${ezjail_jailbase}/usr/ports/Makefile ]; then
302 cp -p ${ezjail_examples}/default/etc/make.conf ${ezjail_jailtemplate}/etc/ 306 # no /usr/ports? link to /basejail/usr/ports
303 echo Note: a non-standard /etc/make.conf was copied to the template jail 307 [ -e ${ezjail_jailtemplate}/usr/ports ] || ln -s /basejail/usr/ports ${ezjail_jailtemplate}/usr/ports
304 echo in order to get the ports collection running inside jails 308
309 if [ -f ${ezjail_examples}/default/etc/make.conf -a ! -f ${ezjail_jailtemplate}/etc/make.conf ]; then
310 cp -p ${ezjail_examples}/default/etc/make.conf ${ezjail_jailtemplate}/etc/
311 echo "Note: a non-standard /etc/make.conf was copied to the template jail in order to get the ports collection running inside jails."
312 fi
305 fi 313 fi
306 314
307 ;; 315 ;;