MacOS – Change Permissions on macOS USB Device

macospermissionusb

I need to find out how to set the permissions of a USB flash drive at the device (not filesystem) level. In other words, I need to grant the user read/write access to a USB device identified by diskutil which is /dev/diskX where X is some integer.

For example, I insert a USB flash drive and according to diskutil it's identifier is /dev/disk5. If I check the permissions of it:

$ ls /dev/disk5*
brw-r-----  1 root  operator    1,  15 May 15 08:35 /dev/disk5
brw-r-----  1 root  operator    1,  16 May 15 08:35 /dev/disk5s1
brw-r-----  1 root  operator    1,  17 May 15 08:35 /dev/disk5s2

It has root:operator ownership and rw access limited to root and r access to operators only (this is why you must issue commands that modify the device as sudo).

What I need to do is give rw access to the operator as well and it's not as simple as just chmod 660 /dev/disk5. The moment you unplug the device and plug it back in, it loses the permissions.

Why do I need to do this?

I need to boot a USB flash drive in VirtualBox. To do so, I need to be able to create a "raw" disk image as a passthrough to the actual USB device. The problem is, to do this, I must issue the command via sudo which then changes the user I am executing VBoxManage which causes a number of errors since root has an environment completely different than the logged in user.

This can be accomplished in FreeBSD (I am sure Linux as well via a different method) by modifying the devfs.rules file and adding in a line like:

add path 'da*' mode 0660 group operator

Which essentially gives rw access to any USB storage device (USB storage is identified by FreeBSD as /dev/da0, /dev/da1, etc.)

How do I do this on macOS?

Best Answer

I was never able to find a way to get direct access to the raw device, however, VirtualBox has a "raw disk access" feature that allowed me to get around this problem.

The way this works is a VMDK drive is created that is actually mapped to a physical volume. This acts as a passthrough so that VB can access a physical device.

VBoxManage internalcommands createrawvmdk -filename "</path/to/file>.vmdk" -rawdisk /dev/disk#

For example, if I had a physical USB device with an OS installer I could create a VMDK which I could attach to a VM.

Example:

Assume a USB installer with macOS/FreeBSD/Linux/Windows. To attach that physical USB to a VM (assuming /dev/disk3 is the USB device),

  1. Insert and unmount (not eject) the device.

    diskutil unmount /dev/disk3
    
  2. Create the VMDK

    sudo VBoxManage internalcommands createrawvmdk -filename "USB_OS_Installer.vmdk" -rawdisk /dev/disk3
    
  3. Take ownership of the VMDK (it was created as root via sudo; replace "user" with your username and leave the group "staff" as is)

    sudo chown user:staff USB_OS_Installer.vmdk
    
  4. Attach the VMDK to the VM in VirtualBox's GUI.

You now have access to a physical boot media device through your VM.