zfs: fix some bugs when rolling back, add default name

This commit is contained in:
tschettervictor
2025-07-16 16:14:15 -06:00
committed by GitHub
parent 2629cae350
commit f08f4dd2e6

View File

@@ -52,8 +52,6 @@ EOF
} }
zfs_jail_dataset() { zfs_jail_dataset() {
info "\n[${_jail}]:"
# Exit if MOUNT or DATASET is empty # Exit if MOUNT or DATASET is empty
if [ -z "${MOUNT}" ] || [ -z "${DATASET}" ]; then if [ -z "${MOUNT}" ] || [ -z "${DATASET}" ]; then
@@ -91,8 +89,6 @@ zfs_jail_dataset() {
zfs_unjail_dataset() { zfs_unjail_dataset() {
info "\n[${_jail}]:"
# Exit if DATASET is empty # Exit if DATASET is empty
if [ -z "${DATASET}" ]; then if [ -z "${DATASET}" ]; then
usage usage
@@ -122,40 +118,34 @@ zfs_unjail_dataset() {
} }
zfs_snapshot() { zfs_snapshot() {
info "\n[${_jail}]:"
# shellcheck disable=SC2140 # shellcheck disable=SC2140
zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}" zfs snapshot ${OPT_CREATE} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}"
_return=$? _return=$?
} }
zfs_rollback() { zfs_rollback() {
info "\n[${_jail}]:"
# shellcheck disable=SC2140 # shellcheck disable=SC2140
zfs rollback -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}" zfs rollback -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}"
_return=$? _return=$?
} }
zfs_destroy_snapshot() { zfs_destroy_snapshot() {
info "\n[${_jail}]:"
# shellcheck disable=SC2140 # shellcheck disable=SC2140
zfs destroy ${OPT_DESTROY} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}" zfs destroy ${OPT_DESTROY} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}"
_return=$? _return=$?
} }
zfs_set_value() { zfs_set_value() {
info "\n[${_jail}]:"
zfs set "${ATTRIBUTE}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}" zfs set "${ATTRIBUTE}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"
_return=$? _return=$?
} }
zfs_get_value() { zfs_get_value() {
info "\n[${_jail}]:"
zfs get "${ATTRIBUTE}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}" zfs get "${ATTRIBUTE}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"
_return=$? _return=$?
} }
zfs_disk_usage() { zfs_disk_usage() {
info "\n[${_jail}]:"
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}"
_return=$? _return=$?
} }
@@ -168,24 +158,29 @@ snapshot_checks() {
fi fi
# Verify rollback snapshots # Verify rollback snapshots
if [ "${SNAP_ROLLBACK}" -eq 1 ] && [ -n "${TAG}" ]; then if [ "${SNAP_ROLLBACK}" -eq 1 ]; then
SNAP_TAG_CHECK=$(echo ${TAG} | grep -wo "bastille_${_jail}_[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{6\}") if [ -n "${TAG}" ]; then
if [ -n "${SNAP_TAG_CHECK}" ]; then SNAP_CHECK_TAG="$(zfs list -H -t snapshot -o name ${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail} | grep -o "${TAG}$" | tail -n 1)"
warn "\n[WARNING]: A snapshot with unique name was given for ALL targets, ignore unwanted errors." else
TAG="$(zfs list -H -t snapshot -o name ${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail} | grep -o "bastille_${_jail}_.*$" | tail -n 1)"
SNAP_TAG_CHECK=$(echo ${TAG} | grep -wo "bastille_${_jail}_[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{6\}")
fi
if [ -z "${SNAP_TAG_CHECK}" ]; then
error_continue "[ERROR]: Snapshot not found: ${TAG}"
fi
elif [ "${SNAP_DESTROY}" -eq 1 ]; then
if [ -z "${TAG}" ]; then
error_continue "[ERROR]: Destroying snapshots requires a TAG to be specified."
fi fi
else
error_exit "[ERROR]: Rollback operation requires a tag name."
fi
# Generate a relatively short but unique name for the snapshots based on the current date/jail name. # Generate a relatively short but unique name for the snapshots based on the current date/jail name.
if [ "${AUTO_TAG}" -eq 1 ]; then elif [ "${AUTO_TAG}" -eq 1 ]; then
DATE=$(date +%F-%H%M%S) DATE=$(date +%F-%H%M%S)
TAG="bastille_${_jail}_${DATE}" TAG="bastille_${_jail}_${DATE}"
# Check for the generated snapshot name. # Check for the generated snapshot name.
SNAP_GEN_CHECK="" SNAP_GEN_CHECK=""
SNAP_GEN_CHECK=$(echo ${TAG} | grep -wo "bastille_${_jail}_[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{6\}") SNAP_GEN_CHECK=$(echo ${TAG} | grep -wo "bastille_${_jail}_[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{6\}")
if [ -z "${SNAP_GEN_CHECK}" ]; then if [ -z "${SNAP_GEN_CHECK}" ]; then
error_notify "[ERROR]: Failed validation for the generated snapshot name." error_notify "[ERROR]: Failed to validate snapshot name."
fi fi
fi fi
} }
@@ -193,13 +188,20 @@ snapshot_checks() {
snapshot_create() { snapshot_create() {
snapshot_checks snapshot_checks
if [ "${OPT_VERBOSE}" -eq 1 ]; then
OPT_CREATE="-v -r"
else
OPT_CREATE="-r"
fi
zfs_snapshot zfs_snapshot
# Check for exit status and notify only for user reference. # Check for exit status and notify only for user reference.
if [ "${_return}" -ne 0 ]; then if [ "${_return}" -ne 0 ]; then
error_notify "[ERROR]: Failed to create snapshot." error_notify "[ERROR]: Failed to create snapshot."
else else
echo "Snapshot successfully created: ${TAG}." echo "Snapshot created: ${TAG}"
fi fi
} }
@@ -214,7 +216,7 @@ snapshot_rollback() {
if [ "${_return}" -ne 0 ]; then if [ "${_return}" -ne 0 ]; then
error_notify "[ERROR]: Failed to restore snapshot: ${TAG}." error_notify "[ERROR]: Failed to restore snapshot: ${TAG}."
else else
echo "Snapshot successfully rolled back: ${TAG}" echo "Snapshot restored: ${TAG}"
fi fi
} }
@@ -229,6 +231,7 @@ snapshot_destroy() {
else else
OPT_DESTROY="-r" OPT_DESTROY="-r"
fi fi
zfs_destroy_snapshot zfs_destroy_snapshot
# Check for exit status and just notify. # Check for exit status and just notify.
@@ -343,4 +346,3 @@ for _jail in ${JAILS}; do
esac esac
done done
echo