Cloning and Copying VirtualBox virtual machines
2008-11-01: First draft.
This article explains how to clone and copy VirtualBox VMs (virtual machines).
Update September 2011
VirtualBox has come along since I fist posted this article back in 2008 (Version 2.0.4) — the latest version of VirtualBox (4.1.2) adds a Clone command which largely renders the techniques described below redundant and hugely simplifies matters (although they will still be useful if you are interested in fine-grain snapshot management).
Here’s the procedure to merge and compact all the snapshots to a new VM containing a a single VDI file:
Problems with 64-bit Windows 7 Guests with .vdmk disks
This note is off-topic but it may be of help to anyone experiencing this problem (sorry, I don’t have a solution, apart from don’t put 64-bit Windows VMs into .ova files).
After importing a .ova (Virtual Applicance) that had been previously exported from VirtualBox I experienced Windows chkdsk errors after creating a snapshot. The chkdsk problems seemed to change with each chkdsk, the problems seemed to snowball. Here are the circumstances under which I was able replicate this problem (I didn’t try any other scenarios):
The techniques described in this article were tested on a Windows XP Pro SP2 host operating system using VirtualBox version 2.0.4. The details may vary if you use a Linux or some other host OS.
Cloning and copying VirtualBox VMs is not as straight forward or obvious as it is with VMWare Desktop and this subject has created a lot of confusion in the support forums. This is because:
- There is no single VirtualBox command for VM cloning.
- The documentation is vague.
- A couple of the VirtualBox commands are poorly named.
The good news is that the underlying technology is just great and with a little understanding copying and cloning VMs is quite easy.
This article explains how to copy an existing VM containing an installed and configured guest OS. Before starting it helps to have a basic understanding of VirtualBox virtual machines.
A VirtualBox VM consists of three parts:
- One or more virtual hard disk image files (VDI files).
- Zero or more Snapshots (also VDI files).
- XML configuration files defining the VM (virtual hardware parameters).
- A snapshot bookmarks the state of the VM (specifically the VM file system) at the point in time the snapshot is made.
- You return to a previous snapshot by deleting the current state and all newer snapshots.
- When you create a snapshot, changes to the file system accumulate in the current snapshot file, older snapshot files and disk image files are effectively read-only.
- If there are no snapshots the VM’s disk image file accumulates file system changes just like normal disks — the VM’s Current State is merged into the disk image automatically when you close the VM.
With respect to copying the important thing to understand about snapshots is that there is no easy way to copy them, so if your source VM has any snapshot information that you want to copy you first need to merge the snapshot(s) into the source VM’s disk images.
Here’s an outline of the copy process:
- If the source VM has any snapshot information that you want to copy you first need to merge the snapshot(s) into the source VM’s disk image files.
- Make a clone the source VM’s disk image file(s).
- Create a new VM and attach the cloned disk image file(s).
Here are the details:
Here’s how to merge the source snapshots to the source disk image VDI file (skip this step if you source VM doesn’t have any snapshots).
- If the source VM is running close it.
- From the VirtualBox window select the source VM then click the Snapshots tab.
- If you want to include the current state of the disk in the merge save it as a snapshot by selecting the Current State then executing the Take Snapshot (Ctrl+Shift+S) command.
- Select the most recent snapshot you want to include in the target VM and execute the Discard Snapshot (Ctrl+Shift+D) command. This merges the snapshot file into it’s parent VDI file (snapshot or base disk).
- Repeat the previous step, working your way down the snapshot list until the oldest snapshot has been merged into the base VDI.
The VirtualBox snapshot commands are poorly named, in particular the inconsistent and confusing use of the word Discard:
|Command||Should be named||Applied to||Description|
Discard Snapshot (Ctrl+Shift+D)
Merge with Parent
Merges the snapshot file into the parent snapshot or disk image file and then deletes the now redundant snapshot file.
Revert to Current Snapshot (Ctrl+Shift+R)
Deletes the current state returning the VM state to the most recent snapshot.
Discard Current Snapshot and State (Ctrl+Shift+B)
Delete Current Snapshot and State
Deletes the current state and the most recent snapshot returning the VM state to that of the penultimate snapshot. Use this command to delete snapshots.
At this point the entire state of the source VM that we want to copy is contained in the disk image VDI file.
To avoid confusion I use the term copying to refer to copying a VM to different host machine and the term cloning to refer to creating a copy on the same host.
Copying to another host machine
If you want to create a copy on another host machine you simply copy the disk image to the new host.
- If the source VM is running close it.
- Copy the source disk image to the VirtualBox VDI directory on the target host machine. You’ll normally find the source disk image in the VirtualBox VDI directory (the standard VDI location on Windows hosts is C:\Documents and Settings\<username>\.VirtualBox\VDI).
Once you’ve copied the disk image you need to register it with VirtualBox on the target host machine:
- From the VirtualBox window select File→Virtual Disk Manager (Ctrl+D) menu command.
- Press the Add button and select the copied disk image file.
Creating a clone on the same host machine
Use the VBoxManage command-line utility the clonevdi command option to clone and register the disk image (you can’t just copy the disk image to the same host machine because the disk UUID will conflict with the source disk image):
- Open a Command Prompt window on the host machine.
- Change to the VirtualBox VDI directory (normally C:\Documents and Settings\<username>\.VirtualBox\VDI).
- Now run the VBoxManage command with the clonevdi command option. The following example transcript shows the creation of a copy of Ubuntu 8.04.vdi named Ubuntu 8.04 clone.vdi and registers the new disk:
C:\Documents and Settings\srackham\.VirtualBox\VDI> "C:\Program Files\Sun\xVM VirtualBox\VBoxManage.exe" clonevdi "Ubuntu 8.04.vdi" "Ubuntu 8.04 clone.vdi" VirtualBox Command Line Management Interface Version 2.0.4 (C) 2005-2008 Sun Microsystems, Inc. All rights reserved. 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% C:\Documents and Settings\srackham\.VirtualBox\VDI>
My experience is that merging snapshots into a single base VDI saves a lot of space (a Windows XP guest with 5 snapshots shrank from 8.6GB to 3.7GB) but defragging the drive and then running the VBoxManage modifyvdi compact command didn’t appear to regain any disk space in either Windows XP or Vista.
There is no command to copy a VM so we create a new VM and attach our copied disk to it:
- From the VirtualBox window select Machine→New… (Ctrl+N) menu command.
- Work your way through the Create New Virtual Machine wizard till you get to the Virtual Hard Disk page then press the Existing… (not the New…) button and select the disk image VDI file that you previously cloned or copied.
That’s it, you should now be able to start and run your new VM.
|Try not to be tempted to edit the VirtualBox XML configuration files manually — the files are interrelated and making changes manually is not as easy as it looks (I’m talking from experience here) — best to use the VirtualBox commands instead.|
Although I have not tried, it should be relatively easy to automate the copying and cloning procedures by scripting the powerful VirtualBox VBoxManage command.