changeset 251:7a6c03445ba1

Implement a "snapshot-tmpl" command: create ZFS snapshots for the RO base and the RW skeleton
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 11 Sep 2022 17:53:38 +0200
parents c4d835ccb4ae
children 62b38b2312b4
files sbin/ftjail
diffstat 1 files changed, 49 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/sbin/ftjail	Sun Sep 11 13:58:08 2022 +0200
+++ b/sbin/ftjail	Sun Sep 11 17:53:38 2022 +0200
@@ -64,6 +64,8 @@
     -L        Populate having a "skeleton" subdirectory within the mountpoint
     -P        Populate directly into the mountpoint
 
+  snapshot-tmpl BASE-RO SKELETON-RW SNAPSHOT-NAME
+
 ENVIRONMENT:
 
   All environment variables that affect "zfs" are effective also.
@@ -354,7 +356,7 @@
         tar -C "${_mp}" --exclude=./root/.cshrc --exclude=./root/.profile --no-safe-writes -xJp -f "${_basetxz}" || return
         # In the original archive they are archived as hardlinks: make proper symlinks here
         (cd "${_mp}/root" && ln -s ../.profile .profile) || return
-        (cd "${_mp}/root" && ln -s ../.cshrc .cshrc) || return        
+        (cd "${_mp}/root" && ln -s ../.cshrc .cshrc) || return
     fi
 
     find "${_mp}/boot" -type f -delete || true
@@ -606,6 +608,49 @@
     return 0
 }
 
+#:
+#: Create a snapshot for a dataset and all of its children.
+#:
+#: Args:
+#:     $1: the datasets
+#:     $2: the name of the snapshot
+#:
+_do_snapshot() {
+    local _ds _snap_name
+
+    _ds="${1}"
+    _snap_name="${2}"
+
+    if ! zfs list -H -o name -t filesystem "${_ds}" 1> /dev/null 2> /dev/null ; then
+        echo "ERROR: parent dataset \`${_ds}' does not exist" 1>&2
+        return 1
+    fi
+
+    zfs snapshot -r "${_ds}@${_snap_name}" || return
+}
+
+
+#:
+#: Implement the "snapshot-tmpl" command
+#:
+command_snapshot_tmpl() {
+    local _ds_base _ds_skel _snap_name
+
+    _ensure_no_options "$@"
+
+    _ds_base="${1-}"
+    _ds_skel="${2-}"
+    _snap_name="${3-}"
+
+    [ -z "${_ds_base}" ] && echo "ERROR: no RO base dataset name given" 1>&2
+    [ -z "${_ds_skel}" ] && echo "ERROR: no RW skeleton dataset name given" 1>&2
+    [ -z "${_snap_name}" ] && echo "ERROR: no snapshot name given" 1>&2
+
+    _do_snapshot "${_ds_base}" "${_snap_name}" || return
+    _do_snapshot "${_ds_skel}" "${_snap_name}" || return
+    return 0
+}
+
 
 #
 # Global option handling
@@ -658,6 +703,9 @@
     populate-tmpl)
         command_populate_tmpl "$@"
         ;;
+    snapshot-tmpl)
+        command_snapshot_tmpl "$@"
+        ;;
     configure)
         echo "ERROR: use \`fjail configure' instead" 1>&2;
         exit 2