How to access raw disk partitions from a Linux guest on VirtualBox Windows host.

  • Root file system from a dual boot partiton, for Booting Linux within Virtual Box on windows.
  • Accessing an SD Card as a raw image.

Booting Linux

This page captures a method that works:

A VirtualBox disk file needs to be created, with a reference to a raw disk and a partition. This needs to be done as administrator, and any virtual machine using the disk needs to be run as administrator.

  • Use Computer Management/Disk management to the find partition number.
    • (In this example Partition 4)

    or wmic

>wmic partition get name,diskindex,index,size

On my system that gives:

DiskIndex  Index  Name                   Size
1          0      Disk #1, Partition #0  272629760
1          1      Disk #1, Partition #1  259592695808
1          2      Disk #1, Partition #2  565182464
1          3      Disk #1, Partition #3  264662679552

Computer management gives:

Disk Management

  • The Linux root filesystem is (Disk 1, Partition 5) in the GUI.
    • WMIC lists this as DiskIndex 1, Index 3.
    • The corrosponding parameters for createrawvmdk are:
      • -rawdisk \.\PHYSICALDRIVE1
      • -partitions 5
  • Open and Admin CMD.exe:
cd C:\Program Files\Oracle\VirtualBox

Then

VBoxManage internalcommands createrawvmdk 
     -filename "C:\Users\xxxx\VirtualBox VMs\rawdisk.vmdk" 
     -rawdisk \\.\PHYSICALDRIVE1 
     -partitions 5
     -relative

or

VBoxManage internalcommands createrawvmdk 
    -filename "C:\Users\xxxx\VirtualBox VMs\rawdisk1.vmdk" 
    -rawdisk \\.\PHYSICALDRIVE1 
    -partitions 5

The -relative option creates a disk description that references the partition file. Otherwise the offset to the partition is set in the vdmk file. The guest still sees the same partition table in both cases, with the other partitions not available. It feels a bit safer…

If you get it wrong, the command will give you an error.

e.g. Trying to make a raw disk image for the windows partition :

VBoxManage internalcommands createrawvmdk      -filename "C:\Users\xxxx\VirtualBox VMs\Ubuntu\rawdisk2.vmdk"      -rawdisk \\.\PHYSICALDRIVE1      -partitions 3      -relative
VBoxManage.exe: error: VMDK: could not create new file 'C:\Users\xxxx\VirtualBox VMs\Ubuntu\rawdisk2.vmdk'
VBoxManage.exe: error: Error code VERR_ALREADY_EXISTS at F:\tinderbox\win-5.2\src\VBox\Storage\VMDK.cpp(3455) in function int __cdecl vmdkCreateRawImage(struct VMDKIMAGE *,struct VDISKRAW *const ,unsigned __int64)
VBoxManage.exe: error: Cannot create the raw disk VMDK: VERR_ALREADY_EXISTS
VBoxManage.exe: error: The raw disk vmdk file was not created
With -relative
rawdisk.vmdk
Without -relative
rawdisk1.vmdk

# Disk DescriptorFile
version=1
CID=5b1493a5
parentCID=ffffffff
createType="partitionedDevice"

# Extent description
RW 63 FLAT "test-pt.vmdk" 0
RW 1985 ZERO 
RW 532480 ZERO 
RW 32768 ZERO 
RW 508123136 ZERO 
RW 516919296 FLAT "\\.\Harddisk1Partition4" 0
RW 1007 ZERO 
RW 33 FLAT "test-pt.vmdk" 63


# Disk DescriptorFile
version=1
CID=ef983b49
parentCID=ffffffff
createType="partitionedDevice"

# Extent description
RW 63 FLAT "rawdisk1-pt.vmdk" 0
RW 1985 ZERO 
RW 532480 ZERO 
RW 32768 ZERO 
RW 508123136 ZERO 
RW 516919296 FLAT "\\.\PHYSICALDRIVE1" 508690432
RW 1007 ZERO 
RW 33 FLAT "rawdisk1-pt.vmdk" 63

Accessing an SD Card

The whole disk image will be made available in this case. The wmic command can show the information we need. (or Computer Management/Disk management could be used.)

C:\>wmic diskdrive list brief /format:list

This shows:

... other disks ...

Caption=SDXC Card
DeviceID=\\.\PHYSICALDRIVE2
Model=SDXC Card
Partitions=1
Size=124719920640

Open and Admin CMD.exe:

cd C:\Program Files\Oracle\VirtualBox

Then

VBoxManage internalcommands createrawvmdk
          -filename "C:\Users\xxxx\VirtualBox VMs\SDCARD.vmdk" 
          -rawdisk \\.\PhysicalDrive2 

Attach the disk to SATA port 1 and it will be available as /dev/sdb. (in this case it has the Firefly RK3399 Ubuntu system image.)

  • The settings needs to be updated
$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sdb: 116.2 GiB, 124721823744 bytes, 243597312 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 34C89256-CF83-4475-A7CF-48F7BDA34490

Device      Start       End   Sectors   Size Type
/dev/sdb1  114688 243595263 243480576 116.1G Microsoft basic data