Skip to main content
Raspberry Pi

Adding External Storage to your Raspberry Pi

By November 27, 2013September 12th, 2022No Comments

External StorageWe have now set up the Samba server and we have shared out the /data directory on the Raspberry Pi. If we want to have content to share in the directory then we will need to look at adding in external storage to the RPi. An 8GB SD card does not leave a lot of space left over for the added extras of data storage. So, in this tutorial we will add and external SSD USB drive to our Pi and partition, format and mount this to our shared directory.

External Drives

As with all things Pi related we really do need to consider the following:

  • Power consumption
  • Noise
  • Cost

Power Consumption:


Looking at power consumption first; one of the main advantages of the Pi is that it uses such a small amount of power. You should be able to get away with an external power supply for the Pi using as little as 700mA. Commonly 1A power supplies are used but you can but power supplies up to about 2.5 A. If you are using 1 A or below then it is unlikely that you will be able to run an external drive directly from the Pi. You will plug the USB drive in and the RPI will crash as too much power will be diverted from the SoC, System on a Chip processor unit. If you have a 2 or 2.5A power supply then you will probably be able to get away with plugging in the USB drive direct to your Pi; however considering an external USB powered hub is always a good option as it also extends the USB ports that you at your disposal. In the example, you will see that I use a 4 port external USB hub that uses its own 1 Amp power supply. I am also using a solid state drive (SSD) so the power requirements for the drive are kept to a minimum.

Noise

The RPi has no fans and is silent, the last thing I want to do is add a noisy drive. You may want this as part of a media server solution or you may be using this to store recoded audio.  No matter your requirement if we can keep the drive quiet then it seems like a great idea. As the cost of SSDs reduces in price you may find these an option. They are still more expensive, by a long way, than conventional drives; however, they are more robust and can withstand being knocked about a little. I have lost countless portable drives before where they have fallen from a desk or table. I am using a small SSD in the example, 120GB but replacing the insides of a conventional USB drive that had previously failed. Especially if you are looking for smaller sized SSD, look on eBay as they are less desirable than larger drives that people want for the laptops.  If SSDs are still not in your price range then choose a reliable and large external drive that looks robust and stable to be stored for long times in the one place.

Cost

As always, this should not matter who you are or what you can afford. Re-using and re-purposing what you already have is a great way of saving money and learning a little more about your hardware and computing. I have a failed LaCie external USB drive. This has a screwed on cover, so I can access the drive to replace it. Adding in a surplus SSD drive I already had meant that my storage was available at no cost. When purchasing from new consider how easy it is going to be to repair or reuse the equipment later. As in my case, having the screws in the cover meant that my drive was not lost. I could still use the case and SATA to USB converter. So the sost saing is not just financial but to the environment too.

Partitioning the drive

Even if the drive does has a partition it is likely to be formatted with a Windows file-system. As this will be used by Linux we will add in a Linux file-system. Even if we will share this through SAMBA to Windows clients the underlying file-system does not need to be a Windows file-system. The SAMBA server abstracts the Windows clients from the underlying files; they access the file server, not the file-system.

lsusb

First we may want to check that the USB drive is seen by the system. We can use the command lsusb,  to produce a printout to the screen of USB devices recognized on the system.

Using the -v option with the command, lsusb, will printout additional information such as the MaxPower attribute of a USB device. This can help in deciding how much power is required and if an external PSU is needed for the Pi.

fdisk

Now we can use the command fdisk(/sbin/fdisk) as root to list drives and partitions.

sudo fdisk -l

Using the command, fdisk -l will list partitions and disks. The USB external drive will normally show as /dev/sda if you only have the one drive currently plugged in. The SD card on the Pi will show as /dev/mmcblk0. Be certain you can identify the correct drive for fdisk. It is easy to destroy data. Check the size of the drive from the output to confirm the drive is your USB device.

From the output of fdisk on my system, we can see that /dev/sda is 120GB which corresponds to the drive which was added.

This does have a partition; it is shown as /dev/sda1 in the output. If you are certain you do not need this partition this can be deleted from fdisk. Deleting the partition will make any data stored on the partition inaccessible! If we do need to repartition the drive then we can use fdisk again:

fdisk /dev/sda

Then from the interactive options shown, we can select the folowing

d

If you have just the single partition then at partition will be deleted. If you have more than 1 partition you will be prompted at to which partition should be removed. We can then enter

n

to create a new partition. Choose

p

for primary, and then

1 

to indicate partition number 1 should be created. We will be partitioning the complete drive in the example so we will hit enter on the starting and ending position. This defaults to the complete drive. Finally, we will need to save this back to the metadata on the disk’s master boot record. We do this by entering:

w

This will also have the effect of exiting the fdisk program.

Formatting the partition

We will need to format the partition this will create the file system that we can share. We will use mkfs(/sbin/mkfs), does what is says on the tin, makes a file system. This needs to be run as root. We can run nit with the -t option or simply run mkfs.ext4, for example, if we want to create a file-system of type ext4.

mkfs -t ext4 is equivalent to mkfs.ext4

The ext4 file-system is the latest generic file-system in Linux and works well in most cases. This is the default filesystem used in Raspbian. If we use the command with the -L option we can set the volume label at the same time as creating the filesystem and this becomes an alternative mechanism to use in mounting the disk. We can mount the filesystem by LABEL rather than the disk partition. If we have more than one disk connected then /dev/sda1 may be the identifier of the other partition where the label can be unique if we make it so.

mkfs.ext4 -L DATA /dev/sda1

This command will format and destroy data on /dev/sda1. The label “DATA” will be assigned to the file-system. Once complete we are now ready to add this to our /data directory.

Mounting the file-system

We can manually mount the file-system to the directory. However, we will need to it to mount automatically at boot time and we can edit the /etc/fstab file to ensure this happens. With the edit in place, we can test that it will mount directly from the file using the following command:

mount -a

This I prefer as we test the edit to the fstab file rather than having to wait for a reboot to see if it work. Using sudo we can use vi or nano to edit the /etc/fstab file:

sudo vi /etc/fstab

We will need to add an entry similar to this:

LABEL=DATA  /data  ext4  defaults 0 2

This mounts the filesystem with the label of DATA to the directory /data. We say that this is an ext4 filesystem and we use the default mount options. We do not include this for the dump backup system and we run a filesystem check at boot but after the root filesystem is complete. With these setting saved to the fstab file, the filesystem will still not be mounted but using the mount command with the option -a. The fstab file will be read and, hopefully, the external drive will be mounted to the data directory.

mount -a

This is very important. If we had mounted the file-system manually we would not know if the typing in the fstab file was correct until the next boot. Finally we should set the required permissions on the directory, even though this may have been set before mounting the directory to a new mount point will lose the set permissions.

chmod 1777 /data

Now we are ready to connect to the SAMBA share and start adding data to the external drive. The video can be view on YouTube