User avatar
socialdefect
Posts: 110
Joined: Mon Jun 25, 2012 9:02 pm
Location: Tilburg, the Netherlands
Contact: Website

HOWTO: Set up Pi for disk sharing

Sun Sep 07, 2014 1:11 am

WARNING: the settings provided in this howto are meant to be used on your local network. So use a local IP address to connect to your Pi. If you use a public IP address and don't have an encrypted home folder on your client your files could be accessed by others when you lose your laptop.

Since I own multiple external hard drives and use those on multiple computers I thought it would be nicer to have them all connected to my local network instead of switching cables. Naturally this can be done very easy using a RaspberryPi. I've tried many ways of sharing disks and connecting to them but only one of those ways seems to be the best by far; SSHFS.

SSH is a secure shell application that allows remote access. SSHFS can use SSH to access a filesystem over this secure connection. It's not only a very secure way to connect but also very fast and stable, even from remote locations. On local networks SSHFS is fast enough for central storage and direct access of audio/video files so ideal for a media center or NAS.

When using autofs on your client pc it's possible to automatically connect to the shared storage when accessing it's configured mountpoint and will be disconnected after a certain period of inactivity.

Setting this up is easy as Pi. This HOWTO explains how to set it up using a Linux client pc using autofs. On the Mac it should work quite similar but you can also use other tools like SSHFS for Mac or Android or WinSSHFS on Windows.

On the RaspberryPi:

Enable the ssh server (is active by default on most OS)

Code: Select all

sudo apt-get install openssh-server
sudo update-rc.d ssh defaults
The SSH server will now start when you start your Pi.

Mounting external disks in a set location using fstab:
Next we need to mount the disks and make sure they always get mounted in the same location. The most easy way to do this is by mounting them by Label or by uuid. I find labels the most effective way since the uuid changes when you re-partition or format the disk. When using labels you can easily replace a disk without having to re-do any settings.

You can see the label and uuid of a disk by mounting it first and then execute the following:

Code: Select all

ls -l /dev/disk/by-label/
or

Code: Select all

ls -l /dev/disk/by-uuid/
This will give you output similar to this:
lrwxrwxrwx 1 root root 10 Sep 4 18:35 ExtHDD_01 -> ../../sda1
In this example the label for /dev/sda1 is ExtHDD_01. Next we need to know the filesystem type of the disk, if you don't know this an easy way to find out is by typing the command;

Code: Select all

mount
One last thing is to create the mountpoint for the disk. A nice place to create this is in the /media directory.

Code: Select all

sudo mkdir /media/ExtHDD_01
Now open fstab in nano (or any other text editor):

Code: Select all

sudo nano /etc/fstab
For every disk you want to mount you need to make an entry in /etc/fstab, for the example disk /dev/sda1 the entry will look like this:
LABEL=ExtHDD_01 /media/ExtHDD_01 xfs default 0 0
Once you've added all your disks you can test your configuration by executing:

Code: Select all

sudo mount -a
This will mount all entry's in fstab. If there are any errors you will be noticed. Most common errors are typo's, missing mountpoints and wrong or unknown filesystem type. The first two are an easy fix and a missing filesystem type can be fixed by installing the corresponding fstools. (ie; dosfstools, ntfs-3g, xfsprogs, hfs-utils etc...)

Once all disks have been set-up correctly it's time to configure the client.

On the Client PC: (Debian/Ubuntu)

Install the tools we need:

Code: Select all

sudo apt-get install autofs sshfs openssh-client
If you don't have an authentication key-pair set-up already create one like this:

Code: Select all

cd
mkdir .ssh
cd .ssh
ssh-keygen
When prompted for a password just type [Enter] twice for an empty password.

Next add your key to the server's authorized_keys file:

Code: Select all

ssh-copy-id username@IP-OF-YOUR-RASPBERRYPI
(enter password)

You should now be able to connect with the Pi using the key for authentication. You can test this by connecting to the Pi:

Code: Select all

ssh username@IP-OF-YOUR-RASPBERRYPI
If you can connect without having to enter a password it works and you can type: exit to disconnect from the Pi.

Configure autofs;
To configure autofs we need to create a directory in which to mount the remote filesystems. Also on the client we choose a new directory under /media. Let's create this first:

Code: Select all

sudo mkdir /media/RemoteFS
All there's left is to configure autofs. To do this you need the uid ( user id) and gid (group id) for the user you like to use for autofs. To find these id's for your current user enter:

Code: Select all

cat /etc/passwd | grep $USER
Example output:
username:x:1000:1000:Full Name,,,:/home/username:/bin/bash
In the output you'll see two numbers, the first is the uid and the second the gid.

Now we need to add a default configuration for SSHFS in the master configuration file /etc/auto.master.

Code: Select all

sudo nano /etc/auto.master
Add the following and save your changes: (make sure to change the uid and gid!)
/media/RemoteFS /etc/auto.sshfs uid=1000,gid=1000,--timeout=30,--ghost
Next we need to create the sshfs configuration for autofs in which we can configure the remote mounts.

Code: Select all

sudo nano /etc/auto.sshfs
For the example the entry will look like this:
ExtHDD_01 -fstype=fuse,rw,nodev,nonempty,noatime,allow_other,max_read=65536 :sshfs\#username@IP-OF-YOUR-RASPBERRYPI\:/media/ExtHDD_01
Make an entry for all the disks you like to configure or simply make one entry for all mounts under /media on your Pi like this:
RasPi -fstype=fuse,rw,nodev,nonempty,noatime,allow_other,max_read=65536 :sshfs\#username@IP-OF-YOUR-RASPBERRYPI\:/media
Next restart the autofs daemon:

Code: Select all

sudo service autofs restart
ls /media/RemoteFS/
If everything is set up correctly the last command should display the RasPi or ExtHDD_01 folder. from now on you can access the files simply by navigating to /media/RemoteFS/ using any application on your client pc.
== If it's not broke... I'm not done fixing it! ==

Return to “Advanced users”