From 78106790f9de2c297dbcedea7ff39ce4192ec1b2 Mon Sep 17 00:00:00 2001 From: Rob Vandermeulen Date: Mon, 24 Jul 2023 11:41:12 +0200 Subject: [klibc] kinit: Support specifying root with PARTLABEL Allow specifying the root device to be mounted as root=PARTLABEL=label. The label is the GPT partition label of the intended root partition, as presented by the kernel with the uevent entry PARTNAME. Signed-off-by: Rob Vandermeulen Signed-off-by: Ben Hutchings --- usr/kinit/name_to_dev.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/usr/kinit/name_to_dev.c b/usr/kinit/name_to_dev.c index d8c17363974ef..c57b7cec6ded5 100644 --- a/usr/kinit/name_to_dev.c +++ b/usr/kinit/name_to_dev.c @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -73,6 +75,72 @@ fail: return (dev_t) 0; } +/* + * Find dev_t for a block device based on the provided GPT partlabel. + * The partlabel to block device mapping is found by scanning all + * the entries in /sys/dev/block/, opening the uevent file and picking + * the device where the PARTNAME= entry matches partlabel. + */ +static dev_t partlabel_to_dev_t(const char *plabel) +{ + char path[BUF_SZ]; + DIR *dir; + FILE *fp; + struct dirent *dent; + char *ret; + char line[BUF_SZ]; + int match_label, major, minor; + + dir = opendir("/sys/dev/block"); + if (!dir) { + dprintf(stderr, "%s: error %i (%s) opening /sys/dev/block\n", + __func__, errno, strerror(errno)); + goto fail; + } + + while ((dent = readdir(dir)) != NULL) { + if (!strncmp(dent->d_name, ".", 1)) + continue; + snprintf(path, sizeof(path), "/sys/dev/block/%s/uevent", + dent->d_name); + + fp = fopen(path, "r"); + if (fp == NULL) { + dprintf(stderr, "kinit %s: error %i (%s) opening %s", + __func__, errno, strerror(errno), path); + continue; + } + + major = 0; + minor = 0; + match_label = 0; + while (!feof(fp)) { + ret = fgets(line, sizeof(line), fp); + if (ret == NULL) + continue; + if (!strncmp(line, "MAJOR=", 6)) + major = atoi(line+6); + if (!strncmp(line, "MINOR=", 6)) + minor = atoi(line+6); + if (!strncmp(line, "PARTNAME=", 9)) { + line[strcspn(line, "\n")] = 0; + if (!strncmp(line + 9, plabel, sizeof(line)-9)) + match_label = 1; + } + if (match_label && major && minor) { + fclose(fp); + closedir(dir); + return makedev(major, minor); + } + } + fclose(fp); + } + closedir(dir); + +fail: + return (dev_t) 0; +} + /* * Convert a name into device number. We accept the following variants: * @@ -85,6 +153,7 @@ fail: * 6) /dev/p - same as the above, that form is * used when disk name of partitioned disk ends on a digit. * 7) an actual block device node in the initramfs filesystem + * 8) PARTLABEL= with name being the GPT partition label. * * If name doesn't have fall into the categories above, we return 0. * Driverfs is used to check if something is a disk name - it has @@ -110,6 +179,9 @@ static inline dev_t name_to_dev_t_real(const char *name) if (strchr(name, ',')) return Root_MULTI; + if (!strncmp(name, "PARTLABEL=", 10)) + return partlabel_to_dev_t(name + 10); + if (name[0] == '/') { devname = name; } else { -- cgit 1.2.3-korg