2. Cloning Virtual Machines


Assuming you've already prepared the machine to run as a VM, you will want to do a few preparatory steps, such as to:
  • Collapse any vm snap-shots or disk deltas you have.
  • Generalize the system with sysprep (for windows)
  • Execute the clone commands
Zeroing out the hard drive space is not needed, as you're not exporting the disk or otherwise compressing it (usually)


Images and Deltas

Let's just say if you clone something you've been taking delta's against, you get unpredictable results. Make sure to apply or discard any you have. If you feel adventurous, you can run in 'delta' mode and never make changes to the original disk and still clone. I just don't know how that behaves.


This is not strictly nessesary, but if will actually save you time by automating the reconfiguring of the clone. Plus, without this step you will have two machines fighting over the same SID and license and that's a problem in a domain environment.
SysPrep is the tool of choice, and you need a SysPrep config file. I've attached a sample one below.  Assuming you've saved it to your desktop, you issue the command like so.
      Sample SysPrep config file  (download to someplace handy, with no spaces in the path)
c:\Windows\System32\sysprep\sysprep.exe /oobe /generalize /unattend:C:\Unattended.xml
For some linux systems, you can set a file that flags the system for reconfiguration. Place the empty file at the root like so;
> touch  /.unconfigured  
See rc.sysinit for details.  You may find this doesn't work or that you also run into issues with disk IDs, depending on your distro. See the notes section.


There is no GUI built in facility to clone a VM in Virtualbox (or other VM products that I know of at the moment), so use a combo method.
  1. Use the command line tool to copy the disk image
  2. Use the GUI to create a new machine and register that disk image with it
It's important to use the VBoxManage utility, rather than a straight-up file copy, because the disk image is not a normal file. The utility is smart enough to accommodate disk image specific things, such as speeding up by not copying empty areas of the file, and dealing with sparse images, etc. We are also using the tool to add it to the Media Manager.
cd ~/Library/VirtualBox/HardDisks  # this will depend on where your HardDisks are at
VBoxManage clonehd <filename> <outputfile>
VBoxManage openmedium disk <outputfile>
Now that you've clone the disk, jump into the GUI and create a new machine and use this disk when you come to that section. It possible to create a new machine at the command line. But the GUI is easier if you're not trying to automate it.


Generalizing the System:
The best way is use SysPrep, as it does other useful things like rename and reset user IDs. 
  • Download the sample sysprep.xml file (linked from the sysprep page)
  • Edit the file so it has your info in it
  • Save it somewhere handy for reuse, and if that wasn't a network share, save it on the local system as well
  • Execute this command (sysprep comes with windows now, for older versions you are on your own) 
A good sysprep file won't ask you for any information, remove the /oobe if you like to improve this process. You can also rename the computer by had too, and this is sometimes faster. 
When to create a new SID:
If you want to be able to reliably join a domain or use a local windows update server, you should consider generating a new SID. This post has some more information. 
SysPrep will do this when the /generalize option is used as above. You can also use (I've read) the sysinternals tool newsid
Why you need to use the clonehd command:
The clonehd command gives the image a new uuid and registers it with the virtual media manager. VBox uses the uuid to keep track of disks and you cannot have two the same. If a uuid change causes your linux box not to boot - you have to refer to the below snippet.
When to compact the disk
If you have an 'extend as you go' harddisk file, you have been extending it as you write to it, but not necessarily reducing it as things have been deleted. If you are going to do several clones, you will want to compact the disk.
In the host, zero out the free space and compact as described in the Creating an Appliance 



Supply some handy kernel parameters

Editing the /boot/grub/grub.conf and change the line that says:
kernel /vmlinuz-2.6.18-164.11.1.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet
kernel /vmlinuz-2.6.18-164.11.1.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet divider=10 notsc iommu=soft elevator=noop
(source here)

Set the box to reconfigure

 Note that newer Linux distributions identify the boot hard disk from the ID of the drive. The ID VirtualBox reports for a drive is determined from the UUID of the virtual disk image. So if you clone a disk image and try to boot the copied image the guest might not be able to determine its own boot disk as the UUID changed. In this case you have to adapt the disk ID in your boot loader script (for example/boot/grub/menu.lst). The disk ID looks like this:

The ID for the copied image can be determined with

hdparm -i /dev/sda