public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server 1/6] Revert "cloudinit: avoid unsafe write of VM config"
Date: Wed, 16 Nov 2022 18:14:03 +0100	[thread overview]
Message-ID: <20221116171408.216775-2-w.bumiller@proxmox.com> (raw)
In-Reply-To: <20221116171408.216775-1-w.bumiller@proxmox.com>

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





  reply	other threads:[~2022-11-16 17:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-16 17:14 [pve-devel] [PATCH qemu-server 0/6] Manage [special:cloudinit] in hotplug code Wolfgang Bumiller
2022-11-16 17:14 ` Wolfgang Bumiller [this message]
2022-11-16 17:14 ` [pve-devel] [PATCH qemu-server 2/6] Partially-revert "cloudinit: add cloudinit section for current generated config" Wolfgang Bumiller
2022-11-16 17:14 ` [pve-devel] [PATCH qemu-server 3/6] delay cloudinit generation in hotplug Wolfgang Bumiller
2022-11-16 17:14 ` [pve-devel] [PATCH qemu-server 4/6] record cloud-init changes in the cloudinit section Wolfgang Bumiller
2022-11-16 17:14 ` [pve-devel] [PATCH qemu-server 5/6] don't call 'cleanup_config' " Wolfgang Bumiller
2022-11-16 17:14 ` [pve-devel] [PATCH qemu-server 6/6] drop get_pending_changes and simplify cloudinit_pending api call Wolfgang Bumiller
2022-11-16 17:41 ` [pve-devel] applied-series: [PATCH qemu-server 0/6] Manage [special:cloudinit] in hotplug code Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221116171408.216775-2-w.bumiller@proxmox.com \
    --to=w.bumiller@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal