Auto-mount an external drive on boot

From Parallel Library Services
Jump to navigation Jump to search

If you are running a library from a self-hosted server computer such as a Raspberry Pi, it's a good idea to have an external USB drive mounted for storage of media. This is because the only other storage format on the Pi, the SD card that holds the operating system, has limited capacity, and also can easily become corrupted with too much overwriting. Connecting a hard drive, or USB flash drive allows you to store a lot more media, and can also be used for backups of a collection.

You will also want to make sure that your drive is mounted in the file system each time the computer starts, automatically. To make an external drive auto-mount on boot, follow these steps:

Step 1: Plug In The Device

Plug it in to an available USB slot.

Step 2: Identify The Device's Unique ID

In order to find the unique reference (UUID) for your drive run the following command in the terminal:

ls -l /dev/disk/by-uuid/

This will give you an output that should list your drive :

Bootleg uuid.png

The line will usually refer to /sda and in this example it is sda1. My ID is 989B-E900. Note down yours. You would need to repeat this step if you wanted to use a different device as the UUID would be different.

Step 3: Create a Mount Point

A mount point is a directory that will point to the contents of your drive. Create a suitable folder :

sudo mkdir /media/usb

I’m calling my mount point usb but you can give yours whatever name you like. Keep it short as it saves typing later on.

Now we need to make sure the Pi user owns this folder:

sudo chown -R pi:pi /media/usb

You will only need to do this step once.

Step 4 – Manually Mount The Drive

To manually mount the drive use the following command:

sudo mount /dev/sda1 /media/usb -o uid=pi,gid=pi

This will mount the drive so that the ordinary Pi user can write to it. Omitting the -o uid=pi,gid=pi would mean you could only write to it using sudo. Now you can read, write and delete files using /media/usb as a destination or source without needing to use sudo.

Step 5: Un-mounting The Drive

You don’t need to manually un-mount the drive if you shutdown your Pi, but if you need to remove the drive at any other time you should un-mount it first. Only the user that mounted the drive can un-mount it. Notice that the command is umount, not unmount.

umount /media/usb

If you used the fstab file to auto-mount it you will need to use:

sudo umount /media/usb

Step 6: Auto Mount

When you restart your Pi your mounts will be lost and you will need to repeat Step 4. If you want your USB drive to be mounted when the system starts you can edit the fstab file :

sudo nano /etc/fstab

Then add the following line at the end :

UUID=989B-E900 /media/usb vfat auto,nofail,noatime,users,rw,uid=pi,gid=pi 0 0

The nofail option allows the boot process to proceed if the drive is not plugged in. The “noatime” option stops the file access time being updated every time a file is read from the USB stick. This helps improve performance. This is what my fstab file looks like:

Bootleg fstab.png

Make sure you set the correct UUID. Use CTRL-X followed by Y to save and exit the nano editor.

Now reboot:

sudo reboot

Your USB drive should be auto-mounted and available at /media/usb.