• 27 . 01 . 10
  • How to automatically mount a WebDAV share in Ubuntu using FuseDAV, an init script and a simple configuration file.

  • Tags

    , , , , , , ,

  • StumbleUpon

Automatically Mounting WebDAV in Ubuntu with a FuseDAV Init Script

I use BingoDisk as a backup and remote storage solution and wanted to use it as the destination for my daily backup of this blog and my development environment. BingoDisk is available via WebDAV which is nice and convenient, and there are 2 main options for Ubuntu, davfs2 and fusedav.

On the face of it, davfs2 is more convenient, as it allows you to automount quite easily using fstab. However, despite a number of attempts, I couldn’t convince it to allow me to write files remotely, only folders. It seems a number of people have had the same issue.

Fusedav, on the other hand, was much more reliable, writing files and folders without errors. It also seemed noticeably faster. Unfortunately, there didn’t seem to be any convenient way to mount the share automatically, and this would be important for my automated backup routine. To solve this, I put together a quick solution using a simple mount description file and an init script that gives me better control over the process. The idea and parts of the code are built on a solution I found by müzso. This goes one step further and enables it to run automatically at boot time.

Below are the steps I took, and the script I’m using. The steps are for Ubuntu, but should be easily modifiable to any flavour of Linux.

First, install fusedav.
[bash]sudo aptitude -y install fusedav[/bash]

Next, we do a quick test to check that fusedav will indeed connect to our WebDAV share.
[bash]sudo /usr/bin/fusedav -u username -p password http://mydomain.bingodisk.com /mnt/bingo[/bash]

We’re running fusedav in the foreground because it will be easier to kill that way once we’ve finished testing, so open a new terminal and check that you can connect.
[bash]
cd /mnt/bingo
ls -la
[/bash]

If all is well, the top level of your remote share should now be visible. I don’t like that you have to specify the password on the command line, but there doesn’t seem to be too much you can do about that at the moment.

Close the second terminal, and kill the fusedav process in the first by pressing Ctrl+c. Now, we’re going to create a directory and a simple mount description file that includes the URL, username and password. I’m going to call the file bingo, and later on this means that the share will be mounted at /mnt/bingo. The directory layout I went for is similar to how Apache and Nginx manage virtual hosts.

[bash]
mkdir -p /etc/fusedav/mounts-available
[/bash]

Now edit /etc/fusedav/mounts-available/bingo as root using your favourite text editor and put the following in it. The 3 segments are the URL to the WebDAV location, username and password, separated by a space.
[bash]
http://mydomain.bingodisk.com username password
[/bash]

Because this file contains your password in plaintext, you should secure it so that only the owner (root) can read it.
[bash]
sudo chmod 600 /etc/fusedav/mounts-available/bingo
[/bash]

Next, the init script, which you can download from here. You need to make it executable.
[bash]
cd /etc/init.d/
sudo wget http://jamietalbot.com/projects/fusedav/files/bingo
sudo chmod +x bingo
[/bash]

You can take a look at the file in its entirety. It’s not especially complex but the highlights are:

[bash]NAME=bingo[/bash]

You should alter bingo on this line to be the same as the name of the file you put in /etc/fusedav/mounts-available. This is the only change you have to make (and you only have to make it if you called your mount something other than bingo).

[bash]
MOUNT_FILE=/etc/fusedav/mounts-available/$NAME
MOUNT_POINT=/mnt/$NAME
[/bash]

These lines define where the mount description files are kept and where the share will be mounted.

[bash]
set — $(cat ${MOUNT_FILE})
URI=$1
USERNAME=$2
PASSWORD=$3

DAEMON_ARGS="-u $USERNAME -p $PASSWORD $URI $MOUNT_POINT"
[/bash]

This section splits the mount description into usable variables and sets the parameters for fusedav.

The script automatically creates a PID file which should help avoid any issues if you start the daemon more than once. It also starts fusedav as a background process, so it doesn’t interfere with your terminal.

Let’s check that the script works.
[bash]
su –
/etc/init.d/bingo start
cd /mnt/bingo/
ls -la
[/bash]

Hopefully, at this point you should see the remote share, as before.

Finally, we can add the script to the default runlevel so that it starts when the server boots up.
[bash]sudo /usr/sbin/update-rc.d -f bingo defaults[/bash]

And that should be that! If you need to stop the daemon use the following.
[bash]sudo /etc/init.d/bingo stop[/bash]

