Welcome, guest | Sign In | My Account | Store | Cart

Kexec is a mechanism to use linux itself to load a new kernel without going through the BIOS thus minimizing down time. This script kexecs the newest kernel on the system managed by rpm (assumes a Redhat like system).

Python, 37 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python

import sys
import os
import subprocess
import fnmatch
import rpm

def newest_kernel_and_initrd():
    ts = rpm.TransactionSet()
    p = max(ts.dbMatch('name', 'kernel'))
    k = [ x for x in p['filenames']
         if fnmatch.fnmatch(x, '/boot/vmlinuz-*') ][0]
    #The initrd is not owned by the kernel rpm but generated in the %post script   
    i = '/boot/initrd-%s.img' % k[len('/boot/vmlinuz-'):]
    return (k, i)

def kexec(kernel, initrd, cmdline=None):
    if cmdline == None:
        cmdline = file('/proc/cmdline').read()[:-1] # strip newline
    r = subprocess.call(['/sbin/kexec', '-l', kernel,
                            '--initrd=%s' % initrd,
                            '--command-line=%s' % cmdline])
    if r != 0:
        raise Exception('kexec returned %d' % r)

def program_name():
    return os.path.basename(sys.argv[0])

def main():
    (k, i) = newest_kernel_and_initrd()
    sys.stderr.write('%s: loading ("%s", "%s")\n' % (program_name(), k, i))
    kexec(k, i)
    sys.stderr.write('%s: reboot to load new kernel\n' % program_name())

if __name__ == '__main__':
    main()

fix code pasting error