public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Mira Limbeck <m.limbeck@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server 2/2] reuse existing cloud-init disks
Date: Thu, 29 Sep 2022 15:36:12 +0200	[thread overview]
Message-ID: <20220929133612.388969-2-m.limbeck@proxmox.com> (raw)
In-Reply-To: <20220929133612.388969-1-m.limbeck@proxmox.com>

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@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





  reply	other threads:[~2022-09-29 13:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-29 13:36 [pve-devel] [PATCH qemu-server 1/2] fix #4201: delete cloud-init disk on rollback Mira Limbeck
2022-09-29 13:36 ` Mira Limbeck [this message]
2022-09-30  8:21   ` [pve-devel] [PATCH qemu-server 2/2] reuse existing cloud-init disks Fiona Ebner
2022-09-30  8:21 ` [pve-devel] [PATCH qemu-server 1/2] fix #4201: delete cloud-init disk on rollback Fiona Ebner
2022-09-30  9:19   ` Mira Limbeck

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=20220929133612.388969-2-m.limbeck@proxmox.com \
    --to=m.limbeck@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