Automatically mount multiple drives

If you are building your own file server, the chances are you would have multiple hard drives or memory sticks for storing all your files on. Raspbian, the recommended linux distribution for the Raspberry Pi doesn’t automatically mount new drives by default on Raspbian Wheezy, and on full Jessie is mounted to /media/pi.

What we plan to achieve

Using the Raspberry Pi as a file server is one of the great uses of the tiny computer, as it has a very low power consumption compared to a full desktop computer performing the same role, so it can be left in a cupboard near a wired router sharing your pictures, media or music around your network to other computers or smart phones.

If you happen to have a few more Raspberry Pi’s being used as media centres, they can all share the same media regardless of which TV you are using cutting down on duplicate files.

Getting started

If you don’t have the latest Raspbian set up on your SD card, or creating a file server from scratch, follow this article first to get set.

If you plan on using an NTFS partition drive, the drivers will need to be installed first by typing (internet connection required);

sudo apt-get update
sudo apt-get install ntfs-3g

NOTE, this article requires editing some key files to the operating system so take your time when editing files.

Plug everything in (read this article if it is your first time) and log in to the terminal, or open LXTerminal if your using the graphical interface.

Finding your USB devices

We will be using a few commands through this guide, which are;

df -h This displays the mounted drives, their respective free space (-h displays file sizes in a human readable format)

blkid To display the UUID of a given partition that we need

sudo nano A simple command line text editor (preceded by sudo for administrative privileges)

/etc/fstab The file used to automatically mount drives to become accessible upon boot

 

With your Raspberry Pi ready to use, lets create the locations to access the files – The mount points. On a Windows PC, this is defined by drive letters (C:\, D:\ etc) but on linux they look like regular folders that you can navigate to.

My personal preference is to store them in /media/, which will be used throughout this article, but essentially it can be stored almost anywhere, such as the home directory; /home/pi/harddrive.

First create the mount point (folder) for the first hard drive;

sudo mkdir /media/pictures
ls /media

The last command just verifies the folder was created

pic1

Plug in the first drive in to a USB port (doesn’t matter which, linux isn’t fussy), if you are using the graphical interface just cancel any pop up for mounting the drive.

The part we are interested in is called the UUID, the Universally Unique Identifier of the partition. If the drive has multiple partitions it will list them individually, and if a partition is modified, a new UUID is created.

sudo blkid

Listing all partitions, including ones used by Raspbian (starts mmcblk0).

pic2

On my USB drive, I have 2 partitions on SDA, and labelled them “first”, an NTFS partition and “second” as a FAT partition, and listed just after is the UUID number. This number we will need for the next step, so copy or make a note of the EXACT numbers and letters.

 

Editing the fstab file

Each line in the file corresponds to a separate entry, so as we are adding a new entry, this will be done on a new line.

sudo nano /etc/fstab

The first few lines are crucial to the running of Raspbian, so don’t make any changes to these!

pic3

Using the arrow keys, go down to the end to begin a new line, and start it with the UUID of YOUR partition, not the example below;

UUID=01D18B2DCEBA7390

Then follow with the mount point we created above, in this example is /media/pictures

UUID=01D18B2DCEBA7390 /media/pictures ntfs

Followed by the file type, NTFS in this example followed by;

UUID=01D18B2DCEBA7390 /media/pictures ntfs defaults,auto,user,rw 0 0

Note the space between everything except the comma section near the end.

Take a deep breath, and re-read and make sure the UUID is correct to your partition, the mount point is correct and the options at the end are correct.

pic4

CTRL + X
y
enter

This will exit nano, save the file and overwrite it.

Testing it stuck

Lets check the file worked;

ls /media/pictures

Doing this should list everything in the folder, but as it hasn’t mounted it should be blank, now to mount the drive (this will be done automatically on boot normally);

sudo mount -a

Error?

If you are greeted with the error;

Mount is denied because the NTFS volume is already exclusively opened.
The volume may be already mounted, or another software may use it which could be identified for example by the help of the 'fuser' command.

This means that Raspbian Jessie has already mounted the USB drive in /media/pi/. So we either need to restart (easiest) for the changes to take effect by typing sudo reboot or by unmounting the drive before continuing; remember my label was “first” for the partition

Check the current status of the partitions by typing;

df -h

pic7

And unmount the drive;

ls /media/pi
sudo umount /media/pi/first
sudo mount -a

This will remove the old mount point associated with the partition and sudo mount -a will action the changes in /etc/fstab.

pic8

Finishing up

And a quick check again;

ls /media/pictures

pic5

A quick test we can write to the drive;

echo "Hello world" >> /media/pictures/test.txt
cat /media/pictures/test.txt

That is the first drive done, many more can be added in exactly the same way by repeating the steps above giving each entry a new line.

Now that Raspbian will automatically mount drives in a given location this makes remembering which drive is which and manually mounting drives upon restart or power failure a thing of the past!

What next?

To make the server a proper file server, the files should be easily accessible from anywhere and Samba handles this perfectly, available in the next part.