Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DietPi-Drive_Manager | Hotplug mount+fstab using udev rules #3339

Open
chips023 opened this issue Jan 13, 2020 · 9 comments
Open

DietPi-Drive_Manager | Hotplug mount+fstab using udev rules #3339

chips023 opened this issue Jan 13, 2020 · 9 comments

Comments

@chips023
Copy link

chips023 commented Jan 13, 2020

echo 'ACTION=="add", KERNEL=="sd[a-z]", RUN+="/DietPi/dietpi/dietpi-drive_manager 3"' > /etc/udev/rules.d/99-dietpi-auto_mount.rules
udevadm control --reload-rules or reboot

@MichaIng
Copy link
Owner

@hifitime
As mentioned in the other issue, this feature has not yet been implemented. Drive manager exits with failure when executing with argument 3. But what could actually work is:

G_DEBUG=1 /DietPi/dietpi/dietpi-drive_manager

This runs the drive detection loop but then prints information about all detected drives only and exits. But the fstab creation/update is done within the loop, AFAIK.

@chips023
Copy link
Author

chips023 commented Jan 13, 2020

OK, I get it..

in addition,I added rules to /etc/udev/rules.d/, which also didn't work...It looks like the rules are right..

nano /etc/udev/rules.d/media_by_label_auto_mount

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
IMPORT{program}="/sbin/blkid -o udev -p %N"
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"
ACTION=="add", ENV{mount_options}="relatime"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"
LABEL="media_by_label_auto_mount_end"

@MichaIng
Copy link
Owner

@hifitime
Looks a bid complicated but should work. Did you run udevadm control -R or rebooted to enable the new rules?

@chips023
Copy link
Author

udevadm control --reload-rules or reboot
Have tried, but still can't run, can't see any mount..

@MichaIng
Copy link
Owner

@hifitime
Does dmesg or journalctl -r show some related logs when you plug and/or unplug a drive?
In case increase the udev log level.

udevadm control --log-priority=info

Or to actively print to console

udevadm monitor

Ahh there is even a test command to simulate a certain event and print debug log:

udevadm test -a add /dev/sda1

@Joulinar
Copy link
Collaborator

@hifitime
glad that the problem could be solved. Would it be possible to share the steps you have taken to get it solved? It might be other are interested in it as well 😉

@chips023
Copy link
Author

I'm so happy too early... The problem is still there...

root@DietPi:~# udevadm test -a add /dev/sda1
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

