blockdev: add change_medium() helper

There is a slight change in behavior for cloud-init disks, when the
file for the new cloud-init disk is 'none'. Previously, the current
drive would not be ejected, now it is. Not sure if that edge case can
even happen in practice and it is more correct, becuase the config was
already updated.

Co-developed-by: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
This commit is contained in:
Fiona Ebner 2025-07-02 18:28:10 +02:00 committed by Fabian Grünbichler
parent 1da911757c
commit 59e01f1d44
2 changed files with 25 additions and 33 deletions

View file

@ -5170,30 +5170,15 @@ sub vmconfig_update_disk {
}
} else { # cdrom
eval { PVE::QemuServer::Blockdev::change_medium($storecfg, $vmid, $opt, $drive); };
my $err = $@;
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, $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",
);
}
if ($drive->{file} eq 'none' && drive_is_cloudinit($old_drive)) {
vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive);
}
die $err if $err;
return 1;
}
}
@ -5230,18 +5215,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, $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",
);
}
PVE::QemuServer::Blockdev::change_medium($storecfg, $vmid, $cloudinit_ds, $cloudinit_drive);
}
}

View file

@ -562,4 +562,22 @@ sub resize {
);
}
sub change_medium {
my ($storecfg, $vmid, $qdev_id, $drive) = @_;
# force eject if locked
mon_cmd($vmid, "eject", force => JSON::true, id => "$qdev_id");
my ($path, $format) = PVE::QemuServer::Drive::get_path_and_format($storecfg, $drive);
if ($path) { # no path for 'none'
mon_cmd(
$vmid, "blockdev-change-medium",
id => "$qdev_id",
filename => "$path",
format => "$format",
);
}
}
1;