This article explains how to clone and copy VirtualBox VMs (virtual machines).
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.
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>
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.
Warning: 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.