[pve-devel] [PATCH zsync 10/10] fix #1669: add prepend-storage-id flag

Fabian Ebner f.ebner at proxmox.com
Tue May 4 10:10:04 CEST 2021


so one can set up jobs for guests with disks with the same name on different
storages. For example,
    storageA:vm-123-disk-0
    storageB:vm-123-disk-0
will be synced to
    target/storageA/vm-123-disk-0
    target/storageB/vm-123-disk-0
when the flag is specified.

The necessary parent file systems (one per storage ID) are created as needed.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 pve-zsync | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/pve-zsync b/pve-zsync
index 75d4d7c..521abf0 100755
--- a/pve-zsync
+++ b/pve-zsync
@@ -249,6 +249,7 @@ sub parse_argv {
 	method => undef,
 	source_user => undef,
 	dest_user => undef,
+	prepend_storage_id => undef,
 	properties => undef,
 	dest_config_path => undef,
     };
@@ -265,6 +266,7 @@ sub parse_argv {
 	'method=s' => \$param->{method},
 	'source-user=s' => \$param->{source_user},
 	'dest-user=s' => \$param->{dest_user},
+	'prepend-storage-id' => \$param->{prepend_storage_id},
 	'properties' => \$param->{properties},
 	'dest-config-path=s' => \$param->{dest_config_path},
     );
@@ -336,6 +338,7 @@ sub param_to_job {
     $job->{source} = $param->{source};
     $job->{source_user} = $param->{source_user};
     $job->{dest_user} = $param->{dest_user};
+    $job->{prepend_storage_id} = !!$param->{prepend_storage_id};
     $job->{properties} = !!$param->{properties};
     $job->{dest_config_path} = $param->{dest_config_path} if $param->{dest_config_path};
 
@@ -461,6 +464,7 @@ sub format_job {
     $text .= " --verbose" if $job->{verbose};
     $text .= " --source-user $job->{source_user}";
     $text .= " --dest-user $job->{dest_user}";
+    $text .= " --prepend-storage-id" if $job->{prepend_storage_id};
     $text .= " --properties" if $job->{properties};
     $text .= " --dest-config-path $job->{dest_config_path}" if $job->{dest_config_path};
     $text .= "\n";
@@ -674,6 +678,8 @@ sub sync {
 
 	    ($dest->{old_snap}, $dest->{last_snap}) = snapshot_get($source, $dest, $param->{maxsnap}, $param->{name}, $param->{dest_user});
 
+	    prepare_prepended_target($source, $dest, $param->{dest_user}) if defined($dest->{prepend});
+
 	    snapshot_add($source, $dest, $param->{name}, $date, $param->{source_user}, $param->{dest_user});
 
 	    send_image($source, $dest, $param);
@@ -693,6 +699,10 @@ sub sync {
 		    $source->{pool} = $disks->{$disk}->{pool};
 		    $source->{path} = $disks->{$disk}->{path} if $disks->{$disk}->{path};
 		    $source->{last_part} = $disks->{$disk}->{last_part};
+
+		    $dest->{prepend} = $disks->{$disk}->{storage_id}
+			if $param->{prepend_storage_id};
+
 		    &$sync_path($source, $dest, $job, $param, $date);
 		}
 		if ($param->{method} eq "ssh" && ($source->{ip} || $dest->{ip})) {
@@ -921,12 +931,30 @@ sub target_dataset {
     my ($source, $dest) = @_;
 
     my $target = "$dest->{all}";
+    $target .= "/$dest->{prepend}" if defined($dest->{prepend});
     $target .= "/$source->{last_part}" if $source->{last_part};
     $target =~ s!/+!/!g;
 
     return $target;
 }
 
+# create the parent dataset for the actual target
+sub prepare_prepended_target {
+    my ($source, $dest, $dest_user) = @_;
+
+    die "internal error - not a prepended target\n" if !defined($dest->{prepend});
+
+    # The parent dataset shouldn't be the actual target.
+    die "internal error - no last_part for source\n" if !$source->{last_part};
+
+    my $target = "$dest->{all}/$dest->{prepend}";
+    $target =~ s!/+!/!g;
+
+    return if check_dataset_exists($target, $dest->{ip}, $dest_user);
+
+    create_file_system($target, $dest->{ip}, $dest_user);
+}
+
 sub snapshot_destroy {
     my ($source, $dest, $method, $snap, $source_user, $dest_user) = @_;
 
@@ -1138,6 +1166,10 @@ $PROGNAME create -dest <string> -source <string> [OPTIONS]
 
 		name of the sync job, if not set it is default
 
+        -prepend-storage-id
+
+		If specified, prepend the storage ID to the destination's path(s).
+
         -skip
 
 		If specified, skip the first sync.
@@ -1184,6 +1216,10 @@ $PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
 		name of the sync job, if not set it is default.
 		It is only necessary if scheduler allready contains this source.
 
+        -prepend-storage-id
+
+		If specified, prepend the storage ID to the destination's path(s).
+
         -source    string
 
 		the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
-- 
2.20.1





More information about the pve-devel mailing list