Raspberry Pi – formatting and mounting USB drive

Lets say you bought fresh new flash disk which most likely isn’t formatted. Simply plug thumb drive in to 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 need to be formatted with 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 following command in to shell:

sudo mkfs -t vfat -I /dev/sdb1

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

cd ~

then enter command:

sudo mkdir usb8gb

while in 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 superuser “sudo” command

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

sudo umount /dev/sda1

then change folder empty folder ownership to user pi by typing

sudo chown -R pi:pi usb8gb

then mount drive so that pi user could perform operations normally:

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

after this you can read write and delete files in your usb drive.

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

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

This gives us a list with drives:

usb_UUD

Simply copy this eight digit number and then edit fstab file:

sudo nano /etc/fstab

Add the following line to the end of file:

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

To test if its working reboot raspbery pi:

sudo reboot

then try to see if usb disc content are present on usb8gb folder with 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 own mounting point. But if you want one universal mounting point for all USB drives then add following line to 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.

Bookmark the permalink.

Leave a Reply

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