Updated: October 28, 2009
If you're using virtualization, either for business or pleasure, you may have come across VirtualBox, a very powerful, highly versatile free solution that allows desktop users enormous flexibility when it comes to deploying operating systems, any which way.
And if you're more than just a hobbyist enjoying virtualization for the sake of geekiness, or an aspiring software enthusiast learning his/her way around new operating systems by the virtue of virtualization, you are going to encounter the following scenario: you need to deploy a large number of virtual machines.
You don't have time to fiddle with individual installations. Indeed, manually configuring machines one by one is the worst thing you can do when massively deploying an install base. It smacks of bad habits, it's slow, it's inefficient, and you will most likely end up with a number of different setups, leaving your environment in an inconsistent state.
To overcome this issue, you require a bit-by-bit solution that will replicate your basic installation image to a desired number of copies. This is called cloning. VirtualBox provides this solution. Let's see how it's done.
There are several ways you can go about cloning.
You can manually copy the hard disk where the installed image resides. Virtual machine hard disks are files, so it's a fairly simple deal. I've demonstrated this concept some time ago, albeit with other virtualization products: VMware Server and VMware ESXi.
You can also use a dedicated imaging software, like perhaps CloneZilla. You boot into the live environment, image the disks/partitions and then restore the image to an empty disk in the second virtual machine, creating an exact copy.
We've also learned how to use the VMware Converter to convert our images back and forth between desired formats, while retaining the basic image. VirtualBox offers yet another method.
Cloning in VirtualBox
VirtualBox contains a built-in utility for disk management called VBoxManage. This utility is available on both Windows and Linux VirtualBox installations. In Windows, you will find it inside the VirtualBox directory. In Linux, it will be added to the execution path.
VBoxManage can do so much more than clone. To see a staggering list of options, execute either VBoxManage /? in Windows or VBoxManage --help on Linux.
VBoxManage cloning syntax is as follows:
VBoxManage clonehd <old> <new> --format VDI
What we have here?
hdclone tells us the operation we want to perform.
<old> and <new> are the names of hard disks. For example, ubuntu-9.04.vdi is our original and ubuntu-9.04-cloned.vdi is going to be our clone.
--format VDI tells us the format we want our disk to be. Like QEMU, VirtualBox has the ability to work with different virtual hard disk formats, which makes it compatible with other virtualization products on the market.
That's it basically. Just run the command and watch for progress in the command line window, either on Windows or Linux.
After a while, you will have your clone available:
You can now attach your new disk to any virtual machine and boot an exact copy of your existing setup. Believe it or not, that's the whole magic!
It is also possible to perform an ordinary copy with VirtualBox, just like VMware. However, some users have reported that the plain file copy sometimes corrupts the target file. Personally, I've not had any issues using this, but if you do encounter problems, you may want to try this trick:
Tar/rar/zip the image before copying it. This seems to work in those cases where the ordinary file copy is not good enough. The best way to make sure your copy has succeeded is to check the MD5 sum of the target file and compare it to the original. And of course, you should boot your virtual machine from the new disk, to make sure it works as expected.
This is a very short, but useful tutorial. We have now learned how to use VBoxManage to improve and automate the administration of our environment. Since this utility runs from the command line, it is possible to use it in scripts and schedule cloning, backups, etc.
In the next article, we will learn how to add disks to virtual machines. In the sequel after that, we will expand/shrink virtual disks. In the fourth part on VirtualBox management, we will learn different ways of configuring networking and sharing.