[pve-devel] [PATCH v11 cloudinit 9/9] cloudinit 'hotplug'

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Aug 19 12:51:14 CEST 2015


*) always replace old cloudinit images
*) apply pending cloudinit changes when generating a new
image

For cloudinit we now always use vdisk_free before
vdisk_alloc in order to always replace old images, this
allows us to hotplug a new drive by setting it to
`none,media=cdrom` first (to eject the disk), then setting
it back to 'storage:cloudinit' to have a new image generated
after applying the currently pending changes.
---
 PVE/API2/Qemu.pm  |  5 ++++-
 PVE/QemuServer.pm | 36 ++++++++++++++++++++++++++++++++++--
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 4341506..8762ca2 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -131,6 +131,9 @@ my $create_disks = sub {
 	    }
 	    # FIXME: Reasonable size? qcow2 shouldn't grow if the space isn't used anyway?
 	    my $cloudinit_iso_size = 5; # in MB
+	    eval { # delete old images if they exist
+		PVE::Storage::vdisk_free($storecfg, "$storeid:$vmid/$name");
+	    }; # ignore errors
 	    my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, 
 						  $fmt, $name, $cloudinit_iso_size*1024);
 	    $disk->{file} = $volid;
@@ -138,7 +141,6 @@ my $create_disks = sub {
 	    push @$vollist, $volid;
 	    delete $disk->{format}; # no longer needed
 	    $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
-	
 	} elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
 	    my ($storeid, $size) = ($2 || $default_storage, $3);
 	    die "no storage ID specified (and no default storage)\n" if !$storeid;
@@ -935,6 +937,7 @@ my $update_vm_api  = sub {
 
 		if (PVE::QemuServer::valid_drivename($opt)) {
 		    my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
+		    # FIXME: cloudinit: CDROM or Disk?
 		    if (PVE::QemuServer::drive_is_cdrom($drive)) { # CDROM
 			$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
 		    } else {
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 5f14a90..a026dd7 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -387,6 +387,9 @@ EODESCR
 	maxLength => 256,
 	optional => 1,
     },
+};
+
+my $confdesc_cloudinit = {
     searchdomain => {
         optional => 1,
         type => 'string',
@@ -503,7 +506,11 @@ PVE::JSONSchema::register_standard_option("pve-qm-ipconfig", $netdesc);
 
 for (my $i = 0; $i < $MAX_NETS; $i++)  {
     $confdesc->{"net$i"} = $netdesc;
-    $confdesc->{"ipconfig$i"} = $ipconfigdesc;
+    $confdesc_cloudinit->{"ipconfig$i"} = $ipconfigdesc;
+}
+
+foreach my $key (keys %$confdesc_cloudinit) {
+    $confdesc->{$key} = $confdesc_cloudinit->{$key};
 }
 
 my $drivename_hash;
@@ -1296,10 +1303,15 @@ sub print_netdev_full {
     return $netdev;
 }
 
+sub drive_is_cloudinit {
+    my ($drive) = @_;
+    return $drive->{file} =~ m@[:/]vm-\d+-cloudinit(?:\.$QEMU_FORMAT_RE)?$@;
+}
+
 sub drive_is_cdrom {
     my ($drive, $exclude_cloudinit) = @_;
 
-    return 0 if $exclude_cloudinit && $drive->{file} =~ m@[:/]vm-\d+-cloudinit(?:\.$QEMU_FORMAT_RE)?$@;
+    return 0 if $exclude_cloudinit && drive_is_cloudinit($drive);
 
     return $drive && $drive->{media} && ($drive->{media} eq 'cdrom');
 
@@ -4064,6 +4076,22 @@ sub vmconfig_hotplug_pending {
 	}
     }
 
+    my $apply_pending_cloudinit;
+    $apply_pending_cloudinit = sub {
+	my ($key, $value) = @_;
+	$apply_pending_cloudinit = sub {}; # once is enough
+
+	my @cloudinit_opts = keys %$confdesc_cloudinit;
+	foreach my $opt (keys %{$conf->{pending}}) {
+	    next if !grep { $_ eq $opt } @cloudinit_opts;
+	    $conf->{$opt} = delete $conf->{pending}->{$opt};
+	}
+
+	my $new_conf = { %$conf };
+	$new_conf->{$key} = $value;
+	generate_cloudinitconfig($new_conf, $vmid);
+    };
+
     foreach my $opt (keys %{$conf->{pending}}) {
 	next if $selection && !$selection->{$opt};
 	my $value = $conf->{pending}->{$opt};
@@ -4097,6 +4125,10 @@ sub vmconfig_hotplug_pending {
 				    $vmid, $opt, $value);
 	    } elsif (valid_drivename($opt)) {
 		# some changes can be done without hotplug
+		my $drive = parse_drive($opt, $value);
+		if (drive_is_cloudinit($drive)) {
+		    &$apply_pending_cloudinit($opt, $value);
+		}
 		vmconfig_update_disk($storecfg, $conf, $hotplug_features->{disk},
 				     $vmid, $opt, $value, 1);
 	    } elsif ($opt =~ m/^memory$/) { #dimms
-- 
2.1.4





More information about the pve-devel mailing list