4b2bfe047bc025e34135f3185dadac0102238326
[bootmanager.git] / source / steps / WriteModprobeConfig.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 import BootAPI
15 import ModelOptions
16 import notify_messages
17 import modprobe
18
19 def Run( vars, log, filename = "/etc/modprobe.conf"):
20     """
21     write out the system file /etc/modprobe.conf with the current
22     set of modules.
23
24     returns a tuple of the number of network driver lines and storage
25     driver lines written as (networkcount,storagecount)
26     """
27
28     # write out the modprobe.conf file for the system. make sure
29     # the order of the ethernet devices are listed in the same order
30     # as the boot cd loaded the modules. this is found in /tmp/loadedmodules
31     # ultimately, the order will only match the boot cd order if
32     # the kernel modules have the same name - which should be true for the later
33     # version boot cds because they use the same kernel version.
34     # older boot cds use a 2.4.19 kernel, and its possible some of the network
35     # module names have changed, in which case the system might not boot
36     # if the network modules are activated in a different order that the
37     # boot cd.
38
39     # make sure we have this class loaded
40     
41     try:
42         SYSIMG_PATH= vars["SYSIMG_PATH"]
43         if SYSIMG_PATH == "":
44             raise ValueError, "SYSIMG_PATH"
45
46     except KeyError, var:
47         raise BootManagerException, "Missing variable in vars: %s\n" % var
48     except ValueError, var:
49         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
50
51     sysmods= systeminfo.get_system_modules(vars, log)
52     if sysmods is None:
53         raise BootManagerException, "Unable to get list of system modules."
54         
55     # parse the existing modprobe.conf file, if one exists
56     mfile = "%s/%s" % (SYSIMG_PATH,filename)
57     m = modprobe.Modprobe()
58     if os.path.exists(mfile):
59         m.input(mfile)
60
61     blacklist = modprobe.Modprobe()
62     if os.path.exists("/etc/modprobe.d"):
63         flist = os.listdir("/etc/modprobe.d")
64         for f in flist:
65             if f.find("blacklist") == 0:            
66                 blacklist.input("/etc/modprobe.d/%s"%f)
67         
68     # storage devices
69     m.optionsset("ata_generic","all_generic_ide=1")
70     scsi_count= 0
71     for a_mod in sysmods[systeminfo.MODULE_CLASS_SCSI]:
72         if m.blacklistget(a_mod) <> None or \
73                blacklist.blacklistget(a_mod) <> None:
74             continue
75         m.aliasset("scsi_hostadapter%d"%scsi_count,a_mod)
76         scsi_count= scsi_count + 1
77
78     # network devices
79     eth_count= 0
80     for a_mod in sysmods[systeminfo.MODULE_CLASS_NETWORK]:
81         if m.blacklistget(a_mod) <> None or \
82                blacklist.blacklistget(a_mod) <> None:
83             continue
84         m.aliasset("eth%d"%eth_count,a_mod)
85         eth_count= eth_count + 1
86     m.output(mfile, "BootManager")
87     m.output("%s.bak"%mfile, "BootManager") # write a backup version of this file
88
89     # dump the modprobe.conf file to the log (not to screen)
90     log.write( "Contents of new modprobe.conf file:\n" )
91     modulesconf_file= file("%s/%s" % (SYSIMG_PATH,filename), "r" )
92     contents= modulesconf_file.read()
93     log.write( contents + "\n" )
94     modulesconf_file.close()
95     modulesconf_file= None
96     log.write( "End contents of new modprobe.conf file.\n" )
97
98     # before we do the real kexec, check to see if we had any
99     # network drivers written to modprobe.conf. if not, return -1,
100     # which will cause this node to be switched to a debug state.
101     if eth_count == 0:
102         log.write( "\nIt appears we don't have any network drivers. Aborting.\n" )
103         
104         vars['BOOT_STATE']= 'dbg'
105         vars['STATE_CHANGE_NOTIFY']= 1
106         vars['STATE_CHANGE_NOTIFY_MESSAGE']= \
107              notify_messages.MSG_NO_DETECTED_NETWORK
108         raise BootManagerException, \
109               notify_messages.MSG_NO_DETECTED_NETWORK
110
111