[PATCH qemu-server 06/13] blockdev: add support for cdrom media eject/insert

Alexandre Derumier alexandre.derumier at groupe-cyllene.com
Tue Jun 3 09:55:47 CEST 2025


reuse the code for cloud-init too

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

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 0af091aa..8e1369fc 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -5347,6 +5347,42 @@ 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) = @_;
+
+    my $machine_type = PVE::QemuServer::Machine::get_current_qemu_machine($vmid);
+    if (PVE::QemuServer::Machine::is_machine_version_at_least($machine_type, 10, 0)) {
+	# 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);
+    } else {
+	# force eject if locked
+	mon_cmd($vmid, "eject", force => JSON::true, id => "$opt");
+	return if $drive->{file} eq 'none';
+
+	my ($path, $format) = PVE::QemuServer::Drive::get_path_and_format(
+	    $storecfg, $vmid, $drive);
+
+	if ($path) {
+	    mon_cmd(
+		$vmid,
+		"blockdev-change-medium",
+		id => "$opt",
+		filename => "$path",
+		format => "$format",
+	    );
+	}
+    }
+}
+
 sub vmconfig_update_disk {
     my ($storecfg, $conf, $hotplug, $vmid, $opt, $value, $arch, $machine_type) = @_;
 
@@ -5430,29 +5466,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->{file} eq 'none' && drive_is_cloudinit($old_drive)) {
+		vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive);
 	    }
 
 	    return 1;
@@ -5486,21 +5505,9 @@ sub vmconfig_update_cloudinit_drive {
     }
 
     my $running = PVE::QemuServer::check_running($vmid);
+    return if !$running;
 
-    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);
 }
 
 # called in locked context by incoming migration
-- 
2.39.5




More information about the pve-devel mailing list