2

Is there a way for a systemd.mount unit to detect the state of a drive without having to reboot the computer, or reload the unit? If not, is there any other way to achieve this?

I would like to automatically mount a drive when it is inserted, and automatically unmount it when it is disconnected.

My current systemd.mount configuration:

[Unit]
Description=var-test.mount

[Mount] What=LABEL=TEST_LABEL Where=/var/test Type=ext4 Options=defaults,noatime

[Install] WantedBy=multi-user.target

The reason I want to do this is because when the drive is disconnected, it doesn't get unmounted. So e.g /dev/sda1 will remain mounted, but not accessible. When the drive is then inserted, it will get a new partition like /dev/sdb1, which cannot be mounted to the same mount-point since it's already in use.

I can avoid this by stopping the systemd.mount unit before disconnecting the drive, and starting it after inserting it again. However this is a step I want to automate.

Edit: I think I got a decent answer from the linked post below. I will look into udev rules. Perhaps it's possible to call my systemd.mount unit from a udev rule. https://unix.stackexchange.com/a/44509/540247

  • 1
    I think there is a problem in the setup of umount systemd process. I have a guess it might be crashed during a upgrade. Try to reinstall this. – Sohan Arafat Sep 05 '22 at 16:33
  • @SohanArafat I realize that I've been a bit naive with my ideas. I actually need to unmount a drive before disconnecting it, otherwise data may become corrupted. And there is no possible way to predict when a drive will be disconnected, and automatically unmount it before it disconnects.

    However, I think I can at least automatically mount a drive when it is connected by calling my systemd.mount with a udev rule.

    – seglaren Sep 06 '22 at 08:45
  • On most systems mount is automatic after attachment. Usually, if it does not mount automatically, you will have a hard time manually and of course disconnection is not predictible. Before disconnecting, you should unmount to avoid problems. – Pierre ALBARÈDE Sep 08 '22 at 13:53

1 Answers1

-1

Using udev rules did the trick for me. I simply created a udev rule that would run my mount-unit. So every time a device connects (properties of device specified in the udev rules below), it will be mounted. And every time a device disconnects, it will be unmounted.

Looks something like this:

# Mount when disk is inserted
KERNEL=="sd[a-z][0-9]", KERNELS=="ata[12]", SUBSYSTEM=="block", ENV{ID_FS_TYPE}=="ext4", ENV{ID_FS_LABEL}=="TEST_LABEL", ACTION=="add", RUN+="/bin/systemctl restart var-test.mount"

Unmount device when removed (Edit: don't do this)

KERNEL=="sd[a-z][0-9]", KERNELS=="ata[12]", SUBSYSTEM=="block", ENV{ID_FS_TYPE}=="ext4", ENV{ID_FS_LABEL}=="TEST_LABEL", ACTION=="remove", RUN+="/bin/systemctl restart var-test.mount"

Edit: As posted by @roaima below, it is a bad idea to unmount a device after it is disconnected. However, the solution above still works for mounting a device when it is inserted.

  • 1
    You can't unmount a filesystem after the device has been disconnected, because the final unmounted state cannot be written back to the filesystem (the device has already gone). At best you can tell your system to accept the filesystem is no longer there, but you have no guarantee data was completely written to the filesystem. And when you then try to mount it next time it will be still marked as in use and may require to be checked for possible errors. It's a great way to corrupt data in that removable filesystem – Chris Davies Sep 08 '22 at 11:04
  • 1
    What might be safer if you're really in the habit of just removing disks is to use a more primitive filesystem that doesn't care too much, such as FAT or vFAT. Or use the automounter to unmount the disk filesystem after a very short period of inactivity (say 1 minute). That will force a flush to the disk and keep the filesystem reasonably tidy – Chris Davies Sep 08 '22 at 11:06
  • @roaima Thank you for your comment! I've been struggling a lot on how to proceed with this task. So even though I did realize that unmounting the device after it has been removed, is in fact bad (see my first post), I still decided to do it. I guess that I got stuck in a thought-loop. Let's say the disk is disconnected without unmounting it first. When the disk is then inserted again, it can't be mounted again since the mountpoint is already in use, no? I'm very unsure on how to proceed here.

    I will remove the "Accept answer" on my posted "solution". Again, thanks for helping!

    – seglaren Sep 08 '22 at 11:58
  • @roaima Do you think it would be OK to just remove the rule that unmounts the device when it's removed? I actually tried doing it now. When I removed the device, the filesystem was still there but unaccessible. I then connected the device again and it re-mounted on the filesystem just with a new partition (sdb1 instead of sda1). Is this an OK approach? I just want to make it semi-OK to accidently remove the disk without unmounting it first. – seglaren Sep 08 '22 at 12:20
  • "I just want to make it semi-OK to accidently remove the disk without unmounting it first" it's never ok to remove a disk mounted read/write without first unmounting its filesystem. – Chris Davies Sep 08 '22 at 17:03
  • 1
    Alrights, I understand. Thank you for your help. I'll look into your tip on unmounting the disk depending on its inactive state. Have a nice evening! – seglaren Sep 08 '22 at 17:18