[klibc] [PATCH v2] kinit: Support specifying root with PARTLABEL

Rob Vandermeulen rvandermeulen at google.com
Mon Jul 24 02:41:12 PDT 2023


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



More information about the klibc mailing list