Can a hypervisor like VirtualBox be used to launch a virtualized OS which is also directly bootable?

This question is rather theoretical:

Suppose I have a PC with two different OS on two different partitions: OS1 on sda1 and OS2 on sda2. Is it somehow possible to set up a program like VMWare or Virtualbox in OS1, which accesses sda2, boots it and runs OS2? On the other hand OS2 should be bootable directly, as well.

I realize that the emulated OS2 would be an awful lot slower and probably noone would really use this setup, but I find this very interesting.


Solution 1:

It's possible, yes, and it doesn't necessarily have to be slower, since these days it's not emulation anymore, it's virtualization – most CPUs support hardware-assisted virtualization (VT-x and such), but even without those, VirtualBox has many tricks for making software-virtualized VMs run fast.

In any case it won't be any slower than booting up a plain old VM. It might even be faster (due to using a raw partition instead of a fragmented disk image).


But I'm not entirely sure about how to do it in VirtualBox – I think you must create a special .vdi file using vboxmanage that points to the real partition? Also, I've no ideas about how to make the bootloader work in such a situation.

Though I used to debug various boot issues on Linux by running a read-only copy of the current OS within itself, using qemu-system-x86_64 -enable-kvm -hda /dev/sda -snapshot -monitor stdio.

Solution 2:

qemu will let you use block devices like /dev/sda2, etc. as virtual hard drives. VMWare does as well. Not sure about Virtualbox.

When you boot /dev/sda2 in a VM, it will have a similar effect as though you took a hard drive out and booted it in a different computer with different hardware. If the OS can handle that change, then you should be OK running the OS both in a VM and natively. Windows will give you problems, of course, and Linux will likely tolerate it very well.

You can mitigate potential problems by making the virtual hardware match as closely as the physical hardware on your system as possible - though of course you'll have to specify less RAM. Of particular importance is specifying the same type of disk controller (IDE, SCSI, etc.).