Blame SOURCES/0020-grubby-add-set-index-to-specify-which-position-to-ad.patch

24fce8
From 8efba98644f9262d74a42fac7ea39d197192443a Mon Sep 17 00:00:00 2001
24fce8
From: Peter Jones <pjones@redhat.com>
24fce8
Date: Wed, 22 Jun 2016 14:07:49 -0400
24fce8
Subject: [PATCH 20/55] grubby: add --set-index to specify which position to
24fce8
 add new entries as
24fce8
24fce8
This adds an option, "--set-index N", to grubby, and will cause creation
24fce8
of any new entry to be at a particular zero-indexed position in the
24fce8
resulting configuration file.
24fce8
24fce8
Related: rhbz#1285601
24fce8
24fce8
Signed-off-by: Peter Jones <pjones@redhat.com>
24fce8
---
24fce8
 grubby.c               | 26 +++++++++++++++++++++-----
24fce8
 .gitignore             |  1 +
24fce8
 grubby.8               |  8 ++++++--
24fce8
 test.sh                | 14 ++++++++++++++
24fce8
 test/grub.15           | 19 +++++++++++++++++++
24fce8
 test/results/add/g1.10 | 22 ++++++++++++++++++++++
24fce8
 test/results/add/g1.17 | 19 +++++++++++++++++++
24fce8
 test/results/add/g1.8  | 22 ++++++++++++++++++++++
24fce8
 test/results/add/g1.9  | 22 ++++++++++++++++++++++
24fce8
 9 files changed, 146 insertions(+), 7 deletions(-)
24fce8
 create mode 100644 test/grub.15
24fce8
 create mode 100644 test/results/add/g1.10
24fce8
 create mode 100644 test/results/add/g1.17
24fce8
 create mode 100644 test/results/add/g1.8
24fce8
 create mode 100644 test/results/add/g1.9
