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

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