remove breakpoints, and use log instead of print when possible
[bootmanager.git] / source / steps / MakeInitrd.py
1 #!/usr/bin/python
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 from __future__ import print_function
10
11 import os
12
13 from Exceptions import *
14 import utils
15 import systeminfo
16
17 # for centos5.3
18 # 14:42:27(UTC) No module dm-mem-cache found for kernel 2.6.22.19-vs2.3.0.34.33.onelab, aborting.
19 # http://kbase.redhat.com/faq/docs/DOC-16528;jsessionid=7E984A99DE8DB094D9FB08181C71717C.ab46478d
20 def bypassRaidIfNeeded(sysimg_path, log):
21     try:
22         a, b, c, d = file('{}/etc/redhat-release'.format(sysimg_path))\
23             .readlines()[0].strip().split()
24         if a != 'CentOS':
25             return
26         major, minor = [ int(x) for x in c.split('.') ]
27         if minor >= 3:
28             utils.sysexec_noerr('echo "DMRAID=no" >> {}/etc/sysconfig/mkinitrd/noraid'
29                                 .format(sysimg_path), log)
30             utils.sysexec_noerr('chmod 755 {}/etc/sysconfig/mkinitrd/noraid'
31                                 .format(sysimg_path), log)
32     except:
33         pass
34             
35         
36 def Run(vars, log):
37     """
38     Rebuilds the system initrd, on first install or in case the
39     hardware changed.
40     """
41
42     log.write("\n\nStep: Rebuilding initrd\n")
43     
44     # make sure we have the variables we need
45     try:
46         SYSIMG_PATH = vars["SYSIMG_PATH"]
47         if SYSIMG_PATH == "":
48             raise ValueError("SYSIMG_PATH")
49
50         PARTITIONS = vars["PARTITIONS"]
51         if PARTITIONS == None:
52             raise ValueError("PARTITIONS")
53
54     except KeyError as var:
55         raise BootManagerException("Missing variable in vars: {}\n".format(var))
56     except ValueError as var:
57         raise BootManagerException("Variable in vars, shouldn't be: {}\n".format(var))
58
59     # mkinitrd needs /dev and /proc to do the right thing.
60     # /proc is already mounted, so bind-mount /dev here
61     # xxx tmp - trying to work around the f14 case:
62     # check that /dev/ is mounted with devtmpfs
63     # tmp - sysexec_noerr not returning what one would expect
64     # if utils.sysexec_noerr ("grep devtmpfs /proc/mounts") != 0:
65     utils.sysexec_noerr("mount -t devtmpfs none /dev")
66     utils.sysexec("mount -o bind /dev {}/dev".format(SYSIMG_PATH))
67     utils.sysexec("mount -t sysfs none {}/sys".format(SYSIMG_PATH))
68
69     initrd, kernel_version = systeminfo.getKernelVersion(vars,log)
70     try:
71         utils.removefile("{}/boot/{}".format(SYSIMG_PATH, initrd))
72     except:
73         log.write("{}/boot/{} is already removed\n".format(SYSIMG_PATH, initrd))
74
75     # hack for CentOS 5.3
76     bypassRaidIfNeeded(SYSIMG_PATH , log)
77     # specify ext3 for fedora14 and above as their default fs is ext4
78     utils.sysexec_chroot(SYSIMG_PATH,
79                          "mkinitrd -v --with=ext3 --allow-missing /boot/initrd-{}.img {}"
80                          .format(kernel_version, kernel_version), log)
81
82     utils.sysexec_noerr("umount {}/sys".format(SYSIMG_PATH), log)
83     utils.sysexec_noerr("umount {}/dev".format(SYSIMG_PATH), log)
84