[pve-devel] [PATCH v2 qemu-server 3/3] apply_pending: handle errors gracefully and remove redundant config write calls

Oguz Bektas o.bektas at proxmox.com
Fri Dec 13 12:41:53 CET 2019


wrap code in eval to handle errors gracefully.

that way we can replace redundant write/load config calls with a single
write_config at the end to avoid unnecessary i/o

Signed-off-by: Oguz Bektas <o.bektas at proxmox.com>
---
 PVE/QemuServer.pm | 52 +++++++++++++++++++++++------------------------
 1 file changed, 26 insertions(+), 26 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index ed6b557..abb2d83 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -4990,42 +4990,42 @@ sub vmconfig_apply_pending {
 
     my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
     foreach my $opt (sort keys %$pending_delete_hash) {
-	die "internal error" if $opt =~ m/^unused/;
 	my $force = $pending_delete_hash->{$opt}->{force};
-	$conf = PVE::QemuConfig->load_config($vmid); # update/reload
-	if (!defined($conf->{$opt})) {
-	    PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
-	    PVE::QemuConfig->write_config($vmid, $conf);
-	} elsif (is_valid_drivename($opt)) {
-	    vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force);
-	    PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
-	    delete $conf->{$opt};
-	    PVE::QemuConfig->write_config($vmid, $conf);
+	eval {
+	    die "internal error" if $opt =~ m/^unused/;
+	    if (!defined($conf->{$opt})) {
+		PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
+	    } elsif (is_valid_drivename($opt)) {
+		vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force);
+	    }
+	};
+	if (my $err = $@) {
+	    $add_apply_error->($opt, $err);
 	} else {
 	    PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
-	    delete $conf->{$opt};
-	    PVE::QemuConfig->write_config($vmid, $conf);
+	    delete $conf->${opt};
 	}
     }
 
-    $conf = PVE::QemuConfig->load_config($vmid); # update/reload
-
     foreach my $opt (keys %{$conf->{pending}}) { # add/change
-	$conf = PVE::QemuConfig->load_config($vmid); # update/reload
-
-	if (defined($conf->{$opt}) && ($conf->{$opt} eq $conf->{pending}->{$opt})) {
-	    # skip if nothing changed
-	} elsif (is_valid_drivename($opt)) {
-	    vmconfig_register_unused_drive($storecfg, $vmid, $conf, parse_drive($opt, $conf->{$opt}))
-		if defined($conf->{$opt});
-	    $conf->{$opt} = $conf->{pending}->{$opt};
+	next if $opt eq 'delete'; # just to be sure
+	eval {
+	    if (defined($conf->{$opt}) && ($conf->{$opt} eq $conf->{pending}->{$opt})) {
+		# skip if nothing changed
+	    } elsif (is_valid_drivename($opt)) {
+		vmconfig_register_unused_drive($storecfg, $vmid, $conf, parse_drive($opt, $conf->{$opt}))
+		    if defined($conf->{$opt});
+	    }
+	};
+	if (my $err = $@) {
+	    $add_apply_error->($opt, $err);
 	} else {
-	    $conf->{$opt} = $conf->{pending}->{$opt};
+	    PVE::QemuConfig->cleanup_pending($conf);
+	    $conf->{$opt} = delete $conf->{pending}->{$opt};
 	}
-
-	delete $conf->{pending}->{$opt};
-	PVE::QemuConfig->write_config($vmid, $conf);
     }
+
+    PVE::QemuConfig->write_config($vmid, $conf);
 }
 
 my $safe_num_ne = sub {
-- 
2.20.1




More information about the pve-devel mailing list