how to automatically mount and umount apple time capsule on linux

2011-10-19

Even though you may not own a Macbook, an iPhone/iPad or any other Apple device, you might still be the owner of a Time Capsule. I bought it when I was using a Macbook Pro. Time Capsule is wonderfully integrated with the various Apple devices and machines. Unfortunately, it becomes a pain to use Time Capsule under you boot GNU/Linux. It is possible to manually mount a Time Capsule Volume on a directory. It is uncomfortable, isn’t it? Using Time Machine clones such as Déjà Dup, flyback or TimeVault becomes difficult. They may be automatically started by your Desktop Environment. However, they may give errors because of the still unmounted Time Capsule Volume. Additionally,

I decided to handle these issues with a script, called timecapsule-handler (Download, gzip).

How to Download, Configure, and Run

I wrote this section especially for those unfamiliar with the GNU/Linux console. It is written keeping Ubuntu as reference distribution. Experienced users may use any GNU/Linux distribution and they only need to know that:

  1. cifs-utils is needed in order to use the script
  2. The script should be under your $PATH and be invoked with root privileges (sudo)
  3. The script must be called just after network setup and before network teardown. Time Capsule likes clean umount.

First, download the script.

wget https://gist.github.com/dgraziotin/4487187/raw/caba5fde05c926d2bf8f9f2e4ed92156c67bfe25/timecapsule-handler

Edit the configuration variables with a text-editor. For your convenience, here is how to edit it with Ubuntu default graphical text editor:

gedit timecapsule-handler

You need to set values for the first three variables:

TIMECAPSULE_IP=""                         # e.g. "192.168.1.100"
TIMECAPSULE_VOLUME="/Time\ Capsule"       # also try "/Data"
TIMECAPSULE_PASSWORD="YOURPASSWORDHERE"   # prefix special characters, e.g. \!
MOUNT_POINT=/mnt/timecapsule              # no need to create the directory. No " here

Save the file, give it execution permissions and move it in a directory under your $PATH (e.g., /usr/local/bin).

chmod +x timecapsule-handler && sudo mv timecapsule-handler /usr/local/bin

Install the required cifs-utils package.

sudo apt-get install cifs-utils

That’s it. Here is how to use the script. To mount Time Capsule, run:

sudo timecapsule-handler

To un-mount Time Capsule, run

sudo timecapsule-handler

Yes, it automatically detects everything.

How does it work?

Let’s observe it:

#!/bin/bash
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#

# Version 3, enhanced for Ubuntu 13.X+, Fedora 19+, and similar distros.
# Runs on all GNU/Linux distros (install cifs-utils)

# Author: Daniel Graziotin  - https://ineed.coffee
# Purpose: Check if there is a TimeCapsule in your network and mount it
#     for use it under Gnu/Linux. Unmount it if it is already mounted.
#      The mount point is created and destroyed after use (for prevent
#      automatic backup software to backup in the directory if the device
#      is not mounted)
# Instructions:
#   1) Install cifs-utils (sudo apt-get install cifs-utils)
#       1) Change the first four variables according to your configuration.
#       2) Run this program at boot when your network is already
#          set up. Also, run it on logoff to umount Time Capsule.

TIMECAPSULE_IP=""                         # e.g. "192.168.1.100"
TIMECAPSULE_VOLUME="/Time\ Capsule"       # also try "/Data"
TIMECAPSULE_PASSWORD="YOURPASSWORDHERE"   # prefix special characters, e.g. \!
MOUNT_POINT=/mnt/timecapsule                # no need to create the directory

IS_MOUNTED=`mount 2> /dev/null | grep "$MOUNT_POINT" | cut -d' ' -f3`
TIMECAPSULE_PATH="//$TIMECAPSULE_IP$TIMECAPSULE_VOLUME"

if [[ "$IS_MOUNTED" ]] ;then
    umount $MOUNT_POINT
    rmdir $MOUNT_POINT
else
    CHECK_TIMECAPSULE=`smbclient --no-pass -L $TIMECAPSULE_IP 2>&1 > /dev/null | grep -m1 -i apple`
    if [[ "$CHECK_TIMECAPSULE" =~ "Apple" ]] ;then
        mkdir $MOUNT_POINT
        echo "mount.cifs $TIMECAPSULE_PATH $MOUNT_POINT -o pass=$TIMECAPSULE_PASSWORD,file_mode=0777,dir_mode=0777,sec=ntlm" | /bin/bash
    fi
fi

At line 31, we simply look if Time Capsule is already mounted, by calling the mount

command. Typically, it will return something like:

$ mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sys on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,nosuid,relatime,size=10240k,nr_inodes=195863,mode=755)
run on /run type tmpfs (rw,nosuid,nodev,relatime,size=10240k,mode=755)
/dev/sda1 on / type ext4 (rw,noatime,nodiratime,discard,errors=remount-ro,commit=0)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,relatime)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
gvfs-fuse-daemon on /home/dgraziotin/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=dgraziotin)
//192.168.0.2/Time Capsule on /mnt/timecapsule type cifs (rw)

See the last line of the command? In line 31 of the script we search in the output of the mount command the string $MOUNT_POINT (e.g, /mnt/timecapsule). Then, between line 34 and line 36, if Time Capsule Volume is mounted, we umount it and delete the mount point using rmdir to be sure to protect data if the umount is unsuccessful. Otherwise, between line 37 and line 42, we use the smbclient program to do a sort of ping of the device on the Network. If there is someone at the given IP, and this someone replies with something like “I am Time Capsule!”, we are sure to mount it. This is a typical output of the smbclient program:

$ smbclient --no-pass -L 192.168.0.2
Anonymous login successful
Domain=[WORKGROUP] OS=[Apple Base Station] Server=[CIFS 4.32]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       
Anonymous login successful
Domain=[WORKGROUP] OS=[Apple Base Station] Server=[CIFS 4.32]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------

The program gives a result within a second, that’s because the script is so fast in doing its job. There may still be issues with my script. However, it would not do anything harmful to the system and fail silently in case of errors. I hope that you may find it useful. Please report your experience in this post comments. If for any reason the script does not work, I encourage you to try this older version.

wget https://gist.github.com/dgraziotin/4487187/raw/d2b2e378937f3be777b0e4617a9b2a83b0a946a4/timecapsule-handler

I do not use a commenting system anymore, but I would be glad to read your comments and feedback. Feel free to contact me.