attach_iso: add cdrom_index to target a specific CD/DVD drive

Enables mounting an ISO on a drive other than the first — e.g. a Cisco
touchless answer-file ISO on CD 2 alongside the bootable installer on CD 0.
Defaults to 0 (first drive); validates the index against the drive count.
This commit is contained in:
Ryan Malloy 2026-06-08 17:35:23 -06:00
parent e71610292a
commit 03d7875a83

View File

@ -430,6 +430,7 @@ class DiskManagementMixin(VSphereMixin):
vm_name: str,
iso_path: str,
datastore: str | None = None,
cdrom_index: int = 0,
host: str | None = None,
) -> dict[str, Any]:
"""Attach an ISO file to a VM's CD/DVD drive.
@ -438,6 +439,9 @@ class DiskManagementMixin(VSphereMixin):
vm_name: Name of the virtual machine
iso_path: Path to ISO file on datastore (e.g., 'iso/ubuntu.iso')
datastore: Datastore containing the ISO (default: first VM datastore)
cdrom_index: Which CD/DVD drive to use, 0-based (default 0 = first).
Use 1 for a second drive, e.g. a Cisco answer-file ISO alongside
the bootable installer on drive 0.
host: Managed ESXi host to target (default: the default host)
Returns:
@ -448,9 +452,19 @@ class DiskManagementMixin(VSphereMixin):
if not vm:
raise ValueError(f"VM '{vm_name}' not found")
cdrom = self._find_cdrom(vm)
if not cdrom:
cdroms = [
d
for d in vm.config.hardware.device
if isinstance(d, vim.vm.device.VirtualCdrom)
]
if not cdroms:
raise ValueError(f"No CD/DVD drive found on VM '{vm_name}'")
if cdrom_index >= len(cdroms):
raise ValueError(
f"VM '{vm_name}' has {len(cdroms)} CD/DVD drive(s); "
f"cdrom_index={cdrom_index} is out of range"
)
cdrom = cdroms[cdrom_index]
# Determine datastore
if not datastore: