overview_timecapsule_20091202

How to automatically mount and umount Apple Time Capsule on Linux

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,

  • What if the mount point (e.g. /mnt/timecapsule) exists but it is empty because of the non-mounted Time Capsule? Such backup programs would backup in our hard disk!
  • We could create a script th7at mounts Time Capsule on login.
    What if we don’t always use our laptop at home? There must be a way to discover if a Time Capsule is present on the current network and mount it.
  • The Time Capsule Volume must be unmounted when we logout, otherwise there will be an error if we are connected to a Wireless Network. Network volumes are sometimes unmounted after Network Manager is stopped, under some configurations.

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.

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

You need to set values for the first three variables:

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

Install the required cifs-utils package.

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

To un-mount Time Capsule, run

Yes, it automatically detects everything.

How does it work?

Let’s observe it:

At line 31, we simply look if Time Capsule is already mounted, by calling the mount command. Typically, it will return something like:

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:

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.

Published by

dgraziotin

Daniel Graziotin is a PhD student in Computer Science at the Free University of Bozen-Bolzano. His research interests include human aspects in empirical software engineering with psychological measurements, Web engineering, and Open Science. He is Editorial Associate at the Journal of Open Research Software and the local coordinator of the OKFN Open science Italian group . He is a member of the ACM, SIGSOFT, IEEE, and the IEEE Computer Society. Read more about.

58 thoughts on “How to automatically mount and umount Apple Time Capsule on Linux”

    1. Thank you for the report. This is quite strange, because
      $ wget /wp-content/uploads/2011/10/timecapsule-handler.gz--09:18:17--  /wp-content/uploads/2011/10/timecapsule-handler.gz           => <pre class="decode:1 inline:true " >timecapsule-handler.gz'Resolving ineed.coffee... 94.75.235.227Connecting to ineed.coffee[94.75.235.227]:80... connected.HTTP request sent, awaiting response... 200 OKLength: 893 [text/plain]100%[========================================>] 893           --.--K/s             09:18:17 (8.52 MB/s) -timecapsule-handler.gz’ saved [893/893]

      aeshma:tm dgraziotin$ gunzip timecapsule-handler.gz
      aeshma:tm dgraziotin$ ls
      timecapsule-handler
      aeshma:tm dgraziotin$ file timecapsule-handler
      timecapsule-handler: Bourne-Again shell script text executable

  1. That’s pretty cool.
    I’m probably going to steal this for auto mounting usb drives without a DE as well :)
    Just have to find out what kind of events are fired on usb plugging/unplugging.
    Anyway; a nice read, thanks.

  2. Thanks for this script!! I can’t believe Apple left us high and dry.

    I can’t get it to work though. I keep getting messages saying

    “sh timecapsule-handler
    mount error(13): Permission denied
    Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)”

    why does it hate me?

          1. make sure you have the correct variable set for time capsule volume. to verify you can download the windows version of “airport utility” and open it with wine. once there go to manual setup-disk there you will see your time capsule volume.

  3. Great script man! Thanks! Care putting it on GitHub or so where it will be persistent for generations of users to come :-)?

  4. This is a great and much needed project, but I’m in some hot water over it.

    At first I ran it and the program seemed to work, though I could never get it to mount the time capsule. My disk was located at /10.0.1.1/Disk, which is what I indicated in the file. After some time, I realized that though I could not seem to access the time capsule within Linux, after running timecapsule-handler I could see it in my Windows virtual machines that run within linux. So that was great news.

    The probablem now is that I restarted the computer and it stops in the middle of booting every time now with a black screen. Nothing happens unless I press a function key, at which point it says

    mount.cifs: bad UNC (/10.0.1.1/Disk)
    mountall: mount /media

    and that’s it. I’ve tried about 100 things to recover, including SystemRescueCD, SuperGrubDisk 2, and Rescatux…

    1. I have never tested this script on a virtual machine. Is this error still happening on the virtual machine?
      According to your description, I image the following settings:

      Is this what you are using?
      Are you running the script after the network has been setup? Are you also running it on shutdown before the network is taken down?
      What happens if you unplug your time capsule and try to boot Linux?

      Hope this helps

  5. Hello, I’m trying to make this work on my ubuntu (so i can get rid of w7 for good) but i can’t get it done. I filled my TIme Caspule configs and tried to run sudo timecapsule-handler – it tells me “command not found” or if i i’m under root it tells “Permission denied”.

    I’m a newbie in linux… could anyone give a little help?

      1. Yes, after that i stucked.

        Actually i forgot to mention two things: first, the link for the script is broken (it doesn’t extract), i had to download fro github and then i could write de configs. Two: I’m using wubi under windows 7 (it’s a shame, i know)

        Thanks in advance

      2. I did it! I was reading around about linux terminal commands, and about the ctrl+alt+F1 shortcut. So i went to the “text-only” mode and tried again with sudo… After that, i just opened mnt and there it is! Beautiful…

        Thanks for the script man, it’s THAT good!

  6. I’m having the same problem Joao had. I followed all of the instructions and when I run “sudo timecapsule-handler”, “sudo: timecapsule-handler: command not found” is returned. I’m new to linux and am unsure how to get this working. Thanks for the help in advance. I did install cifs-utils.

    1. Now I get it! I was not clear enough in my post. I assumed that people reaching the post would have been familiar with the terminal. The issue is not having the script in your $PATH.

      The script must have execution permission. Run the following command while being in the same folder where timecapsule-handler is.

      In order to use the script just by calling its file name (the sudo timecapsule-handler command), you must move timecapsule-handler in a directory included in your $PATH (e.g., /usr/bin/ and /usr/local/bin).

      Otherwise, to run the script you have to be in the same directory where the script is and run:

      You can also provide the full path where the script is

      I updated the post to clarify this. Hope it fixes your issue.

  7. thanks dgraziotin for the scipt , it work for me in ubuntu 12.10 but when I tried to used it in ubuntu 13.04 the script didnt work. so I add a sec=ntlm to the script and works fine in 13.04
    so the last four line of the modified script are:

    I take a while figure it out for me looking in the web. hope this will help others.

    1. Franco,

      You are correct, the sec=ntlm helped me out. Without that I can’t write to the folder on fedora 16.

      Thanks!
      Gary

  8. “The script must be called just after network setup and before network teardown. Time Capsule likes clean umount.”

    How would I accomplish this part?

  9. Man, this is great! You saved me hours of work.

    I was running into an issue when while trying to mount my time capsule as a NAS, but for some reason Ubuntu Server 12.04 kept throwing the error:

    Retrying with upper case share name
    mount error(6): No such device or address
    Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

    The way I was trying to mount it was this:
    sudo mount.cifs //192.168.1.1/Data/ /media/capsule -o pass=password

    Your script probably does something else to make this work. I’ll have to have a closer look at it later. Anyway, thanks for this awesome script!

      1. I had to add the above mentioned “sec=ntlm” after the password in the mount command for it to work in fedora 19. There were no other issues.

        Thanks for the script!

  10. Very nice man.

    Working on Ubuntu 13.04, just need to add “sec=ntlm” on the line of the mount, like that:

    echo “mount.cifs $TIMECAPSULE_PATH $MOUNT_POINT -o pass=$TIMECAPSULE_PASSWORD,file_mode=0777,dir_mode=0777,sec=ntlm” | /bin/bash

  11. The addition of “,sec=ntlm” to the options is also required in more recent 12.04 versions.

    In case you can’t find the volume name, start the air-port utility on your mac and look under ‘disks’.

    Proper quoting prevents the need for escaping of special characters. Here’s my version of the script:

    TIMECAPSULE_IP=""                         # e.g. "192.168.1.100"TIMECAPSULE_VOLUME="/Time Capsule"        # also try "/Data"export PASSWD='YOURPASSWORDHERE'          # No need to escape anything (except "'")MOUNT_POINT="/mnt/timecapsule"            # no need to create the directoryIS_MOUNTED=<pre class="decode:1 inline:true " >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”
    mount -t cifs “$TIMECAPSULE_PATH” “$MOUNT_POINT” -o file_mode=0777,dir_mode=0777,sec=ntlm
    fi
    fi
    unset PASSWD

  12. what did I do wrong?

    [root@centos bin]# timecapsule-handler
    mkdir: cannot create directory /mnt/timecapsule': File exists
    /bin/bash: line 1: unexpected EOF while looking for matching

    /bin/bash: line 2: syntax error: unexpected end of file
    [root@centos bin]#

  13. config file:

    TIMECAPSULE_IP=”192.168.1.2″ # e.g. “192.168.1.100”
    TIMECAPSULE_VOLUME=”PEDRO’s Time Capsule/Data” # also try “/Data”
    TIMECAPSULE_PASSWORD=”passw” # 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

    1. Try to escape PEDRO’s -> PEDRO\’s.

      Otherwise, try to rename your Time Capsule to something simpler, e.g. “TIMECAPSULE” and change the config file to use the simpler name.

  14. Hi, nice job and thank you very much for this script! I have just one more problem: run this script automatically at the login and logout. Is it possible?

  15. Thanks! Worked like a charm. I’d spent hours trying to do this manually on Raspbian but kept getting error(13) permission denied.

  16. Mate, I’ve been trying to figure this out for the last 12 months. This is bloody amazing! THANK YOU!

  17. The main idea of your solution is great.
    I installed your script on LMDE with great expectations, everything passed well, but it does not works. I got the message as follows:
    Retrying with upper case share name
    mount error(6): No such device or address
    Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

    I also tried to change the name of the Time Capsule, but without results.
    I will be thankful for solving this problem.
    Thanks in advance.

  18. This is a wonderful script and works well when linked in /etc/init.d/ to run on startup, but I had noticed on sleep and resume that the timecapsule mount contents would disappear because the network connection would die.

    My fix is to add a script into: /usr/lib/pm-utils/sleep.d
    (These scripts run on sleep/hibernate and wake/thaw in Debian-based systems.)

    Here’s the script. I named the script “01timecapsule” so it executes near the beginning of the sleep process. Make sure it’s owned by root and has execute permission.

    1. Hello! thanks so much for your script, it works perfectly with my Airport Extreme & usb hard drive. I’m having a hard time figuring out where to put the script to run at startup because it requires sudo.

      I tried placing the script in /etc/init.d with no success
      I also tried putting the path in /etc/rc.local

      both with no joy.

      I’m very new to this – have any of you done this before?

      1. Hello. Suppose you have the script at /usr/local/bin/timecapsule-handler, and that it is executable sudo chmod +x /usr/local/bin/timecapsule-handler

        Then, you need to to put

        At the end of /etc/rc.local. This should be sufficient.

        1. oh wow! you hero! I honestly didn’t expect a reply so fast – I’ll give this a try and let you know the score :)

          Thanks again for your help :)

          1. So I tried adding the line to rc.local after double-checking the permissions.
            the script does not run at boot, but will always work if i manually type: sudo timecapsule-handler

            I’ve attached a screenshot that might help better explain what I mean:
            http://imgur.com/a/lziCV

            I imagine it’s something to do with the order things are starting up in, I’ll have a play with it later & see if I can find a different way to get it to start.

            Thanks again for taking the time to help, it genuinely is appreciated – good luck with the rest of your day 😀

            Gareth

  19. You are in the right direction.

    The script needs a network adapter properly configured (and connected) before the actual login to your desktop environment. This is not in the default settings of Network-Manager. However, I think I remember an option for making your connection settings global and active before the starting the desktop environment. Otherwise, you need to ask in the forums of your current Linux distribution for how to start a script (with sudo privileges) right after Network-Manager connects to a certain network.

    1. For those who find this thread after me.
      If you’re having the same issue I do with the network stack not running before rc.local starts
      add:
      sleep 30
      then your script location

      it’ll delay the next command by 30 seconds, allowing the system to start fully.

      Hope it helps the next person 😀

      Good luck everybody!
      Gareth

Leave a Reply

Your email address will not be published. Required fields are marked *