Cloning and Copying VirtualBox virtual machines

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.


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:

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


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

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


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.

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.

  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.

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

  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>

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.

http://srackham.wordpress.com/cloning-and-copying-virtualbox-virtual-machines/

2 thoughts on “Cloning and Copying VirtualBox virtual machines

  1. I doubt there is a grocery store that uses
    handwritten bills or a business of any size that uses paper ledgers for their accounting anymore.
    Increasingly, firms are offering services whereby you can refill your cartridges rather than buy new
    ones. To expand the memory of your color laser printer you
    usually have to buy a small RAM board.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s