Linux - Mount ext4 USB flash drive to Raspberry Pi
Quote from HeelpBook on June 30, 2021, 5:03 pmHow to properly mount a USB flash drive, formatted with ext4 file system, on a Raspberry Pi?
How to properly mount a USB flash drive, formatted with ext4 file system, on a Raspberry Pi?
Quote from HeelpBook on July 1, 2021, 11:59 amLet's create a media directory for the flash drive for our user. The user is for now called user and the flash drive gets the name sandisk, cause I have a 128 GB USB flash drive from SanDisk. You should replace both.
sudo mkdir -p /media/user/sandiskDetecting the flash drive
We need to make sure that the USB flash drive is plugged by running the following command:
sudo blkid -o list device fs_type label mount point UUID ------------------------------------------------------------------------------------------------------ /dev/mmcblk0p1 vfat boot /boot XXXX-XXXX /dev/mmcblk0p2 ext4 / xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /dev/sda1 ext4 SanDisk (not mounted) xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /dev/mmcblk0 (in use)The output should look like the above. If you have not identified the drive, just unplug the drive, run again the above command, plug the drive and re-run the command again. This should help you to find your flash drive.
The important column is the device one. In the above case the SanDisk flash drive has the path /dev/sda1.
Mounting the device
As we now know the path of the device, we can mount it with the following command.
sudo mount -t ext4 -o defaults /dev/sda1 /media/user/sandiskNOTE: the defaults is a shortcut, in mount command, for the following mount options: rw, suid, dev, exec, auto, nouser, and async.
- rw: the Linux "rw" (Read Write) mount option is used to mount the filesystem read-write.
- async: the "async" mount option specifies the input and output to the filesystem is done asynchronously. When you copy a file to a removable media (like floppy drive) with "async" option set, the changes are physically written to the device some time after issuing the copy command. If "async" option is set and if you remove the media without using the "unmount" command, some changes you made may be lost.
- exec: this mount option allows you to execute binaries stored on that partition
- auto: this mount option allows the the device to be mounted automatically at bootup.
- nouser: this mount option allows only the super user (root) to mount the device.
- suid: this is a special permission that stands for Set owner User ID. This permission that applies to scripts or applications and let, when the command/application is run, to run/execute it as it would be executed by the owner of the file (using owner's UID), instead of the user running it (in some environment this bit could lead to a risk of root escalation).
- dev: this mount option tells that the mounted device can contain special devices (or raw devices) on the filesystem (this can also be a security risk, in some environments, letting the a direct control of hardware devices).
Unmount
We can unmount the device using the following approach:
sudo umount /media/user/sandiskMount on boot
We want our USB flash drive for our Nextcloud instance. So it would be nice if the device is always available after every boot. To do this we need the column UUID from the output above and then we need to edit the file /etc/fstab with any editor, for example nano (or vi or vim).
sudo nano /etc/fstabNow we need to add the following line at the end of the file. Make sure to replace the UUID and the path of the mounted device.
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /media/user/sandisk ext4 defaults 0It would be possible to use also /dev/sda1 instead of UUID=..., but the UUID makes sure that we are mounting this flash drive and not any other one that could be in the same USB slot. Keep this in mind if you replace the USB flash drive with an other one.
Setup permissions
After that we will be able change the owner of this media directory to our user (or others, if needed, as shown in the following example):
sudo chown www-data:www-data -R /media/user/
Let's create a media directory for the flash drive for our user. The user is for now called user and the flash drive gets the name sandisk, cause I have a 128 GB USB flash drive from SanDisk. You should replace both.
sudo mkdir -p /media/user/sandisk
Detecting the flash drive
We need to make sure that the USB flash drive is plugged by running the following command:
sudo blkid -o list device fs_type label mount point UUID ------------------------------------------------------------------------------------------------------ /dev/mmcblk0p1 vfat boot /boot XXXX-XXXX /dev/mmcblk0p2 ext4 / xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /dev/sda1 ext4 SanDisk (not mounted) xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /dev/mmcblk0 (in use)
The output should look like the above. If you have not identified the drive, just unplug the drive, run again the above command, plug the drive and re-run the command again. This should help you to find your flash drive.
The important column is the device one. In the above case the SanDisk flash drive has the path /dev/sda1.
Mounting the device
As we now know the path of the device, we can mount it with the following command.
sudo mount -t ext4 -o defaults /dev/sda1 /media/user/sandisk
NOTE: the defaults is a shortcut, in mount command, for the following mount options: rw, suid, dev, exec, auto, nouser, and async.
- rw: the Linux "rw" (Read Write) mount option is used to mount the filesystem read-write.
- async: the "async" mount option specifies the input and output to the filesystem is done asynchronously. When you copy a file to a removable media (like floppy drive) with "async" option set, the changes are physically written to the device some time after issuing the copy command. If "async" option is set and if you remove the media without using the "unmount" command, some changes you made may be lost.
- exec: this mount option allows you to execute binaries stored on that partition
- auto: this mount option allows the the device to be mounted automatically at bootup.
- nouser: this mount option allows only the super user (root) to mount the device.
- suid: this is a special permission that stands for Set owner User ID. This permission that applies to scripts or applications and let, when the command/application is run, to run/execute it as it would be executed by the owner of the file (using owner's UID), instead of the user running it (in some environment this bit could lead to a risk of root escalation).
- dev: this mount option tells that the mounted device can contain special devices (or raw devices) on the filesystem (this can also be a security risk, in some environments, letting the a direct control of hardware devices).
Unmount
We can unmount the device using the following approach:
sudo umount /media/user/sandisk
Mount on boot
We want our USB flash drive for our Nextcloud instance. So it would be nice if the device is always available after every boot. To do this we need the column UUID from the output above and then we need to edit the file /etc/fstab with any editor, for example nano (or vi or vim).
sudo nano /etc/fstab
Now we need to add the following line at the end of the file. Make sure to replace the UUID and the path of the mounted device.
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /media/user/sandisk ext4 defaults 0
It would be possible to use also /dev/sda1 instead of UUID=..., but the UUID makes sure that we are mounting this flash drive and not any other one that could be in the same USB slot. Keep this in mind if you replace the USB flash drive with an other one.
Setup permissions
After that we will be able change the owner of this media directory to our user (or others, if needed, as shown in the following example):
sudo chown www-data:www-data -R /media/user/