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

60

22

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.

Wauzl

Posted 2015-10-26T12:46:12.613

Reputation: 751

No. Currently, you need to install a guest OS on a VM using an image. If you want to run one of your OS setups on a VM, you can create an image of your exact system setup. There are instructions online on how to do this. – MorrisIriga – 2015-10-26T12:50:45.563

1@MorrisIriga are you sure? I remember that there was a virtualisation tool that was able to mount actual harddisks. Not sure if its oracle virtualbox or microsoft virtualpc. – LPChip – 2015-10-26T12:54:29.637

1

@LPChip Sorry, I had misunderstood the question. Yes, you can mount a physical hard disk using VirtualBox. Have a look at this or this. I don't know about other virtualization software.

– MorrisIriga – 2015-10-26T13:01:56.357

1As a small side thought (to answer the question I thought this was) I saw a proof of concept rootkit a while back that would emulate the underlying hardware, thus becoming undetectable i.e. if you had a nvidia graphics card, it would emulate that into the OS, the same with all the other hardware. – Michael B – 2015-10-26T13:23:31.710

@MichaelB: For graphics cards, many hypervisors already support PCI passthrough as a feature. I guess it's the more obscure hardware (like internal chipsets and controllers) that needs to be sneakily emulated. – user1686 – 2015-10-26T13:55:51.103

In fact, it may be inescapable.

– orome – 2015-10-26T16:51:55.530

4

VMWare Fusion can launch a "Boot Camp" partition as a virtual machine inside the Mac OS X host: http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1014618

– Jacob Krall – 2015-10-26T23:28:11.057

Parallels can also do this. – geometrikal – 2015-10-27T05:18:42.283

I once did this with Virtualbox, but I cannot remember how. I think it's something like http://superuser.com/q/495025/190623 . Note: 1. Windows activation was invalidated, and after I re-activated it in VM, it became invalidated again when I reboot to sda2 physically. 2. You may want to unmount sda2 on host OS. I've corrupted sda2 several times before realizing this.

– jingyu9575 – 2015-10-27T12:38:23.947

1Are you talking about Winception? – Prinsig – 2015-10-27T13:41:06.713

I don't think the title "Can a PC emulate itself?" describes this question at all! – mattdm – 2015-10-28T06:46:31.383

1So what you're saying is... you could have your OS running inside your OS, on the same physical hard drive? I gotta try that. – Thomas – 2015-10-28T11:38:18.977

This reminds me of QEMU-Puppy (http://www.erikveen.dds.nl/qemupuppy/), a version of Puppy Linux specifically designed to run off of a bootable USB drive or to be bootable from QEMU running on the Windows or Linux host.

– Kevin – 2015-10-28T15:36:17.770

Isn't this the whole point of QubesOS ? – Racheet – 2015-10-28T17:26:45.337

Answers

58

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.

user1686

Posted 2015-10-26T12:46:12.613

Reputation: 283 655

6You man you actually ran a OS virtualized inside itself? – Wauzl – 2015-10-26T13:04:49.437

8Sure, why not? It is not very different from running a regular VM. The only important part is -snapshot, which makes a temporary copy of /dev/sda (so that both systems won't clobber each other's data). – user1686 – 2015-10-26T13:07:55.097

6Your tentative statement on using vboxmanage so setup a VDI (not VHD) as placeholder for the raw partition is correct. The VBOX manual explains it in detail. I like your debugging trick. Never though about doing it like that but I'm already thinking of some use-cases ;-) – Tonny – 2015-10-26T13:35:04.500

28

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.).

LawrenceC

Posted 2015-10-26T12:46:12.613

Reputation: 63 487

8

Apparently virtualbox can do this also using the 'VBoxManage internalcommands createrawvmdk' comand (seen here )

– Michael B – 2015-10-26T13:35:32.030

19

It is completely possible, in fact, I used to do this!

I had one partition / disk (I forget which) with Windows one with Linux, I think it was in the days of Windows XP (admittedly, that did last for a while). It was a good way of having access to both sets of data without using a load of disk space with different installations.

The only issue I remember having was that I needed to set up two different hardware profiles in XP. It got really confused when it booted into the wrong ones.

I'm not sure how current activation processes would respond to the switching of hardware. That could well cause some issues... (but if you only ever boot Windows as a physical machine, and have Linux as a VM / or physical, that would solve that)

