Use physical harddisk in Virtual Box

This question shows research effort; it is useful and clear

138

Save this question.

Show activity on this post.

Using Virtual Box, how can I install an OS to a secondary, physical disk, and boot it in both (at separate times) Virtual Box, and as a typical secondary OS install?

Follow this question to receive notifications

Petah

asked Oct 28, 2012 at 23:12

PetahPetah

1,8462 gold badges16 silver badges22 bronze badges

2

10 Answers 10

This answer is useful

189

Save this answer.

Show activity on this post.

I made a smug comment ("Straight forward") when this question was originally posted and found myself at this page some months later looking for the same answer


Today, I managed to boot from physical drives. This achievement required;

  • 60 min straight forward reading
  • 30 min writing this
  • 20 min trying different options in VirtualBox

Dear reader, your attention is directed at the fact that it is equally straight forward to kill your data when the disc is accessed in this manner. The procedure on how to accomplish this is left as an exercise...

Pics (it did happen :)

grub 2

1. Create vmdk drives that read from real drives

To create an image that represents an entire physical hard disk (which will not contain any actual data, as this will all be stored on the physical disk), on a Linux host, use the command

VBoxManage internalcommands createrawvmdk \
    -filename /path/to/file.vmdk \
    -rawdisk /dev/sda

You must have read access to the physical disk device file /dev/sda. To accomplish this you can either change the owner and access rights of the device file or run VBoxManage internalcommands createrawvmdk… as root (put sudo before the command).

On a Windows host, instead of the above device specification, use e.g. \\.\PhysicalDrive0 (see "Enumerating physical drives in Windows" section below). On a Mac OS X host, use e.g. /dev/disk1 (and unmount all partitions on disk1).

(all on one line)
C:\VirtualBoxDrives>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe"
    internalcommands createrawvmdk
    -filename C:\VirtualBoxDrives\raw-0.vmdk 
    -rawdisk \\.\PHYSICALDRIVE0

(output)
RAW host disk access VMDK file C:\VirtualBoxDrives\raw-0.vmdk created successf..

internal commands are no longer used in 2023, virtual box 7, so do this instead:

VBoxManage.exe createmedium -filename 3.nvme-pro.vmdk --variant RawDisk --format=VMDK --property RawDrive=\\.\PhysicalDrive3

2. Assign the vmdk drives to a VM

VM -> Settings -> Storage -> add ide / sata -> Choose existing disk -> browse

assign

3. Set one of the Writethrough modes.

File -> Virtual Media Manager -> e.g raw-0.vmdk -> [modify] -> (o) Writethrough

  • repeat for all drives

set writethrough

3. Reorder boot-priority (semi optional)

VM -> Settings -> System -> Boot order

[./] Hard Disk
[./] CD/DVD
[  ] Floppy
[  ] Network

VM -> Settings -> Storage -> Controller: SATA -> e.g. raw-5.vmdk -> Hard Disk: Sata Port 0

Relevant chapters (manual)

  • Chapter 9. Advanced topics

  • 9.9. Advanced storage configuration

  • 9.9.1. Using a raw host hard disk from a guest

  • 9.9.1.1. Access to entire physical hard disk

  • Chapter 5. Virtual storage

  • 5.2. Disk image files (VDI, VMDK, VHD, HDD)

  • 5.3. The Virtual Media Manager

  • 5.4. Special image write modes


Enumerating physical drives in Windows:

C:\Users\win7>wmic diskdrive list brief

Caption                               DeviceID            Model                                 Partitions  Size
Samsung SSD 840 EVO 250GB ATA Device  \\.\PHYSICALDRIVE3  Samsung SSD 840 EVO 250GB ATA Device  6           250057228288
ST1000DM003-1CH162 ATA Device         \\.\PHYSICALDRIVE1  ST1000DM003-1CH162 ATA Device         5           1000202273280
ST1000DM003-1CH162 ATA Device         \\.\PHYSICALDRIVE2  ST1000DM003-1CH162 ATA Device         5           1000202273280
ST1000DM003-1CH162 ATA Device         \\.\PHYSICALDRIVE0  ST1000DM003-1CH162 ATA Device         5           1000202273280
WDC WD1000DHTZ-04N21V0 ATA Device     \\.\PHYSICALDRIVE5  WDC WD1000DHTZ-04N21V0 ATA Device     6           1000202273280
WDC WD1000DHTZ-04N21V0 ATA Device     \\.\PHYSICALDRIVE4  WDC WD1000DHTZ-04N21V0 ATA Device     5           1000202273280
ST1000DM 003-1CH162 SCSI Disk Device  \\.\PHYSICALDRIVE6  ST1000DM 003-1CH162 SCSI Disk Device  5           1000202273280

You can also get the drive numbers with PowerShell:

PS C:\ Get-Disk

Follow this answer to receive notifications

answered May 20, 2014 at 20:57

Ярослав РахматуллинЯрослав Рахматуллин

11.6k5 gold badges44 silver badges74 bronze badges

