Boot Your Real OS from VirtualBox


This post seemed a logical extension of my last post, Installing Ubuntu to a USB Hard Disk. As a preface to this post, I’ll set out the following scenario:

You’ve installed Ubuntu to your USB Hard Disk, but now you want to boot into it to configure it. As with the install itself, you’d rather not have to restart your host system to perform the installation. You’d rather just load up the system in a VM and configure it that way.

For the purposes of this post, we’re going to assume you’re running Ubuntu. I know that not everyone who reads this will be, but since I’m writing this for people who are relatively new to Linux, I have to stick to the Ubuntu way of doing things.

As well, my example scenario is not the only use of this method. You might be able to, for instance, boot Windows in a VM. I tried this with Windows 7, but it did not work. It would simply loop through the “Windows Is Loading Files” screen and then reboot.

Preparation

Before we can boot into your system, we first have to configure a few things.

Raw Disk Access Privileges

If you want to be able to access your disk directly, you’ll need to configure those permissions. The easiest way to do so is to simply add yourself to the “disk” group.

Open the group management utility via System -> Administration -> Users and Groups -> Manage Groups. Find “disk” in the list of groups, and open the Properties. Enable your user in the Group Members list.

Once you have added yourself, you must log out and then back in for the privileges to take effect.

Backup

This should be a given, but make sure your data is properly backed up on the disk you’ll be booting, before you do this.

Additionally, it should go without saying that you should not have the disk mounted on the host system, when you go to boot into it. Two systems accessing the same raw disk is almost certainly going to cause data corruption on a major scale. Linux will probably deny simultaneous access to the disk, but that’s not something I want to test. Do you?

Creating the Virtual Raw Disk

For boot disks, the only format which VirtualBox recognizes is VMDK, ISOs notwithstanding. So, to get VirtualBox to recognize a physical disk, we have to create a sort of wrapper. Luckily, this functionality is built into VirtualBox’s management tool.

Load up a terminal and navigate to ~/.VirtualBox/Machines/. Open up the Disk Utility via System -> Administration -> Disk Utility, then determine which disk you would like to boot. Absolutely, positively, do not try to boot your host system’s disk. It’s probably not even possible, but I really have to hammer down the point that you will inevitably mess something up that way. If you are dual-booting your host system on the same disk, then you absolutely should not attempt to boot the other OS this way.

Under the drive’s information, take note of the “Device” path. It will be something like /dev/sdd. Take note of that.

Back to your terminal. Run VBoxManage internalcommands createrawvmdk -filename ~/.VirtualBox/Machines/Ubuntu.vmdk -rawdisk /dev/sdd -register, substituting the filename for whatever you want, and the rawdisk for the device associated with your intended boot disk.

What this command does is create a wrapper between the raw disk’s i/o and VirtualBox’s format, in order to translate. The documentation is probably worth reading, for a better understanding of what you’re doing.

The -register switch automatically registers this vmdk with VirtualBox, so you don’t need to bother adding it to your Virtual Media Manager.

Configuring a VM

If you’re attempting this, you should already know at least the basics of how to configure a VirtualBox guest, but I will cover some important aspects of “migrating” an existing OS into a VM setting. Because I’ve already created my working VM, I won’t cover the “wizard” aspect of creating the VM. Open the “Settings” dialogue of your VM.

General

It is, of course, best to select the Operating System and Version that you will be booting. This will allow VirtualBox to know how to install the Guest Additions for your system.

System

I would suggest giving your system at least 1GB of memory, if you have it free. As well, it is very important that you enable “IO APIC”, if your original system had this. Most systems do, and all Intel systems do, so far as I know.

If you’re booting a Linux/BSD/Unix system, enable “Hardware clock in UTC time”. I also recommend enabling the absolute pointing device.

Display

Give your guest a reasonable amount of video memory. I have mine configured to have 64MB. That might be overkill, but memory’s cheap.

I don’t know how well 3D acceleration works at this time, but it probably doesn’t hurt to enable it.

Storage

*Important*: Add an IDE controller for a CD and select the VBoxGuestAdditions.iso. This will save you from having to reboot to have that device added.

Depending upon whether your physical disk is attached by IDE or SATA, add a controller and select your raw disk’s VMDK. It should’ve already been registered with the VMM when we created the wrapper.

Boot

Alright, now that we’ve configured the VM, just boot it. It might take a bit longer than usual to boot into the system. There are a couple of reasons for this. First, the system is a couple of layers of abstraction away from the disk itself. It’s going through your host’s kernel, through VirtualBox’s raw VMDK wrapper, to your system’s kernel.

As well, because we’re using IO APIC, there will be a bit of a slow-down. It might not be necessary to have it enabled on Linux systems, but it’s imperative that you keep it enabled for Windows guests. There are guides on how to safely disable it, but I’ll leave that up to you to figure out.

Installing Guest Additions

You’ll notice that your guest VM’s screen size is at 800×600; or, at least, it was for me. In order to support greater resolutions, you’ll need to install guest additions.

This is a fairly simple process. If you already added the device pointing to the VBoxGuestAdditions.iso, it’s as simple as running the AutoRun associated with that CD. After a quick reboot, you should have a higher resolution enabled.

That’s it, folks.

The process is fairly simple when you get down to it, but it takes a bit of ingenuity to really make it work. I’m thinking the next blog post I’ll write will be about setting up a “Reverse Wubi”. That is, install Windows in a file on your Ubuntu install’s disk, and allow it to boot. I haven’t managed it yet, but it’s probably possible. Stay tuned ;).

  1. #1 by Timmy on September 20, 2012 - 1:16 pm

    Actually, you didn’t need to do that, USB Host mode could do this for you ;)

  2. #2 by x10an14 on April 9, 2013 - 12:36 pm

    Hi,

    I realize this might be an addition/question coming a little late, but I was wondering if you were aware of any big issues with doing this with windows as the host OS, instead of linux?

    Would be awesome if I could install the two dual-boot as normal, and then also be able to boot my linux (on a separate partition) inside of windows!

(will not be published)
*