[pve-devel] [PATCH v5 container 03/19] Use parse_volume instead of parse_ct-variants

Fabian Ebner f.ebner at proxmox.com
Wed Apr 8 11:24:58 CEST 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 58f38f8..3a8694a 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -1391,9 +1391,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};
@@ -1623,8 +1621,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};
 
@@ -1786,13 +1783,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 33f564f..54bd7a7 100755
--- a/src/PVE/CLI/pct.pm
+++ b/src/PVE/CLI/pct.pm
@@ -233,8 +233,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 06524ef..9adb366 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -206,7 +206,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;
@@ -687,7 +687,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";
 
@@ -1199,8 +1199,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 e613e78..e7fe99b 100644
--- a/src/PVE/LXC/Config.pm
+++ b/src/PVE/LXC/Config.pm
@@ -129,7 +129,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})
@@ -956,7 +956,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);
@@ -1059,22 +1059,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' ];
@@ -1086,9 +1070,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";
@@ -1282,7 +1268,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);
@@ -1335,7 +1321,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) {
@@ -1349,7 +1335,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);
 		};
@@ -1374,7 +1360,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