VMWare Script to disconnect CD-rom devices and floppy drives

A physical CD-ROM or FDD that is connected to a virtual machine can prevent successful migration with VMotion and stop DRS. So it is important to insure CD-ROM and FDD are disconnected to VMs as part of regular SysAdmin Tasks. Source Reference Link:-http://communities.vmware.com/message/956136



#!/bin/bash

# forgotten devices.

# Edit by: Gabrie van Zanten (LogicaCMG)

vmwarelist=`vmware-cmd -l`
vmwarelist=`echo $vmwarelist | sed -e 's/ /*/g'`
vmwarelist=`echo $vmwarelist | sed -e 's/.vmx/.vmx /g'`

for vm in $vmwarelist
do
vm=`echo $vm | sed -e 's/*/ /g'`
vm=`echo $vm | sed -e 's/ \//*/g'`

if [ `vmware-cmd "$vm" getstate | sed -e 's/getstate() = //'` = "on" ]
then
echo Looking @ $vm
IDEBUS=`seq 0 1`
for i in $IDEBUS;
do
echo BUS : $i
IDEDEVICE=`seq 0 1`
for j in $IDEDEVICE;
do
PRESENT=`vmware-cmd "$vm" getconfig ide$i:$j.present | cut -f3 -d " "`
if [ $PRESENT = "true" ]
then
TYPE=`vmware-cmd "$vm" getconfig ide$i:$j.deviceType | cut -f3 -d " "`
if [[ $TYPE == "atapi-cdrom" || $TYPE == "cdrom-image" ]]
then
echo Found CDROM on IDE$i:$j
vmware-cmd "$vm" disconnectdevice ide$i:$j
fi
fi
done
done

FLOPPYDEV=`seq 0 1`
for f in $FLOPPYDEV;
do
echo FLOPPY $f
PRESENT=`vmware-cmd "$vm" getconfig floppy$f.present | cut -f3 -d " "`

if [ $PRESENT = "true" ]
then
echo Found FLOPPY on floppy$f
vmware-cmd "$vm" disconnectdevice floppy$f
fi
done
fi
done

Advertisements