Ben Hutchings
2023-Jul-23 21:03 UTC
[klibc] [PATCH] kinit: Support specifying root with PARTLABEL
On Thu, 2023-07-20 at 14:08 +0200, Rob Vandermeulen wrote:> 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.It's unfortunate that this doesn't handle PARTUUID here as well. But I realise that's not yet possible because no-one thought to expose that information to user-space when adding it to the kernel... I spotted one bug, commented below:> Signed-off-by: Rob Vandermeulen <rvandermeulen at google.com> > --- > usr/kinit/name_to_dev.c | 71 +++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 71 insertions(+) > > diff --git a/usr/kinit/name_to_dev.c b/usr/kinit/name_to_dev.c > index d8c17363..76cc4f81 100644 > --- a/usr/kinit/name_to_dev.c > +++ b/usr/kinit/name_to_dev.c > @@ -1,4 +1,6 @@ > #include <ctype.h> > +#include <dirent.h> > +#include <errno.h> > #include <fcntl.h> > #include <sys/stat.h> > #include <stdio.h> > @@ -73,6 +75,71 @@ 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 = 0; > + int major = 0; > + int minor = 0;[...] These 3 variables need to be reinitialised for each file, not just once. Otherwise we could (in principle) match on a combination of information from different files. Ben. -- Ben Hutchings If you seem to know what you are doing, you'll be given more to do. -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: This is a digitally signed message part URL: <https://lists.zytor.com/archives/klibc/attachments/20230723/1a51b3e7/attachment.sig>
Rob Vandermeulen
2023-Jul-24 09:41 UTC
[klibc] [PATCH v2] 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 <rvandermeulen at google.com> --- Changelog since original patch: - Initialize major, minor & match_label for each file read. 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 d8c17363..c57b7cec 100644 --- a/usr/kinit/name_to_dev.c +++ b/usr/kinit/name_to_dev.c @@ -1,4 +1,6 @@ #include <ctype.h> +#include <dirent.h> +#include <errno.h> #include <fcntl.h> #include <sys/stat.h> #include <stdio.h> @@ -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/<disk_name>p<decimal> - 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=<name> 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 { -- 2.41.0.487.g6d72f3e995-goog