Saturday, December 1, 2012

OSX: Automatically Unmount Volumes on Startup Login.

Don't you hate it when you start your mac and you have a bunch of unwanted attached drives mounted on your desktop?

Specifically if you have Windows (via Bootcamp) or rarely used drives you dont want shown or accessible. It gets annoying to see those volumes mounted. For example, I don't want my wife to accidentally delete files off my Windows partition. I also have a clone drive of my system and whenever I double click on an image, Photoshop may launch from the clone drive instead of the one on  my SSD system drive. I rarely use the clone drive except to mirror my working system drive once a week.

I prefer to manually mount those drives when I need them.

Here are the 3 drives I dont like littering my desktop: System Reserved, Windows, and my clone drive. System Reserve is the small boot partition that contains files to boot Windows. Delete any of those files and Windows wont boot.

 Here is my solution I found somewhere off the net (I can't find the original link).  You can write an Applescript  to do this. Create the Applescript to unmount and load at login via the "Login Items" under "User & Groups" in the System Preference.

Here is the applescript. Modify with the volume name and save as an application.

 set volname to "System Reserved" -- # name of target volume  
 set p to (POSIX path of (volname & ":" as alias))'s text 1 thru -2  
 set sh to "diskutil umount " & quoted form of p & " &> /dev/null &"  
 do shell script sh  

Next, with the compiled Applescript(s) applications, go to User & Groups and go under Login Items and add your applescript(s).  Also, I check off "Hide" so I don't see the script executes. It will re-run if you log off into another user and re-login back into your user account. Hiding it will run it in the background so if it errors,you won't see it.

Voila. Next time you boot, those drives wont show up.

You can easily remount the drives at any given time via the Disk Utility so this isn't a permanent thing.

Now, the only thing I can think of next is to stop OSX from prompting you to initialize a disk when it can't recognize the filesystem (e.g. Linux EXT4 or Vmware VMFS).


  1. Thank you so much! I had been trying to resolve this issue for quite some time :)
    Unfortunately I wasn't able to get it working properly on my system for some reason (I honestly don't believe it has ANYTHING to do with your script- I've basically done lots of tinkering without documenting it and it's basically time to do a fresh install. I'm just waiting for a good deal on the HD I want to pop up).

    But I'm writing to tell you about another great solution to this issue that I never could get working before because I couldn't figure out where to find a KEY piece of the information required to make it work (the UUID of the "System Reserved" partition).
    The way I know how to do it is to use the terminal to edit a config file used by OS X at boot and add a line for each partition/volume that you do not want to be mounted. This is a system wide setting, but you can still mount the volume with Disk Utility or the terminal.
    Open the terminal and use the following command to determine which partitions you don't want to mount:

    diskutil list

    make a note of their designations (e.g. disk2s1)
    for each partition type the following command to reveal the UUID

    diskutil info disk2s1 ##of course you must use the designations you just noted instead of "disk2s1"

    open a new terminal window (so you can copy from the first one to the second one to simplify things)
    use this command to edit the config file:

    sudo nano /etc/fstab

    you must add the following line (to the end of the file) for each partition you don't want to see:

    UUID=(copy and paste the long hexidecimal number from the 1st window) none ro,noauto 0 0

    type [ctrl]-X when you're done (to exit), it will prompt you to save, type 'Y' for yes and then you must confirm the file name.
    And that's it! When you reboot you won't see any of those partitions.

    You can find out the UUID for apple partition using the Disk Utility application, but for whatever reason it doesn't display it for NTFS volumes UNLESS you use the command above in the terminal. I don't know why it took me so long to figure that out...

    The fstab file also allows you to identify volumes by LABEL as well as the diskXsY format, but it breaks when you have more than one volume with the same label, and the diskXsY designation is NOT constant (it will just ramdonmly, sporatically change)- which seems to be a bug that Apple would have to fix (from what I've found).
    Plus since I sometimes change out HDs or switch them around for various reasons, that would (and should) definitely cause the diskXsY designation to change... So using the UUID is the only viable solution.

  2. Oh and I forgot to mention a possible solution to the "initialize disk" prompt- at least for Linux formatted drives. There are open source drivers available for ext2, ext3 & ext4 file systems, installing those eliminates that prompt. (I think the free ones are all read-only).

    I also forgot to mention that part of my solution above doesn't work if you've got Tuxera NTFS installed & enabled on the "System Reserved" volumes you want to hide/not mount. You must disable it for those particular volumes in the preference pane and then reboot.

  3. My appologies... I just realized an error in my commands!
    all of the instances where "diskXsY" are used should be substituted with: "/dev/diskXsY" e.g.

    diskutil info /dev/disk2s1

  4. Can it be modified to unmount ALL disks at once (whatever their names and number) except the booting one?

    1. Yes, you can chain the script.
      Here is a quick example:

      set volname to "System Reserved" -- # name of target volume
      set p to (POSIX path of (volname & ":" as alias))'s text 1 thru -2

      set volname2 to "Second Disk" -- # name of target volume
      set p2 to (POSIX path of (volname2 & ":" as alias))'s text 1 thru -2

      set sh to "diskutil umount " & quoted form of p & " &> /dev/null &"
      do shell script sh

      set sh2 to "diskutil umount " & quoted form of p2 & " &> /dev/null &"
      do shell script sh2

  5. Best script here:
    MERGING two Scripts to Unmount disks except booting disk