-
Notifications
You must be signed in to change notification settings - Fork 0
/
modprobe.py
180 lines (143 loc) · 5.51 KB
/
modprobe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#
"""Modprobe is a utility to read/modify/write /etc/modprobe.conf"""
import os
import tempfile
class Modprobe:
def __init__(self,filename="/etc/modprobe.conf"):
self.conffile = {}
for keyword in ("include","alias","options","install","remove","blacklist","MODULES"):
self.conffile[keyword]={}
self.filename = filename
def input(self,filename=None):
if filename==None: filename=self.filename
# list of file names; loop itself might add filenames
filenames = [filename]
for filename in filenames:
if not os.path.exists(filename):
continue
fb = file(filename,"r")
for line in fb.readlines():
def __default():
modulename=parts[1]
rest=" ".join(parts[2:])
self._set(command,modulename,rest)
def __alias():
wildcard=parts[1]
modulename=parts[2]
self.aliasset(wildcard,modulename)
options=''
if len(parts)>3:
options=" ".join(parts[3:])
self.optionsset(modulename,options)
self.conffile['MODULES'][modulename]=options
def __options():
modulename=parts[1]
rest=" ".join(parts[2:])
self.conffile['MODULES'][modulename]=rest
__default()
def __blacklist():
modulename=parts[1]
self.blacklistset(modulename,'')
def __include():
newfilename = parts[1]
if os.path.exists(newfilename):
if os.path.isdir(newfilename):
for e in os.listdir(newfilename):
filenames.append("%s/%s"%(newfilename,e))
else:
filenames.append(newfilename)
funcs = {"alias":__alias,
"options":__options,
"blacklis":__blacklist,
"include":__include}
parts = line.split()
# skip empty lines or those that are comments
if len(parts) == 0 or parts[0] == "#":
continue
# lower case first word
command = parts[0].lower()
# check if its a command we support
if not self.conffile.has_key(command):
print "WARNING: command %s not recognized." % command
continue
func = funcs.get(command,__default)
func()
fb.close()
def _get(self,command,key):
return self.conffile[command].get(key,None)
def _set(self,command,key,value):
self.conffile[command][key]=value
def aliasget(self,key):
return self._get('alias',key)
def optionsget(self,key):
return self._get('options',key)
def blacklistget(self,key):
return self._get('blacklist',key)
def aliasset(self,key,value):
self._set("alias",key,value)
def optionsset(self,key,value):
self._set("options",key,value)
def blacklistset(self,key,value):
self._set("blacklist",key,value)
def _comparefiles(self,a,b):
try:
if not os.path.exists(a): return False
fb = open(a)
buf_a = fb.read()
fb.close()
if not os.path.exists(b): return False
fb = open(b)
buf_b = fb.read()
fb.close()
return buf_a == buf_b
except IOError, e:
return False
def output(self,filename="/etc/modprobe.conf",program="NodeManager"):
(fd, tmpnam) = tempfile.mkstemp(dir=os.path.dirname(filename))
fb = os.fdopen(fd, "w")
fb.write("# Written out by %s\n" % program)
for command in ("alias","options","install","remove","blacklist"):
table = self.conffile[command]
keys = table.keys()
keys.sort()
for k in keys:
v = table[k]
fb.write("%s %s %s\n" % (command,k,v))
fb.close()
if not self._comparefiles(tmpnam,filename):
os.rename(tmpnam,filename)
os.chmod(filename,0644)
return True
else:
os.unlink(tmpnam)
return False
def probe(self,name):
o = os.popen("/sbin/modprobe %s" % name)
o.close()
def checkmodules(self):
syspath="/sys/module"
modules = os.listdir(syspath)
for module in modules:
path="%/%s/parameters"%(syspath,module)
if os.path.exists(path):
ps=os.listdir(path)
parameters={}
for p in ps:
fb = file("%s/%s"%(path,p),"r")
parameters[p]=fb.readline()
fb.close()
if __name__ == '__main__':
import sys
m = Modprobe()
if len(sys.argv)>1:
fn = sys.argv[1]
else:
fn = "/etc/modprobe.conf"
m.input()
blacklist = Modprobe()
blacklistfiles = os.listdir("/etc/modprobe.d")
for blf in blacklistfiles:
if os.path.exists("/etc/modprobe.d/%s"%blf):
blacklist.input("/etc/modprobe.d/%s"%blf)
m.output("/tmp/%s-tmp"%os.path.basename(fn),"TEST")
blacklist.output("/tmp/blacklist-tmp.txt","TEST")