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.

Kevin Wilson

.NET developer, JavaScript enthusiast, Android user, Pebble wearer, sometime musician and occasional cook.

20 thoughts on “Force your Raspberry Pi to mount an external USB drive every time it starts up

  1. Hi.
    Thanks for this informative post.
    I struggled with this issue for a long time but gave up.

    What I really wanted was to modify the default rasbpmc behavior when it automounts.

    Do you know how to do this?

    1. As far as I know (my knowledge of Linux is limited), you can modify the mounting behaviour for the disk in the options column of fstab.

      There are quite a few sample entries on the fstab Wikipedia page that might help you.

  2. You are a legend. I appreciate your time you put into this It helped me. I thank you for your effort. Laterz

  3. one more thing I wasn’t sure what my hard drive was formatted in and it took a while searching for the right command. The command I found to work was
    sudo blkid
    This might help others and you might want to add this in the grate guide. And I don’t know if this makes any difference but I read somewhere else that you have to give the new directory access permissions. Blog
    http://eraldmariano.com/how-to-install-xbmc-transmission-on-raspberry-pi/
    (this is to get transmission working to) run the following: command
    sudo chmod g+rw /media/usb0
    sudo chgrp -R debian-transmission /media/usb0

    I’m not sure if this is needed Kevin but it works for me.
    Peace and thankyou again

  4. I hooked up a 2TB WD My Passport to my powered hub and XBMC detected and mounted it no problem. I was surprised it was so easy but it was! Haven’t tried any other types of external drives but My Passport is a zero problem solution.

    1. I think the newer versions of Raspbmc handle mounting of external drives on reboot now. The prerelease version I was using when I wrote this didn’t.

      I’ve not gone back and tested it since (I don’t have an external drive any more). Glad it’s working out of the box now.

  5. When you start the GUI (with startx command) the usb automounts. But on reboot if you stay in command line, it’s not automounted. You have to modify the fstab as stated in this article.
    One problem I found is that sometimes when it is mounted, you have no permissions to write to the usb disk, only to read it. I found a solution to this, openning a command prompt and typing:

    sudo chown pi:pi /media/usbhdd
    sudo chmod 777 /media/usbhdd

    That fixes the problem forever. I don’t know why it happens sometimes (when I re-format my external hd using gparted).

  6. Hi, I’m using RASPBMC and it auto mounts my WD 1TB HDD. I’m able to view it on my WLAN network even and read from it, but i’m unable to write to it. Any help on this??

    Its formatted in ntfs format
    And I’m not really tech person so would need some explanation 🙂

    Thanks!

    1. Sorry for the late reply – can you write to the disk from the local machine? Is it just over the network that you’re having issues?

  7. Thanks for the clear and simple instructions. I’m using Raspbmc too, for the first time, and these instructions and links were just what I needed to get started. USB stick now working!

  8. Thanks for info, was starting to pull out my hair, could not save on WD Elements 500Gb drive, but you helped a newbie to the Rasberry Pi world, many thanks

  9. Hi Kevin. Glad you took the time to add this as will save me time now!

    But can I check the -3G after ntfs is this operator to load as 3gb drive? And I am likely to use 1tb vfat so would it be vfat-2t ?

  10. My usb flash drive will automount when I plug it into my Raspberri PI, but the permissions are wrong and can’t be changed using chmod or chown.
    drwx——
    Only root has access to it.
    I would love to find whatever setting is being used to apply defaults to this automount behavior and change them so that any network user may read and write to any usb flash drive that is plugged in.
    I’ve searched the internet for hours looking for a solution and this is one of the few threads I’ve seen that describes my problem. Automout works! But file-permissions need to be read-write.

  11. Use sudo chmod … to change the permissions
    See “anonymous March 1, 2013 at 17:13” post also may need to change permissions when mounting drive in fstab

Leave a Reply

Your email address will not be published. Required fields are marked *