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
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