svn:keywords
[bootmanager.git] / source / steps / MakeInitrd.py
1 #!/usr/bin/python
2 #
3 # $Id$
4 # $URL$
5 #
6 # Copyright (c) 2003 Intel Corporation
7 # All rights reserved.
8 #
9 # Copyright (c) 2004-2006 The Trustees of Princeton University
10 # All rights reserved.
11
12 import os, string
13
14 from Exceptions import *
15 import utils
16 import systeminfo
17 import shutil
18
19 def kernelHasMkinitrd():
20     #  Older bootcds only support LinuxThreads.  This hack is to get mkinitrd
21     #  to run without segfaulting by using /lib/obsolete/linuxthreads
22     kver = os.popen("/bin/uname -r", "r").readlines()[0].rstrip().split(".")
23     if int(kver[1]) > 4:
24         return True
25     elif int(kver[1]) <=4:
26         return False
27
28
29 # for centos5.3
30 # 14:42:27(UTC) No module dm-mem-cache found for kernel 2.6.22.19-vs2.3.0.34.33.onelab, aborting.
31 # http://kbase.redhat.com/faq/docs/DOC-16528;jsessionid=7E984A99DE8DB094D9FB08181C71717C.ab46478d
32 def bypassRaidIfNeeded(sysimg_path):
33     try:
34         [ a,b,c,d]=file('%s/etc/redhat-release'%sysimg_path).readlines()[0].strip().split()
35         if a !='CentOS': return
36         [major,minor]=[int(x) for x in c.split('.')]
37         if minor >= 3:
38             utils.sysexec_noerr('echo "DMRAID=no" >> %s/etc/sysconfig/mkinitrd/noraid' % sysimg_path)
39             utils.sysexec_noerr('chmod 755 %s/etc/sysconfig/mkinitrd/noraid' % sysimg_path)
40     except:
41         pass
42             
43         
44 def Run( vars, log ):
45     """
46     Rebuilds the system initrd, on first install or in case the
47     hardware changed.
48     """
49
50     log.write( "\n\nStep: Rebuilding initrd\n" )
51     
52     # make sure we have the variables we need
53     try:
54         SYSIMG_PATH= vars["SYSIMG_PATH"]
55         if SYSIMG_PATH == "":
56             raise ValueError, "SYSIMG_PATH"
57
58         PARTITIONS= vars["PARTITIONS"]
59         if PARTITIONS == None:
60             raise ValueError, "PARTITIONS"
61
62     except KeyError, var:
63         raise BootManagerException, "Missing variable in vars: %s\n" % var
64     except ValueError, var:
65         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
66
67     # mkinitrd needs /dev and /proc to do the right thing.
68     # /proc is already mounted, so bind-mount /dev here
69     utils.sysexec("mount -o bind /dev %s/dev" % SYSIMG_PATH)
70     utils.sysexec("mount -t sysfs none %s/sys" % SYSIMG_PATH)
71
72     initrd, kernel_version= systeminfo.getKernelVersion(vars,log)
73     try:
74         utils.removefile( "%s/boot/%s" % (SYSIMG_PATH, initrd) )
75     except:
76         print "%s/boot/%s is already removed" % (SYSIMG_PATH, initrd)
77
78     # hack for CentOS 5.3
79     bypassRaidIfNeeded(SYSIMG_PATH)
80     if kernelHasMkinitrd() == True:
81         utils.sysexec_chroot( SYSIMG_PATH, "mkinitrd -v /boot/initrd-%s.img %s" % \
82                    (kernel_version, kernel_version), log )
83     else:
84         shutil.copy("./mkinitrd.sh","%s/tmp/mkinitrd.sh" % SYSIMG_PATH)
85         os.chmod("%s/tmp/mkinitrd.sh" % SYSIMG_PATH, 755)
86         utils.sysexec_chroot( SYSIMG_PATH, "/tmp/mkinitrd.sh %s" % (kernel_version))
87
88     utils.sysexec_noerr("umount %s/sys" % SYSIMG_PATH)
89     utils.sysexec_noerr("umount %s/dev" % SYSIMG_PATH)
90