Mounting a Raspberry Pi's ext4 SD card on Ubuntu 14.04 inside VirtualBox on Mac OS X

Since I'm running a Mac, and don't have a spare linux-running machine that can mount ext4-formatted partitions (like those used by default for official Raspberry Pi distributions like Raspbian on SD cards), I don't have a simple way to mount the boot partition on my Mac to tweak files on the Pi; this is a necessity if, for example, you break some critical configuration and the Pi no longer boots cleanly.

To mount an ext4-formatted SD or microSD card on a Mac, the easiest option is to use VirtualBox (and, in my case, Vagrant with one of Midwestern Mac's Ubuntu boxes). Boot a new linux VM (any kind will do, as long as it's modern enough to support ext4), shut it down, go into Settings for the VM inside VirtualBox and enable USB, then reboot.

Follow these steps once the VM is booted, to mount the flash drive:

  1. Plug in the flash drive once the VM is booted.
  2. Click on the USB icon in VirtualBox's window, and select the flash drive to 'capture' it inside the VM.
  3. List all visible drives on the system: $ sudo fdisk -l, and it should be one of /dev/sdb1 or /dev/sdb2 (usually, whichever is larger, with 'System' of 'Linux'.
  4. Make a directory into which you'll mount the drive: $ mkdir /media/usb
  5. Mount the drive: $ sudo mount /dev/sdb2 /media/usb
  6. All finished! cd into /media/usb and browse the contents of the drive.

This process was tested with Ubuntu 14.04, but should work with most Linux distros.

Comments

Did you know that you can automatically delegate the usb device to the virtual server by adding this into your vagrant file?

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--usb", "on"]
    vb.customize ["modifyvm", :id, "--usbehci", "on"]
    vb.customize ["usbfilter", "add", "0", 
      "--target", :id, 
      "--name", "This is the identifier",
      "--serialnumber", "your usb serial number here"]
  end

you can get the serial number by typing VBoxManage list usbhost

Nice tip. Thanks. The mount didn't work on Ubuntu 2004 (laptop) to just browse SD cards from rpi4:
Steps taken:
1. boot laptop (Ubuntu 2004 native on Dell XPS 9310 developer edition)
2. Always see chip in microsd slot
3. used adapter to microsd from raspberry pi4
3.1 See df results: /dev/sda1 (/boot partition) is visible
3.2 the Linux partition on /dev/sda2 is not being mounted -- that is area I want to browse if I can mount it.
4. See commands below. Mount fails

I'll keep searching but thought I would share as is:

#// check to be sure there is a Linux partition to mount
root@entwash:/dev# df -h | grep -v loop | grep -v tmpfs
Filesystem Size Used Avail Use% Mounted on
udev 7.7G 0 7.7G 0% /dev
/dev/nvme0n1p3 930G 303G 580G 35% /
/dev/nvme0n1p1 811M 71M 741M 9% /boot/efi
/dev/mmcblk0p1 239G 201G 39G 84% /media/tim/9C33-6BBD <------ 256GM microsd always kept in slot for backups, etc.
/dev/sda1 253M 53M 200M 21% /media/tim/boot <------ from RPI4 first partition
root@entwash:/dev# ls -l /dev/sda* <--- confirm partitions and file system types
brw-rw---- 1 root disk 8, 0 Jun 23 21:16 /dev/sda
brw-rw---- 1 root disk 8, 1 Jun 23 21:16 /dev/sda1
brw-rw---- 1 root disk 8, 2 Jun 23 21:16 /dev/sda2
root@entwash:/dev#
root@entwash:/dev# fdisk -l /dev/sda <----- see /dev/sda2 below (confirms Linux partition x83)
Disk /dev/sda: 27.49 GiB, 29504831488 bytes, 57626624 sectors
Disk model: USB SD Reader
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: dos
Disk identifier: 0x6c586e13

Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 532479 524288 256M c W95 FAT32 (LBA)
/dev/sda2 532480 124735487 124203008 59.2G 83 Linux

# try 01
root@entwash:/dev# mkdir /media/usb
root@entwash:/dev# mount /dev/sda2 /media/usb
mount: /media/usb: wrong fs type, bad option, bad superblock on /dev/sda2, missing codepage or helper program, or other error.

# try #2
root@entwash:/dev# mkdir /mnt/123
root@entwash:/dev# ls -ld /mnt/123
drwxr-xr-x 2 root root 4096 Jun 23 21:27 /mnt/123
root@entwash:/dev# mount /dev/sda2 /mnt/123
mount: /mnt/123: wrong fs type, bad option, bad superblock on /dev/sda2, missing codepage or helper program, or other error.

#// summary - no luck
root@entwash:/dev# df -h | grep -v loop | grep -v tmpfs
Filesystem Size Used Avail Use% Mounted on
udev 7.7G 0 7.7G 0% /dev
/dev/nvme0n1p3 930G 303G 580G 35% /
/dev/nvme0n1p1 811M 71M 741M 9% /boot/efi
/dev/mmcblk0p1 239G 201G 39G 84% /media/tim/9C33-6BBD
/dev/sda1 253M 53M 200M 21% /media/tim/boot
root@entwash:/dev#