b4a8917033765b3588f77e73049fa8e1f3c82cdc
[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 # for centos5.3
20 # 14:42:27(UTC) No module dm-mem-cache found for kernel 2.6.22.19-vs2.3.0.34.33.onelab, aborting.
21 # http://kbase.redhat.com/faq/docs/DOC-16528;jsessionid=7E984A99DE8DB094D9FB08181C71717C.ab46478d
22 def bypassRaidIfNeeded(sysimg_path):
23     try:
24         [ a,b,c,d]=file('%s/etc/redhat-release'%sysimg_path).readlines()[0].strip().split()
25         if a !='CentOS': return
26         [major,minor]=[int(x) for x in c.split('.')]
27         if minor >= 3:
28             utils.sysexec_noerr('echo "DMRAID=no" >> %s/etc/sysconfig/mkinitrd/noraid' % sysimg_path)
29             utils.sysexec_noerr('chmod 755 %s/etc/sysconfig/mkinitrd/noraid' % sysimg_path)
30     except:
31         pass
32             
33         
34 def Run( vars, log ):
35     """
36     Rebuilds the system initrd, on first install or in case the
37     hardware changed.
38     """
39
40     log.write( "\n\nStep: Rebuilding initrd\n" )
41     
42     # make sure we have the variables we need
43     try:
44         SYSIMG_PATH= vars["SYSIMG_PATH"]
45         if SYSIMG_PATH == "":
46             raise ValueError, "SYSIMG_PATH"
47
48         PARTITIONS= vars["PARTITIONS"]
49         if PARTITIONS == None:
50             raise ValueError, "PARTITIONS"
51
52     except KeyError, var:
53         raise BootManagerException, "Missing variable in vars: %s\n" % var
54     except ValueError, var:
55         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
56
57     # mkinitrd needs /dev and /proc to do the right thing.
58     # /proc is already mounted, so bind-mount /dev here
59     utils.sysexec("mount -o bind /dev %s/dev" % SYSIMG_PATH)
60     utils.sysexec("mount -t sysfs none %s/sys" % SYSIMG_PATH)
61
62     initrd, kernel_version= systeminfo.getKernelVersion(vars,log)
63     try:
64         utils.removefile( "%s/boot/%s" % (SYSIMG_PATH, initrd) )
65     except:
66         print "%s/boot/%s is already removed" % (SYSIMG_PATH, initrd)
67
68     # hack for CentOS 5.3
69     bypassRaidIfNeeded(SYSIMG_PATH)
70     utils.sysexec_chroot( SYSIMG_PATH, "mkinitrd -v /boot/initrd-%s.img %s" % \
71                (kernel_version, kernel_version), log )
72
73     utils.sysexec_noerr("umount %s/sys" % SYSIMG_PATH)
74     utils.sysexec_noerr("umount %s/dev" % SYSIMG_PATH)
75