Permanently Mounting a USB Hard Drive to your Raspberry Pi
The SD card that boots and runs your Raspberry Pi is likely very small, offering only a few GBs worth of space. This can be filled very quickly. The first upgrade to a Raspberry Pi is usually adding a USB hard drive. This guide will go through permanently mounting a USB NTFS formatted hard drive to your Raspberry Pi.
This guide assumes you already have a Raspberry Pi up and running with Rasbian as the operating system. If you're not up to this point yet, check out this guide on setting up a Raspberry Pi from the very beginning!
Your new best friend
The ingredients here are simple. You will need:
- A Raspberry Pi. Ours has Rasbian installed as the operating system.
- A way to communicate with the Raspberry Pi, such as directly connecting keyboard, mouse, and monitor or going "headless" and connecting with another computer.
- A USB Hard Drive
For the hard drive, we chose a Seagate 4TB External, available for $139.99 at Amazon.com.
This setup has some pros and cons.
- 4TB is rather large.
- The hard drive uses an external power source. Therefore, it doesn't drain power from the Raspberry Pi's USB port.
- It's pricey at $139.99.
- Because it uses an external power source, it requires another cable and another outlet.
Another option is the WD 1TB Portable Drive, also available from Amazon.com. This comes at a nicer price point, only $69.99. The drive is large enough (1TB) to meet all immediate needs and doesn't require an external power source.
If you have experience with Linux and BASH, here is the code you will need to use. If this looks confusing or intimidating, keep reading! We will break down each and every line of code and command used. By the end of the tutorial, you'll be able to come back up to this point, read the code, and congratulate yourself for knowing exactly what each command is doing.
Let's get started!
#For everyone well versed in Linux and BASH, here's everything up front #Find the drive (in our case /dev/sda1) sudo fdisk -l #install NTFS-3g sudo apt-get install ntfs-3g #Make the mount directory and manage it's owner sudo mkdir /media/pidrive sudo chown pi:pi /media/pidrive #Mount the drive sudo mount -t ntfs-3g -o uid-pi,gid-pi /dev/sda1 /media/pidrive #Now you're done but it's not permanent #Making it permanent #Edit file system table sudo nano /etc/fstab #Add this line of text after the SD card partitions /dev/sda1 /media/pidrive ntfs-3g uid=pi,gid=pi 0 0 #hit ctrl-o to save and ctrl-x to exit nano #Now the mounting will be restored on reboot #Reboot to test it sudo shutdown -r now
Step 1: Connecting and finding the drive
First thing's first: boot up your Raspberry Pi and login. Plug in the USB hard drive into a USB port on the Raspberry Pi and plug in the power source for the drive if it requires one.
Now let's see if the Raspberry Pi "knows" about the new hard drive you've connected to it. Open the terminal window. If you're in a Desktop style screen, click on the menu at the bottom left of the screen, navigate to "Accessories" and then click on "LXTerminal".
sudo fdisk -l
#You should see something like this Disk /dev/mmcblk0: 4110 MB, 4110417920 bytes 4 heads, 16 sectors/track, 125440 cylinders, total 8028160 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000b5098 Device Boot Start End Blocks Id System /dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA) /dev/mmcblk0p2 122880 8028159 3952640 83 Linux Note: sector size is 4096 (not 512) Disk /dev/sda: 4000.8 GB, 4000787025920 bytes 255 heads, 63 sectors/track, 60800 cylinders, total 976754645 sectors Units = sectors of 1 * 4096 = 4096 bytes Sector size (logical/physical): 4096 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disk identifier: 0x90a334b0 Device Boot Start End Blocks Id System /dev/sda1 * 2048 976754644 3907010388 7 HPFS/NTFS/exFAT
"sudo" - this essentially means "Run as Administrator." We'll be using this a lot.
"fdisk" - This is short for "Fixed Disk" and is a command line utility built into the operating system that allows you to manage partitions on a hard drive. Right now we're using it to look at what partitions on what hard drives your Raspberry Pi knows about by using...
"-l" - This is a "list" command that is passed to the fdisk utility. It tells fdisk to list what it knows.
Looking through the list outputted we see, from top to bottom.
"Disk /dev/mmcblk0 4110MB"
This is the SD card which the Raspberry Pi boots off of. It has two partitions:
"/dev/mmcblk0p1" and "/dev/mmcblk0p2"
Neither of these are what we're interested in. The drive that we're interested in is the 4TB Seagate External.
That drive is listed as:
"Disk /dev/sda 4000.0 GB"
and has one partition:
Remember that. "/dev/sda1" That is the name that the drive is going by and the name we'll use to mount it.
Step 2: Loading the correct drivers
We found the drive and the Raspberry Pi knows about it, so we're done right? Not quite. The Raspberry Pi knows about the drive but it isn't accessible yet. You wouldn't be able to find the drive anywhere in the filesystem. To solve this issue we need to mount the drive.
First, we need to download the software that will handle talking to the drive. Drives come in different file system formats. The drive you have, if it's newly bought, has ever been used or will ever be used on a Windows PC, is most likely formatted as NTFS. NTFS is great for Windows but isn't native in Linux, which means Linux doesn't like to communicate with NTFS drives. This is easy enough to fix. Make sure your Raspberry Pi is connected to the internet, open the terminal window and type:
sudo apt-get install ntfs-3g
"apt-get" - "apt" is short for "Advanced Packaging Tool" and "apt-get" is another command line utility. It allows you to grab software packages online and install them onto the local computer.
"install" - This is the keyword we pass to "apt-get" so that it knows that we want it to install something.
"ntfs-3g" - This is the something we want installed. "ntfs-3g" is a read-write driver for NTFS, which means it enables communication between Linux and NTFS formatted drives.
Step 3: Creating a mount point
Now we need a place in the filesystem to mount the drive to, so let's create that.
The next two lines of code will create the file location and then change the user that "owns" that location.
#Type this line then press enter sudo mkdir /media/pidrive #Then type this line and press enter sudo chown pi:pi /media/pidrive
For the first line of code:
"mkdir" - This is short for "Make Directory." It creates a folder in the file system.
"/media/pidrive" - This is the folder you want to create. The Linux convention is to mount USB connected devices in the "Media" folder. The folder I want to use to access the USB drive I'm calling "pidrive." You can call yours whatever you want.
For the second line of code:
"chown" - This is short for "Change Owner."
"pi:pi" - This is choosing the owner:group for the folder. If you're using the Raspberry Pi, the default user is "pi." The default user group is also "pi." Thus, user:group becomes pi:pi.
"/media/pidrive" - This is the folder we want to change ownership of.
We change ownership of the folder so that you don't need "sudo" or root permission to use the hard drive.
Step 4: Getting the drive mounted temporarily
Okay, let's get mounting!
Remember the hard drive location? In our example it was "/dev/sda1". In the terminal type the following code:
sudo mount -t ntfs-3g -o uid=pi,gid=pi /dev/sda1 /media/pidrive
"mount" - mount is an operating system action that will mount the drive to the filesystem.
"-t ntsf-3g" - "-t" is the filesystem type flag and "ntfs-3g" is telling the mount command what filesystem to use.
"-o uid=po,gid=pi" - This is setting the owner of the drive. "uid" is user ID and "gid" is group ID. This is exactly like the ownership settings we put on the mount point folder.
"/dev/sda1" - This is the reference to the hard drive itself and...
"/media/pidrive" - Is the reference to the mount point folder.
Congratulations! Now your hard drive is mounted to the filesystem, accessible, and ready to use! However, this approach has one downside. Every time you reboot the Raspberry Pi, you'll have to execute this line of code again. Now we're lazy and don't want to do that, so let's setup this mount point permanently!
Step 5: Getting the drive mounted permanently!
To get this drive to mount itself every time the Raspberry Pi is rebooted, we have to edit a configuration file on the Raspberry Pi called fstab short for "File System Table." Now this sounds a lot more complex than it really is. Fstab is just a text file that tells the Raspberry Pi what to do with the hard drives connected to it. To edit it, we're going to use a text editor that comes with the Rasbian Operating System and works in the terminal called Nano.
In the terminal type in this code:
sudo nano /etc/fstab
#You will see something like this proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 2 /dev/mmcblk0p2 / ext4 defaults,noatime 0 1 /dev/sda1 /media/pidrive ntfs-3g uid=pi,gid=pi 0 1 #A swapfile is not a swap partition, so no using swapon|off from here on, use dphys-swapfile swap[on|off] for that
The terminal window will look different. We are now in the text editor Nano and are ready to edit this file.
Press the down arrow to navigate to one line below the line that says "/dev/mmcblk0ps" and press enter to insert a new line.
Arrow up one to place your cursor on the new line and type in:
/dev/sda1 /media/pidrive ntfs-3g uid=pi,gid=pi 0 1
#So that your window now looks like this proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 2 /dev/mmcblk0p2 / ext4 defaults,noatime 0 1 /dev/sda1 /media/pidrive ntfs-3g uid=pi,gid=pi 0 1 # a swapfile is not a swap partition, so no using swapon|off from here on, use dphys-swapfile swap[on|off] for that
This code should look familiar since it's very similar to the mount command we used to manually mount the drive.
Now press Ctrl+O (not Ctrl+Shift+O) to save the file. Press Enter to acknowledge the file name and then press Ctrl+X to exit Nano.
Now the terminal should return to the way it originally looked.
Congratulations! You're done! Your USB hard drive is now permanently mounted to the Raspberry Pi and will be remounted on every reboot. Now you're ready to use the USB drive to store your files, music, documents, videos, solutions to all the world's problems, and whatever else you want on your Raspberry Pi!
PiDrive ready to roll
Give me feedback!
Was this guide useful?See results without voting
Drop me a line
Please let me know if this guide has been helpful to you. Take the poll on the right and leave a comment below! Thank you for listening.
© 2014 John Himics