Mounting a USB hard drive on startup on Ubuntu Core

A photo of a Raspberry Pi 4 connected to a USB external hard drive

As you’ll be aware from my regular posts about it, I have a Raspberry Pi 4 running Ubuntu Core, which acts as a server for Home Assistant, Plex and Calibre-Web. Here’s how I’ve set it up to mount an external USB hard drive on boot up.

As it’s a Raspberry Pi, the operating system and binaries set on a microSD card, which in this case is a mere 16 GB. Whilst the me of 20 years ago would have been astounded at the concept of something so tiny holding so much data, 16 GB isn’t much nowadays. So, I have a 1 TB external USB hard drive for storing the media files for Plex and Calibre-Web.

Ubuntu Core doesn’t automatically mount USB storage devices on startup unless you tell it to, and the instructions for doing so are different when compared with a regular Linux distro.

There’s no fstab

Most Linux distros, including regular Ubuntu, include fstab for managing file systems and mounting devices. But Ubuntu Core is designed to be a lightweight distro to act as firmware for Internet of Things devices, and so it doesn’t include many tools that are common in other Linux distros. fstab is one such tool which is missing.

You can, of course, just mount a USB drive manually with the following:

sudo mkdir /media/data
sudo mount /dev/sda1 /media/data

But this won’t persist when the computer restarts. After a bit of searching, I found a solution on StackExchange; it’s for Ubuntu Core 16, but works on 22 as well.

How to tell systemd to mount your USB hard drive

It should go without saying that you should back up your system before doing any of this. If you make a mistake and systemd stops working, your device could become unbootable.

Firstly, you’ll need to run sudo blkid to list all of the file systems that Ubuntu Core can see. Find the one that starts with ‘/dev/sda1’ and make a note of the long hexadecimal string that comes after UUID – it’ll probably look something like ‘2435ba65-f000-234244ac’. Copy and save this, as this identifies your USB hard drive.

Next, you’ll need to create a text file. Ubuntu Core only seems to offer the Vi text editor, which I haven’t bothered to learn to use properly. My favoured text editor is nano, but it’s not available on Ubuntu Core. Therefore, my recommendation is to create a file on another device and FTP it across. The file should be called media-data.mount; it’s really important the file name matches the intended mount point. For example, if you’re instead planning to mount the USB hard drive to /mnt/files, this text file would need to be called mnt-files.mount.

Here’s the template for the file:

[Unit]
Description=Mount unit for data

[Mount]
What=/dev/disk/by-uuid/[Your UUID]
Where=/media/data
Type=ext4

[Install]
WantedBy=multi-user.target

You’ll need to paste in the UUID for your USB hard drive where it says ‘[Your UUID]’. You’ll also need to match the file system type; I have my external USB hard drive formatted as ext4 for maximum compatibility with Linux, but yours may use ExFAT or NTFS.

This file needs to be saved to /etc/systemd/system/media-data.mount . You can either use vi to create and save this file directly or FTP it across and copy it over.

There are three further commands to run in turn:

sudo systemctl daemon-reload
sudo systemctl start media-data.mount
sudo systemctl enable media-data.mount

If you’ve done this correctly, then the next time you restart your device, your USB hard drive should mount automatically. If not, then you should receive some surprisingly helpful error messages explaining what you’ve done wrong.

There’s another guide at Wimpy’s World which has some additional detail and helped me get this working.