[PATCH qemu-server 07/14] blockdev: convert cdrom media eject/insert

Alexandre Derumier alexandre.derumier at groupe-cyllene.com
Tue Apr 22 13:51:34 CEST 2025


reuse the code for cloud-init too

Signed-off-by: Alexandre Derumier <alexandre.derumier at groupe-cyllene.com>
---
 PVE/QemuServer.pm | 58 +++++++++++++++++------------------------------
 1 file changed, 21 insertions(+), 37 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 02bbadd4..c72b58bf 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -5199,6 +5199,22 @@ sub vmconfig_update_agent {
     return; # either no actual change (e.g., format string reordered) or just hotpluggable changes
 }
 
+my sub qemu_change_cdrom_media {
+    my ($storecfg, $vmid, $opt, $drive) = @_;
+
+    # force eject if locked
+    mon_cmd($vmid, "blockdev-open-tray", force => JSON::true, id => $opt);
+    mon_cmd($vmid, "blockdev-remove-medium", id => $opt);
+    eval { qemu_drivedel($vmid, $opt) };
+
+    my $blockdev = generate_drive_blockdev($storecfg, $drive);
+    return if !$blockdev;
+
+    qemu_driveadd($storecfg, $vmid, $drive);
+    mon_cmd($vmid, "blockdev-insert-medium", id => $opt, 'node-name' => $blockdev->{'node-name'});
+    mon_cmd($vmid, "blockdev-close-tray", id => $opt);
+}
+
 sub vmconfig_update_disk {
     my ($storecfg, $conf, $hotplug, $vmid, $opt, $value, $arch, $machine_type) = @_;
 
@@ -5282,31 +5298,12 @@ sub vmconfig_update_disk {
 		return 1;
 	    }
 
-	} else { # cdrom
+       } else { # cdrom
 
-	    if ($drive->{file} eq 'none') {
-		mon_cmd($vmid, "eject", force => JSON::true, id => "$opt");
-		if (drive_is_cloudinit($old_drive)) {
-		    vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive);
-		}
-	    } else {
-		my ($path, $format) = PVE::QemuServer::Drive::get_path_and_format(
-		    $storecfg, $vmid, $drive);
-
-		# force eject if locked
-		mon_cmd($vmid, "eject", force => JSON::true, id => "$opt");
-
-		if ($path) {
-		    mon_cmd(
-			$vmid,
-			"blockdev-change-medium",
-			id => "$opt",
-			filename => "$path",
-			format => "$format",
-		    );
-		}
+	    qemu_change_cdrom_media($storecfg, $vmid, $opt, $drive);
+	    if (drive_is_cloudinit($old_drive)) {
+		vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive);
 	    }
-
 	    return 1;
 	}
     }
@@ -5339,20 +5336,7 @@ sub vmconfig_update_cloudinit_drive {
 
     my $running = PVE::QemuServer::check_running($vmid);
 
-    if ($running) {
-	my ($path, $format) = PVE::QemuServer::Drive::get_path_and_format(
-	    $storecfg, $vmid, $cloudinit_drive);
-	if ($path) {
-	    mon_cmd($vmid, "eject", force => JSON::true, id => "$cloudinit_ds");
-	    mon_cmd(
-		$vmid,
-		"blockdev-change-medium",
-		id => "$cloudinit_ds",
-		filename => "$path",
-		format => "$format",
-	    );
-	}
-    }
+    qemu_change_cdrom_media($storecfg, $vmid, $cloudinit_ds, $cloudinit_drive) if $running;
 }
 
 # called in locked context by incoming migration
-- 
2.39.5




More information about the pve-devel mailing list