Home‎ > ‎Dabbles‎ > ‎RaspberryPi‎ > ‎

Adding Storage - Mounting a USB Drive

posted Dec 21, 2014, 4:55 AM by Joshua S   [ updated Jan 1, 2015, 8:38 AM ]
This post demonstrates how to mount a USB Mass Storage Device from your RaspberryPi.  This is really useful so you can expand your storage beyond the SD Card, offload critical data from your system drive without needing to send it across the network, or even just access a portable hard drive or USB stick from time to time.

This is part of a series and assumes the elements performed in several earlier ones are in place and integrated into the template (see the template projects) in order to work.  

Most of these projects can be performed in any order, but if you follow the order outlined here it will all definitely work.  I've found a number of guides to help me, but many have partial documentation, skip key steps, etc.  The goal is to build out a guide with everything you need to complete each step, but let me know if I miss something or it isn't clear.

For each template and initial setup, I used an 8GB memory card.  With the B+, the image you back up will be the full size of the card whether you expand the file system or not.  For the actual projects, I use a mix of card sizes -- generally 32GB, but I like my templates and initial setup configs to be 8GB to reduce the storage size of my backups.

For additional information, this is a really good post that walks through mounting an NTFS drive -- the process is almost identical except for the filesystem.  Additionally, the RaspberryPi forums have a good overview post, with a link to a more detailed overview of USB Mass Storage Device operations.

Supply List:
  • MicroSD Card  A digital memory card, initially designed for media (think a camera) but which will serve as the hard drive for the RaspberryPi.  All tutorials will focus on the 8GB size, but you can easily use this process for a larger format also.  This should be pre-loaded with the template image created in the previous project (template step 01).
  • PuTTY  A free SSH client which is excellent for working at the command line.  I know, I know, no one loves the Command Line any more, but the more you use the RaspberryPi the more you will quickly learn that CommandLine > GUI.
  • RaspberryPi B+  The actual RaspberryPi hardware this will all be built around.
  • USB Mass Storage Device – The USB storage device we are going to format and use for additional storage.
  • Win32 Disk Imager  A Free Open Source Software (FOSS) utility to write of image (.IMG) files to various flash card media (SD, MicroSD, etc.).  Download the software from the website.

  • Write the image you plan to enhance to the SD Card and load it into the RaspberryPi.  In this example, we'll use the template file, created in a previous tutorial.  
  • Using PuTTY (or whatever SSH client you prefer) connect to the IP address of the RaspberryPi.  You should know this from the previous step ( in this example), but if you do not, follow the steps at the beginning of the first lesson which show how to use AngryIP scanner to locate the IP address.
  • Once connected, log onto the Pi using:
    • UserID:  pi 
    • Password:  raspberry

  • Raspbian uses Advanced Package Tool (APT) to manage and install software.  First, we need to update the tool using:
    • sudo apt-get -y update
  • Now let's update the software currently loaded.  There are several ways to do this, but if we issue the dist-upgrade command it will intelligently add software, update packages, and remove unneeded packages.
    • sudo apt-get -y dist-upgrade
  • Finally, let's upgrade the Pi Kernel:
    • sudo rpi-update
  • Let's insert the USB stick and reboot now that the upgrades are complete:
    • sudo reboot

  • OK, good!  Now that everything is updated, let's find our USB stick.  Note, the disks starting with the name "mmc" refer to the SD Card.  Any USB devices should be noted as starting with "sd" similar to the one found here of "sda".  Use the following commands:
    • ls -al /dev/disk/by-uuid/
  • Note the hardware ID of the drive -- we'll need it later when we permanently mount it.  In this example (see the previous picture), the ID is F498AB4098AAFFEA.
  • In our example, we see our drive has uuid sda1.  Let's start by formatting the drive.  In this case, we'll use EXT4 -- it is a native linux format which allows large file size and delivers good performance.  Here are a list of potential file system formats as you consider the right one for your use.  Note, the -L argument allows us to put a label on the drive -- in this case I gave the label "storage".  Use the following commands:
    • sudo mkfs.ext4 /dev/sda1 -L storage
  • Good job!  Now that the drive is formatted, we need to create the "mount point".  Basically, this will be the directory where we attach the file system of the USB stick.  This is an important difference between Windows and Linux -- as opposed to having drive letters, like in Windows, Linux mounts drives within directories based upon configuration.  Use the following command to create the mount point and provision it appropriate permissions:
    • sudo mkdir /mnt/storage
    • sudo chmod 770 /mnt/storage
  • Let's start by mounting the drive for this session, but then we'll add the config needed to automatically mount the drive when the RaspberryPi reboots.  To mount for this session use the following command:
    • sudo mount -t ext4 /dev/sda1 /mnt/storage
  • Great!  The USB stick is now mounted for this session.  To have it automatically mount, we'll need to edit the FSTab, or file system table, file.  Let's backup up the file, just in case we cause issues, then edit it using these commands:
    • sudo cp /etc/fstab /etc/fstab.backup
    • sudo nano /etc/fstab
  • Within the file, add the following line (see the before and after images below).  This will allow the drive to auto-mount when we reboot.
    • UUID=<Drive UUID from above> /mnt/storage ext4 defaults 0 0

  • Let's reboot now and make sure our changes worked:
    • sudo reboot
  • When the system finishes rebooting, log back on, and use this command to navigate to the drive.  If everything worked as expected, you should be able to navigate there with no errors and issue an ls command against the directory.  Note, since we mounted this using default options, all permissions will be restricted to root -- for this reason, we are going to first switch to root.  Depending on how you plan to use this drive, you may want to alter the permissions appropriately.
    • sudo su
    • cd /mnt/storage
    • ls -al
  • Congratulations!  Your drive is mounted and will automatically re-mount each time the Pi boots.