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

RaspberryPi 130 - Adding Storage (USB)

posted Nov 6, 2015, 1:55 PM by Joshua S   [ updated Nov 26, 2016, 4:48 AM ]
This tutorial will first mount a USB flash drive and then demonstrate how to auto-mount the drive if you wish to use it as permanent storage for the Pi.

With any of the Dabbles on this site, if you have questions, suggestions, or thoughts, please feel free to send me an eMail (I'm still working to figure out how to enable comments on Google Sites -- suggestions would be appreciated)!

Supply List:
  • RaspberryPi  The actual RaspberryPi hardware this will all be built around.  In this tutorial, a Raspberry Pi 2 is used and has a memory card with the Raspbian operating system pre-installed.
  • PuTTY SSH Client – PuTTY is a free and open-source terminal emulator, serial console and network file transfer application. It supports several network protocols, including SCP, SSH, Telnet, rlogin, and raw socket connection.  Other SSH tools can be used, but this tutorial will leverage PuTTY.
  • USB Flash Drive data storage device that includes flash memory with an integrated Universal Serial Bus (USB) interface. USB flash drives are typically removable and rewritable, and physically much smaller than an optical disc. 
Prerequisites:

Project:
  • I know I said this guide was going to be comprehensive and not skip any steps, so what better way to start this off than by skipping steps.  I am not writing out instructions for the following (and illustrating from XKCD):
    • Buying a MicroSD Card
    • Buying a RaspberryPi 
    • Finding the IP Address of your Pi
      • This can be done in many ways, including on your router or using an IP scanner such as (AngryIP Scanner or NMAP) -- if there are requests from the "Contact Me" form; I'll look to create a tutorial for this.
    • Obtaining and installing PuTTY
https://xkcd.com/1343/
  • Using PuTTY (or the SSH client of your choice) enter the IP Address or DNS Name of the RaspberryPi.
  • If this is the first time you connect, you will get a warning that the RaspberryPi's host key is unknown.  Click "Accept" or "Connect Once" to proceed with the connection.
  • Once connected, log onto the Pi using the credentials you created.  If you have not defined your own credentials, you should, but these are the default credentials:
  • UserID:  pi 
  • Password:  raspberry
  • 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 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.
Comments