Hack to make older bootcds create initrds. Uses mkinitrd shell script.
[bootmanager.git] / source / steps / MakeInitrd.py
1 #!/usr/bin/python2 -u
2
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
7 # All rights reserved.
8
9 import os, string
10
11 from Exceptions import *
12 import utils
13 import systeminfo
14 import shutil
15
16 def Run( vars, log ):
17     """
18     Rebuilds the system initrd, on first install or in case the
19     hardware changed.
20     """
21
22     log.write( "\n\nStep: Rebuilding initrd\n" )
23     
24     # make sure we have the variables we need
25     try:
26         SYSIMG_PATH= vars["SYSIMG_PATH"]
27         if SYSIMG_PATH == "":
28             raise ValueError, "SYSIMG_PATH"
29
30         PARTITIONS= vars["PARTITIONS"]
31         if PARTITIONS == None:
32             raise ValueError, "PARTITIONS"
33
34     except KeyError, var:
35         raise BootManagerException, "Missing variable in vars: %s\n" % var
36     except ValueError, var:
37         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
38
39     # mkinitrd attempts to determine if the root fs is on a logical
40     # volume by checking if the root device contains /dev/mapper in
41     # its path. The device node must exist for the check to succeed,
42     # but since it's usually managed by devfs or udev, so is probably
43     # not present, we just create a dummy file.
44     
45     fake_root_lvm= False
46     if not os.path.exists( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]) ):
47         fake_root_lvm= True
48         utils.makedirs( "%s/dev/mapper" % SYSIMG_PATH )
49         rootdev= file( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]), "w" )
50         rootdev.close()
51
52     initrd, kernel_version= systeminfo.getKernelVersion(vars,log)
53     utils.removefile( "%s/boot/%s" % (SYSIMG_PATH, initrd) )
54     if checkKern() == True:
55         utils.sysexec( "chroot %s mkinitrd -v /boot/initrd-%s.img %s" % \
56                    (SYSIMG_PATH, kernel_version, kernel_version), log )
57     else:
58         shutil.copy("./mkinitrd.sh","%s/tmp/mkinitrd.sh" % SYSIMG_PATH)
59         os.chmod("%s/tmp/mkinitrd.sh" % SYSIMG_PATH, 755)
60         utils.sysexec( "chroot %s /tmp/mkinitrd.sh %s" % (SYSIMG_PATH, kernel_version))
61
62     if fake_root_lvm == True:
63         utils.removefile( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]) )
64
65 def checkKern():
66     #  Older bootcds only support LinuxThreads.  This hack is to get mkinitrd
67     #  to run without segfaulting by using /lib/obsolete/linuxthreads
68     kver = os.popen("/bin/uname -r", "r").readlines()[0].rstrip().split(".")
69     if int(kver[1]) > 4:
70         return True
71     elif int(kver[1]) <=4:
72         return False