From 03d7875a83356c65253ed4fdb49c59cd47cce3a6 Mon Sep 17 00:00:00 2001 From: Ryan Malloy Date: Mon, 8 Jun 2026 17:35:23 -0600 Subject: [PATCH] attach_iso: add cdrom_index to target a specific CD/DVD drive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/mcvsphere/mixins/disk_management.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/mcvsphere/mixins/disk_management.py b/src/mcvsphere/mixins/disk_management.py index 26096ac..d22161b 100644 --- a/src/mcvsphere/mixins/disk_management.py +++ b/src/mcvsphere/mixins/disk_management.py @@ -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: