[pve-devel] [PATCH v5 container 02/19] Use foreach_volume instead of foreach_mountpoint-variants

Fabian Ebner f.ebner at proxmox.com
Wed Apr 8 11:24:57 CEST 2020


Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 src/PVE/API2/LXC.pm        |  4 ++--
 src/PVE/API2/LXC/Config.pm |  2 +-
 src/PVE/API2/LXC/Status.pm |  2 +-
 src/PVE/CLI/pct.pm         |  4 ++--
 src/PVE/LXC.pm             | 14 +++++++-------
 src/PVE/LXC/Config.pm      | 35 +++++------------------------------
 src/PVE/LXC/Create.pm      |  4 ++--
 src/PVE/LXC/Migrate.pm     |  6 +++---
 src/PVE/VZDump/LXC.pm      |  2 +-
 src/lxc-pve-prestart-hook  |  2 +-
 10 files changed, 25 insertions(+), 50 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index f4c1a49..58f38f8 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -316,7 +316,7 @@ __PACKAGE__->register_method({
 
 	# check storage access, activate storage
 	my $delayed_mp_param = {};
-	PVE::LXC::Config->foreach_mountpoint($mp_param, sub {
+	PVE::LXC::Config->foreach_volume($mp_param, sub {
 	    my ($ms, $mountpoint) = @_;
 
 	    my $volid = $mountpoint->{volume};
@@ -371,7 +371,7 @@ __PACKAGE__->register_method({
 			$mp_param = $orig_mp_param;
 			die "rootfs configuration could not be recovered, please check and specify manually!\n"
 			    if !defined($mp_param->{rootfs});
-			PVE::LXC::Config->foreach_mountpoint($mp_param, sub {
+			PVE::LXC::Config->foreach_volume($mp_param, sub {
 			    my ($ms, $mountpoint) = @_;
 			    my $type = $mountpoint->{type};
 			    if ($type eq 'volume') {
diff --git a/src/PVE/API2/LXC/Config.pm b/src/PVE/API2/LXC/Config.pm
index 0879172..42e16d1 100644
--- a/src/PVE/API2/LXC/Config.pm
+++ b/src/PVE/API2/LXC/Config.pm
@@ -173,7 +173,7 @@ __PACKAGE__->register_method({
 	my $repl_conf = PVE::ReplicationConfig->new();
 	my $is_replicated = $repl_conf->check_for_existing_jobs($vmid, 1);
 	if ($is_replicated) {
-	    PVE::LXC::Config->foreach_mountpoint_full($param, 0, sub {
+	    PVE::LXC::Config->foreach_volume($param, sub {
 		my ($opt, $mountpoint) = @_;
 		my $volid = $mountpoint->{volume};
 		return if !$volid || !($mountpoint->{replicate}//1);
diff --git a/src/PVE/API2/LXC/Status.pm b/src/PVE/API2/LXC/Status.pm
index 41f1f4f..03d13a3 100644
--- a/src/PVE/API2/LXC/Status.pm
+++ b/src/PVE/API2/LXC/Status.pm
@@ -182,7 +182,7 @@ __PACKAGE__->register_method({
 		}
 
 		if ($conf->{unprivileged}) {
-		    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+		    PVE::LXC::Config->foreach_volume($conf, sub {
 			my ($ms, $mountpoint) = @_;
 			die "Quotas are not supported by unprivileged containers.\n" if $mountpoint->{quota};
 		    });
diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
index 95c6921..33f564f 100755
--- a/src/PVE/CLI/pct.pm
+++ b/src/PVE/CLI/pct.pm
@@ -379,7 +379,7 @@ __PACKAGE__->register_method({
 	    my @len = map { length($_) } @{$list[0]};
 
 	    eval {
-		PVE::LXC::Config->foreach_mountpoint($conf, sub {
+		PVE::LXC::Config->foreach_volume($conf, sub {
 		    my ($name, $mp) = @_;
 		    my $path = $mp->{mp};
 
@@ -783,7 +783,7 @@ __PACKAGE__->register_method ({
 	eval {
 	    my $path = "";
 	    PVE::LXC::mount_all($vmid, $storecfg, $conf);
-	    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+	    PVE::LXC::Config->foreach_volume($conf, sub {
 		my ($name, $mp) = @_;
 		$path = $mp->{mp};
 		my $cmd = ["fstrim", "-v", "$rootdir$path"];
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index b4ffc9b..06524ef 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -832,7 +832,7 @@ sub delete_mountpoint_volume {
 sub destroy_lxc_container {
     my ($storage_cfg, $vmid, $conf, $replacement_conf) = @_;
 
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+    PVE::LXC::Config->foreach_volume($conf, sub {
 	my ($ms, $mountpoint) = @_;
 	delete_mountpoint_volume($storage_cfg, $vmid, $mountpoint->{volume});
     });
@@ -1163,7 +1163,7 @@ sub template_create {
 
     my $storecfg = PVE::Storage::config();
 
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+    PVE::LXC::Config->foreach_volume($conf, sub {
 	my ($ms, $mountpoint) = @_;
 
 	my $volid = $mountpoint->{volume};
@@ -1172,7 +1172,7 @@ sub template_create {
 	    if !PVE::Storage::volume_has_feature($storecfg, 'template', $volid);
     });
 
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+    PVE::LXC::Config->foreach_volume($conf, sub {
 	my ($ms, $mountpoint) = @_;
 
 	my $volid = $mountpoint->{volume};
@@ -1237,7 +1237,7 @@ sub umount_all {
 
     my $res = 1;
 
-    PVE::LXC::Config->foreach_mountpoint_reverse($conf, sub {
+    PVE::LXC::Config->foreach_volume_full($conf, {'reverse' => 1}, sub {
 	my ($ms, $mountpoint) = @_;
 
 	my $volid = $mountpoint->{volume};
@@ -1278,7 +1278,7 @@ sub mount_all {
     my (undef, $rootuid, $rootgid) = parse_id_maps($conf);
 
     eval {
-	PVE::LXC::Config->foreach_mountpoint($conf, sub {
+	PVE::LXC::Config->foreach_volume($conf, sub {
 	    my ($ms, $mountpoint) = @_;
 
 	    $mountpoint->{ro} = 0 if $ignore_ro;
@@ -1885,7 +1885,7 @@ sub create_disks {
 	my (undef, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
 	my $chown_vollist = [];
 
-	PVE::LXC::Config->foreach_mountpoint($settings, sub {
+	PVE::LXC::Config->foreach_volume($settings, sub {
 	    my ($ms, $mountpoint) = @_;
 
 	    my $volid = $mountpoint->{volume};
@@ -1949,7 +1949,7 @@ sub update_disksize {
 	}
     };
 
-    PVE::LXC::Config->foreach_mountpoint($conf, $update_mp);
+    PVE::LXC::Config->foreach_volume($conf, $update_mp);
 
     return $changes;
 }
diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm
index 70aee5a..e613e78 100644
--- a/src/PVE/LXC/Config.pm
+++ b/src/PVE/LXC/Config.pm
@@ -67,7 +67,7 @@ sub has_feature {
 	$opts = {'valid_target_formats' => ['raw', 'subvol']};
     }
 
-    $class->foreach_mountpoint($conf, sub {
+    $class->foreach_volume($conf, sub {
 	my ($ms, $mountpoint) = @_;
 
 	return if $err; # skip further test
@@ -1395,7 +1395,7 @@ my $__is_volume_in_use = sub {
     my ($class, $config, $volid) = @_;
     my $used = 0;
 
-    $class->foreach_mountpoint($config, sub {
+    $class->foreach_volume($config, sub {
 	my ($ms, $mountpoint) = @_;
 	return if $used;
 	$used = $mountpoint->{type} eq 'volume' && $mountpoint->{volume} eq $volid;
@@ -1467,37 +1467,12 @@ sub valid_volume_keys {
     return $reverse ? reverse @names : @names;
 }
 
-sub foreach_mountpoint_full {
-    my ($class, $conf, $reverse, $func, @param) = @_;
-
-    my $mps = [ grep { defined($conf->{$_}) } $class->valid_volume_keys($reverse) ];
-    foreach my $key (@$mps) {
-	my $value = $conf->{$key};
-	my $mountpoint = $key eq 'rootfs' ? $class->parse_ct_rootfs($value, 1) : $class->parse_ct_mountpoint($value, 1);
-	next if !defined($mountpoint);
-
-	&$func($key, $mountpoint, @param);
-    }
-}
-
-sub foreach_mountpoint {
-    my ($class, $conf, $func, @param) = @_;
-
-    $class->foreach_mountpoint_full($conf, 0, $func, @param);
-}
-
-sub foreach_mountpoint_reverse {
-    my ($class, $conf, $func, @param) = @_;
-
-    $class->foreach_mountpoint_full($conf, 1, $func, @param);
-}
-
 sub get_vm_volumes {
     my ($class, $conf, $excludes) = @_;
 
     my $vollist = [];
 
-    $class->foreach_mountpoint($conf, sub {
+    $class->foreach_volume($conf, sub {
 	my ($ms, $mountpoint) = @_;
 
 	return if $excludes && $ms eq $excludes;
@@ -1555,14 +1530,14 @@ sub get_replicatable_volumes {
 	$volhash->{$volid} = 1;
     };
 
-    $class->foreach_mountpoint($conf, sub {
+    $class->foreach_volume($conf, sub {
 	my ($ms, $mountpoint) = @_;
 	$test_volid->($mountpoint->{volume}, $mountpoint);
     });
 
     foreach my $snapname (keys %{$conf->{snapshots}}) {
 	my $snap = $conf->{snapshots}->{$snapname};
-	$class->foreach_mountpoint($snap, sub {
+	$class->foreach_volume($snap, sub {
 	    my ($ms, $mountpoint) = @_;
 	    $test_volid->($mountpoint->{volume}, $mountpoint);
         });
diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm
index 9faec63..52b0b48 100644
--- a/src/PVE/LXC/Create.pm
+++ b/src/PVE/LXC/Create.pm
@@ -212,7 +212,7 @@ sub recover_config_from_proxmox_backup {
     delete $conf->{snapshots};
 
     my $mp_param = {};
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+    PVE::LXC::Config->foreach_volume($conf, sub {
 	my ($ms, $mountpoint) = @_;
 	$mp_param->{$ms} = $conf->{$ms};
     });
@@ -233,7 +233,7 @@ sub recover_config_from_tar {
 
 	delete $conf->{snapshots};
 
-	PVE::LXC::Config->foreach_mountpoint($conf, sub {
+	PVE::LXC::Config->foreach_volume($conf, sub {
 	    my ($ms, $mountpoint) = @_;
 	    $mp_param->{$ms} = $conf->{$ms};
 	});
diff --git a/src/PVE/LXC/Migrate.pm b/src/PVE/LXC/Migrate.pm
index 6c1e9c1..638ce1f 100644
--- a/src/PVE/LXC/Migrate.pm
+++ b/src/PVE/LXC/Migrate.pm
@@ -47,7 +47,7 @@ sub prepare {
     my $force = $self->{opts}->{force} // 0;
     my $need_activate = [];
 
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+    PVE::LXC::Config->foreach_volume($conf, sub {
 	my ($ms, $mountpoint) = @_;
 
 	my $volid = $mountpoint->{volume};
@@ -217,11 +217,11 @@ sub phase1 {
     foreach my $snapname (keys %{$conf->{snapshots}}) {
 	&$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef)
 	    if defined($conf->{snapshots}->{$snapname}->{'vmstate'});
-	PVE::LXC::Config->foreach_mountpoint($conf->{snapshots}->{$snapname}, $test_mp, $snapname);
+	PVE::LXC::Config->foreach_volume($conf->{snapshots}->{$snapname}, $test_mp, $snapname);
     }
 
     # finally all currently used volumes
-    PVE::LXC::Config->foreach_mountpoint($conf, $test_mp);
+    PVE::LXC::Config->foreach_volume($conf, $test_mp);
 
 
     # additional checks for local storage
diff --git a/src/PVE/VZDump/LXC.pm b/src/PVE/VZDump/LXC.pm
index 09c4d47..2d003d0 100644
--- a/src/PVE/VZDump/LXC.pm
+++ b/src/PVE/VZDump/LXC.pm
@@ -120,7 +120,7 @@ sub prepare {
     $task->{rootgid} = $rootgid;
 
     my $volids = $task->{volids} = [];
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+    PVE::LXC::Config->foreach_volume($conf, sub {
 	my ($name, $data) = @_;
 	my $volid = $data->{volume};
 	my $mount = $data->{mp};
diff --git a/src/lxc-pve-prestart-hook b/src/lxc-pve-prestart-hook
index 66569f9..ed25aa4 100755
--- a/src/lxc-pve-prestart-hook
+++ b/src/lxc-pve-prestart-hook
@@ -125,7 +125,7 @@ PVE::LXC::Tools::lxc_hook('pre-start', 'lxc', sub {
 	};
     }
 
-    PVE::LXC::Config->foreach_mountpoint($conf, $setup_mountpoint);
+    PVE::LXC::Config->foreach_volume($conf, $setup_mountpoint);
 
     my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir);
     $lxc_setup->pre_start_hook();
-- 
2.20.1





More information about the pve-devel mailing list