Recently, I had the need to remove some snapshots from a virtual machine using the CLI as there were issues with network connectivity. The CLI tool to do this is vim-cmd. It’s not something you have to use regularly, generally speaking, so I thought I’d do a quick post on how you can use it in relation to virtual machine snapshots. To start with you can use the tool to list the virtual machines registered on the host by running:
~ # vim-cmd vmsvc/getallvms Vmid Name File Guest OS Version 10 XP3 [datastore1] XP3/XP3.vmx windows7Server64Guest vmx-08 11 XP4 [datastore1] XP4/XP4.vmx windows7Server64Guest vmx-08 8 XP [datastore1] XP/XP.vmx otherGuest vmx-08 9 XP2 [datastore1] XP2/XP2.vmx otherGuest vmx-08
The snapshot related commands that the vmsvc/ namespace has available are:
- snapshot.create
- snapshot.dumpoption
- snapshot.get
- snapshot.remove
- snapshot.removeall
- snapshot.revert
- snapshot.setoption
- get.snapshotinfo
The virtual machine I am going to work with in this example is XP3, which has 10 as its VMID. From now on we will be using the VMID when running any commands against the virtual machine.
As it stands, this virtual machine has no snapshots. We can check by using the snapshot.get command, which won’t return any result. We can also keep an eye on the virtual machine’s work directory by using either just a directory listing or by using the watch command, for example – watch -d ‘ls -luth | grep -E “delta|flat”‘ :
-rw------- 1 root root 5.0G Aug 11 05:15 XP3-flat.vmdk
At the moment, there is just the -flat file, showing that there are no snapshots. We can take a snapshot by running:
~ # vim-cmd vmsvc/snapshot.create 10 test Create Snapshot:
The two options we have added to the snapshot.create command are the vmid and ‘test’ which is the name of the snapshot. We can create another, giving it a different name:
~ # vim-cmd vmsvc/snapshot.create 10 test2 Create Snapshot:
Checking the VMs directory we can now see:
-rw------- 1 root root 12.0k Aug 11 05:46 XP3-000002-delta.vmdk -rw------- 1 root root 12.0k Aug 11 05:44 XP3-000001-delta.vmdk -rw------- 1 root root 5.0G Aug 11 05:15 XP3-flat.vmdk
We can also view the snapshot chain by running:
~ # vim-cmd vmsvc/snapshot.get 10 Get Snapshot: |-ROOT --Snapshot Name : test --Snapshot Desciption : --Snapshot Created On : 8/11/2012 5:44:22 --Snapshot State : powered off --|-CHILD ----Snapshot Name : test2 ----Snapshot Desciption : ----Snapshot Created On : 8/11/2012 5:45:35 ----Snapshot State : powered off
You can also see the snapshot chain by looking at the contents of the xp3.vmsd file:
/vmfs/volumes/4f27b82e-3fc1540e-bf6b-000c295da2d9/XP3 # cat XP3.vmsd .encoding = "UTF-8" snapshot.lastUID = "12" snapshot.current = "12" snapshot0.uid = "11" snapshot0.filename = "XP3-Snapshot11.vmsn" snapshot0.displayName = "test" snapshot0.createTimeHigh = "313078" snapshot0.createTimeLow = "-203345443" snapshot0.numDisks = "1" snapshot0.disk0.fileName = "XP3.vmdk" snapshot0.disk0.node = "scsi0:0" snapshot1.uid = "12" snapshot1.filename = "XP3-Snapshot12.vmsn" snapshot1.parent = "11" snapshot1.displayName = "test2" snapshot1.description = "" snapshot1.createTimeHigh = "313078" snapshot1.createTimeLow = "-130379567" snapshot1.numDisks = "1" snapshot1.disk0.fileName = "XP3-000001.vmdk" snapshot1.disk0.node = "scsi0:0" snapshot.numSnapshots = "2"
To remove a snapshot you can use the snapshot.remove command, which will remove the most recent snapshot in the chain, or the snapshot.removeall command:
~ # vim-cmd vmsvc/snapshot.removeall 10 Remove Snapshot:
There are a few other options available, as listed above, but hopefully this has given a feel for how to interact with snapshots using vim-cmd.