Cloning and Copying VirtualBox virtual machines

Nov 2, 2008 VirtualBox

This article explains how to clone and copy VirtualBox VMs (virtual machines).

Change History
2018-11-14: Updates.
2013-12-08: Updates.
2011-09-22: Updates.
2008-11-03: Updates.
2008-11-01_: First draft.

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.

Introduction

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:

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.

Virtual Machines

A VirtualBox VM consists of three parts:

  1. One or more virtual hard disk image files (VDI files).
  2. Zero or more Snapshots (also VDI files).
  3. XML configuration files defining the VM (virtual hardware parameters).

Snapshots

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.

Copying a Virtual Machine

Here's an outline of the copy process:

  1. 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.
  2. Make a clone the source VM's disk image file(s).
  3. Create a new VM and attach the cloned disk image file(s).

Here are the details:

Merge source snapshots

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).

  1. If the source VM is running close it.
  2. From the VirtualBox window select the source VM then click the Snapshots tab.
  3. 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.
  4. 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).
  5. 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 Snapshot 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) Current State 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 Current 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.

Clone or copy the source disk image

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.

If you want to create a copy on another host machine you simply copy the disk image to the new host.

  1. If the source VM is running close it.
  2. 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:

  1. From the VirtualBox window select File->Virtual Disk Manager (Ctrl+D) menu command.
  2. Press the Add button and select the copied disk image file.

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):

  1. Open a Command Prompt window on the host machine.
  2. Change to the VirtualBox VDI directory (normally C:\Documents and Settings\<username>\.VirtualBox\VDI).
  3. 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.

Create a new virtual machine

There is no command to copy a VM so we create a new VM and attach our copied disk to it:

  1. From the VirtualBox window select Machine->New... (Ctrl+N) menu command.
  2. 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.

Final Thoughts

Although I have not tried, it should be relatively easy to automate the copying and cloning procedures by scripting the powerful VirtualBox VBoxManage command.

« Previous Next »