Blob Blame History Raw
From 8efba98644f9262d74a42fac7ea39d197192443a Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Wed, 22 Jun 2016 14:07:49 -0400
Subject: [PATCH 20/55] grubby: add --set-index to specify which position to
 add new entries as

This adds an option, "--set-index N", to grubby, and will cause creation
of any new entry to be at a particular zero-indexed position in the
resulting configuration file.

Related: rhbz#1285601

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 grubby.c               | 26 +++++++++++++++++++++-----
 .gitignore             |  1 +
 grubby.8               |  8 ++++++--
 test.sh                | 14 ++++++++++++++
 test/grub.15           | 19 +++++++++++++++++++
 test/results/add/g1.10 | 22 ++++++++++++++++++++++
 test/results/add/g1.17 | 19 +++++++++++++++++++
 test/results/add/g1.8  | 22 ++++++++++++++++++++++
 test/results/add/g1.9  | 22 ++++++++++++++++++++++
 9 files changed, 146 insertions(+), 7 deletions(-)
 create mode 100644 test/grub.15
 create mode 100644 test/results/add/g1.10
 create mode 100644 test/results/add/g1.17
 create mode 100644 test/results/add/g1.8
 create mode 100644 test/results/add/g1.9

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