You can easily add further shares if necessary with the following steps as root (assuming the new share name of bongo).
[bash]
cp /etc/init.d/bingo /etc/init.d/bongo
chmod +x /etc/init.d/bongo
vim /etc/init.d/bongo
:%s/bingo/bongo/g
:wq
cat ‘http://bongo.remote.drive username password’ > /etc/fusedav/mounts-available/bongo
chmod 600 /etc/fusedav/mounts-available/bongo
/usr/sbin/update-rc.d -f bongo defaults
/etc/init.d/bongo start
[/bash]

At which point, /mnt/bongo should be ready and waiting.

8 responses to “Automatically Mounting WebDAV in Ubuntu with a FuseDAV Init Script”

  1. Jerome says:

    Thank you for this post! This is exactly what I wanted. My only issue is that the FuseDAV drive is mounted with root:root as the owner and group and when I attempt to “chown it” I get an error. Is there a way for this to be mounted with root:users as the owner and group?

  2. Jamie Talbot says:

    Hi Jerome,

    Don't know the answer to this I'm afraid – I noticed the same thing, but as I was the only user of the box it wasn't too much of an issue for me. If you find out though, please let me know!

  3. Andras says:

    Great post.
    I tried mount.cifs, davfs2, all of them gave me heart ache. Followed you post, was easy and looks like it works. I have run into one problem and not sure if I can ignore it:
    after executing the : su /etc/init.d/bingo start #(I changed the name from bingo…)
    got the following error msg: Unkown id: /etc/init.d/bingo

    checked the var/run directory > there is no bingo.pid file
    checked the script and it says in the start section: and it the PID file does not exist it should create it. Do you have any idea what is wrong?

    thanks
    Andras

    do_start()
    {
    # Return
    start-stop-daemon –start –quiet –pidfile $PIDFILE –exec $DAEMON –test > /dev/null
    || return 1

    set — $(cat ${MOUNT_FILE})
    URI=$1
    USERNAME=$2
    PASSWORD=$3

    DAEMON_ARGS=”-u $USERNAME -p $PASSWORD $URI $MOUNT_POINT”

    start-stop-daemon –start –quiet –pidfile $PIDFILE –background –make-pidfile –exec $DAEMON —
    $DAEMON_ARGS
    || return 2
    }

    • Jamie Talbot says:

      Hi Andras,

      Couple of things to try. Firstly, try the script exactly as is (i.e., without renaming bingo) and see if that works. If that works then you can move onto renaming. Have you granted +x permissions on /etc/init.d/your-name ? Have you changed $NAME in /etc/init.d/your-name to ‘your-name’?

      The final code block in the post shows an example of how to take a working mount definition ‘bingo’ and copy it to create another one. Did that work? If you don’t want the bingo script lying around, you can use ‘mv’ instead of ‘cp’ on the first line.

      Hope that helps a bit…

  4. Prosper says:

    Thanks a lot. This really saved my day.I had but one inconvenience and that is that the share was only readable by root. Since I do not like running Nautilus as root, I removed the script from startup and start it manually as a normal user. Of course, I had to change permissions on the file with the URL and password information, but it is reasonably well hidden. In order to create the .pid file I also had to make /var/run writable.

  5. […] Here is a good tutorial show you how to Automatically Mounting WebDAV in Ubuntu with a FuseDAV Init Script:  the face of it, davfs2 is more convenient, as it allows you to automount quite easily using fstab. However, despite a number of attempts, I couldn’t convince it to allow me to write files remotely, only folders. It seems a number of people have had the same issue. […]

  6. Curious says:

    Interesting article. Thanks for the pointers. Seems to work ok. A couple of issues: 

    ps -ef | grep fuse

    reveals the username and password of the dav share to all users. Not so secure.

    Also, I’d be interested to know if you have ideas for managing permissions on the local mount so that an ordinary user might be allowed access?

    Thanks again.

    • Jamie Talbot says:

      Hi,

      Yeah, it’s not perfect.  As I note above, because you type the password on the command line it is plainly visible in many ways – even htop will reveal it.  I’m not aware at the moment of anyway of calling fusedav without specifying a password.  Would be happy to hear otherwise.

      As for permissions, off the top of my head, you could create a low privilege account and give it ownership of the mount.  Then, instead of using sudo the command in the init script, use su newaccountname -c ‘fusedav …’ 

      I haven’t tried this however.

      Hope this helps somewhat.

      Cheers,

      Jamie.

Leave a Reply to Jamie Talbot Cancel reply

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