As I recall I did this with VMware workstation. That definitely supports booting from a raw disk. I'm sure the MS version does. I'm not so sure about the Linux VMs, but I'd be very surprised if not.

Michael B

Posted 2015-10-26T12:46:12.613

Reputation: 714

Oh, hardware profiles... I have never actually touched that feature. Licensing would definitely ruin it now. – sinni800 – 2015-10-26T13:27:30.770

@sinni800 This was my only time ever using them, according to this they still exist, but have moved to the driver kit as devconx64.exe

– Michael B – 2015-10-26T13:31:24.613

Hardware profiles were stripped from Vista and later windows systems – ZAB – 2016-09-02T19:31:26.797

8

Interestingly, for those running VMware Fusion on a Mac, with a Windows bootcamp partition, it's possible to start a virtual machine that runs off the bootcamp partition:

VMware Fusion allows you to launch an already-installed Boot Camp partition without affecting its ability to boot natively. This gives you the ability to access a Boot Camp partition from both inside the Mac OS using Fusion, and by rebooting and launching Windows directly.

Note, however:

You may be prompted to reactivate Windows because different hardware is recognized by the operating system.

Source: VMware - Launching your Boot Camp partition in VMware Fusion

magma

Posted 2015-10-26T12:46:12.613

Reputation: 181

3They recommend you get their tools installed first so the activation process sees the same "hardware" in both situations. Follow instructions or you will end up talking to Microsoft activation servers on the phone. – Thorbjørn Ravn Andersen – 2015-10-27T20:58:06.587

@ThorbjørnRavnAndersen great advice — thank you – magma – 2015-10-28T00:12:20.387

7

I did that. It's pretty useful, but there are some limitations and setting it up isn't trivial.

  • The native-or-VM OS must be tolerant about hardware it runs on. Most Linux distros don't care and just run on anything you boot them on as long as CPU architecture matches. It's not that smooth with Windows. You can also face activation/licensing problems.

  • On Windows, you can't boot OS in a virtual machine while its partition is mounted on host. This is not a big deal, as in this case you'd probably like to boot Linux inside Windows and Windows doesn't support ext filesystems anyway.

  • Setting bootloaders is tricky. You need separate boot configuration for booting in VM and for booting natively. I have used Grub4DOS/NeoGrub as 1st stage bootloader when booting natively, then I was able to chainload Windows bootloader or boot into Ubuntu. VM had a small (few MB) drive mounted with higher priority than the main drive. That drive contained manually installed GRUB2 which I used to boot Ubuntu inside VM.

  • Unusual bootloader setup requires careful updating. For example updating GRUB when running Linux natively could break your bootloader chain. Updating inside VM requires manually installing it to the boot partition.

gronostaj

Posted 2015-10-26T12:46:12.613

Reputation: 33 047

0

That is possible quite easily.. If you use seperate hard disks for each installation!

Have a look here for example: http://www.serverwatch.com/server-tutorials/using-a-physical-hard-drive-with-a-virtualbox-vm.html - or search the VirtualBox website (which is down at the moment) for these keywords.

I use it to be able to access my Linux partition when I do some Windows work, like Adobe stuff or other things that wont run that smoothly under Wine or Virtualisation of Windows in Linux.

Also, because I often access my tower remotely - and my whole Linux hard disk is encrypted, while my Windows hard disk isnt and only has non sensitive data on it. So I start up Windows (the Windows entry in GRUB is preselected) log into Teamviewer, start Virtualbox and then my Linux hard disk.

In this direction - Windows -> Virtualbox -> other HD with Linux -> it is possible quite simple, because of the superiour hardware detection, on the fly, that Linux, namely Ubuntu, supports.

In the other direction, you will end up with a mess. Windows doesnt like ever changing hardware.. It could mess up your system, doesnt start at all, or even if it does, maybe tells you that your license isnt valid anymore because too much hardware changes happend too often.

That said, you want to keep Windows as preselected boot entry in GRUB, but really watch out that you select the right entry in GRUB when it runs in Virtualbox. If you have problems with lags remotely, increase your boot timeout in GRUB to be safe.

Hope that helped you :)

Markus

Posted 2015-10-26T12:46:12.613

Reputation: 1