4
votes

I'm trying to write a bash script as simple as possible to clone virtual machines from a "parent" virtual machine (called peer00). In order to clone those machines I firstly need a snapshot.

vboxmanage snapshot peer00 delete "MySnapShot"
vboxmanage snapshot peer00 take "MySnapShot"

# for loop to create several machines

Now if I run this script when there is no MySnapShot snapshot I get the following error (obviously):

VBoxManage: error: Could not find a snapshot named 'MySnapShot' VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component SessionMachine, interface IMachine, callee nsISupports VBoxManage: error: Context: "FindSnapshot(Bstr(a->argv[2]).raw(), pSnapshot.asOutParam())" at line 514 of file VBoxManageSnapshot.cpp

On the other hand, if I run it while the snapshot already exists I get this other output:

0%... Progress state: NS_ERROR_FAILURE VBoxManage: error: Snapshot operation failed VBoxManage: error: Hard disk '/home/pietro/VirtualBox VMs/peer00/Snapshots/{0183ad41-71b6-41bf-af74-38db828b2d82}.vdi' has more than one child hard disk (7) VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component SessionMachine, interface IMachine VBoxManage: error: Context: "RTEXITCODE handleSnapshot(HandlerArg*)" at line 532 of file VBoxManageSnapshot.cpp

How to write a sort of "if statement" that will check if the snapshot already exists before trying to delete it? Moreover why am I getting the second error? I really don't understand

1
There is a --uniquename parameter (that supports Number,Timestamp,Space,Force values). It might be worth to check its behavior.Gonzalo Matheu

1 Answers

0
votes

You could check if there is a snapshot using list subcommand:

vboxmanage snapshot peer00 list |\ grep "MySnapshot" &&\ vboxmanage snapshot peer00 delete "MySnapShot" ||\ echo "No snapshot"

An alternative is to just ignore delete failure:

vboxmanage snapshot peer00 delete "My Snapshot" ||\ echo "No snapshot"