• 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.

sudo aptitude -y install fusedav

Next, we do a quick test to check that fusedav will indeed connect to our WebDAV share.

sudo /usr/bin/fusedav -u username -p password http://mydomain.bingodisk.com /mnt/bingo

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.

cd /mnt/bingo
ls -la

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.

mkdir -p /etc/fusedav/mounts-available

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.

http://mydomain.bingodisk.com username password

Because this file contains your password in plaintext, you should secure it so that only the owner (root) can read it.

sudo chmod 600 /etc/fusedav/mounts-available/bingo

Next, the init script, which you can download from here. You need to make it executable.

cd /etc/init.d/
sudo wget http://jamietalbot.com/projects/fusedav/files/bingo
sudo chmod +x bingo

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

NAME=bingo

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).

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

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

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

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

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.

su -
/etc/init.d/bingo start
cd /mnt/bingo/
ls -la

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.

sudo /usr/sbin/update-rc.d -f bingo defaults

And that should be that! If you need to stop the daemon use the following.

sudo /etc/init.d/bingo stop

You can easily add further shares if necessary with the following steps as root (assuming the new share name of bongo).

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

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