Automatically Mounting an External USB Hard Disk on the Raspberry Pi

How-to Guide: Automatically Mount an External USB Storage Device at Boot Time, and Within Emulation Station

Raspbian Logo
External Hard Disk - Image: Clipshrine.com
USB Symbol - Image: Clipshrine.com

This how-to guide shows a method to automatically mount an external USB drive on the Raspberry Pi. The general technique which I have adopted and is common, and whilst there are similar guides available, I have adapted the approach specifically for use on a Pi running Raspbian Lxde Desktop, Kodi Media Center, and Emulation Station with RetroPie.

In this article I aim both to demonstrate and expand upon the steps involved, whilst highlighting some issues which I have encountered when using this approach, and providing their resolutions.

Topics

A Little Background Information

My Raspberry Pi 3 is setup to serve triple duty as a lightweight PC replacement, running the Raspbian desktop, as a media center using Kodi, and as a retro video game emulator suite, via RetroPie. I have my machine set to boot to a custom menu at the command prompt, rather than directly to the desktop, to facilitate easy switching between these options. Please see the Related Posts section for setup guides detailing how this was achieved.

The Raspbian kernel does not automatically mount external USB drives by default; this isn’t an issue when launching the Kodi media center, or the desktop, as both have the capability to detect and mount a USB hard disk or flash storage device once it is connected.

If the system is configured to boot directly to the command line, or to an application suite such as RetroPie‘s Emulation Station GUI, a connected drive will be inaccessible, although it is detected as present by the system.

This was a problem I needed to resolve, as I have customised RetroPie’s RetroArch subsystem to allow the Libretro emulators to record live audio-video footage during gameplay, an intensive task which is generally runs smoothly only when the output is directed to a fast storage medium such as an external hard disk.

For further details on Libretro and RetroArch, please see the What is RetroArch? section in the post RetroPie system overview – software and hardware components, and the related article: RetroPie Emulation: RetroArch, Libretro, and the Power of the Options Menu

Automount Options

What is the mount Command

The mount command instructs the operating system that a file system is ready to use, and associates it with a particular point in the overall file system hierarchy (its mount point) and sets options relating to its access.

Mounting makes file systems, files, directories, devices and special files available for use and available to the user

[Definition from Wikipedia]

As is typical on a Linux system, there are multiple ways to achieve the goal of automatically mounting an external USB drive; generally these directly or indirectly use the mount command, which in turn utilises entries in the fstab file.

One workable, if unwieldy, option on a system with the Raspbian desktop installed is to launch Lxde, attach a USB drive, allow it to be automatically mounted, then exit to the command line. The drive will remain mounted and be present when Emulation Station, for example, is started.

Another approach is to use a wrapper named pmount; this command provides an automounting mechanism similar to that provided by the Raspbian desktop, without requiring explicit entries in fstab. The pmount package is not installed by default.

Modifying the fstab system file is a simple, if somewhat inflexible, alternative. It has the advantage of requiring no additional packages, and of clearly and consistently mapping a specific external device to a specific mount point.

This latter point is useful for my requirements given that when recording from RetroArch emulators, the launch command must explicitly provide the path and file name as parameters.

What is fstab

According to the Wikipedia fstab entry the fstab (or file systems table) file is a system configuration file commonly found at /etc/fstab [which] typically lists all available disk partitions and other types of file systems and data sources… and indicates how they are to be initialized or otherwise integrated into the larger file system structure.

The fstab file is read by the mount command, which happens automatically at boot time to determine the overall file system structure, and thereafter when a user executes the mount command to modify that structure.

Using the lsusb Command

A step which is not strictly required, but may well be worth undertaking to avoid headaches later, is to run the lsusb command to determine that a drive has been detected by the system.

Before connecting an external USB storage device, at the terminal issue:

lsusb

This will output a list of all USB devices attached to the system, including the in-built USB hub, and USB-based ethernet.

Next, plug in the USB storage device, and re-run the command; if the system has successfully detected the device, a new entry will appear in the list. In the following image a Kingston USB drive (circled in yellow) has been connected:

lsusb Command - Before and After Connecting USB Drive
lsusb Command – Before and After Connecting USB Drive

Automount from Boot or within Emulation Station

In the following steps we’ll modify the fstab file to allow direct automounting, either as the system boots, while at the command line, or whilst Emulation Station is running.

First we need to obtain the external USB device’s unique UUID. This can be achieved either by issuing the command:

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

Or, my personal preference, using the blkid command, which has the advantage of displaying the drive’s label and file system type, making identification of a specific device much easier. A the command line or within a terminal window, run:

sudo blkid

The resulting output of using blkid should appear similar to the following:

/dev/mmcblk0: PTUUID="b3ff2c23" PTTYPE="dos"
/dev/mmcblk0p1: SEC_TYPE="msdos" LABEL="boot" UUID="2A5C-3F20" TYPE="vfat" PARTUUID="b3ff2c23-01"
/dev/mmcblk0p2: UUID="c320462b-3cdc-49c1-ac93-41f46268295d" TYPE="ext4" PARTUUID="b3ff2c23-01"
/dev/sda1: LABEL="3TB-HDD" UUID="FA3033A6A3E7F82B" TYPE="ntfs"

