drive: add get_path_and_format() helper

To allow re-using it for CD-ROM hotplug.

No functional change intended.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
Reviewed-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Tested-by: Friedrich Weber <f.weber@proxmox.com>
This commit is contained in:
Fiona Ebner 2025-01-17 12:54:33 +01:00 committed by Fabian Grünbichler
parent 9f95aa78c7
commit edaf48cf55
2 changed files with 45 additions and 29 deletions

View File

@ -1463,41 +1463,14 @@ my sub drive_uses_cache_direct {
sub print_drive_commandline_full { sub print_drive_commandline_full {
my ($storecfg, $vmid, $drive, $live_restore_name, $io_uring) = @_; my ($storecfg, $vmid, $drive, $live_restore_name, $io_uring) = @_;
my $path;
my $volid = $drive->{file}; my $volid = $drive->{file};
my $drive_id = PVE::QemuServer::Drive::get_drive_id($drive); my $drive_id = PVE::QemuServer::Drive::get_drive_id($drive);
my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1); my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
my $scfg = $storeid ? PVE::Storage::storage_config($storecfg, $storeid) : undef; my $scfg = $storeid ? PVE::Storage::storage_config($storecfg, $storeid) : undef;
if (drive_is_cdrom($drive)) { my ($path, $format) = PVE::QemuServer::Drive::get_path_and_format(
$path = PVE::QemuServer::Drive::get_iso_path($storecfg, $vmid, $volid); $storecfg, $vmid, $drive, $live_restore_name);
die "$drive_id: cannot back cdrom drive with a live restore image\n" if $live_restore_name;
} else {
if ($storeid) {
$path = PVE::Storage::path($storecfg, $volid);
} else {
$path = $volid;
}
}
# For PVE-managed volumes, use the format from the storage layer and prevent overrides via the
# drive's 'format' option. For unmanaged volumes, fallback to 'raw' to avoid auto-detection by
# QEMU. For the special case 'none' (get_iso_path() returns an empty $path), there should be no
# format or QEMU won't start.
my $format;
if (drive_is_cdrom($drive) && !$path) {
# no format
} elsif ($storeid) {
$format = checked_volume_format($storecfg, $volid);
if ($drive->{format} && $drive->{format} ne $format) {
die "drive '$drive->{interface}$drive->{index}' - volume '$volid'"
." - 'format=$drive->{format}' option different from storage format '$format'\n";
}
} else {
$format = $drive->{format} // 'raw';
}
my $is_rbd = $path =~ m/^rbd:/; my $is_rbd = $path =~ m/^rbd:/;

View File

@ -100,6 +100,49 @@ sub get_iso_path {
} }
} }
# Returns the path that can be used on the QEMU commandline and in QMP commands as well as the
# checked format of the drive.
sub get_path_and_format {
my ($storecfg, $vmid, $drive, $live_restore_name) = @_;
my $path;
my $volid = $drive->{file};
my $drive_id = get_drive_id($drive);
my ($storeid) = PVE::Storage::parse_volume_id($volid, 1);
if (drive_is_cdrom($drive)) {
$path = get_iso_path($storecfg, $vmid, $volid);
die "$drive_id: cannot back cdrom drive with a live restore image\n" if $live_restore_name;
} else {
if ($storeid) {
$path = PVE::Storage::path($storecfg, $volid);
} else {
$path = $volid;
}
}
# For PVE-managed volumes, use the format from the storage layer and prevent overrides via the
# drive's 'format' option. For unmanaged volumes, fallback to 'raw' to avoid auto-detection by
# QEMU. For the special case 'none' (get_iso_path() returns an empty $path), there should be no
# format or QEMU won't start.
my $format;
if (drive_is_cdrom($drive) && !$path) {
# no format
} elsif ($storeid) {
$format = checked_volume_format($storecfg, $volid);
if ($drive->{format} && $drive->{format} ne $format) {
die "drive '$drive->{interface}$drive->{index}' - volume '$volid'"
." - 'format=$drive->{format}' option different from storage format '$format'\n";
}
} else {
$format = $drive->{format} // 'raw';
}
return ($path, $format);
}
my $MAX_IDE_DISKS = 4; my $MAX_IDE_DISKS = 4;
my $MAX_SCSI_DISKS = 31; my $MAX_SCSI_DISKS = 31;
my $MAX_VIRTIO_DISKS = 16; my $MAX_VIRTIO_DISKS = 16;