From 9725ed4a85232ac02d14f1f78aa9186cf2963d7b Mon Sep 17 00:00:00 2001 From: erdgeist Date: Wed, 15 Nov 2006 18:51:22 +0000 Subject: HEADS UP: renaming jails has been introduced. Lots of checking will probably be required, this version is considered unstable. Also two minr style glitches have been fixed. --- ezjail-admin | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 106 insertions(+), 5 deletions(-) diff --git a/ezjail-admin b/ezjail-admin index c4c754a..655e939 100755 --- a/ezjail-admin +++ b/ezjail-admin @@ -38,7 +38,7 @@ ezjail_usage_install="Usage: ${ezjail_admin} install [-mps] [-h host] [-r releas ezjail_usage_create="Usage: ${ezjail_admin} create [-xbi] [-f flavour] [-r jailroot] [-s size] [-c bde|eli] [-C args] 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] [-i attach|detach] jailname" +ezjail_usage_config="Usage: ${ezjail_admin} config [-r run|norun] [-n newname] [-i attach|detach] jailname" ezjail_usage_list="Usage: ${ezjail_admin} list" ################################ @@ -239,7 +239,7 @@ create) # jail names may lead to identical configs, eg. foo.bar.com == foo-bar.com # so check, whether we might be running into problems - [ -e ${ezjail_config} ] && exerr "Error: an ezjail config already exists at ${ezjail_config}. Please rename the ezjail." + [ -e ${ezjail_config} -o -e ${ezjail_config}.norun ] && exerr "Error: an ezjail config already exists at ${ezjail_config}. Please rename the ezjail." # if jail root specified on command line is not absolute, make it absolute # inside our jail directory @@ -343,7 +343,7 @@ create) # if the automount feature is not disabled, this fstab entry for new jail # will be obeyed echo -n > /etc/fstab.${ezjail_safename} - [ "${ezjail_imagetype}" ] && \ + [ -n "${ezjail_imagetype}" ] && \ 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} @@ -596,9 +596,10 @@ config) # Clean variables, prevent polution unset ezjail_setrunnable ezjail_imageaction - shift; while getopts :r:i: arg; do case ${arg} in + shift; while getopts :r:i:n: arg; do case ${arg} in i) ezjail_imageaction=${OPTARG};; r) ezjail_setrunnable=${OPTARG};; + n) ezjail_new_name=${OPTARG};; ?) exerr ${ezjail_usage_config};; esac; done; shift $(( ${OPTIND} - 1 )) @@ -611,7 +612,107 @@ config) [ -n "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}." # Nothing to be configured? - [ -z "${ezjail_setrunnable}" -a -z "${ezjail_imageaction}" ] && echo "Warning: No config option specified." + [ -z "${ezjail_setrunnable}" -a -z "${ezjail_new_name}" -a -z "${ezjail_imageaction}" ] && echo "Warning: No config option specified." + + # Do we want a new name for our jail? + if [ -n "${ezjail_new_name}" ]; then + # if jail is still running, refuse to go any further + [ -n "${ezjail_id}" ] && exerr "Error: Jail appears to be still running, stop it first." + + # Cannot rename an attached jail + [ -n "${ezjail_attached}" ] && exerr "Error: Jail image file ${ezjail_image} is attached as ${ezjail_device}. '${ezjail_admin} config -i detach' it first." + + # The new values for the jail + ezjail_new_hostname=`echo -n ${ezjail_new_name} | tr '/~' '__'` + ezjail_new_safename=`echo -n "${ezjail_new_name}" | tr -c '[:alnum:]' _` + ezjail_new_rootdir=`basename -- ${ezjail_rootdir}`/${ezjail_new_hostname} + ezjail_new_config=${ezjail_jailcfgs}/${ezjail_new_safename} + [ "${ezjail_config}" = "${ezjail_config%.norun}" ] || ezjail_new_config=${ezjail_jailcfgs}/${ezjail_new_safename}.norun + ezjail_new_softlink=${ezjail_jaildir}/`basename -- ${ezjail_new_rootdir}` + + # those are just copied + eval ezjail_new_ip=\"\$jail_${ezjail_safename}_ip\" + eval ezjail_new_exec=\"\$jail_${ezjail_safename}_exec\" + eval ezjail_new_mount_enable=\"\$jail_${ezjail_safename}_mount_enable\" + eval ezjail_new_devfs_enable=\"\$jail_${ezjail_safename}_devfs_enable\" + eval ezjail_new_devfs_ruleset=\"\$jail_${ezjail_safename}_devfs_ruleset\" + eval ezjail_new_procfs_enable=\"\$jail_${ezjail_safename}_devfs_enable\" + eval ezjail_new_fdescfs_enable=\"\$jail_${ezjail_safename}_fdescfs_enable\" + eval ezjail_new_attachparams=\"\$jail_${ezjail_safename}_attachparams\" + eval ezjail_new_attachblocking=\"\$jail_${ezjail_safename}_attachblocking\" + eval ezjail_new_forceblocking=\"\$jail_${ezjail_safename}_forceblocking\" + eval ezjail_new_imagetype=\"\$jail_${ezjail_safename}_imagetype\" + + # 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" + case ${ezjail_new_hostname} in basejail|newjail|fulljail|flavours|ezjailtemp) exerr "Error: ezjail needs the ${ezjail_new_hostname} directory for its own administrative purposes. Please rename the ezjail.";; esac + + # jail names may lead to identical configs, eg. foo.bar.com == foo-bar.com + # so check, whether we might be running into problems + [ -e ${ezjail_new_config} -o -e ${ezjail_new_config}.norun ] && exerr "Error: an ezjail config already exists at ${ezjail_new_config}. Please rename the ezjail." + + # need to rename the image? + if [ -n "${ezjail_image}" ] + # Do we have an auto generated image name? + if [ "${ezjail_rootdir}.img" = "${ezjail_image}" ] + ezjail_newimage=${ezjail_newrootdir}.img} + mv ${ezjail_image} ${ezjail_new_image} + else + ezjail_new_image=${ezjail_image} + echo "Warning: Image file for jail ${ezjail_name} remains ${ezjail_image}, as it was not auto generated" + fi + else + unset ezjail_new_image + fi + + # adjust softlink + if [ -L "${ezjail_softlink}" ]; then + rm ${ezjail_softlink} + ln -s ${ezjail_new_rootdir} ${ezjail_new_softlink} + fi + + # rename rootdir + mv ${ezjail_rootdir} ${ezjail_new_rootdir} + + # rename fstab + echo -n > /etc/fstab.${ezjail_new_safename} + [ -n "${ezjail_new_imagetype}" ] && \ + echo ${ezjail_new_rootdir}.device ${ezjail_new_rootdir} ufs rw 0 0 >> /etc/fstab.${ezjail_new_safename} + echo ${ezjail_jailbase} ${ezjail_new_rootdir}/basejail nullfs ro 0 0 >> /etc/fstab.${ezjail_new_safename} + rm /etc/fstab.${ezjail_safename} + + # rename config file, preserve comments + ( + grep -e ^\# ${ezjail_config} + echo + echo export jail_${ezjail_new_safename}_hostname=\"${ezjail_new_hostname}\" + echo export jail_${ezjail_new_safename}_ip=\"${ezjail_new_ip}\" + echo export jail_${ezjail_new_safename}_rootdir=\"${ezjail_new_rootdir}\" + echo export jail_${ezjail_new_safename}_exec=\"${ezjail_new_exec}\" + echo export jail_${ezjail_new_safename}_mount_enable=\"${ezjail_new_mount_enable}\" + echo export jail_${ezjail_new_safename}_devfs_enable=\"${ezjail_new_devfs_enable}\" + echo export jail_${ezjail_new_safename}_devfs_ruleset=\"${ezjail_new_devfs_ruleset\" + echo export jail_${ezjail_new_safename}_procfs_enable=\"${ezjail_new_procfs_enable}\" + echo export jail_${ezjail_new_safename}_fdescfs_enable=\"${ezjail_new_fdescfs_enable}\" + echo export jail_${ezjail_new_safename}_image=\"${ezjail_new_image}\" + echo export jail_${ezjail_new_safename}_imagetype=\"${ezjail_new_imagetype}\" + echo export jail_${ezjail_new_safename}_attachparams=\"${ezjail_new_attachparams}\" + echo export jail_${ezjail_new_safename}_attachblocking=\"${ezjail_new_attachblocking}\" + echo export jail_${ezjail_new_safename}_forceblocking=\"${ezjail_new_forceblocking}\" + ) > ${ezjail_new_config} + + # remove old config + rm ${ezjail_config} + + # usually that doesnt go smoothly, but the user wanted it + # that way ;) + echo Jail has been renamed. You might want to check ${ezjail_new_config} and /etc/fstab.${ezjail_new_safename} to ensure everything has gone smoothly. + echo Also check settings in your Jail's /etc/ directory (especially /etc/rc.conf). + + # reread config + fetchjailinfo ${ezjail_new_safename} + fi case ${ezjail_setrunnable} in run) [ "${ezjail_config}" = "${ezjail_config%.norun}" ] || mv ${ezjail_config} ${ezjail_config%.norun};; -- cgit v1.2.3