From 55b1af44fd8fec28c2415a3c30a72dd8c29a5388 Mon Sep 17 00:00:00 2001 From: Fiona Ebner Date: Mon, 13 Jun 2022 12:29:55 +0200 Subject: [PATCH] pvesr: prepare local job: remove stale replicated volumes immediately Commit d8cd8e8cf9795dc9c2462a67e9ef89ad31759796 introduced a regression where only stale replicated volumes with an older timestamp would be cleaned up. This meant that after removing a volume from the guest config, it would only be cleaned up the second time the replication ran afterwards. And the volume could become completely orphaned in case the relevant storage wasn't used by the job anymore. Signed-off-by: Fiona Ebner --- PVE/CLI/pvesr.pm | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/PVE/CLI/pvesr.pm b/PVE/CLI/pvesr.pm index a1be88af..95dad64e 100644 --- a/PVE/CLI/pvesr.pm +++ b/PVE/CLI/pvesr.pm @@ -137,8 +137,18 @@ __PACKAGE__->register_method ({ push @$volids, map { $_->{volid} } @$images; } my ($local_snapshots, $cleaned_replicated_volumes) = PVE::Replication::prepare($storecfg, $volids, $jobid, $last_sync, $parent_snapname, $logfunc); - foreach my $volid (keys %$cleaned_replicated_volumes) { - if (!$wanted_volids->{$volid}) { + for my $volid ($volids->@*) { + next if $wanted_volids->{$volid}; + + my $stale = $cleaned_replicated_volumes->{$volid}; + # prepare() will not remove the last_sync snapshot, but if the volume was used by the + # job and is not wanted anymore, it is stale too. And not removing it now might cause + # it to be missed later, because the relevant storage might not get scanned anymore. + $stale ||= grep { + PVE::Replication::is_replication_snapshot($_, $jobid) + } keys %{$local_snapshots->{$volid} // {}}; + + if ($stale) { $logfunc->("$jobid: delete stale volume '$volid'"); PVE::Storage::vdisk_free($storecfg, $volid); delete $local_snapshots->{$volid}; -- 2.39.2