043db9c3f7794fe1638ec741355f1de09e208761
[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 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 needs /dev and /proc to do the right thing.
40     # /proc is already mounted, so bind-mount /dev here
41     utils.sysexec("mount -o bind /dev %s/dev" % SYSIMG_PATH)
42     utils.sysexec("mount -t sysfs none %s/sys" % SYSIMG_PATH)
43
44     initrd, kernel_version= systeminfo.getKernelVersion(vars,log)
45     utils.removefile( "%s/boot/%s" % (SYSIMG_PATH, initrd) )
46     if checkKern() == True:
47         utils.sysexec( "chroot %s mkinitrd -v /boot/initrd-%s.img %s" % \
48                    (SYSIMG_PATH, kernel_version, kernel_version), log )
49     else:
50         shutil.copy("./mkinitrd.sh","%s/tmp/mkinitrd.sh" % SYSIMG_PATH)
51         os.chmod("%s/tmp/mkinitrd.sh" % SYSIMG_PATH, 755)
52         utils.sysexec( "chroot %s /tmp/mkinitrd.sh %s" % (SYSIMG_PATH, kernel_version))
53
54     utils.sysexec_noerr("umount %s/sys" % SYSIMG_PATH)
55     utils.sysexec_noerr("umount %s/dev" % SYSIMG_PATH)
56
57 def checkKern():
58     #  Older bootcds only support LinuxThreads.  This hack is to get mkinitrd
59     #  to run without segfaulting by using /lib/obsolete/linuxthreads
60     kver = os.popen("/bin/uname -r", "r").readlines()[0].rstrip().split(".")
61     if int(kver[1]) > 4:
62         return True
63     elif int(kver[1]) <=4:
64         return False