changeset 77:5aab3a28895f

Implemented a "copy" command to employ zfs send/receive for recursively copying jail datasets
author Franz Glasner <hg@dom66.de>
date Thu, 15 Aug 2019 17:52:06 +0200
parents fea2ef3ff89a
children 42e7af4ea570
files bin/fjail
diffstat 1 files changed, 52 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/bin/fjail	Thu Aug 15 17:01:02 2019 +0200
+++ b/bin/fjail	Thu Aug 15 17:52:06 2019 +0200
@@ -2,7 +2,7 @@
 # -*- indent-tabs-mode: nil; -*-
 # @(#)$HGid$
 
-set -e
+set -eu
 
 VERSION="@@VERSION@@"
 
@@ -30,6 +30,13 @@
   populate MOUNTPOINT BASETXZ
 
     Populate the jail directory in MOUNTPOINT with the base system in BASETXZ
+
+  copy SOURCE-DATASET DEST-DATASET
+
+    Copy a tree of ZFS datasets with \"zfs send -R\" and \"zfs receive\".
+    Note that the destination dataset must not exist already.
+
+    -u        Do not automatically mount received datasets
 "
 
 
@@ -154,6 +161,47 @@
 
 
 #
+# "copy" -- ZFS copy of datasets
+#
+# command_copy source-dataset destination-dataset
+#
+command_copy() {
+    # source dataset -- destination dataset
+    local _source _dest
+    # dynamic ZFS options
+    local _zfsopts
+
+    _zfsopts=""
+    while getopts "u" _opt ; do
+        case ${_opt} in
+            u)
+                # do not mount newly created datasets
+                _zfsopts="${_zfsopts} -u"
+                ;;
+            \?|:)
+                return 2;
+                ;;
+        esac
+    done
+    shift $((OPTIND-1))
+    OPTIND=1
+
+    _source="$1"
+    if [ -z "${_source}" ]; then
+        echo "ERROR: no source dataset given" >&2
+        return 2
+    fi
+    _dest="$2"
+    if [ -z "${_dest}" ]; then
+        echo "ERROR: no source dataset given" >&2
+        return 2
+    fi
+    zfs send -R -n -v ${_source} || { echo "ERROR: ZFS operation failed in no-op mode" >&2; return 1; }
+    zfs send -R "${_source}" | zfs receive ${_zfsopts} "${_dest}"  || { echo "ERROR: ZFS operation failed" >&2; return 1; }
+}
+
+
+#
 # "privs" -- adjust privileges
 #
 # To be used when all ZFS datasets are mounted.
@@ -214,6 +262,9 @@
     populate)
         command_populate "$@"
         ;;
+    copy)
+        command_copy "$@"
+        ;;
     *)
         echo "ERROR: unknown command \`${command}'" >&2
         exit 2