Mount external flash drive read-only on Mac OS X
- April
- 22
5:25 pm Mac
This post came about because of the hard time I had hunting down information on the topic. So I'm sharing what I've learned in the hope that others may be helped by it.
I own several Garmin GPS devices and notoriously I will have one dangling from my laptop and it will become unplugged accidentally without properly closing the filesystem and ejecting. I've had a few issues with file corruption on these devices and I decided to eliminate the possibility that these unintentional unplugging events had something to do with it.
I looked around on the net and there were a few utilities which claimed to be able to force read-only mounting of certain drives but I would rather not depend on a software program that has to watch for events like USB insertion in the kernel. It's just one more driver that needs to be upgraded when a new kernel comes out. So I settled on doing it the old-fashioned way.
The first step is to name your drive so you can identify it properly when it's inserted. If every drive you have comes up as "Untitled" it's not going to be easy to detect when a particular drive is on the system. My Garmin Edge 800 comes up as two drives, one named "GARMIN" and one named "NO NAME" which is the micro flash card. I always store my data on the main device leaving the micro flash card for maps only. I also have an Edge 500 which comes up with a single drive named "GARMIN." Recently I purchased the Garmin Virb Elite camera and put a 64 GB micro flash drive in it. It came up as "Untitled" initially but I simply renamed it to VIRB through the GUI and it is still detected properly by the Garmin software so all is well.
Next, you want to modify /etc/fstab as root. Be careful or you can mess up your boot drive and possible render your machine unbootable if you're not careful. But this is simply a text file that tells OS X what to do when a particular volume is mounted. My /etc/fstab was empty so I just added what I needed. One of the parameters you can specify for a mounted drive is whether it is read/write or read-only. That makes it easy to get the system to mount the drives the way we want.
Here is a copy of my fstab with the entries I had to add in. Note that there are tab characters between fields.
# Identifier, mount point, fs type, options
LABEL=GARMIN none msdos ro
LABEL=NO\040NAME none msdos ro
LABEL=VIRB none exfat ro
One thing to note is that the last entry's fs type is exfat. That's not documented anywhere I could find. I had to guess to find the right entry to put there. My Garmin VIRB Elite camera uses a flash card that is formatted EXFAT (64 bit FAT) so that there are no large file problems. The other two devices use normal 16 bit FAT so you use the 'msdos' file type.
Now when you plug in the device you should be able to see it mounting read-only via tha mount command. You'll see your other disk partitions in the output as well.
$ mount
/dev/disk1 on / (hfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
localhost:/9A5XAFt6LFmeQe3kcbu0RD on /Volumes/MobileBackups (mtmfs, nosuid, read-only, nobrowse)
/dev/disk2s1 on /Volumes/VIRB (exfat, local, nodev, nosuid, read-only, noowners)
Notice the last line fir the Virb camera includes the read-only parameter.
Now this is all neat but what if you want to erase something on the camera or write a file? I normally purge the old activities and videos off and I upload courses to the Garmin Edge 800. That can be accomplished with a script. I wrote one script to set the devices read-write and one to set them back to read-only. That way I can run the script, write a file or erase something and then run the read-only script. There is one caveat though. The Mac GUI doesn't seem to get informed when you unmount and remount the filesystem read-write. So you can't use it to copy files. I just do it from the command line instead, no big deal.
Here is the garmin-rw script:
#!/bin/sh
# garmin-rw.sh
#
if [ -d /Volumes/GARMIN ]; then
DISK=`mount | grep GARMIN | awk '{ print $1 }'`
sudo mount -uw -o nodev,nosuid $DISK /Volumes/GARMIN
fi
DISK=""
if [ -d /Volumes/NO\ NAME ]; then
DISK=`mount | grep "NO NAME" | awk '{ print $1 }'`
sudo mount -uw -o nodev,nosuid $DISK /Volumes/NO\ NAME
fi
DISK=""
if [ -d /Volumes/VIRB ]; then
DISK=`mount | grep "VIRB" | awk '{ print $1 }'`
sudo mount -uw -o nodev,nosuid $DISK /Volumes/VIRB
fi
Here is the garmin-ro script:
#!/bin/sh
# garmin-ro.sh
#
if [ -d /Volumes/GARMIN ]; then
DISK=`mount | grep GARMIN | awk '{ print $1 }'`
sudo mount -ur -o nodev,nosuid $DISK /Volumes/GARMIN
fi
DISK=""
if [ -d /Volumes/NO\ NAME ]; then
DISK=`mount | grep "NO NAME" | awk '{ print $1 }'`
sudo mount -ur -o nodev,nosuid $DISK /Volumes/NO\ NAME
fi
DISK=""
if [ -d /Volumes/VIRB ]; then
DISK=`mount | grep "VIRB" | awk '{ print $1 }'`
sudo mount -ur -o nodev,nosuid $DISK /Volumes/VIRB
fi
For quicker ejecting I also wrote a shell script to eject the drives. Works much better than right clicking on the icon.
#!/bin/sh
# garmin-eject.sh
#
if [ -d /Volumes/GARMIN ]; then
DISK=`mount | grep GARMIN | awk '{ print $1 }'`
sudo umount $DISK
fi
DISK=""
if [ -d /Volumes/NO\ NAME ]; then
DISK=`mount | grep "NO NAME" | awk '{ print $1 }'`
sudo umount $DISK
fi
DISK=""
if [ -d /Volumes/VIRB ]; then
DISK=`mount | grep "VIRB" | awk '{ print $1 }'`
sudo umount $DISK
fi
For the record I am using Mac OS X 10.9.2. I am not sure of the existence of the exfat file system type in other versions.
« Racedots | Chicken Vindaloo - Slow Cooker » |