USB Automount Topic is solved

Having issues with your DietPi installation, or, found a bug? Post it here.
chrnobel
Posts: 7
Joined: Sun May 12, 2019 11:34 pm

Re: USB Automount

Post by chrnobel »

Actually I have just installed usbmount, and it works.

But it is necessary to change MountFlags=slave to MountFlags=shared in /lib/systemd/system/systemd-udevd.service
peddanet
Posts: 6
Joined: Fri Sep 06, 2019 5:58 pm

Re: USB Automount

Post by peddanet »

@rosswesleyporter
Hi Your link is no longer working! Can you provide a new one?

For me the following worked:
I uninstalled autofs and usbmount cause they seem to conflict to my udev rule which did the job perfectly for me. I want to mount a special USB stick (and only this stick, if it is plugged in, to start a rsync process). So I created a file in udev rules in /etc/udev/rules.d/60-usbstartscript.rules:

Code: Select all

#Action when mounting
KERNEL=="sd[a-z][0-9]", ENV{ID_MODEL_ID}=="1a03", ENV{ID_VENDOR_ID}=="1b1c", ACTION=="add", ENV{mount_options}="relatime", RUN+="/bin/mount -o $env{mount_options} /dev/%k /    myUSBdevice"

#Action when unmounting
KERNEL=="sd[a-z][0-9]", ENV{ID_MODEL_ID}=="1a03", ENV{ID_VENDOR_ID}=="1b1c", ACTION=="remove", RUN+="/bin/umount -l /myUSBdevice"
You can find out the relevant Model ID and Vendor id by typing:

Code: Select all

$udevadm info -q all -p $(udevadm info -q path -n /dev/sdXY)
in my case /dev/sdb1
After that restart system with

Code: Select all

$udevadm control --reload-rules &&udevadm  trigger
$systemctl  daemon-reload
The code here helped me:
https://askubuntu.com/questions/792362/ ... untu-14-04

At least that worked for me!

[EDIT]:
That was not the whole solution! It turned out that commands run via RUN+= have only a short limited live and I got soon errors like "Transport endpoint is not connected” and my mount point broke before the script was run succesfully through: So I set up a systemd process which I instead started when hot plugged in:
Script in /etc/systemd/system/myUSBservice@.service

Code: Select all

[Unit]
Description=myUSBrsync.sh to /nextCloud
[Service]
Type=simple
ExecStart=/root/myUSBrsync.sh -v %I
and in the udev rule file /etc/udev/rules.d/60-usbstartscript.rules:

Code: Select all

KERNEL=="sd[a-z][0-9]", ENV{ID_MODEL_ID}=="1a03", ENV{ID_VENDOR_ID}=="1b1c", ACTION=="add", ENV{SYSTEMD_WANTS}="paeddasUSBStickSync@%k.service"
In the related script (myUSBrsync.sh) I do the mounting of the device using its UUID and prove if it is already mounted and then rsync all folders to the /nextcloud folder. In the end I umount the device! Now I can rsync my USB device without even connecting to the a bash remotely.

I derived that recipe from the following link and is now tested:
https://unix.stackexchange.com/question ... dev-script
https://yakking.branchable.com/posts/systemd-2-udevd/
rosswesleyporter
Posts: 18
Joined: Tue May 24, 2016 6:57 am

Re: USB Automount

Post by rosswesleyporter »

@peddanet : here is an updated link, see page 3. https://github.com/rosswesleyporter/dqm ... EATION.pdf
User avatar
MichaIng
Site Admin
Posts: 2295
Joined: Sat Nov 18, 2017 6:21 pm

Re: USB Automount

Post by MichaIng »

@rosswesleyporter
Many thanks for sharing. A systemd unit is btw automatically created when adding an fstab entry and running systemctl daemon-reload. My current idea about implementing automount to DietPi is to add a dietpi-drive_manager command that simply re-scans attached drives, re-creates fstab, does above reload and mounts the given input drive automatically to a reasonable mount point, optionally to the given mount point. Most code/functions for this is already there, hence should be quite simple. E.g. dietpi-drive_manager mount /dev/sda1 or with optional mount point argument: dietpi-drive_manager mount /dev/sda1 /mnt/myData
And: dietpi-drive_manager umount [/dev/sda1|/mnt/myData]

The udev rules would then be same, but the above used as RUN+=. The only thing that I am not sure how to handle yet:
- If the fstab entry already exist for a device, on boot it will be automounted already, hence the udev rule creates additional overhead or could even conflict with existing systemd mount/automount units.
- We want all attached drives to be mounted automatically, but actually only after boot schedule has finished and/or those are definitely not in the fstab already. Not sure how to add such a delay or requirement to the udev rule. Ugly workaround would be to move the rule in place at end of boot and remove it on shutdown or better move it to tmpfs and link it from there, so it is assured to be empty/skipped on boot.

