How to: Use External Hard Drive with Raspberry Pi

SD cards on the Raspberry Pi are cheap and come in relatively larger sizes, but have a few limitation, such as wearing out quickly and become exponentially more expensive at larger capacities. Wouldn’t it be great if you could take a standard hard drive or SSD and use that for storage on the Pi instead? Well, good news, you can! Though it will take a bit of work to set it up initially, but once all the hard work is done, you can use all of that sweet capacity for whatever project you’d like!!

Step 1: Get a Disk

First we will want to obtain a disk that we want to use for the Pi. This can be anything from a Hard Drive to an SSD or even a USB memory stick. Ensure this is a disk you do not care about erasing, as all contents will be deleted permanently.

For this project I chose to use a hard drive I had laying around: a 600 GB . I got myself a SATA to USB3 adapter so that I can plug it into my Pi. If you go this route, make sure to get a USB3 type, especially if working with a Raspberry Pi 4. If using a hard drive instead of SSD, you may be required to use an external power supply as USB might not be enough to turn the disk on.

Step 2: Format Disk and Create Partition

We first want to determine what the disk is called on the system when it is plugged in:

sudo lsblk

Should look as follows:

In my case, my disk is “sda” and is about 600 GB in size. As can be seen, the disk has a partition already name “sda1”. The mmcblk0 is the SD card.

Next, we want to use the fdisk tool to create a new partition. Type to following command, ensuring the “/dev/sda/” matches the disk name on your Pi.

sudo fdisk /dev/sda

Once in the tool, type ‘p’ into the terminal to check the details of the disk. In my case, the “Disklabel type” was of type “DOS”. We will want to change that to “GPT”.

Type in “g” (lowercase) to switch it to GPT. Then type ‘w’ to save this change. Go back into the fdisk tool using the “sudo fdisk /dev/sda” command again.

Type ‘p’ again to ensure that the “Disklabel type” has changed to “gpt”.

Type ‘n’ to create a new partition. Hit the enter key three times to choose the default values for all options. This should create a new partition on the disk of the full size of the disk.

Finally, we want to save all our selections and actually write all of this to the disk. Type ‘w’ to write to disk.

Step 3: Mount the Drive

Now that we have formatted the disk to work properly with Linux, we want to mount the drive so we can start to actually use it with Home Assistant. We can mount it in two ways, the manual way or the automatic way. The manual way is good for initial testing, but is required on each power cycle. With the automatic way, the system will automatically mount the disk at boot up, which is what we ultimately want to do.

First we are going to need to create a filesystem on the disk. This is going to create an EXT4 filesystem on the disk:

mkfs.ext4 /dev/sda1

Again, make sure to use the partition name we just created before of your system. Depending on the size of the disk, this might take a while. It might ask you to confirm that you want to do the command. Next we will want to create a folder in the /mnt directory to attach the disk so it can be mounted.

sudo mkdir /mnt/hassio

I decided to name my folder “hassio”, but this can be whatever the user wants. Next we want to make sure we have permission to access the new folder:

sudo chown -R pi:pi /mnt/hassio

Finally, we mount the disk to the new folder we just created:

 sudo mount /dev/sda1 /mnt/hassio

Of course, you will want to match your disk name and folder name. And there we go, we finally have a disk to use! Of course we mounted it manually, and we dont want to have to do that everytime, so lets change that.

Step 4: Setup Automatic Disk Mounting

We first have to find out the UUID of the disk, so that if the name changes when a new disk is plugged in, it wont mount that one instead. Luckily Linux makes that easy to find:

ls -l /dev/disk/by-uuid/

You should see the partition after a set of random HEX characters, in mine case it is “63dc61ad-81b3-4990-9db3-f63b1c885f72”. Write this down somewhere, we will need it soon. We want to edit the “fstab” file to tell the system what to mount and how to mount it. Use “nano” to edit this file.

sudo nano /etc/fstab

At the bottom of the file, add the following line:

UUID=63dc61ad-81b3-4990-9db3-f63b1c885f72 /mnt/hassio   exfat   auto,nofail,noatime,users,rw,uid=pi,gid=pi 0 0

Replace the UUID with the one you found earlier, as well as the folder name to match the one you created. Hit ctrl-s, ctrl-x to save and exit nano. Now whenever we reset the Pi, the disk will be mounted to the folder we created. Neat!

Step 5: Now what?

Now that the disk has been mounted, we can use the folder that the disk is mounted to just like any other Linux folder. All file access inside that folder will actually be on the disk itself, so try playing around with it!

Leave a Reply