Friday, December 11, 2009

Cloning openSUSE VirtualBox Machines

... or "Waiting for device /dev/disk/by-ID/... to appear." problem

A guess many people have figured this out by now, but i still know a lot who have not, so here it goes.

There are a few caveats in cloning openSUSE or SLE VirtualBox machines. One is that openSUSE uses device ID in /etc/fstab and grub's menu.lst to reference your hard disk partitions. This is cool for physical machines, since no matter what the usual device name of your hard disk is (/dev/sda, sdb, hda, ...), the device ID always stays the same. When cloning the VirtualBox hard disk image, however, the device ID changes. This consequently causes a problem when booting your new machine with cloned image.

Another one concerns the network card's udev rules, which affect the device names. These are set to use MAC address to identify the card, which also changes when creating new virtual machine (new random MAC address is generated). After booting the cloned machine with this new network card, the original one becomes eth1, and the new one is not configured; you need to configure it, e.g. using YaST. Thanx to Mišo Žugec for this tip.

To avoid these problems, after installing a fresh openSUSE or SLE which i want to use for cloning, i change its /etc/fstab and /boot/grub/menu.lst to use the usual device files, and change the network card's udev rules from MAC to BusID. So, in nutshell, the cloning process looks like this:
  1. Boot the virtual machine you want to use as base for cloning.
  2. Replace the /dev/disk/by-id/...-partY with /dev/sdXY in /etc/fstab and /boot/grub/menu.lst.
  3. In YaST Network module, click Edit to edit the network card, open the Hardware tab, click Change in the Udev rules frame and select BusID instead of MAC address.
  4. Shut the machine down (from within the machine itself, not using VirtualBox's power off).
  5. Find the machine's .vdi hard disk file (usually under ~/.VirtualBox/ dir) and use VBoxManage clonevdi command to clone it, for example:
    (~/.VirtualBox/HardDisks)$ VBoxManage clonevdi my-perfect-oS11.2-base.vdi oS11.2-some-experiment.vdi
  6. Create new machine in VirtualBox GUI and attach the cloned disk image to it when asked. You can also copy an existing machine's .xml configuration file and change the hard disk UUID, or create a script that will do this for you.
You're ready to boot your clone now. Of course, if you cloned a machine still using the device ID for hard disk, you should still be able to boot it, if you pass the right root=/dev/sdXY and resume=/dev/sdXY parameters to grub. You can also reconfigure the network devices after booting. But preparing the base is more convenient, if you need to clone it often.

For more info see:
http://srackham.wordpress.com/cloning-and-copying-virtualbox-virtual-machines/
http://forgeftp.novell.com/lfl/.html/virtualbox.html

3 comments:

Anonymous said...

You can also use labels, to prevent both /dev/sd? vs. /dev/hd? and /dev/disk/by-ID problems.

Unfortunately you have to add labels to your partitions by hand, using tune2fs, mkswap or other tools, depending on filesystem type.

Then, in YaST's Partitioner module, edit all mounted partitions and in 'fstab options' select 'Mount by Volume label' (you can also set it to 'Mount by Device name' here to achieve what Jano describes).

Using volume labels is just a tiny bit more flexible, because even if for some reason /dev/sda becomes /dev/sdb or whatever, the correct partition will still be available in /dev/disk/by-label/[volume-label].

Then, in the Boot Loader module in YaST, ask it to 'Propose new configuration' under Other. It will fix the menu and entries in /boot/grub/device.map and install the new menu.

BTW, Jano you forgot to mention device.map and installing grub to actually apply the changes.

Michele C. said...

Very usefull, thank you very much

mz said...

Thank you Jano !
Excellent post !!!