mirror of
https://github.com/hackacad/bastille.git
synced 2025-12-18 08:12:53 +01:00
Merge pull request #1164 from JRGTH/bastille_zfs_enhancement
[Enhancement] 'Bastille zfs command'
This commit is contained in:
@@ -114,7 +114,7 @@ Available Commands:
|
|||||||
update Update container base -pX release.
|
update Update container base -pX release.
|
||||||
upgrade Upgrade container release to X.Y-RELEASE.
|
upgrade Upgrade container release to X.Y-RELEASE.
|
||||||
verify Compare release against a "known good" index.
|
verify Compare release against a "known good" index.
|
||||||
zfs Manage (get|set) ZFS attributes on targeted container(s).
|
zfs Manage (get|set) ZFS attributes or snapshots on targeted container(s).
|
||||||
|
|
||||||
Use "bastille -v|--version" for version information.
|
Use "bastille -v|--version" for version information.
|
||||||
Use "bastille command -h|--help" for more information about a command.
|
Use "bastille command -h|--help" for more information about a command.
|
||||||
|
|||||||
@@ -595,6 +595,20 @@ list_release(){
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
list_snapshot(){
|
||||||
|
# TODO: Ability to list snapshot data for a single target.
|
||||||
|
# List snapshots with its usage data for valid bastille jails only.
|
||||||
|
if [ -d "${bastille_jailsdir}" ]; then
|
||||||
|
JAIL_LIST=$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g")
|
||||||
|
for _JAIL in ${JAIL_LIST}; do
|
||||||
|
if [ -f "${bastille_jailsdir}/${_JAIL}/jail.conf" ]; then
|
||||||
|
info "\n[${_JAIL}]:"
|
||||||
|
zfs list -r -t snapshot "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_JAIL}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
list_template(){
|
list_template(){
|
||||||
find "${bastille_templatesdir}" -type d -maxdepth 2 | sed "s#${bastille_templatesdir}/##g"
|
find "${bastille_templatesdir}" -type d -maxdepth 2 | sed "s#${bastille_templatesdir}/##g"
|
||||||
}
|
}
|
||||||
@@ -767,6 +781,10 @@ if [ "$#" -eq 1 ]; then
|
|||||||
release|releases)
|
release|releases)
|
||||||
list_release "${2}"
|
list_release "${2}"
|
||||||
;;
|
;;
|
||||||
|
snap|snapshot|snapshots)
|
||||||
|
list_snapshot
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
template|templates)
|
template|templates)
|
||||||
list_template
|
list_template
|
||||||
;;
|
;;
|
||||||
|
|||||||
@@ -33,20 +33,34 @@
|
|||||||
. /usr/local/share/bastille/common.sh
|
. /usr/local/share/bastille/common.sh
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
error_notify "Usage: bastille zfs [option(s)] TARGET [destroy_snap|(df|usage)|get|set|(snap|snapshot)] [key=value|date]"
|
|
||||||
|
error_notify "Usage: bastille zfs [option(s)] TARGET [destroy|(df|usage)|get|set|(snap|snapshot)] [key=value|date]"
|
||||||
error_notify " [jail pool/dataset /jail/path]"
|
error_notify " [jail pool/dataset /jail/path]"
|
||||||
error_notify " [unjail pool/dataset]"
|
error_notify " [unjail pool/dataset]"
|
||||||
|
|
||||||
cat << EOF
|
cat << EOF
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
|
||||||
-x | --debug Enable debug mode.
|
snapshot Create a ZFS snapshot for the specified container.
|
||||||
|
rollback Rollback a ZFS snapshot on the specified container.
|
||||||
|
destroy Destroy a ZFS snapshot on the specified container.
|
||||||
|
-a | --auto Auto mode. Start/stop jail(s) if required.
|
||||||
|
-v | --verbose Be more verbose during the snapshot destroy operation.
|
||||||
|
-x | --debug Enable debug mode.
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AUTO="0"
|
||||||
|
SNAP_NAME_GEN=
|
||||||
|
SNAP_ROLLBACK=
|
||||||
|
SNAP_DESTROY=
|
||||||
|
SNAP_VERBOSE=
|
||||||
|
SNAP_TAGCHECK=
|
||||||
|
SNAP_GENCHECK=
|
||||||
|
SNAP_BATCH=
|
||||||
|
|
||||||
zfs_jail_dataset() {
|
zfs_jail_dataset() {
|
||||||
|
|
||||||
info "\n[${_jail}]:"
|
info "\n[${_jail}]:"
|
||||||
@@ -121,17 +135,26 @@ zfs_snapshot() {
|
|||||||
info "\n[${_jail}]:"
|
info "\n[${_jail}]:"
|
||||||
# shellcheck disable=SC2140
|
# shellcheck disable=SC2140
|
||||||
zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}"
|
zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}"
|
||||||
|
_return=$?
|
||||||
|
}
|
||||||
|
|
||||||
|
zfs_rollback() {
|
||||||
|
info "\n[${_jail}]:"
|
||||||
|
# shellcheck disable=SC2140
|
||||||
|
zfs rollback -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}"@"${TAG}"
|
||||||
|
_return=$?
|
||||||
}
|
}
|
||||||
|
|
||||||
zfs_destroy_snapshot() {
|
zfs_destroy_snapshot() {
|
||||||
info "\n[${_jail}]:"
|
info "\n[${_jail}]:"
|
||||||
# shellcheck disable=SC2140
|
# shellcheck disable=SC2140
|
||||||
zfs destroy -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}"
|
zfs destroy ${_opts} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}"
|
||||||
|
_return=$?
|
||||||
}
|
}
|
||||||
|
|
||||||
zfs_set_value() {
|
zfs_set_value() {
|
||||||
info "\n[${_jail}]:"
|
info "\n[${_jail}]:"
|
||||||
zfs "${ATTRIBUTE}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"
|
zfs set "${ATTRIBUTE}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"
|
||||||
}
|
}
|
||||||
|
|
||||||
zfs_get_value() {
|
zfs_get_value() {
|
||||||
@@ -144,8 +167,7 @@ zfs_disk_usage() {
|
|||||||
zfs list -t all -o name,used,avail,refer,mountpoint,compress,ratio -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"
|
zfs list -t all -o name,used,avail,refer,mountpoint,compress,ratio -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Handle options.
|
# Handle some options.
|
||||||
AUTO=0
|
|
||||||
while [ "$#" -gt 0 ]; do
|
while [ "$#" -gt 0 ]; do
|
||||||
case "${1}" in
|
case "${1}" in
|
||||||
-h|--help|help)
|
-h|--help|help)
|
||||||
@@ -155,6 +177,10 @@ while [ "$#" -gt 0 ]; do
|
|||||||
AUTO=1
|
AUTO=1
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
-v|--verbose)
|
||||||
|
SNAP_VERBOSE="1"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
-x|--debug)
|
-x|--debug)
|
||||||
enable_debug
|
enable_debug
|
||||||
shift
|
shift
|
||||||
@@ -182,6 +208,10 @@ fi
|
|||||||
TARGET="${1}"
|
TARGET="${1}"
|
||||||
ACTION="${2}"
|
ACTION="${2}"
|
||||||
|
|
||||||
|
if [ "${TARGET}" = "ALL" ] || [ "${TARGET}" = "all" ]; then
|
||||||
|
SNAP_BATCH="1"
|
||||||
|
fi
|
||||||
|
|
||||||
bastille_root_check
|
bastille_root_check
|
||||||
set_target "${TARGET}"
|
set_target "${TARGET}"
|
||||||
|
|
||||||
@@ -195,14 +225,122 @@ if [ -z "${bastille_zfs_zpool}" ]; then
|
|||||||
error_exit "[ERROR]: ZFS zpool not defined."
|
error_exit "[ERROR]: ZFS zpool not defined."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
snapshot_checks() {
|
||||||
|
# Check if we requested ALL jails.
|
||||||
|
if [ -n "${SNAP_ROLLBACK}" ]; then
|
||||||
|
if [ -n "${SNAP_BATCH}" ]; then
|
||||||
|
TARGET="${_jail}"
|
||||||
|
|
||||||
|
# Check if is a bastille created snapshot with a unique name and warn about unwanted errors.
|
||||||
|
# Just warn here and don't exit, as a user may copy/use that unique name for other targets manually.
|
||||||
|
SNAP_TAGCHECK=$(echo ${TAG} | grep -wo "Bastille_[0-9a-fA-F]\{6\}_${_jail}_[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{6\}")
|
||||||
|
if [ -n "${SNAP_TAGCHECK}" ]; then
|
||||||
|
warn "\n[WARNING]: A snapshot with unique name was given for ALL targets, ignore unwanted errors."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if jail is running and stop if requested.
|
||||||
|
if [ -z "${SNAP_DESTROY}" ]; then
|
||||||
|
check_target_is_stopped "${_jail}" || \
|
||||||
|
if [ "${AUTO}" -eq 1 ]; then
|
||||||
|
bastille stop "${_jail}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Generate a relatively short but unique name for the snapshots based on the current date/jail name.
|
||||||
|
if [ -n "${SNAP_NAME_GEN}" ]; then
|
||||||
|
for _JAIL in ${_jail}; do
|
||||||
|
DATE=$(date +%F-%H%M%S)
|
||||||
|
NAME_MD5X6=$(echo "${DATE} ${_JAIL}" | md5 | cut -b -6)
|
||||||
|
SNAPSHOT_NAME="Bastille_${NAME_MD5X6}_${_JAIL}_${DATE}"
|
||||||
|
TAG="${SNAPSHOT_NAME}"
|
||||||
|
SNAPSHOT_NAME=
|
||||||
|
done
|
||||||
|
|
||||||
|
# Check for the generated snapshot name.
|
||||||
|
SNAP_GENCHECK=$(echo ${TAG} | grep -wo "Bastille_[0-9a-fA-F]\{6\}_${_jail}_[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{6\}")
|
||||||
|
if [ -z "${SNAP_GENCHECK}" ]; then
|
||||||
|
info "\n[${_jail}]:"
|
||||||
|
error_notify "[ERROR]: Failed validation for the generated snapshot name."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshot_create() {
|
||||||
|
# Attempt to snapshot the container.
|
||||||
|
# Thiw will create a ZFS snapshot for the specified container with an auto-generated name with the
|
||||||
|
# following format "Bastille_XXXXXX_JAILNAME_YYYY-MM-DD-HHMMSS" unless a name tag is manually entered.
|
||||||
|
if [ -z "${TAG}" ]; then
|
||||||
|
SNAP_NAME_GEN="1"
|
||||||
|
fi
|
||||||
|
|
||||||
|
snapshot_checks
|
||||||
|
zfs_snapshot
|
||||||
|
|
||||||
|
# Check for exit status and notify only for user reference.
|
||||||
|
if [ "${_return}" -ne 0 ]; then
|
||||||
|
error_notify "[ERROR]: Failed to snapshot jail: '${_jail}'"
|
||||||
|
else
|
||||||
|
info "Snapshot for ${_jail} successfully created as '${TAG}'."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Start the jail after snapshot if requested.
|
||||||
|
if [ "${AUTO}" -eq 1 ]; then
|
||||||
|
bastille start "${_jail}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshot_rollback() {
|
||||||
|
# This feature is intended work with snapshots created by either, bastille or manually created by the user.
|
||||||
|
# An error about "more recent snapshots or bookmarks exist" will appears if the '-r' flag is not specified.
|
||||||
|
SNAP_ROLLBACK="1"
|
||||||
|
snapshot_checks
|
||||||
|
zfs_rollback
|
||||||
|
|
||||||
|
# Check for exit status and just notify.
|
||||||
|
if [ "${_return}" -ne 0 ]; then
|
||||||
|
error_notify "[ERROR]: Failed to restore '${TAG}' snapshot for '${_jail}'."
|
||||||
|
else
|
||||||
|
info "Snapshot '${TAG}' successfully rolled back for '${_jail}'."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Start the jail after rollback if requested.
|
||||||
|
if [ "${AUTO}" -eq 1 ]; then
|
||||||
|
bastille start "${_jail}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshot_destroy() {
|
||||||
|
# Destroy the user specifier bastille snapshot.
|
||||||
|
SNAP_DESTROY="1"
|
||||||
|
snapshot_checks
|
||||||
|
|
||||||
|
# Set some options.
|
||||||
|
if [ -n "${SNAP_VERBOSE}" ]; then
|
||||||
|
_opts="-v -r"
|
||||||
|
else
|
||||||
|
_opts="-r"
|
||||||
|
fi
|
||||||
|
zfs_destroy_snapshot
|
||||||
|
|
||||||
|
# Check for exit status and just notify.
|
||||||
|
if [ "${_return}" -ne 0 ]; then
|
||||||
|
error_notify "[ERROR]: Failed to destroy '${TAG}' snapshot for '${_jail}'"
|
||||||
|
else
|
||||||
|
info "Snapshot '${TAG}' destroyed successfully."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
for _jail in ${JAILS}; do
|
for _jail in ${JAILS}; do
|
||||||
|
|
||||||
(
|
(
|
||||||
|
|
||||||
case "${ACTION}" in
|
case "${ACTION}" in
|
||||||
destroy_snap|destroy_snapshot)
|
destroy|destroy_snap|destroy_snapshot)
|
||||||
TAG="${3}"
|
TAG="${3}"
|
||||||
zfs_destroy_snapshot
|
snapshot_destroy
|
||||||
;;
|
;;
|
||||||
df|usage)
|
df|usage)
|
||||||
zfs_disk_usage
|
zfs_disk_usage
|
||||||
@@ -216,6 +354,10 @@ for _jail in ${JAILS}; do
|
|||||||
MOUNT="${4}"
|
MOUNT="${4}"
|
||||||
zfs_jail_dataset
|
zfs_jail_dataset
|
||||||
;;
|
;;
|
||||||
|
rollback)
|
||||||
|
TAG="${3}"
|
||||||
|
snapshot_rollback
|
||||||
|
;;
|
||||||
unjail)
|
unjail)
|
||||||
DATASET="${3}"
|
DATASET="${3}"
|
||||||
zfs_unjail_dataset
|
zfs_unjail_dataset
|
||||||
@@ -226,7 +368,7 @@ for _jail in ${JAILS}; do
|
|||||||
;;
|
;;
|
||||||
snap|snapshot)
|
snap|snapshot)
|
||||||
TAG="${3}"
|
TAG="${3}"
|
||||||
zfs_snapshot
|
snapshot_create
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
usage
|
usage
|
||||||
@@ -236,6 +378,6 @@ for _jail in ${JAILS}; do
|
|||||||
) &
|
) &
|
||||||
|
|
||||||
bastille_running_jobs "${bastille_process_limit}"
|
bastille_running_jobs "${bastille_process_limit}"
|
||||||
|
|
||||||
done
|
done
|
||||||
wait
|
wait
|
||||||
|
|||||||
Reference in New Issue
Block a user