February 28, 2011

Dynamically Reboot Your Ubuntu Linux

Have you ever heard about [highlight2]Kexec[/highlight2]? [highlight2]Linux kernels[/highlight2] have had a feature to dynamically boot into a new kernel image stored on the memory.

Wikipedia defines Kexec as follows :
In computing, kexec (kernel execution) is a mechanism of the Linux kernel that allows “live” booting of a new kernel “over” the currently running kernel. kexec skips the [highlight2]bootloader[/highlight2] stage (hardware initialization phase by the firmware or BIOS) and directly loads the new kernel into memory, where it starts executing immediately. This avoids the long times associated with a full reboot, and can help systems to meet high-availability requirements by minimizing downtime.

Kexec is included in most Linux distributions and Linux kernel configurations by default. There are two reasons why such a feature exists in the Linux kernel :

  • Capturing crash dump upon a kernel crash (AKA kernel panic)
  • Rebooting faster into a newly installed kernel (or the same version)
    • We do not discuss crash dumps here, and will use Kexec to reboot into a kernel image dynamically. Kexec consists of two parts, a kernel space and a user space tool. The user space tool should be installed, if you want to take advantage of the existing kernel feature.

      To fast reboot into a kernel you need to install the user-space tool. These tools are packaged with the kexec-tools name, and could be installed with the following apt line :
      apt-get install kexec-tools

      The package might be just called ‘kexec’ in the older ubuntu versions.

      Now you’re ready to load a secondary kernel into memory with the following command (replace the fields written as capital) :
      kexec -l KERNEL_PATH –append=” KERNEL_OPTIONS” –initrd=INITRD_PATH

      I’m using latest kernel on Ubuntu Lucid, so replacing them for my kernel version and parameters, results in the following command :
      kexec -l /boot/vmlinuz-2.6.32-28-generic-pae –append=” linux /boot/vmlinuz-2.6.32-28-generic-pae root=UUID=72ecf84a-b99f-4c63-87f6-135f39bfb906 ro quiet” –initrd=/boot/initrd.img-2.6.32-28-generic-pae

      Finally boot your new kernel !
      kexec -e

      You can save the last two commands as an script file called quick-reboot, and use it instead of the standard reboot command. It is also possible to replace default reboot command in /etc/init.d/reboot, so that any standard reboot process leads to your quick-reboot command.

      Don’t worry, installing this package won’t enable kexec forever and in case of any problems, rebooting the system would make everything as it was. But use at your own risk 🙂