always regenerate initrd before booting in case hardware changes or new kernel is...
[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
15 def Run( vars, log ):
16     """
17     Rebuilds the system initrd, on first install or in case the
18     hardware changed.
19     """
20
21     log.write( "\n\nStep: Rebuilding initrd\n" )
22     
23     # make sure we have the variables we need
24     try:
25         SYSIMG_PATH= vars["SYSIMG_PATH"]
26         if SYSIMG_PATH == "":
27             raise ValueError, "SYSIMG_PATH"
28
29         PARTITIONS= vars["PARTITIONS"]
30         if PARTITIONS == None:
31             raise ValueError, "PARTITIONS"
32
33     except KeyError, var:
34         raise BootManagerException, "Missing variable in vars: %s\n" % var
35     except ValueError, var:
36         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
37
38     # mkinitrd attempts to determine if the root fs is on a logical
39     # volume by checking if the root device contains /dev/mapper in
40     # its path. The device node must exist for the check to succeed,
41     # but since it's usually managed by devfs or udev, so is probably
42     # not present, we just create a dummy file.
43     
44     fake_root_lvm= False
45     if not os.path.exists( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]) ):
46         fake_root_lvm= True
47         utils.makedirs( "%s/dev/mapper" % SYSIMG_PATH )
48         rootdev= file( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]), "w" )
49         rootdev.close()
50
51     initrd, kernel_version= systeminfo.getKernelVersion(vars,log)
52     utils.removefile( "%s/boot/%s" % (SYSIMG_PATH, initrd) )
53     utils.sysexec( "chroot %s mkinitrd -v /boot/initrd-%s.img %s" % \
54                    (SYSIMG_PATH, kernel_version, kernel_version), log )
55
56     if fake_root_lvm == True:
57         utils.removefile( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]) )