2005-09-03 15:42:28 +00:00
#!/bin/sh
# ugly: this variable is set during port install time
2005-09-22 18:15:14 +00:00
ezjail_prefix=EZJAIL_PREFIX
2005-11-14 20:33:11 +00:00
ezjail_etc=${ezjail_prefix}/etc
2005-11-17 12:28:39 +00:00
ezjail_share=${ezjail_prefix}/share/ezjail
2005-12-18 16:47:21 +00:00
ezjail_examples=${ezjail_prefix}/share/examples/ezjail
2005-11-14 20:33:11 +00:00
ezjail_jailcfgs=${ezjail_etc}/ezjail
2005-09-03 15:42:28 +00:00
2005-11-14 20:33:11 +00:00
if [ -f ${ezjail_etc}/ezjail.conf ]; then
. ${ezjail_etc}/ezjail.conf;
2005-09-03 15:42:28 +00:00
fi
2005-09-12 12:33:42 +00:00
# set defaults
ezjail_jaildir=${ezjail_jaildir:-"/usr/jails"}
ezjail_jailtemplate=${ezjail_jailtemplate:-"$ezjail_jaildir/newjail"}
ezjail_jailbase=${ezjail_jailbase:-"$ezjail_jaildir/basejail"}
2005-09-25 02:24:01 +00:00
ezjail_jailfull=${ezjail_jailfull:-"$ezjail_jaildir/fulljail"}
2005-12-18 16:47:21 +00:00
ezjail_flavours=${ezjail_flavours:-"$ezjail_jaildir/flavours"}
2005-09-12 12:33:42 +00:00
ezjail_sourcetree=${ezjail_sourcetree:-"/usr/src"}
2006-01-16 05:06:04 +00:00
ezjail_portscvsroot=${ezjail_portscvsroot:-":pserver:anoncvs@anoncvs.at.FreeBSD.org:/home/ncvs"}
2005-09-12 12:33:42 +00:00
ezjail_mount_enable=${ezjail_mount_enable:-"YES"}
ezjail_devfs_enable=${ezjail_devfs_enable:-"YES"}
ezjail_devfs_ruleset=${ezjail_devfs_ruleset:-"devfsrules_jail"}
ezjail_procfs_enable=${ezjail_procfs_enable:-"YES"}
ezjail_fdescfs_enable=${ezjail_fdescfs_enable:-"YES"}
2005-11-15 14:31:50 +00:00
exerr () { echo $*; exit 1; }
2005-11-14 20:33:11 +00:00
2005-09-12 12:33:42 +00:00
# check for command
2005-11-14 20:33:11 +00:00
[ "$1" ] || exerr "Usage: `basename $0` [create|delete|list|update] {params}"
2005-09-03 15:42:28 +00:00
case "$1" in
2005-11-14 20:33:11 +00:00
######################## ezjail-admin CREATE ########################
2005-09-03 15:42:28 +00:00
create)
2005-09-12 12:33:42 +00:00
shift
2005-11-14 20:33:11 +00:00
args=`getopt xf:r: $*`
[ $? = 0 ] || exerr 'Usage: ezjail create [-f flavour] [-r jailroot] [-x] jailname jailip';
2005-09-12 12:33:42 +00:00
newjail_root=
2005-11-14 20:33:11 +00:00
newjail_flavour=
2005-09-14 15:22:57 +00:00
newjail_softlink=
2005-09-12 12:33:42 +00:00
newjail_fill="YES"
2005-09-22 11:45:47 +00:00
set -- $args
for arg do
2005-09-12 12:33:42 +00:00
case $arg in
-x) newjail_fill="NO"; shift;;
-r) newjail_root="$2"; shift 2;;
2005-11-14 20:33:11 +00:00
-f) newjail_flavour="$2"; shift 2;;
2005-09-12 12:33:42 +00:00
--) shift; break;;
esac
done;
2005-11-14 20:33:11 +00:00
newjail_name=$1; newjail_ip=$2;
2005-09-12 12:33:42 +00:00
2005-09-24 13:41:35 +00:00
# we need at least a name and an ip for new jail
2006-01-16 05:06:04 +00:00
[ "${newjail_name}" -a "${newjail_ip}" -a $# = 2 ] || exerr 'Usage: ezjail create [-f flavour] [-r jailroot] [-x] jailname jailip'
2005-09-12 12:33:42 +00:00
2005-10-21 20:42:53 +00:00
# check, whether ezjail-update has been called. existence of
# ezjail_jailbase is our indicator
2006-01-16 05:06:04 +00:00
[ -d ${ezjail_jailbase} ] || exerr "Error: base jail does not exist. Please run 'ezjail-admin update' first"
2005-10-21 20:42:53 +00:00
2005-09-14 15:22:57 +00:00
# relative paths don't make sense in rc.scripts
2005-11-22 05:19:47 +00:00
[ ${ezjail_jaildir#/} = ${ezjail_jaildir} ] && exerr "Error: Need an absolute path in ezjail_jaildir, it currently is set to: $ezjail_jaildir"
2005-09-14 15:22:57 +00:00
# jail names must not have names that irritate file systems,
# excluding dots from this list was done intentionally to
# allow foo.com style directory names, however, the jail
# name will be foo_com in most scripts
newjail_name=`echo $newjail_name | tr /~ __`;
2005-11-17 12:36:49 +00:00
newjail_root=${newjail_root:-"${ezjail_jaildir}/${newjail_name}"}
2005-09-25 12:52:04 +00:00
newjail_nname=`echo $newjail_name | tr . _`;
2005-09-14 15:22:57 +00:00
# if jail root specified on command line is not absolute,
# make it absolute inside our jail directory
2005-11-17 12:36:49 +00:00
[ ${newjail_root#/} = ${newjail_root} ] && newjail_root=$ezjail_jaildir/$newjail_root
2005-09-14 15:22:57 +00:00
# if jail root specified on command line does not lie
# within our jail directory, we need to create a softlink
if [ ${newjail_root##${ezjail_jaildir}} = $newjail_root ]; then
newjail_softlink=$ezjail_jaildir/`basename $newjail_root`
2005-11-14 20:33:11 +00:00
[ -e $newjail_softlink -a $newjail_fill = "YES" ] && exerr "Error: an ezjail already exists at $newjail_softlink"
fi
# do some sanity checks on the selected flavour (if any)
2005-12-18 16:47:21 +00:00
if [ "${newjail_flavour}" ]; then
[ -d ${ezjail_flavours}/${newjail_flavour}/ ] || exerr "Error: Flavour config directory ${ezjail_flavours}/${newjail_flavour} not found"
2005-09-14 15:22:57 +00:00
fi
# now take a copy of our template jail
2005-11-14 20:33:11 +00:00
if [ "$newjail_fill" = "YES" ]; then
mkdir -p ${newjail_root} && cd ${ezjail_jailtemplate} && \
2005-11-22 05:19:47 +00:00
find * | cpio -p -v ${newjail_root} > /dev/null
2005-09-14 15:22:57 +00:00
fi
# if a soft link is necessary, create it now
2005-11-14 20:33:11 +00:00
[ "$newjail_softlink" ] && ln -s $newjail_root $newjail_softlink
2005-09-12 12:33:42 +00:00
2005-09-14 15:22:57 +00:00
# if the automount feature is not disabled, create an
# fstab entry for new jail
2005-12-18 16:47:21 +00:00
echo $ezjail_jailbase $newjail_root/basejail nullfs ro 0 0 > /etc/fstab.$newjail_nname
2005-09-14 15:22:57 +00:00
# now, where everything seems to have gone right,
# create control file in ezjails config dir
mkdir -p $ezjail_jailcfgs
2005-12-18 16:47:21 +00:00
echo export jail_${newjail_nname}_hostname=\"${newjail_name}\" > ${ezjail_jailcfgs}/${newjail_nname}
echo export jail_${newjail_nname}_ip=\"${newjail_ip}\" >> ${ezjail_jailcfgs}/${newjail_nname}
echo export jail_${newjail_nname}_rootdir=\"${newjail_root}\" >> ${ezjail_jailcfgs}/${newjail_nname}
echo export jail_${newjail_nname}_exec=\"/bin/sh /etc/rc\" >> ${ezjail_jailcfgs}/${newjail_nname}
echo export jail_${newjail_nname}_mount_enable=\"${ezjail_mount_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname}
echo export jail_${newjail_nname}_devfs_enable=\"${ezjail_devfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname}
echo export jail_${newjail_nname}_devfs_ruleset=\"devfsrules_jail\" >> ${ezjail_jailcfgs}/${newjail_nname}
echo export jail_${newjail_nname}_procfs_enable=\"${ezjail_procfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname}
2005-09-22 11:45:47 +00:00
echo export jail_${newjail_nname}_fdescfs_enable=\"${ezjail_fdescfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname}
2005-09-12 12:33:42 +00:00
2005-12-31 16:40:36 +00:00
# Final steps for flavour installation
if [ "${newjail_flavour}" ]; then
# install files, packages and config to new jail
# user creating, chown and package installation on jails startup
cd ${ezjail_flavours}/${newjail_flavour}
find * | cpio -p -v ${newjail_root} > /dev/null
install -o root -g wheel -m 0755 ${ezjail_share}/ezjail-config.sh ${newjail_root}/etc/rc.d/ezjail-config.sh
echo "Note: Shell scripts installed, flavourizing on jails first startup"
fi
2005-11-19 04:27:56 +00:00
# check, whether IP is configured on a local interface, warn if it isnt
ping -c 1 -m 1 -t 1 -q $newjail_ip > /dev/null
[ $? = 0 ] || echo "Warning: IP $newjail_ip not configured on a local interface"
# check, whether some host system services do listen on the Jails IP
TIFS=$IFS; IFS=_
newjail_listener=`sockstat -4 -l | grep $newjail_ip:[[:digit:]]`
if [ $? = 0 ]; then
echo "Warning: Some services already seem to be listening on IP $newjail_ip"
2005-12-18 16:47:21 +00:00
echo " This may cause some confusion, here they are:"
2005-11-19 04:27:56 +00:00
echo $newjail_listener
fi
newjail_listener=`sockstat -4 -l | grep \*:[[:digit:]]`
if [ $? = 0 ]; then
echo "Warning: Some services already seem to be listening on all IPs"
2005-12-18 16:47:21 +00:00
echo " (including $newjail_ip)"
echo " This may cause some confusion, here they are:"
2005-11-19 04:27:56 +00:00
echo $newjail_listener
fi
IFS=$TIFS
2005-09-03 15:42:28 +00:00
;;
2005-11-14 20:33:11 +00:00
######################## ezjail-admin DELETE ########################
2005-12-18 16:47:21 +00:00
delete)
2005-09-24 13:41:35 +00:00
shift
args=`getopt w $*`
2005-11-17 12:36:49 +00:00
[ $? = 0 ] || exerr 'Usage: ezjail delete [-w] jailname';
2005-09-24 13:41:35 +00:00
oldjail_wipe="NO"
set -- $args
for arg do
case $arg in
-w) oldjail_wipe="YES"; shift;;
--) shift; break;;
esac
done;
2005-09-25 13:31:36 +00:00
oldjail_name=$1;
2005-09-24 13:41:35 +00:00
# we only need name of jail to vanish
2005-11-14 20:33:11 +00:00
[ "$oldjail_name" -a $# = 1 ] || exerr 'Usage: ezjail delete [-w] jailname'
2005-09-24 13:41:35 +00:00
2005-09-25 11:57:21 +00:00
# tidy up jail name the ezjail way
oldjail_nname=`echo $oldjail_name | tr /~. ___`;
2005-09-24 13:41:35 +00:00
# check for existence of jail in our records
2005-11-14 20:33:11 +00:00
[ -f ${ezjail_jailcfgs}/${oldjail_nname} ] || exerr "Error: Nothing known about jail $oldjail_name"
2005-09-24 13:41:35 +00:00
2005-09-25 11:57:21 +00:00
# fetch information about the jail to be gone
# by parsing our records
. ${ezjail_jailcfgs}/${oldjail_nname}
2005-09-25 12:52:04 +00:00
eval oldjail_rootdir=\"\$jail_${oldjail_nname}_rootdir\"
2005-09-25 11:57:21 +00:00
2005-11-09 01:24:59 +00:00
# if jail is still running, refuse to go any further
if [ -f /var/run/jail_${oldjail_nname}.id ]; then
echo "Error: Jail appears to be still running, stop it first"
echo "(/var/run/jail_${oldjail_nname}.id exists)"
exit 1;
fi
2005-09-25 11:57:21 +00:00
# now we know everything we need to let the jail be gone
# remove entry from ezjail resource structure
2005-09-25 12:52:04 +00:00
rm -f ${ezjail_jailcfgs}/${oldjail_nname}
2005-09-25 11:57:21 +00:00
# delete fstab.JAILNAME
2005-09-25 12:52:04 +00:00
rm -f /etc/fstab.$oldjail_nname
2005-09-25 11:57:21 +00:00
# if there is a soft link pointing to the jail root, remove it
2005-09-25 12:52:04 +00:00
oldjail_softlink=$ezjail_jaildir/`basename $oldjail_rootdir`
2005-11-14 20:33:11 +00:00
[ -L $oldjail_softlink ] && rm $oldjail_softlink
2005-09-25 11:57:21 +00:00
# if wiping the jail was requested, remove it
2005-11-14 20:33:11 +00:00
[ $oldjail_wipe = "YES" ] && rm -rf $oldjail_rootdir
2005-09-03 15:42:28 +00:00
;;
2005-11-14 20:33:11 +00:00
######################## ezjail-admin LIST ########################
2005-12-18 16:47:21 +00:00
list)
2005-09-22 18:14:43 +00:00
jail_list=`ls $ezjail_jailcfgs`
for jail in $jail_list; do
. ${ezjail_jailcfgs}/$jail
2005-09-27 13:39:30 +00:00
eval jail_ip=\"\$jail_${jail}_ip\"
eval jail_hostname=\"\$jail_${jail}_hostname\"
eval jail_rootdir=\"\$jail_${jail}_rootdir\"
printf "%-15s %-28s %s\\n" $jail_ip $jail_hostname $jail_rootdir
2005-09-22 18:14:43 +00:00
done
2005-11-17 00:30:15 +00:00
2005-09-03 15:42:28 +00:00
;;
2005-11-14 20:33:11 +00:00
######################## ezjail-admin UPDATE ########################
2005-12-18 16:47:21 +00:00
setup|update)
2005-09-20 22:30:40 +00:00
shift
2006-01-16 05:06:04 +00:00
args=`getopt ipPs: $*`
[ $? = 0 ] || exerr 'Usage: ezjail update [-s sourcetree] [-i] [-pP]'
2005-09-20 22:30:40 +00:00
2005-09-22 11:45:47 +00:00
updatejail_installaction="world"
2006-01-16 05:06:04 +00:00
updatejail_provideports=
2005-09-20 22:30:40 +00:00
2005-09-22 11:45:47 +00:00
set -- $args
for arg do
2005-09-20 22:30:40 +00:00
case $arg in
2005-09-22 11:45:47 +00:00
-i) updatejail_installaction="installworld"; shift;;
2005-09-20 22:30:40 +00:00
-s) ezjail_sourcetree="$2"; shift 2;;
2006-01-16 05:06:04 +00:00
-p) updatejail_provideports="YES"; shift;;
-P) updatejail_provideports="YES"; updatejail_installaction="none"; shift;;
2005-09-20 22:30:40 +00:00
--) shift; break;;
esac
done;
2005-09-03 15:42:28 +00:00
2006-01-16 05:06:04 +00:00
if [ $updatejail_installaction = "none" ]; then
# check, whether ezjail-update has been called. existence of
# ezjail_jailbase is our indicator
[ -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"
else
# Bump the user for some of the most common errors
[ -d ${ezjail_sourcetree} ] || exerr "Cannot find your copy of the FreeBSD source tree in $ezjail_sourcetree."
[ -f ${ezjail_sourcetree}/Makefile ] || exerr "Your source tree in $ezjail_sourcetree seems to be incomplete (Makefile missing)."
cd ${ezjail_sourcetree}
# Normally fulljail should be renamed by past ezjail-admin commands
# However those may have failed
rm -rf ${ezjail_jailfull}; mkdir -p ${ezjail_jailfull}
# make our world
make ${updatejail_installaction} DESTDIR=${ezjail_jailfull}
[ $? = 0 ] || exerr "make ${updatejail_installaction} failed"
# setup world
make distribution DESTDIR=${ezjail_jailfull}
[ $? = 0 ] || exerr "make distribution failed"
cd ${ezjail_jailfull}
# Fill basejail from installed world
mkdir -p ${ezjail_jailbase}/usr ${ezjail_jailbase}/config/pkg
for a in bin lib libexec sbin usr/bin usr/include usr/lib usr/libexec usr/sbin usr/src usr/share; do
find ${a} | cpio -d -p -v ${ezjail_jailbase};
2006-01-18 20:18:22 +00:00
[ $? = 0 ] || exerr "Installation of ${a} failed."
2006-01-16 05:06:04 +00:00
chflags -R noschg ${a}; rm -r ${a}; ln -s /basejail/${a} ${a}
done
mkdir basejail
if [ -d ${ezjail_jailtemplate} ]; then
chflags -R noschg ${ezjail_jailtemplate}_old
rm -rf ${ezjail_jailtemplate}_old
mv ${ezjail_jailtemplate} ${ezjail_jailtemplate}_old
fi
mv ${ezjail_jailfull} ${ezjail_jailtemplate}
# If the default flavour example has not yet been copied, do it now
[ -d ${ezjail_flavours}/default ] || cp -p -r ${ezjail_examples}/default ${ezjail_flavours}/
fi # installaction="none"
# The user may want to have a ports tree in basejail
if [ "$updatejail_provideports" ]; then
# if /usr/ports/CVS exists, assume cvs up is safe
if [ -f ${ezjail_jailbase}/usr/ports/CVS/Root ]; then
echo -n "Updating ports from "; cat ${ezjail_jailbase}/usr/ports/CVS/Root
echo "Gathering local information may take a while."
2006-01-16 06:12:02 +00:00
cd ${ezjail_jailbase}/usr/ports/; cvs up -Pd;
2006-01-16 05:06:04 +00:00
else
echo "Checking out ports from ${ezjail_portscvsroot}"
mkdir -p ${ezjail_jailbase}/usr/ports/
2006-01-16 06:12:02 +00:00
cd ${ezjail_jailbase}/usr/; cvs -d ${ezjail_portscvsroot} co ports;
2006-01-16 05:06:04 +00:00
fi
[ $? = 0 ] || exerr "Updating ports failed."
2006-01-16 06:12:02 +00:00
[ -e ${ezjail_jailtemplate}/usr/ports ] || ln -s /basejail/usr/ports ${ezjail_jailtemplate}/usr/ports
2005-09-03 15:42:28 +00:00
fi
2006-01-16 05:06:04 +00:00
# A ports collection inside jails is hardly useful w/o an appropriate /etc/make.conf
# if we find basejail/usr/ports/Makefile, assume that the admin wants a working make.conf in new jails
if [ -f ${ezjail_jailbase}/usr/ports/Makefile -a -f ${ezjail_examples}/default/etc/make.conf -a \
! -f ${ezjail_jailtemplate}/etc/make.conf ]; then
cp -p ${ezjail_examples}/default/etc/make.conf ${ezjail_jailtemplate}/etc/
echo Note: a non-standard /etc/make.conf was copied to the template jail
echo in order to get the ports collection running inside jails
fi
2005-12-18 16:47:21 +00:00
2005-09-05 02:51:02 +00:00
;;
*)
2005-11-17 12:36:49 +00:00
exerr "Usage: `basename $0` [create|delete|list|update] {params}"
2005-09-03 15:42:28 +00:00
;;
esac