Blob Blame History Raw
From dad65a1811412fb6b5e9e7211f8cf9d397732255 Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Mon, 15 Sep 2014 15:18:48 -0400
Subject: [PATCH 14/16] Support filtering --update-kernel= by title as well.

If there are two entries with the same kernel, we need to match the
title to uniquely identify one.

Related: rhbz#957681
Signed-off-by: Peter Jones <pjones@redhat.com>
---
 grubby.c | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/grubby.c b/grubby.c
index c82ab00..24c6df0 100644
--- a/grubby.c
+++ b/grubby.c
@@ -3282,7 +3282,8 @@ int updateImage(struct grubConfig * cfg, const char * image,
 }
 
 int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
-		 const char * image, const char * prefix, const char * initrd) {
+		 const char * image, const char * prefix, const char * initrd,
+		 const char * title) {
     struct singleEntry * entry;
     struct singleLine * line, * kernelLine, *endLine = NULL;
     int index = 0;
@@ -3293,6 +3294,20 @@ int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
         kernelLine = getLineByType(LT_MBMODULE, entry->lines);
         if (!kernelLine) continue;
 
+	/* if title is supplied, the entry's title must match it. */
+	if (title) {
+	    line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
+	    char *linetitle = extractTitle(line);
+
+	    if (!linetitle)
+		continue;
+	    if (strcmp(title, linetitle)) {
+		free(linetitle);
+		continue;
+	    }
+	    free(linetitle);
+	}
+
         if (prefix) {
             int prefixLen = strlen(prefix);
             if (!strncmp(initrd, prefix, prefixLen))
@@ -3318,7 +3333,7 @@ int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
 }
 
 int updateInitrd(struct grubConfig * cfg, const char * image,
-                 const char * prefix, const char * initrd) {
+                 const char * prefix, const char * initrd, const char * title) {
     struct singleEntry * entry;
     struct singleLine * line, * kernelLine, *endLine = NULL;
     int index = 0;
@@ -3329,6 +3344,20 @@ int updateInitrd(struct grubConfig * cfg, const char * image,
         kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
         if (!kernelLine) continue;
 
+	/* if title is supplied, the entry's title must match it. */
+	if (title) {
+	    line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
+	    char *linetitle = extractTitle(line);
+
+	    if (!linetitle)
+		continue;
+	    if (strcmp(title, linetitle)) {
+		free(linetitle);
+		continue;
+	    }
+	    free(linetitle);
+	}
+
         line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
         if (line)
             removeLine(entry, line);
@@ -4335,7 +4364,7 @@ int main(int argc, const char ** argv) {
     if (newKernelPath && !newKernelTitle) {
 	fprintf(stderr, _("grubby: kernel title must be specified\n"));
 	return 1;
-    } else if (!newKernelPath && (newKernelTitle  || copyDefault ||
+    } else if (!newKernelPath && (copyDefault ||
 				  (newKernelInitrd && !updateKernelPath)||
 				  makeDefault || extraInitrdCount > 0)) {
 	fprintf(stderr, _("grubby: kernel path expected\n"));
@@ -4557,11 +4586,12 @@ int main(int argc, const char ** argv) {
     if (updateKernelPath && newKernelInitrd) {
 	    if (newMBKernel) {
 		    if (addMBInitrd(config, newMBKernel, updateKernelPath,
-					bootPrefix, newKernelInitrd))
+					bootPrefix, newKernelInitrd,
+					newKernelTitle))
 			    return 1;
 	    } else {
 		    if (updateInitrd(config, updateKernelPath, bootPrefix,
-					newKernelInitrd))
+					newKernelInitrd, newKernelTitle))
 			return 1;
 	    }
     }
-- 
1.9.3