Follow by Email

Blog Archive

Search This Blog

mount unmount USB stick and USB hard disks using fstab

how to mount or unmount USB stick and USB hard disks using fstab
Share it:
Automatically mount / unmount USB stick and USB hard disks with "fstab"

By default, Raspbian Jessie automatically includes new USB drives in the path "/ media / {USERNAME} / {LABEL}". But that only does it when a user logs on to the system. However, if the system is running in headless mode and / or without a logged in user, then USB drives are not automatically included.

For automatic mounting / mounting / mounting of USB sticks and USB hard disks, even without user login, "usbmount" is recommended.
  1. Automatically mount / unmount USB stick with "usbmount"
However, "usbmount" has the disadvantage that all drives are treated the same. This means that it is not sure under which directory a particular drive can be found. It can be annoying if you want to plug in a USB stick and then always find out first where to find it in the file system.
The random assignment can also be a problem, for example, if you want to automate a backup to a specific USB stick or a hard drive. In this case, you have to rely on the drive always being mounted in the same directory.
The requirements:
  • Fixed mapping between a specific drive and a specific directory in the file system.
  • Inclusion even in the headless mode if no user logon has taken place.
  • Check available support for file systems.
  • Automatic mounting of drives with "fstab".
Solution: Check available support for file systems
For a file system to be recognized and mounted on a drive, the appropriate file system modules must be installed. For this purpose, one should check which file systems the running kernel supports.
ls -1 / lib / modules / $ (uname -r) / kernel / fs
To access NTFS partitions you need the package "ntfs-3g", which is automatically installed on Raspbian Jessie. If not, then you can install it later.
sudo apt-get install ntfs-3g
Note: NTFS may be quite useful under Linux in most cases. Nevertheless, there are voices that call it "unstable" under Linux. Try it and decide for yourself.

Solution: Mount / mount drives with the file "/ etc / fstab"
This solution is equally suitable for USB sticks and USB hard disks.

Before connecting the USB stick or the hard disk, you should check which drives are already mounted.
df -h
Then you can connect the drive and check if the drive has been detected.
df -h
As a rule, USB sticks and hard disks labeled "sda", "sdb", etc. are displayed and the partition on them is labeled "sda1", "sda2" and so on. It is important that we identify the right drive and the corresponding partitions on the spot.

The next step is to find the so-called UUID (Universally Unique Identifier) or the LABEL (user-defined name) of the partition to be mounted, which we will later use to define the mount.
sudo blkid
It is important that the command "blkid" is executed with "sudo" or as "root", because otherwise it will produce an incomplete or a wrong output.

Example output from a drive (sda) containing a partition (sda1) with "FAT" formatted file system:
/ dev / sda1: SEC_TYPE = "msdos" LABEL = "USB" UUID = "BB30-110E" TYPE = "vfat" PARTLABEL = "USB" PARTUUID = "724fd228-1c4b-450a-bc1a-087008c2a3fc"
Example output from a drive (sda) containing a partition (sda1) with "ext2" formatted file system:
/ dev / sda1: LABEL = "USB" UUID = "3f21afb1-15d1-43f9-9e0f-664afd587a6b" TYPE = "ext4" PARTUUID = "0004f309-01"
Example output from a drive (sda) containing a partition (sda1) with "NTFS" formatted file system:
/ dev / sda1: LABEL = "USB" UUID = "74EA618724897ACE" TYPE = "ntfs" PARTUUID = "00034166-01"
Depending on the system, the output of "sudo blkid" may differ. By way of example, we are interested in either the UUID or the LABEL of the partition and the file system (TYPE). But beware, if you change the file system by formatting, then the UUID changes too.

The UUID or LABEL is important because the device file name may change in the form of / dev / sda, / dev / sdb, and so on. Only the UUID of the file system is unique, as long as you do not format it.

Now it's about assigning the partition or the file system to a mount point in the file "/ etc / fstab". To do this, we open the following file:
sudo nano / etc / fstab
In this file, no drives (USB stick or hard disk) but file systems are entered, with which one or more partitions are formatted on the drive.

Please note that the following lines have to be changed individually. Namely the UUID or the LABEL (here we use the UUID), the Mount Point, the file system and possibly also the options. You can get the UUID and the file system via the command "blkid". The mount point is the directory under which you want to find the file system later.

Example of a FAT formatted file system:
UUID = BB30-110E / media / usb vfat auto, nofail, sync, users, rw, umask = 000 0 0
Example of an ext4 formatted file system:
UUID = 3f21afb1-15d1-43f9-9e0f-664afd587a6b / media / usb ext4 auto, nofail, sync, users, rw 0 0
Example of an NTFS formatted file system:
UUID = 74EA618724897ACE / media / usb ntfs auto, nofail, sync, users, rw 0 0
If you have made or changed the entry, then save the file and close it with Ctrl + O, Return, Ctrl + X.

Then, the local directory for the mount point must be created.
sudo mkdir -p / media / usb
Then you can mount / unmount the drive or the file system manually.
sudo mount -a
You check whether the entry in the file "/ etc / fstab" has been created correctly.

With "df -h" and "lsblk" you can check if and where (mount point) was mounted.

After a reboot, the drive should be automatically included.

Extension: Permissions in the file system FAT
A FAT file system, in contrast to Linux file systems, does not recognize file owners or user groups. If you want to give a particular user and / or group of users access to the file system, then you have to provide additional information in the / etc / fstab file in the mount options columns. Here you can use the options "uid" and "gid" to define the owner and / or group affiliation. 
In the example above, the owner of the user is "pi" and the user group is "users".
..., uid = pi, gid = users, ...
Note: The user group "users" in this case has nothing to do with the fstab option "users".

There may be reasons why the integration does not work. For example, because the drive can not be addressed while processing the file "/ etc / fstab". Then a "mount -a" before the "exit 0" in the file
"/etc/rc.local" helps.
Share it:


Post A Comment: