From c6aded8d0ae980663c9bd97ddc87aa94d1bc65f5 Mon Sep 17 00:00:00 2001 From: Chris Wells Date: Sun, 30 Aug 2020 15:37:54 -0400 Subject: [PATCH] Ensure target is specified and exists in 1 place instead of many. Require target to be running for commands that need it. Closes #239. --- usr/local/bin/bastille | 58 ++++++++++++++++----- usr/local/share/bastille/clone.sh | 62 ++++++++++------------ usr/local/share/bastille/cmd.sh | 12 +---- usr/local/share/bastille/console.sh | 13 +---- usr/local/share/bastille/convert.sh | 12 +---- usr/local/share/bastille/cp.sh | 14 ++--- usr/local/share/bastille/edit.sh | 16 ++---- usr/local/share/bastille/export.sh | 59 ++++++++++----------- usr/local/share/bastille/htop.sh | 12 +---- usr/local/share/bastille/limits.sh | 16 ++---- usr/local/share/bastille/mount.sh | 13 +---- usr/local/share/bastille/pkg.sh | 12 +---- usr/local/share/bastille/rename.sh | 78 +++++++++++++--------------- usr/local/share/bastille/service.sh | 13 +---- usr/local/share/bastille/stop.sh | 18 +------ usr/local/share/bastille/sysrc.sh | 13 +---- usr/local/share/bastille/template.sh | 13 +---- usr/local/share/bastille/top.sh | 13 +---- usr/local/share/bastille/umount.sh | 12 +---- usr/local/share/bastille/zfs.sh | 23 ++------ 20 files changed, 162 insertions(+), 320 deletions(-) diff --git a/usr/local/bin/bastille b/usr/local/bin/bastille index 0ec31b97..f2bafe35 100755 --- a/usr/local/bin/bastille +++ b/usr/local/bin/bastille @@ -133,20 +133,48 @@ version|-v|--version) help|-h|--help) usage ;; -esac - -# Filter out all non-commands -case "${CMD}" in -bootstrap|clone|cmd|console|convert|cp|create) - ;; -destroy|edit|export|htop|import|limits|list|mount) - ;; -pkg|rdr|rename|restart|service|start|stop|sysrc|umount) +bootstrap|create|destroy|import|list|rdr|restart|start|update|upgrade|verify) + # Nothing "extra" to do for these commands. -- cwells ;; -template|top|update|upgrade|verify|zfs) +clone|cmd|console|convert|cp|edit|export|htop|limits|mount|pkg|rename|service|stop|sysrc|template|top|umount|zfs) + # Parse the target and ensure it exists. -- cwells + if [ $# -eq 0 ]; then # No target was given, so show the command's help. -- cwells + PARAMS='help' + elif [ "${1}" != 'help' ] && [ "${1}" != '-h' ] && [ "${1}" != '--help' ]; then + TARGET="${1}" + shift + + if [ "${TARGET}" = 'ALL' ]; then + JAILS=$(jls name) + else + JAILS=$(jls name | awk "/^${TARGET}$/") + + # Ensure the target exists. -- cwells + if [ ! -d "${bastille_jailsdir}/${TARGET}" ]; then + error_exit "[${TARGET}]: Not found." + fi + + case "${CMD}" in + cmd|console|htop|pkg|service|stop|sysrc|template|top) + # Require the target to be running. -- cwells + if [ ! "$(jls name | awk "/^${TARGET}$/")" ]; then + error_exit "[${TARGET}]: Not started. See 'bastille start ${TARGET}'." + fi + ;; + convert|rename) + # Require the target to be stopped. -- cwells + if [ "$(jls name | awk "/^${TARGET}$/")" ]; then + error_exit "${TARGET} is running. See 'bastille stop ${TARGET}'." + fi + ;; + esac + fi + export TARGET + export JAILS + fi ;; -*) -usage +*) # Filter out all non-commands + usage ;; esac @@ -157,7 +185,11 @@ if [ -f "${SCRIPTPATH}" ]; then : "${SH:=sh}" - exec "${SH}" "${SCRIPTPATH}" "$@" + if [ -n "${PARAMS}" ]; then + exec "${SH}" "${SCRIPTPATH}" "${PARAMS}" + else + exec "${SH}" "${SCRIPTPATH}" "$@" + fi else error_exit "${SCRIPTPATH} not found." fi diff --git a/usr/local/share/bastille/clone.sh b/usr/local/share/bastille/clone.sh index 4f4e5e11..ec824141 100644 --- a/usr/local/share/bastille/clone.sh +++ b/usr/local/share/bastille/clone.sh @@ -42,14 +42,12 @@ help|-h|--help) ;; esac -if [ $# -ne 3 ]; then +if [ $# -ne 2 ]; then usage fi -TARGET="${1}" -NEWNAME="${2}" -IP="${3}" -shift +NEWNAME="${1}" +IP="${2}" validate_ip() { IPX_ADDR="ip4.addr" @@ -146,39 +144,35 @@ update_fstab() { clone_jail() { # Attempt container clone - if [ -d "${bastille_jailsdir}/${TARGET}" ]; then - echo -e "${COLOR_GREEN}Attempting to clone '${TARGET}' to ${NEWNAME}...${COLOR_RESET}" - if ! [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then - if [ "${bastille_zfs_enable}" = "YES" ]; then - if [ -n "${bastille_zfs_zpool}" ]; then - # Replicate the existing container - DATE=$(date +%F-%H%M%S) - zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}" - zfs send -R "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}" | zfs recv "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}" - - # Cleanup source temporary snapshots - zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_clone_${DATE}" - zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}" - - # Cleanup target temporary snapshots - zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}/root@bastille_clone_${DATE}" - zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}@bastille_clone_${DATE}" - fi - else - # Just clone the jail directory - # Check if container is running - if [ -n "$(jls name | awk "/^${TARGET}$/")" ]; then - error_exit "${TARGET} is running, See 'bastille stop ${TARGET}'." - fi - - # Perform container file copy(archive mode) - cp -a "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}" + echo -e "${COLOR_GREEN}Attempting to clone '${TARGET}' to ${NEWNAME}...${COLOR_RESET}" + if ! [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then + if [ "${bastille_zfs_enable}" = "YES" ]; then + if [ -n "${bastille_zfs_zpool}" ]; then + # Replicate the existing container + DATE=$(date +%F-%H%M%S) + zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}" + zfs send -R "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}" | zfs recv "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}" + + # Cleanup source temporary snapshots + zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_clone_${DATE}" + zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}" + + # Cleanup target temporary snapshots + zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}/root@bastille_clone_${DATE}" + zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}@bastille_clone_${DATE}" fi else - error_exit "${NEWNAME} already exists." + # Just clone the jail directory + # Check if container is running + if [ -n "$(jls name | awk "/^${TARGET}$/")" ]; then + error_exit "${TARGET} is running. See 'bastille stop ${TARGET}'." + fi + + # Perform container file copy(archive mode) + cp -a "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}" fi else - error_exit "${TARGET} not found. See bootstrap." + error_exit "${NEWNAME} already exists." fi # Generate jail configuration files diff --git a/usr/local/share/bastille/cmd.sh b/usr/local/share/bastille/cmd.sh index 3859f7a6..dcb63506 100644 --- a/usr/local/share/bastille/cmd.sh +++ b/usr/local/share/bastille/cmd.sh @@ -41,20 +41,10 @@ help|-h|--help) ;; esac -if [ $# -lt 2 ]; then +if [ $# -eq 0 ]; then usage fi -TARGET="${1}" -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi - for _jail in ${JAILS}; do echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" jexec -l "${_jail}" "$@" diff --git a/usr/local/share/bastille/console.sh b/usr/local/share/bastille/console.sh index b17a7a49..149291d1 100644 --- a/usr/local/share/bastille/console.sh +++ b/usr/local/share/bastille/console.sh @@ -31,7 +31,7 @@ . /usr/local/share/bastille/common.sh usage() { - error_exit "Usage: bastille console TARGET [user]'." + error_exit "Usage: bastille console TARGET [user]'" } # Handle special-case commands first. @@ -41,21 +41,12 @@ help|-h|--help) ;; esac -if [ $# -gt 2 ] || [ $# -lt 1 ]; then +if [ $# -gt 1 ]; then usage fi -TARGET="${1}" -shift USER="${1}" -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi - validate_user() { if jexec -l "${_jail}" id "${USER}" >/dev/null 2>&1; then USER_SHELL="$(jexec -l "${_jail}" getent passwd "${USER}" | cut -d: -f7)" diff --git a/usr/local/share/bastille/convert.sh b/usr/local/share/bastille/convert.sh index 69750f8d..7ecc76cf 100644 --- a/usr/local/share/bastille/convert.sh +++ b/usr/local/share/bastille/convert.sh @@ -32,7 +32,7 @@ . /usr/local/etc/bastille/bastille.conf usage() { - error_exit "Usage: bastille convert TARGET." + error_exit "Usage: bastille convert TARGET" } # Handle special-case commands first. @@ -42,13 +42,10 @@ help|-h|--help) ;; esac -if [ $# -gt 1 ] || [ $# -lt 1 ]; then +if [ $# -ne 0 ]; then usage fi -TARGET="${1}" -shift - convert_symlinks() { # Work with the symlinks, revert on first cp error if [ -d "${bastille_releasesdir}/${RELEASE}" ]; then @@ -131,11 +128,6 @@ start_convert() { fi } -# Check if container is running -if [ -n "$(jls name | awk "/^${TARGET}$/")" ]; then - error_exit "${TARGET} is running. See 'bastille stop'." -fi - # Check if is a thin container if [ ! -d "${bastille_jailsdir}/${TARGET}/root/.bastille" ]; then error_exit "${TARGET} is not a thin container." diff --git a/usr/local/share/bastille/cp.sh b/usr/local/share/bastille/cp.sh index 8d55009a..8c59ba69 100644 --- a/usr/local/share/bastille/cp.sh +++ b/usr/local/share/bastille/cp.sh @@ -42,20 +42,12 @@ help|-h|--help) ;; esac -if [ $# -gt 3 ] || [ $# -lt 3 ]; then +if [ $# -ne 2 ]; then usage fi -TARGET="${1}" -CPSOURCE="${2}" -CPDEST="${3}" - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi +CPSOURCE="${1}" +CPDEST="${2}" for _jail in ${JAILS}; do bastille_jail_path="$(jls -j "${_jail}" path)" diff --git a/usr/local/share/bastille/edit.sh b/usr/local/share/bastille/edit.sh index 8eb958f8..c7f25173 100644 --- a/usr/local/share/bastille/edit.sh +++ b/usr/local/share/bastille/edit.sh @@ -42,26 +42,16 @@ help|-h|--help) ;; esac -if [ $# -gt 2 ] || [ $# -lt 1 ]; then +if [ $# -gt 1 ]; then usage -fi - -TARGET="${1}" -if [ $# == 2 ]; then - TARGET_FILENAME="${2}" +elif [ $# -eq 1 ]; then + TARGET_FILENAME="${1}" fi if [ -z "${EDITOR}" ]; then EDITOR=vi fi -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(bastille list jails) -fi -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(bastille list jails | awk "/^${TARGET}$/") -fi - for _jail in ${JAILS}; do if [ -n "${TARGET_FILENAME}" ]; then "${EDITOR}" "${bastille_jailsdir}/${_jail}/${TARGET_FILENAME}" diff --git a/usr/local/share/bastille/export.sh b/usr/local/share/bastille/export.sh index d377736b..65863f97 100644 --- a/usr/local/share/bastille/export.sh +++ b/usr/local/share/bastille/export.sh @@ -42,50 +42,43 @@ help|-h|--help) ;; esac -if [ $# -gt 1 ] || [ $# -lt 1 ]; then +if [ $# -ne 0 ]; then usage fi -TARGET="${1}" -shift - jail_export() { # Attempt to export the container DATE=$(date +%F-%H%M%S) - if [ -d "${bastille_jailsdir}/${TARGET}" ]; then - if [ "${bastille_zfs_enable}" = "YES" ]; then - if [ -n "${bastille_zfs_zpool}" ]; then - FILE_EXT="xz" - echo -e "${COLOR_GREEN}Exporting '${TARGET}' to a compressed .${FILE_EXT} archive.${COLOR_RESET}" - echo -e "${COLOR_GREEN}Sending zfs data stream...${COLOR_RESET}" - # Take a recursive temporary snapshot - zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" + if [ "${bastille_zfs_enable}" = "YES" ]; then + if [ -n "${bastille_zfs_zpool}" ]; then + FILE_EXT="xz" + echo -e "${COLOR_GREEN}Exporting '${TARGET}' to a compressed .${FILE_EXT} archive.${COLOR_RESET}" + echo -e "${COLOR_GREEN}Sending zfs data stream...${COLOR_RESET}" + # Take a recursive temporary snapshot + zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" - # Export the container recursively and cleanup temporary snapshots - zfs send -R "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" | \ - xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}" - zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_export_${DATE}" - zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" - fi - else - # Create standard backup archive - FILE_EXT="txz" - echo -e "${COLOR_GREEN}Exporting '${TARGET}' to a compressed .${FILE_EXT} archive...${COLOR_RESET}" - cd "${bastille_jailsdir}" && tar -cf - "${TARGET}" | xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}" + # Export the container recursively and cleanup temporary snapshots + zfs send -R "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" | \ + xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}" + zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_export_${DATE}" + zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" fi + else + # Create standard backup archive + FILE_EXT="txz" + echo -e "${COLOR_GREEN}Exporting '${TARGET}' to a compressed .${FILE_EXT} archive...${COLOR_RESET}" + cd "${bastille_jailsdir}" && tar -cf - "${TARGET}" | xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}" + fi - if [ "$?" -ne 0 ]; then - error_exit "Failed to export '${TARGET}' container." - else - # Generate container checksum file - cd "${bastille_backupsdir}" - sha256 -q "${TARGET}_${DATE}.${FILE_EXT}" > "${TARGET}_${DATE}.sha256" - echo -e "${COLOR_GREEN}Exported '${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}' successfully.${COLOR_RESET}" - exit 0 - fi + if [ "$?" -ne 0 ]; then + error_exit "Failed to export '${TARGET}' container." else - error_exit "Container '${TARGET}' does not exist." + # Generate container checksum file + cd "${bastille_backupsdir}" + sha256 -q "${TARGET}_${DATE}.${FILE_EXT}" > "${TARGET}_${DATE}.sha256" + echo -e "${COLOR_GREEN}Exported '${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}' successfully.${COLOR_RESET}" + exit 0 fi } diff --git a/usr/local/share/bastille/htop.sh b/usr/local/share/bastille/htop.sh index 96263b17..6f1bdf20 100644 --- a/usr/local/share/bastille/htop.sh +++ b/usr/local/share/bastille/htop.sh @@ -42,20 +42,10 @@ help|-h|--help) ;; esac -if [ $# -gt 1 ] || [ $# -lt 1 ]; then +if [ $# -ne 0 ]; then usage fi -TARGET="${1}" -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi - for _jail in ${JAILS}; do bastille_jail_path=$(jls -j "${_jail}" path) if [ ! -x "${bastille_jail_path}/usr/local/bin/htop" ]; then diff --git a/usr/local/share/bastille/limits.sh b/usr/local/share/bastille/limits.sh index d65eb81f..1d4de5cb 100644 --- a/usr/local/share/bastille/limits.sh +++ b/usr/local/share/bastille/limits.sh @@ -51,22 +51,12 @@ help|-h|--help) ;; esac -if [ $# -lt 3 ]; then +if [ $# -ne 2 ]; then usage fi -TARGET="${1}" -OPTION="${2}" -VALUE="${3}" -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi - -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi +OPTION="${1}" +VALUE="${2}" for _jail in ${JAILS}; do echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" diff --git a/usr/local/share/bastille/mount.sh b/usr/local/share/bastille/mount.sh index f3714128..21173ab7 100644 --- a/usr/local/share/bastille/mount.sh +++ b/usr/local/share/bastille/mount.sh @@ -44,18 +44,7 @@ esac if [ $# -lt 2 ]; then usage -fi - -TARGET=$1 -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -else - JAILS=$(jls name | awk "/^${TARGET}$/") -fi - -if [ $# -eq 2 ]; then +elif [ $# -eq 2 ]; then _fstab="$@ nullfs ro 0 0" else _fstab="$@" diff --git a/usr/local/share/bastille/pkg.sh b/usr/local/share/bastille/pkg.sh index 0cf6cac9..d3f7181c 100644 --- a/usr/local/share/bastille/pkg.sh +++ b/usr/local/share/bastille/pkg.sh @@ -41,20 +41,10 @@ help|-h|--help) ;; esac -if [ $# -lt 2 ]; then +if [ $# -lt 1 ]; then usage fi -TARGET="${1}" -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi - for _jail in ${JAILS}; do echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" jexec -l "${_jail}" /usr/sbin/pkg "$@" diff --git a/usr/local/share/bastille/rename.sh b/usr/local/share/bastille/rename.sh index 9f8d2bd2..14541fb2 100644 --- a/usr/local/share/bastille/rename.sh +++ b/usr/local/share/bastille/rename.sh @@ -32,7 +32,7 @@ . /usr/local/etc/bastille/bastille.conf usage() { - error_exit "Usage: bastille rename [TARGET] [NEW_NAME]" + error_exit "Usage: bastille rename TARGET NEW_NAME" } validate_name() { @@ -50,13 +50,11 @@ help|-h|--help) ;; esac -if [ $# -gt 2 ] || [ $# -lt 2 ]; then +if [ $# -ne 1 ]; then usage fi -TARGET="${1}" -NEWNAME="${2}" -shift +NEWNAME="${1}" update_jailconf() { # Update jail.conf @@ -90,43 +88,39 @@ update_fstab() { change_name() { # Attempt container name change - if [ -d "${bastille_jailsdir}/${TARGET}" ]; then - echo -e "${COLOR_GREEN}Attempting to rename '${TARGET}' to ${NEWNAME}...${COLOR_RESET}" - if [ "${bastille_zfs_enable}" = "YES" ]; then - if [ -n "${bastille_zfs_zpool}" ] && [ -n "${bastille_zfs_prefix}" ]; then - # Check and rename container ZFS dataset accordingly - # Perform additional checks in case of non-zfs existing containers - if zfs list | grep -qw "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}"; then - if ! zfs rename -f "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}"; then - error_exit "Can't rename '${TARGET}' dataset." - fi - else - # Check and rename container directory instead - if ! zfs list | grep -qw "jails/${TARGET}$"; then - mv "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}" - fi + echo -e "${COLOR_GREEN}Attempting to rename '${TARGET}' to ${NEWNAME}...${COLOR_RESET}" + if [ "${bastille_zfs_enable}" = "YES" ]; then + if [ -n "${bastille_zfs_zpool}" ] && [ -n "${bastille_zfs_prefix}" ]; then + # Check and rename container ZFS dataset accordingly + # Perform additional checks in case of non-zfs existing containers + if zfs list | grep -qw "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}"; then + if ! zfs rename -f "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}"; then + error_exit "Can't rename '${TARGET}' dataset." + fi + else + # Check and rename container directory instead + if ! zfs list | grep -qw "jails/${TARGET}$"; then + mv "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}" fi fi - else - # Check if container is a zfs/dataset before rename attempt - # Perform additional checks in case of bastille.conf miss-configuration - if zfs list | grep -qw "jails/${TARGET}$"; then - ZFS_DATASET_ORIGIN=$(zfs list | grep -w "jails/${TARGET}$" | awk '{print $1}') - ZFS_DATASET_TARGET=$(echo "${ZFS_DATASET_ORIGIN}" | sed "s|\/${TARGET}||") - if [ -n "${ZFS_DATASET_ORIGIN}" ] && [ -n "${ZFS_DATASET_TARGET}" ]; then - if ! zfs rename -f "${ZFS_DATASET_ORIGIN}" "${ZFS_DATASET_TARGET}/${NEWNAME}"; then - error_exit "Can't rename '${TARGET}' dataset." - fi - else - error_exit "Can't determine the zfs origin path of '${TARGET}'." + fi + else + # Check if container is a zfs/dataset before rename attempt + # Perform additional checks in case of bastille.conf miss-configuration + if zfs list | grep -qw "jails/${TARGET}$"; then + ZFS_DATASET_ORIGIN=$(zfs list | grep -w "jails/${TARGET}$" | awk '{print $1}') + ZFS_DATASET_TARGET=$(echo "${ZFS_DATASET_ORIGIN}" | sed "s|\/${TARGET}||") + if [ -n "${ZFS_DATASET_ORIGIN}" ] && [ -n "${ZFS_DATASET_TARGET}" ]; then + if ! zfs rename -f "${ZFS_DATASET_ORIGIN}" "${ZFS_DATASET_TARGET}/${NEWNAME}"; then + error_exit "Can't rename '${TARGET}' dataset." fi else - # Just rename the jail directory - mv "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}" + error_exit "Can't determine the zfs origin path of '${TARGET}'." fi + else + # Just rename the jail directory + mv "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}" fi - else - error_exit "${TARGET} not found. See 'bastille bootstrap'." fi # Update jail configuration files accordingly @@ -141,16 +135,14 @@ change_name() { fi } -## check if a running jail matches name or already exist -if [ "$(jls name | awk "/^${TARGET}$/")" ]; then - error_exit "Warning: ${TARGET} is running." -elif [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then - error_exit "Jail: ${NEWNAME} already exists." -fi - ## validate jail name if [ -n "${NEWNAME}" ]; then validate_name fi +## check if a jail already exists with the new name +if [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then + error_exit "Jail: ${NEWNAME} already exists." +fi + change_name diff --git a/usr/local/share/bastille/service.sh b/usr/local/share/bastille/service.sh index fcc659e9..b90b881b 100644 --- a/usr/local/share/bastille/service.sh +++ b/usr/local/share/bastille/service.sh @@ -41,21 +41,10 @@ help|-h|--help) ;; esac -if [ $# -lt 2 ]; then +if [ $# -ne 2 ]; then usage fi -TARGET=$1 -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi - -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi - for _jail in ${JAILS}; do echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" jexec -l "${_jail}" /usr/sbin/service "$@" diff --git a/usr/local/share/bastille/stop.sh b/usr/local/share/bastille/stop.sh index f9f3f0ed..21e84931 100644 --- a/usr/local/share/bastille/stop.sh +++ b/usr/local/share/bastille/stop.sh @@ -42,26 +42,10 @@ help|-h|--help) ;; esac -if [ $# -gt 1 ] || [ $# -lt 1 ]; then +if [ $# -ne 0 ]; then usage fi -TARGET="${1}" -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") - ## check if exist or not running - if [ ! -d "${bastille_jailsdir}/${TARGET}" ]; then - error_exit "[${TARGET}]: Not found." - elif [ ! "$(jls name | awk "/^${TARGET}$/")" ]; then - error_exit "[${TARGET}]: Not started." - fi -fi - for _jail in ${JAILS}; do ## test if running if [ "$(jls name | awk "/^${_jail}$/")" ]; then diff --git a/usr/local/share/bastille/sysrc.sh b/usr/local/share/bastille/sysrc.sh index 4c542825..2ab807d1 100644 --- a/usr/local/share/bastille/sysrc.sh +++ b/usr/local/share/bastille/sysrc.sh @@ -41,21 +41,10 @@ help|-h|--help) ;; esac -if [ $# -lt 2 ]; then +if [ $# -lt 1 ]; then usage fi -TARGET="${1}" -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi - -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi - for _jail in ${JAILS}; do echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" jexec -l "${_jail}" /usr/sbin/sysrc "$@" diff --git a/usr/local/share/bastille/template.sh b/usr/local/share/bastille/template.sh index 3054c603..ea04f845 100644 --- a/usr/local/share/bastille/template.sh +++ b/usr/local/share/bastille/template.sh @@ -42,22 +42,11 @@ help|-h|--help) ;; esac -if [ $# -gt 2 ] || [ $# -lt 2 ]; then +if [ $# -ne 1 ]; then bastille_usage fi -TARGET="${1}" -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi - TEMPLATE="${1}" -shift case ${TEMPLATE} in http?://github.com/*/*|http?://gitlab.com/*/*) diff --git a/usr/local/share/bastille/top.sh b/usr/local/share/bastille/top.sh index 21d27f6a..5f248653 100644 --- a/usr/local/share/bastille/top.sh +++ b/usr/local/share/bastille/top.sh @@ -41,21 +41,10 @@ help|-h|--help) ;; esac -if [ $# -gt 1 ] || [ $# -lt 1 ]; then +if [ $# -ne 0 ]; then usage fi -TARGET="${1}" -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi - -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi - for _jail in ${JAILS}; do echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" jexec -l "${_jail}" /usr/bin/top diff --git a/usr/local/share/bastille/umount.sh b/usr/local/share/bastille/umount.sh index c16fcaf0..0b1894d2 100644 --- a/usr/local/share/bastille/umount.sh +++ b/usr/local/share/bastille/umount.sh @@ -42,21 +42,11 @@ help|-h|--help) ;; esac -if [ $# -ne 2 ]; then +if [ $# -ne 1 ]; then usage fi -TARGET=$1 -shift - MOUNT_PATH=$1 -shift - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -else - JAILS=$(jls name | awk "/^${TARGET}$/") -fi for _jail in ${JAILS}; do echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" diff --git a/usr/local/share/bastille/zfs.sh b/usr/local/share/bastille/zfs.sh index b6a35990..0f34f328 100644 --- a/usr/local/share/bastille/zfs.sh +++ b/usr/local/share/bastille/zfs.sh @@ -84,34 +84,21 @@ if [ -z "${bastille_zfs_zpool}" ]; then error_exit "ZFS zpool not defined." fi -if [ $# -lt 2 ]; then +if [ $# -lt 1 ]; then usage fi -TARGET="${1}" - -if [ "${TARGET}" = 'ALL' ]; then - JAILS=$(jls name) -fi - -if [ "${TARGET}" != 'ALL' ]; then - JAILS=$(jls name | awk "/^${TARGET}$/") -fi - -case "$2" in +case "$1" in set) - ATTRIBUTE=$3 - JAILS=${JAILS} + ATTRIBUTE=$2 zfs_set_value ;; get) - ATTRIBUTE=$3 - JAILS=${JAILS} + ATTRIBUTE=$2 zfs_get_value ;; snap|snapshot) - TAG=$3 - JAILS=${JAILS} + TAG=$2 zfs_snapshot ;; df|usage)