Linux is my main operating system, but there are times when I need to use a Windows machine. So, I have a Windows install running in VirtualBox. I used to run a Windows virtual machine that was installed onto virtual hard drive, but I then found that you can also install an operating system onto a physical hard drive and configure the virtual machine to use it. I would expect this to be faster (but I haven't tested), but the main benefit for me is that I can now boot into my Windows machine using VirtualBox from my Linux machine, or reboot my physical machine and boot directly into Windows. You can even create the virtual machine after installing Windows. So I actually did a normal install onto a hard drive with a physical machine, and then created a virtual machine for the drive.
To use a physical drive with VirtualBox, you need to first create a virtual
disk that wraps the physical drive. Lets say you have Windows (or any other
operating system for that matter) installed on a hard drive that shows up as
/dev/sdc in your Linux machine. You can create a virtual disk for the physical drive
with the VBoxManage command, but will have to have read access to the device. You can
either change the owner of the device to your user:
are a couple ways to do this. You can either change the owner and access rights on the device,
or you can add your user to the disk group (this works on Ubuntu and Gentoo). I prefer this method.
After adding your user to the disk group, you will need to logout. Once you log back in, you can run
VBoxManage internalcommands createrawvmdk -filename MyDisk.vmdk -rawdisk /dev/sdc
Here MyDisk.vmdk can be whatever you want, and I store mine in a directory named disks/ under the
directory VirtualBox uses to store virtual machines (~/VirtualBox\ VMs/ on my machine). I also
prefer to use the disk ID instead of the device name because it's possible for the device name to
change, especially if you add a new hard drive (i.e. /dev/sdc ends up as /dev/sdd after a reboot).
to use the disk ID instead, just look at the symbolic links in /dev/disk/by-id/. One of them will
point to /dev/sdc (note that you want the device for the drive, not one of the partitions). For
example:
$ ls -l /dev/disk/by-id/
...
lrwxrwxrwx 1 root root 9 Jul 22 13:09 usb-SABRENT_SABRENT_DB9876543214E-0:0 -> ../../sdc
lrwxrwxrwx 1 root root 10 Jul 22 13:09 usb-SABRENT_SABRENT_DB9876543214E-0:0-part1 -> ../../sdc1
lrwxrwxrwx 1 root root 10 Jul 22 13:09 usb-SABRENT_SABRENT_DB9876543214E-0:0-part2 -> ../../sdc2
lrwxrwxrwx 1 root root 10 Jul 22 13:09 usb-SABRENT_SABRENT_DB9876543214E-0:0-part3 -> ../../sdc3
lrwxrwxrwx 1 root root 10 Jul 22 13:09 usb-SABRENT_SABRENT_DB9876543214E-0:0-part4 -> ../../sdc4
...
So, to create a virtual drive for my Windows install, I would do
cd ~/VirtualBox\ VMs/disks/
VBoxManage internalcommands createrawvmdk -filename Windows10.vmdk -rawdisk /dev/disk/by-id/usb-SABRENT_SABRENT_DB9876543214E-0:0
Now you can open VirtualBox and:
- Click "new" to add a new machine.
- When you get to the "Hard Disk" window, select "Use an existing virtual hard disk file".
- Click on the folder icon next to the drop down box to add the drive.
- In the Hard Disk Selector window, click on "Add".
- In the file browser that opens, go to the directory that you created the virtual disk in. Select the file, and click "Open".
- The drive should appear in the list now. Select it and click "Choose".
- Complete the rest of the setup as normal.
And that's it. Boot up the new machine and it will boot from the physical drive. If you don't already have an operating system installed on the drive, then you can install it through the virtual machine as normal, and all of the OS files will be written to the physical drive.
If you have run into permission errors, make sure your user is in the disk group,
and that the device you are trying to use is in this group as well.