Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.5: Mount a partition or volume on demand System 10.5
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.]
    •    
  • Currently 2.11 / 5
  You rated: 2 / 5 (9 votes cast)
 
[24,881 views]  

10.5: Mount a partition or volume on demand | 33 comments | Create New Account
Click here to return to the '10.5: Mount a partition or volume on demand' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Mount a partition or volume on demand
Authored by: pub3abn on Aug 12, '09 08:03:21AM

This solution sounds nice. An alternative solution, for those who like GUI interfaces, is to use Lingon (http://sourceforge.net/projects/lingon/)

In Lingon, create a User Daemon.

You will need to give it a name. Use something like the following (it needs to be in the reversed hostname format, but otherwise it can be anything you want):

com.yourusername.UnmountMyDisk

The command will take the form:

/usr/bin/hdiutil unmount /dev/disk1s10

...where "disk1s10" is the name of the disk. Alternatively, in place of "/dev/disk1s10", I believe you can use the format "/Volumes/volumename" ... but I have not tried that.

Then tick the checkbox for "Run it when it is loaded by the system" (or "RunAtLoad" in older versions of the app). And then save.

This will run at startup, before anyone logs in. Seems to work great, and as I use Lingon for other things, it fits well in my workflow. I know I won't forget where the setting is or how to change it.

[ Reply to This | # ]

10.5: Mount a partition or volume on demand
Authored by: pub3abn on Aug 12, '09 08:07:15AM

I failed to mention that Lingon is simply a GUI for editing OS X's launchd daemon. It does not add any new background tasks or hacks to your system.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: mmnw on Aug 12, '09 08:57:57AM

Your solution will only work for disks connected at startup, not for USB, Firewire or external SATA devices connected afterwards. Also, the device will be mounted, and then unmounted. I don't know, but eventually OS X will work on the drive before the unmount command is executed (i.e. TimeMachine or Spotlight).

I should also add: you can only use the /dev/disk** part with internal drives, not with externals. The reason being the actual disk number may change depending on the port the disk is connected to and the order in which the disks are connected.

You can use the /Volume/Partition_Name for unmounting (I have tested it). But what you should use is the UUID of the partition, which can be determined with diskutil info.

Also, you probably shouldn't use hdiutil (I know, it works ...), but diskutil as this hint suggests. hdiutil was made to manage disk images (like a dmg), diskutil to manage actual disks. Read the man hdiutil and man diskutil for further information.

So the command would actually look like this:

/usr/bin/diskutil unmount UUID-number


[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: pub3abn on Aug 12, '09 12:46:55PM

Thanks, that was helpful.

In my case, I am unmounting a partition of an internal drive. I don't know about Spotlight, but it seems that Time Machine is only active when a user is logged in. In any case, we've never had any problems with corruption or failure to unmount, and I've been using that unmount technique for probably a year or more. But if I get around to tinkering with it again, I may try the alternate command you suggested.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: mmnw on Aug 13, '09 03:27:00AM

I also am not sure if it can cause problems. Guess it won't. Since you are unmounting it grafecully without force.
But it could take longer than not mounting it at all. I'm pretty sure spotlight starts it's indexing process even if no user is logged in. Also I don't know if spotlight is already running at this point of boot.
So in the end, it's just a matter of elegance. Not mounting seems more "right" to me than mounting/unmounting ;)



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: mmnw on Aug 12, '09 09:03:51AM

I'd like to add something:

You don't need to determine the dev_id of the drive. Since you already know the UUID of the partition for the first part of the hint you can also use the UUID to mount the partition. Just use

#! /bin/bash
/usr/bin/diskutil mount UUID

where UUID is this weird long number assigned to the partition, which you can get, btw, by typing

diskutil info /Volume/PartitionName


[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 12, '09 12:28:18PM

the main problem is that UUIDs only apply (so far as I can tell) to volumes formatted with HFS and HFS+. Windows-compatible volumes don't seem to have them, and I don't know about NFS or other formats (since I haven't made any to test). if you're only working with Apple file systems, then yes, UUIDs are certainly better and easier, but I don't think it generalizes.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 12, '09 01:18:36PM
ah, wait, I see what you're saying: since you've already but the LABEL or UUID into the fstab file, then you might as well just put it directly into diskutil's mount command without searching for it, right? The reason I did it this way, though, is to handle cases where the script gets run while the disk isn't mounted: the dev_id variable will be the null string if the disk is not there, and so the script will fail gracefully. I don't know what will happen if you ask diskutil to mount a volume on a non-present drive; probably nothing, but... point taken.

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: mmnw on Aug 13, '09 03:22:43AM

You are absolutely right, FAT and NTF volumes don't have a UUID. Didn't consider this. This will make the first part of the hint pretty hard with external volumes. Anyone know what to use in fstab instead of UUID?
(Don't say /dev/disk** since this will only work with fixed internal drives).



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: mmnw on Aug 13, '09 03:29:42AM

Oh, I'm sorry ... didn't read the first part right. Of course you use the LABEL of the drive ...



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: woodgie on Aug 13, '09 01:10:49AM

There's a small typo, it should read:

diskutil info /Volumes/PartitionName

Add an s at the end of Volume.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: Ram-Z on Aug 12, '09 11:30:45AM

Very nice hint thx.

but it didn't work for me at first, trying to disbable mouting of my bootcamp partition. the comand:

dev_id=`diskutil list | awk '/Windows/{ print $6; }'`

didn't return the device id but the size of the partition...

I had to change it to:

dev_id=`diskutil list | awk '/Windows/{ print $8; }'`

This was because

diskutil list

returned

#: TYPE NAME SIZE IDENTIFIER
...
3: Microsoft Basic Data Windows 48.9 Gi disk0s3
...

So a little tinkering might be necessary depending on filesystem type.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 12, '09 12:53:48PM
ah, I see. I'd assumed it was computer-readable output when in fact it's only human-readable. since it seems to be a fixed width layout, however, you can try this:
dev_id=`diskutil list | awk '/Windows/{ print substr($0,69); }'`
or if you want to be tricky, this:
dev_id=`diskutil list | awk '/Windows/{ match( $0, /disk[0-9]+(s[0-9]+)?/ ); print substr( $0, RSTART, RLENGTH); }'`
which ought to extract the correct identifier without relying on a (non-existent) field structure. sorry about that.

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: Ram-Z on Aug 12, '09 01:27:20PM

This matches the correct column but also matches another partition. But this is because of the LABEL of my partition:

3: Microsoft Basic Data Windows 48.9 Gi disk0s3
...
1: Windows_NTFS Evolution 3.8 Gi disk2s1

It maches the first line correctly and the second because of the TYPE.
I'm not that familiar with awk regex but it should be able to match on word boundary like this in perl:

/bWindowsb/

But my workaround by selecting the 8th column works fine, so i'll be using this for now...

Thanks

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 12, '09 01:35:37PM

yeah, sjk caught this kind of problem in the post below yours. I posted a workaround there, and either he or I (or someone else) will think of something better in a bit.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: sjk on Aug 12, '09 02:15:01PM
sjk caught this kind of problem in the post below yours
Thanks for noticing; I can't the last time someone acknowledged one of my comments on this site. Usually I arrive too late to the party, after most everyone else has moved on. :)

I didn't see your reply to Ram-Z with the non-existent field structure examples 'til after I'd posted. Glad we're all understanding the issue. I'm busy with other things for awhile so it's doubtful I'll be the one to think of something better in a bit. :)

I'll check back here later. Hmm, just realized subscribing to comment RSS feeds for specific articles in Apple Mail is a reasonable workaround for lack of e-mail notifications and could likely be my preferred method to monitor for article/comment followups on this site.

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: sjk on Aug 12, '09 01:17:26PM
diskutil list
returned

#: TYPE NAME SIZE IDENTIFIER 
... 
3: Microsoft Basic Data Windows 48.9 Gi disk0s3
...
So a little tinkering might be necessary depending on filesystem type.
It can also be necessary to consider whitespace in partition/volume names under the NAME field, e.g. Macintosh HD for this line:
   2:                  Apple_HFS Macintosh HD            54.9 Gi    disk0s3
And you have to be careful matching names that have the same pre-whitespace prefix. For example, with Boot, Boot i386, Boot ppc volume names the awk '/Boot/…' command will match all three when the intention may be to match Boot.

I consider my workarounds for whitespace issues too icky for posting here. Eventually I'll write (or find) a nicer diskutil list output parser/matcher to handle them.

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 12, '09 01:31:23PM
And you have to be careful matching names that have the same pre-whitespace prefix. For example, with Boot, Boot i386, Boot ppc volume names the awk '/Boot/…' command will match all three when the intention may be to match Boot.
I hadn't considered this, either, but since it's regular expressions it's not too difficult to resolve. if you have multiple volumes with similar names (like your boot, boot i3686, boot ppc) all you need to do is space-pad the regexp; i.e. use /boot / or /boot {3,}/ with a large enough number of spaces to exclude other names. diskutil will always output with space padding (so you don't need to worry about other whitespace), and the regexp won't match shorter names, so this should do the trick.

good point, thank you.

[ Reply to This | # ]

10.5: Mount a partition or volume on demand
Authored by: sjk on Aug 12, '09 02:18:30PM

That's my thinking on it, too. I'm just not code-savvy enough to quickly bang out a clean script. :)



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: Ram-Z on Aug 12, '09 02:09:59PM
So this is what i got:

dev_id=`diskutil list | awk '/\\yPARTION_NAME\\y/{ match( $0, /disk[0-9]+(s[0-9]+)?/ ); print substr( $0, RSTART, RLENGTH); }'` && echo $dev_id
It does the trick nicely for me. I just added a word boundary around the partition name e.g. \y...\y

I don't have any partition with spaces but it might work as well.

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: Ram-Z on Aug 12, '09 02:14:23PM
The && echo... is of course not necessary... I was just using for tests purposes.
The bash script would look like this:

#! /bin/bash
dev_id=`diskutil list | awk '/\\yPARTION_NAME\\y/{ match( $0, /disk[0-9]+(s[0-9]+)?/ ); print substr( $0, RSTART, RLENGTH); }'`
[[ $dev_id ]] && diskutil mount $dev_id


[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 12, '09 02:47:54PM
So this is what i got: dev_id=`diskutil list | awk '/\yPARTION_NAME\y/{ match( $0, /disk[0-9]+(s[0-9]+)?/ ); print substr( $0, RSTART, RLENGTH); }'` && echo $dev_id It does the trick nicely for me. I just added a word boundary around the partition name e.g. y...y I don't have any partition with spaces but it might work as well.
I puddled with it a bit and decided to drop awk altogether - ended up with this:
dev_id=`diskutil list | egrep '.{33}PARTION_NAME {2,}' | egrep -o 'disk[0-9]+(s[0-9]+)?'`
the first grep returns a line where the partition name is in the name column and has at least two spaces following; the second grep digs out the actual device name from that line. not very elegant, though...

is there a way to tell awk to use fixed width fields rather than delimiters? apparently there is supposed to be a FIELDWIDTHS variable that can be set, but I can't seem to get it to work in Darwin.

[ Reply to This | # ]

10.5: Mount a partition or volume on demand
Authored by: rduke15 on Nov 08, '10 11:13:31PM
I had to change it to: dev_id=`diskutil list | awk '/Windows/{ print $8; }'`
This seems very fragile. If the disk is always the last word on the line, use $NF (Number of Fields) instead of a fixed position:
dev_id=`diskutil list | awk '/Windows/{ print $NF; }'` 


[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: Pedro Estarque on Aug 12, '09 01:05:12PM
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)
cat /etc/fstab.hd
IGNORE THIS FILE. This file does nothing, contains no useful data, and might go away in future releases. Do not depend on this file or its contents.
This made my day. Nice hint btw.

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: Ram-Z on Aug 12, '09 01:35:31PM
IGNORE THIS FILE. This file does nothing, contains no useful data, and might go away in future releases. Do not depend on this file or its contents.
They should add:
PLEASE don't open it again!!!
I wonder if there are more of those files... :D

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: sjk on Aug 12, '09 01:37:36PM
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.
I've used fstab entries like:
UUID=2F7B7609-9937-310A-87D2-810C883AAF87	/Users	hfs	rw
… for mounting separate /Users volumes at least since 10.3 and maybe earlier. That way I avoid the issue of the mount point on those systems being saved as /Volumes/Users in files that might be used on others systems when /Users is (by default) on the startup volume and /Volumes/Users doesn't exist. I posted a similar comment a few years ago: here

[ Reply to This | # ]
How can launchd be used to keep a secondary drive from mounting?
Authored by: Ronald Lanham on Aug 12, '09 02:00:01PM
"...leave a drive plugged in but mount/unmount volumes programmatically (using launchd to set appropriate times or conditions)."

How can this be done?

I'd like my secondary drive to not mount every time I start up. (FWIW I have six partitions on my secondary drive and prefer not to have to go through your detailed steps six times.)

By not having my secondary drive mounted it would make searches easier and less chance of misplacing a file when one of my aliases point to the wrong drive (which has happened in the past).

Thanks for any info.

[ Reply to This | # ]
How can launchd be used to keep a secondary drive from mounting?
Authored by: tedw on Aug 12, '09 03:14:15PM
I'd like my secondary drive to not mount every time I start up. (FWIW I have six partitions on my secondary drive and prefer not to have to go through your detailed steps six times.)
fstab (so far as I know) only works with volumes/partitions (file systems), not with actual drives. there doesn't seem to be a way to specify "all the volumes on drive X". so (sorry), you're going to have to write 6 lines into fstab (1 for each file system) rather than just 1. you can use the cookie cutter approach, though. start with this template:
UUID=   none    hfs   rw,noauto
LABEL=   none    msdos   rw,noauto
copy it 6 times into fstab, then open Disk Utility and copy the UUID into the UUID=spot (or the volume name into the LABEL= spot), making sure you don't add any spaces after the equals or lose the space before the 'none'. save that, and the volumes will not mount.

getting the volumes to mount at need, though, depends on how you want to do it. do you always want to mount them all at once? do you want to be able to choose which you want to mount? do you want them to mount automatically at certain times? diskutil has a command for mounting all the volumes on a disk at once (diskutil mountDisk /dev/disk1, though you'll have to check which disk number it actually is), but the others will take a little programming, depending on your needs.

[ Reply to This | # ]

How can launchd be used to keep a secondary drive from mounting?
Authored by: Ronald Lanham on Aug 16, '09 02:06:49PM

Thanks tedw for the post.

I may try this, but with my limited knowledge of the Terminal it makes me a little nervous.

Too bad there is not a little app or script that can do this or that DU has an option to keep HDs unmounted at startup.

Anyway... thanks again.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: robogobo on Aug 12, '09 03:40:37PM

I use Butler to mount and unmount volumes.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: Ronald Lanham on Aug 16, '09 02:02:03PM

Thanks Robogobo for the post, but I don't see how Butler can be used to keep an entire HD from mounting at startup.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 16, '09 04:44:24PM

Give me a few minutes, I'll see if I can whip up a widget for it. Don't know where I'll post it, of course, but I'll figure that out then.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: yeidel on Aug 19, '09 12:19:23PM

WARNING: UNTESTED!

If you have Butler set to run at login, and you have a dismount Applescript set to run when Butler starts, that might have the desired effect.



---
-- Joshua Yeidel
Center for Teaching, Learning, and Technology
Washington State University
Pullman, WA, USA



[ Reply to This | # ]