Directly mount disk in virtualbox vm
At some point I came across the need to use a Linux VirtualBox VM to dd to SD card inserted into my mac. You might need this to dd an image of an OS or just access the disk directly for what ever reason maybe formatting? Eg. Format a disk in EXT using a Linux VM on a mac.
First insert the SD card and check where it has been mounted using the following commands,
1 |
sudo diskutil list |
1 2 3 4 5 6 7 8 9 10 |
/dev/disk0 #: TYPE NAME SIZE IDENTIFIER 0: GUID_partition_scheme *251.0 GB disk0 1: EFI EFI 209.7 MB disk0s1 2: Apple_HFS Macintosh HD 250.1 GB disk0s2 3: Apple_Boot Recovery HD 650.0 MB disk0s3 /dev/disk1 #: TYPE NAME SIZE IDENTIFIER 0: FDisk_partition_scheme *31.9 GB disk1 1: DOS_FAT_32 CUBOX 31.9 GB disk1s1 |
The first disk0 being your hard drive and the second disk1 being the SD card you can gernerally tell by the size and by referencing the Disk Utility app for mac. WARNING **If you are unsure DO NOT continue you may cause your computer to become unusable requiring a reinstall of the OS**
An issue with Mavericks and I believe Mountain Lion is that these disks get mounted as root:operator which means your user cannot modify them so we’ll need to change the owner of these devices and verify with ls;
1 2 3 4 5 6 7 8 9 |
➜ ~ sudo chown jamesh /dev/disk1* ➜ ~ ls -al /dev/disk* brw-r----- 1 root operator 1, 0 13 Apr 16:33 /dev/disk0 brw-r----- 1 root operator 1, 1 13 Apr 16:33 /dev/disk0s1 brw-r----- 1 root operator 1, 2 13 Apr 16:33 /dev/disk0s2 brw-r----- 1 root operator 1, 3 13 Apr 16:33 /dev/disk0s3 brw-r----- 1 jamesh operator 1, 7 16 Apr 09:01 /dev/disk1 brw-r----- 1 jamesh operator 1, 8 16 Apr 09:17 /dev/disk1s1 |
Once that is done we can create the Virtualbox Rawdisk using VBoxManage internalcommands
1 |
vboxmanage internalcommands createrawvmdk -filename disk2.vmdk -rawdisk /dev/disk1 |
If you get an error “VERR_RESOURCE_BUSY” like below its because the disk you are using has a partition that has been mounted. You can “unmount” the partition in Disk Utility just don’t “Eject” the whole disk.
1 2 3 4 5 |
➜ ~ vboxmanage internalcommands createrawvmdk -filename disk1.vmdk -rawdisk /dev/disk1 VBoxManage: error: VMDK: could not open raw disk file '/dev/disk1' VBoxManage: error: Error code VERR_RESOURCE_BUSY at /Users/vbox/tinderbox/4.3-mac-rel/src/VBox/Storage/VMDK.cpp(3388) in function int vmdkCreateRawImage(VMDKIMAGE*, VBOXHDDRAW*, uint64_t) VBoxManage: error: Cannot create the raw disk VMDK: VERR_RESOURCE_BUSY VBoxManage: error: The raw disk vmdk file was not created |
At any point during this tutorial (Even when starting the VM) if you get a disk busy the partition has been mounted, just unmount it.
Now that we have created the raw vmdk we can attach it to the VM and we are done.
Open Virtualbox, right click your machine -> Settings -> Storage -> “Add Hard Disk” icon on the SATA Controller -> Select your disk1.vmdk file or what ever you called it. OK -> OK -> and your Done! Start your VM and you should be able to see the disk for linux sudo fdisk -l should show you the same disk parameters (size partitions etc) as diskutil list (/dev/sdb is the SD card from earlier)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
jamesh@jamesh-ubuntu:$ sudo fdisk -l Disk /dev/sda: 21.0 GB, 20971520000 bytes 255 heads, 63 sectors/track, 2549 cylinders, total 40960000 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 Disk identifier: 0x000efbc8 Device Boot Start End Blocks Id System /dev/sda1 2048 39847935 19922944 83 Linux /dev/sda2 39847936 40959999 556032 5 Extended /dev/sda5 39849984 40959999 555008 82 Linux swap / Solaris Disk /dev/sdb: 31.9 GB, 31914983424 bytes 255 heads, 63 sectors/track, 3880 cylinders, total 62333952 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 Disk identifier: 0x00000000 Device Boot Start End Blocks Id System /dev/sdb1 8192 62333951 31162880 b W95 FAT32 |
Thanks for this tutorial.
When I try to open the file in Virtualbox -> storage, the file appears greyed out (as in no vmdk types allowed). Then when I select All files, I get:
Failed to open the optical disk file /file.vmdk.
Permission problem accessing the file for the medium ‘/file.vmdk’ (VERR_ACCESS_DENIED).
Result Code: VBOX_E_FILE_ERROR (0x80BB0004)
Component: Medium
Interface: IMedium {05f2bbb6-a3a6-4fb9-9b49-6d0dda7142ac}
Callee: IVirtualBox {fafa4e17-1ee2-4905-a10e-fe7c18bf5554}
Callee RC: VBOX_E_OBJECT_NOT_FOUND (0x80BB0001)
Version: 4.3.28
Host: Yosemite
Thanks again for any help.