Configuring udev and systemd to USB automount

Linux Kernel, Network, and Services configuration.
Post Reply
Message
Author
jager
Posts: 17
Joined: 2022-03-28 14:55

Configuring udev and systemd to USB automount

#1 Post by jager »

Hello,
I need yours help after spending 2 whole days to try configuring usb automount.
I run net install Debian 12 and with only dwm window manager.

What I try to achieve is to automount usb drives just after insertion. I found a lot of resources but nothing worked for me.
I wrote a bash script which works fine when I run it manually - means when I insert usb and run script it is mounted and when I run it with different parameter is unmounted.

I found this : https://www.baeldung.com/linux/automount-usb-device - but it did not work.
and this - https://andreafortuna.org//2019/06/26/a ... d-systemd/ which is faulty.

but I made some changes according the latter example and this looks like this:
/etc/systemd/system/usb-mount@.service

Code: Select all

[Unit]
Description=Mount USB Drive on %i

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/usb_mount.sh %i
ExecStop=/usr/local/bin/usb_mount.sh %i

[Install]
WantedBy=multi-user.target
/etc/udev/rules.d/99-local.rules

Code: Select all

KERNEL=="sd[a-z][0-9]", SUBSYSTEMS=="usb", ACTION=="add", RUN+="/bin/systemctl start usb_mount@add.service"
KERNEL=="sd[a-z][0-9]", SUBSYSTEMS=="usb", ACTION=="remove", RUN+="/bin/systemctl stop usb_mount@remove.service"
and the problem is that

Code: Select all

sudo systemctl status usb_mount@add.service
is always active and to mount usb I need to stop this and when I insert usb is going active and mounting the drive but

Code: Select all

sudo systemctl status usb_mount@remove.service
is always dead - and to unmount the usb I need to start it. And whenever I want to insert next/second usb is not going to mount.

Is there anyone who knows hot to configure above solution to be correct one - because I did something wron but no Idea how fix this.
Thank you in advance!!!

jager
Posts: 17
Joined: 2022-03-28 14:55

Re: Configuring udev and systemd to USB automount

#2 Post by jager »

Ok I changed RemainAfterExit=yes to RemainAfterExit=no - and now it can mount one or more drives but still whenever I get off usb it will not unmount and the systemctl status is dead and not unmounting.

This means that when I remove the usb my script with removing function is not invoked!
And when I hit manually run systemctl start usb_mount@remove.service then all removed usb are unmouted in the system.
So probably the problem is how to start ExecStop=/usr/local/bin/usb_mount.sh remove %i from the usb_mount@remove.service when removing usb?

jager
Posts: 17
Joined: 2022-03-28 14:55

Re: Configuring udev and systemd to USB automount

#3 Post by jager »

Ok I solved this - sorry Guys for bothering you :)

jager
Posts: 17
Joined: 2022-03-28 14:55

Re: Configuring udev and systemd to USB automount

#4 Post by jager »

Sent: 2024-09-15 22:59
by bw123
For usb flash drives I use systemd-automount, in fstab. Looks like this:

Code: Select all

UUID=GET_YOUR_OWN_UUID /wherever/YOURMNTPOINT ext4 noauto,x-systemd.automount,x-systemd.idle-timeout=4,x-systemd.device-timeout=3s,errors=remount-ro 0 0
It mounts when accessed, if present, and unmounts after 4 sec when nothing accessing the device is open. You shouldn't remove the usb device when in use. Using an unmount service seems silly, because you can't cleanly unmount something if it's already removed?

good luck,
bw
bw123 - you sent me a private message and switched off posibility to answer you in this mode so I am responding here. Hope it is ok.


Using an unmount service seems silly, because you can't cleanly unmount something if it's already removed?
(BTW that is standard procedure - in every file manager - you removing the usb - do not need to unmount this - and the file manager does this unmounting for you. obiously there is an option - button where you click and do manually unmounting - but this is done by minority of users :) )

Yes you can it holds this mounting point in cache even if usb was removed - it just not giving an access to the files on usb. So after removing for example /media/sda1 - this directory is present and if you run sudo rm -r /meda/sda1 it complaining that there are sill resources and the drive is busy. You need to first do sudo umount /media/sda1 and then remove directory.

Am I understood correctly - you need to find at first GET_YOUR_OWN_UUID and then manually do input to fstb as it is hard-coded?
Thanks

Aki
Global Moderator
Global Moderator
Posts: 4176
Joined: 2014-07-20 18:12
Location: Europe
Has thanked: 122 times
Been thanked: 561 times

Re: Configuring udev and systemd to USB automount

#5 Post by Aki »

Hello,
jager wrote: 2024-09-15 21:17 Ok I solved this - sorry Guys for bothering you :)
Can you please tell us what solution you have chosen ?

Please, use code tags to include commands and/or their logs in the body of a message. I did this time for you in previous messages.
⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
⠈⠳⣄⠀

jager
Posts: 17
Joined: 2022-03-28 14:55

Re: Configuring udev and systemd to USB automount

#6 Post by jager »

Hello

@ Please, use code tags to include commands and/or their logs in the body of a message. I did this time for you in previous messages.
Thank you I was trying but without success and later on I forgot to fix it.

@Can you please tell us what solution you have chosen ?
Sure I had to test some edge cases - what I did and tomorrow in the evening CET I should give a detailed description how I did automounting the usb flash drives which solution might be a pattern how to implement automounting other devices.
Best regards

jager
Posts: 17
Joined: 2022-03-28 14:55

Re: Configuring udev and systemd to USB automount

#7 Post by jager »

Ok - as I mentioned above that I will share with you my solution - I need to ask you for little bit more patience (because right now I am busy at my job a little bit). I tested another edge case and my solution was failing - short story long - there are an issues that ssd disks are seen as sd* because they use SATA 3 interface but can be installed as a hard drive and a usb as well, there nowadays are nvme disks which can be pluged in through usb socket as well they can be installed as a hard drives. All those must be take into a consideration. I compered my solution of mountng and unmounting with the standard ubuntu gnome installment and more or less is the same but I need to do some amendments to not provide you a faulty solution.

Post Reply