Raspberry Pi – formatting and mounting USB drive

Let us say you bought a fresh new flash disk which most likely isn’t formatted. Just plug a thumb drive into the Raspberry Pi USB port and check if it has been detected by typing

sudo blkid

You should see in the list /dev/sda1 device, which is our USB drive:

pi_device_attached

Since I want my drive to work on PC, it needs to be formatted with a FAT32 partition. First of all, you need to install Windows/DOS FAT32 support tools:

sudo apt-get install dosfstool

After it gets installed, you can format the disk by entering the following command into the shell:

sudo mkfs -t vfat -I /dev/sdb1

Once the format is complete, you can create a mounting point. You will find that usually drives are mounted to directories in /mnt, which is the standard location. But you can create your directory pretty anywhere. So for my purposes, I created directory /pi/home/usb8gb. For this, go to your home directory by typing

cd ~

then enter the command:

sudo mkdir usb8gb

while in the home directory, you can mount your drive as follows:

sudo mount /dev/sda1 usb8gb

You will notice that folder ownership is set to root user, and you will need the superuser “sudo” command

To perform any actions. To make things much easier, go to the home directory. First, unmount the disk with the command

sudo umount /dev/sda1

then change folder empty folder ownership to user pi by typing

sudo chown -R pi:pi usb8gb

Then mount the drive so that the pi user can perform the operations usually:

sudo mount /dev/sda1 usb8gb -o uid=pi,gid=pi

After this, you can read, write and delete files on your USB drive.

As a step further, let’s make this mounting process automatic, so we don’t have to mount every time after reboot. Lets us first identify our drive. Each drive carries a unique ID. To find one, lets type command

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

This gives us a list with drives:

usb_UUD

Just copy this eight-digit number and then edit fstab file:

sudo nano /etc/fstab

Add the following line to the end of the f file:

UUID=5EF3-BBF0 /home/pi/usb8gb auto,users,rw,uid=pi,gid=pi 0 0

To test if it’s working, reboot raspberry pi:

sudo reboot

Then try to see if USB disc content are present on a usb8gb folder with the command

ls usb8gb

Final note. Using UUID in fstab gives some advantage over using /dev/sda1. This way, you can have a collection of USB drives where each can be mounted automatically to its mounting point. But if you want one universal mounting point for all USB drives then add the following line to the fstab file:

/dev/sda1 /home/pi/usb8gb auto,users,rw,uid=pi,gid=pi 0 0

If you have questions or more to add, please comment.

Comments are closed.