Skip to main content

Cannot unregister the machine 'foo' because it has 1 snapshots

So, I dabbled with this VirtualBox VM a bit but the guest OS was broken anyway so I decided to get rid of the VM:

$ VBoxManage unregistervm foo --delete
ERROR: Cannot unregister the machine 'foo' because it has 1 snapshots
Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component 
            Machine, interface IMachine, callee nsISupports
Context: "UnregisterMachine(uuid, machine.asOutParam())" at line 164 
              of file VBoxManageMisc.cpp
Oh, right. The VM in question had a snapshop attached to it as well, so let's delete it too:
$ VBoxManage showvminfo foo | less
[...]
Snapshots:
   Name: snap1 (UUID: 8e2914fa-de59-4842-9391-3afac42e0125)

$ VBoxManage snapshot foo delete 8e2914fa-de59-4842-9391-3afac42e0125
0%...FAILED
Error: snapshot operation failed. Error message: Hard disk '../deb0.vdi' has 
       more than one child hard disk (2)
Hm, I remember now, the VM was using another VM's disk. Kinda weird setup, no wonder I wanted to get rid of it :-) So let's find out which VM also uses "deb0.vdi":
$ VBoxManage list hdds
[...]
UUID:        03b1afd9-4ce4-4e42-9347-226b55cba657
Parent UUID: base
Format:      VDI
Location:    ../deb0.vdi
State:       created
Type:        normal
Usage:       foo (UUID: 3c57773a-de6a-4714-9149-407a98f85ae7)
          [snap1 (UUID: 8e2914fa-de59-4842-9391-3afac42e0125)]

UUID:        bc3b45a9-db44-41a4-822d-52987f2734c8
Parent UUID: 03b1afd9-4ce4-4e42-9347-226b55cba657
Format:      VDI
Location:    ../foo/Snapshots/{bc3b45a9-db44-41a4-822d-52987f2734c8}.vdi
State:       created
Type:        normal

UUID:        06280b86-8389-40c4-8bfb-3562a3e206df
Parent UUID: 03b1afd9-4ce4-4e42-9347-226b55cba657
Format:      VDI
Location:    ../debian/Snapshots/{06280b86-8389-40c4-8bfb-3562a3e206df}.vdi
State:       created
Type:        normal
Usage:       debian (UUID: bdbf5c46-aefe-4004-acea-ac521eaedb2e)
So, "deb0.vdi" was used by VM foo and debian an also by a snapshot. Maybe I could just edit VirtualBox.xml manually, but that wouldn't be that much fun, right? So let's detach 06280b86-8389-40c4-8bfb-3562a3e206df from debian, then we should be able to delete the snapshot, right?
$ VBoxManage showvminfo debian | grep 06280b86-8389-40c4-8bfb-3562a3e206df
SATA Controller (1, 0): ../debian/Snapshots/{06280b86-8389-40c4-8bfb-3562a3e206df}.vdi
                                      (UUID: 06280b86-8389-40c4-8bfb-3562a3e206df)

$ VBoxManage storageattach debian --storagectl "SATA Controller" \
                                  --port 1 --device 0 --medium none


$ VBoxManage snapshot foo delete 8e2914fa-de59-4842-9391-3afac42e0125
0%...FAILED
Error: snapshot operation failed. Error message: Hard disk '../deb0.vdi' has
       more than one child hard disk (2)
Huh? OK, I say it again: the VM's setup was seriously braindamaged, so maybe VirtualBox got a little confused. A somehow related ticket suggested to revert the snapshot to a current state. Did that and went on to detach the disks from the VM but now things got a bit out of hand:
$ VBoxManage snapshot 3c57773a-de6a-4714-9149-407a98f85ae7 restorecurrent
$ VBoxManage showvminfo foo | grep SCSI

SCSI Controller (0, 0): ../foo/Snapshots/{95000117-e5e0-46dc-bbbd-4929afd9b88c}.vdi 
                                   (UUID: 95000117-e5e0-46dc-bbbd-4929afd9b88c)
SCSI Controller (1, 0): ../foo/Snapshots/{f209322c-b784-4ec3-b0af-e0374444b349}.vdi 
                                   (UUID: f209322c-b784-4ec3-b0af-e0374444b349)

$ VBoxManage storageattach foo --storagectl "SCSI Controller" \
                               --port 0 --device 0 --medium none
$ VBoxManage storageattach foo --storagectl "SCSI Controller" \
                               --port 1 --device 0 --medium none

$ VBoxManage showvminfo foo | grep ^SCSI
SCSI Controller (1, 0): ../foo/Snapshots/{f209322c-b784-4ec3-b0af-e0374444b349}.vdi 
                                   (UUID: f209322c-b784-4ec3-b0af-e0374444b349)
Huh? I just detached the disk from the VM, how comes it's still attached? Shortly after, both disks were attached to the SCSI controller again. This did not look right, so I felt like cheating a bit:
$ pkill VBoxSVC
$ mv ../Machines/foo/Snapshots/* ~/trash/
Meanwhile, I had 4 (!) disks referring to 03b1afd9-4ce4-4e42-9347-226b55cba657 now, I wonder why:
$ VBoxManage list hdds | egrep '^(UUID|Parent)'
[...]
UUID:        03b1afd9-4ce4-4e42-9347-226b55cba657
Parent UUID: base

UUID:        bc3b45a9-db44-41a4-822d-52987f2734c8
Parent UUID: 03b1afd9-4ce4-4e42-9347-226b55cba657

UUID:        06280b86-8389-40c4-8bfb-3562a3e206df
Parent UUID: 03b1afd9-4ce4-4e42-9347-226b55cba657

UUID:        95000117-e5e0-46dc-bbbd-4929afd9b88c
Parent UUID: 03b1afd9-4ce4-4e42-9347-226b55cba657

$ VBoxManage closemedium disk bc3b45a9-db44-41a4-822d-52987f2734c8
$ VBoxManage closemedium disk 06280b86-8389-40c4-8bfb-3562a3e206df
$ VBoxManage closemedium disk 95000117-e5e0-46dc-bbbd-4929afd9b88c
Also, I removed all the storage controllers attached to VM foo:
$ VBoxManage storagectl foo --name "IDE Controller"  --remove
$ VBoxManage storagectl foo --name "SATA Controller" --remove
$ VBoxManage storagectl foo --name "SCSI Controller" --remove
$ VBoxManage storagectl foo --name "SAS Controller"  --remove
This did the trick, apparently:
$ VBoxManage snapshot foo delete 8e2914fa-de59-4842-9391-3afac42e0125
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

$ VBoxManage unregistervm foo --delete
Re-attaching the disks to the other VM, and we're done:
$ VBoxManage storageattach debian --storagectl "SATA Controller" \
                 --port 0 --device 0 --type hdd --medium ../debian/deb0.vdi 
$ VBoxManage storageattach debian --storagectl "SATA Controller" \
                 --port 1 --device 0 --type hdd --medium ../debian/deb1.vdi