Load module index
Skipping empty file: /etc/systemd/network/99-default.link
Created link configuration context.
Reading rules file: /etc/udev/rules.d/10-wifi-disable-powermanagement.rules
Reading rules file: /etc/udev/rules.d/11-automount.rules
Reading rules file: /lib/udev/rules.d/50-firmware.rules
Reading rules file: /lib/udev/rules.d/50-udev-default.rules
Reading rules file: /lib/udev/rules.d/55-dm.rules
Reading rules file: /lib/udev/rules.d/60-block.rules
Reading rules file: /lib/udev/rules.d/60-cdrom_id.rules
Reading rules file: /lib/udev/rules.d/60-drm.rules
Reading rules file: /lib/udev/rules.d/60-evdev.rules
Reading rules file: /lib/udev/rules.d/60-input-id.rules
Reading rules file: /lib/udev/rules.d/60-persistent-alsa.rules
Reading rules file: /lib/udev/rules.d/60-persistent-input.rules
Reading rules file: /lib/udev/rules.d/60-persistent-storage-dm.rules
Reading rules file: /lib/udev/rules.d/60-persistent-storage-tape.rules
Reading rules file: /lib/udev/rules.d/60-persistent-storage.rules
Reading rules file: /lib/udev/rules.d/60-persistent-v4l.rules
Reading rules file: /lib/udev/rules.d/60-sensor.rules
Reading rules file: /lib/udev/rules.d/60-serial.rules
Reading rules file: /lib/udev/rules.d/60-sunxi-tools.rules
Reading rules file: /lib/udev/rules.d/64-btrfs.rules
Reading rules file: /lib/udev/rules.d/70-joystick.rules
Reading rules file: /lib/udev/rules.d/70-mouse.rules
Reading rules file: /lib/udev/rules.d/70-power-switch.rules
Reading rules file: /lib/udev/rules.d/70-touchpad.rules
Reading rules file: /lib/udev/rules.d/70-uaccess.rules
Reading rules file: /lib/udev/rules.d/71-axp-power-button.rules
Reading rules file: /lib/udev/rules.d/71-seat.rules
Reading rules file: /lib/udev/rules.d/73-seat-late.rules
Reading rules file: /lib/udev/rules.d/73-special-net-names.rules
Reading rules file: /lib/udev/rules.d/73-usb-net-by-mac.rules
Reading rules file: /lib/udev/rules.d/75-net-description.rules
Reading rules file: /lib/udev/rules.d/75-probe_mtd.rules
Reading rules file: /lib/udev/rules.d/78-sound-card.rules
Reading rules file: /lib/udev/rules.d/80-debian-compat.rules
Reading rules file: /lib/udev/rules.d/80-drivers.rules
Reading rules file: /lib/udev/rules.d/80-ifupdown.rules
Reading rules file: /lib/udev/rules.d/80-net-setup-link.rules
Reading rules file: /lib/udev/rules.d/85-hdparm.rules
Reading rules file: /lib/udev/rules.d/85-hwclock.rules
Reading rules file: /lib/udev/rules.d/89-alsa-ucm.rules
Reading rules file: /lib/udev/rules.d/90-alsa-restore.rules
Reading rules file: /lib/udev/rules.d/90-console-setup.rules
Reading rules file: /lib/udev/rules.d/95-dm-notify.rules
Reading rules file: /lib/udev/rules.d/99-systemd.rules
Rules contain 24576 bytes tokens (2048 * 12 bytes), 13443 bytes strings
1848 strings (23487 bytes), 1210 de-duplicated (10683 bytes), 639 trie nodes used
Failed to open device '/sys/dev/sda1': No such device
Unload module index
Unloaded link configuration context.
root@DietPi:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 1 7.2G 0 disk
└─sda1 8:1 1 7.2G 0 part
mtdblock0 31:0 0 1.5M 0 disk
mtdblock1 31:1 0 64K 0 disk
mtdblock2 31:2 0 2.4M 0 disk
mmcblk0 179:0 0 3.7G 0 disk
└─mmcblk0p1 179:1 0 3.7G 0 part
mmcblk0boot0 179:8 0 2M 1 disk
mmcblk0boot1 179:16 0 2M 1 disk
mmcblk1 179:24 0 29.7G 0 disk
└─mmcblk1p1 179:25 0 3G 0 part /

@chips023 chips023 reopened this Jan 14, 2020
@MichaIng
Copy link
Owner

@hifitime
The output shows that there is at least no syntax issue with the udev rule. But the path was not given correctly, so no rules are applied. Please try:

udevadm test -a add $(udevadm info -q path -n /dev/sda1)

@MichaIng
Copy link
Owner

MichaIng commented Feb 24, 2020

@hifitime
Btw one main issue we'd need to think about is conflict between the udev rule and fstab entries on boot:

  • If the drive is plugged on boot, ACTION=="add" will happen and mount the drive/execute drive manager, regardless of already existing fstab entry.
  • Also for most external drives, this mount attempt will fail since the system is not yet ready for mounts when udev trigger happens (very early boot stage).
  • So basically we must enable the udev rule AFTER boot has fully finished and disable it again on shutdown.
  • Since we can never assume a successful shutdown, easiest is to place a symbolic link to a tmpfs like /tmp/dietpi-automount.rules and copy the actual rules file there as last boot step. So whenever the system looses power, on next boot the in-place udev rule will be an empty symlink once boot has finished.

For reference: https://dietpi.com/phpbb/viewtopic.php?t=5106

Would be also interesting how other hotplug mount implementations handle this. Actually "hotplug" is the better wording here to not mix up with regular "automount" on boot, like mixed up in above linked forum thread.

@MichaIng MichaIng changed the title Auto mount+fstab using udev rules DietPi-Drive_Manager | Hotplug mount+fstab using udev rules Feb 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants