Quickest way to merge snapshots in VirtualBox?

50

21

I have vdi with quite a number of snapshots taken from it - say, 50 or 70 snapshots. I want to have one plain vdi with a current state. These snapshots take way too much disk space and are not needed as snapshots.

I know that if I call

VBoxManage clonehd thedisk.vdi thedisk-full.vdi
(50-70 times) VBoxManage clonehd {uuid-of-snapshot}.vdi thedisk-full.vdi --existing

I will get what I want. But it takes very very long time (more than 5 hours given my hardware and 16G initial disk size).

And, of course, I tried VBoxManage snapshot delete and it is strange: doesn't work on stopped machine, can fail on running machine and when it fails once, it will fail all subsequent calls.

Is there a way to make plain vdi from a number of snapshots - quicker?

$ VBoxManage --version
3.2.14r75509

(asked it on ServerFault but decided this place is more appropriate for this question)

Andrey Regentov

Posted 2013-05-03T09:43:52.543

Reputation: 1 196

The command your using is the command you want to use there isn't a faster way. – Ramhound – 2013-05-03T10:51:54.603

What about exporting the whole VM? Will its vdi contain what i want? – Andrey Regentov – 2013-05-03T11:16:18.510

You should try it. In the end any other method is still going to take awhile because of the size of the Virtual Machine. – Ramhound – 2013-05-03T11:25:08.970

Answers

54

Found the solution. It is as simple as the last step from the commands I wrote here. I.e.

VBoxManage clonehd fullpath/{uuid-of-last-snapshot}.vdi thedisk-full.vdi

So I should clonehd only the last snapshot, not every snapshot from the chain. And it is thousands percent faster.

The uuid can be found from VBoxManage list hdds | grep VmName | tail

In the GUI (on Windows), select the VM, select Snapshots from the top right, right click on the snapshot you want, select Clone..., and select Full Clone in the dialog.

Andrey Regentov

Posted 2013-05-03T09:43:52.543

Reputation: 1 196

The fullpath needs to be the one that is registered in VirtualBox, of course. Symbolic links wont work, since VBox needs to look up all Snapshots – Andy – 2014-10-26T20:47:38.740

not sure what does the tail do, do you mean "tail -1"? – eckes – 2015-10-22T15:12:22.120

1In VirtualBox 5, clonehd is deprecated and left for backward compatibility, use clonemedium instead. – George Sovetov – 2018-12-27T13:57:49.170

thx but it doesn't work on my setup, it says the image would already exist. Did you forget the --existing? – Andy – 2014-04-15T15:07:13.527

3if you already have thedisk-full.vdi file on disk then please first change the name in command to something like thedisk-full-new.vdi. – Andrey Regentov – 2014-04-16T02:36:51.450

22

Here's a technique (tested on VirtualBox 4.3.4) that uses the 'Clone' command to merge and compact all the snapshots to a new VM containing a single VDI file:

  1. Rename the VM so you can reuse its current name for cloned VM (you can change the VM name from the 'Basic' tab of the 'General' settings).

  2. Right-click on the VM and run the 'Clone...' command:

    • Set the name to the VM's original name.
    • Accept the remaining defaults i.e. Clone type: 'Full clone'; Snapshots: 'Current machine state'.
  3. Delete the old VM (right-click on the VM and run the 'Remove...' command).

Stuart Rackham

Posted 2013-05-03T09:43:52.543

Reputation: 341

As of VirtualBox 6.0.8, this still works. – Paul Sturm – 2019-08-29T15:11:39.540

3

You can try to export to OVF and then import back the generated file.

As explained here:

OVF cannot describe snapshots that were taken for a virtual machine. As a result, when you export a virtual machine that has snapshots, only the current state of the machine will be exported, and the disk images in the export will have a "flattened" state identical to the current state of the virtual machine.

As result you will have a cloned VM with the current state without other snapshots.

StreakyCobra

Posted 2013-05-03T09:43:52.543

Reputation: 411

I ask about vdis because cannot export machine - it was broken (inaccessible state after host reboot), and all that I had was the bunch of VDIs - disk and about fifty snapshots. – Andrey Regentov – 2013-05-10T05:03:44.233

0

That operation means reading and writing very large files in the same physical disk. The only way you can speed that up is to put those files in different disks changing the paths in your VBoxManage command. If the machine has only one disk, you could use an external USB drive or even a disk in another machine via a network share (depending on the speed of your network)

golimar

Posted 2013-05-03T09:43:52.543

Reputation: 846

1Typically network is orders of magnitude slower than internal HDD interface, so this probably wouldn't make the process faster. – Andrey Regentov – 2013-05-08T10:55:49.620

-3

Some have 10Gib/s LAN, so it can transfer more than one gigabyte per second, so it will not be a bottleneck.

Some have 1Gib/s LAN, so it can transfer more than more than 100 megabytes per second, so it will not be a bottleneck (unless you have a really fast SSD).

Mechanical disks, when read & write on same disk at same time (or much fragmented), go down for near 20 megabytes per second, while on non fragmented only read or only write can go to more than 100 megabytes per second.

You tell about LAN be slower, but not about USB (2.0, not to mention 1.1 or 1.0) is much times slower.

What i would suggest would be: Use USB 3.1 Gen2 Type C or a eSATA / SATA / SAS

But allways: Use a second fast enough disk.

And allways: Cloning to another disk and the copy back the file is much faster that using only one of the disk unless it is a SSD or a hard disk with independent multi-head arms (they can read and write at the same time on two surface places, each head has its own arm, very expensive, about >9876€).

The best is to clone only the virtual hard disk of the state you want, then check it works, then RESTORE top level (will delete all the snapshots), then remove the HDD on storage and connect that new one.

So steps, to loose all old states on a no tree snapshots scheme:

  1. Clone from one hardisk to another one, avoid bottlenecks
  2. Create a new machine, connect that disk, disconnect it, configure it as inmutable
  3. Test it works (since it is inmutable) you will have the same state
  4. If you need it not be inmutable, disconnect it, change back to normal, etc
  5. Delete the old machine snapshots (RESTORE first)
  6. Delete the temporal test machine
  7. Copy back to the disk the virtual disk
  8. Attach the cloned disk

But allways have in mind, snapshots can be like a full tree, one node can have more than one child, each child can have more than one child, etc.

To work with trees of snoapshots, allways think on DELETE as deleting a node without affeting the rest of nodes (only loose that state, but not there rest, no parent and no child will be affeted, data will be merged on all the first sub level childs, so slow process) and on RESTORE as destroying all of its childs on all sub levels (loosing all states after that one you restore, it only deletes the files, so really fast process, just seconds).

That is!

P.D.: How horrible names VBOX gave to that two operations (delete just one node = DELETE; delete all child nodes on all sub levels = RESTORE)

Claudio

Posted 2013-05-03T09:43:52.543

Reputation: 1

Restore does not delete any snapshots, just the current (unsaved) state. – Melebius – 2017-04-25T10:08:04.190