skip to content
Hamza Butt

Raw Disk Virtualisation with VirtualBox

/ 5 min read

Last Updated:
Tags: virtualbox
WARNING - Following this guide incorrectly could lead to data corruption. It is advised not to mount or boot a drive that contains partitions running the host OS through a VM. Backing up any data before continuing is recommended as an extra precaution.

1. The problem

Being compelled to switch back to Windows for university work, I decided to set up dual-boot with an extra hard drive I had lying around, keeping Windows on one hard drive and Arch on the other. Over time, I became tired of having to restart my system every time I wanted to switch operating systems.

2. The solution

Having used Virtual Machines for a while, they proposed an easy solution, as they allow running and switching between various Linux distributions as needed.

A few weeks went by, and I wanted to take this concept one step further. What if I could get a virtual hard disk image to “point” to a physical drive so it can be mounted directly to the VM, eliminating the need to dual-boot.

[VM] -> [Virtual disk] -> [Physical hard drive]

After some looking around, I found out this was called “raw hard disk access” and is possible to pull off with VirtualBox. This guide will take you through my journey, attempting to set up accessing a physical drive through virtualisation.

3. The process

Creating the virtual box disk image was reasonably straightforward, following the VirtualBox documentation. As I wanted to access the entire physical hard disk, I followed section “9.7.1.1. Access to Entire Physical Hard Disk” but depending on your specific needs, select the relevant subsection under “9.7 Advanced Storage Configuration”.

The first you’ll need is the physical drive number, which can be found by running:

C:\Users\Hamza> Get-PhysicalDisk
Number FriendlyName SerialNumber MediaType CanPool OperationalStatus ...
------ ------------ ------------ --------- ------- ----------------- ...
2 TOSHIBA #### #### HDD False OK ...
1 Hitachi #### #### HDD False OK ...
0 OCZ #### #### SSD False OK ...

In my case, the physical drive number is 1. Now navigate to the VirtualBox directory for easier access to the built in utility programs by running:

cd "C:\Program Files\Oracle\VirtualBox"

Followed by the command shown in the documentation to create the disk image:

./VBoxManage internalcommands createrawvmdk -filename \
"C:\Users\Hamza\Desktop\arch.vmdk" -rawdisk \\.\PhysicalDrive1

A small VMDK file should be made that contains information pointing to the physical drive, and to check this I opened the VMDK file with notepad, and it should look similar to:

# Disk DescriptorFile
version=1
CID=###
parentCID=###
createType="fullDevice"
# Extent description
RW ### FLAT "\\.\PhysicalDrive1" 0
# The disk Data Base
...

The next thing to do is create a standard virtual machine, but this time making sure to select “Do not add a virtual hard drive”. Once the initial VM set-up is complete, open the settings to the storage tab and mount a hard drive, under the SATA controller, selecting the VMDK that was just made. In an ideal world, you would now start the VM, and it should work flawlessly, but for me that was not the case.

3.1. VERR_ACCESS_DENIED

Looking back at the documentation, it does warn that “on some host platforms, such as Windows, raw disk access may be restricted and not permitted by the host OS in some situations.” The solution is to run VirtualBox as administrator, giving it access to read and write access to the physical drive, avoiding the “VERR_ACCESS_DENIED” error.

3.2. Boot order issues

Starting the VM, I ran into the next first problem. I had forgotten to change the boot order in the VM settings. Navigating to the system tab in the settings and pushing the hard disk up to first priority ensured the VM would look to the hard drive when booting.

3.3. Boot mode issues

Attempting to start the VM once again gave an error saying it “could not read from boot medium”. Remembering that my system boots using the UEFI boot mode, I enabled it in the system settings by ticking enable EFI. To check the boot mode of your Windows system, press Win + r and then type msinfo32.exe (alternatively, typing “system info” into the start search bar should open the same tool). Pressing enter should open a window and on the right pane, the “BIOS Mode” should be visible. If it states “Legacy” then your system has BIOS, else if it states UEFI then your system boots in the UEFI mode. To check the boot mode on Linux, which can vary depending on distribution, look for the “/sys/firmware/efi” folder. If it exists your system is using UEFI but if not then your system is using BIOS.

3.4. BLKCACHE_IOERR

Starting the VM once again leads to another error, this time being “BLKCACHE_IOERR”, caused by VirtualBox using its own small cache to buffer writes, but not performing any read caching since it is typically carried out by the guest OS. To fix this, enable Host I/O caching under the SATA controller.

4. Conclusion

After fixing all the issues, the VM finally booted and worked great. I suggest making quality of life improvements such as provisioning the guest additions disc, playing around with the graphics controller to see which gives the best performance (in my case “VboxVGA” worked best) and assigning a decent amount of memory and video memory.

Overall, the process was a valuable learning experience, and the next step would be to try the same using VMware, having heard it’s slightly easier to set up to boot from a physical drive.