[pve-devel] [PATCH v4 container 11/27] Use parse_volume instead of parse_ct-variants

Fabian Ebner f.ebner at proxmox.com
Thu Mar 26 09:09:41 CET 2020


Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 src/PVE/API2/LXC.pm   | 15 +++------------
 src/PVE/CLI/pct.pm    |  3 +--
 src/PVE/LXC.pm        |  7 +++----
 src/PVE/LXC/Config.pm | 34 ++++++++++------------------------
 4 files changed, 17 insertions(+), 42 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index c5d42ff..e901dea 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -1416,9 +1416,7 @@ __PACKAGE__->register_method({
 		    my $value = $src_conf->{$opt};
 
 		    if (($opt eq 'rootfs') || ($opt =~ m/^mp\d+$/)) {
-			my $mp = $opt eq 'rootfs' ?
-			    PVE::LXC::Config->parse_ct_rootfs($value) :
-			    PVE::LXC::Config->parse_ct_mountpoint($value);
+			my $mp = PVE::LXC::Config->parse_volume($opt, $value);
 
 			if ($mp->{type} eq 'volume') {
 			    my $volid = $mp->{volume};
@@ -1648,8 +1646,7 @@ __PACKAGE__->register_method({
 	    my $running = PVE::LXC::check_running($vmid);
 
 	    my $disk = $param->{disk};
-	    my $mp = $disk eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$disk}) :
-		PVE::LXC::Config->parse_ct_mountpoint($conf->{$disk});
+	    my $mp = PVE::LXC::Config->parse_volume($disk, $conf->{$disk});
 
 	    my $volid = $mp->{volume};
 
@@ -1811,13 +1808,7 @@ __PACKAGE__->register_method({
 
 	    die "cannot move volumes of a running container\n" if PVE::LXC::check_running($vmid);
 
-	    if ($mpkey eq 'rootfs') {
-		$mpdata = PVE::LXC::Config->parse_ct_rootfs($conf->{$mpkey});
-	    } elsif ($mpkey =~ m/mp\d+/) {
-		$mpdata = PVE::LXC::Config->parse_ct_mountpoint($conf->{$mpkey});
-	    } else {
-		die "Can't parse $mpkey\n";
-	    }
+	    PVE::LXC::Config->parse_volume($mpkey, $conf->{$mpkey});
 	    $old_volid = $mpdata->{volume};
 
 	    die "you can't move a volume with snapshots and delete the source\n"
diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
index a880705..b048c44 100755
--- a/src/PVE/CLI/pct.pm
+++ b/src/PVE/CLI/pct.pm
@@ -232,8 +232,7 @@ __PACKAGE__->register_method ({
 
 	    defined($conf->{$device}) || die "cannot run command on non-existing mount point $device\n";
 
-	    my $mount_point = $device eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$device}) :
-		PVE::LXC::Config->parse_ct_mountpoint($conf->{$device});
+	    my $mount_point = PVE::LXC::Config->parse_volume($device, $conf->{$device});
 
 	    die "cannot run fsck when container is running\n"
 		if PVE::LXC::check_running($vmid);
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index e20005a..4b0059b 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -222,7 +222,7 @@ sub vmstatus {
 	    $d->{disk} = 0;
 	    # use 4GB by default ??
 	    if (my $rootfs = $conf->{rootfs}) {
-		my $rootinfo = PVE::LXC::Config->parse_ct_rootfs($rootfs);
+		my $rootinfo = PVE::LXC::Config->parse_volume('rootfs', $rootfs);
 		$d->{maxdisk} = $rootinfo->{size} || (4*1024*1024*1024);
 	    } else {
 		$d->{maxdisk} = 4*1024*1024*1024;
@@ -722,7 +722,7 @@ sub update_lxc_config {
     die "missing 'rootfs' configuration\n"
 	if !defined($conf->{rootfs});
 
-    my $mountpoint = PVE::LXC::Config->parse_ct_rootfs($conf->{rootfs});
+    my $mountpoint = PVE::LXC::Config->parse_volume('rootfs', $conf->{rootfs});
 
     $raw .= "lxc.rootfs.path = $dir/rootfs\n";
 
@@ -1227,8 +1227,7 @@ sub check_ct_modify_config_perm {
 	} elsif ($opt eq 'rootfs' || $opt =~ /^mp\d+$/) {
 	    $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
 	    return if $delete;
-	    my $data = $opt eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($newconf->{$opt})
-					: PVE::LXC::Config->parse_ct_mountpoint($newconf->{$opt});
+	    my $data = PVE::LXC::Config->parse_volume($opt, $newconf->{$opt});
 	    raise_perm_exc("mount point type $data->{type} is only allowed for root\@pam")
 		if $data->{type} ne 'volume';
 	} elsif ($opt eq 'memory' || $opt eq 'swap') {
diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm
index 821e84d..1f8e968 100644
--- a/src/PVE/LXC/Config.pm
+++ b/src/PVE/LXC/Config.pm
@@ -124,7 +124,7 @@ sub __snapshot_delete_remove_drive {
 	die "implement me - saving vmstate\n";
     } else {
 	my $value = $snap->{$remove_drive};
-	my $mountpoint = $remove_drive eq 'rootfs' ? $class->parse_ct_rootfs($value, 1) : $class->parse_ct_mountpoint($value, 1);
+	my $mountpoint = $class->parse_volume($remove_drive, $value, 1);
 	delete $snap->{$remove_drive};
 
 	$class->add_unused_volume($snap, $mountpoint->{volume})
@@ -951,7 +951,7 @@ sub update_pct_config {
 	my $value = $param->{$opt};
 	if ($opt =~ m/^mp(\d+)$/ || $opt eq 'rootfs') {
 	    $class->check_protection($conf, "can't update CT $vmid drive '$opt'");
-	    my $mp = $opt eq 'rootfs' ? $class->parse_ct_rootfs($value) : $class->parse_ct_mountpoint($value);
+	    my $mp = $class->parse_volume($opt, $value);
 	    $check_content_type->($mp) if ($mp->{type} eq 'volume');
 	} elsif ($opt eq 'hookscript') {
 	    PVE::GuestHelpers::check_hookscript($value);
@@ -1054,22 +1054,6 @@ sub __parse_ct_mountpoint_full {
     return $res;
 };
 
-sub parse_ct_rootfs {
-    my ($class, $data, $noerr) = @_;
-
-    my $res =  $class->__parse_ct_mountpoint_full($rootfs_desc, $data, $noerr);
-
-    $res->{mp} = '/' if defined($res);
-
-    return $res;
-}
-
-sub parse_ct_mountpoint {
-    my ($class, $data, $noerr) = @_;
-
-    return $class->__parse_ct_mountpoint_full($mp_desc, $data, $noerr);
-}
-
 sub print_ct_mountpoint {
     my ($class, $info, $nomp) = @_;
     my $skip = [ 'type' ];
@@ -1081,9 +1065,11 @@ sub parse_volume {
     my ($class, $key, $volume_string, $noerr) = @_;
 
     if ($key eq 'rootfs') {
-	return $class->parse_ct_rootfs($volume_string, $noerr);
+	my $res =  $class->__parse_ct_mountpoint_full($rootfs_desc, $volume_string, $noerr);
+	$res->{mp} = '/' if defined($res);
+	return $res;
     } elsif ($key =~ m/^mp\d+$/ || $key =~ m/^unused\d+$/) {
-	return $class->parse_ct_mountpoint($volume_string, $noerr);
+	return $class->__parse_ct_mountpoint_full($mp_desc, $volume_string, $noerr);
     }
 
     die "parse_volume - unknown type: $key\n";
@@ -1295,7 +1281,7 @@ sub vmconfig_apply_pending {
 	next if $selection && !$selection->{$opt};
 	eval {
 	    if ($opt =~ m/^mp(\d+)$/) {
-		my $mp = $class->parse_ct_mountpoint($conf->{$opt});
+		my $mp = $class->parse_volume($opt, $conf->{$opt});
 		if ($mp->{type} eq 'volume') {
 		    $class->add_unused_volume($conf, $mp->{volume})
 			if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
@@ -1348,7 +1334,7 @@ my $rescan_volume = sub {
 sub apply_pending_mountpoint {
     my ($class, $vmid, $conf, $opt, $storecfg, $running) = @_;
 
-    my $mp = $class->parse_ct_mountpoint($conf->{pending}->{$opt});
+    my $mp = $class->parse_volume($opt, $conf->{pending}->{$opt});
     my $old = $conf->{$opt};
     if ($mp->{type} eq 'volume') {
 	if ($mp->{volume} =~ $PVE::LXC::NEW_DISK_RE) {
@@ -1362,7 +1348,7 @@ sub apply_pending_mountpoint {
 	    );
 	    if ($running) {
 		# Re-parse mount point:
-		my $mp = $class->parse_ct_mountpoint($conf->{pending}->{$opt});
+		my $mp = $class->parse_volume($opt, $conf->{pending}->{$opt});
 		eval {
 		    PVE::LXC::mountpoint_hotplug($vmid, $conf, $opt, $mp, $storecfg);
 		};
@@ -1387,7 +1373,7 @@ sub apply_pending_mountpoint {
     }
 
     if (defined($old)) {
-	my $mp = $class->parse_ct_mountpoint($old);
+	my $mp = $class->parse_volume($opt, $old);
 	if ($mp->{type} eq 'volume') {
 	    $class->add_unused_volume($conf, $mp->{volume})
 		if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
-- 
2.20.1





More information about the pve-devel mailing list