de1fe8556a30634274d672c93d8c417fa5cdf686
[bootmanager.git] / source / steps / WriteModprobeConfig.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 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     log.write( "\n\nStep: WriteModProbeConfig.\n" )
40
41     # make sure we have this class loaded
42     
43     try:
44         SYSIMG_PATH= vars["SYSIMG_PATH"]
45         if SYSIMG_PATH == "":
46             raise ValueError, "SYSIMG_PATH"
47
48     except KeyError, var:
49         raise BootManagerException, "Missing variable in vars: %s\n" % var
50     except ValueError, var:
51         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
52
53     sysmods= systeminfo.get_system_modules(vars, log)
54     if sysmods is None:
55         raise BootManagerException, "Unable to get list of system modules."
56         
57     # parse the existing modprobe.conf file, if one exists
58     mfile = "%s/%s" % (SYSIMG_PATH,filename)
59     m = modprobe.Modprobe()
60     if os.path.exists(mfile):
61         m.input(mfile)
62
63     blacklist = modprobe.Modprobe()
64     blacklistfiles = os.listdir("/etc/modprobe.d")
65     for blf in blacklistfiles:
66         if os.path.exists("/etc/modprobe.d/%s"%blf):
67             blacklist.input("/etc/modprobe.d/%s"%blf)
68         
69     # storage devices
70     m.optionsset("ata_generic","all_generic_ide=1")
71     scsi_count= 0
72     for a_mod in sysmods[systeminfo.MODULE_CLASS_SCSI]:
73         if m.blacklistget(a_mod) <> None or \
74                blacklist.blacklistget(a_mod) <> None:
75             continue
76         m.aliasset("scsi_hostadapter%d"%scsi_count,a_mod)
77         scsi_count= scsi_count + 1
78
79     # network devices
80     eth_count= 0
81     for a_mod in sysmods[systeminfo.MODULE_CLASS_NETWORK]:
82         if m.blacklistget(a_mod) <> None or \
83                blacklist.blacklistget(a_mod) <> None:
84             continue
85         m.aliasset("eth%d"%eth_count,a_mod)
86         eth_count= eth_count + 1
87     m.output(mfile, "BootManager")
88     m.output("%s.bak"%mfile, "BootManager") # write a backup version of this file
89
90     # dump the modprobe.conf file to the log (not to screen)
91     log.write( "Contents of new modprobe.conf file:\n" )
92     modulesconf_file= file("%s/%s" % (SYSIMG_PATH,filename), "r" )
93     contents= modulesconf_file.read()
94     log.write( contents + "\n" )
95     modulesconf_file.close()
96     modulesconf_file= None
97     log.write( "End contents of new modprobe.conf file.\n" )
98
99     # before we do the real kexec, check to see if we had any
100     # network drivers written to modprobe.conf. if not, return -1,
101     # which will cause this node to be switched to a debug state.
102     if eth_count == 0:
103         log.write( "\nIt appears we don't have any network drivers. Aborting.\n" )
104         
105         vars['RUN_LEVEL']= 'failboot'
106         vars['STATE_CHANGE_NOTIFY']= 1
107         vars['STATE_CHANGE_NOTIFY_MESSAGE']= \
108              notify_messages.MSG_NO_DETECTED_NETWORK
109         raise BootManagerException, \
110               notify_messages.MSG_NO_DETECTED_NETWORK
111
112