2005-09-03 15:42:28 +00:00
#!/bin/sh
2006-05-04 14:02:17 +00:00
# $Id$
2005-09-03 15:42:28 +00:00
# ugly: this variable is set during port install time
2005-09-22 18:15:14 +00:00
ezjail_prefix=EZJAIL_PREFIX
2006-05-22 00:20:47 +00:00
ezjail_admin=`basename -- $0`
2007-10-07 21:22:28 +00:00
ezjail_etc="${ezjail_prefix}/etc"
ezjail_share="${ezjail_prefix}/share/ezjail"
ezjail_examples="${ezjail_prefix}/share/examples/ezjail"
ezjail_jailcfgs="${ezjail_etc}/ezjail"
2013-04-24 03:01:19 +00:00
ezjail_snap_date_format="%Y%m%d%H%M"
2005-09-03 15:42:28 +00:00
2006-01-20 19:32:50 +00:00
# read user config
2007-10-07 21:22:28 +00:00
[ -f "${ezjail_etc}/ezjail.conf" ] && . "${ezjail_etc}/ezjail.conf"
2005-09-03 15:42:28 +00:00
2005-09-12 12:33:42 +00:00
# set defaults
2008-10-22 15:54:15 +00:00
: ${ezjail_jaildir="/usr/jails"}
: ${ezjail_jailtemplate="${ezjail_jaildir}/newjail"}
: ${ezjail_jailbase="${ezjail_jaildir}/basejail"}
: ${ezjail_jailfull="${ezjail_jaildir}/fulljail"}
: ${ezjail_jailtemp="${ezjail_jaildir}/ezjailtemp"}
2010-02-08 04:03:47 +00:00
: ${ezjail_flavours_dir="${ezjail_jaildir}/flavours"}
2011-01-20 21:20:36 +00:00
: ${ezjail_archivedir="${ezjail_jaildir}/ezjail_archives"}
2008-10-22 15:54:15 +00:00
: ${ezjail_sourcetree="/usr/src"}
: ${ezjail_uglyperlhack="YES"}
: ${ezjail_default_execute="/usr/bin/login -f root"}
: ${ezjail_mount_enable="YES"}
: ${ezjail_devfs_enable="YES"}
: ${ezjail_devfs_ruleset="devfsrules_jail"}
: ${ezjail_procfs_enable="YES"}
: ${ezjail_fdescfs_enable="YES"}
2010-02-14 23:34:37 +00:00
: ${ezjail_exec_start="/bin/sh /etc/rc"}
2005-09-12 12:33:42 +00:00
2009-08-21 13:18:53 +00:00
: ${ezjail_use_zfs="NO"}
2010-05-26 00:16:21 +00:00
ezjail_dirlist="bin boot lib libexec rescue sbin usr/bin usr/include usr/lib usr/libdata usr/libexec usr/sbin usr/src usr/share"
2008-03-22 11:35:07 +00:00
ezjail_basesystem="base"
2008-09-26 12:16:12 +00:00
# amd64 needs some extra libs
2008-03-22 11:35:07 +00:00
case `uname -p` in amd64) ezjail_dirlist="${ezjail_dirlist} usr/lib32"; ezjail_basesystem="${ezjail_basesystem} lib32";; esac
2006-04-24 10:45:46 +00:00
2006-05-07 22:25:25 +00:00
# Synopsis messages
2015-04-15 00:11:46 +02:00
ezjail_usage_ezjailadmin="${ezjail_admin} v3.4.2\nUsage: ${ezjail_admin} [archive|config|console|create|delete|freeze|install|list|restore|snapshot|troubleshoot|update] {params}"
2008-07-10 18:55:39 +00:00
ezjail_usage_install="Usage: ${ezjail_admin} install [-mMpPsS] [-h host] [-r release]"
2013-04-20 00:19:16 +00:00
ezjail_usage_create="Usage: ${ezjail_admin} create [-xbi] [-f flavour] [-r jailroot] [-s size] [-c bde|eli|zfs] [-C args] [-a archive] [-z parentzfs] jailname jailip"
2011-01-20 21:20:36 +00:00
ezjail_usage_delete="Usage: ${ezjail_admin} delete [-wf] jailname"
2011-03-14 01:31:14 +00:00
ezjail_usage_update="Usage: ${ezjail_admin} update [-s sourcetree|sourceosversion] [-p] (-b|-i|-u|-U|-P)"
2009-12-28 22:09:17 +00:00
ezjail_usage_config="Usage: ${ezjail_admin} config [-r run|norun] [-n newname] [-c cpuset] [-z zfs-datasets] [-f fib] [-i attach|detach|fsck] jailname"
2007-09-15 00:25:52 +00:00
ezjail_usage_console="Usage: ${ezjail_admin} console [-f] [-e command] jailname"
2007-10-01 01:02:45 +00:00
ezjail_usage_archive="Usage: ${ezjail_admin} archive [-Af] [-a archive] [-d archivedir] jailname [jailname...]"
2008-07-08 18:30:06 +00:00
ezjail_usage_restore="Usage: ${ezjail_admin} restore [-f] [-d archivedir] (archive|jailname)..."
2015-04-15 00:11:46 +02:00
ezjail_usage_freeze="Usage: ${ezjail_admin} freeze jailname newflavour"
2014-06-09 23:30:46 +02:00
ezjail_usage_troubleshoot="Usage: ${ezjail_admin} troubleshoot [jailname...]"
2006-05-22 00:20:47 +00:00
ezjail_usage_list="Usage: ${ezjail_admin} list"
2006-05-04 15:06:59 +00:00
2006-04-24 14:31:55 +00:00
################################
# End of variable initialization
#
2006-01-20 19:32:50 +00:00
# define our bail out shortcut
2008-03-22 11:35:07 +00:00
exerr () { echo -e "$*" >&2 ; exit 1; }
2005-11-14 20:33:11 +00:00
2007-01-15 14:03:46 +00:00
# generic attach routine for image jails
attach_images () {
# Create a memory disc from jail image
ezjail_imagedevice=`mdconfig -a -t vnode -f ${ezjail_image}` || exerr "Error: Could not attach memory disc."
# If this is a crypto jail, try to mount it, remind user, which jail
# this is. In this case, the device to mount is
case ${ezjail_imagetype} in
crypto|bde)
2007-10-07 21:22:28 +00:00
echo Attaching bde device for image jail ${ezjail}...
echo gbde attach "/dev/${ezjail_imagedevice}" ${ezjail_attachparams} | /bin/sh
2007-01-15 14:03:46 +00:00
[ $? -eq 0 ] || detach_images keep || exerr "Error: Attaching bde device failed."
# Device to mount is not md anymore
2007-10-07 21:22:28 +00:00
ezjail_device="${ezjail_imagedevice}.bde"
2007-01-15 14:03:46 +00:00
;;
eli)
echo "Attaching eli device for image jail ${ezjail}..."
2010-01-20 15:57:27 +00:00
echo geli attach ${ezjail_attachparams} "/dev/${ezjail_imagedevice}" | /bin/sh
2007-01-15 14:03:46 +00:00
[ $? -eq 0 ] || detach_images keep || exerr "Error: Attaching eli device failed."
# Device to mount is not md anymore
2007-10-07 21:22:28 +00:00
ezjail_device="${ezjail_imagedevice}.eli"
2007-01-15 14:03:46 +00:00
;;
simple)
ezjail_device=${ezjail_imagedevice}
;;
esac
}
2008-07-09 10:37:11 +00:00
# generic mount routine for image jails
mount_images () {
rm -f "${ezjail_devicelink}"
# Attach images by type
attach_images
# Clean image
fsck -t ffs -p "/dev/${ezjail_device}"
mount "/dev/${ezjail_device}" "${ezjail_rootdir}" || detach_images keep || exerr "Error: Could not mount /dev/${ezjail_device} to ${ezjail_rootdir}."
# relink image device
ln -s "/dev/${ezjail_device}" "${ezjail_devicelink}"
}
# generic detach routine for image jails
2006-03-21 16:33:05 +00:00
detach_images () {
2008-07-07 04:29:32 +00:00
# Avoid ending up inside mount point
cd /
2006-03-21 16:33:05 +00:00
# unmount and detach memory disc
2007-10-07 21:22:28 +00:00
if [ "${ezjail_imagedevice}" ]; then
umount "${ezjail_rootdir}" > /dev/null 2> /dev/null
2006-04-26 09:31:37 +00:00
case ${ezjail_imagetype} in
2007-10-07 21:22:28 +00:00
bde) gbde detach "/dev/${ezjail_imagedevice}" > /dev/null;;
eli) geli detach "/dev/${ezjail_imagedevice}" > /dev/null;;
2006-04-26 09:31:37 +00:00
esac
2007-10-07 21:22:28 +00:00
mdconfig -d -u "${ezjail_imagedevice}" > /dev/null
[ "$1" = "keep" ] || rm -f "${ezjail_image}"
2006-03-21 16:33:05 +00:00
fi
2007-10-07 21:22:28 +00:00
# Remove soft link (which acts as a lock)
[ -e "/dev/${ezjail_imagedevice}" ] || rm -f "${ezjail_devicelink}"
2006-05-09 00:26:04 +00:00
# This function is being called in case of error. Keep $? bad
return 1
2006-03-21 16:33:05 +00:00
}
2007-10-07 21:22:28 +00:00
# Find and execute our rc script
start_stop_jail_by_script () {
ezjail_action=$1
2011-04-25 17:42:44 +00:00
case ${ezjail_action} in *start*) ezjail_success_check="-n";; *) ezjail_success_check="-z";; esac
2007-10-07 21:22:28 +00:00
# Try to locate and run ezjails rc.d script
if [ -x "${ezjail_prefix}/etc/rc.d/ezjail" ]; then
2011-04-25 17:42:44 +00:00
"${ezjail_prefix}/etc/rc.d/ezjail" $@
2007-10-07 21:22:28 +00:00
elif [ -x "${ezjail_prefix}/etc/rc.d/ezjail.sh" ]; then
2011-04-25 17:42:44 +00:00
"${ezjail_prefix}/etc/rc.d/ezjail.sh" $@
2007-10-07 21:22:28 +00:00
else
2008-07-10 18:55:39 +00:00
exerr "Error: Could not find ezjail's rc.d script in ${ezjail_prefix}/etc/rc.d/.\n You need to ${ezjail_action} ${ezjail_name} by hand."
2007-10-07 21:22:28 +00:00
fi
# Check for success of our operation
2011-04-25 17:42:44 +00:00
shift
for ezjail; do
fetchjailinfo ${ezjail}
2014-06-09 23:43:29 +02:00
case ${ezjail_config} in *.norun) continue;; esac
2011-04-25 17:42:44 +00:00
[ ${ezjail_success_check} "${ezjail_id}" ] || exerr "Error: Could not ${ezjail_action} $@.\n You need to ${ezjail_action} it by hand."
done
2007-10-07 21:22:28 +00:00
}
2010-01-11 03:06:27 +00:00
# write everything we know about an ezjail to config
writejailinfo () {
ezjail_destconf=$1
ezjail_sourceconf=$2
(
if [ "${ezjail_sourceconf}" ]; then
grep -E ^\# ${ezjail_sourceconf}; echo
else
echo -e "# To specify the start up order of your ezjails, use these lines to\n# create a Jail dependency tree. See rcorder(8) for more details."
echo -e "#\n# PROVIDE: standard_ezjail\n# REQUIRE: \n# BEFORE: \n#\n"
fi
echo export jail_${ezjail_safename}_hostname=\"${ezjail_hostname}\"
echo export jail_${ezjail_safename}_ip=\"${ezjail_ips}\"
echo export jail_${ezjail_safename}_rootdir=\"${ezjail_rootdir}\"
2010-02-14 23:34:37 +00:00
echo export jail_${ezjail_safename}_exec_start=\"${ezjail_exec_start}\"
echo export jail_${ezjail_safename}_exec_stop=\"${ezjail_exec_stop}\"
2010-01-11 03:06:27 +00:00
echo export jail_${ezjail_safename}_mount_enable=\"${ezjail_mount_enable}\"
echo export jail_${ezjail_safename}_devfs_enable=\"${ezjail_devfs_enable}\"
2010-02-14 23:34:37 +00:00
echo export jail_${ezjail_safename}_devfs_ruleset=\"${ezjail_devfs_ruleset}\"
2010-01-11 03:06:27 +00:00
echo export jail_${ezjail_safename}_procfs_enable=\"${ezjail_procfs_enable}\"
echo export jail_${ezjail_safename}_fdescfs_enable=\"${ezjail_fdescfs_enable}\"
echo export jail_${ezjail_safename}_image=\"${ezjail_image}\"
echo export jail_${ezjail_safename}_imagetype=\"${ezjail_imagetype}\"
echo export jail_${ezjail_safename}_attachparams=\"${ezjail_attachparams}\"
echo export jail_${ezjail_safename}_attachblocking=\"${ezjail_attachblocking}\"
echo export jail_${ezjail_safename}_forceblocking=\"${ezjail_forceblocking}\"
echo export jail_${ezjail_safename}_zfs_datasets=\"${ezjail_zfs_datasets}\"
echo export jail_${ezjail_safename}_cpuset=\"${ezjail_cpuset}\"
echo export jail_${ezjail_safename}_fib=\"${ezjail_fib}\"
2013-04-20 01:16:17 +00:00
echo export jail_${ezjail_safename}_parentzfs=\"${ezjail_parentzfs}\"
2013-04-01 15:15:06 +00:00
echo export jail_${ezjail_safename}_parameters=\"${ezjail_parameters}\"
2013-04-14 17:38:54 +00:00
echo export jail_${ezjail_safename}_post_start_script=\"${ezjail_post_start_script}\"
2013-04-24 03:15:58 +00:00
echo export jail_${ezjail_safename}_retention_policy=\"${ezjail_retention_policy}\"
2010-01-11 03:06:27 +00:00
) > "${ezjail_destconf}"
}
2006-04-15 20:20:12 +00:00
# fetch everything we need to know about an ezjail from config
fetchjailinfo () {
ezjail_name=$1
2011-06-14 22:18:29 +00:00
# Clean variables, prevent pollution
2010-02-14 23:34:37 +00:00
unset ezjail_config ezjail_running ezjail_hostname ezjail_rootdir ezjail_image ezjail_imagetype ezjail_imagedevice ezjail_devicelink ezjail_ips ezjail_id ezjail_attached ezjail_device ezjail_device_geom ezjail_exec_start ezjail_exec_stop ezjail_mount_enable ezjail_devfs_enable ezjail_devfs_ruleset ezjail_procfs_enable ezjail_fdescfs_enable
2006-04-15 20:20:12 +00:00
2006-07-15 16:11:22 +00:00
ezjail_safename=`echo -n "${ezjail_name}" | tr -c '[:alnum:]' _`
2006-04-15 20:20:12 +00:00
2008-07-07 04:29:32 +00:00
if [ -z "$2" ]; then
[ -e "${ezjail_jailcfgs}/${ezjail_safename}" ] && ezjail_config="${ezjail_jailcfgs}/${ezjail_safename}"
[ -e "${ezjail_jailcfgs}/${ezjail_safename}.norun" ] && ezjail_config="${ezjail_jailcfgs}/${ezjail_safename}.norun"
else
ezjail_config=$2
fi
2006-04-15 20:20:12 +00:00
[ "${ezjail_config}" ] || return 0
2007-10-07 21:22:28 +00:00
. "${ezjail_config}"
2006-04-15 20:24:46 +00:00
eval ezjail_hostname=\"\$jail_${ezjail_safename}_hostname\"
2010-01-08 22:07:30 +00:00
eval ezjail_ips=\"\$jail_${ezjail_safename}_ip\"
2010-02-14 23:34:37 +00:00
eval ezjail_rootdir=\"\$jail_${ezjail_safename}_rootdir\"
eval ezjail_exec_start=\"\$jail_${ezjail_safename}_exec_start\"
eval ezjail_exec_stop=\"\$jail_${ezjail_safename}_exec_stop\"
# fix backward compatibility issue
eval ezjail_exec=\"\$jail_${ezjail_safename}_exec\"
2010-02-16 22:33:52 +00:00
[ "${ezjail_exec}" -a -z "${ezjail_exec_start}" ] && ezjail_exec_start=${ezjail_exec}
2010-02-14 23:34:37 +00:00
eval ezjail_mount_enable=\"\$jail_${ezjail_safename}_mount_enable\"
eval ezjail_devfs_enable=\"\$jail_${ezjail_safename}_devfs_enable\"
eval ezjail_devfs_ruleset=\"\$jail_${ezjail_safename}_devfs_ruleset\"
eval ezjail_procfs_enable=\"\$jail_${ezjail_safename}_procfs_enable\"
eval ezjail_fdescfs_enable=\"\$jail_${ezjail_safename}_fdescfs_enable\"
2006-04-15 20:20:12 +00:00
eval ezjail_image=\"\$jail_${ezjail_safename}_image\"
eval ezjail_imagetype=\"\$jail_${ezjail_safename}_imagetype\"
2006-05-09 00:26:04 +00:00
eval ezjail_attachparams=\"\$jail_${ezjail_safename}_attachparams\"
eval ezjail_attachblocking=\"\$jail_${ezjail_safename}_attachblocking\"
eval ezjail_forceblocking=\"\$jail_${ezjail_safename}_forceblocking\"
2009-12-28 22:09:17 +00:00
eval ezjail_zfs_datasets=\"\$jail_${ezjail_safename}_zfs_datasets\"
eval ezjail_cpuset=\"\$jail_${ezjail_safename}_cpuset\"
eval ezjail_fib=\"\$jail_${ezjail_safename}_fib\"
2013-04-20 01:16:17 +00:00
eval ezjail_parentzfs=\"\$jail_${ezjail_safename}_parentzfs\"
2013-04-01 15:15:06 +00:00
eval ezjail_parameters=\"\$jail_${ezjail_safename}_parameters\"
2013-04-14 17:38:54 +00:00
eval ezjail_post_start_script=\"\$jail_${ezjail_safename}_post_start_script\"
2013-04-24 03:15:58 +00:00
eval ezjail_retention_policy=\"\$jail_${ezjail_safename}_retention_policy\"
2006-04-15 20:20:12 +00:00
2013-04-20 01:16:17 +00:00
# Pre ezjail-3.3-jails do not have this set
2013-04-24 02:59:55 +00:00
: ${ezjail_parentzfs:=${ezjail_jailzfs}}
2013-04-20 01:16:17 +00:00
2007-10-07 21:22:28 +00:00
ezjail_softlink=${ezjail_jaildir}/`basename -- "${ezjail_rootdir}"`
ezjail_devicelink="${ezjail_rootdir}.device"
2006-05-09 02:00:04 +00:00
2007-10-07 21:22:28 +00:00
if [ "${ezjail_image}" -a -L "${ezjail_devicelink}" ]; then
2006-05-09 02:00:04 +00:00
# Fetch destination of soft link
2007-10-07 21:22:28 +00:00
ezjail_device=`stat -f "%Y" ${ezjail_devicelink}`
ezjail_device_geom=${ezjail_device#/dev/}
# Add this device to the list of devices to be unmounted
case ${ezjail_imagetype} in
crypto|bde) ezjail_imagedevice=${ezjail_device_geom%.bde} ;;
eli) ezjail_imagedevice=${ezjail_device_geom%.eli} ;;
2010-01-24 18:33:09 +00:00
zfs) ezjail_imagedevice='' ;;
2007-10-07 21:22:28 +00:00
*) ezjail_imagedevice=${ezjail_device_geom} ;;
esac
2007-03-23 16:08:43 +00:00
2008-07-07 04:29:32 +00:00
mount -p -v | grep -q -E "^${ezjail_devicelink}[[:space:]]+${ezjail_rootdir}" && ezjail_attached="YES"
mount -p -v | grep -q -E "^${ezjail_device}[[:space:]]+${ezjail_rootdir}" && ezjail_attached="YES"
2007-03-23 16:08:43 +00:00
# Stale device link detected. Remove and clean.
2007-10-07 21:22:28 +00:00
[ -z "${ezjail_attached}" ] && unset ezjail_device && rm -f "${ezjail_devicelink}"
2006-05-09 02:00:04 +00:00
fi
2007-10-07 21:22:28 +00:00
[ -f "/var/run/jail_${ezjail_safename}.id" ] && ezjail_id=`cat /var/run/jail_${ezjail_safename}.id` || return
2013-11-13 16:12:15 +00:00
jls | grep -q -E "^ *${ezjail_id} " || unset ezjail_id
2006-04-15 20:20:12 +00:00
}
2006-04-24 14:31:55 +00:00
# fill the base jail - this function is used by install and update
ezjail_splitworld() {
# Fill basejail from installed world
2008-07-10 18:55:39 +00:00
cd "${ezjail_jailfull}" || exerr "Error: Cant access temporary Jail directory."
2006-04-24 14:31:55 +00:00
2009-05-23 13:09:53 +00:00
if [ "${ezjail_use_zfs}" = "YES" ]; then
2012-09-08 15:09:47 +00:00
ensure_jailzfs
/sbin/zfs create ${ezjail_jailzfs}/basejail
2014-08-04 14:11:39 +02:00
/sbin/zfs snapshot ${ezjail_jailzfs}/basejail@`date +"%C%y%m%d_%H:%M:%S"`
2009-05-23 13:09:53 +00:00
fi
2012-01-28 16:38:48 +00:00
# Remove schg flags from old basejail binaries. The flags
# will be re-added in the cpio for the new basejail
[ -d "${ezjail_jailbase}" ] && chflags -R noschg "${ezjail_jailbase}"
# This mkdir is important, since cpio will create intermediate
# directories with permission 0700 which is bad
2007-10-07 21:22:28 +00:00
mkdir -p "${ezjail_jailbase}/usr"
2006-04-24 14:31:55 +00:00
for dir in ${ezjail_dirlist}; do
2008-07-10 18:55:39 +00:00
find ${dir} | cpio -d -p -v "${ezjail_jailbase}" || exerr "Error: Installation of ${dir} failed."
2006-04-24 14:31:55 +00:00
chflags -R noschg ${dir}; rm -r ${dir}; ln -s /basejail/${dir} ${dir}
done
mkdir basejail
# Try to remove the old template jail
2009-05-23 13:09:53 +00:00
if [ "${ezjail_use_zfs}" = "YES" ]; then
[ -d "${ezjail_jailtemplate}" ] && zfs destroy -R ${ezjail_jailzfs}/newjail && rm -rf "${ezjail_jailtemplate}"
cd ${ezjail_jaildir}
zfs rename ${ezjail_jailzfs}/fulljail ${ezjail_jailzfs}/newjail
else
[ -d "${ezjail_jailtemplate}" ] && chflags -R noschg "${ezjail_jailtemplate}" && rm -rf "${ezjail_jailtemplate}"
mv "${ezjail_jailfull}" "${ezjail_jailtemplate}"
fi
2006-04-24 14:31:55 +00:00
# If the default flavour example has not yet been copied, do it now
2010-02-08 04:03:47 +00:00
[ -d "${ezjail_flavours_dir}/example" ] || mkdir -p "${ezjail_flavours_dir}" && cp -p -R "${ezjail_examples}/example" "${ezjail_flavours_dir}"
2006-04-24 14:31:55 +00:00
2006-04-25 00:38:37 +00:00
# no /usr/ports? link to /basejail/usr/ports
2007-10-07 21:22:28 +00:00
[ -e "${ezjail_jailtemplate}/usr/ports" ] || ln -s /basejail/usr/ports "${ezjail_jailtemplate}/usr/ports"
2006-04-25 00:38:37 +00:00
2010-02-08 03:22:37 +00:00
# A ports collection inside jails is hardly useful w/o an appropriate
# /etc/make.conf.
if [ -f "${ezjail_examples}/example/etc/make.conf" -a ! -f "${ezjail_jailtemplate}/etc/make.conf" ]; then
cp -p "${ezjail_examples}/example/etc/make.conf" "${ezjail_jailtemplate}/etc/"
2013-09-25 17:34:04 +00:00
mkdir -p ${ezjail_jailtemplate}/var/ports/packages ${ezjail_jailtemplate}/var/ports/distfiles
2010-02-08 03:22:37 +00:00
echo "Note: a non-standard /etc/make.conf was copied to the template jail in order to get the ports collection running inside jails."
fi
2006-04-24 14:31:55 +00:00
# The ugly perl hack[tm]. Note: we wont do such things for any given
# port :(
2008-07-08 23:09:51 +00:00
[ "${ezjail_uglyperlhack}" -a ! -L "${ezjail_jailbase}/usr/bin/perl" ] && ln -s /usr/local/bin/perl "${ezjail_jailbase}/usr/bin/perl"
2006-04-24 14:31:55 +00:00
}
# The user may want to have a ports tree in basejail
ezjail_updateports () {
2007-01-16 14:15:21 +00:00
local _portsnap_fetch="fetch"
2013-04-11 16:37:20 +00:00
[ -z "$TERM" -o "$TERM" = "dumb" ] && _portsnap_fetch="cron"
portsnap ${_portsnap_fetch}
[ -d "${ezjail_jailbase}/usr/ports" ] && ezjail_portsnapaction="update"
portsnap -p "${ezjail_jailbase}/usr/ports" ${ezjail_portsnapaction:-"extract"} || exerr "Error: Updating ports failed."
2006-04-24 14:31:55 +00:00
}
2006-05-17 20:22:04 +00:00
# Try to fetch the list of releases the server provides
ezjail_queryftpserver () {
unset _ret
2006-05-17 20:42:13 +00:00
echo -n "Querying your ftp-server... "
2006-05-17 20:22:04 +00:00
TIFS=${IFS}; IFS=
2010-04-08 21:03:25 +00:00
for ezjail_path in pub/FreeBSD/releases pub/FreeBSD/snapshot pub/FreeBSD releases snapshots pub/FreeBSD-Archive/old-releases NO; do
2006-05-17 20:22:04 +00:00
if [ ${ezjail_path} = "NO" ]; then
2012-11-17 21:20:23 +00:00
echo "Warning: I am having problems querying the ftp server you specified (${ezjail_disturi})."
2006-05-17 20:22:04 +00:00
_ret=1; break
fi
2012-11-17 21:20:23 +00:00
ezjail_ftpresponse=`echo ls | ftp "${ezjail_disturi}:${ezjail_path}/${ezjail_installarch}/" 2> /dev/null` 2> /dev/null
2006-05-17 20:22:04 +00:00
if [ $? -eq 0 ]; then
2012-11-17 21:20:23 +00:00
echo -e "The ftp server you specified (${ezjail_disturi}) seems to provide the following builds:\n${ezjail_ftpresponse}"
2012-01-07 03:42:14 +00:00
_ret=0; break
2006-05-17 20:22:04 +00:00
fi
done
IFS=${TIFS}
2006-05-17 20:42:13 +00:00
ezjail_ftpserverqueried="YES"
return ${_ret}
2006-05-17 20:22:04 +00:00
}
2008-03-14 00:17:22 +00:00
# Make a path absolute, if it isn't already
ezjail_makeabsolute ( ) {
2010-01-20 15:57:27 +00:00
eval [ \"\${$1%%[!/]*}\" -o \"\${$1}\" = \"-\" ] && return
2008-07-06 22:24:56 +00:00
[ "${2%%[!/]*}" ] && path=${2} || path=`pwd -P`/${2}
eval export ${1}="${path}/\${$1}"
2008-03-14 00:17:22 +00:00
}
2007-10-08 05:02:50 +00:00
parse_geli_attach_args () {
# create geli(8) attach arguments from geli(8) init arguments:
2011-01-20 21:20:36 +00:00
# -P becomes -p if present, -K newkeyfile becomes -k newkeyfile if present,
# everything else is discarded
2007-10-08 05:02:50 +00:00
# exit values: 0->NO PASSWORD SET, 1->PASSWORD SET
_exit=0
while getopts :bPva:i:K:l:s: arg; do
case ${arg} in
b|v|a|i|l|s);; # ignore these
P) echo -n "-p "; _exit=1 ;;
K) echo -n "-k '$OPTARG' ";;
?) exerr "Error: Processing of attach params failed.";;
esac
done
return ${_exit}
}
parse_gbde_attach_args () {
# create gbde(8) attach arguments from gbde(8) init arguments:
# -L lockfile becomes -l lockfile if present
# -K keyfile becomes -k keyfile if present
# -P passphrase becomes -p passphrase if present
# everything else is discarded
# exit values: 0->NO PASSWORD SET, 1->PASSWORD SET
_exit=0
while getopts :iK:f:L:P: arg; do
case ${arg} in
i|f);; # ignore these
P) echo -n "-p '$OPTARG' "; _exit=1;;
K) echo -n "-k '$OPTARG' ";;
L) echo -n "-l '$OPTARG' ";;
?) exerr "Error: Processing of attach params failed.";;
esac
done
return ${_exit}
}
2010-01-08 22:07:30 +00:00
check_for_zfs () {
2012-09-08 17:53:18 +00:00
# check if the ezjail zfs has been specified
[ -n "${ezjail_jailzfs}" ] || exerr "Error: The variable ezjail_jailzfs needs to point a zfs ezjail can work in.\n Set it in your ezjail.conf."
2010-01-08 22:07:30 +00:00
# check the ZFS version
_zfs_version=`sysctl -nq vfs.zfs.version.spa`
[ -z "${_zfs_version}" -o "${_zfs_version}" -lt 13 ] && exerr "Error: ZFS is not loaded or your ZFS version is not supported."
# check if ZFS is enabled when managing basejail/newjail in ZFS
. /etc/rc.subr
load_rc_config_var zfs zfs_enable
2010-01-08 22:18:31 +00:00
checkyesno ezjail_use_zfs && ! checkyesno zfs_enable && echo "Warning: You should enable ZFS in /etc/rc.conf"
2009-05-24 13:53:42 +00:00
}
2009-05-23 13:09:53 +00:00
2009-05-24 13:53:42 +00:00
check_for_zpool () {
2010-01-08 22:07:30 +00:00
# check for the ZFS zpool to be online
check_for_zfs
_zpoolstatus=`/sbin/zpool list -H -o health ${ezjail_jailzfs%%/*} 2> /dev/null`
2010-01-12 12:55:01 +00:00
[ "${_zpoolstatus}" = "DEGRADED" ] && echo "Warning: Your zpool is in a DEGRADED state."
[ "${_zpoolstatus}" = "ONLINE" -o "${_zpoolstatus}" = "DEGRADED" ] || exerr "Error: Your zpool does not exist or is not online."
2009-05-23 13:09:53 +00:00
}
2010-01-15 13:38:23 +00:00
check_for_zfs_exist () {
# check if the zfs we want to use already exists or not, return 0 if it does and 1 if not
_exit=1
2013-04-20 00:34:58 +00:00
_to_check=$1
_zfs_status=`/sbin/zfs list -H -o name ${_to_check} 2> /dev/null`
[ "${_zfs_status}" = "${_to_check}" ] && _exit=0
2010-01-15 13:38:23 +00:00
return ${_exit}
}
2012-09-08 15:09:47 +00:00
ensure_jailzfs() {
# make sure we do have a zfs to work with, create it if necessary or exit with an error
# if a plain non-empty directory in the place
# ensure that the system has a working zfs
check_for_zpool
# if a zfs already exists, we assume it to be the one we need
_zfs_status=`/sbin/zfs list -H -o name ${ezjail_jailzfs} 2> /dev/null`
[ "${_zfs_status}" = "${ezjail_jailzfs}" ] && return
2013-12-07 03:18:16 +01:00
# if a directory already exists in that place, make sure it is empty, else bump user
2012-09-08 15:09:47 +00:00
[ -d "${ezjail_jaildir}" -a -n "`ls -A ${ezjail_jaildir}`" ] && exerr "Error: Can not create zfs at ${ezjail_jaildir}.\n There is a non-empty directory in the way."
# create all parent file systems if necessary
case "${ezjail_jailzfs}" in */*/*) /sbin/zfs create -p ${ezjail_jailzfs%/*};; esac
# create the ezjail root zfs
# Note, we can not use -p here, because the -o options are ignored
/sbin/zfs create -o mountpoint=${ezjail_jaildir} ${ezjail_zfs_properties} ${ezjail_jailzfs}
}
2013-04-24 03:01:19 +00:00
# implement snapshot retentions
filteroldsnapshots() {
2013-05-10 19:12:50 +00:00
local win repeat bottom in_window snap_id snap_del snap_first first_round_done max_diff
2013-04-24 03:01:19 +00:00
bottom=`date +%s`
unset snap_del first_round_done
snap_id=0
for win in ${ezjail_retention_policy}; do
# split repeat count from window chunk
case ${win} in *x*) repeat=${win%x*}; win=${win#*x};; *) repeat=1;; KEEP) return;; esac
# check for correct value
case ${win} in [0-9]);; [0-9]*[0-9mhdwy]);; *) echo "Unknown window length declaration ${win}"; return ;; esac
# values default to minutes
2016-06-21 22:44:24 +02:00
case ${win} in *h) m=60;; *d) m=1440;; *w) m=10080;; *y) m=525600;; *) m=1;; esac
2013-04-24 03:01:19 +00:00
win=$((${win%[mhdwy]}*m*60))
# innerloop $repeats over windows
while [ $(( repeat-=1 )) -ge 0 ]; do
# Shift bottom of window
bottom=$(( ${bottom} - ${win} ))
2013-05-10 19:12:50 +00:00
unset snap_first
2013-04-24 03:01:19 +00:00
# now loop over parameters
in_window=YES; while [ "${in_window}" ]; do
# When snap_id is required, pop one if possible
if [ "${snap_id}" -eq 0 -a $# -gt 0 ]; then
snap_id=`date -j -f ${ezjail_snap_date_format} $1 +%s`
shift
fi
# is next snapshot before this window? test next window
if [ ${snap_id} -lt ${bottom} ]; then
[ "${first_round_done}" ] || echo /sbin/zfs snapshot -r ${ezjail_zfs}@ez-autosnap-`date +${ezjail_snap_date_format}`
[ "${first_round_done}" ] || /sbin/zfs snapshot -r ${ezjail_zfs}@ez-autosnap-`date +${ezjail_snap_date_format}`
2013-05-10 19:12:50 +00:00
# we remembered the first snapshot in window
# only keep it, if it is newer than the oldest by more than half the window size
if [ "${snap_first}" -a "${snap_del}" -a $(( snap_del - snap_first )) -lt $(( win / 2 )) ]; then
echo /sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_first} +${ezjail_snap_date_format}`
/sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_first} +${ezjail_snap_date_format}`
fi
2013-04-24 03:01:19 +00:00
# Zero marks end of snaps list
[ "${snap_id}" -eq 0 ] && return
unset snap_del in_window
else
2013-05-10 19:12:50 +00:00
if [ "${snap_del}" ]; then
if [ "${snap_first}" ]; then
echo /sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}`
/sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}`
else
snap_first="${snap_del}"
fi
2013-04-24 03:01:19 +00:00
fi
snap_del="${snap_id}"
snap_id=0
fi
first_round_done="YES"
done
done
done
# if out of windows, default to delete the remainder of snaps
for snap_del in ${snap_id} $*; do
echo /sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}`
/sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}`
done
}
2006-04-24 14:31:55 +00:00
#############################
# End of function definitions
2010-02-11 17:32:25 +00:00
# "
2006-04-24 14:31:55 +00:00
2005-09-12 12:33:42 +00:00
# check for command
2006-05-10 13:47:28 +00:00
[ $# -gt 0 ] || exerr ${ezjail_usage_ezjailadmin}
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)
2011-06-14 22:18:29 +00:00
# Clean variables, prevent pollution
2013-04-20 01:16:17 +00:00
unset ezjail_rootdir ezjail_flavours ezjail_softlink ezjail_image ezjail_imagetype ezjail_imageparams ezjail_imagesize ezjail_parentzfs ezjail_device ezjail_devicelink ezjail_config ezjail_attachparams ezjail_exists ezjail_attachblocking ezjail_forceblocking ezjail_sourcedevice ezjail_rootdirempty ezjail_fromarchive ezjail_fromarchive_config
2015-04-15 00:11:46 +02:00
shift; while getopts :f:r:s:xbkic:C:a:A:z: arg; do case ${arg} in
2006-05-04 15:06:59 +00:00
x) ezjail_exists="YES";;
2007-10-07 21:22:28 +00:00
r) ezjail_rootdir=${OPTARG};;
2010-02-08 04:03:47 +00:00
f) ezjail_flavours=${OPTARG};;
2007-10-07 21:22:28 +00:00
a) ezjail_fromarchive=${OPTARG};;
2008-07-07 04:29:32 +00:00
A) ezjail_fromarchive_config=${OPTARG};;
2007-10-07 21:22:28 +00:00
c) ezjail_imagetype=${OPTARG};;
C) ezjail_imageparams=${OPTARG};;
2006-05-07 22:25:25 +00:00
b) ezjail_forceblocking="YES";;
2008-10-22 15:54:15 +00:00
i) : ${ezjail_imagetype="simple"};;
2007-10-07 21:22:28 +00:00
s) ezjail_imagesize=${OPTARG};;
2013-04-10 21:42:56 +00:00
z) ezjail_imagetype="zfs";
2013-04-20 01:16:17 +00:00
ezjail_parentzfs=${OPTARG};;
2015-04-15 00:11:46 +02:00
k) ezjail_keepsnapshot="YES";; # undocumented
2006-05-05 00:59:54 +00:00
?) exerr ${ezjail_usage_create};;
2006-05-03 00:23:49 +00:00
esac; done; shift $(( ${OPTIND} - 1 ))
2010-01-08 22:07:30 +00:00
ezjail_name=$1; ezjail_ips=$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
2010-01-08 22:07:30 +00:00
[ "${ezjail_name}" -a "${ezjail_ips}" -a $# -eq 2 ] || exerr ${ezjail_usage_create}
2006-03-21 16:33:05 +00:00
# check for sanity of settings concerning the image feature
2009-05-23 13:09:53 +00:00
if [ "${ezjail_imagetype}" != "zfs" ]; then
[ -z "${ezjail_imagetype}" -o "${ezjail_exists}" -o "${ezjail_imagesize}" ] || exerr "Error: Image jails need an image size."
fi
2012-09-08 17:53:18 +00:00
# If user wants jails to be in zfs by default, and did not override it on
# the command line, make the jail a zfs one
[ "${ezjail_use_zfs_for_jails}" = "YES" -a ! "${ezjail_imagetype}" ] && ezjail_imagetype=zfs
# check for an active ZFS zpool
[ "${ezjail_imagetype}" = "zfs" ] && check_for_zpool
2006-04-26 09:31:37 +00:00
# check for a sane image type
2009-05-23 13:09:53 +00:00
case ${ezjail_imagetype} in ""|simple|bde|eli|zfs) ;; *) exerr ${ezjail_usage_create};; esac
2006-04-26 09:31:37 +00:00
2006-05-03 00:23:49 +00:00
# check for a sane image size and split it up in blocks
if [ "${ezjail_imagesize}" ]; then
2006-07-16 12:02:46 +00:00
_val=`echo "${ezjail_imagesize}"|tr GMKBWX gmkbwx|sed -Ees:g:km:g -es:m:kk:g -es:k:"*2b":g -es:b:"*128w":g -es:w:"*4 ":g -e"s:(^|[^0-9])0x:\1\0X:g" -ey:x:"*":|bc`
2006-05-03 01:08:21 +00:00
[ $? -eq 0 -a ${_val} -gt 0 ] || exerr "Error: The image size you specified is somehow incomprehensible (you specified ${ezjail_imagesize})."
2006-05-15 19:50:22 +00:00
ezjail_imageblockcount=`echo ${_val} / 1048576 | bc`
ezjail_imagerestbytes=`echo ${_val} % 1048576 | bc`
2006-05-03 00:23:49 +00:00
fi
2006-05-22 00:20:47 +00:00
# check, whether ezjail has been set up correctly. existence of
2005-10-21 20:42:53 +00:00
# ezjail_jailbase is our indicator
2008-07-10 18:55:39 +00:00
[ -d "${ezjail_jailbase}" ] || exerr "Error: base jail does not exist.\n Please run '${ezjail_admin} install' or '${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
2008-07-10 18:55:39 +00:00
[ "${ezjail_jaildir%%[!/]*}" ] || exerr "Error: Need an absolute path in ezjail_jaildir.\n It is currently set to: ${ezjail_jaildir}."
2006-01-20 19:32:50 +00:00
2006-02-09 16:43:24 +00:00
# jail names must not irritate file systems, excluding dots from this list
# was done intentionally to permit foo.com style directory names, however,
# the jail name will be foo_com in most scripts
2006-02-01 19:54:26 +00:00
2007-10-07 21:22:28 +00:00
ezjail_hostname=`echo -n "${ezjail_name}" | tr '/~' '__'`
2006-07-15 16:11:22 +00:00
ezjail_safename=`echo -n "${ezjail_name}" | tr -c '[:alnum:]' _`
2008-10-22 15:54:15 +00:00
: ${ezjail_rootdir="${ezjail_jaildir}/${ezjail_hostname}"}
2007-10-07 21:22:28 +00:00
ezjail_config="${ezjail_jailcfgs}/${ezjail_safename}"
2005-09-14 15:22:57 +00:00
2006-03-06 18:24:50 +00:00
# This scenario really will only lead to real troubles in the 'fulljail'
# case, but I should still explain this to the user and not claim that
# "an ezjail would already exist"
2011-01-20 21:20:36 +00:00
case ${ezjail_hostname} in basejail|newjail|fulljail|flavours|ezjailtemp|ezjail_archives) exerr "Error: Cannot name the jail ${ezjail_hostname}.\n ezjail needs the ${ezjail_hostname} directory for its own administrative purposes.\n Please rename the ezjail.";; esac
2006-03-06 18:24:50 +00:00
2006-02-02 22:55:33 +00:00
# jail names may lead to identical configs, eg. foo.bar.com == foo-bar.com
# so check, whether we might be running into problems
2008-07-13 21:15:42 +00:00
[ -e "${ezjail_config}" -o -e "${ezjail_config}.norun" ] && exerr "Error: An ezjail config already exists at ${ezjail_config}.\n This can happen because ezjail converts non alphanumeric characters in jail names to '_'.\n Please rename the ezjail."
2006-02-02 22:55:33 +00:00
2006-02-09 16:43:24 +00:00
# if jail root specified on command line is not absolute, make it absolute
# inside our jail directory
2008-03-14 00:17:22 +00:00
ezjail_makeabsolute ezjail_rootdir ${ezjail_jaildir}
2005-09-14 15:22:57 +00:00
2006-03-12 23:27:45 +00:00
# if a directory at the specified jail root already exists, refuse to
2006-08-27 00:29:03 +00:00
# install. Empty root dirs are considered okay, sometimes they are
# mount points to be filled by ezjail.
2007-10-07 21:22:28 +00:00
[ -d "${ezjail_rootdir}" ] && [ -z "`ls -I ${ezjail_rootdir}`" ] && ezjail_rootdirempty="YES"
2008-07-10 18:55:39 +00:00
[ -e "${ezjail_rootdir}" -a -z "${ezjail_rootdirempty}" -a -z "${ezjail_exists}" ] && exerr "Error: A file or a non empty directory already exists at the specified jail root ${ezjail_rootdir}.\n Maybe you want to '${ezjail_admin} create -x' an existing jail?\n Please specify another jail root with the -r switch."
2006-02-15 14:36:55 +00:00
2006-03-12 23:27:45 +00:00
# if jail root specified on command line does not lie within our jail
2006-02-09 16:43:24 +00:00
# directory, we need to create a softlink
2006-04-15 20:20:12 +00:00
if [ "${ezjail_rootdir##${ezjail_jaildir}}" = "${ezjail_rootdir}" ]; then
2007-10-07 21:22:28 +00:00
ezjail_softlink=${ezjail_jaildir}/`basename -- "${ezjail_rootdir}"`
2010-01-11 03:06:27 +00:00
[ -e "${ezjail_softlink}" ] && ezjail_softlink=`mktemp -u "${ezjail_softlink}.XXXXXX"`
2005-11-14 20:33:11 +00:00
fi
2010-01-14 02:11:03 +00:00
# if no flavour specified on command line, use default flavour
2010-03-17 10:16:20 +00:00
[ "${ezjail_fromarchive}" -o "${ezjail_exists}" ] || : ${ezjail_flavours=${ezjail_default_flavour}}
2010-01-14 02:11:03 +00:00
2005-11-14 20:33:11 +00:00
# do some sanity checks on the selected flavour (if any)
2010-02-08 04:03:47 +00:00
for ezjail_flavour in ${ezjail_flavours}; do
2010-02-08 05:05:04 +00:00
[ -d "${ezjail_flavours_dir}/${ezjail_flavour}" ] || exerr "Error: Flavour config directory ${ezjail_flavours_dir}/${ezjail_flavour} not found.\n Refer to ${ezjail_admin}s man page for details on flavours."
2010-02-08 04:03:47 +00:00
done
2005-09-14 15:22:57 +00:00
2007-09-26 15:06:40 +00:00
# check for restore circumstances, normally this is invoked by the restore command
2008-07-10 18:55:39 +00:00
[ "${ezjail_fromarchive}" -a "${ezjail_exists}" ] && exerr "Error: You can not restore an archive over an existing jail.\n '${ezjail_admin} delete -w ${ezjail_name}' the old version first."
[ "${ezjail_fromarchive}" -a "${ezjail_flavour}" ] && exerr "Error: Cannot apply flavours to a jail being restored."
[ "${ezjail_fromarchive}" -a "${ezjail_fromarchive}" != "-" -a ! -r "${ezjail_fromarchive}" ] && exerr "Error: No archive found at ${ezjail_fromarchive}."
# Ensure existence of our control directory
mkdir -p "${ezjail_jailcfgs}" || exerr "Error: ezjail can not create its control directory ${ezjail_jailcfgs}."
2007-09-26 15:06:40 +00:00
2006-02-09 16:43:24 +00:00
#
# All sanity checks that may lead to errors are hopefully passed here
#
2006-01-20 19:32:50 +00:00
2007-10-07 21:22:28 +00:00
if [ "${ezjail_imagetype}" ]; then
2006-03-21 16:33:05 +00:00
# Strip trailing slashes from jail root, those would confuse image path
2006-04-15 20:20:12 +00:00
ezjail_image=${ezjail_rootdir%/}; while [ "${ezjail_image}" -a -z "${ezjail_image%%*/}" ]; do ezjail_image=${ezjail_image%/}; done
2008-07-10 18:55:39 +00:00
[ "${ezjail_image}" ] || exerr "Error: Could not determine image file name.\n Something is wrong with the jail root: ${ezjail_rootdir}."
2006-03-21 16:33:05 +00:00
2006-04-22 15:59:37 +00:00
# Location of our image file
2006-05-22 00:20:47 +00:00
ezjail_image="${ezjail_image}.img"
2010-01-08 22:07:30 +00:00
2009-05-23 13:09:53 +00:00
# zfs does not use image files
[ "${ezjail_imagetype}" = "zfs" ] && unset ezjail_image
2006-03-21 16:33:05 +00:00
2006-05-15 20:15:18 +00:00
# Prepare crypto jail so that an attacker cannot guess which blocks
# have been written
2007-10-07 21:22:28 +00:00
case ${ezjail_imagetype} in bde|eli) ezjail_sourcedevice="/dev/random";; simple) ezjail_sourcedevice="/dev/zero";; esac
2006-05-15 20:15:18 +00:00
2009-05-23 13:09:53 +00:00
# If NOT exist and imagetype not ZFS, create image
2009-05-24 14:11:23 +00:00
if [ -z "${ezjail_exists}" -a ${ezjail_imagetype} != "zfs" ]; then
2008-07-10 18:55:39 +00:00
[ -e "${ezjail_image}" ] && exerr "Error: A file exists at ${ezjail_image}.\n Won't overwrite an existing image."
2006-03-21 16:33:05 +00:00
# Now create jail disc image
2006-04-15 20:20:12 +00:00
touch "${ezjail_image}"
2007-09-26 15:06:40 +00:00
echo "Creating jail image ${ezjail_image}. This may take a while."
2006-05-03 01:04:45 +00:00
if [ "${ezjail_imageblockcount}" -gt 0 ]; then
2008-07-10 18:55:39 +00:00
dd if="${ezjail_sourcedevice}" of="${ezjail_image}" bs=1m count=${ezjail_imageblockcount} || exerr "Error: Could not (or not fully) create the image file.\n You might want to check (and possibly remove) the file ${ezjail_image}.\n The image size provided was ${ezjail_imagesize}."
2006-05-03 01:04:45 +00:00
fi
if [ "${ezjail_imagerestbytes}" -gt 0 ]; then
2008-07-10 18:55:39 +00:00
( dd if="${ezjail_sourcedevice}" bs=${ezjail_imagerestbytes} count=1 >> "${ezjail_image}" ) || exerr "Error: Could not (or not fully) create the image file.\n You might want to check (and possibly remove) the file ${ezjail_image}.\n The image size provided was ${ezjail_imagesize}."
2006-05-03 01:04:45 +00:00
fi
2006-03-21 16:33:05 +00:00
2006-05-22 00:20:47 +00:00
# Attach device
2007-10-07 21:22:28 +00:00
ezjail_imagedevice=`mdconfig -a -t vnode -f "${ezjail_image}"`
ezjail_devicelink="${ezjail_rootdir}.device"
2008-07-10 18:55:39 +00:00
[ $? -eq 0 ] || detach_images || exerr "Error: Could not attach image device.\n Command failed was 'mdconfig -a -t vnode -f ${ezjail_image}'."
2007-10-08 02:04:59 +00:00
fi
case ${ezjail_imagetype} in
2007-10-08 02:11:35 +00:00
bde|eli)
# parse imageparams, generate attachparams
ezjail_attachblocking="YES"
if [ "${ezjail_imageparams}" ]; then
2007-10-08 05:02:50 +00:00
ezjail_attachparams=`eval parse_g${ezjail_imagetype}_attach_args ${ezjail_imageparams}` || unset ezjail_attachblocking
2007-10-08 02:11:35 +00:00
fi
case ${ezjail_imagetype} in
bde) init_cmd="gbde init /dev/${ezjail_imagedevice} ${ezjail_imageparams}"
attach_cmd="gbde attach /dev/${ezjail_imagedevice} ${ezjail_attachparams}";;
eli) init_cmd="geli init ${ezjail_imageparams} /dev/${ezjail_imagedevice}"
attach_cmd="geli attach ${ezjail_attachparams} /dev/${ezjail_imagedevice}";;
esac
2007-10-08 02:15:21 +00:00
if [ -z "${ezjail_exists}" ]; then
2007-10-08 02:11:35 +00:00
[ "${ezjail_attachblocking}" ] && echo "Initialising crypto device. You will be asked to enter a new passphrase twice... "
( echo ${init_cmd} | /bin/sh ) || detach_images || exerr "Error: Could not initialise crypto image."
[ "${ezjail_attachblocking}" ] && echo "Attaching crypto device. You will be asked to enter the new passphrase... "
( echo ${attach_cmd} | /bin/sh ) || detach_images || exerr "Error: Could not attach crypto image."
fi
ezjail_device="${ezjail_imagedevice}.${ezjail_imagetype}"
;;
simple)
ezjail_device=${ezjail_imagedevice}
;;
2010-01-15 13:38:23 +00:00
zfs)
2013-04-20 01:16:17 +00:00
: ${ezjail_parentzfs=${ezjail_jailzfs}}
2010-01-15 13:38:23 +00:00
if [ -z "${ezjail_exists}" ]; then
2012-09-08 17:53:18 +00:00
[ "${ezjail_imagesize}" ] && ezjail_zfs_jail_properties="${ezjail_zfs_jail_properties} -o quota=${ezjail_imagesize}"
2010-01-15 13:38:23 +00:00
[ -d "${ezjail_jaildir}/${ezjail_hostname}" ] && exerr "Error: Could not create jail root mount point ${ezjail_rootdir}"
2012-09-08 15:09:47 +00:00
2013-04-20 01:16:17 +00:00
check_for_zfs_exist "${ezjail_parentzfs}" || exerr "Error: The parent zfs dataset does not exist.\n Use 'zfs create -p ${ezjail_parentzfs}' to create it."
2013-04-20 00:34:58 +00:00
2013-04-20 01:16:17 +00:00
/sbin/zfs create -o mountpoint=${ezjail_rootdir} ${ezjail_zfs_jail_properties} ${ezjail_parentzfs}/${ezjail_hostname}
2010-01-15 13:38:23 +00:00
else
2013-04-20 01:16:17 +00:00
check_for_zfs_exist "${ezjail_parentzfs}/${ezjail_hostname}" || exerr "Error: The existing destination is not a ZFS filesystem."
2009-05-23 13:09:53 +00:00
fi
;;
2007-10-08 02:04:59 +00:00
esac
2006-03-21 16:33:05 +00:00
2009-05-24 14:11:23 +00:00
if [ -z "${ezjail_exists}" -a ${ezjail_imagetype} != "zfs" ]; then
2006-03-21 16:33:05 +00:00
# Format memory image
2007-10-07 21:22:28 +00:00
newfs -U "/dev/${ezjail_device}" || detach_images || exerr "Error: Could not newfs /dev/${ezjail_device}."
2006-03-21 16:33:05 +00:00
# Create mount point and mount
2007-10-07 21:22:28 +00:00
mkdir -p "${ezjail_rootdir}" || detach_images || exerr "Error: Could not create jail root mount point ${ezjail_rootdir}."
mount "/dev/${ezjail_device}" "${ezjail_rootdir}" || detach_images || exerr "Error: Could not mount /dev/${ezjail_device} to ${ezjail_root}."
2006-03-21 16:33:05 +00:00
else
2008-07-10 18:55:39 +00:00
if [ -e "${ezjail_rootdir}" -a ! -d "${ezjail_rootdir}" ]; then
2010-01-08 22:07:30 +00:00
[ "${ezjail_rootdir%%*.img}" ] || exerr "Error: Could not create mount point for your jails image.\n A file exists at its location.\n Try '${ezjail_admin} create -x -r ${ezjail_rootdir%%.img} ${ezjail_name} ${ezjail_ips}' instead."
2008-07-10 18:55:39 +00:00
exerr "Error: Could not create mount point for your jails image.\n A file exists at its location."
fi
2007-10-07 21:22:28 +00:00
[ -d "${ezjail_rootdir}" ] || mkdir -p "${ezjail_rootdir}"
2006-03-21 16:33:05 +00:00
fi
fi
2007-10-02 20:20:05 +00:00
if [ "${ezjail_fromarchive}" ]; then
2008-03-14 00:17:22 +00:00
unset ezjail_archive_opt
ezjail_makeabsolute ezjail_fromarchive
[ "${ezjail_fromarchive}" = "-" ] && unset ezjail_archive_opt || ezjail_archive_opt="-f ${ezjail_fromarchive}"
2008-07-06 18:17:36 +00:00
mkdir -p "${ezjail_rootdir}" && cd "${ezjail_rootdir}" && pax -rz -pe ${ezjail_archive_opt} -s:^ezjail:.: ezjail/*
2007-10-07 21:22:28 +00:00
[ $? -eq 0 ] || detach_images || exerr "Error: Could not extract archive from ${ezjail_fromarchive}."
2007-09-26 15:06:40 +00:00
elif [ -z "${ezjail_exists}" ]; then
# now take a copy of our template jail
2009-05-24 14:11:23 +00:00
if [ "${ezjail_imagetype}" = "zfs" -a "${ezjail_use_zfs}" = "YES" ]; then
2009-05-23 13:09:53 +00:00
# create ZFS filesystem first when using ZFS
2015-04-15 00:11:46 +02:00
/sbin/zfs snapshot ${ezjail_jailzfs}/newjail@_vanilla_newjail_
/sbin/zfs send ${ezjail_jailzfs}/newjail@_vanilla_newjail_ | zfs receive -F ${ezjail_parentzfs}/${ezjail_hostname}
[ -z "${ezjail_keepsnapshot}" ] && /sbin/zfs destroy ${ezjail_parentzfs}/${ezjail_hostname}@_vanilla_newjail_
/sbin/zfs destroy ${ezjail_jailzfs}/newjail@_vanilla_newjail_
2009-05-23 13:09:53 +00:00
else
mkdir -p "${ezjail_rootdir}" && cd "${ezjail_jailtemplate}" && find . | cpio -p -v "${ezjail_rootdir}" > /dev/null
fi
2007-10-07 21:22:28 +00:00
[ $? -eq 0 ] || detach_images || exerr "Error: Could not copy template jail."
2005-09-14 15:22:57 +00:00
fi
# if a soft link is necessary, create it now
2010-01-11 03:06:27 +00:00
[ "${ezjail_softlink}" ] && ln -fs "${ezjail_rootdir}" "${ezjail_softlink}"
2005-09-12 12:33:42 +00:00
2006-02-09 16:43:24 +00:00
# if the automount feature is not disabled, this fstab entry for new jail
# will be obeyed
2006-04-15 20:20:12 +00:00
echo -n > /etc/fstab.${ezjail_safename}
2009-05-24 13:53:42 +00:00
if [ "${ezjail_imagetype}" -a "${ezjail_imagetype}" != "zfs" ] ; then
2009-05-23 13:09:53 +00:00
echo ${ezjail_devicelink} ${ezjail_rootdir} ufs rw 0 0 >> "/etc/fstab.${ezjail_safename}"
fi
2007-10-07 21:22:28 +00:00
echo ${ezjail_jailbase} ${ezjail_rootdir}/basejail nullfs ro 0 0 >> "/etc/fstab.${ezjail_safename}"
2005-09-14 15:22:57 +00:00
2006-05-04 15:06:59 +00:00
# now, where everything seems to have gone right, create control file in
2006-02-09 16:43:24 +00:00
# ezjails config dir
2010-01-11 03:06:27 +00:00
writejailinfo "${ezjail_config}" "${ezjail_fromarchive_config}"
2006-04-15 20:20:12 +00:00
# Final steps for flavour installation
2010-02-08 04:03:47 +00:00
if [ -z "${ezjail_exists}" ]; then
2010-02-08 05:05:04 +00:00
installed_flavours=0
2010-02-08 04:03:47 +00:00
for ezjail_flavour in ${ezjail_flavours}; do
# install files and config to new jail
cd "${ezjail_flavours_dir}/${ezjail_flavour}" && find . | cpio -p -u -v "${ezjail_rootdir}" > /dev/null
[ $? -eq 0 ] || echo "Warning: Could not fully install flavour ${ezjail_flavour}."
# if the packages are links and not files we have to copy them now
find "${ezjail_rootdir}/pkg/" -type l -exec cp -r -f {} {}.ezjail \; -exec mv {}.ezjail {} \;
2011-11-07 04:33:27 +00:00
# If an old style flavour config is found, make it auto run on jails startup
2010-02-08 04:03:47 +00:00
if [ -f "${ezjail_rootdir}/ezjail.flavour" ]; then
chmod 0755 "${ezjail_rootdir}/ezjail.flavour"
2012-01-09 23:40:45 +00:00
mv "${ezjail_rootdir}/ezjail.flavour" "${ezjail_rootdir}/ezjail.flavour".`printf %04d ${installed_flavours}`
[ $(( installed_flavours+=1 )) == 1 ] && echo "Note: Shell scripts for flavour ${ezjail_flavour} installed, flavourizing on jails first startup."
2010-02-08 04:03:47 +00:00
2010-02-08 05:05:04 +00:00
cat > "${ezjail_rootdir}/etc/rc.d/ezjail-config" <<"EOF"
2010-02-08 04:03:47 +00:00
#!/bin/sh
#
# BEFORE: DAEMON
2011-11-07 04:33:27 +00:00
# PROVIDE: ezjail-config
2010-02-08 04:03:47 +00:00
#
2012-01-07 05:16:48 +00:00
name=ezjail-config
start_cmd=flavour_setup
flavour_setup() {
# N.B.: Do NOT rm $0, it points to /etc/rc
rm -f "/etc/rc.d/ezjail-config"
for ezjail_flavour in /ezjail.flavour.*; do
[ -x "${ezjail_flavour}" ] && "${ezjail_flavour}"
rm -f "${ezjail_flavour}"
done
}
run_rc_command "$1"
2010-02-08 04:03:47 +00:00
EOF
2011-11-07 04:33:27 +00:00
chmod 0755 "${ezjail_rootdir}/etc/rc.d/ezjail-config"
fi
2010-02-08 04:03:47 +00:00
done
2005-12-31 16:40:36 +00:00
fi
2006-03-21 16:33:05 +00:00
# Detach (crypto and) memory discs
2006-05-09 00:26:04 +00:00
detach_images keep
2006-03-21 16:33:05 +00:00
2006-02-10 16:05:58 +00:00
#
2006-02-09 16:43:24 +00:00
# For user convenience some scenarios commonly causing headaches are checked
2006-02-10 16:05:58 +00:00
#
2006-01-20 19:32:50 +00:00
2010-01-08 22:07:30 +00:00
TIFS=${IFS}; IFS=,
2013-04-14 18:40:36 +00:00
for ezjail_ip_in in ${ezjail_ips}; do
2013-04-14 17:49:55 +00:00
# From 9.0 IP addresses can be prefixed by their interface, for now ignore
# the prefix
2013-04-14 18:40:36 +00:00
ezjail_ip="${ezjail_ip_in#*|}"
# If the IP address is not automatically configured, test if it is configured
# on a local interface
if [ "${ezjail_ip}" = "${ezjail_ip_in}" ]; then
case ${ezjail_ip} in *.*.*.*) _ping=ping;; *) _ping=ping6;; esac
# check, whether IP is configured on a local interface, warn if it isnt
${_ping} -S ${ezjail_ip} -q -c 1 localhost >/dev/null 2>/dev/null
[ $? -eq 0 ] || echo "Warning: IP ${ezjail_ip} not configured on a local interface."
fi
2005-11-19 04:27:56 +00:00
2010-01-08 22:07:30 +00:00
# check, whether some host system services do listen on the Jails IP
IFS=_
2011-03-07 03:57:49 +00:00
ezjail_listener=`sockstat -64l | grep "${ezjail_ip}:[[:digit:]]"`
2010-01-08 22:07:30 +00:00
[ $? -eq 0 ] && echo -e "Warning: Some services already seem to be listening on IP ${ezjail_ip}\n This may cause some confusion, here they are:\n${ezjail_listener}"
2006-02-08 10:56:35 +00:00
2011-03-07 03:57:49 +00:00
# collect list of jail ids with our ip address in their ip address set.
# Add none meaning the host system. Prepare this list as argument for pgrep
# by prepending -j to each jid
IFS=${TIFS}
2012-05-06 00:58:06 +00:00
_freebsd_version=`uname -r`
if [ ${_freebsd_version%%.*} -gt 7 ]; then
jail_ids=`( echo none=
jls -n | sed -E -n s/'.*jid=([0-9]+).*ip4\.addr=([0-9.,]+).*'/'\1=\2'/p | grep -Ee "${ezjail_ip}(,|$)"
jls -n | sed -E -n s/'.*jid=([0-9]+).*ip6\.addr=([0-9a-f:,]+).*'/'\1=\2'/p | grep -Ee "${ezjail_ip}(,|$)"
) | cut -d= -f1 | sed s/^/-j/`
2011-03-07 03:57:49 +00:00
2012-05-06 00:58:06 +00:00
# Fetch all corresponding process ids for all matching jail
jail_pids=`pgrep $jail_ids`
2011-03-07 03:57:49 +00:00
2012-05-06 00:58:06 +00:00
# expand pids to form a greppable expression
jail_grep=`echo $jail_pids | sed -E -e"s/ / )|( /g" -e"s/^/( /" -e"s/$/ )/"`
2011-03-07 03:57:49 +00:00
2012-05-06 00:58:06 +00:00
IFS=_
else
jail_grep=.
fi
2012-01-14 20:00:55 +00:00
ezjail_listener=`sockstat -46l | grep -E -e "\*:[[:digit:]]" | grep -E -e "${jail_grep}"`
2010-01-08 22:07:30 +00:00
[ $? -eq 0 ] && echo -e "Warning: Some services already seem to be listening on all IP, (including ${ezjail_ip})\n This may cause some confusion, here they are:\n${ezjail_listener}"
IFS=,
done
2006-02-06 02:29:42 +00:00
IFS=${TIFS}
2006-05-04 15:06:59 +00:00
2009-05-24 14:11:23 +00:00
[ "${ezjail_imagetype}" -a "${ezjail_imagetype}" != "zfs" ] && echo "Note: To administrate your image jail, attach it using the '${ezjail_admin} config -i attach ${ezjail_hostname}' command."
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)
2011-06-14 22:18:29 +00:00
# Clean variables, prevent pollution
2007-10-07 21:22:28 +00:00
unset ezjail_wipeme ezjail_forcestop
2006-01-20 19:32:50 +00:00
2007-10-07 23:11:24 +00:00
shift; while getopts :wf arg; do case ${arg} in
2006-05-03 00:23:49 +00:00
w) ezjail_wipeme="YES";;
2007-10-07 21:22:28 +00:00
f) ezjail_forcestop="YES";;
2006-05-05 00:59:54 +00:00
?) exerr ${ezjail_usage_delete};;
2006-05-03 00:23:49 +00:00
esac; done; shift $(( $OPTIND - 1 ))
2005-09-24 13:41:35 +00:00
2006-04-19 14:10:11 +00:00
# we need name of jail to vanish
2006-05-04 15:06:59 +00:00
[ $# -eq 1 ] || exerr ${ezjail_usage_delete}
2006-04-19 14:10:11 +00:00
2006-04-15 20:20:12 +00:00
# Get all info we have on that jail
fetchjailinfo $1
2005-09-24 13:41:35 +00:00
# check for existence of jail in our records
2007-10-07 21:22:28 +00:00
[ "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}."
2010-01-08 22:07:30 +00:00
2009-05-24 13:53:42 +00:00
# check for an active ZFS zpool
2012-09-08 15:09:47 +00:00
[ "${ezjail_imagetype}" = "zfs" ] && check_for_zpool
2007-10-07 21:22:28 +00:00
if [ "${ezjail_id}" ]; then
# if jail is still running, refuse to go any further
2008-07-10 18:55:39 +00:00
[ "${ezjail_forcestop}" ] || exerr "Error: Jail appears to be still running.\n '${ezjail_admin} stop ${ezjail_name}' it first or use '${ezjail_admin} delete -f ${ezjail_name}' to force stop."
2005-09-25 11:57:21 +00:00
2007-10-07 21:22:28 +00:00
# This one will also exerr on failure
2012-01-14 17:02:10 +00:00
start_stop_jail_by_script onestop ${ezjail_name}
2007-10-07 21:22:28 +00:00
fi
if [ "${ezjail_attached}" ]; then
# if jail is attached and detach is not forced, refuse to go any further
2008-07-10 18:55:39 +00:00
[ "${ezjail_forcestop}" ] || exerr "Error: Jail image file ${ezjail_image} is attached as ${ezjail_device}.\n '${ezjail_admin} config -i detach ${ezjail_name}' it first, or use '${ezjail_admin} delete -f ${ezjail_name}' to force detach."
2007-10-07 21:22:28 +00:00
detach_images keep
2006-05-09 02:00:04 +00:00
2007-10-07 21:22:28 +00:00
# See, if it successfully detached
fetchjailinfo ${ezjail_name}
2008-07-10 18:55:39 +00:00
[ "${ezjail_attached}" ] && exerr "Error: Could not detach ${ezjail_name}.\n You need to detach it by hand."
2007-10-07 21:22:28 +00:00
fi
2005-11-09 01:24:59 +00:00
2006-05-22 00:20:47 +00:00
# now we know everything we need to let the jail be gone. remove entry
2006-02-09 16:43:24 +00:00
# from ezjail resource structure, delete fstab.JAILNAME
2007-10-07 21:22:28 +00:00
rm -f "${ezjail_config}" "/etc/fstab.${ezjail_safename}"
2005-09-25 11:57:21 +00:00
# if there is a soft link pointing to the jail root, remove it
2008-06-04 01:02:54 +00:00
[ -L "${ezjail_softlink}" ] && rm -f "${ezjail_softlink}"
2005-09-25 11:57:21 +00:00
# if wiping the jail was requested, remove it
2006-04-15 20:20:12 +00:00
if [ "${ezjail_wipeme}" ]; then
2009-05-23 13:09:53 +00:00
case ${ezjail_imagetype} in
simple|bde|eli)
2010-01-08 22:07:30 +00:00
[ "${ezjail_image}" ] && rm -f "${ezjail_image}" "${ezjail_image%.img}.device"
2009-05-23 13:09:53 +00:00
;;
zfs)
2014-06-10 16:56:44 +02:00
if ! /sbin/zfs destroy -r ${ezjail_parentzfs}/${ezjail_hostname}; then
[ "${ezjail_forcestop}" ] || exerr "Error: Could not destroy file system ${ezjail_parentzfs}/${ezjail_hostname} for jail ${ezjail_name}."
echo "Warning: Could not destroy file system ${ezjail_parentzfs}/${ezjail_hostname} for jail ${ezjail_name}."
echo " Sleeping 5 seconds before retrying forcefully."
sleep 5
/sbin/zfs destroy -rf ${ezjail_parentzfs}/${ezjail_hostname} || exerr "Error: Failed to destroy file system."
echo "Success."
fi
2009-05-23 13:09:53 +00:00
;;
2013-04-10 23:29:13 +00:00
*)
chflags -R noschg "${ezjail_rootdir}"
;;
2009-05-23 13:09:53 +00:00
esac
2013-04-10 23:29:13 +00:00
rm -rf "${ezjail_rootdir}"
2006-03-21 16:33:05 +00:00
fi
2005-09-03 15:42:28 +00:00
;;
2005-11-14 20:33:11 +00:00
######################## ezjail-admin LIST ########################
2006-02-09 16:43:24 +00:00
list)
2006-05-04 15:06:59 +00:00
[ $# -eq 1 ] || exerr ${ezjail_usage_list}
2006-07-16 12:14:47 +00:00
unset ezjail_list
2006-05-03 00:23:49 +00:00
2012-09-08 15:17:02 +00:00
cd ${ezjail_jailcfgs} && ezjail_list=`ls -A | xargs rcorder`
2006-04-12 19:28:02 +00:00
2010-01-08 22:33:08 +00:00
printf "%-3s %-4s %-15s %-30s %s\\n" STA JID IP Hostname "Root Directory"
2010-02-11 17:32:25 +00:00
echo "--- ---- --------------- ------------------------------ ------------------------"
2006-04-15 20:20:12 +00:00
for ezjail in ${ezjail_list}; do
fetchjailinfo ${ezjail%.norun}
2010-04-25 22:05:47 +00:00
case ${ezjail_imagetype} in simple) ezjail_state="I";; bde) ezjail_state="B";; eli) ezjail_state="E";; zfs) ezjail_state="Z";; *) ezjail_state="D";; esac
2006-05-09 02:00:04 +00:00
if [ "${ezjail_id}" ]; then
2007-10-07 21:22:28 +00:00
ezjail_state="${ezjail_state}R"
2006-05-09 02:00:04 +00:00
else
2007-10-07 21:22:28 +00:00
[ "${ezjail_attached}" ] && ezjail_state="${ezjail_state}A" || ezjail_state="${ezjail_state}S"
2006-05-09 02:00:04 +00:00
fi
2007-10-07 21:22:28 +00:00
[ "${ezjail_safename}" != "${ezjail}" ] && ezjail_state="${ezjail_state}N"
2006-04-15 20:20:12 +00:00
2010-01-08 22:07:30 +00:00
TIFS=${IFS}; IFS=,; unset _multiline
2010-02-05 11:27:28 +00:00
for ezjail_ip in ${ezjail_ips:="-"}; do
2010-01-08 22:14:49 +00:00
if [ -z "${_multiline}" ]; then
2013-04-14 17:49:55 +00:00
printf "%-3s %-4s %-15s %-30s %s\\n" "${ezjail_state}" "${ezjail_id:-N/A}" "${ezjail_ip#*|}" "${ezjail_hostname}" "${ezjail_rootdir}"
2010-01-08 22:07:30 +00:00
_multiline=yes
2010-01-08 22:14:49 +00:00
else
2010-01-08 22:48:10 +00:00
printf " %-4s %s\\n" "${ezjail_id:-N/A}" "${ezjail_ip}"
2010-01-08 22:07:30 +00:00
fi
done
IFS=${TIFS}
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)
2011-06-14 22:18:29 +00:00
# Clean variables, prevent pollution
2012-09-20 11:26:45 +00:00
unset ezjail_provideports ezjail_installaction ezjail_osversion_source ezjail_osversion_destination ezjail_source
2005-09-20 22:30:40 +00:00
2011-04-05 09:17:54 +00:00
shift; while getopts :biuUpPs: arg; do case ${arg} in
2008-07-18 18:15:41 +00:00
b) ezjail_installaction="buildworld installworld";;
2006-05-03 00:23:49 +00:00
i) ezjail_installaction="installworld";;
2008-07-16 04:25:35 +00:00
u) ezjail_installaction="freebsd-update";;
2011-03-14 01:31:14 +00:00
U) ezjail_installaction="freebsd-upgrade";;
2012-09-20 11:26:45 +00:00
s) ezjail_source=${OPTARG};;
2006-05-03 00:23:49 +00:00
P) ezjail_provideports="YES"; ezjail_installaction="none";;
2008-07-16 04:25:35 +00:00
p) ezjail_provideports="YES";;
2006-05-05 00:59:54 +00:00
?) exerr ${ezjail_usage_update};;
2006-05-03 00:23:49 +00:00
esac; done; shift $(( ${OPTIND} - 1 ))
2006-05-04 15:06:59 +00:00
[ $# -eq 0 ] || exerr ${ezjail_usage_update}
2005-09-03 15:42:28 +00:00
2008-07-16 04:25:35 +00:00
# Check if some action was requested
[ "${ezjail_installaction}" ] || exerr "Error: No install action has been chosen.\n Please note that ezjails behaviour changed. Rebuilding the world no longer is default.\n Run '${ezjail_admin} update -b' to build and install a world from source or '${ezjail_admin} update -i' to install an already built world."
2006-04-19 14:10:11 +00:00
if [ "${ezjail_installaction}" = "none" ]; then
2006-05-22 00:20:47 +00:00
# check, whether ezjail has been setup correctly. existence of
2006-01-16 05:06:04 +00:00
# ezjail_jailbase is our indicator
2008-07-10 18:55:39 +00:00
[ -d "${ezjail_jailbase}" ] || exerr "Error: base jail does not exist.\n You cannot fill base jails ports tree before creating it.\n Please run '${ezjail_admin} update' or '${ezjail_admin} install' first."
2008-07-16 04:25:35 +00:00
elif [ "${ezjail_installaction}" = "freebsd-update" ]; then
[ -d "${ezjail_jailbase}" ] || exerr "Error: base jail does not exist.\n You cannot update a base jail until it is created.\n Please run '${ezjail_admin} update' or '${ezjail_admin} install' first."
2010-01-08 22:07:30 +00:00
2008-07-16 04:25:35 +00:00
# If ran from cron be kind to freebsds update servers and sleep first
2010-01-11 17:04:44 +00:00
[ -z "$TERM" -o "$TERM" = "dumb" ] && ezjail_urgency="cron" || ezjail_urgency="fetch"
2013-05-18 16:26:08 +00:00
if [ "${ezjail_use_zfs}" = "YES" ]; then
2014-08-04 14:11:39 +02:00
zfs snapshot ${ezjail_jailzfs}/basejail@`date +"%C%y%m%d_%H:%M:%S"`
zfs snapshot ${ezjail_jailzfs}/newjail@`date +"%C%y%m%d_%H:%M:%S"`
2013-05-17 22:36:03 +00:00
fi
2013-05-17 21:14:48 +00:00
env PAGER=/bin/cat freebsd-update -b ${ezjail_jailbase} ${ezjail_urgency} install
2013-05-17 22:36:03 +00:00
env PAGER=/bin/cat freebsd-update -b ${ezjail_jailtemplate} ${ezjail_urgency} install
2011-04-05 09:17:54 +00:00
elif [ "${ezjail_installaction}" = "freebsd-upgrade" ]; then
2011-03-14 01:31:14 +00:00
[ -d "${ezjail_jailbase}" ] || exerr "Error: base jail does not exist.\n You cannot update a base jail until it is created.\n Please run '${ezjail_admin} update' or '${ezjail_admin} install' first."
2012-09-20 11:26:45 +00:00
[ -z "${ezjail_source}" ] && exerr "Error: Can not (yet automatically) infer the basejail's osversion.\n Please run ${ezjail_admin} update -U -s X.X-RELEASE, with X.X-RELEASE being to osversion currently installed in the basejail in need of an upgrade."
2011-03-14 01:31:14 +00:00
# That would be the part where we try to lookup the osversion from a file in the basejail
2012-09-20 11:26:45 +00:00
ezjail_osversion_source="${ezjail_source}"
2011-03-14 01:31:14 +00:00
# Make the host systems os version our target version
# Users can override this by setting the UNAME_r environment variable
2013-07-23 00:00:54 +00:00
ezjail_osversion_target="`uname -r | sed -E 's,-p[0-9]+,,' | sed -E 's,-SECURITY,-RELEASE,'`"
2011-03-14 01:31:14 +00:00
# Finally run freebsd-update to upgrade our basejail
2013-05-17 22:36:03 +00:00
for DESTDIR in ${ezjail_jailbase} ${ezjail_jailtemplate}; do
env UNAME_r="${ezjail_osversion_source}" PAGER=/bin/cat freebsd-update -b ${DESTDIR} -r ${ezjail_osversion_target} upgrade
while [ $? -eq 0 ]; do
env UNAME_r="${ezjail_osversion_source}" PAGER=/bin/cat freebsd-update -b ${DESTDIR} -r ${ezjail_osversion_target} install
done
2013-03-27 18:42:39 +00:00
done
2011-03-14 01:31:14 +00:00
# Here we should write the file with the new osversion in case of success
2006-01-16 05:06:04 +00:00
else
2012-09-20 11:26:45 +00:00
# If user gave a source tree on command line, use that
[ "${ezjail_source}" ] && ezjail_sourcetree="${ezjail_source}"
2006-01-16 05:06:04 +00:00
# Bump the user for some of the most common errors
2008-07-10 18:55:39 +00:00
[ -d "${ezjail_sourcetree}" ] || exerr "Error: Cannot find your copy of the FreeBSD source tree in ${ezjail_sourcetree}.\n Consider using '${ezjail_admin} install' to create the base jail from an ftp server."
[ -e "${ezjail_sourcetree}/Makefile" ] || exerr "Error: Your source tree in ${ezjail_sourcetree} seems to be incomplete (Makefile is missing)."
[ "`sysctl -n kern.securelevel`" -gt 0 ] && exerr "Error: You are running in a secure level higher than 0.\n ${ezjail_admin} will not update correctly.\n Please reboot into a lower secure level."
2006-01-16 05:06:04 +00:00
2006-02-09 16:43:24 +00:00
# Normally fulljail should be renamed by past ezjail-admin commands.
2006-01-16 05:06:04 +00:00
# However those may have failed
2009-05-23 13:09:53 +00:00
if [ "${ezjail_use_zfs}" = "YES" ]; then
2012-09-08 15:09:47 +00:00
ensure_jailzfs
[ -d "${ezjail_jailfull}" ] && /sbin/zfs destroy -R "${ezjail_jailzfs}/fulljail" 2>/dev/null && rm -rf "${ezjail_jailfull}"
2009-05-27 23:24:35 +00:00
/sbin/zfs create "${ezjail_jailzfs}/fulljail" || exerr "Error: Cannot create temporary Jail directory."
2009-05-23 13:09:53 +00:00
else
[ -d "${ezjail_jailfull}" ] && chflags -R noschg "${ezjail_jailfull}" && rm -rf "${ezjail_jailfull}"
mkdir -p "${ezjail_jailfull}" || exerr "Error: Cannot create temporary Jail directory."
fi
2006-01-16 05:06:04 +00:00
2006-04-24 14:31:55 +00:00
# make and setup our world, then split basejail and newjail
2008-07-10 18:55:39 +00:00
cd "${ezjail_sourcetree}" && env DESTDIR="${ezjail_jailfull}" make ${ezjail_installaction} || exerr "Error: The command 'make ${ezjail_installaction}' failed.\n Refer to the error report(s) above."
cd "${ezjail_sourcetree}/etc" && env DESTDIR="${ezjail_jailfull}" make distribution || exerr "Error: The command 'make distribution' failed.\n Refer to the error report(s) above."
2006-04-24 14:31:55 +00:00
ezjail_splitworld
2006-01-16 05:06:04 +00:00
fi # installaction="none"
2006-04-24 14:31:55 +00:00
# Provide a copy of ports tree in basejail
2008-07-08 23:09:51 +00:00
[ "${ezjail_provideports}" ] && ezjail_updateports
2006-04-10 13:27:15 +00:00
2006-04-19 14:10:11 +00:00
;;
######################## ezjail-admin INSTALL ########################
install)
2011-06-14 22:18:29 +00:00
# Clean variables, prevent pollution
2012-11-17 21:20:23 +00:00
unset ezjail_release ezjail_installmanpages ezjail_installports ezjail_installsources ezjail_dir ezjail_ftpserverqueried ezjail_proto ezjail_disturi
2006-04-19 14:10:11 +00:00
2008-07-10 18:55:39 +00:00
shift; while getopts :mMpPsSh:r: arg; do case ${arg} in
2006-05-03 00:23:49 +00:00
m) ezjail_installmanpages=" manpages";;
2008-07-10 18:55:39 +00:00
M) ezjail_installmanpages=" manpages"; unset ezjail_basesystem;;
2006-05-03 00:23:49 +00:00
s) ezjail_installsources=" src";;
2008-07-10 18:55:39 +00:00
S) ezjail_installsources=" src"; unset ezjail_basesystem;;
2006-05-03 00:23:49 +00:00
p) ezjail_installports="YES";;
2008-07-10 18:55:39 +00:00
P) ezjail_installports="YES"; unset ezjail_basesystem;;
2007-10-07 21:22:28 +00:00
h) ezjail_ftphost=${OPTARG};;
r) ezjail_release=${OPTARG};;
2006-05-05 00:59:54 +00:00
?) exerr ${ezjail_usage_install};;
2006-05-03 00:23:49 +00:00
esac; done; shift $(( ${OPTIND} - 1 ))
2006-05-04 15:06:59 +00:00
[ $# -eq 0 ] || exerr ${ezjail_usage_install}
2006-04-19 14:10:11 +00:00
ezjail_installarch=`uname -p`
2012-01-02 04:37:27 +00:00
ezjail_installplatform=`uname -m`
2008-10-22 15:54:15 +00:00
: ${ezjail_ftphost="ftp.freebsd.org"}
2012-11-17 21:20:23 +00:00
ezjail_proto=${ezjail_ftphost%%://*}
[ "${ezjail_proto}" = "${ezjail_ftphost}" ] && ezjail_proto=ftp
ezjail_disturi=${ezjail_ftphost#*://}
2006-04-19 14:10:11 +00:00
2008-07-10 18:55:39 +00:00
[ "`sysctl -n kern.securelevel`" -gt 0 ] && exerr "Error: You are running in a secure level higher than 0.\n ${ezjail_admin} will not install correctly.\n Please reboot into a lower secure level."
# Check for basejail when not installing base jail
[ "${ezjail_basesystem}" -o -d "${ezjail_jailbase}" ] || exerr "Error: The basejail does not exist.\n You cannot install distribution packages before creating ezjails environment.\n Please run '${ezjail_admin} update' or '${ezjail_admin} install' using lower case parameters first."
2007-01-24 11:31:04 +00:00
2012-11-08 23:14:17 +00:00
if [ -z "${ezjail_release}" ]; then
# if no release version is requested, use the host system's
2006-04-19 14:10:11 +00:00
ezjail_release=`uname -r`
2012-01-04 18:27:27 +00:00
ezjail_release_major=${ezjail_release%%.*}
2012-11-08 23:14:17 +00:00
# ftp servers normally wont provide non-RELEASE-builds
2012-11-17 21:20:23 +00:00
if [ "${ezjail_proto}" != "file" ]; then
2012-11-08 23:14:17 +00:00
case ${ezjail_release} in *-STABLE) ezjail_release="${ezjail_release%-STABLE}-RELEASE";; esac
2013-05-18 16:26:08 +00:00
case ${ezjail_release} in *-RELEASE-p*) ezjail_release=${ezjail_release%-p*};; esac
2012-11-08 23:14:17 +00:00
if [ "${ezjail_release%-RELEASE}" = "${ezjail_release}" ]; then
echo "Your system is ${ezjail_release}. Normally FTP-servers don't provide non-RELEASE-builds."
[ ${ezjail_release_major} -ge 9 ] && ezjail_installarch="${ezjail_installplatform}/${ezjail_installarch}"
2012-11-17 21:20:23 +00:00
[ "${ezjail_proto}" != "ftp" ] || ezjail_queryftpserver || echo "... I'll continue anyway."
echo -n "Release to fetch [ ${ezjail_release} ]: "
2012-11-08 23:14:17 +00:00
read ezjail_releasetmp
[ "${ezjail_releasetmp}" ] && ezjail_release=${ezjail_releasetmp}
ezjail_installarch=`uname -p`
2012-11-08 23:15:35 +00:00
fi
2006-04-19 14:10:11 +00:00
fi
fi
2012-01-02 04:37:27 +00:00
# From 9.0 releases come with a new layout
# It is now a single archive ${pkg}.txz instead of a list of archive parts under ${pkg}/*
2012-01-02 05:17:54 +00:00
# man pages come with the base
2012-11-17 21:20:23 +00:00
# We can use fetch to connect to the ftp host, allowing the use of proxies. We needed ftp
# in pre-9.0-releases to make ${pkg}/* work.
2012-01-02 04:37:27 +00:00
# We assume for now that jail's platform is supposed to be that of the host. That may change later.
2012-01-02 04:54:05 +00:00
ezjail_release_major=${ezjail_release%%.*}
2012-11-17 21:20:23 +00:00
[ "${ezjail_release_major}" -eq "${ezjail_release_major}" ] 2>/dev/null || exerr ${ezjail_release} does not look like a valid FreeBSD version descriptor
2012-01-02 05:17:54 +00:00
if [ ${ezjail_release_major} -ge 9 -a -n "${ezjail_installmanpages}" ]; then
echo "Note: From FreeBSD 9.0 man pages are part of the base package"
unset ezjail_installmanpages
fi
2012-01-02 04:37:27 +00:00
2006-04-19 14:10:11 +00:00
# Normally fulljail should be renamed by past ezjail-admin commands.
# However those may have failed
2009-05-23 13:09:53 +00:00
if [ "${ezjail_use_zfs}" = "YES" ]; then
2012-09-08 15:09:47 +00:00
ensure_jailzfs
[ -d "${ezjail_jailfull}" ] && /sbin/zfs destroy -R "${ezjail_jailzfs}/fulljail" 2>/dev/null && rm -rf "${ezjail_jailfull}"
/sbin/zfs create "${ezjail_jailzfs}/fulljail" || exerr "Error: Cannot create temporary Jail directory."
2009-05-23 13:09:53 +00:00
else
[ -d "${ezjail_jailfull}" ] && chflags -R noschg "${ezjail_jailfull}" && rm -rf "${ezjail_jailfull}"
mkdir -p "${ezjail_jailfull}" || exerr "Error: Cannot create temporary Jail directory."
fi
2011-01-20 14:23:59 +00:00
# Install into staging directory for full install or into basejail
# for post-install man/src installations
[ "${ezjail_basesystem}" ] && DESTDIR="${ezjail_jailfull}" || DESTDIR="${ezjail_jailbase}"
2006-04-19 14:10:11 +00:00
2013-03-27 18:36:11 +00:00
ezjail_makeabsolute ezjail_jailtemp
2007-10-07 21:22:28 +00:00
rm -rf "${ezjail_jailtemp}"
2008-03-22 11:35:07 +00:00
for pkg in ${ezjail_basesystem} ${ezjail_installmanpages} ${ezjail_installsources}; do
2006-04-19 14:10:11 +00:00
2012-11-17 21:20:23 +00:00
if [ "${ezjail_proto}" = "file" ]; then
# The easy case means, that a local distribution directory has been specified.
if [ ${ezjail_release_major} -ge 9 ]; then
2015-04-15 01:24:31 +02:00
if [ -r "${ezjail_disturi}/${pkg}.txz" ]; then
xzdec ${ezjail_disturi}/${pkg}.txz | tar --unlink -xpJf - -C ${DESTDIR}
else
[ ${pkg} = "lib32" ] || exerr "Error: Can not access package file ${ezjail_disturi}/${pkg}.txz"
echo "Warning: Can not access package file ${ezjail_disturi}/${pkg}.txz, ignored for auto added lib32."
fi
2012-11-17 21:20:23 +00:00
else
2013-03-27 18:36:11 +00:00
ezjail_makeabsolute ezjail_disturi
cd "${ezjail_disturi}/${pkg}" || exerr "Error: Could not cd to ${ezjail_disturi}/${pkg}."
2012-11-17 21:20:23 +00:00
[ "${pkg}" = "base" ] && echo "Ignore the next question, ezjail answers it for you."
set -- all
[ -f install.sh ] && yes | . install.sh
[ $? -eq 0 ] || exerr "Error: Package install script for ${pkg} failed."
fi
else
# The hard case means, we have to fetch the distribution files from a remote server
2006-04-19 14:10:11 +00:00
# Create and try to access temp dir
2008-07-10 18:55:39 +00:00
mkdir -p "${ezjail_jailtemp}" || exerr "Error: Could not create temporary base jail directory ${ezjail_jailtemp}."
cd "${ezjail_jailtemp}" || exerr "Error: Could not cd to ${ezjail_jailtemp}."
2006-04-19 14:10:11 +00:00
# Try all paths as stolen from sysinstall, break on success.
2010-05-26 00:53:05 +00:00
for ezjail_path in pub/FreeBSD/releases pub/FreeBSD/snapshot pub/FreeBSD releases snapshots pub/FreeBSD-Archive/old-releases NO; do
2012-11-17 21:20:23 +00:00
# Once we tried all paths, we give up and nudge the user
2006-05-17 20:22:04 +00:00
if [ "${ezjail_path}" = "NO" ]; then
2012-11-17 21:20:23 +00:00
echo -e "\nCould not fetch ${pkg} from ${ezjail_proto}://${ezjail_disturi}.\n Maybe your release (${ezjail_release}) is specified incorrectly or the host ${ezjail_disturi} does not provide that release build.\n Use the -r option to specify an existing release or the -h option to specify an alternative ftp server." >&2
[ "${ezjail_proto}" = "ftp" -a -z "${ezjail_ftpserverqueried}" ] && ezjail_queryftpserver
2006-05-17 20:42:13 +00:00
exit 1
2006-05-17 20:22:04 +00:00
fi
2006-04-19 14:10:11 +00:00
2012-11-17 21:20:23 +00:00
# Fetching and extraction distributions has become much easier from 9.0
if [ ${ezjail_release_major} -ge 9 ]; then
fetch "${ezjail_proto}://${ezjail_disturi}/${ezjail_path}/${ezjail_installplatform}/${ezjail_installarch}/${ezjail_release}/${pkg}.txz" || continue
xzdec ${pkg}.txz | tar --unlink -xpJf - -C ${DESTDIR}
2013-03-27 18:36:11 +00:00
_res=$?
2012-11-17 21:20:23 +00:00
else
2013-03-27 18:36:11 +00:00
[ "${ezjail_proto}" = "ftp" ] || echo "Warning: Ignoring ${ezjail_proto} protocol on FreeBSD pre 9.0"
2012-11-17 21:20:23 +00:00
ftp ${ezjail_disturi}:${ezjail_path}/${ezjail_installarch}/${ezjail_release}/${pkg}/* || continue
# These actions are really ugly: sources want $1 to contain the set
# of sources to install, base asks the user if he is sure, hence the
# yes and the set -- all
[ "${pkg}" = "base" ] && echo "Ignore the next question, ezjail answers it for you."
set -- all
[ -f install.sh ] && yes | . install.sh
2013-03-27 18:36:11 +00:00
_res=$?
2012-11-17 21:20:23 +00:00
fi
2006-04-19 14:10:11 +00:00
2012-11-17 21:20:23 +00:00
rm -rf "${ezjail_jailtemp}"
2013-03-27 18:36:11 +00:00
[ ${_res} -eq 0 ] || exerr "Error: Package install script for ${pkg} failed."
2012-11-17 21:58:55 +00:00
break
2012-11-17 21:20:23 +00:00
done
2006-04-19 14:10:11 +00:00
fi
done
2013-05-17 19:59:26 +00:00
# Update to current patch level, split basejail and newjail
if [ "${ezjail_basesystem}" ]; then
2013-05-17 21:14:48 +00:00
env PAGER=/bin/cat freebsd-update -b "${DESTDIR}" fetch install
2013-05-17 19:59:26 +00:00
ezjail_splitworld
fi
2006-04-19 14:10:11 +00:00
2006-04-24 14:31:55 +00:00
# Fill ports, if requested
2008-07-08 23:09:51 +00:00
[ "${ezjail_installports}" ] && ezjail_updateports
2006-04-19 14:10:11 +00:00
2007-09-14 23:25:04 +00:00
;;
######################## ezjail-admin SHORTCUT ########################
2008-03-14 01:48:40 +00:00
*start|*stop|*startcrypto|*stopcrypto)
2011-04-25 17:42:44 +00:00
start_stop_jail_by_script $@
2006-04-15 20:20:12 +00:00
;;
2007-09-15 00:25:52 +00:00
######################## ezjail-admin CONSOLE ########################
console)
2011-06-14 22:18:29 +00:00
# Clean variables, prevent pollution
2007-09-15 00:25:52 +00:00
unset ezjail_execute ezjail_forcestart
shift; while getopts :e:f arg; do case ${arg} in
e) ezjail_execute=${OPTARG};;
f) ezjail_forcestart="YES";;
?) exerr ${ezjail_usage_console};;
esac; done; shift $(( $OPTIND - 1 ))
# we need name of jail to attach to
[ $# -eq 1 ] || exerr ${ezjail_usage_console}
# Get all info we have on that jail
fetchjailinfo $1
# check for existence of jail in our records
2007-10-07 21:22:28 +00:00
[ "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}."
2007-09-15 00:25:52 +00:00
# if jail is not running, we either try to force start it or fail
if [ -z "${ezjail_id}" ]; then
# If force start is requested, try that
2011-07-27 11:20:32 +00:00
[ "${ezjail_forcestart}" ] || exerr "Error: Jail ${ezjail_name} appears not to be running\n Start it first, or use '${ezjail_admin} console -f ${ezjail_name}' to force start."
2007-09-15 00:25:52 +00:00
2007-10-07 21:22:28 +00:00
# This one will also exerr on failure
2012-09-18 16:23:28 +00:00
start_stop_jail_by_script onestart ${ezjail_name}
2007-09-15 00:25:52 +00:00
fi
2010-01-08 22:07:30 +00:00
2009-12-29 13:02:56 +00:00
# use the jails FIB if there is one
if [ -n "${ezjail_fib}" ]; then
_setfib="setfib -F ${ezjail_fib}"
else
_setfib=""
fi
2007-09-15 00:25:52 +00:00
# Try to attach to jail
2009-12-29 13:02:56 +00:00
[ "${ezjail_execute}" ] && exec ${_setfib} jexec ${ezjail_id} ${ezjail_execute}
2007-09-15 00:25:52 +00:00
2009-12-29 13:02:56 +00:00
exec ${_setfib} jexec ${ezjail_id} ${ezjail_default_execute}
2007-09-26 15:06:40 +00:00
;;
2015-04-15 00:11:46 +02:00
######################## ezjail-admin FREEZE ########################
freeze)
shift
[ $# -eq 2 ] || exerr ${ezjail_usage_freeze}
ezjail="$1"
ezjail_flavour="$2"
fetchjailinfo ${ezjail}
# Check for existence of jail in our records
[ "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}."
# Check if jail is of zfs type
[ ${ezjail_imagetype} = "zfs" ] || exerr "Error: Can only freeze zfs based jails into a flavour.\n ${ezjail_name} is not a zfs jail."
# Check if jail has been snapshotted at create time
/sbin/zfs list -H "${ezjail_parentzfs}/${ezjail_hostname}@_vanilla_newjail_" >/dev/null 2>/dev/null
[ $? -eq 0 ] || exerr "Error: Jail ${ezjail_name} did not keep a snapshot when it was first created.\n Use ezjail-admin create with the -k option to keep a snapshot to diff."
# For now we will not overwrite an existing flavour
[ -e "${ezjail_flavours_dir}/${ezjail_flavour}" ] && exerr "Error: A flavour already exists at ${ezjail_flavours_dir}/${ezjail_flavour}."
# Create the new frozen flavour's dir
mkdir -p "${ezjail_flavours_dir}/${ezjail_flavour}"
2015-04-15 01:10:04 +02:00
# Work relative to jails rootdir
cd "${ezjail_rootdir}"
2015-04-15 00:11:46 +02:00
# Now list all files that have been added or modified
# TODO: This assumes no files have been deleted from when the jail was initially created. Safe assumption for now.
# TODO: This assumes no files have been renamed from when the jail was initially created. Not so sure.
/sbin/zfs diff -H "${ezjail_parentzfs}/${ezjail_hostname}@_vanilla_newjail_" "${ezjail_parentzfs}/${ezjail_hostname}" | \
2015-04-15 01:10:04 +02:00
grep -E "^[M+]" | cut -f 2 | sed -E s:"^${ezjail_rootdir}/?":: | cpio -d -m -p -v "${ezjail_flavours_dir}/${ezjail_flavour}"
2015-04-15 00:11:46 +02:00
;;
2013-04-24 03:01:19 +00:00
######################## ezjail-admin SNAPSHOT ########################
snapshot)
shift
if [ $# -eq 0 ]; then
cd ${ezjail_jailcfgs} && ezjail_list=`ls -A`
else
ezjail_list=$*
fi
for ezjail in ${ezjail_list}; do
fetchjailinfo ${ezjail%.norun}
# Check for existence of jail in our records
[ "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}."
# Snapshots only work with zfs jails
[ "${ezjail_imagetype}" = "zfs" -o -n "${ezjail_zfs_datasets}" ] || continue
[ "${ezjail_imagetype}" = "zfs" ] && ezjail_zfs_datasets="${ezjail_parentzfs}/${ezjail_hostname} ${ezjail_zfs_datasets}"
# Use global retention policy, if none set
2013-04-24 03:15:58 +00:00
: ${ezjail_retention_policy:=${ezjail_default_retention_policy}}
2013-04-24 03:01:19 +00:00
for ezjail_zfs in ${ezjail_zfs_datasets}; do
check_for_zfs_exist ${ezjail_zfs} || continue
zfs_retention_policy=`/sbin/zfs get -H -o value ezjail:autosnap_retention ${ezjail_zfs}`
[ "${zfs_retention_policy}" = "-" ] || ezjail_retention_policy="${zfs_retention_policy}"
if [ -z "${ezjail_retention_policy}" ]; then
2013-04-24 03:06:00 +00:00
/sbin/zfs snapshot -r ${ezjail_zfs}@ez-autosnap-`date +${ezjail_snap_date_format}`
2013-04-24 03:01:19 +00:00
else
snap_list=`/sbin/zfs list -H -t snapshot -o name -S creation -r ${ezjail_zfs} | \
grep ^${ezjail_zfs}@ez-autosnap- | cut -d '@' -f 2 | cut -d '-' -f 3`
filteroldsnapshots ${snap_list}
fi
done
done
;;
######################## ezjail-admin ARCHIVE ########################
2007-09-26 15:06:40 +00:00
archive)
2011-06-14 22:18:29 +00:00
# Clean variables, prevent pollution
2008-03-14 00:17:22 +00:00
unset ezjail_archive ezjail_archive_tag ezjail_force ezjail_archivealljails ezjail_addfiles
2007-09-26 15:06:40 +00:00
2007-10-01 01:02:45 +00:00
shift; while getopts :Afa:d: arg; do case ${arg} in
2007-09-30 12:09:03 +00:00
f) ezjail_force="YES";;
2007-10-01 01:02:45 +00:00
a) ezjail_archive=${OPTARG};;
2007-09-26 15:06:40 +00:00
d) ezjail_archivedir=${OPTARG};;
2007-10-01 01:02:45 +00:00
A) ezjail_archivealljails="YES";;
2007-09-26 15:06:40 +00:00
?) exerr ${ezjail_usage_archive};;
esac; done; shift $(( ${OPTIND} - 1 ))
2007-09-30 13:13:08 +00:00
# Specifying no jails only is acceptable if archiving all jails
[ $# -lt 1 -a -z "${ezjail_archivealljails}" ] && exerr ${ezjail_usage_archive}
2007-09-26 15:06:40 +00:00
2011-01-20 21:20:36 +00:00
# Ensure that archive directory is there
2013-10-14 16:29:46 +00:00
[ "${ezjail_archive}" = "-" ] || mkdir -p "${ezjail_archivedir}" || exerr "Error: Can not create archive directory ${ezjail_archivedir}."
2007-09-26 15:06:40 +00:00
2007-09-30 13:13:08 +00:00
# Will not backup more than one jail per archive
2008-07-10 18:55:39 +00:00
[ "${ezjail_archive}" -a "${ezjail_archivealljails}" ] && exerr "Error: Must not specify an archive location for multiple archives.\n Can not archive multiple jails into one archive."
2007-09-30 13:13:08 +00:00
# Will not backup more than one jail per archive
2008-07-10 18:55:39 +00:00
[ $# -gt 1 -a "${ezjail_archive}" ] && exerr "Error: Must not specify an archive location for multiple archives.\n Can not archive multiple jails into one archive."
2007-09-30 13:13:08 +00:00
# Either all or only some. Decide.
2008-07-10 18:55:39 +00:00
[ $# -gt 0 -a "${ezjail_archivealljails}" ] && exerr "Error: Must not specify an ezjail to backup with -A.\n Please use either '${ezjail_admin} archive -A' or '${ezjail_admin} archive $*'."
2007-09-26 15:06:40 +00:00
2007-09-30 13:13:08 +00:00
# Fetch list of all ezjails
2011-04-25 17:42:44 +00:00
[ "${ezjail_archivealljails}" ] && cd ${ezjail_jailcfgs} && set - `rcorder *`
2007-09-30 13:13:08 +00:00
2011-04-25 17:42:44 +00:00
for ezjail; do
2008-07-07 04:29:32 +00:00
unset ezjail_imagesize
2007-09-30 13:13:08 +00:00
# Jail name mandatory
2008-07-06 22:24:56 +00:00
fetchjailinfo ${ezjail%.norun}
2007-09-30 13:13:08 +00:00
2008-05-11 03:04:46 +00:00
# Check for existence of jail in our records
2007-10-07 21:22:28 +00:00
[ "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}."
2007-09-26 15:06:40 +00:00
2008-05-11 03:04:46 +00:00
# If jail is still running, refuse to go any further - unless forced
2008-07-07 04:29:32 +00:00
if [ "${ezjail_id}" -a -z "${ezjail_force}" ]; then
2009-08-21 12:03:19 +00:00
echo -e "Warning: Jail ${ezjail_name} appears to be still running.\n Stop it first, or use '${ezjail_admin} archive -f ${ezjail_name}' to force archiving."
2008-07-07 04:29:32 +00:00
continue
fi
# Attach non-attached jails, if they can be attached non blocking
2010-01-15 12:59:44 +00:00
if [ "${ezjail_imagetype}" -a "${ezjail_imagetype}" != "zfs" -a -z "${ezjail_attached}" ]; then
2008-07-07 04:29:32 +00:00
if [ "${ezjail_attachblocking}" ]; then
echo "Warning: Jail ${ezjail_name} is an image jail and can not be attached automatically."
2013-05-01 18:38:18 +00:00
echo " Use '${ezjail_admin} config -i attach ${ezjail_name}' to attach it first."
2008-07-07 04:29:32 +00:00
continue
fi
2008-07-09 10:37:11 +00:00
mount_images
2008-07-10 18:55:39 +00:00
ezjail_imagesize=-`stat -Lf %z "${ezjail_image}"`
2008-07-07 04:29:32 +00:00
fi
2007-10-07 21:22:28 +00:00
# This one goes into archive to identify jail by name and restore date
ezjail_archive_tag="${ezjail_safename}-`date +%Y%m%d%H%M.%S`"
2007-09-26 15:06:40 +00:00
2008-05-11 03:04:46 +00:00
# If no archive name was specified, make one up
2007-10-07 21:22:28 +00:00
[ "${ezjail_archive}" ] || ezjail_archive="${ezjail_archive_tag}.tar.gz"
2007-09-26 15:06:40 +00:00
2008-07-08 18:30:06 +00:00
# Archives need to know, where they came from. Restore by default only
2008-05-11 03:04:46 +00:00
# reinstalls jails on the same machine. We also warn on OS upgrades and
# processor type changes
ezjail_hostsystem_name=$( echo -n `uname -n` | tr -c '[:alnum:].' _ )
ezjail_hostsystem_version=$( echo -n `uname -r` | tr -c '[:alnum:].' _ )
ezjail_hostsystem_cpu=$( echo -n `uname -p` | tr -c '[:alnum:].' _ )
2008-07-07 04:29:32 +00:00
ezjail_archive_tag="${ezjail_archive_tag}-${ezjail_hostsystem_name}-${ezjail_hostsystem_version}-${ezjail_hostsystem_cpu}${ezjail_imagesize}"
2008-05-11 03:04:46 +00:00
# If archive location is not absolute, prepend archive directory
2008-03-14 00:17:22 +00:00
ezjail_makeabsolute ezjail_archive ${ezjail_archivedir}
2008-07-07 04:29:32 +00:00
2007-09-30 13:13:08 +00:00
# It's a tar archive, after all
case ${ezjail_archive} in
2007-11-27 23:01:32 +00:00
*.tar.gz|*.tgz|-) ;;
*) ezjail_archive="${ezjail_archive}.tar.gz";;
2007-11-27 22:11:59 +00:00
esac
2007-09-30 12:09:03 +00:00
2007-11-27 23:01:32 +00:00
# For stdout do specify nothing
[ "${ezjail_archive}" = "-" ] && unset ezjail_archive_opt || ezjail_archive_opt="-f ${ezjail_archive}"
2008-05-11 03:04:46 +00:00
[ -f "/etc/fstab.${ezjail_safename}" ] && ezjail_addfiles=/etc/fstab.${ezjail_safename}
2008-03-14 00:17:22 +00:00
2008-07-10 18:55:39 +00:00
cd "${ezjail_rootdir}" || exerr "Error: Can't cd to ${ezjail_root}."
2009-08-21 12:03:19 +00:00
pax -wzXt -x cpio ${ezjail_archive_opt} \
2008-01-08 03:34:48 +00:00
-s:"^[^\\.].*/${ezjail_safename}\$":prop.ezjail-${ezjail_archive_tag}: \
-s:"^[^\\.].*/${ezjail_safename}.norun\$":prop.ezjail-${ezjail_archive_tag}.norun: \
2007-09-30 13:13:08 +00:00
-s:"etc/fstab.${ezjail_safename}\$":fstab.ezjail: \
-s:"^\\.":ezjail: \
2008-03-14 00:17:22 +00:00
"${ezjail_config}" ${ezjail_addfiles} .
2007-09-30 12:09:03 +00:00
2008-07-07 04:29:32 +00:00
ezjail_paxresult=$?
# Detach previously attached jail
2008-07-09 10:37:11 +00:00
[ "${ezjail_imagesize}" ] && detach_images keep
2008-07-07 04:29:32 +00:00
2007-09-30 13:13:08 +00:00
# An error on a jail not running is bad
2013-05-01 18:38:18 +00:00
[ ${ezjail_paxresult} -eq 0 -o "${ezjail_force}" ] || echo -n "Warning: Archiving jail ${ezjail_name} was not completely successful.\n Please refer to the output above for problems the archiving tool encountered.\n You may ignore reports concerning setting access and modification times.\n You might want to check and remove ${ezjail_archive}."
2007-09-26 15:06:40 +00:00
2007-09-30 13:13:08 +00:00
# When archiving a running jail, some errors might occur
2008-07-10 18:55:39 +00:00
[ ${ezjail_paxresult} -eq 0 ] || echo "Warning: Archiving jail ${ezjail_name} was not completely successful. For a running jail this is not unusual."
2007-09-30 13:13:08 +00:00
2008-03-14 00:17:22 +00:00
unset ezjail_archive ezjail_archive_opt ezjail_addfiles
2007-09-30 13:13:08 +00:00
done
2007-10-07 21:22:28 +00:00
;;
####################### ezjail-admin RESTORE ########################
restore)
2011-06-14 22:18:29 +00:00
# Clean variables, prevent pollution
2011-01-20 21:20:36 +00:00
unset ezjail_safename ezjail_forcerestore
2007-10-07 21:22:28 +00:00
2008-07-08 18:30:06 +00:00
shift; while getopts :d:f arg; do case ${arg} in
2007-10-07 21:22:28 +00:00
d) ezjail_archivedir=${OPTARG};;
2008-07-08 18:30:06 +00:00
f) ezjail_forcerestore="YES";;
2007-10-07 21:22:28 +00:00
?) exerr ${ezjail_usage_restore};;
esac; done; shift $(( ${OPTIND} - 1 ))
2007-11-27 21:51:48 +00:00
[ $# -eq 0 ] && exerr ${ezjail_usage_restore}
2007-10-07 21:22:28 +00:00
2008-07-06 22:24:56 +00:00
for ezjail_fromarchive in $@; do
2008-07-07 04:29:32 +00:00
unset ezjail_safename ezjail_imagedata ezjail_nameprop
2008-01-02 07:03:59 +00:00
# if archive location is absolute and doesn't exist, fail
2008-07-10 18:55:39 +00:00
[ "${ezjail_fromarchive%%[!/]*}" -a ! -f "${ezjail_fromarchive}" ] && exerr "Error: Archive ${ezjail_fromarchive} not found."
2008-01-02 07:03:59 +00:00
if [ -z "${ezjail_fromarchive%%[!/]*}" ]; then
# Try archive location
if [ -r "${ezjail_archivedir}/${ezjail_fromarchive}" ]; then
ezjail_fromarchive="${ezjail_archivedir}/${ezjail_fromarchive}"
else
# If archive is not found, try guessing by jail name
2008-01-02 07:44:32 +00:00
ezjail_safename=`echo -n "${ezjail_fromarchive}" | tr -c '[:alnum:]' _`
unset ezjail_fromarchive
for ezjail_archive in "${ezjail_archivedir}/${ezjail_safename}"*; do
[ -z "${ezjail_fromarchive}" -a -f "${ezjail_archive}" ] && ezjail_fromarchive=${ezjail_archive}
[ "${ezjail_archive}" -nt "${ezjail_fromarchive}" ] && ezjail_fromarchive=${ezjail_archive}
done
2008-07-08 18:30:06 +00:00
[ -f "${ezjail_fromarchive}" ] || exerr "Error: No archive for pattern ${ezjail_safename} can be found."
2008-01-02 07:03:59 +00:00
fi
fi
2008-01-02 07:44:32 +00:00
2008-05-11 03:04:46 +00:00
# We want to parse some content from archive. In order to reduce
# security implication this may have, we check owner and permission.
#
# However, this does not protect against admins transporting
# archives over insecure lines over the net.
2008-07-10 18:55:39 +00:00
[ `stat -f %u "${ezjail_fromarchive}"` -eq 0 ] || exerr "Error: Insecure ownership of archive ${ezjail_fromarchive}.\n Please check the file and chown it to root if you trust its source."
[ $(( `stat -f %OLp "${ezjail_fromarchive}"` & 0022 )) -eq 0 ] || exerr "Error: Insecure permissions for archive ${ezjail_fromarchive}.\n Please check the file and fix permission (chmod og-w) if you trust its source."
2008-05-11 03:04:46 +00:00
ezjail_nameprop=`pax -zn -f ${ezjail_fromarchive} prop.ezjail-\*`
2008-01-02 07:44:32 +00:00
[ $? -eq 0 -a "${ezjail_nameprop}" ] || exerr "Error: File ${ezjail_fromarchive} is not an ezjail archive."
2008-01-08 03:34:48 +00:00
2008-07-06 18:54:56 +00:00
# Figure out, what jail and jail enviroment archive claims to contain
TIFS=${IFS}; IFS=-; set - ${ezjail_nameprop}
2008-07-07 23:17:28 +00:00
ezjail_nameprop_safename=$2 ezjail_nameprop_hsname=$4 ezjail_nameprop_hsversion=$5 ezjail_nameprop_hscpu=$6 ezjail_nameprop_imgagesize=$7
2008-07-06 18:54:56 +00:00
IFS=${TIFS}
2008-05-11 03:04:46 +00:00
# Figure out current system environment
ezjail_hsname=$( echo -n `uname -n` | tr -c '[:alnum:].' _ )
ezjail_hsversion=$( echo -n `uname -r` | tr -c '[:alnum:].' _ )
ezjail_hscpu=$( echo -n `uname -p` | tr -c '[:alnum:].' _ )
# Catch all errors that will likely create a broken backup
[ "${ezjail_safename}" -a "${ezjail_safename}" != "${ezjail_nameprop_safename}" ] && exerr "Error: Archive name ${ezjail_fromarchive} does not match archived jail ${ezjail_nameprop_safename}."
2008-07-10 18:55:39 +00:00
[ "${ezjail_hsname}" != "${ezjail_nameprop_hsname}" -a -z "${ezjail_forcerestore}" ] && exerr "Error: Archive was created on host named ${ezjail_nameprop_hsname}.\n Consider using '${ezjail_admin} create -a ${ezjail_fromarchive}' when migrating ezjails, or '${ezjail_admin} restore -f ${ezjail_fromarchive}' to force restore."
[ "${ezjail_hscpu}" != "${ezjail_nameprop_hscpu}" -a -z "${ezjail_forcerestore}" ] && exerr "Error: Archive was created on a different CPU. Can not restore.\n Consider using '${ezjail_admin} create -a ${ezjail_fromarchive}' when migrating ezjails, or '${ezjail_admin} restore -f ${ezjail_fromarchive}' to force restore."
2008-01-02 07:44:32 +00:00
2008-06-01 01:22:05 +00:00
# Save config to tempfile and source it
2008-07-07 04:29:32 +00:00
ezjail_config=`mktemp /tmp/ezjail.prop.XXXXXXXX`
2008-06-01 01:22:05 +00:00
[ $? -ne 0 ] && exerr "Error: Can't create temporary file."
2008-07-07 04:29:32 +00:00
pax -rzn -s:${ezjail_nameprop}:${ezjail_config}: -f ${ezjail_fromarchive} ${ezjail_nameprop}
2013-10-14 16:44:50 +00:00
fetchjailinfo ${ezjail_safename:-${ezjail_nameprop_safename}} ${ezjail_config}
2008-07-07 04:29:32 +00:00
# Now all parameters are here, invoke ezjail-admin create
2010-01-08 22:07:30 +00:00
[ "${ezjail_rootdir}" -a "${ezjail_ips}" -a "${ezjail_hostname}" ] || exerr "Error: Archive does not contain a valid ezjail properties file.\n Some jails properties are missing."
2010-01-15 15:06:24 +00:00
# Setup the ezjail_imagedata.
2010-01-15 14:34:00 +00:00
[ "${ezjail_imagetype}" ] && ezjail_imagedata="-c ${ezjail_imagetype} -C '${ezjail_attachparams}'"
2010-01-15 15:06:24 +00:00
# Only use the imagesize if we really have one, ZFS imagejails don't necessarily have a size.
2010-01-15 15:15:27 +00:00
[ "${ezjail_imagetype}" -a "${ezjail_nameprop_imgagesize}" ] && ezjail_imagedata="-c ${ezjail_imagetype} -C '${ezjail_attachparams}' -s ${ezjail_nameprop_imgagesize}"
2010-01-08 22:07:30 +00:00
$0 create -a "${ezjail_fromarchive}" -A "${ezjail_config}" ${ezjail_imagedata} -r "${ezjail_rootdir}" "${ezjail_hostname}" "${ezjail_ips}" || exerr "Error: Create failed."
2008-07-07 04:29:32 +00:00
rm -f "${ezjail_config}"
2008-06-01 01:22:05 +00:00
2008-01-02 07:03:59 +00:00
done
2007-09-15 00:25:52 +00:00
;;
2006-04-15 20:20:12 +00:00
######################## ezjail-admin CONFIG ########################
config)
2011-06-14 22:18:29 +00:00
# Clean variables, prevent pollution
2010-01-11 03:06:27 +00:00
unset ezjail_setrunnable ezjail_imageaction ezjail_new_name ezjail_new_zfs_datasets ezjail_new_cpuset ezjail_new_fib ezjail_old_config
2006-04-15 20:48:26 +00:00
2009-12-28 22:09:17 +00:00
shift; while getopts :r:i:n:z:c:f: arg; do case ${arg} in
2006-05-09 00:26:04 +00:00
i) ezjail_imageaction=${OPTARG};;
2006-05-03 00:23:49 +00:00
r) ezjail_setrunnable=${OPTARG};;
2006-11-15 18:51:22 +00:00
n) ezjail_new_name=${OPTARG};;
2009-12-28 22:09:17 +00:00
z) ezjail_new_zfs_datasets=${OPTARG};;
c) ezjail_new_cpuset=${OPTARG};;
2010-01-11 03:06:27 +00:00
f) ezjail_new_fib=${OPTARG}
[ "${ezjail_new_fib}" -ge 0 ] || exerr "Error: fib number has to be an integer.";;
2006-05-05 00:59:54 +00:00
?) exerr ${ezjail_usage_config};;
2006-05-03 00:23:49 +00:00
esac; done; shift $(( ${OPTIND} - 1 ))
2006-04-15 20:48:26 +00:00
2006-05-04 15:06:59 +00:00
[ $# -eq 1 ] || exerr ${ezjail_usage_config}
2006-04-15 20:48:26 +00:00
2006-04-15 20:20:12 +00:00
# Jail name mandatory
fetchjailinfo $1
2010-01-11 03:06:27 +00:00
# Check for existence of jail in our records
2007-10-07 21:22:28 +00:00
[ "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}."
2006-04-15 20:20:12 +00:00
# Nothing to be configured?
2010-01-11 03:06:27 +00:00
[ -n "${ezjail_setrunnable}" -o -n "${ezjail_new_name}" -o -n "${ezjail_imageaction}" -o -n "${ezjail_new_zfs_datasets}" -o -n "${ezjail_new_cpuset}" -o -n "${ezjail_new_fib}" ] || exerr ${ezjail_usage_config}
2006-11-15 18:51:22 +00:00
2010-01-11 03:06:27 +00:00
[ -n "${ezjail_id}" -a -n "${ezjail_new_name}" ] && exerr "Error: Jail ${ezjail_name} appears to be still running.\n '${ezjail_admin} stop ${ezjail_name}' it first."
[ -n "${ezjail_id}" ] && [ -n "${ezjail_new_zfs_datasets}" -o -n "${ezjail_new_fib}" ] && echo "Warning: New settings for running jail ${ezjail_name} will only take effect when next restarting it."
# Mark old config as source for new config written later
[ -n "${ezjail_new_name}" -o -n "${ezjail_new_zfs_datasets}" -o -n "${ezjail_new_cpuset}" -o -n "${ezjail_new_fib}" ] && ezjail_old_config="${ezjail_config}"
2006-11-15 18:51:22 +00:00
2010-01-11 03:06:27 +00:00
[ "${ezjail_new_zfs_datasets}" ] && ezjail_zfs_datasets="${ezjail_new_zfs_datasets}"
[ "${ezjail_new_fib}" ] && ezjail_fib="${ezjail_new_fib}"
if [ "${ezjail_new_cpuset}" ]; then
# Configure the new cpuset if the jail is currently running
[ "${ezjail_id}" ] && /usr/bin/cpuset -l ${ezjail_new_cpuset} -j ${ezjail_id} || exerr "Error: The defined cpuset is malformed."
ezjail_cpuset="${ezjail_new_cpuset}"
fi
# Do we want a new name for our jail?
if [ "${ezjail_new_name}" -a "${ezjail_new_name}" != "${ezjail_name}" ]; then
2006-11-15 18:51:22 +00:00
# Cannot rename an attached jail
2008-07-10 18:55:39 +00:00
[ "${ezjail_attached}" ] && exerr "Error: Jail image file ${ezjail_image} is attached as ${ezjail_device}.\n '${ezjail_admin} config -i detach ${ezjail_name}' it first."
2006-11-15 18:51:22 +00:00
2010-01-11 03:06:27 +00:00
# Save some old values
ezjail_old_hostname="${ezjail_hostname}"
ezjail_old_safename="${ezjail_safename}"
ezjail_old_rootdir="${ezjail_rootdir}"
ezjail_old_image="${ezjail_image}"
2006-11-15 18:51:22 +00:00
# The new values for the jail
2010-01-11 03:06:27 +00:00
ezjail_hostname=`echo -n ${ezjail_new_name} | tr '/~' '__'`
ezjail_safename=`echo -n "${ezjail_new_name}" | tr -c '[:alnum:]' _`
ezjail_config="${ezjail_jailcfgs}/${ezjail_safename}"
[ "${ezjail_old_config}" = "${ezjail_old_config%.norun}" ] || ezjail_config="${ezjail_jailcfgs}/${ezjail_safename}.norun"
# If rootdir is in our jails directory, and was auto generated, also rename the root
if [ "${ezjail_old_rootdir}" = "${ezjail_jaildir}/${ezjail_old_hostname}" ]; then
ezjail_rootdir=`dirname -- ${ezjail_rootdir}`/${ezjail_hostname}
# since we just used the old rootdir prefix and added the new hostname,
# we might end up at an existing directory
[ -e "${ezjail_rootdir}" ] && exerr "Error: An object already exists at ${ezjail_rootdir}, cant rename."
2010-01-15 13:59:38 +00:00
[ "${ezjail_imagetype}" = "zfs" ] || mv "${ezjail_old_rootdir}" "${ezjail_rootdir}"
2010-01-11 03:06:27 +00:00
fi
2006-11-15 18:51:22 +00:00
# This scenario really will only lead to real troubles in the 'fulljail'
# case, but I should still explain this to the user and not claim that
# "an ezjail would already exist"
2011-01-20 21:20:36 +00:00
case ${ezjail_hostname} in basejail|newjail|fulljail|flavours|ezjailtemp|ezjail_archives) exerr "Error: ezjail needs the ${ezjail_hostname} directory for its own administrative purposes.\n Please chose another name.";; esac
2009-12-28 22:09:17 +00:00
2006-11-15 18:51:22 +00:00
# jail names may lead to identical configs, eg. foo.bar.com == foo-bar.com
# so check, whether we might be running into problems
2010-01-11 03:06:27 +00:00
[ -e "${ezjail_config}" -o -e "${ezjail_config}.norun" ] && exerr "Error: An ezjail config already exists at ${ezjail_config}.\n Please chose another name."
2006-11-15 19:46:37 +00:00
2006-11-15 18:51:22 +00:00
# need to rename the image?
2010-01-11 03:06:27 +00:00
if [ "${ezjail_old_image}" ]; then
# Do we have an auto generated image name? Then auto generate the new one
if [ "${ezjail_old_rootdir}.img" = "${ezjail_old_image}" ]; then
ezjail_image="${ezjail_rootdir}.img"
[ -e "${ezjail_image}" ] && exerr "Error: An object already exists at ${ezjail_image}, cant rename image."
mv "${ezjail_old_image}" "${ezjail_image}"
2006-11-15 18:51:22 +00:00
else
2010-01-11 03:06:27 +00:00
echo "Warning: Image file for jail ${ezjail_new_name} remains ${ezjail_image}, as it was not auto generated"
2006-11-15 18:51:22 +00:00
fi
fi
2009-05-23 13:09:53 +00:00
2010-01-11 03:06:27 +00:00
# rename the filesystem, remounting is done by ZFS
2013-04-20 01:16:17 +00:00
[ "${ezjail_imagetype}" = "zfs" ] && /sbin/zfs rename ${ezjail_parentzfs}/${ezjail_old_hostname} ${ezjail_parentzfs}/${ezjail_hostname} && /sbin/zfs set mountpoint=${ezjail_rootdir} ${ezjail_parentzfs}/${ezjail_hostname} && rmdir ${ezjail_old_rootdir}
2006-11-15 18:51:22 +00:00
# rename fstab
2010-01-11 03:06:27 +00:00
rm -f "/etc/fstab.${ezjail_old_safename}"
echo -n > "/etc/fstab.${ezjail_safename}"
2010-01-24 18:33:09 +00:00
[ "${ezjail_imagetype}" -a "${ezjail_imagetype}" != "zfs" ] && \
2010-01-11 03:06:27 +00:00
echo ${ezjail_rootdir}.device ${ezjail_rootdir} ufs rw 0 0 >> "/etc/fstab.${ezjail_safename}"
echo ${ezjail_jailbase} ${ezjail_rootdir}/basejail nullfs ro 0 0 >> "/etc/fstab.${ezjail_safename}"
2006-11-15 18:51:22 +00:00
# usually that doesnt go smoothly, but the user wanted it
# that way ;)
2010-01-11 03:06:27 +00:00
echo "Jail has been renamed. You might want to check ${ezjail_config} and /etc/fstab.${ezjail_safename} to ensure everything has gone smoothly."
2006-11-15 19:07:09 +00:00
echo "Also check settings in your Jail's /etc/ directory (especially /etc/rc.conf)."
2009-12-28 22:09:17 +00:00
fi
2010-01-11 03:06:27 +00:00
if [ "${ezjail_old_config}" ]; then
ezjail_tmpconfig=`mktemp -u "${ezjail_config}".XXXXXX` || exerr "Error: Could not write new config.\n You will have to manually fix ${ezjail_old_config}. Sorry."
2009-12-28 22:09:17 +00:00
2010-01-11 03:06:27 +00:00
writejailinfo "${ezjail_tmpconfig}" "${ezjail_old_config}"
mv "${ezjail_tmpconfig}" "${ezjail_config}"
[ "${ezjail_config}" != "${ezjail_old_config}" ] && rm -f "${ezjail_old_config}"
2009-12-28 22:09:17 +00:00
fi
2007-10-07 21:22:28 +00:00
case "${ezjail_setrunnable}" in
run) [ "${ezjail_config}" = "${ezjail_config%.norun}" ] || mv "${ezjail_config}" "${ezjail_config%.norun}";;
2010-01-11 03:06:27 +00:00
norun) [ "${ezjail_config}" = "${ezjail_config%.norun}" ] && mv "${ezjail_config}" "${ezjail_config}.norun";;
2013-09-01 13:09:43 +00:00
test) [ "${ezjail_config}" = "${ezjail_config%.norun}" ] && echo "Jail ${ezjail_name} is runnable" || echo "Jail ${ezjail_name} is not runnable";;
2006-04-15 20:20:12 +00:00
esac
2007-10-07 21:22:28 +00:00
[ "${ezjail_imageaction}" -a -z "${ezjail_image}" ] && exerr "Error: Jail ${ezjail_name} not an image jail."
2006-05-09 00:26:04 +00:00
2006-05-09 02:00:04 +00:00
case "${ezjail_imageaction}" in
2006-05-09 00:26:04 +00:00
attach)
# Check, if image already attached
2007-10-07 21:22:28 +00:00
[ "${ezjail_attached}" ] && exerr "Error: Jail image file ${ezjail_image} already attached as ${ezjail_device}."
2006-05-09 00:26:04 +00:00
2008-07-09 10:37:11 +00:00
# Actually, just "attaching" is not enough, we need to mount
mount_images
2006-05-09 00:26:04 +00:00
;;
detach)
2006-05-09 02:00:04 +00:00
# Check, if image really attached or running
2008-07-10 18:55:39 +00:00
[ "${ezjail_id}" ] && exerr "Error: Jail ${ezjail_name} still running\n Can not detach.\n '${ezjail_admin} stop ${ezjail_name}' it first."
2007-10-07 21:22:28 +00:00
[ "${ezjail_attached}" ] || exerr "Error: Jail image file ${ezjail_name} is not attached."
2006-05-09 00:26:04 +00:00
# Unmount/detach everything
detach_images keep
2007-01-15 14:03:46 +00:00
;;
fsck)
# Check, if image already attached
2007-10-07 21:22:28 +00:00
[ "${ezjail_attached}" ] && exerr "Error: Jail image file ${ezjail_image} already attached as ${ezjail_device}."
rm -f "${ezjail_devicelink}"
2007-01-15 14:03:46 +00:00
# Attach images by type
attach_images
2007-02-22 16:01:09 +00:00
# Clean image
2007-10-07 21:22:28 +00:00
fsck -t ufs "/dev/${ezjail_device}"
2007-01-15 14:03:46 +00:00
# Detach images by type
detach_images keep
2006-05-09 00:26:04 +00:00
;;
2006-05-09 00:33:14 +00:00
esac
2005-09-05 02:51:02 +00:00
;;
2014-06-09 23:30:46 +02:00
######################## ezjail-admin TROUBLESHOOT ########################
troubleshoot)
2014-06-09 23:55:13 +02:00
2014-06-10 00:31:05 +02:00
# XXX TOOD:
2014-06-09 23:55:13 +02:00
# Start with some simple tests, check for basejail
# Check if we are ourself jailed
# Check if all IPv4 addresses are either configured already or in
# interface config line
# Check for duplicate non unique IPv4 addresses
# Check if all necessary file systems for zfs jails are mounted
# For zfs jails check, if zpool is full
2014-06-10 00:11:54 +02:00
# Check if all mount points inside the jails are there
2014-06-09 23:55:13 +02:00
2014-06-09 23:30:46 +02:00
shift
2014-06-10 13:22:41 +02:00
[ $# -gt 0 ] && cd ${ezjail_jailcfgs} && set - `ls -A`
2014-06-09 23:30:46 +02:00
for ezjail; do
fetchjailinfo ${ezjail%.norun}
# Check for existence of jail in our records
[ "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}."
done
;;
2005-09-05 02:51:02 +00:00
*)
2008-03-22 11:35:07 +00:00
exerr "${ezjail_usage_ezjailadmin}"
2005-09-03 15:42:28 +00:00
;;
esac
2010-01-08 23:45:42 +00:00
exit 0