Fix detach_iso: eject the ISO-bearing drive(s), not the first CD/DVD
detach_iso used _find_cdrom (first drive), so on VMs with multiple CD/DVD drives (e.g. Cisco CUCM's two) it 'ejected' an already-empty drive and reported previous_iso=null while the mounted ISO stayed put. Now it finds every drive with an IsoBackingInfo and ejects them all, reporting each.
This commit is contained in:
parent
3e435e3d5f
commit
e71610292a
@ -511,42 +511,56 @@ class DiskManagementMixin(VSphereMixin):
|
|||||||
if not vm:
|
if not vm:
|
||||||
raise ValueError(f"VM '{vm_name}' not found")
|
raise ValueError(f"VM '{vm_name}' not found")
|
||||||
|
|
||||||
cdrom = self._find_cdrom(vm)
|
cdroms = [
|
||||||
if not cdrom:
|
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}'")
|
raise ValueError(f"No CD/DVD drive found on VM '{vm_name}'")
|
||||||
|
|
||||||
# Get current ISO path for reporting
|
# Eject every drive that actually has an ISO mounted (a VM may have
|
||||||
old_iso = None
|
# several CD/DVD drives; the ISO is rarely on the first one).
|
||||||
if hasattr(cdrom.backing, "fileName"):
|
iso_cdroms = [
|
||||||
old_iso = cdrom.backing.fileName
|
c
|
||||||
|
for c in cdroms
|
||||||
|
if isinstance(c.backing, vim.vm.device.VirtualCdrom.IsoBackingInfo)
|
||||||
|
]
|
||||||
|
if not iso_cdroms:
|
||||||
|
return {
|
||||||
|
"vm": vm_name,
|
||||||
|
"action": "no_iso_mounted",
|
||||||
|
"previous_iso": None,
|
||||||
|
"ejected": [],
|
||||||
|
}
|
||||||
|
|
||||||
# Create empty client device backing (ejects the ISO)
|
device_changes = []
|
||||||
backing = vim.vm.device.VirtualCdrom.RemotePassthroughBackingInfo()
|
ejected = []
|
||||||
backing.deviceName = ""
|
for cdrom in iso_cdroms:
|
||||||
|
ejected.append(
|
||||||
|
{"cdrom": cdrom.deviceInfo.label, "previous_iso": cdrom.backing.fileName}
|
||||||
|
)
|
||||||
|
backing = vim.vm.device.VirtualCdrom.RemotePassthroughBackingInfo()
|
||||||
|
backing.deviceName = ""
|
||||||
|
backing.exclusive = False
|
||||||
|
cdrom.backing = backing
|
||||||
|
cdrom.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
|
||||||
|
cdrom.connectable.connected = False
|
||||||
|
cdrom.connectable.startConnected = False
|
||||||
|
cdrom.connectable.allowGuestControl = True
|
||||||
|
|
||||||
# Configure CD-ROM
|
spec = vim.vm.device.VirtualDeviceSpec()
|
||||||
cdrom.backing = backing
|
spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
|
||||||
cdrom.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
|
spec.device = cdrom
|
||||||
cdrom.connectable.connected = False
|
device_changes.append(spec)
|
||||||
cdrom.connectable.startConnected = False
|
|
||||||
cdrom.connectable.allowGuestControl = True
|
|
||||||
|
|
||||||
# Create device edit spec
|
config_spec = vim.vm.ConfigSpec(deviceChange=device_changes)
|
||||||
cdrom_spec = vim.vm.device.VirtualDeviceSpec()
|
|
||||||
cdrom_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
|
|
||||||
cdrom_spec.device = cdrom
|
|
||||||
|
|
||||||
# Create VM config spec
|
|
||||||
config_spec = vim.vm.ConfigSpec()
|
|
||||||
config_spec.deviceChange = [cdrom_spec]
|
|
||||||
|
|
||||||
# Reconfigure VM
|
|
||||||
task = vm.ReconfigVM_Task(spec=config_spec)
|
task = vm.ReconfigVM_Task(spec=config_spec)
|
||||||
conn.wait_for_task(task)
|
conn.wait_for_task(task)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"vm": vm_name,
|
"vm": vm_name,
|
||||||
"action": "iso_detached",
|
"action": "iso_detached",
|
||||||
"previous_iso": old_iso,
|
"previous_iso": ejected[0]["previous_iso"],
|
||||||
"cdrom": cdrom.deviceInfo.label,
|
"ejected": ejected,
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user