24fce8
24fce8
diff --git a/grubby.c b/grubby.c
24fce8
index d66c1c5a40a..54625e7da0a 100644
24fce8
--- a/grubby.c
24fce8
+++ b/grubby.c
24fce8
@@ -4207,9 +4207,9 @@ int addNewKernel(struct grubConfig *config, struct singleEntry *template,
24fce8
 		 const char *newKernelArgs, const char *newKernelInitrd,
24fce8
 		 const char **extraInitrds, int extraInitrdCount,
24fce8
 		 const char *newMBKernel, const char *newMBKernelArgs,
24fce8
-		 const char *newDevTreePath)
24fce8
+		 const char *newDevTreePath, int newIndex)
24fce8
 {
24fce8
-	struct singleEntry *new;
24fce8
+	struct singleEntry *new, *entry, *prev = NULL;
24fce8
 	struct singleLine *newLine = NULL, *tmplLine = NULL, *masterLine = NULL;
24fce8
 	int needs;
24fce8
 	char *chptr;
24fce8
@@ -4239,9 +4239,20 @@ int addNewKernel(struct grubConfig *config, struct singleEntry *template,
24fce8
 	new = malloc(sizeof(*new));
24fce8
 	new->skip = 0;
24fce8
 	new->multiboot = 0;
24fce8
-	new->next = config->entries;
24fce8
 	new->lines = NULL;
24fce8
-	config->entries = new;
24fce8
+	entry = config->entries;
24fce8
+	for (unsigned int i = 0; i < newIndex; i++) {
24fce8
+		if (!entry)
24fce8
+			break;
24fce8
+		prev = entry;
24fce8
+		entry = entry->next;
24fce8
+	}
24fce8
+	new->next = entry;
24fce8
+
24fce8
+	if (prev)
24fce8
+		prev->next = new;
24fce8
+	else
24fce8
+		config->entries = new;
24fce8
 
24fce8
 	/* copy/update from the template */
24fce8
 	needs = NEED_KERNEL | NEED_TITLE;
24fce8
@@ -4734,6 +4745,7 @@ int main(int argc, const char **argv)
24fce8
 	char *newDevTreePath = NULL;
24fce8
 	char *newMBKernel = NULL;
24fce8
 	char *newMBKernelArgs = NULL;
24fce8
+	int newIndex = 0;
24fce8
 	char *removeMBKernelArgs = NULL;
24fce8
 	char *removeMBKernel = NULL;
24fce8
 	char *bootPrefix = NULL;
24fce8
@@ -4840,6 +4852,9 @@ int main(int argc, const char **argv)
24fce8
 		{"set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
24fce8
 		 _("make the given entry index the default entry"),
24fce8
 		 _("entry-index")},
24fce8
+		{"set-index", 0, POPT_ARG_INT, &newIndex, 0,
24fce8
+		 _("use the given index when creating a new entry"),
24fce8
+		 _("entry-index")},
24fce8
 		{"silo", 0, POPT_ARG_NONE, &configureSilo, 0,
24fce8
 		 _("configure silo bootloader")},
24fce8
 		{"title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
24fce8
@@ -5255,7 +5270,8 @@ int main(int argc, const char **argv)
24fce8
 	if (addNewKernel(config, template, bootPrefix, newKernelPath,
24fce8
 			 newKernelTitle, newKernelArgs, newKernelInitrd,
24fce8
 			 (const char **)extraInitrds, extraInitrdCount,
24fce8
-			 newMBKernel, newMBKernelArgs, newDevTreePath))
24fce8
+			 newMBKernel, newMBKernelArgs, newDevTreePath,
24fce8
+			 newIndex))
24fce8
 		return 1;
24fce8
 
24fce8
 	if (numEntries(config) == 0) {
24fce8
diff --git a/.gitignore b/.gitignore
24fce8
index e78a392d601..1c00ff7c5ed 100644
24fce8
--- a/.gitignore
24fce8
+++ b/.gitignore
24fce8
@@ -3,3 +3,4 @@ version.h
24fce8
 *.o
24fce8
 core.*
24fce8
 vgcore.*
24fce8
+*.tar.*
24fce8
diff --git a/grubby.8 b/grubby.8
24fce8
index 355b6eb6908..a4691f8ddb2 100644
24fce8
--- a/grubby.8
24fce8
+++ b/grubby.8
24fce8
@@ -7,12 +7,12 @@ grubby \- command line tool for configuring grub, lilo, elilo, yaboot and zipl
24fce8
        [--bad-image-okay] [--boot-filesystem=\fIbootfs\fR] 
24fce8
        [--bootloader-probe] [--config-file \fIpath\fR] [--copy-default]
24fce8
        [--debug] [--default-kernel] [--default-index] [--default-title]
24fce8
-       [--devtree=\fIdevicetree.dtb\fR]
24fce8
+       [--devtree=\fIdevicetree.dtb\fR] [--set-entry=\fIentry-index\fR]
24fce8
        [--grub] [--lilo] [--yaboot] [--silo] [--zipl]
24fce8
        [--info=\fIkernel-path\fR] [--initrd=\fIinitrd-path\fR] 
24fce8
        [--make-default] [-o path] [--version]
24fce8
        [--remove-kernel=\fIkernel-path\fR] [--remove-args=\fIargs\fR]
24fce8
-       [--set-default=\fIkernel-path\fR] [--set-default-index=\fientry-index\fR]
24fce8
+       [--set-default=\fIkernel-path\fR] [--set-default-index=\fIentry-index\fR]
24fce8
        [--title=entry-title] [--add-multiboot=\fImultiboot-path\fR]
24fce8
        [--mbargs=\fIargs\fR] [--remove-multiboot=\fImultiboot-path\fR]
24fce8
        [--remove-mbargs=\fIargs\fR]
24fce8
@@ -48,6 +48,10 @@ with that title are used.
24fce8
 \fB-\-add-kernel\fR=\fIkernel-path\fR
24fce8
 Add a new boot entry for the kernel located at \fIkernel-path\fR.
24fce8
 
24fce8
+.TP
24fce8
+\fB-\-set-entry\fR=\fIentry-index\fR
24fce8
+Set the position at which to add a new entry created with \fB-\-add-kernel\fR.
24fce8
+
24fce8
 .TP
24fce8
 \fB-\-args\fR=\fIkernel-args\fR
24fce8
 When a new kernel is added, this specifies the command line arguments
24fce8
diff --git a/test.sh b/test.sh
24fce8
index ba466a50501..7d1794c83df 100755
24fce8
--- a/test.sh
24fce8
+++ b/test.sh
24fce8
@@ -485,6 +485,20 @@ grubTest grub.8 add/g8.2 --add-kernel=/boot/new-kernel.img --title='title' \
24fce8
 grubTest grub.11 add/g11.1 --add-kernel=/boot/new-kernel.img --title='title' \
24fce8
     --initrd=/boot/new-initrd --boot-filesystem=/boot --copy-default \
24fce8
     --args='console=tty0 console=ttyS1,9600n81 single'
24fce8
+grubTest grub.1 add/g1.1 --add-kernel=/boot/new-kernel.img --title='title' \
24fce8
+    --initrd=/boot/new-initrd --boot-filesystem=/ --set-index 0
24fce8
+grubTest grub.1 add/g1.17 --add-kernel=/boot/new-kernel.img --title='title' \
24fce8
+    --initrd=/boot/new-initrd --boot-filesystem=/ --set-index 1
24fce8
+grubTest grub.1 add/g1.17 --add-kernel=/boot/new-kernel.img --title='title' \
24fce8
+    --initrd=/boot/new-initrd --boot-filesystem=/ --set-index 2
24fce8
+grubTest grub.15 add/g1.10 --add-kernel=/boot/new-kernel.img --title='title' \
24fce8
+    --initrd=/boot/new-initrd --boot-filesystem=/ --set-index 0
24fce8
+grubTest grub.15 add/g1.8 --add-kernel=/boot/new-kernel.img --title='title' \
24fce8
+    --initrd=/boot/new-initrd --boot-filesystem=/ --set-index 1
24fce8
+grubTest grub.15 add/g1.9 --add-kernel=/boot/new-kernel.img --title='title' \
24fce8
+    --initrd=/boot/new-initrd --boot-filesystem=/ --set-index 2
24fce8
+grubTest grub.15 add/g1.9 --add-kernel=/boot/new-kernel.img --title='title' \
24fce8
+    --initrd=/boot/new-initrd --boot-filesystem=/ --set-index 5
24fce8
 
24fce8
 testgrub2=n
24fce8
 ARCH=$(uname -m | sed s,i[3456789]86,ia32,)
24fce8
diff --git a/test/grub.15 b/test/grub.15
24fce8
new file mode 100644
24fce8
index 00000000000..e1c5f8a9b52
24fce8
--- /dev/null
24fce8
+++ b/test/grub.15
24fce8
@@ -0,0 +1,19 @@
24fce8
+# grub.conf generated by anaconda
24fce8
+#
24fce8
+# Note that you do not have to rerun grub after making changes to this file
24fce8
+# NOTICE:  You have a /boot partition.  This means that
24fce8
+#          all kernel and initrd paths are relative to /boot/, eg.
24fce8
+#          root (hd0,0)
24fce8
+#          kernel /vmlinuz-version ro root=/dev/sda1
24fce8
+#          initrd /initrd-version.img
24fce8
+#boot=/dev/hda
24fce8
+default=1
24fce8
+timeout=10
24fce8
+splashimage=(hd0,0)/grub/splash.xpm.gz
24fce8
+title Red Hat Linux (2.4.7-2)
24fce8
+	root (hd0,0)
24fce8
+	kernel /vmlinuz-2.4.7-2 ro root=/dev/sda1
24fce8
+	initrd /initrd-2.4.7-2.img
24fce8
+title zonk
24fce8
+	kernel /boot/new-kernel.img
24fce8
+	initrd /boot/new-initrd
24fce8
diff --git a/test/results/add/g1.10 b/test/results/add/g1.10
24fce8
new file mode 100644
24fce8
index 00000000000..dcdd8a8ce10
24fce8
--- /dev/null
24fce8
+++ b/test/results/add/g1.10
24fce8
@@ -0,0 +1,22 @@
24fce8
+# grub.conf generated by anaconda
24fce8
+#
24fce8
+# Note that you do not have to rerun grub after making changes to this file
24fce8
+# NOTICE:  You have a /boot partition.  This means that
24fce8
+#          all kernel and initrd paths are relative to /boot/, eg.
24fce8
+#          root (hd0,0)
24fce8
+#          kernel /vmlinuz-version ro root=/dev/sda1
24fce8
+#          initrd /initrd-version.img
24fce8
+#boot=/dev/hda
24fce8
+default=2
24fce8
+timeout=10
24fce8
+splashimage=(hd0,0)/grub/splash.xpm.gz
24fce8
+title title
24fce8
+	kernel /boot/new-kernel.img
24fce8
+	initrd /boot/new-initrd
24fce8
+title Red Hat Linux (2.4.7-2)
24fce8
+	root (hd0,0)
24fce8
+	kernel /vmlinuz-2.4.7-2 ro root=/dev/sda1
24fce8
+	initrd /initrd-2.4.7-2.img
24fce8
+title zonk
24fce8
+	kernel /boot/new-kernel.img
24fce8
+	initrd /boot/new-initrd
24fce8
diff --git a/test/results/add/g1.17 b/test/results/add/g1.17
24fce8
new file mode 100644
24fce8
index 00000000000..6a388228768
24fce8
--- /dev/null
24fce8
+++ b/test/results/add/g1.17
24fce8
@@ -0,0 +1,19 @@
24fce8
+# grub.conf generated by anaconda
24fce8
+#
24fce8
+# Note that you do not have to rerun grub after making changes to this file
24fce8
+# NOTICE:  You have a /boot partition.  This means that
24fce8
+#          all kernel and initrd paths are relative to /boot/, eg.
24fce8
+#          root (hd0,0)
24fce8
+#          kernel /vmlinuz-version ro root=/dev/sda1
24fce8
+#          initrd /initrd-version.img
24fce8
+#boot=/dev/hda
24fce8
+default=1
24fce8
+timeout=10
24fce8
+splashimage=(hd0,0)/grub/splash.xpm.gz
24fce8
+title Red Hat Linux (2.4.7-2)
24fce8
+	root (hd0,0)
24fce8
+	kernel /vmlinuz-2.4.7-2 ro root=/dev/sda1
24fce8
+	initrd /initrd-2.4.7-2.img
24fce8
+title title
24fce8
+	kernel /boot/new-kernel.img
24fce8
+	initrd /boot/new-initrd
24fce8
diff --git a/test/results/add/g1.8 b/test/results/add/g1.8
24fce8
new file mode 100644
24fce8
index 00000000000..5893a2f5af1
24fce8
--- /dev/null
24fce8
+++ b/test/results/add/g1.8
24fce8
@@ -0,0 +1,22 @@
24fce8
+# grub.conf generated by anaconda
24fce8
+#
24fce8
+# Note that you do not have to rerun grub after making changes to this file
24fce8
+# NOTICE:  You have a /boot partition.  This means that
24fce8
+#          all kernel and initrd paths are relative to /boot/, eg.
24fce8
+#          root (hd0,0)
24fce8
+#          kernel /vmlinuz-version ro root=/dev/sda1
24fce8
+#          initrd /initrd-version.img
24fce8
+#boot=/dev/hda
24fce8
+default=2
24fce8
+timeout=10
24fce8
+splashimage=(hd0,0)/grub/splash.xpm.gz
24fce8
+title Red Hat Linux (2.4.7-2)
24fce8
+	root (hd0,0)
24fce8
+	kernel /vmlinuz-2.4.7-2 ro root=/dev/sda1
24fce8
+	initrd /initrd-2.4.7-2.img
24fce8
+title title
24fce8
+	kernel /boot/new-kernel.img
24fce8
+	initrd /boot/new-initrd
24fce8
+title zonk
24fce8
+	kernel /boot/new-kernel.img
24fce8
+	initrd /boot/new-initrd
24fce8
diff --git a/test/results/add/g1.9 b/test/results/add/g1.9
24fce8
new file mode 100644
24fce8
index 00000000000..310623d13d2
24fce8
--- /dev/null
24fce8
+++ b/test/results/add/g1.9
24fce8
@@ -0,0 +1,22 @@
24fce8
+# grub.conf generated by anaconda
24fce8
+#
24fce8
+# Note that you do not have to rerun grub after making changes to this file
24fce8
+# NOTICE:  You have a /boot partition.  This means that
24fce8
+#          all kernel and initrd paths are relative to /boot/, eg.
24fce8
+#          root (hd0,0)
24fce8
+#          kernel /vmlinuz-version ro root=/dev/sda1
24fce8
+#          initrd /initrd-version.img
24fce8
+#boot=/dev/hda
24fce8
+default=2
24fce8
+timeout=10
24fce8
+splashimage=(hd0,0)/grub/splash.xpm.gz
24fce8
+title Red Hat Linux (2.4.7-2)
24fce8
+	root (hd0,0)
24fce8
+	kernel /vmlinuz-2.4.7-2 ro root=/dev/sda1
24fce8
+	initrd /initrd-2.4.7-2.img
24fce8
+title zonk
24fce8
+	kernel /boot/new-kernel.img
24fce8
+	initrd /boot/new-initrd
24fce8
+title title
24fce8
+	kernel /boot/new-kernel.img
24fce8
+	initrd /boot/new-initrd
24fce8
-- 
24fce8
2.17.1
24fce8