From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server 04/12] drive: move vm_is_volid_owner() and try_deallocate_drive() helpers to Drive module
Date: Tue, 4 Mar 2025 11:44:05 +0100 [thread overview]
Message-ID: <20250304104413.38638-5-f.ebner@proxmox.com> (raw)
In-Reply-To: <20250304104413.38638-1-f.ebner@proxmox.com>
While at it, avoid making the if conditions/lines for the
try_deallocate_drive() callers even longer, and bring the code in-line
with the style guide.
No functional change intended.
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
PVE/API2/Qemu.pm | 8 ++++++--
PVE/QemuServer.pm | 42 +++--------------------------------------
PVE/QemuServer/Drive.pm | 38 +++++++++++++++++++++++++++++++++++++
3 files changed, 47 insertions(+), 41 deletions(-)
diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 5ac61aa5..fc0df860 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -1972,13 +1972,17 @@ my $update_vm_api = sub {
my $drive = PVE::QemuServer::parse_drive($opt, $val);
PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'");
$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
- if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) {
+ my $remove_from_config = PVE::QemuServer::Drive::try_deallocate_drive(
+ $storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser);
+ if ($remove_from_config) {
delete $conf->{$opt};
PVE::QemuConfig->write_config($vmid, $conf);
}
} elsif ($opt eq 'vmstate') {
PVE::QemuConfig->check_protection($conf, "can't remove vmstate '$val'");
- if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, { file => $val }, $rpcenv, $authuser, 1)) {
+ my $remove_from_config = PVE::QemuServer::Drive::try_deallocate_drive(
+ $storecfg, $vmid, $conf, $opt, { file => $val }, $rpcenv, $authuser, 1);
+ if ($remove_from_config) {
delete $conf->{$opt};
PVE::QemuConfig->write_config($vmid, $conf);
}
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 9d06ac8b..454ee64a 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -1830,20 +1830,6 @@ sub add_random_macs {
}
}
-sub vm_is_volid_owner {
- my ($storecfg, $vmid, $volid) = @_;
-
- if ($volid !~ m|^/|) {
- my ($path, $owner);
- eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
- if ($owner && ($owner == $vmid)) {
- return 1;
- }
- }
-
- return;
-}
-
sub vmconfig_register_unused_drive {
my ($storecfg, $vmid, $conf, $drive) = @_;
@@ -1853,7 +1839,7 @@ sub vmconfig_register_unused_drive {
delete $conf->{cloudinit};
} elsif (!drive_is_cdrom($drive)) {
my $volid = $drive->{file};
- if (vm_is_volid_owner($storecfg, $vmid, $volid)) {
+ if (PVE::QemuServer::Drive::vm_is_volid_owner($storecfg, $vmid, $volid)) {
PVE::QemuConfig->add_unused_volume($conf, $volid, $vmid);
}
}
@@ -5027,29 +5013,6 @@ sub vmconfig_hotplug_pending {
}
}
-sub try_deallocate_drive {
- my ($storecfg, $vmid, $conf, $key, $drive, $rpcenv, $authuser, $force) = @_;
-
- if (($force || $key =~ /^unused/) && !drive_is_cdrom($drive, 1)) {
- my $volid = $drive->{file};
- if (vm_is_volid_owner($storecfg, $vmid, $volid)) {
- my $sid = PVE::Storage::parse_volume_id($volid);
- $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
-
- # check if the disk is really unused
- die "unable to delete '$volid' - volume is still in use (snapshot?)\n"
- if PVE::QemuServer::Drive::is_volume_in_use($storecfg, $conf, $key, $volid);
- PVE::Storage::vdisk_free($storecfg, $volid);
- return 1;
- } else {
- # If vm is not owner of this disk remove from config
- return 1;
- }
- }
-
- return;
-}
-
sub vmconfig_delete_or_detach_drive {
my ($vmid, $storecfg, $conf, $opt, $force) = @_;
@@ -5060,7 +5023,8 @@ sub vmconfig_delete_or_detach_drive {
if ($force) {
$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
- try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser, $force);
+ PVE::QemuServer::Drive::try_deallocate_drive(
+ $storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser, $force);
} else {
vmconfig_register_unused_drive($storecfg, $vmid, $conf, $drive);
}
diff --git a/PVE/QemuServer/Drive.pm b/PVE/QemuServer/Drive.pm
index 1041c1dd..8ff44641 100644
--- a/PVE/QemuServer/Drive.pm
+++ b/PVE/QemuServer/Drive.pm
@@ -998,4 +998,42 @@ sub get_scsi_device_type {
return $devicetype;
}
+
+sub vm_is_volid_owner {
+ my ($storecfg, $vmid, $volid) = @_;
+
+ if ($volid !~ m|^/|) {
+ my ($path, $owner);
+ eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
+ if ($owner && ($owner == $vmid)) {
+ return 1;
+ }
+ }
+
+ return;
+}
+
+sub try_deallocate_drive {
+ my ($storecfg, $vmid, $conf, $key, $drive, $rpcenv, $authuser, $force) = @_;
+
+ if (($force || $key =~ /^unused/) && !drive_is_cdrom($drive, 1)) {
+ my $volid = $drive->{file};
+ if (vm_is_volid_owner($storecfg, $vmid, $volid)) {
+ my $sid = PVE::Storage::parse_volume_id($volid);
+ $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
+
+ # check if the disk is really unused
+ die "unable to delete '$volid' - volume is still in use (snapshot?)\n"
+ if is_volume_in_use($storecfg, $conf, $key, $volid);
+ PVE::Storage::vdisk_free($storecfg, $volid);
+ return 1;
+ } else {
+ # If vm is not owner of this disk remove from config
+ return 1;
+ }
+ }
+
+ return;
+}
+
1;
--
2.39.5
_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
next prev parent reply other threads:[~2025-03-04 10:45 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-04 10:44 [pve-devel] [PATCH-SERIES guest-common/manager/qemu-server/container 00/12] backup/restore: improve missing drive and owned CD-ROM handling Fiona Ebner
2025-03-04 10:44 ` [pve-devel] [PATCH guest-common 01/12] abstract config: make get_backup_volumes() method more flexible Fiona Ebner
2025-03-04 10:44 ` [pve-devel] [PATCH manager 02/12] api: backup: adapt to new get_backup_volumes() signature Fiona Ebner
2025-03-04 10:44 ` [pve-devel] [PATCH qemu-server 03/12] cloudinit: avoid undefined value warning when no searchdomain is defined Fiona Ebner
2025-03-04 10:44 ` Fiona Ebner [this message]
2025-03-04 10:44 ` [pve-devel] [PATCH qemu-server 05/12] test: restore config: use diff to make failure human-readable Fiona Ebner
2025-03-04 10:44 ` [pve-devel] [PATCH qemu-server 06/12] test: restore config: add config with missing disks Fiona Ebner
2025-03-04 10:44 ` [pve-devel] [PATCH qemu-server 07/12] test: restore config: test behavior for disks with absolute paths Fiona Ebner
2025-03-04 10:44 ` [pve-devel] [PATCH qemu-server 08/12] backup restore: improve missing drive handling Fiona Ebner
2025-03-04 10:44 ` [pve-devel] [PATCH qemu-server 09/12] backup: adapt to new get_backup_volumes() signature Fiona Ebner
2025-03-04 10:44 ` [pve-devel] [PATCH qemu-server 10/12] backup: include owned CD-ROM volumes Fiona Ebner
2025-03-04 10:44 ` [pve-devel] [PATCH qemu-server 11/12] destroy vm: add reminder to also remove owned CD-ROMs starting with PVE 9 Fiona Ebner
2025-03-04 10:44 ` [pve-devel] [PATCH container 12/12] backup: adapt to new get_backup_volumes() signature Fiona Ebner
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=20250304104413.38638-5-f.ebner@proxmox.com \
--to=f.ebner@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.