2

I'm planning on compiling my own kernel, which I've done many times before but as always there's still a chance this could go wrong and the machine could fail to reboot.

This machine is within a remote data center to which I have no physical access and thus it would cost me a great amount to have someone to physically go to the server to fix it should the machine fail to boot correctly.

Is there a way I can set grub to use a new kernel for a single boot only but fall back to the old kernel upon an additional reboot. So I could use my remote power management console to reboot the machine if it failed to boot correctly with the new kernel, it would then fall back to the old kernel.

I know this is possible with lilo using the lilo -r kernel command, is there a grub equivalent?

Adam Gibbins
  • 7,147
  • 2
  • 28
  • 42

2 Answers2

5

To quote a bit from the GRUB Manual which adds a bit of extra checking for katriel's answer.

You can teach GRUB to boot an entry only at next boot time. Suppose that your have an old kernel old_kernel and a new kernel new_kernel. You know that old_kernel can boot your system correctly, and you want to test new_kernel.

To ensure that your system will go back to the old kernel even if the new kernel fails (e.g. it panics), you can specify that GRUB should try the new kernel only once and boot the old kernel after that.

First, modify your configuration file. Here is an example:

 default saved        # This is important!!!
 timeout 10

 title the old kernel
 root (hd0,0)
 kernel /old_kernel
 savedefault

 title the new kernel
 root (hd0,0)
 kernel /new_kernel
 savedefault 0         # This is important!!!

Note that this configuration file uses default saved' (see default) at the head andsavedefault 0' (see savedefault) in the entry for the new kernel. This means that GRUB boots a saved entry by default, and booting the entry for the new kernel saves `0' as the saved entry.

With this configuration file, after all, GRUB always tries to boot the old kernel after it booted the new one, because `0' is the entry of the old kernel.

The next step is to tell GRUB to boot the new kernel at next boot time. For this, execute grub-set-default (see Invoking grub-set-default):

 # grub-set-default 1

This command sets the saved entry to `1', that is, to the new kernel.

This method is useful, but still not very robust, because GRUB stops booting, if there is any error in the boot entry, such that the new kernel has an invalid executable format. Thus, it it even better to use the fallback mechanism of GRUB. Look at next subsection for this feature.

Dominic Cronin
  • 670
  • 4
  • 21
sysadmin1138
  • 131,083
  • 18
  • 173
  • 296
1

You could try using the "fallback" directive in /boot/grub/menu.lst. You will need to specify a fallback kernel or kernels, and use the savedefault directive in the kernel stanzas to be used as backups, for example (taken from the grub manual):

default saved        # This is important!!!
timeout 10
fallback 1 2         # This is important!!!

title A
root (hd0,0)
kernel /kernel
savedefault fallback # This is important!!!

title B
root (hd1,0)
kernel /kernel
savedefault fallback # This is important!!!

title C
root (hd2,0)
kernel /kernel
savedefault

This will cause the A kernel to boot, and if any failure occurs (kernel not found or kernel panic), the first and then the second fallback will be used (as specified in the fallback directive)

Please note that the manual specifies a missing kernel or kernel panic, it says nothing about a network card not working. Which in your case can be as bad as a kernel panic

katriel
  • 4,407
  • 22
  • 20