From 0c382a5e74dbf67f79a22bdf79c48e2e3ae973b1 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Wed, 26 Sep 2007 15:06:40 +0000 Subject: Started experimenting with archive and restore of jails --- ezjail-admin | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 7 deletions(-) (limited to 'ezjail-admin') diff --git a/ezjail-admin b/ezjail-admin index 372f30e..4fcd0b8 100755 --- a/ezjail-admin +++ b/ezjail-admin @@ -34,13 +34,14 @@ ezjail_dirlist="bin boot lib libexec rescue sbin usr/bin usr/games usr/include u case `uname -p` in amd64) ezjail_dirlist="${ezjail_dirlist} usr/lib32";; esac # Synopsis messages -ezjail_usage_ezjailadmin="Usage: ${ezjail_admin} [config|console|create|delete|install|list|update] {params}" +ezjail_usage_ezjailadmin="Usage: ${ezjail_admin} [archive|config|console|create|delete|install|list|update] {params}" ezjail_usage_install="Usage: ${ezjail_admin} install [-mps] [-h host] [-r release]" -ezjail_usage_create="Usage: ${ezjail_admin} create [-xbi] [-f flavour] [-r jailroot] [-s size] [-c bde|eli] [-C args] jailname jailip" +ezjail_usage_create="Usage: ${ezjail_admin} create [-xbi] [-f flavour] [-r jailroot] [-s size] [-c bde|eli] [-C args] [-r archive] jailname jailip" ezjail_usage_delete="Usage: ${ezjail_admin} delete [-w] jailname" ezjail_usage_update="Usage: ${ezjail_admin} update [-s sourcetree] [-i] [-pP]" ezjail_usage_config="Usage: ${ezjail_admin} config [-r run|norun] [-n newname] [-i attach|detach|fsck] jailname" ezjail_usage_console="Usage: ${ezjail_admin} console [-f] [-e command] jailname" +ezjail_usage_archive="Usage: ${ezjail_admin} archive [-f] [-r archive] [-d archivedir] jailname" ezjail_usage_list="Usage: ${ezjail_admin} list" ################################ @@ -221,11 +222,12 @@ case "$1" in ######################## ezjail-admin CREATE ######################## create) # Clean variables, prevent polution - unset ezjail_rootdir ezjail_flavour ezjail_softlink ezjail_image ezjail_imagetype ezjail_imageparams ezjail_imagesize ezjail_device ezjail_config ezjail_attachparams ezjail_exists ezjail_attachblocking ezjail_forceblocking ezjail_sourcedevice ezjail_rootdirempty - shift; while getopts :f:r:s:xbic:C: arg; do case ${arg} in + unset ezjail_rootdir ezjail_flavour ezjail_softlink ezjail_image ezjail_imagetype ezjail_imageparams ezjail_imagesize ezjail_device ezjail_config ezjail_attachparams ezjail_exists ezjail_attachblocking ezjail_forceblocking ezjail_sourcedevice ezjail_rootdirempty ezjail_fromarchive + shift; while getopts :f:r:s:xbic:C:r: arg; do case ${arg} in x) ezjail_exists="YES";; r) ezjail_rootdir="${OPTARG}";; f) ezjail_flavour="${OPTARG}";; + r) ezjail_fromarchive="${OPTARG}";; c) ezjail_imagetype="${OPTARG}";; C) ezjail_imageparams="${OPTARG}";; b) ezjail_forceblocking="YES";; @@ -298,6 +300,11 @@ create) # do some sanity checks on the selected flavour (if any) [ "${ezjail_flavour}" -a ! -d ${ezjail_flavours}/${ezjail_flavour} ] && exerr "Error: Flavour config directory ${ezjail_flavours}/${ezjail_flavour} not found." + # check for restore circumstances, normally this is invoked by the restore command + [ "${ezjail_fromarchive}" -a "${ezjail_exists}" ] && exerr "Error: Cannot restore a jail that exists." + [ "${ezjail_fromarchive}" -a "${ezjail_flavour}" ] && exerr "Error: Cannot apply flavours to a restored jail." + [ "${ezjail_fromarchive}" -a ! -r ${ezjail_fromarchive} ] & exerr "Error: Cannot restore from non existin garchive: ${ezjail_fromarchive}." + # # All sanity checks that may lead to errors are hopefully passed here # @@ -320,6 +327,7 @@ create) # Now create jail disc image touch "${ezjail_image}" + echo "Creating jail image ${ezjail_image}. This may take a while." if [ "${ezjail_imageblockcount}" -gt 0 ]; then dd if=${ezjail_sourcedevice} of="${ezjail_image}" bs=1m count=${ezjail_imageblockcount} || exerr "Error: Could not (or not fully) create the image file. You might want to check (and possibly remove) the file ${ezjail_image}. The image size provided was ${ezjail_imagesize}." fi @@ -368,12 +376,16 @@ create) fi fi - # now take a copy of our template jail - if [ -z "${ezjail_exists}" ]; then + if [ -z "${ezjail_fromarchive}" ]; then + mkdir -p ${ezjail_rootdir} && tar xfp ${ezjail_fromarchive} -C ${ezjail_rootdir} --strip-components 1 ezjail + [ $? = 0 ] || detach_images || exerr "Error: Could not extract archive from ${ezjail_fromarchive}." + elif [ -z "${ezjail_exists}" ]; then + # now take a copy of our template jail mkdir -p ${ezjail_rootdir} && cd ${ezjail_jailtemplate} && find * | cpio -p -v ${ezjail_rootdir} > /dev/null [ $? = 0 ] || detach_images || exerr "Error: Could not copy template jail." fi + # if a soft link is necessary, create it now [ "${ezjail_softlink}" ] && ln -s ${ezjail_rootdir} ${ezjail_softlink} @@ -687,11 +699,50 @@ console) [ -n "${ezjail_execute}" ] && exec jexec ${ezjail_id} ${ezjail_execute} exec jexec ${ezjail_id} ${ezjail_default_execute} + ;; +######################## ezjail-admin ARCHIVE ######################## +archive) + # Clean variables, prevent polution + unset ezjail_archive ezjail_force ezjail_taroption + + shift; while getopts :fr:d: arg; do case ${arg} in + f) ezjail_force=${OPTARG};; + r) ezjail_archive=${OPTARG};; + d) ezjail_archivedir=${OPTARG};; + ?) exerr ${ezjail_usage_archive};; + esac; done; shift $(( ${OPTIND} - 1 )) + + [ $# -eq 1 ] || exerr ${ezjail_usage_archive} + + # Jail name mandatory + fetchjailinfo $1 + + # check for existence of jail in our records + [ -n "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}." + + # if jail is still running, refuse to go any further - unless forced + [ -n "${ezjail_id}" -a -z "${ezjail_force}" ] && exerr "Error: Jail appears to be still running, stop it first or [-f]orce archiving." + + # if no archive name was specified, make one up + [ -z "${ezjail_archive}" ] && ezjail_archive="${ezjail_safename}-`date +%Y%m%d%H%M.%S`.tar.bz2 + + # prepend archive directory + ezjail_archive="${ezjail_archivedir%/}"/"${ezjail_archive}" + + case ${ezjail_archive} in + *.tar.bz|*.tar.bz2|*.tbz|*.tar.bzip ) ezjail_taroption="j" ;; + *.tar.gz|*.tgz|*.tar.gzip) ezjail_taroption="z" ;; + *.tar) ;; + *) ezjail_archive="${ezjail_archive}.tar" ;; + esac; + +# TODO: tar cpf${ezjail_taroption} ${ezjail_archive} --atime-preserve --one-file-system ${ezjail_config} -C ${ezjail_root} + ;; ######################## ezjail-admin CONFIG ######################## config) # Clean variables, prevent polution - unset ezjail_setrunnable ezjail_imageaction + unset ezjail_setrunnable ezjail_imageaction ezjail_new_name shift; while getopts :r:i:n: arg; do case ${arg} in i) ezjail_imageaction=${OPTARG};; -- cgit v1.2.3