Format and mount external drive in Raspberry Pi

Salvador Guerrero
2 min readAug 27, 2023

--

There is a ton of material out there on how to do this; these are my notes when configuring my external drive for a Bitcoin lightning node.

First, connect the hard drive to a USB-3 port; you can identify these ports because they’re blue.

Format to ext4

I decided to format to ext4 because this filesystem is optimized for reading and writing; additionally, it takes less space to store files than exFAT because of its smaller cluster size.

Identify the drive name using the below command:

$ lsblk -f
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINT
sda
└─sda1 exfat 1.0 T7 Shield 96C3-D15C
mmcblk0
├─mmcblk0p1 vfat FAT32 bootfs C336-AC83 203.1M 20% /boot
└─mmcblk0p2 ext4 1.0 rootfs eaaa4faa-eab6-400c-950f-dc96ae4e0400 447G 1% /

Format using:
(change /dev/sda1 to your drive)

$ sudo mkfs -t ext4 /dev/sda1
mke2fs 1.46.2 (28-Feb-2021)
/dev/sda1 contains a exfat file system labelled 'T7 Shield'
Proceed anyway? (y,N) y
Creating filesystem with 488378008 4k blocks and 122101760 inodes
Filesystem UUID: bf98a500-09a8-4ab7-8157-b7bae1f9dc09
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848
Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

Mount the drive

Before we mount the drive, we need to create a mounting point, let’s create a folder as follows

$ sudo mkdir /mnt/sda

Now mount it using:

$ sudo mount -t auto /dev/sda1 /mnt/sda

# verify that it now shows the mount point
$ lsblk -f

Set the permissions, or group of users that will have to write permissions, in my case I want the group sandbox to be added as follows

$ sudo chown :sandbox /mnt/sda
$ sudo chmod g+w /mnt/sda

Auto-mount drive

So we don’t have to manually mount every time we restart the rpi, follow the below steps to configure auto-mount:

Copy the PARTUUID from the drive, in my case its 026ccff8–01.

$ blkid
/dev/mmcblk0p1: LABEL_FATBOOT="bootfs" LABEL="bootfs" UUID="C336-AC83" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="ba544ecf-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="eaaa4faa-eab6-400c-950f-dc96ae4e0400" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="ba544ecf-02"
/dev/sda1: UUID="bf98a500-09a8-4ab7-8157-b7bae1f9dc09" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="026ccff8-01"

Add an entry to fstab.
But before we do that, let’s back it up first:

$ sudo cp /etc/fstab /etc/fstab.backup

Now open it:

$ sudo vi /etc/fstab

Add the below line to the end of the file, change PARTUUID to your value:

PARTUUID=026ccff8-01 /mnt/sda ext4 defaults,auto,users,rw,nofail 0 0

If you’re going to be running executables from this drive add exec to the line above like follows:

PARTUUID=026ccff8-01 /mnt/sda ext4 defaults,auto,users,exec,rw,nofail 0 0

Check that it worked by rebooting

$ sudo reboot

After rebooting run lsblk, sda1 should have a mount point now:

$ lsblk -f

References

https://phoenixnap.com/kb/linux-format-disk
https://www.shellhacks.com/raspberry-pi-mount-usb-drive-automatically/

--

--

Salvador Guerrero

Computer Science Engineer, Cross-Platform App Developer, Open Source contributor. 🇲🇽🇺🇸