How to Use a USB Drive on a Raspberry PI

Sometimes you need a little extra legroom on a Raspberry. Just to store some files or maybe you’ve got an application that does a lot of reading and writing. You need to keep THAT kind of activity OFF of your SD card. SD cards will fail much more quickly if you have a lot of reading and writing. Better to use a USB drive!

USB drives are cheap. Plug one in and leave it there! Set it up so that you don’t need to think about it. Let me walk you through the steps:

Where’s the drive?

Plug your USB drive into one of the USB ports. Raspberry Pi 4 has two USB 3 ports (the blue ones). Find a decent quality USB3 thumb drive for some awesome performance! The hardware will be recognized but not mounted–that’s our job. List the attached USB devices with “lsusb”:

root@node3-3:~# lsusb
Bus 001 Device 004: ID 0781:5571 SanDisk Corp. Cruzer Fit
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

That first device at the top of the list is my Cruzer Fit. Let’s get it’s device name with “blkid”:

root@node3-3:~# blkid
/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="4AD7-B4D5" TYPE="vfat" PARTUUID="8e17a580-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="2887d26c-6ae7-449d-9701-c5a4018755b0" TYPE="ext4" PARTUUID="8e17a580-02"
/dev/sda1: UUID="68a5f487-75dc-4fc9-9328-700440f0f12f" TYPE="ext4" PARTLABEL="USB" PARTUUID="fe0e554a-174a-4d25-8d66-5c4f5017af3e"
/dev/mmcblk0: PTUUID="8e17a580" PTTYPE="dos"

The other block device present is the SD card. The Raspberry calls that mmcblk0. It’s got two partitions, p1 and p2. Don’t mess with that during this exercise. You will break things. Let’s concentrate on /dev/sda. We could probably use it as it, but I want to start from scratch. Let’s partition it and create a file system on it. Then we can mount it into our existing file system.

Prepare to be Mounted!

Partition Editor or “parted” is a great partitioning utility. Much better than the old fdisk standby. Run “parted” from root’s command line. You can follow my commands to set up your drive:

root@node3-3:~# parted
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
Warning: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) mkpart
Partition name? []? USB
File system type? [ext2]? ext4
Start? 1
End? 100%
(parted) p
Model: SanDisk Cruzer Fit (scsi)
Disk /dev/sda: 8003MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 8002MB 8001MB ext4 USB
(parted) q
Information: You may need to update /etc/fstab.

Easy-peasy! What’s I do? First, I made sure that the utility was looking at /dev/sda. Next, I wrote a new partition table to the drive in the gpt format. Then, I created the partition, named USB, of type ext4, and using the entire drive. parted let’s you use “100%” for the End block. Nice! No math!

Prepare Your Filesystem

For the next step, we need to create a filesystem in the partition. mk2fs is our utility of choice. Run mk2fs from root’s command line:

root@node3-3:~# mkfs /dev/sda1
mke2fs 1.44.5 (15-Dec-2018)
/dev/sda1 contains a ext4 file system
last mounted on / on Mon Aug 24 08:17:01 2020
Proceed anyway? (y,N) y
Creating filesystem with 1953280 4k blocks and 488640 inodes
Filesystem UUID: 090a720d-6d1a-44b4-8b53-e4d4b812a881
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done

Again, a little explanation. I ran “mkfs /dev/sda1” to start the utility and run it on the device that we just partitioned. mkfs did a quick check, FOUND a file system and then confirmed that I really wanted to destroy whatever was on there. After I answered “y”, it went to work. It takes a couple of minutes. Don’t despair. It will finish. When it does, you can try to mount it.

Mount the Drive

You’ll need a “mount point”. This is just an empty directory that serves as a place holder for the file system. Raspberry OS comes with one that we can use as a test:

root@node3-3:~# mount /dev/sda1 /mnt

Let’s see if it worked:

root@node3-3:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 29G 1.5G 27G 6% /
devtmpfs 431M 0 431M 0% /dev
tmpfs 463M 140K 463M 1% /dev/shm
tmpfs 463M 36M 428M 8% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 463M 0 463M 0% /sys/fs/cgroup
/dev/mmcblk0p1 253M 54M 199M 22% /boot
tmpfs 93M 0 93M 0% /run/user/0
/dev/sda1 7.4G 17M 7.0G 1% /mnt

Yep! That last entry. OK. It works. What are you going to be using this drive for? html files? data? mp3 files? Make a new mount point and give it a good name.

root@node3-3:~# mkdir /data

Now unmount it from /mnt and mount it to your new directory “data”. Make sure you cd out of /mnt, first!

root@node3-3:~# umount /mnt
root@node3-3:~# mount /dev/sda1 /data


Now, let’s set it up to mount at boot time. Edit your “/etc/fstab” and add one line to the end of the file:

proc                 /proc proc defaults          0 0
PARTUUID=8e17a580-01 /boot vfat defaults          0 2
PARTUUID=8e17a580-02 /     ext4 defaults,noatime  0 1
/dev/sda1            /data ext4 defaults          0 0

Save and reboot. Is your new directory still there? Good job!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.