Use physical harddisk in Virtual Box

176,512

Solution 1

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


Straight forward instructions

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..

 

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

Solution 2

The VirtualBox documentation refers to accessing a raw hard disk:

http://www.virtualbox.org/manual/ch09.html#rawdisk

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.

Solution 3

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
  1. Change VirtualBox service account (credits to Kapodamy: https://unix.stackexchange.com/questions/284704/verr-access-denied-when-trying-to-add-a-raw-disk-to-virtual-box)
  • 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

Solution 4

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]
    

Solution 5

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!

Share:
176,512

Related videos on Youtube

Petah
Author by

Petah

All your base are belong to us!

Updated on September 18, 2022

Comments

  • Petah
    Petah over 1 year

    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?

  • Admin
    Admin almost 10 years
    Thanks very much for the clear step by step instructions. One problem I have had however is that the virtual .vmdk file can be owned by me as a user but when I start VirtualBox and it tries to access it an error is generated. This appears to be because the physical /dev/sda is owned by root. I tried changing ownership but no joy - still same error. Starting VirtualBox as root does work (though I'm always cautious about running any application as root, and one such as this with the potential for so much to go wrong scares me silly).
  • Ярослав Рахматуллин
    Ярослав Рахматуллин almost 10 years
    Most distros have a special group such as "disk" or "block". Bock devices in /dev/sd? should be writable by users belonging to one of those groups. Check the docs for your distro.
  • Meetai.com
    Meetai.com almost 10 years
    Here's a link to similar instructions: sysprobs.com/…
  • David Käriu III
    David Käriu III over 7 years
    Anybody got this working on Windows 10? I'm getting this error: The I/O cache encountered an error while updating data in medium "ahci-0-0" (rc=VERR_ACCESS_DENIED)., even when running VirtualBox as admin.
  • Epigene
    Epigene over 7 years
    @szx I managed to get around the VERR_ACCESS_DENIED problem following these steps
  • ens
    ens over 6 years
    On a Windows host, I additionally had to set the disk to offline via diskpart (offline disk)
  • Display Name
    Display Name almost 6 years
    @Epigene it could be that you missed the writethrough part
  • Wowfunhappy
    Wowfunhappy over 5 years
    How would one accidentally kill their data via raw disk access? I figure knowing what not to do would help avoid problems. The instructions look so straightforward...
  • William
    William over 5 years
    I got this working with linux(guest) and windows host but unfortunately a windows host and W10 guest throws a boot error.
  • noraj
    noraj about 5 years
    Instead of running Virtualbox as root to access the block device you can create a udev rule: wiki.archlinux.org/index.php/VirtualBox/…
  • user1742529
    user1742529 over 4 years
    And what about other formats except VMDK? serverfault.com/questions/991179/…
  • Yves Martin
    Yves Martin over 4 years
    For users getting this error: The I/O cache encountered an error while updating data in medium "ahci-0-0" (rc=VERR_ACCESS_DENIED), typically when using a partition of in-use disk, it is required to enable "Host I/O Cache" on storage controller: virtualbox.org/manual/ch05.html#iocaching
  • ceremcem
    ceremcem over 4 years
    Thanks for the crystal clear instructions. One suggestion would be that you should NOT use /dev/sdx in step-1, you should use /dev/disk/by-id/... instead (your disks serial number or wwn- here, check by ls -l /dev/disk/by-id | grep "/sd.$" | awk '{print $11" -> "$9}' | sort). This way your .vmdk files will be consistent between reboots while you keep your disks swapping around.
  • Daniel
    Daniel over 4 years
    This is a brilliant guide! I followed it to boot a drive on my PC and later the Mac I use daily! Please could you update your guide to include the disabling of SIP on MacOS, changing the owner of the unmounted drive chown and unmounting the external drive?
  • Gellweiler
    Gellweiler almost 4 years
    Worked like a charm I would also like to add that you should remember to tick the "Enable EFI (spcial OSes only)" box in the System settings for the VM if your computer is using UEFI.
  • Francuz
    Francuz almost 4 years
    Thank you very much. Just a little notice to help Windows's users. To show disk drives you can write in console VBoxManage internalcommands listpartitions -rawdisk \\.\PhysicalDrive0
  • Tomáš M.
    Tomáš M. over 3 years
    You could also create the VMDK file with the physical partition's UUID (/dev/disk/by-uuid/uuid-of-your-partition), which is exactly what it's used for, to prevent issues in case it changes on boot.
  • rostok
    rostok over 3 years
    I had similar problem but couldn't get past VERR_ACCESS_DENIED with VBoxManage command. Turns out I had VirtualBox GUI running that prevented adding the drive.
  • Coop.Computer
    Coop.Computer over 3 years
    I've not been able to make that work with Windows 10. The drive has two partitions, and using the UUID method won't let it boot unless it can see both partitions.
  • A_Elric
    A_Elric about 3 years
    Here in 2021, this answer still fucks <3
  • Dwight Schrute
    Dwight Schrute over 2 years
    @ЯрославРахматуллин: I am trying to access SD card on my guest OS(Ubuntu 20.04). On Host(OS) I am able to add, delete and modify files on SD card. But on guest OS I can view files but cannot modify them. I am almost but not 100% sure that Windows is preventing guest OS to access the SD card. I simply do not know the mechanism by which it is doing it. I am really curious about it. Can you shed some light on it? My full saga is over here: superuser.com/questions/1678065/…
  • jave.web
    jave.web over 2 years
    @ЯрославРахматуллин have you tried / were you able to run a defragmentation from the virtualized OS? Or is this just for the common file/folder read/write?
  • Ярослав Рахматуллин
    Ярослав Рахматуллин over 2 years
    @jave.web there are no restrictions on what you can do with the device from the guest OS afaik. I had no reason to defragment the drive, but I made partitions on it and formatted filesystems from the guest (the guest was linux and the host windows in my case). It's just a "normal" disk as far as the guest OS is concerned. Why are you asking about this one specific operation?
  • jave.web
    jave.web over 2 years
    @ЯрославРахматуллин because of the infamous problem on how to defragment NTFS filesystem disk on Ubuntu(or Linux in general)
  • Dean MacGregor
    Dean MacGregor about 2 years
    Does windows 10 not complain that the hardware footprint changes or does VirtualBox emulate the host computer to the virtualized windows os?
  • Ярослав Рахматуллин
    Ярослав Рахматуллин about 2 years
    @DeanMacGregor the guest is not win 10, also it's kinda outside of the scope of this question/answer. I'd say you need to ask MS Support about that. There are even dedicated MS stack websites / forums. Based on what I know, if you have a "full copy/licence", it does not matter. There are also "free" images of windows for testing / development compatible with virtualbox that come with their own virtual disks which could probably be mapped onto a physical drive... idk
  • Admin
    Admin about 2 years
    Make sure that VirtualBox.exe and VBoxSVC.exe are run administrator.