From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 2D59191A04 for ; Thu, 29 Sep 2022 15:42:02 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 10EB54D9A for ; Thu, 29 Sep 2022 15:41:32 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Thu, 29 Sep 2022 15:41:30 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 8975B44657 for ; Thu, 29 Sep 2022 15:36:17 +0200 (CEST) From: Mira Limbeck To: pve-devel@lists.proxmox.com Date: Thu, 29 Sep 2022 15:36:12 +0200 Message-Id: <20220929133612.388969-2-m.limbeck@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220929133612.388969-1-m.limbeck@proxmox.com> References: <20220929133612.388969-1-m.limbeck@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.194 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods NO_DNS_FOR_FROM 0.001 Envelope sender has no MX or A DNS records SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [qemu.pm] Subject: [pve-devel] [PATCH qemu-server 2/2] reuse existing cloud-init disks X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Sep 2022 13:42:02 -0000 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 --- 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