34

This answer is useful

19

Save this answer.

Show activity on this post.

The VirtualBox documentation refers to accessing a raw hard disk:

To create an image that represents an entire physical hard disk (which will not contain any actual data, as this will all be stored on the physical disk), on a Linux host, use the command

VBoxManage internalcommands createrawvmdk -filename /path/to/file.vmdk
  -rawdisk /dev/sda

This creates the image /path/to/file.vmdk (must be absolute), and all data will be read and written from /dev/sda.

To create a special image for raw partition support (which will contain a small >amount of data, as already mentioned), on a Linux host, use the command

VBoxManage internalcommands createrawvmdk -filename /path/to/file.vmdk
  -rawdisk /dev/sda -partitions 1,5

VirtualBox uses the same partition numbering as your Linux host. As a result, the numbers given in the above example would refer to the first primary partition and the first logical drive in the extended partition, respectively.

See the documentation for caveats and Windows-specific instructions.

Follow this answer to receive notifications

answered Oct 31, 2012 at 1:53

trpt4himtrpt4him

1,6401 gold badge10 silver badges16 bronze badges

0

This answer is useful

10

Save this answer.

Show activity on this post.

Just want to elaborate how to do this in Windows 10

  1. Get the Physical disk ID
  • Open powershell
  • Type 'Get-Disk'
  • Look for your disk (write down the number column value)
  1. Clear readonly attributes (credits to: Korkman at https://forums.virtualbox.org/viewtopic.php?f=6&t=38914#p175089)
  • run DISKPART (command line utility)
  • select hard drive carefully using SELECT DISK. disk numbering starts at zero. you can roughly verify your selection with LIST PARTITION if you know what your partitions should look like or LIST DISK to see disk sizes.
  • offline the disk using OFFLINE DISK. all volumes will disappear from windows explorer.
  • ATTRIBUTES DISK CLEAR READONLY
  • verify with ATTRIBUTES DISK
  • Run services.msc in Run
  • Look for virtual box service, change account on Log On tab to Administrator
  • Restart the service
  1. Create VMDK disk
  • Open command prompt as Administrator
  • type "C:\Program Files\Oracle\VirtualBox\VBoxManage" internalcommands createrawvmdk -filename C:\VM\Kubernetes\linux.vmdk -rawdisk \\.\PHYSICALDRIVE2
  • type "C:\Program Files\Oracle\VirtualBox\VBoxManage" modifyhd "C:\VM\Kubernetes\linux.vmdk" settype writethrough
  • type "C:\Program Files\Oracle\VirtualBox\VBoxManage" storageattach Kubernetes --storagectl SATA --device 0 --port 0 --type hdd --medium "C:\VM\Kubernetes\linux.vmdk"
  1. Start your VM

Follow this answer to receive notifications

32 bronze badges

answered May 31, 2020 at 11:27

JamesJames

1011 silver badge3 bronze badges

This answer is useful

7

Save this answer.

Show activity on this post.

I was unable to get this working in Windows 10 for the longest time; I kept getting a VERR_ACCESS_DENIED error when attempting to attach the VMDK.

However, I was (finally) able to get this working with a few tricks:

  1. Open PowerShell as an administrator (I'm not sure why, but I couldn't get some of the commands to work in an administrator command prompt)
  2. Run the following command to create the VMDK:
VBoxManage.exe internalcommands createrawvmdk \
    -filename "path\to\physical\disk\file.vmdk" \
    -rawdisk \\.\PhysicalDrive[n]

...where path\to\physical\disk\file.vmdk is the file that will be created to represent the disk and [n] is the disk number (use Disk Management to obtain this number)
3. Run the following command to attach the VMDK:

VBoxManage.exe storageattach [VM NAME] \
    --storagectl [CONTROLLER] \
    --device 0 \
    --port 0 \
    --type hdd \
    --medium "path\to\physical\disk\file.vmdk"

...where [CONTROLLER] is the name of the disk controller you are attaching to - "IDE" in my case
4. Run the following command to start the VM:

VBoxManage.exe startvm [VM NAME]

Follow this answer to receive notifications

answered Oct 26, 2019 at 5:24

Nathan OsmanNathan Osman

2,37211 gold badges48 silver badges66 bronze badges

1

This answer is useful

6

Save this answer.

Show activity on this post.

I was attempting to boot a Linux VM (specifically Ubuntu 19.10) on macOS where Ubuntu is installed on a physical ext4 formatted partition. Many of the answers I found, including that from @ЯрославРахматуллин got me most of the way there, but I kept getting the following error message:

FATAL: No bootable medium found! System halted.

One answer referred to booting into a LiveCD and then altering the GRUB configuration to address this. However, I don't even have GRUB installed on my Mac. Eventually, I found the key for my situation was to ensure the VM had access to the EFI partition on my physical disk, and to enable EFI support in the VM settings.

enter image description here

Below is the output of diskutil list from my host Mac machine:

/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI NO NAME                 536.9 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            161.1 GB   disk0s2
   3:       Microsoft Basic Data Development             9.1 GB     disk0s3
   4:                  Apple_HFS Recovery HD             650.0 MB   disk0s4
   5:           Linux Filesystem                         79.2 GB    disk0s5

From this, I generated the VMDK file as follows, giving the guest VM read-only access to the EFI partition and full write access to the NTFS (3) and Ext4 (5) partitions.

sudo VBoxManage internalcommands createrawvmdk -filename disk0.vmdk -rawdisk -partitions 1r,3,5

Now I can dual-boot MacOS or Ubuntu, and when already in MacOS, I can boot Ubuntu as a VM if needed. Happy days!

Follow this answer to receive notifications

answered Nov 7, 2019 at 13:43

John RixJohn Rix

2713 silver badges6 bronze badges

This answer is useful

3

Save this answer.

Show activity on this post.

I found that to add the raw vmdk file to a vbox guest (Running ubuntu as host) one needs to change the permissions of the /dev/sdx file to 777 before building the vmdk file.

Follow this answer to receive notifications

answered Jan 16, 2020 at 16:48

user1130560user1130560

311 bronze badge

This answer is useful

3

Save this answer.

Show activity on this post.

I got this mostly working with a Windows host and Linux guest by following the accepted answer.

However, I ran into the problem that the physical drive numbering in Windows doesn't stay the same across reboots. This is apparently the expected behaviour for disk numbering:

So technically, if you have a Windows host and a Linux guest, you should take an extra step: check the output of wmic diskdrive list brief again before you run the VM. If the numbering is different than it was when you created the VM, then don't run it. Otherwise, there's a risk that it will boot the Windows hard drive from within itself, which is the dangerous behaviour that they warn you about (hasn't seemed to cause any permanent damage in my case though). Or, if you prefer, create a second VM with the hard drives swapped, and choose which one to start based on the output of wmic diskdrive list brief. It should be possible to do all this in a script, but the script would be dependent on your system.

Or, perhaps simpler, make Linux the host and Windows the guest, if possible. Linux can get a consistent drive identification using /dev/disk/by-id/..., as mentioned in a comment.

Follow this answer to receive notifications

answered May 18, 2020 at 16:47

user2596667user2596667

413 bronze badges

This answer is useful

3

Save this answer.

Show activity on this post.

Windows 11 Host / VirtualBox 7.0 Update

Start with this answer, and modify as follows for Windows 11 and VirtualBox 7.0.8:

  1. Run VirtualBox Manager as Adminstrator
  2. Create a new machine but do not specify Hard Disk
  3. Open Powershell or command as Administrator
  4. cd 'c:\Program Files\Oracle\VirtualBox'
  5. .\VBoxManage.exe createmedium disk --filename 'C:\VMs Directory\Specific VM\Redhat8.vmdk' --format VMDK --variant RawDisk --property RawDrive=\\.\PhysicalDrive3 ref
  6. Return to VirtualBox Manager and under

Storage > Settings>Storage Devices>Controller:SATA>Add Attachment

select Redhat8.vmdk (filename from createmedium statement)

This works (as you might have guessed) with RHEL 8.8.

The answer here (nearby) is helpful to keep your sanity around the seemingly capricious PhysicalDisk assignments from Windows. Essentially, use wmic diskdrive list brief

Follow this answer to receive notifications

answered Jun 4, 2023 at 22:52

wistlowistlo

2012 silver badges6 bronze badges

2

This answer is useful

1

Save this answer.

Show activity on this post.

MacOS 10.14:

  1. Create a virtual disk file corresponding to the real disk:
# First, find the device you want to use inside Virtual Box:
diskutil list  

# Create the virtual disk
sudo VBoxManage internalcommands createrawvmdk \
        -filename ~/realdisk.vmdk \
        -rawdisk /dev/disk2
  1. Launch VirtualBox as a super user:
sudo VirtualBox
  1. Add ~/realdisk.vmdk in Storage --> Controller: Sata.

Follow this answer to receive notifications

answered Aug 25, 2019 at 22:56

Max MalyshMax Malysh

1411 gold badge1 silver badge6 bronze badges

This answer is useful

0

Save this answer.

Show activity on this post.

I'm using a Linux host and Windows 10 guest. My problem is that the Windows physical drive changes on every boot - it's ranged from /dev/sdc to /dev/sdm.

My solution was to create a script thats runs on startup. It detects the drive with the WIN10 label, and then uses sed to replace the device in the .vmdk file to the current drive device.

#!/bin/bash

windrive=$(blkid | grep WIN10 | awk 'END{print $1}')

drive_device="${windrive: -5:3}"

sed -i "s/sd./${drive_device}/g" "/home/testuser/VirtualBox VMs/Win10/test.vmdk"

Follow this answer to receive notifications

answered Jul 25, 2020 at 11:11

Coop.ComputerCoop.Computer

1

2

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Explore related questions

See similar questions with these tags.