10.5: Mount a partition or volume on demand

Aug 12, '09 07:30:00AM

Contributed by: tedw

I have a portable drive that I've divided into two partitions: an HFS+ partition for backups and extra storage, and an MSDOS partition so that I can transfer large files or folders to or from Windows boxes. However, I don't need the second partition very often, so I wanted a way to keep it from automatically mounting on my machine when I plug in the drive, while still being able to access it quickly and easily. This turned out to be reasonably easy to do.

First, to prevent the partition from mounting when the disk as a whole mounts, edit/create the fstab system file, which lives down in the root folders at /etc/fstab. Leopard does not seem to have this file by default; it does have a file /etc/fstab.hd, which is not the file you want (though it makes for interesting reading in a Hitchhikers Guide to the Galaxy sort of way). Use a plain-text (i.e. programmer's) text editor such as TextWrangler, Smultron, or Editra, or use a unix tool like vi, and create a file called fstab with the following contents:

LABEL=PARTITION_NAME none msdos rw,noauto
Fields on this line are separated by tabs or spaces, so be careful to escape any spaces in your partition name. The escape character for a space seems to be \040 (octal 40) -- see the example in the man page for fstab.

The four fields specify:

  1. Partition name: use LABEL= and then the partition name. For a Mac-formatted partition, you should use UUID= and then its actual UUID, which you can find using diskutil or Disk Utility.
  2. Mount point: use none to let the system decide, unless you really know what you're doing.
  3. Volume type: msdos in this case, or hfs+, nfs, etc., as required.
  4. Option flags: rw specifies that the volume is read/write (you could use ro for read-only, but you need to give some value for the file system type). noauto does the actual work -- it tells diskarbitrationd not to mount the partition automatically.
Once this file is moved to the correct place (which will generally require an admin password), the partition will be registered in the system when the disk is connected, but will not mount. Other partitions on the same disk will mount normally. To get the disk to mount when you need it, use the following bash script:
#! /bin/bash
dev_id=`diskutil list | awk '/PARTITION_NAME/{ print $6; }'`
[[ $dev_id ]] && diskutil mount $dev_id
The dev_id line recovers the partition's device ID, and then (if the drive is available), the next line gets diskutil to mount it. Save this script with a .sh extension and run it from the script menu, QuickKeys, Butler, or other script launcher; or save it with a .command extension and run it by double-clicking it.

Of course, this could be expanded to other uses. Examples: keep an internal partition off-line until you need it; mount a partition as read-only for safety and switch it to read/write when desired; leave a drive plugged in but mount/unmount volumes programmatically (using launchd to set appropriate times or conditions). If you try them, let us know how it goes via the comments.

[robg adds: This hint was submitted as Leopard-only, so I've left it marked that way. fstab obviously predates 10.5, but I can't test this hint on a pre-10.5 system, so I don't know if it works as written on 10.4, etc.]

Comments (33)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090811124320441