# HG changeset patch # User Franz Glasner # Date 1725219267 -7200 # Node ID 7d08fd78775cad2be24c5eaaa249ac92adaeee25 # Parent e26183b3bac9bd2774898454a0dc62ab323a87a8 fzfs: implement "fzfs clone-tree". Clone a ZFS dataset tree and preserve locally set properties. diff -r e26183b3bac9 -r 7d08fd78775c docs/conf.py --- a/docs/conf.py Sun Sep 01 19:46:15 2024 +0200 +++ b/docs/conf.py Sun Sep 01 21:34:27 2024 +0200 @@ -104,6 +104,7 @@ ("man/man8/ftjail-umount-tmpl", "ftjail-umount-tmpl", "Unmount mounted Thin Jail template datasets", [author], 8), ("man/man8/fwireguard", "fwireguard", "Manage Wireguard interfaces", [author], 8), ("man/man8/fzfs", "fzfs", "A ZFS management helper tool", [author], 8), + ("man/man8/fzfs-clone-tree", "fzfs-clone-tree", "Clone a ZFS dataset tree", [author], 8), ("man/man8/fzfs-copy-tree", "fzfs-copy-tree", "Copy a ZFS dataset tree based on an existing tree", [author], 8), ("man/man8/fzfs-create-tree", "fzfs-create-tree", "Create a ZFS dataset tree structure based on an existing tree", [author], 8), ("man/man8/fzfs-mount", "fzfs-mount", "Recursively mount a ZFS dataset and its children", [author], 8), diff -r e26183b3bac9 -r 7d08fd78775c docs/man/index.rst --- a/docs/man/index.rst Sun Sep 01 19:46:15 2024 +0200 +++ b/docs/man/index.rst Sun Sep 01 21:34:27 2024 +0200 @@ -29,6 +29,7 @@ man8/ftjail-snapshot-tmpl man8/ftjail-umount-tmpl man8/fzfs + man8/fzfs-clone-tree man8/fzfs-copy-tree man8/fzfs-create-tree man8/fzfs-mount diff -r e26183b3bac9 -r 7d08fd78775c docs/man/man8/fzfs-clone-tree.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/docs/man/man8/fzfs-clone-tree.rst Sun Sep 01 21:34:27 2024 +0200 @@ -0,0 +1,53 @@ +.. -*- coding: utf-8; indent-tabs-mode: nil; -*- + +fzfs-clone-tree +=============== + +.. program:: fzfs clone-tree + + +Synopsis +-------- + +**fzfs clone-tree** [**-n**] `source-dataset` `dest-dataset` + + +Description +----------- + +Clone the ZFS snapshot that is rooted at `source-dataset` and all +descendent snapshots into the destination dataset rooted at +`dest-dataset`. + +`source-dataset` must be a snapshot. All of its children must have a +snapshot with the very same snapshot. + +`dest-dataset` must not exist already. + +All properties that are of type ``local`` or ``received``are copied to the +destination. This is also true for ``canmount`` and ``mountpoint``. + +The cloned datasets will are *not* mounted automatically. + +If something fails it is tried to delete the intermediately created +datasets. + + +Options +------- + +.. option:: -n + + Dry-run. Do not really clone datasets but show what would be done. + + +Environment +----------- + +All environment variables that affect :command:`zfs` are effective also. + + +See Also +-------- + +:manpage:`fzfs(8)` diff -r e26183b3bac9 -r 7d08fd78775c docs/man/man8/fzfs.rst --- a/docs/man/man8/fzfs.rst Sun Sep 01 19:46:15 2024 +0200 +++ b/docs/man/man8/fzfs.rst Sun Sep 01 21:34:27 2024 +0200 @@ -35,6 +35,10 @@ Subcommands ----------- +:manpage:`fzfs-clone-tree(8)` + + Recursively clone a ZFS dataset tree + :manpage:`fzfs-copy-tree(8)` Recursively copy a ZFS dataset tree based while copying some diff -r e26183b3bac9 -r 7d08fd78775c docs/man/man8/local-bsdtools.rst --- a/docs/man/man8/local-bsdtools.rst Sun Sep 01 19:46:15 2024 +0200 +++ b/docs/man/man8/local-bsdtools.rst Sun Sep 01 21:34:27 2024 +0200 @@ -72,6 +72,7 @@ - :manpage:`fzfs(8)` + * :manpage:`fzfs-clone-tree(8)` * :manpage:`fzfs-copy-tree(8)` * :manpage:`fzfs-create-tree(8)` * :manpage:`fzfs-mount(8)` diff -r e26183b3bac9 -r 7d08fd78775c pkg-plist --- a/pkg-plist Sun Sep 01 19:46:15 2024 +0200 +++ b/pkg-plist Sun Sep 01 21:34:27 2024 +0200 @@ -37,6 +37,7 @@ %%DOCS%%share/man/man8/ftjail-umount-tmpl.8.gz %%DOCS%%share/man/man8/fwireguard.8.gz %%DOCS%%share/man/man8/fzfs.8.gz +%%DOCS%%share/man/man8/fzfs-clone-tree.8.gz %%DOCS%%share/man/man8/fzfs-copy-tree.8.gz %%DOCS%%share/man/man8/fzfs-create-tree.8.gz %%DOCS%%share/man/man8/fzfs-mount.8.gz diff -r e26183b3bac9 -r 7d08fd78775c sbin/fzfs --- a/sbin/fzfs Sun Sep 01 19:46:15 2024 +0200 +++ b/sbin/fzfs Sun Sep 01 21:34:27 2024 +0200 @@ -28,6 +28,8 @@ COMMANDS: + clone-tree [-n] SOUECE-DATASET DEST-DATASET + copy-tree [-A] [-M MOUNTPOINT] [-n] [-u] SOURCE-DATASET DEST-DATASET create-tree [-A] [-M MOUNTPOINT] [-n] [-p] [-u] SOURCE-DATASET DEST-DATASET @@ -430,6 +432,190 @@ #: +#: Implementation of "clone-tree" +#: +command_clone_tree() { + local _ds_source _ds_dest + local _opt_dry_run + + local _ds snapshot_name _ds_source_base _ds_relname + local _ds_canmount _ds_mountpoint + local _clone_props _arg_canmount _arg_other_clone_props + local _opt _idx _idx_lp _prop _propval + + _opt_dry_run="" + + while getopts "n" _opt ; do + case ${_opt} in + n) + _opt_dry_run="yes" + ;; + \?|:) + return 2; + ;; + esac + done + shift $((OPTIND-1)) + OPTIND=1 + + _ds_source="${1-}" + _ds_dest="${2-}" + + [ -z "${_ds_source}" ] && { err "no source dataset/snapshot given"; return 2; } + [ -z "${_ds_dest}" ] && { err "no destination name given"; return 2; } + + if ! zfs get -H name "${_ds_source}" >/dev/null 2>&1; then + err "source dataset does not exist: ${_ds_source}" + return 1 + fi + if zfs get -H name "${_ds_dest}" >/dev/null 2>&1; then + err "destination dataset already exists: ${_ds_dest}" + return 1 + fi + + _ds_source_base="${_ds_source%@*}" + _snapshot_name="${_ds_source##*@}" + + if [ "${_ds_source_base}" = "${_snapshot_name}" ]; then + err "no snapshot given" + return 1 + fi + + array_create _ds_tree + + while IFS=$'\n' read -r _ds; do + array_append _ds_tree "${_ds}" + done </dev/null 2>&1; then + err "child dataset does not exist: ${_ds}@${_snapshot_name}" 1>&2 + array_destroy _ds_tree + return 1 + fi + _idx=$((${_idx} + 1)) + done + + array_create _cloned_datasets + alist_create _local_props + + # + # 1. Clone with "safe" canmount settings + # + + _idx=1 + while array_tryget _ds _ds_tree ${_idx}; do + # Determine the relative name of the dataset + _ds_relname="${_ds#${_ds_source_base}}" + + # Need to determine in *every* case (local, default, received, ...) + _ds_canmount="$(zfs get -H -o value canmount "${_ds}")" + + alist_clear _local_props + while IFS=$'\t' read -r _prop _propval ; do + alist_set _local_props "${_prop}" "${_propval}" + done <