How to Mount USB Disks on Raspberry Pi
I’ve got a Raspberry Pi, and I’ve got an old external hard drive. So what can I do with them? I decided I was going to create a networked shared drive from them, so in this first tutorial, I will show you how to mount the drive on a Linux operating system. Doing this is easy, but can get a little tricky when you attach and remove drives on the fly.
Linux and Your Drives
The first thing to know about Linux and your drives, or indeed any other piece of hardware, is that Linux will create a file to manage it. Typically files relating to hardware devices are created under the /dev directory. So if you connect your drive, Linux will create a new entry here to represent the drive. This file is special, in that is represents the raw data on the drive itself, so it’s not readily usable. This is the drives block file, as Linux just treats it as containing blocks of data.
Your drive will encode files in a particular way, according to the file-system the drive was formatted with. For example, the way NTFS (typical of drives formatted under Microsoft Windows) arranges data into files, will differ from the way EXT3 (typical of Linux formatted drives), or even FAT drives. You’ll need to tell Linux how to interpret the data on the disk, using the corresponding file system. Doing this allows you to access the data as the files you intend to read and write to.
Linux names these files in an ordered way. The first drive detected will be called /dev/sda (or /dev/hda) with subsequent disks given subsequent letters (/dev/sdb, /dev/sdc, etc.). As drives are often divided into several partitions, the partition number is added to the end, so you may end up with multiple entries. As an example, if I list these devices, you might see something similar to:
gonzales ccollins # ls -al /dev/sd* brw-rw---- 1 root disk 8, 0 Feb 4 18:42 /dev/sda brw-rw---- 1 root disk 8, 1 Feb 4 18:42 /dev/sda1 brw-rw---- 1 root disk 8, 2 Feb 4 18:42 /dev/sda2 brw-rw---- 1 root disk 8, 5 Feb 4 18:42 /dev/sda5 brw-rw---- 1 root disk 8, 16 Feb 4 18:42 /dev/sdb brw-rw---- 1 root disk 8, 17 Feb 4 18:42 /dev/sdb1 brw-rw---- 1 root disk 8, 18 Feb 4 18:42 /dev/sdb2 brw-rw---- 1 root disk 8, 21 Feb 4 18:42 /dev/sdb5 brw-rw---- 1 root disk 8, 32 Feb 4 18:42 /dev/sdc brw-rw---- 1 root disk 8, 48 Feb 4 18:42 /dev/sdd brw-rw---- 1 root disk 8, 64 Feb 4 18:46 /dev/sde brw-rw---- 1 root disk 8, 65 Feb 4 18:46 /dev/sde1 brw-rw---- 1 root disk 8, 69 Feb 4 20:05 /dev/sde5
As you can see, I have several drives connected, some with several partitions.
Step 1 – Connect your drive
The first step in mounting your drive is to connect your drive, and find the block device Linux creates for you. This can easily be done using the ‘blkid’ utility as root, or use sudo. Using this I get the following output:
gonzales ccollins # blkid /dev/sda1: UUID="756dfe8e-1ab6-4933-be98-776e38f97857" TYPE="ext4" /dev/sda5: UUID="b1e575f1-ad95-47a9-b0d8-215a15736ed4" TYPE="swap" /dev/sdb2: UUID="86B89827B898182F" TYPE="ntfs" /dev/sdb5: UUID="CE1E6F081E6EE8C3" TYPE="ntfs" /dev/sde5: LABEL="LENOVO_USB_HDD" UUID="BA504FD6504F9851" TYPE="ntfs"
In this case I would like to mount the LENOVO_USB_HDD, so as we go, we will discuss the various options we have.
Step 2 – Create a Mount Point
Linux represents all files under a single directory hierarchy, with the root (/) folder at the top. We will need to let Linux know where we want our drives files to appear on this directory structure. We will create an empty directory as our mount point. This directory can be anywhere in the hierarchy, but typically we use a subdirectory of /media. In my case I created a directory as:
gonzales ccollins # mkdir /media/usbhdd
Step 3 – Edit the fstab file
Now that we know where our block device file lives, and where we want to mount it, we can instruct Linux to mount the drive at that location. This can be done manually each time you want to ‘mount’ and ‘unmount’ the drive as needed, but if you’re doing this frequently, it may be easier to have Linux automate this process for you. This can be done by editing the file system configuration file, /etc/fstab.
As root, open the /etc/fstab file in your favourite editor and add the following line:
/dev/sde5 /media/usbhdd ntfs defaults 0 2
The first field here is our block device, the second our mount point, the third shows the file system type to use, the fourth field allows for different ‘mount’ options, the fifth field configures the ‘dump’ command (0 disables this), and finally the sixth field determines in which order the filesystem is checked at boot time (2 here says do it after the boot drive). You can find more information on these fields by checking out the manual (man) pages for fstab and mount. Save your edits to the /etc/fstab file and exit your editor.
Step 4 – Mount it
Now we can mount the new filesystem by using ‘mount -a’ (as root, or with sudo). This will mount all file systems listed in the /etc/fstab configuration file. This step gets automatically done for you at boot time, or when you add the device in future. So now, you should see your drives files under the mount point directory.
We’re Done? Not Quite!
So the steps outlined so far will work, but can be a little brittle to system changes. As you add and remove USB drives from your system, Linux won’t guarantee to always create the same block file to represent the same drive. In our example here, we’re depending on the drive always being available at /dev/sde5.
What would happen if this block file is allocated to a different drive? What if my drive is given a different block file name, such as /dev/sdf5? In these cases, the configuration we have specified above may not be suitable, and the files you expect may not appear under the mount point directory.
To fix this, we refer back to Step 1 and the ‘blkid’ utility. This lists both Universally Unique IDentifiers (UUID) and LABELS which are associated with your drive. These are specific to your drive, and are guaranteed never to change as you add and remove the drive. You can use these in the /etc/fstab file to create a more robust mounting mechanism.
We can edit the line we added earlier in the /etc/fstab file to either of the following for the same result:
UUID=756dfe8e-1ab6-4933-be98-776e38f97857 /media/usbhdd ntfs defaults 0 2 LABEL=LENOVO_USB_HDD /media/usbhdd ntfs defaults 0 2
Your UUID and/or label will differ, so use those. In the first case we’re telling Linux to mount any drive with a corresponding UUID to the mount point. This will happen regardless of the block file allocated to the drive, so it allows it to change over time.
In the case of a FAT file system drive, such as a thumb drive, you may not have a UUID allocated, so the LABEL directive might suit you better. The same configuration applies, with Linux mounting any drive with the same label to the mount point, again regardless of the block device allocated.
What I have described here only touches the surface to the configuration options available to you when you add drives to Linux. I would recommend reading the manual pages for fstab and mount as they work together to enable easier configuration of how your drives files are mounted, which users should be able to mount drives, and what the default permissions for mounted files are going to be. These are options which you can specify in the fstab file and which are passed to mount when the drive is being added.
Blog entries are licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Blog comments are copyright to their respective authors.
- How To Measure Air Pressure with Arduino
- Unified Process vs Agile Processes
- How to change date formats on Ubuntu
- Testing SD Cards with Linux
- Introduction to the Zachman Framework
- A Brief History of XML
- How to Share a Folder on Raspberry Pi
- History of SQL
- Howto: Pause a Shell Script
- How to Mount USB Disks on Raspberry Pi
|ccollins on When Life Gives you Oranges, M…|
|madeinromeblog on When Life Gives you Oranges, M…|
|ccollins on Manage Passwords and Keep Your…|
|Dejan Savic on Manage Passwords and Keep Your…|
|Tea Brack | Chris Co… on Damson Liqueurs|
- March 2016
- January 2016
- July 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- August 2014
- March 2014
- January 2014
- December 2013
- February 2013
- January 2012
- March 2011
- January 2010
- October 2009
- July 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- June 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- July 2007
- June 2007
- May 2007
- November 2006
- October 2006
- September 2006