0

I want to automatically assign a label to a partition when a disk is inserted. My approach to the task is creating a new udev rule which looks something like:

KERNEL=="sd[a-z][0-9]", KERNELS=="ata[12]", SUBSYSTEM=="block", ENV{ID_FS_TYPE}=="ext4", ENV{ID_FS_LABEL}=="", ACTION=="add", RUN+="/bin/sh -c 'sudo e2label %E{DEVNAME} TEST_LABEL'"

The rule checks that the filesystem is ext4, and that the current e2label is empty: "". When all the criterias in the udev rule is met, the command sudo e2label %E{DEVNAME} TEST_LABEL is executed.

This works. Kind of? When I insert a disk, and check the assigned label with e2label, I see that it has gotten TEST_LABEL assigned. However, when I check the label with lsblk -f, the LABEL section is empty. And my other udev rules that check for the label TEST_LABEL don't trigger either, even though they have lower priority. If I then remove the disk and insert it again, the label becomes visible when I run lsblk -f, and the other udev rules now trigger.

Why is this? What am I doing wrong and/or what do I not understand?

2
  • 1
    Note, you don't need the sudo as the command will be run as root anyway, and you don't need the sh -c since there are no shell metacharacters to parse, but you may then need to use the full path to e2label.
    – meuh
    Sep 13, 2022 at 11:35
  • Thank you for the information! I really do appreciate it.
    – seglaren
    Sep 13, 2022 at 13:06

1 Answer 1

2

udev doesn't read the label for every operation/rule, it is read once in 60-persistent-storage.rules using blkid and then never updated unless the device is changed. So rules that execute after you've changed the label still use the (now outdated) value obtained in 60-persistent-storage.rules. This is also the reason why lsblk doesn't see the new label -- lsblk uses the udev database for these information, it doesn't probe the device (you'd have to use blkid -p for that). So you either need to re-probe the device in your rule or just set ID_FS_LABEL and ID_FS_LABEL_ENC to your new label.

Btw. normally when you run e2fslabel it produces a udev change event so udev will rescan the device -- udev uses inotify to watch for metadata changes on devices, but this doesn't happen if you change the device from the rule (otherwise you could cause an infinite loop of change event producing a new change event every time).

2
  • Thank you for your thorough explanation! I now understand the problem that I described, much better. However I would love to learn how I can re-probe the device with my rule. I've tried searching about it, to no avail. And I also don't quite understand what you mean by populating ID_FS_LABEL and ID_FS_LABEL_ENC with my new label. How would I achieve this? I thought that I was already doing it in the rule with "e2label %E{DEVNAME} TEST_LABEL". Again, thank you for helping out. I really do appreciate it!
    – seglaren
    Sep 13, 2022 at 13:12
  • I figured out how to re-probe the device. I just ran "blockdev --rereadpt /dev/sda" in my udev rule, after setting the label.
    – seglaren
    Sep 15, 2022 at 13:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .