Automatically Mount your ownCloud folder with webDAV

Posted on December 02, 2014 ~4 minutes reading time

Warning! This post is old!

This post was originally written in 2014, and I no longer use this method for mounting folders. I kept the post here as it still sees some traffic, but it is now hidden from the blog archive. You might still find the information useful here, but the content is not being updated / maintained.

Storage Containers

Both of my RaspberryPi's run without any input devices attached to them, so I don't run GUI desktop environments on them. However, I do still want to be able to sync my ownCloud folder with them. Unfortunately the official ownCloud client is severely lacking when it comes to command line support. So here is how I get around it, by mounting my cloud folders via webDAV.

The Solution

The instructions in this post were devised on a RaspberryPi running Raspbian GNU/Linux 7 (wheezy), but they should be applicable to other setups where you want command line access to an ownCloud folder.

In order to access a webDAV share as a filesystem, you'll need to install the davfs2 package. It's in the repositories by default, so this step is easy:

sudo apt-get install davfs2

davfs2 has a quirk if you are using Debian (or a Debian based variant), in which it doesn't have the correct permissions to run as a non-root user. You may see the error: /sbin/mount.davfs: program is not setuid root This can be fixed using one of the following commands:

sudo dpkg-reconfigure davfs2
# or a more manual approach
sudo chmod u+s /usr/sbin/mount.davfs

Now add a line to your /etc/fstab file to make mounting easier. Note we are specifying noauto here, we will perform the automatic mounting step later. /etc/fstab is often run before network initialisation, and therefore an automatic mount here will fail. 	/media/cloud 	davfs 	noauto,user		0		0

Add your user to the correct group:

usermod -a -G davfs2 <username>

Now you should be able to mount your share and work with your files. You will be prompted for your username and password, and if you have set up ownCloud with a self-signed certificate, then you will be asked to confirm that you trust it.

mount /media/cloud
umount /media/cloud

Storing Credentials

It's pretty inconvenient to have to type in your username / password and accept your self-signed certificate every time you want to mount your drive. It's also a hurdle which needs to be overcome if you want to be able to automatically have your webDAV share mounted. So let's break it down and take care of the username / password first.

davfs2 makes use of the /home/user/.davfs2/secrets file for storing such credentials. You simply need to create this with the correct permissions and then provide the host, username and password details.

touch /home/user/.dav2fs/secrets
chmod 0600 /home/user/.dav2fs/secrets
echo " <username> <password>" > /home/user/.dav2fs/secrets

After that step, you will be able to mount your share without providing a username and password, but you will still be asked if you trust your self-signed certificate (if you are using one). If that's the case, let's take care of it now.

Accepting a Self Signed Certificate

If you created a self-signed certificate for ownCloud, then you should have the server.crt & server.key on your server. We need to convert the server.crt into a .pem certificate that will work with davfs2:

openssl x509 -in server.crt -out server.pem -outform PEM

You'll need to store the new server.pem that we just created in /home/user/.davfs2/certs/server.pem (you may have to create the certs folder). Then you need to make davfs2 aware of it's existence, which is done in the config file /home/user/.davfs2/davfs2.conf.

# find the line which starts:
# servercert
# and change it to:
servercert server.pem

Once you have done that, you should be able to mount your webDAV share without any user input being required. Give it a test:

mount /media/cloud
umount /media/cloud

Automatic Share Mounting

Remember when we edited the /etc/fstab file we used the noauto option? Well, what if you do want your share mounted automatically?

Because we can't mount a webDAV share until after the network connection has been established, we can't use the auto option in our /etc/fstab file. Instead, we will take advantage of everything which we have set up so far to use one simple command to take care of this.

Your /etc/rc.local file is executed after boot, and so is an excellent place to provide the functionality we are looking for, so simply add:

mount -a
# make sure this command is above the exit 0 command

Assuming you have a network connection when you boot, you should now have your ownCloud webDAV share mounted automatically.