Mount a USB Disk on your RaspberryPi Server

I use all my raspberry pi's as servers and in this quick post, I will demonstrate how to mount your usb drive as a disk

Identify the Disk

I am using a SanDisk 32GB in this demonstration.

After we inserted into the RaspberryPi, we need to identify the block device:

$ sudo lsblk
sda           8:0    1 28.7G  0 disk  
└─sda1        8:1    1 28.7G  0 part

We can see that its /dev/sda.

Formatting the Disk

Since I will only be using this drive on Linux, I don't need to use a filesystem such as NTFS or exFat, because I won't be using this disk on Windows.

Therefore, I will be formatting this disk to ext4:

$ sudo mkfs.ext4 /dev/sda

mke2fs 1.44.5 (15-Dec-2018)  
Found a dos partition table in /dev/sda  
Proceed anyway? (y,N) y  
Creating filesystem with 7511040 4k blocks and 1880480 inodes  
Filesystem UUID: badcfb6a-81ec-4a3b-8182-e0a950a379e5  
Superblock backups stored on blocks:  
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

Allocating group tables: done  
Writing inode tables: done  
Creating journal (32768 blocks): done  
Writing superblocks and filesystem accounting information: done  

Prepare the Directory

Let's create the directory where we want our disk to be mounted:

$ sudo mkdir -p /disk/1

Since we might add more usb disks to our raspberry pi in the future, we dont want our mount information to specify the block device name, as they might change the way it gets booted and it could potentially mount to the wrong directory.

We would rather want the block id, instead of the name.

Now to get the block id, as we will be using it in our /etc/fstab to mount the disk on boot.

First let's check the block devices again:

$ sudo lsblk
sda           8:0    1 28.7G  0 disk  

Then get the block id:

$ sudo blkid /dev/sda
/dev/sda: UUID="badcfb6a-81ec-4a3b-8182-e0a950a379e5" TYPE="ext4"


Append the disk information to your /etc/fstab so that the disk will be mounted on boot, in my case:

$ echo 'UUID=badcfb6a-81ec-4a3b-8182-e0a950a379e5    /disk/1     ext4    defaults,auto,users,rw,nofail,noatime 0 0' >> /etc/fstab

So my complete /etc/fstab looks like this:

$ proc            /proc           proc    defaults          0       0
PARTUUID=738a4d67-01  /boot           vfat    defaults          0       2  
PARTUUID=738a4d67-02  /               ext4    defaults,noatime  0       1  
UUID=badcfb6a-81ec-4a3b-8182-e0a950a379e5    /disk/1     ext4    defaults,auto,users,rw,nofail,noatime 0 0  

We can test the mount:

$ sudo mount -a

And you should see the disk below:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on  
/dev/root        15G  1.3G   13G  10% /
devtmpfs        1.9G     0  1.9G   0% /dev  
tmpfs           2.0G     0  2.0G   0% /dev/shm  
tmpfs           2.0G  8.4M  2.0G   1% /run  
tmpfs           5.0M  4.0K  5.0M   1% /run/lock  
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup  
/dev/sda         29G   45M   27G   1% /disk/1
/dev/mmcblk0p1  253M   54M  199M  22% /boot
tmpfs           396M     0  396M   0% /run/user/1000  

Grant Permissions

The only non-root user that will use this disk, is pi, so to grant permissions:

$ sudo chown -R pi:pi /disk/1

Else, you might want to do:

$ find /disk/1/ -type d -exec chmod 755 {} \;
$ find /disk/1/ -type f -exec chmod 644 {} \;

Test Access

As the pi user, test writing to the drive:

$ echo 1 > /disk/1/file.txt

And reading from the drive:

$ cat /disk/1/file.txt