Blame SOURCES/0050-Add-btrfs-subvolume-support-for-grub2.patch

24fce8
From 9c6e5cd813d0c064e2805cdb4c6726d32b49d3e1 Mon Sep 17 00:00:00 2001
24fce8
From: Nathaniel McCallum <npmccallum@redhat.com>
24fce8
Date: Fri, 2 Mar 2018 08:40:18 -0500
24fce8
Subject: [PATCH 50/55] Add btrfs subvolume support for grub2
24fce8
24fce8
In order to find the subvolume prefix from a given path, we parse
24fce8
/proc/mounts. In cases where /proc/mounts doesn't contain the
24fce8
filesystem, the caller can use the --mounts option to specify his own
24fce8
mounts file.
24fce8
24fce8
Btrfs subvolumes are already supported by grub2 and by grub2-mkconfig.
24fce8
24fce8
Fixes #22
24fce8
---
24fce8
 grubby.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
24fce8
 1 file changed, 142 insertions(+), 4 deletions(-)
24fce8
24fce8
diff --git a/grubby.c b/grubby.c
24fce8
index 1fe850a3ddf..396041a1187 100644
24fce8
--- a/grubby.c
24fce8
+++ b/grubby.c
24fce8
@@ -68,6 +68,8 @@ int isEfi = 0;
24fce8
 
24fce8
 char *saved_command_line = NULL;
24fce8
 
24fce8
+const char *mounts = "/proc/mounts";
24fce8
+
24fce8
 /* comments get lumped in with indention */
24fce8
 struct lineElement {
24fce8
 	char *item;
24fce8
@@ -2115,6 +2117,129 @@ static int endswith(const char *s, char c)
24fce8
 	return s[slen] == c;
24fce8
 }
24fce8
 
