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

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