Also it would be great if drive manager would remember the chosen mount point. We could leave the fstab entry in place with "noauto" and without x-systemd.automount. When re-creating fstab, we would need to scan for those entries first and re-add them, marked as unmounted or even unplugged. It must then be possible to actively remove those fstab entries. Currently unmounted drives are added commented with fixed mount point to /tmp/<UUID> and unplugged drives are removed from fstab when starting/refreshing drive manager, hence the chosen mount point is lost.



@rosswesleyporter
Two questions:
What happens on boot (journalctl) when you added this drive to fstab while having the udev rule in place? Do you see the mount being done two times, of the udev rule failing due to already mounted drive?

I am not 100% sure about the umount thing. If the device is removed/unplugged manually, BEFORE unmounting it, this is not failsafe against file corruption anyway. I guess umount would normally throw an error, umount -l seems to be possible and reasonable at least to tell the system that the mount has gone, but actually every drive should be umounted BEFORE removing/unplugging it.
rosswesleyporter
Posts: 18
Joined: Tue May 24, 2016 6:57 am

Re: USB Automount

Post by rosswesleyporter »

I approached the problem in a very pragmatic way, so unfortunately I do not have deep answers to your question (wish I did). My need is for a USB drive to automount at boot and for those files to be accessible on a known/predictable path. I found the solution in the usual pragmatic way (Google). My pragmatic answer is that the method described has worked very reliably for me and for the small number of users of my Dementia Friendly Music Player. However, that use is in a simple low-stress system, so it is unlikely to reveal subtle issues.

All this said, if Michael provides a better way, I'm happy to beta test and then switch.
User avatar
MichaIng
Site Admin
Posts: 2295
Joined: Sat Nov 18, 2017 6:21 pm

Re: USB Automount

Post by MichaIng »

@rosswesleyporter
If it only needs to mount at boot, then you don't need all the udev rules thing. Simply add an entry to /etc/fstab or start dietpi-drive_manager, select the drive and select mount.
NB: Non-root/boot file systems are added with noauto,x-systemd.automount options, hence they will not be mounted automatically on boot, but an automount watcher is placed which mounts the drive immediately once anything accesses the mount point. So practically it is like mounted.

All this udev thing is only a topic when its about auto-mounting hotplugged drives, e.g. plugging an USB stick. This would be a great feature but it must not collide/conflict with the on-boot automount since the udev trigger is called on boot as well.

For reference: https://github.com/MichaIng/DietPi/issues/3339
rosswesleyporter
Posts: 18
Joined: Tue May 24, 2016 6:57 am

Re: USB Automount

Post by rosswesleyporter »

Thanks Michael. I'm always looking for simpler / built-in methods. But if I understand your method correctly, it requires a one-time admin intervention per USB stick. I chose the udev route because it needs to work for someone that downloads the SD card image that I created who then uses their own Pi & USB stick in a headless and potentially networkless box.
User avatar
MichaIng
Site Admin
Posts: 2295
Joined: Sat Nov 18, 2017 6:21 pm

Re: USB Automount

Post by MichaIng »

Okay if there is no interaction (no SSH and no local console) possible, then indeed the generic udev-based automount is the only way. The content is then indeed script/program-based or what? Just thinking what for to mount a USB stick on a machine that is networkless and headless :).
rosswesleyporter
Posts: 18
Joined: Tue May 24, 2016 6:57 am

Re: USB Automount

Post by rosswesleyporter »

The USB stick has music. The project is The Dementia Friendly Music Player http://dementiamusicplayer.org/. Essentially DietPi + headless VLC + rotary encoders + Python + laser cut bamboo or cherry. DietPi has been consistently excellent for this.
User avatar
MichaIng
Site Admin
Posts: 2295
Joined: Sat Nov 18, 2017 6:21 pm

Re: USB Automount

Post by MichaIng »

Ah yes totally reasonable then. So regardless of which USB stick is attached, it must be mounted to where the music payer is looking for music. Indeed fstab is not suitable for this and the udev rule totally makes sense.

I'm thinking if we can add this somehow to drive manager as well. E.g. input a device definition or pattern and a fixed mount point. It could then skip these during drive loop to not add them to fstab...

Okay much more ideas then time currently, but I'll keep this on mind when doing some drive manager rework anyway.
Post Reply