mirror of
https://github.com/hackacad/bastille.git
synced 2025-12-21 09:41:47 +01:00
zfs: Support jailing and unjailing
This commit is contained in:
@@ -33,7 +33,10 @@
|
|||||||
. /usr/local/share/bastille/common.sh
|
. /usr/local/share/bastille/common.sh
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
error_notify "Usage: bastille zfs TARGET [set|get|snap|destroy_snap|df|usage] [key=value|date]"
|
error_notify "Usage: bastille zfs TARGET [destroy_snap|df|get|set|(snap|snapshot)|usage] [key=value|date]"
|
||||||
|
error_notify " [jail pool/dataset /jail/path]"
|
||||||
|
error_notify " [unjail pool/dataset]"
|
||||||
|
|
||||||
cat << EOF
|
cat << EOF
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
@@ -44,42 +47,130 @@ EOF
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zfs_jail_dataset() {
|
||||||
|
|
||||||
|
# Validate jail state
|
||||||
|
check_target_is_stopped "${_jail}" || if [ "${AUTO}" -eq 1 ]; then
|
||||||
|
bastille stop "${_jail}"
|
||||||
|
else
|
||||||
|
info "\n[${_jail}]:"
|
||||||
|
error_notify "Jail is running."
|
||||||
|
error_exit "Use [-a|--auto] to auto-stop the jail."
|
||||||
|
fi
|
||||||
|
|
||||||
|
info "\n[${_jail}]:"
|
||||||
|
|
||||||
|
# Exit if MOUNT or DATASET is empty
|
||||||
|
if [ -z "${MOUNT}" ] || [ -z "${DATASET}" ]; then
|
||||||
|
usage
|
||||||
|
# Exit if datset does not exist
|
||||||
|
elif ! zfs list "${DATASET}" >/dev/null 2>/dev/null; then
|
||||||
|
error_exit "[ERROR]: Dataset does not exist: ${DATASET}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Ensure dataset is not already present in *zfs.conf*
|
||||||
|
if grep -hoqsw "${DATASET}" ${bastille_jailsdir}/*/zfs.conf; then
|
||||||
|
error_exit "[ERROR]: Dataset already assigned."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Add necessary config variables to jail
|
||||||
|
bastille config ${_jail} set enforce_statfs 1
|
||||||
|
bastille config ${_jail} set allow.mount
|
||||||
|
bastille config ${_jail} set allow.mount.devfs
|
||||||
|
bastille config ${_jail} set allow.mount.zfs
|
||||||
|
|
||||||
|
# Add dataset to zfs.conf
|
||||||
|
echo "${DATASET} ${MOUNT}" >> "${bastille_jailsdir}/${_jail}/zfs.conf"
|
||||||
|
|
||||||
|
if [ "${AUTO}" -eq 1 ]; then
|
||||||
|
bastille start "${_jail}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
zfs_unjail_dataset() {
|
||||||
|
|
||||||
|
# Validate jail state
|
||||||
|
check_target_is_stopped "${_jail}" || if [ "${AUTO}" -eq 1 ]; then
|
||||||
|
bastille stop "${_jail}"
|
||||||
|
else
|
||||||
|
info "\n[${_jail}]:"
|
||||||
|
error_notify "Jail is running."
|
||||||
|
error_exit "Use [-a|--auto] to auto-stop the jail."
|
||||||
|
fi
|
||||||
|
|
||||||
|
info "\n[${_jail}]:"
|
||||||
|
|
||||||
|
# Exit if DATASET is empty
|
||||||
|
if [ -z "${DATASET}" ]; then
|
||||||
|
usage
|
||||||
|
# Warn if datset does not exist
|
||||||
|
elif ! zfs list "${DATASET}" >/dev/null 2>/dev/null; then
|
||||||
|
warn "[WARNING]: Dataset does not exist: ${DATASET}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Remove dataset from zfs.conf
|
||||||
|
if ! grep -hoqsw "${DATASET}" ${bastille_jailsdir}/${_jail}/zfs.conf; then
|
||||||
|
error_exit "[ERROR]: Dataset not present in zfs.conf."
|
||||||
|
else
|
||||||
|
sed -i '' "\#.*${DATASET}.*#d" "${bastille_jailsdir}/${_jail}/zfs.conf"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "${AUTO}" -eq 1 ]; then
|
||||||
|
bastille start "${_jail}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
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 -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}"
|
||||||
}
|
}
|
||||||
|
|
||||||
zfs_destroy_snapshot() {
|
zfs_destroy_snapshot() {
|
||||||
|
info "\n[${_jail}]:"
|
||||||
# shellcheck disable=SC2140
|
# shellcheck disable=SC2140
|
||||||
zfs destroy -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}"
|
zfs destroy -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"@"${TAG}"
|
||||||
}
|
}
|
||||||
|
|
||||||
zfs_set_value() {
|
zfs_set_value() {
|
||||||
|
info "\n[${_jail}]:"
|
||||||
zfs "${ATTRIBUTE}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"
|
zfs "${ATTRIBUTE}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}"
|
||||||
}
|
}
|
||||||
|
|
||||||
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}"
|
||||||
}
|
}
|
||||||
|
|
||||||
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}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Handle options.
|
# Handle options.
|
||||||
|
AUTO=0
|
||||||
while [ "$#" -gt 0 ]; do
|
while [ "$#" -gt 0 ]; do
|
||||||
case "${1}" in
|
case "${1}" in
|
||||||
-h|--help|help)
|
-h|--help|help)
|
||||||
usage
|
usage
|
||||||
;;
|
;;
|
||||||
|
-a|--auto)
|
||||||
|
AUTO=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
-x|--debug)
|
-x|--debug)
|
||||||
enable_debug
|
enable_debug
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
-*)
|
-*)
|
||||||
error_notify "[ERROR]: Unknown Option: \"${1}\""
|
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do
|
||||||
usage
|
case ${_opt} in
|
||||||
|
a) AUTO=1 ;;
|
||||||
|
x) enable_debug ;;
|
||||||
|
*) error_exit "[ERROR]: Unknown Option: \"${1}\"" ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
shift
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
break
|
break
|
||||||
@@ -87,7 +178,7 @@ while [ "$#" -gt 0 ]; do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ "$#" -lt 2 ]; then
|
if [ "$#" -lt 2 ] || [ "$#" -gt 4 ]; then
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -111,21 +202,7 @@ for _jail in ${JAILS}; do
|
|||||||
|
|
||||||
(
|
(
|
||||||
|
|
||||||
info "\n[${_jail}]:"
|
|
||||||
|
|
||||||
case "${ACTION}" in
|
case "${ACTION}" in
|
||||||
set)
|
|
||||||
ATTRIBUTE="${3}"
|
|
||||||
zfs_set_value
|
|
||||||
;;
|
|
||||||
get)
|
|
||||||
ATTRIBUTE="${3}"
|
|
||||||
zfs_get_value
|
|
||||||
;;
|
|
||||||
snap|snapshot)
|
|
||||||
TAG="${3}"
|
|
||||||
zfs_snapshot
|
|
||||||
;;
|
|
||||||
destroy_snap|destroy_snapshot)
|
destroy_snap|destroy_snapshot)
|
||||||
TAG="${3}"
|
TAG="${3}"
|
||||||
zfs_destroy_snapshot
|
zfs_destroy_snapshot
|
||||||
@@ -133,6 +210,27 @@ for _jail in ${JAILS}; do
|
|||||||
df|usage)
|
df|usage)
|
||||||
zfs_disk_usage
|
zfs_disk_usage
|
||||||
;;
|
;;
|
||||||
|
get)
|
||||||
|
ATTRIBUTE="${3}"
|
||||||
|
zfs_get_value
|
||||||
|
;;
|
||||||
|
jail)
|
||||||
|
DATASET="${3}"
|
||||||
|
MOUNT="${4}"
|
||||||
|
zfs_jail_dataset
|
||||||
|
;;
|
||||||
|
unjail)
|
||||||
|
DATASET="${3}"
|
||||||
|
zfs_unjail_dataset
|
||||||
|
;;
|
||||||
|
set)
|
||||||
|
ATTRIBUTE="${3}"
|
||||||
|
zfs_set_value
|
||||||
|
;;
|
||||||
|
snap|snapshot)
|
||||||
|
TAG="${3}"
|
||||||
|
zfs_snapshot
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
usage
|
usage
|
||||||
;;
|
;;
|
||||||
|
|||||||
Reference in New Issue
Block a user