[pve-devel] [PATCH v3 guest-common 03/22] foreach_volume: generalize and implement function

Fabian Ebner f.ebner at proxmox.com
Thu Mar 12 13:08:13 CET 2020


Introduce a parameter $opts to allow for better control of which
keys/volumes to use for the iteration and ability to reverse the order.
Also, allow extra parameters for the function.

Removes the '__snapshot'-prefix for future use from outside the module.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 PVE/AbstractConfig.pm | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

diff --git a/PVE/AbstractConfig.pm b/PVE/AbstractConfig.pm
index 5b1683b..f2e130c 100644
--- a/PVE/AbstractConfig.pm
+++ b/PVE/AbstractConfig.pm
@@ -432,6 +432,31 @@ sub foreach_unused_volume {
     }
 }
 
+# Iterate over all configured volumes, calling $func for each key/value pair
+# with additional parameters @param.
+# By default, unused volumes and specials like vmstate are excluded.
+# Options: reverse         - reverses the order for the iteration
+#	   include_unused  - also iterate over unused volumes
+#	   extra_keys      - an array of extra keys to use for the iteration
+sub foreach_volume {
+    my ($class, $conf, $opts, $func, @param) = @_;
+
+    my @keys = $class->valid_volume_keys($opts->{reverse});
+    push @keys, @{$opts->{extra_keys}} if $opts->{extra_keys};
+
+    foreach my $key (@keys) {
+	my $volume_string = $conf->{$key};
+	next if !defined($volume_string);
+
+	my $volume = $class->parse_volume($key, $volume_string, 1);
+	next if !defined($volume);
+
+	$func->($key, $volume, @param);
+    }
+
+    $class->foreach_unused_volume($conf, $func, @param) if $opts->{include_unused};
+}
+
 # Returns whether the template parameter is set in $conf.
 sub is_template {
     my ($class, $conf) = @_;
@@ -583,13 +608,6 @@ sub __snapshot_rollback_get_unused {
     die "abstract method - implement me\n";
 }
 
-# Iterate over all configured volumes, calling $func for each key/value pair.
-sub __snapshot_foreach_volume {
-    my ($class, $conf, $func) = @_;
-
-    die "abstract method - implement me\n";
-}
-
 # Copy the current config $source to the snapshot config $dest
 sub __snapshot_copy_config {
     my ($class, $source, $dest) = @_;
@@ -722,7 +740,7 @@ sub snapshot_create {
 
 	$class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
 
-	$class->__snapshot_foreach_volume($snap, sub {
+	$class->foreach_volume($snap, undef, sub {
 	    my ($vs, $volume) = @_;
 
 	    $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
@@ -814,7 +832,7 @@ sub snapshot_delete {
     };
 
     # now remove all volume snapshots
-    $class->__snapshot_foreach_volume($snap, sub {
+    $class->foreach_volume($snap, undef, sub {
 	my ($vs, $volume) = @_;
 
 	return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup};
@@ -888,7 +906,7 @@ sub snapshot_rollback {
     
     my $snap = &$get_snapshot_config();
 
-    $class->__snapshot_foreach_volume($snap, sub {
+    $class->foreach_volume($snap, undef, sub {
 	my ($vs, $volume) = @_;
 
 	$class->__snapshot_rollback_vol_possible($volume, $snapname);
@@ -941,7 +959,7 @@ sub snapshot_rollback {
 
     $class->lock_config($vmid, $updatefn);
 
-    $class->__snapshot_foreach_volume($snap, sub {
+    $class->foreach_volume($snap, undef, sub {
 	my ($vs, $volume) = @_;
 
 	$class->__snapshot_rollback_vol_rollback($volume, $snapname);
-- 
2.20.1





More information about the pve-devel mailing list