2 """Modprobe is a utility to read/modify/write /etc/modprobe.conf"""
8 def __init__(self,filename="/etc/modprobe.conf"):
10 for keyword in ("include","alias","options","install","remove","blacklist","MODULES"):
11 self.conffile[keyword]={}
12 self.filename = filename
14 def input(self,filename=None):
15 if filename==None: filename=self.filename
17 # list of file names; loop itself might add filenames
18 filenames = [filename]
20 for filename in filenames:
21 if not os.path.exists(filename):
24 fb = file(filename,"r")
25 for line in fb.readlines():
28 rest=" ".join(parts[2:])
29 self._set(command,modulename,rest)
34 self.aliasset(wildcard,modulename)
37 options=" ".join(parts[3:])
38 self.optionsset(modulename,options)
39 self.conffile['MODULES'][modulename]=options
43 rest=" ".join(parts[2:])
44 self.conffile['MODULES'][modulename]=rest
49 self.blacklistset(modulename,'')
52 newfilename = parts[1]
53 if os.path.exists(newfilename):
54 if os.path.isdir(newfilename):
55 for e in os.listdir(newfilename):
56 filenames.append("%s/%s"%(newfilename,e))
58 filenames.append(newfilename)
60 funcs = {"alias":__alias,
62 "blacklis":__blacklist,
67 # skip empty lines or those that are comments
68 if len(parts) == 0 or parts[0] == "#":
71 # lower case first word
72 command = parts[0].lower()
74 # check if its a command we support
75 if not self.conffile.has_key(command):
76 print "WARNING: command %s not recognized." % command
79 func = funcs.get(command,__default)
84 def _get(self,command,key):
85 return self.conffile[command].get(key,None)
87 def _set(self,command,key,value):
88 self.conffile[command][key]=value
90 def aliasget(self,key):
91 return self._get('alias',key)
93 def optionsget(self,key):
94 return self._get('options',key)
96 def blacklistget(self,key):
97 return self._get('blacklist',key)
99 def aliasset(self,key,value):
100 self._set("alias",key,value)
102 def optionsset(self,key,value):
103 self._set("options",key,value)
105 def blacklistset(self,key,value):
106 self._set("blacklist",key,value)
108 def _comparefiles(self,a,b):
110 if not os.path.exists(a): return False
115 if not os.path.exists(b): return False
120 return buf_a == buf_b
124 def output(self,filename="/etc/modprobe.conf",program="NodeManager"):
125 (fd, tmpnam) = tempfile.mkstemp(dir=os.path.dirname(filename))
126 fb = os.fdopen(fd, "w")
127 fb.write("# Written out by %s\n" % program)
129 for command in ("alias","options","install","remove","blacklist"):
130 table = self.conffile[command]
135 fb.write("%s %s %s\n" % (command,k,v))
138 if not self._comparefiles(tmpnam,filename):
139 os.rename(tmpnam,filename)
140 os.chmod(filename,0644)
146 def probe(self,name):
147 o = os.popen("/sbin/modprobe %s" % name)
150 def checkmodules(self):
151 syspath="/sys/module"
152 modules = os.listdir(syspath)
153 for module in modules:
154 path="%/%s/parameters"%(syspath,module)
155 if os.path.exists(path):
159 fb = file("%s/%s"%(path,p),"r")
160 parameters[p]=fb.readline()
163 if __name__ == '__main__':
169 fn = "/etc/modprobe.conf"
173 blacklist = Modprobe()
174 blacklistfiles = os.listdir("/etc/modprobe.d")
175 for blf in blacklistfiles:
176 if os.path.exists("/etc/modprobe.d/%s"%blf):
177 blacklist.input("/etc/modprobe.d/%s"%blf)
179 m.output("/tmp/%s-tmp"%os.path.basename(fn),"TEST")
180 blacklist.output("/tmp/blacklist-tmp.txt","TEST")