How to support automatic mounting of USB disk in Ubuntu 18.04

Follow these steps:

1. Change the /media/lianro directory permissions

Use the following command:

linaro@bionic:~$ sudo chown linaro:linaro -R /media/linaro/
linaro@bionic:~$ ls /media/ -l
total 8
drwxr-xr-x  4 linaro linaro 4096 Oct 21 17:42 linaro
drwxr-x---+ 2 root   root   4096 Oct 21 14:11 root

In this way, you can mount the USB drive to the /media/linaro directory.

2. Create udev rules

Create a file in the following directory: /etc/udev/rules.d/99-usb-mount.rules, with the following content:

ACTION=="add", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", ENV{ID_FS_TYPE}!="",  RUN+="/bin/systemctl restart usb-mount.service"

ACTION=="remove", SUBSYSTEM=="block", RUN+="/bin/systemctl start usb-unmount@%k.service"

3. Create systemd service

3.1 usb-mount.service

Use the following steps:

  • (1). Create service
sudo vim /etc/systemd/system/usb-mount.service

The content is as follows:

linaro@bionic:~$ cat /etc/systemd/system/usb-mount.service
[Unit]
Description=USB auto-mount service
After=dev-sda4.device

[Service]
Type=oneshot
ExecStart=/usr/local/bin/mount_usb.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
  • (2). Enable service
sudo systemctl enable usb-mount.service

3.2 usb-unmount@.service

  • (1). Create service
sudo vim /etc/systemd/system/usb-unmount@.service

The content is as follows:

linaro@bionic:~$ cat /etc/systemd/system/usb-unmount@.service
[Unit]
Description=Unmount USB Drive %i
Requires=dev-%i.device
After=dev-%i.device

[Service]
Type=oneshot
ExecStart=/usr/local/bin/unmount_usb.sh /dev/%i

[Install]
WantedBy=multi-user.target

(2). Start up
The following command

sudo systemctl enable /etc/systemd/system/usb-unmount@.service

4. Create an execution script file

Create the following two script files respectively:

4.1 mount_usb.sh

linaro@bionic:~$ cat /usr/local/bin/mount_usb.sh
#!/bin/bash

device=$(lsblk -p -o NAME,TYPE --noheadings -r | grep -E "part" | grep -v "boot\|rpmb\|loop\|mmcblk" | awk '{print $1}' | tail -n 1)

if [ -n "$device" ]; then
  label=$(blkid -o value -s LABEL "$device")

  if [ -z "$label" ]; then
    label="Unknown"
  fi

  mount_point="/media/linaro/$label"

  mkdir -p "$mount_point"
  sudo mount "$device" "$mount_point"
else
  echo "cannot find any u disk"
fi

Change to executable permissions, as follows:

linaro@bionic:~$ sudo chmod +x /usr/local/bin/mount_usb.sh
linaro@bionic:~$ ls -l /usr/local/bin/mount_usb.sh
-rwxr-xr-x 1 root root 799 Oct 21 15:50 /usr/local/bin/mount_usb.sh

4.2 unmount_usb.sh

linaro@bionic:~$ cat /usr/local/bin/unmount_usb.sh
#!/bin/bash

# get the device name that was removed.
device="$1"

mount_point=$(mount | grep "$device" | awk '{print $3}')

if [ -n "$mount_point" ]; then
  sudo umount "$device"
  label=$(blkid -o value -s LABEL "$device")

  if [ -z "$label" ]; then
    label="Unknown"
  fi

  sudo rmdir "$mount_point"
else
  echo "cannot find mounted disk"
fi

Change to executable permissions, as follows:

linaro@bionic:~$ sudo chmod +x /usr/local/bin/unmount_usb.sh
linaro@bionic:~$ ls -l  /usr/local/bin/unmount_usb.sh
-rwxr-xr-x 1 root root 1136 Oct 21 17:41 /usr/local/bin/unmount_usb.sh

5. Test

  • First, make sure udev is in effect. You can use the following command:
linaro@bionic:~$ sudo udevadm control --reload
linaro@bionic:~$ sudo udevadm trigger
linaro@bionic:~$ systemctl status udev
● systemd-udevd.service - udev Kernel Device Manager
   Loaded: loaded (/lib/systemd/system/systemd-udevd.service; static; vendor preset: enabled)
   Active: active (running) since Mon 2024-10-21 17:35:15 CST; 42min ago
     Docs: man:systemd-udevd.service(8)
           man:udev(7)
 Main PID: 20237 (systemd-udevd)
   Status: "Processing with 20 children at max"
    Tasks: 1
   CGroup: /system.slice/systemd-udevd.service
           └─20237 /lib/systemd/systemd-udevd

Of course, you can also reboot the device and test again.

  • Insert different USB drives respectively
linaro@bionic:~$ mount | grep media
/dev/sda4 on /media/linaro/DEBIAN type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=936,iocharset=utf8,shortname=mixed,errors=remount-ro)
linaro@bionic:~$ ls /media/linaro/DEBIAN/
'Camtasia Studio2019-64位.zip'              SDIO-Fimware                 tek00000.png   tek00002.png   wifi-swt6652
 MATLAB_Runtime_R2022b_Update_4_win64.zip  'System Volume Information'   tek00001.png   tek00003.png
linaro@bionic:~$ mount | grep media
linaro@bionic:~$ mount | grep media
/dev/sda1 on /media/linaro/B1 type fuseblk (rw,relatime,user_id=0,group_id=0,allow_other,blksize=4096)
linaro@bionic:~$ ls /media/linaro/B1/
 ND_Test   over_night  'System Volume Information'
linaro@bionic:~$ mount | grep media
linaro@bionic:~$

From the log above, we can see that when the USB flash drive is inserted, it is automatically mounted to the /media/linaro/DEBIAN directory, where DEBIAN is the volume label of the USB flash drive; after the USB flash drive is unplugged, it is automatically unmounted.