January 28, 2015

MacOS X Yosemite: How to mount a disk on startup

Apple

MacOS X Yosemite removed few capabilities that existed in previous versions of MacOS X. Notably the StartupItems feature which allowed you to run scripts when the system boots up.

On my machine I want to be able to mount an internal disk as soon as the machine boots up, but before a user logs in. A common solution in the past involved a custom StartupItems script, but this no longer works on Yosemite.

To solve this problem I created a LaunchDaemons script. My disk's volume name is BigDisk and is accessible via /dev/disk0s2. To mount it on startup I created a file named com.jollyturns.disks.plist in /System/Library/LaunchDaemons with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
  <dict>
    <key>RunAtLoad</key>
    <true/>

    <key>KeepAlive</key>
    <dict>
      <key>PathState</key>
      <dict>
        <key>/Volumes/BigDisk</key>
        <false/>
      </dict>
    </dict>

    <key>Label</key>
    <string>com.jollyturns.disks</string>

    <key>ProgramArguments</key>
    <array>
      <string>/usr/sbin/diskutil</string>
      <string>mount</string>
      <string>/dev/disk0s2</string>
    </array>        
  </dict>
</plist>

When the machine starts up, launchd will load the file since we have the RunAtLoad set to true. It then finds that /Volumes/BigDisk does not exist, since the disk is not yet mounted, so it executes the diskutil command to mount the directory.

Posted by ovidiu at January 28, 2015 01:52 PM |
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?



 
Copyright © 2002-2016 Ovidiu Predescu.