I use Ubuntu 20.04 LTS and have wrote a custom udev rule in /etc/udev/rules.d/, which creates a symlink, called "mybackup" and wrotes a short message to syslog/journalctl, if a specific external USB hard drive is connected. My udev rule with the name 99-datensicherung.rules:
ENV{DEVTYPE}=="usb_device", \
SUBSYSTEM=="usb", \
ATTRS{idVendor}=="174c", \
ATTRS{idProduct}=="55aa", \
ATTRS{bcdDevice}=="0100", \
SYMLINK+="mybackup", \
ACTION=="add", \
RUN+="/usr/bin/logger MY DEVICE CONNECTED."
If I look into the syslog, I found the message of logger and see, that the symlink is created:
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: /etc/udev/rules.d/99-datensicherung.rules:9 LINK 'mybackup'
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: /etc/udev/rules.d/99-datensicherung.rules:9 RUN '/usr/bin/logger MY DEVICE CONNECTED.'
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: Handling device node '/dev/bus/usb/002/055', devnum=c189:182
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: Setting permissions /dev/bus/usb/002/055, uid=0, gid=0, mode=0664
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: Creating symlink '/dev/char/189:182' to '../bus/usb/002/055'
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: Creating symlink '/dev/mybackup' to 'bus/usb/002/055'
But a very short time later, the syslog tell me:
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: /usr/lib/udev/rules.d/69-libmtp.rules:2685 Running PROGRAM 'mtp-probe /sys/devices/pci0000:00/0000:00:14.0/usb2/2-4 2 55'
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: Starting 'mtp-probe /sys/devices/pci0000:00/0000:00:14.0/usb2/2-4 2 55'
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: Successfully forked off '(spawn)' as PID 185132.
Okt 15 11:22:46 SWPITSLNB mtp-probe[185132]: checking bus 2, device 55: "/sys/devices/pci0000:00/0000:00:14.0/usb2/2-4"
Okt 15 11:22:46 SWPITSLNB mtp-probe[185132]: bus: 2, device: 55 was not an MTP device
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: 'mtp-probe /sys/devices/pci0000:00/0000:00:14.0/usb2/2-4 2 55'(out) '0'
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: Process 'mtp-probe /sys/devices/pci0000:00/0000:00:14.0/usb2/2-4 2 55' succeeded.
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: Updating old name, '/dev/mybackup' no longer belonging to '/devices/pci0000:00/0000:00:14.0/usb2/2-4'
Okt 15 11:22:46 SWPITSLNB systemd-udevd[185107]: 2-4: No reference left, removing '/dev/mybackup'
How can I prevent the deletion of my symlink, from the other udev rule? Thank you very much!
Full syslog: https://pastebin.com/k5q1uuB0
ENV{DEVTYPE}=="usb_device"
is the "low-level" device that deals with raw USB packets, on top of which the OS will layer a usb_interface, then an scsi_host, then probably something like SCSI target and LUN, and only then you getsda
as an abstract "block device" which can be mounted or partitioned. (And which, by the way, already has quite a few such symlinks predefined under/dev/disk/by-xxx
for exactly the reason that you mention.)