[pve-devel] [PATCH qemu-server 2/2] reuse existing cloud-init disks

Mira Limbeck m.limbeck at proxmox.com
Thu Sep 29 15:36:12 CEST 2022


When a disk exists but is not referenced in the config, it will be
reused instead of dying during the attempt to allocate the disk.

Signed-off-by: Mira Limbeck <m.limbeck at proxmox.com>
---
This patch is not required to fix the rollback code, but might be nice
to have in addition since there will still be some users with cloud-init
disks left on the storage.

 PVE/API2/Qemu.pm | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 3ec31c2..73d6ab9 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -348,15 +348,28 @@ my $create_disks = sub {
 		$fmt = $disk->{format} // "raw";
 	    }
 
-	    # Initial disk created with 4 MB and aligned to 4MB on regeneration
-	    my $ci_size = PVE::QemuServer::Cloudinit::CLOUDINIT_DISK_SIZE;
-	    my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, $name, $ci_size/1024);
-	    $disk->{file} = $volid;
+	    # since there was an issue with the rollback code not deleting cloud-init disks
+	    # there can be a case where leftover cloud-init disks are still on the storage.
+	    # since those will be overwritten anyway on each boot, we can just reuse them
+	    # if they already exist
+	    my $reused;
+	    my $ci_disk_found = PVE::Storage::vdisk_list($storecfg, $storeid, $vmid, ["$storeid:$name"], 'images');
+	    if ($ci_disk_found->{$storeid} && scalar($ci_disk_found->{$storeid}->@*)) {
+		print "Cloud-Init disk $name already exists on the storage '$storeid', reusing it.\n";
+		my $ci_disk = $ci_disk_found->{$storeid}[0];
+		$disk->{file} = $ci_disk->{volid};
+		$reused = 1;
+	    } else {
+		# Initial disk created with 4 MB and aligned to 4MB on regeneration
+		my $ci_size = PVE::QemuServer::Cloudinit::CLOUDINIT_DISK_SIZE;
+		my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, $name, $ci_size/1024);
+		$disk->{file} = $volid;
+	    }
 	    $disk->{media} = 'cdrom';
 	    push @$vollist, $volid;
 	    delete $disk->{format}; # no longer needed
 	    $res->{$ds} = PVE::QemuServer::print_drive($disk);
-	    print "$ds: successfully created disk '$res->{$ds}'\n";
+	    print "$ds: successfully created disk '$res->{$ds}'\n" if !$reused;
 	} elsif ($volid =~ $NEW_DISK_RE) {
 	    my ($storeid, $size) = ($2 || $default_storage, $3);
 	    die "no storage ID specified (and no default storage)\n" if !$storeid;
-- 
2.30.2






More information about the pve-devel mailing list