Cloud Storage On Your Raspberry Pi

SD-Card OK, the Raspberry Pi is a small computer with modest storage capacity. Mine sometimes run on 2 GB SD cards, with plenty of space to spare. But wouldn't it be nice to expand the storage capacity, for free?
My preferred way to expand storage capacity on my RPi is to use sshfs to connect a local or remote hard disk. All you need for that is an always-on computer which can be reached over ssh. This way storage capacity is virtually limitless, but not quite for free If your RPi is running 24/7, your host computer which provides the storage has to run 24/7 too.

Cloud services on the internet may be a good alternative for your storage needs. They are completely for free as long as your storage needs are lower than the free space offered by the storage provider.
You can even share the same space among multiple Raspberry Pies and your desktop computer. That way data gathered by all of your equipment out there is always instantly available on your desktop computer.
Yet another possibility is to send commands to your remote Raspberry Pi through this cloud disk and let the Raspberry Pi send the results of these commands back the same way.

Which Cloud Service To Choose?

Dropbox There are plenty cloud services out there. Possibly the best known one is Dropbox is a syncing service, which means that you'll need the same space locally on all the machines the accounts syncs to. If you have 1 GB stored on Dropbox, you'll need 1 GB of storage for your local copy on your local disk system.
Dropbox is a good concept for desktop and laptop use, but it is not what we are after on our RPi. We want extra space. On top of that there is no ARM based client software for Dropbox, at least not that I am aware of.
So let's drop Dropbox from our selection list.
Similar problems exist for Google Drive and Microsoft's Skydrive and probably a dozen other cloud services out there. Then I became aware of an extra feature offered by an unexpected service, lacked a syncing client for a long time and still doesn't have a Linux syncing client today. Let alone a client for our ARM based Raspberry Pi. This is no problem for us because syncing is not what we are after anyway.
What makes stand out above the others is the ability to connect to your cloud drive over the webdav protocol, which is well supported by Linux.
You can sign up for a account for free and you'll get 5 GB of free cloud storage, which can all be available to your Raspberry Pi, or to your entire fleet of Raspberry Pies. Your desktop computer can also connect to that cloud space using webdav, which simplifies sharing files with your RPi fleet.

Hidrive While searching the web I found another free 5 GB webdav service called Hidrive. I had never heard of them. But it works equally well as You can even connect to both services simultaneously, giving you a whopping 10 GB of free extra storage space for your RPi fleet.

Setting Up Cloud Storage On Your Raspberry Pi

The examples below show you how to set up a webdav connection for Setting it up for other storage providers is equally simple. You can even connect to multiple storage locations simultaneously if you need more storage space.

Setting up webdav is quite simple. First you need to install one program:

sudo apt-get install davfs2

Then create a mount point, from which you are going to access the cloud storage. I simply create a directory in user pi's home directory. You can choose the location for the mount directory wherever you want, and you can name it whatever you want.

mkdir /home/pi/box

That's all you have to do to be able to connect to your cloud disk manually. Type or copy/paste the next command to test this:

sudo mount -o uid=pi -o gid=pi -t davfs /home/pi/box

After that you'll have to supply your user name and password. Then you can browse your cloud drive, which is now mounted on /home/pi/box. When you're done playing you can un-mount the drive with the following command. Please note that you should close all files which are stored on your cloud drive and leave the mount directory before you can un-mount.

sudo umount /home/pi/box

Adding Some Automation

OK, nice, now we can manually connect to our cloud drive. But we can completely automate this process. Let's start by supplying the user name and password automatically and work our way gradually to a fully automated mounting of your cloud drive.

Edit the file /etc/davfs2/secrets and add the next line at the bottom of this file:  username  password

Replace username and password by your own user name and password of course. Also make sure the file /etc/davfs2/secrets can only be read by root. If your RPi is used by multiple users you may consider creating the directory .davfs2 in your home directory and create a file named secrets in it containing the line above. Then ensure that only you, the owner of the file, can read it. This way each user on your RPi can have his/her own cloud drive settings.

Now you can mount your web drive with the same mount command as above. The only difference this time is that you don't have to supply the user name and password any more. They are taken from the secrets file.
You can add multiple secrets into this file, all belonging to a different storage provider. You can't have two different secrets for one and the same storage provider though, otherwise webdav won't know which one to choose from.

Until now only the root user can mount and un-mount the cloud drive. This is not very desirable, especially on a multi user system. So let's change that now.

Add yourself, and all users who need to be able to mount a cloud drive, to the group davfs2.

sudo usermod -aG davfs2 pi

After that you'll have to log out and log back in to make the change effective. You can verify if you are added to the davfs2 group by typing the id command.

Now you'll have to give normal users the permission to run davfs2, without the need for root privileges. For that you'll have to enter the next command:

sudo dpkg-reconfigure davfs2

Select "Yes" in the menu which appears and press enter.

From now on you can mount the cloud drive as a normal user by typing the command:

mount -o uid=pi -o gid=pi -t davfs /home/pi/box

Un-mounting the drive as normal user can be done by the command:

fusermount -u /home/pi/box

Nice, it works. But I still think the mount command looks a bit complicated. Let's change that by adding the next line to the end of the file /etc/fstab: /home/pi/box  davfs  rw,noexec,noauto,user,async,uid=pi,gid=pi  0  0

With this line in place mounting the cloud drive is only a matter of typing the command:

mount /home/pi/box

That's a lot easier to remember, isn't it.

And finally we're going make mounting fully automatic. The cloud file system will be mounted when the system boots, which makes it available at all times.

Replace the line you have added to the file /etc/fstab by the following line: /home/pi/box davfs rw,noexec,auto,user,async,_netdev,uid=pi,gid=pi  0  0

Needless to say your RPi does need an internet connection for this all to work. That's what the option _netdev in the line above is for. It will only mount the cloud drive if a network connection is available.


Since Raspbian has switched to Debian Jessie, with systemd, a serious issue has surfaced. When setting up /etc/fstab like in the last example, the Raspberry Pi doesn't boot properly anymore. It will boot into a so called emergency mode and will display the message below:

Welcome to emergency mode! After logging in, type "journalctl -xb" to view
system logs, "systemctl reboot" to reboot, "systemctl default" to try again
to boot into default mode.
Give root password for maintenance
(or type Control-D to continue):

What makes matter worse is that it also fails to start the ssh daemon, which prohibits you to log in to your Pi remotely. You'll need to hook up a keyboard an a monitor in order to solve this problem.

I have read about some possible fixes to this problem, including adding autofs or _netdev to the davfs options, but none of them seemed to work for me. The only thing that did work was: /home/pi/box davfs rw,noexec,noauto,user,async,_netdev,uid=pi,gid=pi  0  0

So I have changed auto in to noauto. It works, the Pi will boot normally again, but the file system is no longer mounted automatically. You'll have to do that manually by typing mount /home/pi/box after every reboot.

Like just about everything in Linux this can be automated again, in several different ways. I've chosen for adding a line to root's crontab. You do that by typing the command sudo crontab -u root -e and add the following line at the end of the file:

@reboot /bin/sleep 5; /bin/mount /home/pi/box

That should mount the drive, at each reboot, after a 5 second delay. I have chosen the delay time very carefully by counting the fingers on one of my hands.