Virtualization has been a tremendous help to developers such as myself. It keeps us from having to keep a pile of hard drives/machines all running different operating systems. And while this is great, there are some times when you might want to use a raw hard drive. For example, if you pull a hard drive out of an existing system and want to keep the OS/data intact but use it as a virtual machine inside another host OS.
Well, you can set up VirtualBox to use a physical hard drive inside a VM. This method is called “raw hard disk access.” This allows you to mount or boot a secondary hard drive or external USB as your storage device inside the VM.
Warning: Do not attempt to mount or boot the partition that runs the host OS with a VM, as that can lead to severe data corruption. And no matter which drive or partition you want to mount, it’s always a good idea to back up any existing data on it before continuing.
Creating the Virtual Machine:
You’ll first want to create a virtual machine within the VirtualBox GUI. Choose all options like normal, but when asked about the virtual hard drive, select Do not add a virtual hard drive.
Next, you’ll need to find the path and drive number of the physical drive you’d like to mount:
In Windows, open Disk Management. You’ll see the associated drive numbers on the left and will identify them later as PhysicalDrive0, PhysicalDrive1, etc.
In Linux, run the following command:
sudo fdisk -l. You’ll see a listing of the partitions. Make note of the desired drive in the format of dev/sda0, dev/sda1, etc.
In Mac OS X, use the following command:
diskutil list. You’ll see a listing of the partitions. Make note of the desired drive in the format of /dev/disk0, /dev/disk1, etc.
Now open a command prompt on your OS. If you’re using Windows as the host OS you will also need to go to the root directory of VirtualBox using the following command: cd C:\Program Files\Oracle\VirtualBox
Creating the VirtualBox Hard Drive:
Now you can run the command to create the virtual hard drive file that points to your desired physical drive. For each of the following examples ensure that you input the desired names between the arrows and replace the number or pound sign with your desired drive number.
You often must be administrator to run this command. Make sure your cmd.exe is running as administrator. This also means you must start VirtualBox as administrator to use these drive links.
VBoxManage internalcommands createrawvmdk -filename "C:\Users\\VirtualBox VMs\\.vmdk" -rawdisk \\.\PhysicalDrive#
VBoxManage internalcommands createrawvmdk -filename ".vmdk" -rawdisk /dev/sda
For Mac OS X:
VBoxManage internalcommands createrawvmdk -filename ".vmdk" -rawdisk /dev/disk#
This VMDK will be a very small file that simply points to your physical drive. Now you can attach the VMDK to your VM. To do so, open the VirtualBox GUI, select the desired VM, click Settings, clickStorage, click Add Hard Disk button, select Choose existing drive, and then select the VMDK file you just created.