How to edit a microSD card or *.IMG file on your workstation


If you're like me and you've put your microSD (from the Raspberry Pi) into an SD adapter sleeve and then into your MacBook, you see that the boot partition has mounted nicely and you can even edit the files on there.

But what if you wanted to adjust things on the next partition, the one that's associated with root?

Or, what if you've made an IMG file of your entire microSD (using something like Etcher or ApplePi-Baker) and you'd like to edit that second partition there in place in the file (without first burning it to media)?

Or what if, like me, you have a pile of microSD cards each with different projects on them and you need to figure out what this one is? I'll take you through this scenario below to show you how easy it can be.

The bad news

The bad news is that the second partition of a Raspbian install uses a file format that isn't readily supported on OSX without paying for a driver. Regardless of any hack you've heard about, attempting to mount it directly on a Mac will likely result in media or image corruption and I say this from experience.

The good news

Fortunately, Ubuntu does a great job of this and it's free as an operating system. You don't technically have to install it over your hard drive or even to move your partitions around; Ubuntu has a LIVE version in which you simply boot to CD or USB drive. (Make sure that you get the Desktop version.)

Assuming that you're now in an Ubuntu session

So, you've now booted your MacBook with either an Ubuntu Live CD or USB, continue...

Editing directly on the microSD media

  1. Insert the microSD card into a USB type of adapter
  2. Insert the USB adapter then into your MacBook
  3. In many cases, Ubuntu's Desktop version will just auto-mount BOTH partitions for you
  4. Edit anything you need to either from the Terminal or from the File Manager
  5. Find the /etc/hostname file and you can find out the name of the Raspi if it had booted

Don't forget to safely eject those partitions and wait for the dialog box informing you that it's now safe to remove it.

Just remember that you're in Ubuntu at the moment and not Raspbian so if you're developing in this operating system, you still need to test things in Raspbian.

Editing directly an IMG file

  1. If you've copied the octopi.img file, say, from earlier to a USB drive, insert this into the MacBook and it should auto-mount
  2. Open up a Terminal (Ctrl-Alt-T)
  3. cd /media
  4. Look for your own username and cd me into that (this is where auto-mount decides to mount things for you)
  5. ls
    In my case, I see boot and 3598e-09be-47ef-9d01-f24cf61dff1d
  6. (In my case, I'll just type the first character of that to save typing all that). cd 3*
  7. ls
    And there is your IMG file.

So now, this part gets technical.

  1. file octopi.img
CHS (0x0,130,3), end-CHS (0x8,138,2), startsector 8192, 129024 sectors; partition 2 : ID=0x3, start-CHS (0x8,138,3), end-CHS (0x213,120,37),
startsector 137216, 8400896 sectors

You're interested in the value after startsector at the end of all that (not the first time seen): 137216. Multiply this by 512 to get 70254592.

  1. sudo mount octopi.img -o offset=70254592 /mnt
  2. sudo ls /mnt

You should now see the entire set of folders from the root area of that image file (the second partition).

When finished, don't forget to safely eject the mounted system:
sudo umount /mnt

If you do this a lot and you actually have Ubuntu installed, then create a mnt folder under your home directory and mount it there instead. Then you won't have to use sudo just to list the files and to work with it.

mkdir ~/mnt
sudo mount octopi.img -o offset=70254592 ~/mnt
cat ~/mnt/etc/hostname # Didn't have to use sudo this way
sudo umount ~/mnt


A much easier way to get offsets of an image file is to use

sudo parted image-file

In parted:
unit b

Now you have booth offsets for /boot and /fsroot and you can use

mount -o loop,offset="the offset" "image-file" "mount point"

If using parted you reduce the need to calculate everything yourself and you get booth offsets at the same time.