Pages

2020/02/17

Raspberry Pi - How to mount an USB Flash Drive

This is a quick write up of me following the instruction below on how to mount an USB Flash Drive to my Raspberry Pi.

How To Mount A USB Flash Disk On The Raspberry Pi
https://www.raspberrypi-spy.co.uk/2014/05/how-to-mount-a-usb-flash-disk-on-the-raspberry-pi/


1. Be sure the RPI is power off

Plugging in the USB Flash Drive when RPI is on will cause the RPI to re-boot and it could crash the system.

2. Plug in the USB Flash Drive

3. Switch on the power to RPI

4. List all block devices

pi@raspberrypi:~ $ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    1 14.7G  0 disk
└─sda1        8:1    1 14.7G  0 part
mmcblk0     179:0    0 14.9G  0 disk
├─mmcblk0p1 179:1    0  256M  0 part /boot
└─mmcblk0p2 179:2    0 14.6G  0 part /
pi@raspberrypi:~ $

In the output above, sda represents the external USB Flash Drive, mmcblk0 represents the internal SD Card.

5. Identify the devices unique ID (UUID).

pi@raspberrypi:~ $ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Feb 16 08:18 2420-5E40 -> ../../sda1
lrwxrwxrwx 1 root root 15 Feb 16 08:18 2ab3f8e1-7dc6-43f5-b0db-dd5759d51d4e -> ../../mmcblk0p2
lrwxrwxrwx 1 root root 15 Feb 16 08:18 5203-DB74 -> ../../mmcblk0p1
pi@raspberrypi:~ $

Note down the UUID for /sda1, in my case, it's "2420-5E40". This UUID will be used later.

6. Create a mount point

A mount point is a directory that points to the contents of the USB Flash Drive. Use the command below to create a directory (/media/usb).

sudo mkdir /media/usb

Now that we need to make sure the Pi user owns this folder :

sudo chown -R pi:pi /media/usb

pi@raspberrypi:/media $ ls -l
total 4
drwxr-xr-x 2 root root 4096 Feb 16 09:15 usb
pi@raspberrypi:/media $ sudo chown -R pi:pi /media/usb
pi@raspberrypi:/media $ ls -l
total 4
drwxr-xr-x 2 pi pi 4096 Feb 16 09:15 usb
pi@raspberrypi:/media $

Below is the meaning of each field.

1 | Permissions | drwxr-xr-x
2 | # of Hard Links | 2
3 | User That Owns File or Directory | root
4 | Group for File or Directory | root
5 | File Size | 4096
6 | Timestamp | Feb 16 09:15
7 | Filename | usb

Ref.: https://www.liquidweb.com/kb/7-extremely-useful-linux-commands-for-beginners/

Note that in the output above, before executing the command sudo chown -R pi:pi /media/usb, the owner of the folder is root, the owner changed to pi after the command is executed.

7. Manually mounting the drive

To manually mount the drive use the following command :

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

This will mount the drive so that the ordinary Pi user can write to it. Omitting the “-o uid=pi,gid=pi” would mean you could only write to it using “sudo”.

Now you can read, write and delete files using “/media/usb” as a destination or source without needing to use sudo.

8. Un-mounting the drive

You don’t need to manually un-mount if you shutdown your Pi but if you need to remove the drive at any other time you should un-mount it first. Only the user that mounted the drive can un-mount it.

umount /media/usb

If you used the fstab file to auto-mount it you will need to use :

sudo umount /media/usb

9. Auto mount

When you restart your Pi your mounts will be lost and you will need to repeat Step 7 (Manually mounting the drive). If you want your USB drive to be mounted when the system starts you can edit the fstab file :

sudo nano /etc/fstab

Then add the following line at the end :

UUID=2420-5E40 /media/usb vfat auto,nofail,noatime,users,rw,uid=pi,gid=pi 0 0

***IMPORTANT - Be sure to set the correct UUID ***

The “nofail” option allows the boot process to proceed if the drive is not plugged in. The “noatime” option stops the file access time being updated every time a file is read from the USB stick. This helps improve performance.

Below is how my fstab file looks like after adding the line to auto mount the USB Drive.

pi@raspberrypi:/media $ sudo nano /etc/fstab
pi@raspberrypi:/media $ cat nano /etc/fstab
cat: nano: No such file or directory
proc            /proc           proc    defaults          0       0
PARTUUID=6c586e13-01  /boot           vfat    defaults          0       2
PARTUUID=6c586e13-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
UUID=2420-5E40 /media/usb vfat auto,nofail,noatime,users,rw,uid=pi,gid=pi 0 0
pi@raspberrypi:/media $

Additional note about file systems

In the examples above I specified “vfat” as the file system of the device as it was formatted as FAT32. If you need to change the file system replace references of “vfat” with “ntfs-3g”, “ext3” or “ext4”.

If you are using NTFS you will also need to install the following package :

sudo apt-get install ntfs-3g

No comments:

Post a Comment