24fce8
+typedef struct {
24fce8
+	const char *start;
24fce8
+	size_t      chars;
24fce8
+} field;
24fce8
+
24fce8
+static int iscomma(int c)
24fce8
+{
24fce8
+	return c == ',';
24fce8
+}
24fce8
+
24fce8
+static int isequal(int c)
24fce8
+{
24fce8
+	return c == '=';
24fce8
+}
24fce8
+
24fce8
+static field findField(const field *in, typeof(isspace) *isdelim, field *out)
24fce8
+{
24fce8
+	field nxt = {};
24fce8
+	size_t off = 0;
24fce8
+
24fce8
+	while (off < in->chars && isdelim(in->start[off]))
24fce8
+		off++;
24fce8
+
24fce8
+	if (off == in->chars)
24fce8
+		return nxt;
24fce8
+
24fce8
+	out->start = &in->start[off];
24fce8
+	out->chars = 0;
24fce8
+
24fce8
+	while (off + out->chars < in->chars && !isdelim(out->start[out->chars]))
24fce8
+		out->chars++;
24fce8
+
24fce8
+	nxt.start = out->start + out->chars;
24fce8
+	nxt.chars = in->chars - off - out->chars;
24fce8
+	return nxt;
24fce8
+}
24fce8
+
24fce8
+static int fieldEquals(const field *in, const char *str)
24fce8
+{
24fce8
+	return in->chars == strlen(str) &&
24fce8
+		strncmp(in->start, str, in->chars) == 0;
24fce8
+}
24fce8
+
24fce8
+/* Parse /proc/mounts to determine the subvolume prefix. */
24fce8
+static size_t subvolPrefix(const char *str)
24fce8
+{
24fce8
+	FILE *file = NULL;
24fce8
+	char *line = NULL;
24fce8
+	size_t prfx = 0;
24fce8
+	size_t size = 0;
24fce8
+
24fce8
+	file = fopen(mounts, "r");
24fce8
+	if (!file)
24fce8
+		return 0;
24fce8
+
24fce8
+	for (ssize_t s; (s = getline(&line, &size, file)) >= 0; ) {
24fce8
+		field nxt = { line, s };
24fce8
+		field dev = {};
24fce8
+		field path = {};
24fce8
+		field type = {};
24fce8
+		field opts = {};
24fce8
+		field opt = {};
24fce8
+
24fce8
+		nxt = findField(&nxt, isspace, &dev;;
24fce8
+		if (!nxt.start)
24fce8
+			continue;
24fce8
+
24fce8
+		nxt = findField(&nxt, isspace, &path);
24fce8
+		if (!nxt.start)
24fce8
+			continue;
24fce8
+
24fce8
+		nxt = findField(&nxt, isspace, &type);
24fce8
+		if (!nxt.start)
24fce8
+			continue;
24fce8
+
24fce8
+		nxt = findField(&nxt, isspace, &opts);
24fce8
+		if (!nxt.start)
24fce8
+			continue;
24fce8
+
24fce8
+		if (!fieldEquals(&type, "btrfs"))
24fce8
+			continue;
24fce8
+
24fce8
+		/* We have found a btrfs mount point. */
24fce8
+
24fce8
+		nxt = opts;
24fce8
+		while ((nxt = findField(&nxt, iscomma, &opt)).start) {
24fce8
+			field key = {};
24fce8
+			field val = {};
24fce8
+
24fce8
+			opt = findField(&opt, isequal, &key);
24fce8
+			if (!opt.start)
24fce8
+				continue;
24fce8
+
24fce8
+			opt = findField(&opt, isequal, &val;;
24fce8
+			if (!opt.start)
24fce8
+				continue;
24fce8
+
24fce8
+			if (!fieldEquals(&key, "subvol"))
24fce8
+				continue;
24fce8
+
24fce8
+			/* We have found a btrfs subvolume mount point. */
24fce8
+
24fce8
+			if (strncmp(val.start, str, val.chars))
24fce8
+				continue;
24fce8
+
24fce8
+			if (val.start[val.chars - 1] != '/' &&
24fce8
+				str[val.chars] != '/')
24fce8
+				continue;
24fce8
+
24fce8
+			/* The subvolume mount point matches our input. */
24fce8
+
24fce8
+			if (prfx < val.chars)
24fce8
+				prfx = val.chars;
24fce8
+		}
24fce8
+	}
24fce8
+
24fce8
+	dbgPrintf("%s(): str: '%s', prfx: '%s'\n", __FUNCTION__, str, prfx);
24fce8
+
24fce8
+	fclose(file);
24fce8
+	free(line);
24fce8
+	return prfx;
24fce8
+}
24fce8
+
24fce8
 int suitableImage(struct singleEntry *entry, const char *bootPrefix,
24fce8
 		  int skipRemoved, int flags)
24fce8
 {
24fce8
@@ -3262,12 +3387,22 @@ struct singleLine *addLineTmpl(struct singleEntry *entry,
24fce8
 		    type & (LT_HYPER | LT_KERNEL | LT_MBMODULE | LT_INITRD |
24fce8
 			    LT_KERNEL_EFI | LT_INITRD_EFI | LT_KERNEL_16 |
24fce8
 			    LT_INITRD_16)) {
24fce8
-			size_t rs = getRootSpecifier(tmplLine->elements[1].item);
24fce8
+			const char *prfx = tmplLine->elements[1].item;
24fce8
+			size_t rs = getRootSpecifier(prfx);
24fce8
+			if (isinitrd(tmplLine->type)) {
24fce8
+				for (struct singleLine *l = entry->lines;
24fce8
+				     rs == 0 && l; l = l->next) {
24fce8
+					if (iskernel(l->type)) {
24fce8
+						prfx = l->elements[1].item;
24fce8
+						rs = getRootSpecifier(prfx);
24fce8
+						break;
24fce8
+					}
24fce8
+				}
24fce8
+			}
24fce8
 			if (rs > 0) {
24fce8
 				free(newLine->elements[1].item);
24fce8
 				newLine->elements[1].item = sdupprintf(
24fce8
-					"%.*s%s", (int) rs,
24fce8
-					tmplLine->elements[1].item, val);
24fce8
+					"%.*s%s", (int) rs, prfx, val);
24fce8
 			}
24fce8
 		}
24fce8
 	}
24fce8
@@ -4331,7 +4466,7 @@ static size_t getRootSpecifier(const char *str)
24fce8
 		rs++;
24fce8
 	}
24fce8
 
24fce8
-	return rs;
24fce8
+	return rs + subvolPrefix(str + rs);
24fce8
 }
24fce8
 
24fce8
 static char *getInitrdVal(struct grubConfig *config,
24fce8
@@ -4963,6 +5098,9 @@ int main(int argc, const char **argv)
24fce8
 		{"mbargs", 0, POPT_ARG_STRING, &newMBKernelArgs, 0,
24fce8
 		 _("default arguments for the new multiboot kernel or "
24fce8
 		   "new arguments for multiboot kernel being updated"), NULL},
24fce8
+		{"mounts", 0, POPT_ARG_STRING, &mounts, 0,
24fce8
+		 _("path to fake /proc/mounts file (for testing only)"),
24fce8
+		 _("mounts")},
24fce8
 		{"bad-image-okay", 0, 0, &badImageOkay, 0,
24fce8
 		 _
24fce8
 		 ("don't sanity check images in boot entries (for testing only)"),
24fce8
-- 
24fce8
2.17.1
24fce8