Force your Raspberry Pi to mount an external USB drive every time it starts up


I spent a while figuring this out about a month ago with a Linux expert colleague (hi, Caz!) but didn’t write any of it down because it was going to be “fixed in the next release of Raspbmc”.

I’m still getting the issue with the latest release so thought I’d best document the steps for when I inevitably have to do this again in another month.

The Issue

I’m using the excellent Raspbmc distribution to turn the Pi into a mini media centre PC using XBMC. I’m using an external USB drive to store media.

Sometimes, when I boot up the Pi, it doesn’t mount the external drive. So, media is unavailable and, if you do a clean through XBMC (like I did ten minutes ago), it’ll wipe all media info from the database.

The Solution

So, we need to get the drive to automatically mount when the Pi starts up.

fstab

The fstab (/etc/fstab) (or file systems table) file is a system configuration file that lists all available disks and disk partitions and indicates how they are to be initialised or otherwise integrated into the overall system’s file system.

We can edit this to include our USB drive and make sure it is mounted where we want it to be on every boot.

The Location

To work out where we want the drive to mount, I found it easiest to just get the Pi to mount it itself to see where it put it by default. Then we can just use that location as our “forced” one.

So, start up your Pi with the USB drive disconnected and wait till it boots. Once done, plug in the drive and wait for it to mount.

Once that’s done, we need to jump into the command line (this is Linux – were you expecting anything else?). So, either open up a terminal on the machine or connect via Putty.

Use the df command to work out where your device is mounted:

pi@raspbmc:~$ df
Filesystem      1K-blocks       Used Available Use% Mounted on
/dev/mmcblk0p3   15203328    1468324  12962704  11% /
/dev/mmcblk0p1      71569       4365     67204   7% /boot
/dev/sda1      1953512000 1897227140  56284860  98% /media/usb0

From that result, I can see that my USB drive is device /dev/sda1 and is mounted at /media/usb0. Take note of these two values; this is where we’ll mount it within fstab.

Edit fstab

You can edit the fstab file using the built-in text editor. This is a system file, so you’ll need to use sudo to edit it as root. So use sudo pico /etc/fstab.

This will bring up a text editor with something like this:

GNU nano 2.2.6                   File: /etc/fstab
 
proc              /proc           proc       defaults           0       0
devpts            /dev/pts        devpts     defaults           0       0
/dev/mmcblk0p1    /boot           vfat       defaults           0       0
/dev/mmcblk0p2    none            swap       sw                 0       0
/dev/mmcblk0p3    /               ext4       defaults,noatime   0       0

We can ignore the existing content. All we want to do is add another entry at the bottom for our USB drive.

The columns are as follows:

  1. The device name or other means of locating the partition or data source.
  2. The mount point, where the data is to be attached to the filesystem.
  3. The filesystem type, or the algorithm used to interpret the filesystem.
  4. Options, including if the filesystem should be mounted at boot.
  5. dump-freq adjusts the archiving schedule for the partition (used by dump).
  6. pass-num Controls the order in which fsck checks the device/partition for errors at boot time. The root device should be 1. Other partitions should be either 2 (to check after root) or 0 (to disable checking for that partition altogether).

A value of zero in either of the last 2 columns disables the corresponding feature.

I know that my USB drive is NTFS, so this is the entry I’ll be adding:

/dev/sda1         /media/usb0     ntfs-3g    defaults           0       0

If my drive had been FAT, I’d use vfat instead of ntfs-3g.

Save the settings

To save your settings, use Ctrl + X to exit Pico. You’ll be prompted on whether you want to save so select Y.

You’ll now be asked to enter a save filename. This will already be filled in with the current filename so just hit return to accept that.

So, there you have it. You can now restart your Pi as much as you want and your drive should always be mounted where you expect it.

Thanks

A large chunk of this came from picking the brain of Caz, so many thanks for that.