Monday, March 17, 2014

How to find hidden snapshots in VMware

Sometimes VMware screws up, and leaves virtual machines running on a hidden snapshot.  This snapshot doesn't show up in the snapshot manager (so you can't remove it), nor does it show up with the consolidate helper.  If you edit the VMX file in the CLI or vSphere client - you can see the disk is pointed to a base disk ending in 00001.vdmk...or sometimes 00005.vmdk

WTF VMware?


If you have a large environment, this can be time consuming and difficult to track down for all your VMs.  However, you can use the power of the command line to save yourself some time.

SSH into the ESX host:

cd /vmfs/volumes/
grep -i 000001.vmdk */*/*.vmx |awk '{print $3}' |sort |uniq |cut -d "\"" -f 2

This will output a list of VMs (and their disks) that are running on a snapshot.  You may be surprised by the number of VMs.

Alternatively via PowerCLI (one-liner swiped from here):

foreach($vmview in get-view -viewtype virtualmachine){ $vmview.Config.Hardware.Device | ? {$_ -is [VMware.Vim.VirtualDisk]} | %{$_.Backing | select @{N="VMname";E={$vmview.name}},Filename |?{$_.FileName.Split('/')[-1] -match ".*\-[0-9]{6}\.vmdk"} }  }

Oh, you want to remove all the snaps at once?  Well, here's my new and improved code, based upon the above.

$DirtyVMs = foreach($vmview in get-view -viewtype virtualmachine){ $vmview.Config.Hardware.Device | ? {$_ -is [VMware.Vim.VirtualDisk]} | %{$_.Backing | select @{N="VMname";E={$vmview.name}},Filename |?{$_.FileName.Split('/')[-1] -match ".*\-[0-9]{6}\.vmdk"} }  }

$VMlist = $dirtyVMs | select VMname -unique

foreach($i in $VMlist)
{
New-Snapshot -Name DirtySnapCleaner -Description "Temporary Snapshot" -Memory:$false -Quiesce:$false -VM $i.VMname -Confirm:$false
Get-Snapshot -vm $i.VMname | remove-snapshot -removechildren -confirm:$false
}

1 comment:

  1. Hi there,
    just few remarks you can use to optimize.
    1) From my experience , try to avoid getting vms using their names, this can be tricky when you have env with vms with the same name, use morefs instead
    2) There is no need to switch to powercli cmdlets when the whole script was written using vsphere api
    3) in regards of that powercli script you have here you could combine the current $vmview with creating + deleting the snapshot using :
    $vmview.CreateSnapshot_Task + $vmview.RemoveAllSnapshots_Task
    In that way you would be 100% sure that you create and destroy snap chain for that particular vm, and not a vm that has a duplicated name.
    at the end, i know how it goes, i mean every administrator knows his env, and probably is aware if he has duplicate names or not, but , just wanted to share my thought.
    Great work!
    Greg

    ReplyDelete

Featured Post

Remove 3D Objects and other annoying folders on Windows 10

 Microsoft just keeps adding more crap to clutter up the navigation in Windows 10.  Seriously, who needs a 3D Objects folder?  The tiny perc...