How to list mounted ISO’s and unmount them from the command line in VMware ESXi

vmware-esxi

I have a VMware ESXi 6 host with several guests running on it. There is a datastore with an ISO file that is in use by one or more of these clients.
I am accessing the ESXi host through ssh from linux, so PowerCLI is not an option.

Q1: How can I, from the VMware CLI, find out which guests have this ISO file mounted?

Q2: How can I, once I know that, unmount this ISO file from these guests, also from the VMware CLI?

I expect it to be vim-cmd vmsvc subcommands, but I haven't been able to find them.

Best Answer

A quick & crude solution on the shell via SSH would be to write a small script that connects several actions:

  1. Get your VM IDs with vim-cmd vmsvc/getallvms|awk '{print $1}'|grep -o -E '[0-9]+' (list all VMs, only show first column with awk, filter out IPs and text and empty lines with grep)
  2. Iterate over the lines (ash does not have arrays like bash) and check for each number/ID if one occurrence of your chosen ISO name is found in the device listing for each VM: vim-cmd vmsvc/device.getdevices yourVmId|grep -o -A 12 -E 'yourImageName.iso' | grep -c 'connected = true' (list all devices, get the area around your ISO file, check if the ISO is currently mounted/active)
  3. Get the device ID of each CD drive (assuming it is only one, change the code for multiple drives on a single machine) with a modified grep from the same initial listing: vim-cmd vmsvc/device.getdevices yourVmId|grep -o -B 4 -E 'yourImageName.iso'|grep -o -E 'key = [0-9]+'|grep -o -E '[0-9]+' (double grep is neccessary because of missing group option -P)
  4. Use vim-cmd vmsvc/device.connection yourVmId yourDeviceId disconnect to disconnect the device. (Edit: It seems that this is not entirely correct, it does something, but not what I would expect. I'll update when I have time to investigate this further)

The only problem I encountered is that the message "CD drive locked by guest" may appear in the VSphere client while doing the last step, but maybe this can be disabled generally.

Related Topic