8

So I am trying to set up raw device access w/ VirtualBox to use a ext4/linux disk as a guest OS within Mac OS X, but haven't succeeded yet.

Following the VirtualBox docs, Chapter 9.9.1.1, https://www.virtualbox.org/manual/ch09.html#rawdisk will Mac OS X allow raw disk access for VMs? How do I set this up?

Things done so far: (don't try this at home)

Created a raw vmdk file:

$ sudo VBoxManage internalcommands createrawvmdk -filename /Users/me/VirtualBoxVMs/Xub_raw_disk.vmdk -rawdisk /dev/disk0
RAW host disk access VMDK file /Users/me/VirtualBoxVMs/Xub_raw_disk.vmdk created successfully.

make me the owner:

$ sudo chown me /Users/me/VirtualBoxVMs/Xub_raw_disk.vmdk

see if VirtualBox can see anything on the Xub_raw_disk.vmdk file:

$ VBoxManage internalcommands listpartitions -rawdisk /Users/me/VirtualBoxVMs/Xub_raw_disk.vmdk
Number  Type   StartCHS       EndCHS      Size (MiB)  Start (Sect)

Since nothing shows up here for sectors or partitions, this must be a symptom of the problem...

Problem is when I try to add the .vmdk file from the GUI or with the command below, I get an error:

$ VBoxManage storageattach "Xub_raw_testing" --storagectl "SATA" --port 0 --type hdd --medium /Users/me/VirtualBoxVMs/Xub_raw_disk.vmdk 
VBoxManage: error: Could not find file for the medium '/Users/me/VirtualBoxVMs/Xub_raw_disk.vmdk' (VERR_FILE_NOT_FOUND)
VBoxManage: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component Medium, interface IMedium, callee nsISupports
VBoxManage: error: Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 178 of file VBoxManageDisk.cpp
VBoxManage: error: Invalid UUID or filename "/Users/me/VirtualBoxVMs/Xub_raw_disk.vmdk"

UPDATE: Here, disk0 is the dedicated linux drive. disk1 (not used) is for Mac OS X. Making myself the owner of the /dev/disk0 and /dev/disk0s2 with

$ chown $USER /dev/disk0
$ chown $USER /dev/disk0s2

per the Froggard howto made it so I could add the image to a VM. Also, /dev/disk0 is now readable from VBoxManage:

$ VBoxManage internalcommands listpartitions -rawdisk /dev/disk0
Number  Type   StartCHS       EndCHS      Size (MiB)  Start (Sect)
1       0x00  0   /0  /0   0   /0  /0            976         2048
2       0x00  0   /0  /0   0   /0  /0         227959      2000896

Now, when I start the linux guest OS, I get the FATAL: No bootable medium found! System halted. message. Any ideas what's wrong?

3
  • Did you check if the SATA port 0 is already in use in your VM? Or did you already try to select a different controller like the IDE Controller?
    – klanomath
    Jun 19, 2015 at 16:23
  • If OS X is running on your internal disk0, then you can not get access to the entire disk 0. However, you can get access to the partition containing ext4 formatted file system. Follow steps 6 through 12 given at Froggard's answer. He does this for a Windows formatted partition. Just substitute your Linux partition. Jun 19, 2015 at 18:46
  • What operating system are you running in the physical machine? What physical partition is this machine installed? What operation system will you be running in the virtual machine? Will the virtual machine be installed in a virtual or physical partition. If physical, which one? Jun 19, 2015 at 22:46

3 Answers 3

9

I had a need to do this today and I saw your question, so here is a protocol of the steps I took.

Find the Device Number

diskutil list e.g. 2, as in /dev/disk2 Assuming 2 for the rest of the instructions.

Create the Virtual Machine VMDK

VBoxManage internalcommands createrawvmdk -filename "$HOME/dev-disk2.vmdk" -rawdisk /dev/disk2

Eject the device before this step, otherwise you will get an error: VERR_RESOURCE_BUSY. You may want to add an entry in fstab with the added parameters noauto to prevent the OS from remounting the drive.

I use rdisk-disk2.vmdk for the name, because this virtual machine is just a pointer to this device. After reboot, the situation could change e.g. a device might be assigned a different number especially if you unplug things and move things around.

Start Virtual Box Manager with Root Privileges

sudo /Applications/VirtualBox.app/Contents/MacOS/VirtualBox

Because devices are all owned by root, you need to start VirtualBox with root privileges. This has consequences. Your home context (home folder and preferences) will be changed to root. Your other virtual machines will not be visible. If you open a file browser, you will see the root's home folder by default.

Another option is to change the permissions of the device (not recommended) as you have done.

To confirm permissions of devices, you can run ls -l /dev/disk*

  1. Add New Machine, provide name: /dev/disk2, type: linux, version: arch linux (64-bit)
  2. Give it some memory (whatever you can)
  3. Select existing drive using VMDK created above. (IMPORTANT ensure the drive has not been remounted, otherwise you will get VERR_RESOURCE_BUSY, NS_ERROR_FAILURE)

Install a Fresh System

Add an optical disk file (e.g. a bootable linux ISO of your liking) and turn on the Live CD/DVD optional so that you boot from it.

Make System Bootable in VirtualBox

This is not so obvious, especially if you use an EFI setup. If using grub-mkconfig, the name of the efi file is grubx64.efi (assuming 64 bits). VirtualBox is finicky about where it looks for a boot file. The default will not do.

Rename grubx64.efi to bootx64.efi

/boot/EFI/BOOT/bootx64.efi


Notes

  • tip: get yourself iterm 2
  • This is super useful for Macbooks when installing Arch Linux, because the default ISO does not load the wlan card drivers.
6
  • I still get VERR_NOT_SUPPORTED and VERR_ACCESS_DENIED when doing this, even with the disk permissions set to 777 and with VirtualBox running as root.
    – user260616
    Sep 5, 2019 at 14:00
  • @BeanstheWizard May I suggest you verify that your disk is indeed not mounted? It might be worth setting up /etc/fstab to block mounts. Sep 5, 2019 at 14:01
  • I am sure; wouldn't it give a different error anyways?
    – user260616
    Sep 5, 2019 at 14:05
  • One might expect that. No, the error feedback is rubbish. That is why it induces headaches. And thats why I posted this answer. Sep 5, 2019 at 14:15
  • Enabling "Use Host I/O Cache" fixed it, for some reason.
    – user260616
    Sep 12, 2019 at 0:51
1

I have made a small script for fixing the permission and that creates a new raw image with the correct UUID:

#!/bin/bash

UUID="BDA52A9B-E398-4245-AF6B-6C274F54D4BD"
VBOXFILE=Linux.vmdk

DEVICE_IDENTIFIERIDENTIFIER=$(diskutil info $UUID | grep "Device Identifier:" - | grep -o "disk." - )
DISK="/dev/$DEVICE_IDENTIFIERIDENTIFIER"

echo "using $DISK"

if test -f "$VBOXFILE"; then
    rm $VBOXFILE
fi

sudo chown $USER $DISK
sudo VBoxManage internalcommands createrawvmdk -filename $VBOXFILE -rawdisk $DISK
sudo chown $USER $VBOXFILE

VBoxManage internalcommands sethduuid Linux.vmdk "$UUID"
0

I did more or less what you want: to access the disk in raw mode as Linux boot.

mac2011% diskutil list

Identify the disk to access with VirtualBox. Here it is /dev/disk1, containing two partitions /dev/disk1s1 /dev/disk1s2 (one Windows and one Linux).

Unmount:

mac2011% diskutil unmountDisk /dev/disk1 

Change permissions:

mac2011% sudo chown myself /dev/disk1*

Create raw virtual machine disk :

mac2011% VBoxManage internalcommands createrawvmdk -filename /Users/myself/VirtualBox\ VMs/link.vmdk -rawdisk /dev/disk

Open VirtualBox:

mac2011% open -a VirtualBox

Create a virtual machine using "an existing virtual hard disk file", actually choose the one just created. Click on Add... Create...

Often an error occurs "could not open the medium VERR_RESOURCE_BUSY", because the Mac has automatically remounted (check the desktop), so unmount and try again (use shell history or write a script or sudo vifs /etc/fstab -- and look elsewhere how to fill it).

The virtual machine is shown [Running] so probably it has accessed the disk. (However, the boot process hangs but this is another question.)

Hopefully you will be able to adapt this recipe to your need.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .