Author Topic: Mac users - Make use of launchd/unix to mount your volumes  (Read 35485 times)

Offline speeding_ant

  • Hero Member
  • *****
  • Posts: 1535
    • PixelEyes
Mac users - Make use of launchd/unix to mount your volumes
« on: January 17, 2011, 01:13:23 PM »
Thought I'd share my quick and simple bash script (with the help from Stokkes) for mounting AFP/SMB shares. Set your preferences, add the shares, and it will ping the server. If it is available then it will mount the shares. There is also an option to load iTunes at the same time.

Now works with Lion.

**NEW** Added functionality to wake the server via WOL **NEW**
 28/12/2012 - Added the ability to call "wake" at the end of the script to wake the server, no matter which time it is. eg: /scripts/mount.sh wake

 You must have wolcmd (included) in the same folder as the script. This is a definable directory. If the server is unavailable via ping, WAKE is set to TRUE, and the time is within the hours specified, then the script will attempt wake on lan. You must have your server MAC address and subnet in the options. This is all shown in the script.

Now if the server is unavailable it will exit iTunes. I found iTunes to be unstable if it can't resolve links to its music. You can set this in the script

Step 1: Where to put mount.sh & com.media.mount.plist

 - Create a folder called "Scripts" on the root of your hard drive. This could be /Macintosh HD/Scripts.
 - Copy mount.sh to this folder.
 - Copy com.media.mount.plist to your user *Username*/Library/LaunchAgents folder. If the folder doesn't exist, create it.

Step 2: Editing the mount script

 - Open mount.sh in text edit. Read the notes at the top of the script.
 - Enter your username and password inside the quotation marks where shown. Read the notes in the file if your password has symbols (eg @, as you'll need to url encode them). Site is given in the script.
 - Enter your server IP address (eg "192.168.0.100") and hostname (eg "tower") where shown. If your nas has a dynamic IP address (DHCP), enter the hostname instead of the IP Address.
 - Leave Mount point alone, unless you want the shares to mount somewhere other than the default /Volumes
 - Enter TRUE or FALSE if you want iTunes to open or not.
 - Enter the protocol you want to use, either smb or afp.
 - Enter the shares you want to mount. The shares must be space seperated (eg "TV Movies HD%20Movies". If the share name has a space in it, replace the space with %20 (eg "TV%20Shows").

Step 3: Enable script

 - Double check the settings are correct, and make sure the mount.sh, and com.media.mount.plist files are in the correct place.
 - Open Terminal (Applications/Utilities/Terminal). The sudo command will need your administrator password. Type:
                  - sudo chown root:staff /Scripts/mount.sh
                  - sudo chmod 775 /Scripts/mount.sh
 - Restart your computer. It should mount your shares on login, and check if they're mounted every one minute.

If it's not working, double check permissions and settings. Try running the shell script by opening Terminal and typing:

 - /Scripts/mount.sh
 - Press enter

If that works, then check the status of the launchd agent by going into Terminal and typing

 - launchctl list
 - This will display the loaded launch daemons. If there is a 1 next to the com.media.mount.plist file, make sure the script is located on your boot volume, "/Scripts/mount.sh". If it's not in the list at all, make sure that the plist in your user Library folder, in a folder called "LaunchAgents".

Hope this is useful for someone!
« Last Edit: December 27, 2011, 01:06:05 PM by speeding_ant »

Offline Stokkes

  • Sr. Member
  • ****
  • Posts: 282
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #1 on: January 17, 2011, 01:32:37 PM »
Always nice to see people adding custom scripts for Mac.

Good job.

Offline speeding_ant

  • Hero Member
  • *****
  • Posts: 1535
    • PixelEyes
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #2 on: January 17, 2011, 01:43:19 PM »
Cheers! I've found a solution for quitting applications by their real names via terminal (not process ID, which tends to change often), and it will prompt to save documents first, or exit quietly. Unfortunately doesn't work with iTunes, as iTunesHelper daemon conflicts with the name.

If you need this, let me know.

You can also decide to umount Volumes if there server isn't available, where the else statement is. In the else statement (at the bottom) remove exit 0 and add umount /Volumes/Music etc.

Offline Stokkes

  • Sr. Member
  • ****
  • Posts: 282
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #3 on: January 17, 2011, 02:18:04 PM »
I'll take a look at it when I get home this evening.

I've been meaning to make a script that'll run on a set schedule (read: often) to make sure my volumes are still mapped. I often have an issue in Plex where the volumes get unmounted (either the Mini goes to sleep or something else happens) and I can no longer stream my media. I'm then forced to vnc into the mini and remap the drives manually.. PITA.

So if this does what I've ben meaning to do, then fantastic.

Offline speeding_ant

  • Hero Member
  • *****
  • Posts: 1535
    • PixelEyes
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #4 on: January 17, 2011, 02:25:21 PM »
Exactly the reason why I made this script  ;)   Also doesn't try and open the Volume in a window, like if you add the mounts to your login items. They just mount quietly in the background, and only if they're not mounted already and the server is on.

Very useful if you run plex, or if you have iTunes running all the time, and your library is sitting on the shares.

It runs every minute, so you're seldom left without your volumes mounted.

Also, remember to put in the LaunchAgents folder in your user folder. If you use the main Library folder, it won't work.

Offline Stokkes

  • Sr. Member
  • ****
  • Posts: 282
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #5 on: January 17, 2011, 06:36:30 PM »
Few potential problems I've seen with the script. I'm working on a slight modification.

Long story short, there's a possibility that the shares mount correctly but are inaccessible because they are mounted by "root"

Offline speeding_ant

  • Hero Member
  • *****
  • Posts: 1535
    • PixelEyes
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #6 on: January 17, 2011, 06:40:46 PM »
I'm not having those issues. You need to run them from your user folder launch agents. If you run from the main Library Folder, it will run as root. There's no way around this as far as I could see.

Offline speeding_ant

  • Hero Member
  • *****
  • Posts: 1535
    • PixelEyes
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #7 on: January 17, 2011, 06:44:08 PM »
Here's screenshots of my setup.

Offline Stokkes

  • Sr. Member
  • ****
  • Posts: 282
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #8 on: January 17, 2011, 06:48:29 PM »
I've actually quickly rewritten the script using your basic premise..

Try it out and let me know what you think.. (rename to .sh and chmod +x)

Cheers,
« Last Edit: January 17, 2011, 06:54:42 PM by Stokkes »

Offline speeding_ant

  • Hero Member
  • *****
  • Posts: 1535
    • PixelEyes
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #9 on: January 17, 2011, 06:51:19 PM »
Nice one - bit more polished now  ;)   Move iTunes below the shares, otherwise will just spit errors when shares aren't mounted.

Offline Stokkes

  • Sr. Member
  • ****
  • Posts: 282
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #10 on: January 17, 2011, 06:56:52 PM »
Updated..

I've actually written a quasi Microsoft DFS mounting script (seeing as it OSX doesn't have built-in DFS support) and learnt a lot about mounting shares from the command line.

The password is truly a bugger, especially with symbols, etc. For example, the character "!" is a valid URL character and doesn't change during the url encode process, but wont' be accepted on the command line (have to change to %21). Very weird and frustrating :)


Offline speeding_ant

  • Hero Member
  • *****
  • Posts: 1535
    • PixelEyes
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #11 on: January 17, 2011, 07:00:54 PM »
Learning something every day!

How long have you been in the business?

Offline Stokkes

  • Sr. Member
  • ****
  • Posts: 282
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #12 on: January 17, 2011, 07:04:06 PM »
Oye... I've been in IT for about 16 years now. I do mostly management now, but still dabble in scripting, small-time programming.

Offline speeding_ant

  • Hero Member
  • *****
  • Posts: 1535
    • PixelEyes
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #13 on: January 17, 2011, 07:35:34 PM »
Only been in the scene for a couple of years now. Continual effort to keep learning, it's hard work!

Have you tested the script yet? Just checked it out, something fishy with the loop for shares. It's creating folders called "echo" "if" etc, under /.

Offline Stokkes

  • Sr. Member
  • ****
  • Posts: 282
Re: Mac users - Make use of launchd/unix to mount your volumes
« Reply #14 on: January 17, 2011, 08:03:46 PM »
Yes I did and it works fine.. It's creating a folder called "echo" and "if"?


Edit:
Can you post your variable SHARES= & ROOT= lines?
« Last Edit: January 17, 2011, 08:07:05 PM by Stokkes »