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

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