bug-grub
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Change default on boot


From: Andrew Athan
Subject: Re: Change default on boot
Date: Thu, 07 Jul 2005 15:30:43 -0400
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

It turns out that the RedHat version of grub implements a useful feature:

savedefault: savedefault [--stage2=STAGE2_FILE] [--default=DEFAULT] [--once]
   Save DEFAULT as the default boot entry in STAGE2_FILE. If
   '--once' is specified, the default is reset after the next reboot.

So, lets say that menu item 0 is the new, possibly unstable kernel. Using this, one could add a line "savedefault --default=1" to the commands for the "0" menu item. Then, the boot will revert back to "1" after one boot. For example:

default=saved
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Xen 2.0 / XenLinux 2.6.10
       kernel /xen.gz dom0_mem=1048576 sched=bvt
module /vmlinuz-2.6.10-xen0-syncraid root=/dev/VolGroup00/LogVol01 ro rhgb quiet
       module /initrd-2.6.10.img
title Fedora Core (2.6.10 NO DEBUG)
       root (hd0,0)
       savedefault --default=1
       kernel /vmlinuz-2.6.10 ro root=/dev/VolGroup00/LogVol01 rhgb quiet
       initrd /initrd-2.6.10.img

Then, in a command line, run grub, and at the prompt type "savedefault --default=0"

The next time the machine boots, it will boot into Xen. But if it fails to boot, and/or on the next reboot, it will boot Fedora Core.

I was pointed in this direction by the email at this location: http://lists.gnu.org/archive/html/bug-grub/2004-06/msg00063.html , "Subject: GRUB: extending savedefault" by Martin von Gagern.

If these features are not already part of GNU Grub, they certainly have my vote! By the way, Martin also provides the source for a command-line utility that changes the savedefault value, excerpted here. I am not sure if it is compatible with the current format of the grub stage2 file.

A.


--- grub-0.94/util/grub-savedefault.c   1970-01-01 01:00:00.000000000 +0100
+++ grub-0.94-savedefault/util/grub-savedefault.c       2004-06-19
00:33:01.871447584 +0200
@@ -0,0 +1,67 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define SAVED_ENTRY_POS 0x20c
+#define DEFAULT_STAGE2_LOCATION "/boot/grub/stage2"
+
+int main(int argc, char** argv) {
+       const char *filename=DEFAULT_STAGE2_LOCATION;
+       char* ptr;
+       int entry;
+       FILE* file;
+ + if (argc>=2) entry=strtoul(argv[1], &ptr, 0);
+       if (argc<2 || argc>3 || *ptr || entry > 0xff) {
+               ptr=strrchr(argv[0], '/');
+               if (ptr==NULL) ptr=strrchr(argv[0], '\\');
+               if (ptr==NULL) ptr=argv[0];
+               else ++ptr;
+               fprintf(stderr,
+                       "Usage: %s NUM [STAGE2]\n\t"
+                       "Modify grub stage2 file STAGE2 (default "
+                       DEFAULT_STAGE2_LOCATION
+                       ")\n\tto set default entry to NUM\n",
+                       ptr);
+               return argc==1 ? 0 : 1;
+       }
+       file=fopen(filename, "r+b");
+       if (file==NULL) {
+               perror(filename);
+               return 1;
+       }
+       if (fseek(file, SAVED_ENTRY_POS, SEEK_SET)) {
+               perror("seek to entry number location (20C)");
+               fclose(file);
+               return 2;
+       }
+       if (fputc(entry, file) != entry) {
+               perror("write number to stage2 file");
+               fclose(file);
+               return 2;
+       }
+       if (fclose(file)) {
+               perror("close stage2 file");
+               return 2;
+       }
+       return 0;
+}


A.


Andrew Athan wrote:


I am trying to remotely debug a linux kernel problem which causes the machine to reboot early in the bootup process. When this happens, the machine goes into an infinite loop attempting to boot.

It would be nice to be be able to override the default menu entry that grub boots into, but only for a specified number of boots (or unsuccessful boots). This could be hardcoded at "1".

(Like a poor-man's "last known good" bootup capability )

This would allow safer updates to the boot-loader configurations of remote machines, since by setting the count to "1", one could make a change and have some chance that if the boot is not successful, the next boot will revert to the prior "known good" config. Differentiation between successful and unsuccessful boots, which is really an "extra credit" feature, could be signaled by a monotonically incrementing count stored within a file in the boot partition.


Or, someone could point me to an existing solution...

Thanks!
Andrew







reply via email to

[Prev in Thread] Current Thread [Next in Thread]