[pve-devel] [PATCH pve-container] parsing: throw by default unless $noerr is passed

Wolfgang Bumiller w.bumiller at proxmox.com
Thu Oct 15 11:09:49 CEST 2015


parse_ct_mountpoint and parse_lxc_mountpoint are now not usd
in schema verification anymore, so instead of returning
undef on error it can now die.

parse_ct_mountpoint now also gets a $noerr parameter as it
is used in foreach_mountpoint, and to be safe we'll just
skip invalid mountpoints there to avoid unexpected
inconsistent states.
---
 src/PVE/LXC.pm | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index a9a67c1..814dd4c 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -839,21 +839,29 @@ sub vmstatus {
 }
 
 sub parse_ct_mountpoint {
-    my ($data) = @_;
+    my ($data, $noerr) = @_;
 
     $data //= '';
 
     my $res;
     eval { $res = PVE::JSONSchema::parse_property_string($mp_desc, $data) };
     if ($@) {
-	warn $@;
-	return undef;
+	return undef if $noerr;
+	die $@;
     }
 
-    return undef if !defined($res->{volume});
+    if (!defined($res->{volume})) {
+	return undef if $noerr;
+	die "no volume set on mountpoint\n";
+    }
 
-    if ($res->{size}) {
-	return undef if !defined($res->{size} = PVE::JSONSchema::parse_size($res->{size}));
+    if (my $size = $res->{size}) {
+	$size = PVE::JSONSchema::parse_size($size);
+	if (!defined($size)) {
+	    return undef if $noerr;
+	    die "invalid size: $size\n";
+	}
+	$res->{size} = $size;
     }
 
     return $res;
@@ -877,11 +885,7 @@ sub parse_lxc_network {
 
     return $res if !$data;
 
-    eval { $res = PVE::JSONSchema::parse_property_string($netconf_desc, $data) };
-    if ($@) {
-	warn $@;
-	return undef;
-    }
+    $res = PVE::JSONSchema::parse_property_string($netconf_desc, $data);
 
     $res->{type} = 'veth';
     $res->{hwaddr} = PVE::Tools::random_ether_addr() if !$res->{hwaddr};
@@ -1926,7 +1930,8 @@ sub foreach_mountpoint_full {
     foreach my $key (mountpoint_names($reverse)) {
 	my $value = $conf->{$key};
 	next if !defined($value);
-	my $mountpoint = parse_ct_mountpoint($value);
+	my $mountpoint = parse_ct_mountpoint($value, 1);
+	next if !defined($mountpoint);
 
 	# just to be sure: rootfs is /
 	my $path = $key eq 'rootfs' ? '/' : $mountpoint->{mp};
-- 
2.1.4




More information about the pve-devel mailing list