3

I configured an automount for a USB drive by adding an extra rule to /etc/fstab as belows

/dev/sda1 /media vfat noauto,nofail,x-systemd.automount,x-systemd.idle-timeout=2,x-systemd.device-timeout=2 

This entry is then picked by systemd's fsab-generator to create two unit files for mount and automount of /dev/sda1:

  • media.automount
  • media.mount

Once inserted, the USB drive is mounted correctly into the mount point as specified. This is verified by running ls /media. The output of journalctl is:

Sep 17 13:19:55 pcxx systemd[1]: Mounted /media. Sep 17 13:19:57 pcxx systemd[1]: Unmounting /media... Sep 17 13:19:57 pcxx systemd[1]: Unmounted /media. 

1. Why does systemd unmount /media right after mounting it?

The aim is to be able to cleanly unmount the USB drive, after using it.

I found out there are two ways to do it.

The first one is to run umount /media. This does the job but does corrupt media.automount service. The error message observed in journalctl and when running systemctl status -l media.automount is:

Sep 17 13:28:44 pcxx systemd[1]: media.automount: Got invalid poll event 16 on pipe (fd=59) Sep 17 13:28:44 pcxx systemd[1]: media.automount: Failed with result 'resources'. 

The second one is to run systemd's umount command systemd-umount /media. This also does the job but stops both media.mount and media.automount services. The consequence of this being not able to automount when the next USB drive is inserted, which defeats the purpose of having automount in the first place.

2. Is there a proper way to unmount a drive whose automount is handled by systemd without stopping the automount service?

0

2 Answers 2

2

Q1: Why does systemd unmount /media right after mounting it?

A1: Because you told it to unmount after 2 seconds of inactivity:

The x-systemd.idle-timeout option controls this. You set it to 2 seconds in your /etc/fstab entry : x-systemd.idle-timeout=2

Q2. Is there a proper way to unmount a drive whose automount is handled by systemd without stopping the automount service?

A2. Let systemd.automount do that for you.

As in Q1, the x-systemd.idle-timeout option controls how long the mount may be idle before it is unmounted. As you currently have it set, that is 2 seconds. And so, after 2 seconds of inactivity, your USB drive is unmounted. At that point, all you need do is un-plug/pull the drive. You may also verify it is unmounted if you wish using lsblk -f command: if it shows no mount point for your USB drive, then it is not mounted. Just so we're clear, here's an example from my system (ignore mmcblk0 - it's the system SD card):

When the USB drive is mounted, lsblk -f shows the mount point for sdb1:
pi@raspberrypi3b:~ $ lsblk -f NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINT sdb └─sdb1 ext4 1.0 SANDISK8GB e5cb39a9-b041-4339-92f5-4172201a4b1a 2.6G 59% /home/pi/SANDISK8GB_ThumbDrv mmcblk0 ├─mmcblk0p1 vfat FAT32 boot C839-E506 203M 19% /boot └─mmcblk0p2 ext4 1.0 rootfs 568caafd-bab1-46cb-921b-cd257b61f505 26.3G 5% / 
If you monitor the output of journalctl --follow, a message similar to this informs you that your USB drive has unmounted after being idle for the time value set by x-systemd.idle-timeout:
Jan 17 10:46:40 raspberrypi3b systemd[1]: Unmounting /home/pi/SANDISK8GB_ThumbDrv... Jan 17 10:46:40 raspberrypi3b systemd[750]: home-pi-SANDISK8GB_ThumbDrv.mount: Succeeded. Jan 17 10:46:40 raspberrypi3b systemd[1]: home-pi-SANDISK8GB_ThumbDrv.mount: Succeeded. Jan 17 10:46:40 raspberrypi3b systemd[1]: Unmounted /home/pi/SANDISK8GB_ThumbDrv. 
The drive will remain unmounted until it gets another automount request. In this case, the request was triggered by typing ls in the CLI as shown at the end of the first line below:
Jan 17 10:40:38 raspberrypi3b systemd[1]: home-pi-SANDISK8GB_ThumbDrv.automount: Got automount request for /home/pi/SANDISK8GB_ThumbDrv, triggered by 927 (ls) Jan 17 10:40:38 raspberrypi3b systemd[1]: Mounting /home/pi/SANDISK8GB_ThumbDrv... Jan 17 10:40:38 raspberrypi3b kernel: EXT4-fs (sdb1): mounted filesystem with ordered data mode. Opts: (null) Jan 17 10:40:38 raspberrypi3b systemd[1]: Mounted /home/pi/SANDISK8GB_ThumbDrv. 
You can verify the USB drive is unmounted w/ lsblk -f; if no mount point is shown, the device is not mounted:
pi@raspberrypi3b:~ $ lsblk -f NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINT sdb └─sdb1 ext4 1.0 SANDISK8GB e5cb39a9-b041-4339-92f5-4172201a4b1a mmcblk0 ├─mmcblk0p1 vfat FAT32 boot C839-E506 203M 19% /boot └─mmcblk0p2 ext4 1.0 rootfs 568caafd-bab1-46cb-921b-cd257b61f505 26.3G 5% / 
1

Don't mount on /media but instead make a new directory in it and mount it there. /media is a reserved location in most linux's.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.