[pve-devel] [PATCH qemu-server 1/6] Revert "cloudinit: avoid unsafe write of VM config"

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Nov 16 18:14:03 CET 2022


This reverts commit b137c30c3a5e4f5394e961a2048724fa18f86b2c.

In preparation of turning the special:cloudinig section from
a "current-state" into a "pending-changes" section.
---
 PVE/QemuServer.pm           | 20 ++++++++------------
 PVE/QemuServer/Cloudinit.pm | 19 +++++++++++--------
 2 files changed, 19 insertions(+), 20 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 3a8bc26..a585680 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -5020,7 +5020,7 @@ sub vmconfig_hotplug_pending {
 		# some changes can be done without hotplug
 		my $drive = parse_drive($opt, $value);
 		if (drive_is_cloudinit($drive)) {
-		    $conf->{cloudinit} = PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
+		    PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
 		}
 		vmconfig_update_disk($storecfg, $conf, $hotplug_features->{disk},
 				     $vmid, $opt, $value, $arch, $machine_type);
@@ -5062,7 +5062,7 @@ sub vmconfig_hotplug_pending {
 
     PVE::QemuConfig->write_config($vmid, $conf);
 
-    if ($hotplug_features->{cloudinit}) {
+    if($hotplug_features->{cloudinit}) {
 	my $pending = PVE::QemuServer::Cloudinit::get_pending_config($conf, $vmid);
 	my $regenerate = undef;
 	for my $item (@$pending) {
@@ -5169,11 +5169,9 @@ sub vmconfig_apply_pending {
 	}
     }
 
-    $conf->{cloudinit} = PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid)
-	if $generate_cloudnit;
-
     # write all changes at once to avoid unnecessary i/o
     PVE::QemuConfig->write_config($vmid, $conf);
+    PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid) if $generate_cloudnit;
 }
 
 sub vmconfig_update_net {
@@ -5377,8 +5375,7 @@ sub vmconfig_update_cloudinit_drive {
 
     return if !$cloudinit_drive;
 
-    $conf->{cloudinit} = PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
-    # FIXME: write out changed config here? needs to be sure that config is locked though!
+    PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
     my $running = PVE::QemuServer::check_running($vmid);
 
     if ($running) {
@@ -5560,13 +5557,12 @@ sub vm_start_nolock {
     if (!$statefile && scalar(keys %{$conf->{pending}})) {
 	vmconfig_apply_pending($vmid, $conf, $storecfg);
 	$conf = PVE::QemuConfig->load_config($vmid); # update/reload
-    } elsif (!$migratedfrom) {
-	# don't regenerate the ISO if the VM is started as part of a live migration
-	# this way we can reuse the old ISO with the correct config
-	$conf->{cloudinit} = PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
-	# FIXME: write out changed config here? if any changes
     }
 
+    # don't regenerate the ISO if the VM is started as part of a live migration
+    # this way we can reuse the old ISO with the correct config
+    PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid) if !$migratedfrom;
+
     # override offline migrated volumes, conf is out of date still
     if (my $offline_volumes = $migrate_opts->{offline_volumes}) {
 	for my $key (sort keys $offline_volumes->%*) {
diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm
index d4a34c4..b616c7b 100644
--- a/PVE/QemuServer/Cloudinit.pm
+++ b/PVE/QemuServer/Cloudinit.pm
@@ -566,6 +566,7 @@ sub generate_cloudinitconfig {
 
     PVE::QemuConfig->foreach_volume($conf, sub {
         my ($ds, $drive) = @_;
+
 	my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
 
 	return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
@@ -576,34 +577,36 @@ sub generate_cloudinitconfig {
 	$generator->($conf, $vmid, $drive, $volname, $storeid);
     });
 
-    my $cloudinit_conf = {};
+    my $cloudinitconf = delete $conf->{cloudinit};
+    $cloudinitconf = {};
 
     my @cloudinit_opts = keys %{PVE::QemuServer::cloudinit_config_properties()};
     push @cloudinit_opts, 'name';
 
     for my $opt (@cloudinit_opts) {
+
 	if ($opt =~ m/^ipconfig(\d+)/) {
 	    my $netid = "net$1";
 	    next if !defined($conf->{$netid});
-	    $cloudinit_conf->{$netid} = $conf->{$netid};
+	    $conf->{cloudinit}->{$netid} = $conf->{$netid};
 	}
 
-	$cloudinit_conf->{$opt} = $conf->{$opt} if $conf->{$opt};
+	$conf->{cloudinit}->{$opt} = $conf->{$opt} if $conf->{$opt};
     }
 
-    my $has_cloudinit_drive = 0;
+    $conf->{cloudinit}->{name} = "VM$vmid" if !$conf->{cloudinit}->{name};
+
     for my $opt (keys %{$conf}) {
 	if (PVE::QemuServer::is_valid_drivename($opt)) {
 	    my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
 	    if (PVE::QemuServer::drive_is_cloudinit($drive)) {
-		$has_cloudinit_drive = 1;
-		$cloudinit_conf->{$opt} = $conf->{$opt};
+		$conf->{cloudinit}->{$opt} = $conf->{$opt};
 	    }
 	}
     }
-    $cloudinit_conf->{name} //= "VM$vmid" if $has_cloudinit_drive;
 
-    return $cloudinit_conf;
+    PVE::QemuConfig->write_config($vmid, $conf);
+
 }
 
 sub dump_cloudinit_config {
-- 
2.30.2






More information about the pve-devel mailing list