Entries in the form: /dev/mmcblk0* are partitions on the Raspberry Pi‘s SD Card.

In the case of a Pi with a single USB drive attached, the external disk can be found at /dev/sda1 (here, an entry with a LABEL value of “3TB-HDD”).

Choosing a Mount Point Directory Name

I use a name which differs from the drive’s label, as the Raspbian desktop’s automatic mechanism uses this when creating a temporary mount point directory.

Please see the section Notes Regarding Raspbian Desktop’s Automount Feature for further details.

Next, we need to create a directory to act as the mount point for the external drive:

sudo mkdir /media/pi/3TB-HDD-AUTO

Set ownership of the new directory to the Pi user, and the Pi group:

sudo chown -R pi:pi /media/pi/3TB-HDD-AUTO

Please note: this approach creates an entry unique to a specific external USB drive; if multiple drives are to be used it is necessary to repeat the procedure for each, adding additional lines to the fstab file. This contrasts with the more flexbile mechanism incorporated into the Raspbian desktop, and the pmount wrapper package.

The next step involves editing fstab. As good practice, it’s worth beginning by making a backup of the file:

sudo cp /etc/fstab /etc/fstab.bak

Next, edit the file. In this example I’m using the in-built nano editor:

sudo nano /etc/fstab

At the end of the file add the following (all on a single line), substituting the previously determined UUID and the desired mount point directory name:

UUID=FA3033A6A3E7F82B /media/pi/3TB-HDD-AUTO ntfs nofail,noatime,auto,users,rw,uid=1000,gid=100,umask=0002 0 0

The fstab Parameters

uid=1000 is the pi user
gid=100 is the pi group
umask=0002 sets permissions for the mount point to rwxrwxr-x
nofail,noatime prevent system waiting 1 minute 30 seconds during boot if the external disk is not attached. This also prevents the system entering ’emergency mode’ at the console after booting.

Finally, the system must be rebooted for the updates to fstab to take effect.

Following the reboot, whenever the USB drive is plugged in it will automatically be mounted, and accessible from the directory specified in the fstab entry.

Manually Unmounting an Automounted USB Drive

To dismount a drive which has been automatically mounted via an fstab entry, use the umount command. Please note that the spelling is umount and not unmount:

sudo umount /path/directory

The value of /path/directory must match the mount point specified in the corresponding fstab entry, for example:

sudo umount /media/pi/3TB-HDD-AUTO

Using an External Drive with an NTFS File System

On a reasonably new Raspbian installation the operating system will include a package which provides compatibility with the Microsoft NTFS file system, as used on Windows systems dating back to NT4 and Windows 2000.

If you are experiencing problems using an external drive formatted as NTFS, check if the ntfs-3g package is installed:

dpkg -L ntfs-3g

Assuming ntfs-3g is installed, the system will display a list of directories containing the package’s components, otherwise the following message is shown:

“dpkg-query: package ‘ntfs-3gc’ is not installed”

Should this be the case, you can install the package by issuing the command:

sudo apt-get install ntfs-3g

Notes Regarding Raspbian Desktop’s Automount Feature

If an external USB storage device which is present in fstab is connected at the Raspbian desktop, it will automount to the directory specified. This contrasts with the default behaviour, which is to use the device’s label to automatically create a temporary directory of the same name within /media/pi.

If a device mounted via an fstab entry is unmounted via the Ejector Panel item on the Raspbian desktop, the associated mount point directory will not automatically be removed. This is in contrast to the temporary folders created by the Raspbian desktop automount mechanism.

When a drive present in fstab, with a mount point directory matching the drive’s label, is connected only once the desktop has loaded, error messages regarding file locks, and ‘temporarily unavailable’ warnings may be displayed. This appears to be due to the system attempting to create a temporary directory with the same name. The drive is, however, mounted and accessible.

Alternatively, I have witnessed the desktop automount mechanism creating a temporary mount point directory name comprising the drive label plus a numeric suffix, thus avoiding the conflict.

Given the possibility of conflicts between the two mechanisms, it is worth ensuring the fstab specifies a mount point which differs from the device’s label.

A Labour of Love

Retro Resolution is entirely a labour of love. Please consider offering a donation if the information here has helped illuminate, enlighten, or otherwise assisted you!
Donate Using PayPal

About
Disclaimers
Privacy Policy
Terms and Conditions
© Retro Resolution

Advertisement

5 thoughts on “Automatically Mounting an External USB Hard Disk on the Raspberry Pi

  1. Bem explicado, mas não consigo criar a pasta /home/pi (erro => Acesso negado.
    Código de erro: 3
    Mensagem de erro do servidor: Permission denied

    Like

Please Leave a Reply! Anonymous comments and 'markdown' formatting enabled.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s