Skip to contentSkip to conversation

libblkid: match uppercase UUIDs

PATCH In review Alyssa Ross 3 participants 4 messages Jun 14, 2021 – Jun 15, 2021 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libblkid: match uppercase UUIDs

In a script, I wanted to look up the device node for the booted EFI partition. systemd-boot exposes this in an EFI variable but it's uppercase, so when I tried to do this, it didn't work:

    findfs "(< /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440)"

This was very confusing to me, and I lost some time trying to figure out what was wrong before I realised that case of UUIDs was significant to findfs.

Here, I've made comparisons of UUID and PARTUUID case-insensitive in libblkid, which fixes the command above.

---
 libblkid/src/evaluate.c | 12 ++++++++++++
 libblkid/src/tag.c      | 22 +++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)
libblkid/src/evaluate.c +12 -0
index 710eac956..ceaa63cc7 100644
@@ -135,6 +135,15 @@ int blkid_send_uevent(const char *devname, const char *action)
 	return rc;
 }
 
+static void ascii_downcase(char *s)
+{
+	while (*s) {
+		if ('A' <= *s && *s <= 'Z')
+			*s += 'a' - 'A';
+		s++;
+	}
+}
+
 static char *evaluate_by_udev(const char *token, const char *value, int uevent)
 {
 	char dev[PATH_MAX];
@@ -163,6 +172,9 @@ static char *evaluate_by_udev(const char *token, const char *value, int uevent)
 	if (blkid_encode_string(value, &dev[len], sizeof(dev) - len) != 0)
 		return NULL;
 
+	if (!strcmp(token, "UUID") || !strcmp(token, "PARTUUID"))
+		ascii_downcase(dev);
+
 	DBG(EVALUATE, ul_debug("expected udev link: %s", dev));
 
 	if (stat(dev, &st))
libblkid/src/tag.c +19 -3
index 390a64864..ca81c6921 100644
@@ -10,10 +10,13 @@
  * %End-Header%
  */
 
-#include <unistd.h>
+#include <locale.h>
+#include <stdbool.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
+#include <strings.h>
+#include <unistd.h>
 
 #include "blkidP.h"
 
@@ -101,6 +104,19 @@ static blkid_tag blkid_find_head_cache(blkid_cache cache, const char *type)
 	return head;
 }
 
+static bool tag_value_matches(const char *type, const char *expected,
+			      const char *actual)
+{
+	if (!strcmp(type, "UUID") || !strcmp(type, "PARTUUID")) {
+		locale_t posix = newlocale(0, "POSIX", 0);
+		bool r = !strcasecmp_l(expected, actual, posix);
+		freelocale(posix);
+		return r;
+	}
+
+	return !strcmp(expected, actual);
+}
+
 /*
  * Set a tag on an existing device.
  *
@@ -345,7 +361,7 @@ try_again:
 			blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
 						   bit_names);
 
-			if (!strcmp(tmp->bit_val, value) &&
+			if (tag_value_matches(type, tmp->bit_val, value) &&
 			    (tmp->bit_dev->bid_pri > pri) &&
 			    !access(tmp->bit_dev->bid_name, F_OK)) {
 				dev = tmp->bit_dev;
Signature · 1 line
2.31.1
#1 lore raw
Quoted 5 lines · Alyssa Ross

In a script, I wanted to look up the device node for the booted EFI partition. systemd-boot exposes this in an EFI variable but it's uppercase, so when I tried to do this, it didn't work:

    findfs "(< /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440)"

Bogus command here, sorry. It should have been:

    findfs PARTUUID="$(cat /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f/data)"

But my point stands. :)

Quoted 8 lines

This was very confusing to me, and I lost some time trying to figure out what was wrong before I realised that case of UUIDs was significant to findfs.

Here, I've made comparisons of UUID and PARTUUID case-insensitive in libblkid, which fixes the command above.

#2 lore raw
Quoted 6 lines · Alyssa Ross
Quoted 4 lines · Alyssa Ross

In a script, I wanted to look up the device node for the booted EFI partition. systemd-boot exposes this in an EFI variable but it's uppercase, so when I tried to do this, it didn't work:

Quoted 7 lines
Quoted 1 line
    findfs "(< /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440)"

Bogus command here, sorry. It should have been:

    findfs PARTUUID="$(cat /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f/data)"

But my point stands. :)

probably could use a pipe after cat, like:

cat ... | tr [:lower:] [:upper:]

or patch util-linux docs, to implicitly state that comparison is done with upper case letters (UUID)... Not sure which way is better (including your patch).

Thanks.

maintainer
#3 lore raw
Quoted 15 lines · Anatoly Pugachev
Quoted 6 lines · Alyssa Ross
Quoted 4 lines · Alyssa Ross

In a script, I wanted to look up the device node for the booted EFI partition. systemd-boot exposes this in an EFI variable but it's uppercase, so when I tried to do this, it didn't work:

Quoted 7 lines
Quoted 1 line
    findfs "(< /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440)"

Bogus command here, sorry. It should have been:

    findfs PARTUUID="$(cat /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f/data)"

But my point stands. :)

Quoted 7 lines

probably could use a pipe after cat, like:

cat ... | tr [:lower:] [:upper:]

or patch util-linux docs, to implicitly state that comparison is done with upper case letters (UUID)... Not sure which way is better (including your patch).

There is no guaranty that the UUID will be the upper or lower case although in fstab.5 and mount.8 man pages we have "should be based on lower case characters".

libblkid itself does not modify UUID/PARTUUID when reading it from the filesystem. It depends on FS-specific prober in the library or in some cases on the filesystem superblock if UUID is stored as a string there.

And it's also important to support 3rd party UUID from another source as you can write whatever to your udev rules and use others tools to get UUIDs.

So, I think your request to compare UUIDs in a case-insensitive way makes sense. The library should be smart enough to accept whatever.

I'll think about it and try to improve the current situation.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com