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

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