Nov 10, 2008

BbOoOoTt: Parallel Dual Boot

On my laptop I have a work partition with a stable release and a test partition with a development release. Here I want to show you how I set it up to work with both of them at once, using VirtualBox.

The idea is that if virtualization enables you to run an additional machine on an additional (virtual) disk represented by an image file, it should also be able to operate on the actual disk and in this way convert dual booting from serialized to parallelized. And indeed VirtualBox can do it and the documentation is in its manual, chapters 9.9 "Using a raw host hard disk from a guest" and 9.9.2 "Access to individual physical hard disk partitions".


Caution: accessing one file system from two machines at once is a sure way to corrupt it and lose data. VB gives you some protection but you still have to be careful. One thing, you must not share a swap partition between the two systems. Also, make sure that you do not have the other system's partition mounted, especially as a "quickie" on /mnt.

Setting up

Change this according to your needs:
Give yourself access rights to the other system's partitions (LOGNAME is preset by bash):
sudo setfacl -m u:$LOGNAME:r $DISK
sudo setfacl -m u:$LOGNAME:rw $DISK$SWAP $DISK$ROOT
Copy the bootloader of the other system to a file:
dd if=$DISK$ROOT of=/tmp/boot.bin bs=512 count=1
Create a virtual disk to refer to the other system's partitions:
VBoxManage internalcommands createrawvmdk -register \
-filename ~/.VirtualBox/VDI/other-system.vmdk \
-rawdisk $DISK -partitions $SWAP,$ROOT \
-relative -mbr /tmp/boot.bin
That's quite a mouthful so let's go over the options:
  • register is a convenience setting that attaches the created disk to the VB disk manager.
  • filename is the virtual disk being created and it must be an absolute path.
  • rawdisk is the device of the whole target disk.
  • partitions restricts the access to these partitions. Other partitions will be visible but appear to contain only zeroes.
  • relative means that we will be able to drop access rights to the whole disk (see the next command) and retain only those to the used partitions
  • mbr specifies bootloader code to be used (partition info is still taken from the real MBR). That is important since in my case the real MBR references GRUB's menu.lst from sda1. That partition will appear as empty and GRUB would fail with "Error 17". So I give VB the boot record of the other system which references menu.lst on its own, visible, partition.
Then give up rights we no longer need:
sudo setfacl -x u:$LOGNAME $DISK
Now create a new machine in VB and attach the vmdk to it.
(there is a CLI way but I just clicked in the GUI)

Running it

Make sure that the other partitions are not used: (Hmm, VB should do that by itself. Must file a bug for that...)
sudo swapoff $DISK$SWAP
sudo umount $DISK$ROOT
Click Play and have a lot of fun.

If you used the defaults when partitioning, you will not get very far because the initrd will not find the disk. The bootloader refers to it by its ID but it is no longer /dev/disk/by-id/ata-TOSHIBA_MK8032GAX_Y6EAT0PKT-part6 but rather .../ata-VBOX_HARDDISK_VB22889591-0c34ac94-part6. The workaround is to edit the GRUB menu on the fly (ESC, Enter, (E)dit, ..., (B)oot) and to replace the long strings by the short names like /dev/hda6. The fix is to use paths from /dev/disk/by-uuid.