5 """Modprobe is a utility to read/modify/write /etc/modprobe.conf"""
11 def __init__(self,filename="/etc/modprobe.conf"):
13 self.origconffile = {}
14 for keyword in ("alias","options","install","remove","blacklist","MODULES"):
15 self.conffile[keyword]={}
16 self.filename = filename
18 def input(self,filename=None):
19 if filename==None: filename=self.filename
20 fb = file(filename,"r")
21 for line in fb.readlines():
23 command = parts[0].lower()
25 table = self.conffile.get(command,None)
27 print "WARNING: command %s not recognize. Ignoring!" % command
30 if command == "alias":
33 self.aliasset(wildcard,modulename)
36 options=" ".join(parts[3:])
37 self.optionsset(modulename,options)
38 self.conffile['MODULES']={}
39 self.conffile['MODULES'][modulename]=options
42 rest=" ".join(parts[2:])
43 self._set(command,modulename,rest)
44 if command == "options":
45 self.conffile['MODULES'][modulename]=rest
47 self.origconffile = self.conffile.copy()
49 def _get(self,command,key):
50 return self.conffile[command].get(key,None)
52 def _set(self,command,key,value):
53 self.conffile[command][key]=value
55 def aliasget(self,key):
56 return self._get('alias',key)
58 def optionsget(self,key):
59 return self._get('options',key)
61 def aliasset(self,key,value):
62 self._set("alias",key,value)
64 def optionsset(self,key,value):
65 self._set("options",key,value)
67 def _comparefiles(self,a,b):
69 if not os.path.exists(a): return False
74 if not os.path.exists(b): return False
83 def output(self,filename="/etc/modprobe.conf",program="NodeManager"):
84 (fd, tmpnam) = tempfile.mkstemp(dir=os.path.dirname(filename))
85 fb = os.fdopen(fd, "w")
86 fb.write("# Written out by %s\n" % program)
88 for command in ("alias","options","install","remove","blacklist"):
89 table = self.conffile[command]
94 fb.write("%s %s %s\n" % (command,k,v))
97 if not self._comparefiles(tmpnam,filename):
98 os.rename(tmpnam,filename)
99 os.chmod(filename,0644)
104 def probe(self,name):
105 o = os.popen("/sbin/modprobe %s" % name)
108 def checkmodules(self):
109 syspath="/sys/module"
110 modules = os.listdir(syspath)
111 for module in modules:
112 path="%/%s/parameters"%(syspath,module)
113 if os.path.exists(path):
117 fb = file("%s/%s"%(path,p),"r")
118 parameters[p]=fb.readline()
121 if __name__ == '__main__':
124 m = Modprobe(sys.argv[1])
129 m.aliasset("bond0","bonding")
130 m.optionsset("bond0","miimon=100")