Ubuntu – Using a Physical Hard Drive Pariition as a VirtualBox VM and as a Bootable Partition

dual-boothard drivepartitioningvirtualbox

Is it possible to install Ubuntu OS with VirtualBox using physical Hard Drive Partition and then make it bootable?

What I want is the Ubuntu OS that I've installed in VirtualBox can be accessed and be booted both from physical machine and VirtualBox VM (not at the same time of course). So, sometimes i can boot it as a virtual machine and sometimes i can boot it as a real machine (as a dual boot alongside my main OS which is Linux Mint).

After reading some articles about this, here's the link:

  1. Migrate from a virtual machine (VM) to a physical system
  2. Using a raw host hard disk from a guest
  3. Using a Physical Hard Drive with a VirtualBox VM
  4. VirtualBox: using physical partition as virtual drive

I think I can do that, but I'm not sure about the result for my issue. It's said, if there's a problem in the process it will lead to severe data corruption.

Is it mean the entire data in my hard drive (not just a partition) will be corrupted?

EDIT

I forgot to mention that, the Virtual Ubuntu OS that I've installed is partitioned to three partition. And the physical partition drive that I will use is one logical partition. This is the issue that make me not sure about the result.

Best Answer

I find solution for my own problem. It's not so simple. For me as an inexperienced Ubuntu users it's confusing. Eventually, I decide not to do this. But, maybe someone have another way to do this better than I do.

Install new Ubuntu OS

Install Ubuntu OS along side your main OS. In my case, it's linux mint. But, I see no difference in the process between mint and Ubuntu since mint is based on Ubuntu.

Create Medium File for Virtual Box to Read The New OS

After done installing. You need to create a medium for VirtualBox to read the partition which is contains the fresh installed Ubuntu OS. The way you do is like this:

VBoxManage internalcommands createrawvmdk -filename /path/to/file.vmdk -rawdisk /dev/sdX -partitions Y,Y

Where X is your hard drive and Y is your parition number. Don't include the partition that contains your main OS boot. Because we need different boot medium for the new OS that doesn't contains main OS boot. You may need to run as root.

Create Medium for Virtual Box to boot from new OS

For creating boot medium we will create an ISO image from new OS boot file. First, we have to do this in our main OS. Restart the computer and boot from main OS. In my case, I reboot to Linux Mint.

We need to create a temporary folder for creating iso boot file. So, once Mint is ready to do the job. Create a folder. I assume we doing this in Desktop. We will do like this:

  • Go to Desktop.

  • Create new folder and give it name "iso".

  • Open that iso folder and create a new folder again named "boot".

  • Open that boot folder and create a new folder once again named "grub".

    If we are doing this via terminal, all we have to do just type this line and hit enter

    mkdir -p ~/Desktop/iso/boot/grub
    

    Terminal is a great tool if we know what we're doing! ;)

  • Once the folders are created, we need to copy the new Ubuntu OS boot files to that folder. So, open the partition where we install Ubuntu OS and then copy all files from /usr/lib/grub/i386-pc/ to ~/Desktop/iso/boot/grub. And copy all files from /boot/grub/grub.cfg to ~/Desktopiso/boot/grub.

    Again, if this step is done via terminal, we should do like this:

    cp /usr/lib/grub/i386-pc/* ~/Desktop/iso/boot/grub
    
    cp /boot/grub/grub.cfg ~/Desktop/iso/boot/grub
    
  • Open grub.cfg in ~/Desktop/iso/boot/grub/grub.cfg with text editor. Warning don't make any mistake, the one that you'll edit here is not the one in your /boot/grub/grub.cfg. If you edit the last I mentioned, your main OS boot will fail.

  • Once grup.cfg opened. Delete menuentry that not related to the new Ubuntu OS that we've installed. It's usually look like this:

    ### Begin /etc/grub.d/your_main_os ###
    menuentry 'The text displayed in boot menu' {
        ----
        some code we don't need understand for this
        ----
    }
    ### End /etc/grub.d/your_main_os  ###
    

    Delete every menu entry that not related to the new Ubuntu OS we've installed. We don't need them to be exist in VirtualBox don't we?

  • Next, convert the folders with all files included to iso with this:

    grub-mkrescue -o boot.iso ~/Desktop/iso
    

    Note: If you got an error while doing this, you need to install xorriso first. with this:

    sudo apt-get install xorriso
    
  • Iso file for booting is created. It will be good if we put that iso file in the same folder with the medium file that we're created.

Set the Virtual Machine to be Able to Boot the Exact Ubuntu OS that installed

We've created the medium to read the new installed OS and we've created it's boot loader for VirtualBox independently. The last thing is to configure, how Virtual Box should do this.

  • Go to where we put our medium.vmdk. Open terminal there and do this:

    chmod -v 666 medium.vmdk
    
  • As root change medium.vmdk and boot.iso file permission. We've to be the owner of those file.

  • Open VirtualBox. Create a new machine. When the Hard Drive option gives options, select use an existing virtual hard drive and take themedium.vmdk

  • Finish it, but don't run it yet. With the new machine selected, open settings.

  • In Storage section, if there's no CD storage create the new one. Then make mount boot.iso that we've created in that CD storage.

  • Before close the Settings, go to System section to make sure. The CD storage that contains boot.iso is read first before Hard Disk. That way, Virtual Box will boot from CD to read the new installed OS.

  • Finish. And we can run the new Installed OS both from Virtual Machine and from real machine.

Important Notes

Even though we can boot it from two way. There's some issues i found.

  1. Whenever we boot from real machine and then try to boot it from virtual machine it will be error. Because the physical drive state is changed without medium.vmdk knowing it. So, we have to create a new medium and change it's permission again to be able to boot from virtual machine. But, we don't need to create new boot.iso.

  2. Because the MAC address from both machines is different. The OS will be confused when try to configure network settings. In my case, I installed Ubuntu Server as new OS and everytime I boot from different machine, it will give a message like "Waiting for network configuration ..." for about 2 minutes and then boot.

That's it. My solution for my own issue.