Virtualbox and raw devices

Warning: Usage of raw devices is for experts only. Incorrect use can lead to severe data corruption or even complete loss of data. Please read the corresponding chapter in the virtualbox manual as well.

Virtualbox supports raw devices as disks for virtual machines. I would like to sum up my experiences which may help to circumvent some problems with raw devices.

Windows

Windows grants read only permission to disks (or in my case, to the partitions on the raw disk it is able to read, like a FAT32 formatted EFI system partition (ESP)). Every time a write access to the ESP was performed by Virtualbox, it crashed with an access violation. The solution to this problem is to offline the raw device before using it in Virtualbox.

So in general, this is the procedure to use a raw device with Virtualbox in Windows:

  1. Start cmd.exe as administrator
  2. Use diskpart in the administrative command interpreter to offline the device. Make sure to select the correct disk in diskpart!
    diskpart
    list disk
    select disk #
    offline disk
    attributes disk clear readonly
    exit
    
  3. Use VBoxManage to generate a vmdk file for the raw device. Again use the same disk number as in diskpart above:
    cd "C:\Program Files\Oracle\VirtualBox"
    VBoxManage internalcommands createrawvmdk -filename "%UserProfile%\Desktop\rawdisk.vmdk" -rawdisk "\\.\PhysicalDrive#"
    
  4. Start up the Virtualbox GUI as administrator
  5. Create a new virtual machine and use “%UserProfile%\Desktop\rawdisk.vmdk” as the hard disk for the new machine
  6. Boot up the machine. It should now directly read and write to the chosen raw device.

Windows still has the problem that the device ordering may change after a reboot. As my Linux system is on an USB stick, I have to plug that stick in right after Windows booted. That assures that I always access the correct physical drive.

Linux

Linux (in this particular case: udev) makes it much simpler to use raw devices with Virtualbox. These are the steps to use raw devices with Linux:

  1. Unmount all partitions from the raw device. Also make sure that no partition of the raw device gets automounted on the host as long as the virtual machine using the disk is running
  2. Determine the device node. Thanks to udev, we can use a symlink which always points to the correct physical drive. In my case, this is for example:
    ls -l /dev/disk/by-id/ata-USB_3.0_Express_RC8_120103000000000003
    
  3. The normal user must have write access to the raw device. This can be accomplished on most distributions by adding the user to the “disk” group. But that might leave the system in a very unsafe state, if the user can write to ANY raw device. Therefore, it is much better to write an udev rule which sets the owner of the designated raw device to the user running Virtualbox (it is also possible to change that rule to, e.g., assign a specific group). So create or edit a ruleset file for udev. On my system, this is “/etc/udev/rules.d/99-local.rules”.
    vi /etc/udev/rules.d/99-local.rules

    Add something like the following code to the file:

    SYMLINK=="disk/by-id/ata-USB_3.0_Express_RC8_120103000000000003", OWNER="manuel"

    My username here is “manuel”, and the symlink is the device node from the previous step without the leading “/dev/”.

  4. For the new rules to take effect, reboot the host. It may be possible to activate the new rule with:
    udevadm control --reload-rules
    udevadm trigger

    or unplug and reconnect the device. Triggering udev might not work sometimes though. So rebooting the host is the safest option.

  5. Use VBoxManage to generate a vmdk file for the raw device. Again use the same device node as above:
    VBoxManage internalcommands createrawvmdk -filename "$HOME/rawdisk.vmdk" -rawdisk "/dev/disk/by-id/ata-USB_3.0_Express_RC8_120103000000000003"
    
  6. Start up the Virtualbox GUI
  7. Create a new virtual machine and use “$HOME/rawdisk.vmdk” as the hard disk for the new machine
  8. Boot up the machine. It should now directly read and write to the chosen raw device.

As a general safety note, I would like to repeat that these instructions are for expert users. In general, you have to make sure that one partition is mounted ONLY ONCE. As soon as you start mounting partitions multiple times (on the host and in a VM, or in multiple VMs), very bad things start to happen.