10 def InitInterfaces(logger, plc, data, root="", files_only=False):
11 sysconfig = "%s/etc/sysconfig/network-scripts" % root
13 # query running network interfaces
15 ips = dict(zip(devs.values(), devs.keys()))
18 macs[sioc.gifhwaddr(dev).lower()] = dev
20 # assume data['networks'] contains this node's NodeNetworks
23 hostname = data.get('hostname',socket.gethostname())
24 networks = data['networks']
25 failedToGetSettings = False
26 for network in networks:
27 logger.verbose('net:InitInterfaces interface %d: %s'%(interface,network))
28 logger.verbose('net:InitInterfaces macs = %s' % macs)
29 logger.verbose('net:InitInterfaces ips = %s' % ips)
30 # Get interface name preferably from MAC address, falling back
33 if hwaddr <> None: hwaddr=hwaddr.lower()
35 orig_ifname = macs[hwaddr]
36 elif network['ip'] in ips:
37 orig_ifname = ips[network['ip']]
42 logger.verbose('net:InitInterfaces orig_ifname = %s' % orig_ifname)
48 inter['HWADDR'] = network['mac']
50 if network['method'] == "static":
51 inter['BOOTPROTO'] = "static"
52 inter['IPADDR'] = network['ip']
53 inter['NETMASK'] = network['netmask']
55 elif network['method'] == "dhcp":
56 inter['BOOTPROTO'] = "dhcp"
57 if network['hostname']:
58 inter['DHCP_HOSTNAME'] = network['hostname']
60 inter['DHCP_HOSTNAME'] = hostname
61 if not network['is_primary']:
62 inter['DHCLIENTARGS'] = "-R subnet-mask"
64 if len(network['nodenetwork_setting_ids']) > 0:
66 settings = plc.GetNodeNetworkSettings({'nodenetwork_setting_id':
67 network['nodenetwork_setting_ids']})
69 logger.log("net:InitInterfaces FATAL: failed call GetNodeNetworkSettings({'nodenetwork_setting_id':{%s})"% \
70 network['nodenetwork_setting_ids'])
71 failedToGetSettings = True
72 continue # on to the next network
74 for setting in settings:
75 # to explicitly set interface name
76 settingname = setting['name'].upper()
77 if settingname in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER'):
78 inter[settingname]=setting['value']
80 logger.log("net:InitInterfaces WARNING: ignored setting named %s"%setting['name'])
82 # support aliases to interfaces either by name or HWADDR
85 hwaddr = inter['HWADDR'].lower()
88 hwifname = macs[hwaddr]
89 if ('IFNAME' in inter) and inter['IFNAME'] <> hwifname:
90 logger.log("net:InitInterfaces WARNING: alias ifname (%s) and hwaddr ifname (%s) do not match"%\
91 (inter['IFNAME'],hwifname))
92 inter['IFNAME'] = hwifname
94 logger.log('net:InitInterfaces WARNING: mac addr %s for alias not found' %(hwaddr,alias))
97 # stupid RH /etc/sysconfig/network-scripts/ifup-aliases:new_interface()
98 # checks if the "$DEVNUM" only consists of '^[0-9A-Za-z_]*$'. Need to make
99 # our aliases compliant.
100 parts = inter['ALIAS'].split('_')
103 isValid=isValid and part.isalnum()
106 interfaces["%s:%s" % (inter['IFNAME'],inter['ALIAS'])] = inter
108 logger.log("net:InitInterfaces WARNING: interface alias (%s) not a valid string for RH ifup-aliases"% inter['ALIAS'])
110 logger.log("net:InitInterfaces WARNING: interface alias (%s) not matched to an interface"% inter['ALIAS'])
113 if ('IFNAME' not in inter) and not orig_ifname:
114 ifname="eth%d" % (interface-1)
115 # should check if $ifname is an eth already defines
116 if os.path.exists("%s/ifcfg-%s"%(sysconfig,ifname)):
117 logger.log("net:InitInterfaces WARNING: possibly blowing away %s configuration"%ifname)
119 if ('IFNAME' not in inter) and orig_ifname:
122 ifname = inter['IFNAME']
124 interfaces[ifname] = inter
126 m = modprobe.Modprobe()
127 m.input("%s/etc/modprobe.conf" % root)
128 for (dev, inter) in interfaces.iteritems():
129 # get the driver string "moduleName option1=a option2=b"
130 driver=inter.get('DRIVER','')
132 driver=driver.split()
133 kernelmodule=driver[0]
134 m.aliasset(dev,kernelmodule)
135 options=" ".join(driver[1:])
137 m.optionsset(dev,options)
138 m.output("%s/etc/modprobe.conf" % root)
140 # clean up after any ifcfg-$dev script that's no longer listed as
141 # part of the NodeNetworks associated with this node
143 # list all network-scripts
144 files = os.listdir(sysconfig)
146 # filter out the ifcfg-* files
149 if f.find("ifcfg-") == 0:
152 # remove loopback (lo) from ifcfgs list
154 if lo in ifcfgs: ifcfgs.remove(lo)
156 # remove known devices from icfgs list
157 for (dev, inter) in interfaces.iteritems():
159 if ifcfg in ifcfgs: ifcfgs.remove(ifcfg)
161 # delete the remaining ifcfgs from
162 deletedSomething = False
164 if not failedToGetSettings:
166 dev = ifcfg[len('ifcfg-'):]
167 path = "%s/ifcfg-%s" % (sysconfig,dev)
168 logger.verbose("net:InitInterfaces removing %s %s"%(dev,path))
169 os.system("/sbin/ifdown %s" % dev)
170 deletedSomething=True
173 # wait a bit for the one or more ifdowns to have taken effect
177 # Process ifcg-$dev changes / additions
179 for (dev, inter) in interfaces.iteritems():
181 f = file(tmpnam, "w")
182 f.write("# Autogenerated by NodeManager/net.py.... do not edit!\n")
183 if 'DRIVER' in inter:
184 f.write("# using %s driver for device %s\n" % (inter['DRIVER'],dev))
185 f.write('DEVICE="%s"\n' % dev)
187 # print the configuration values
188 for (key, val) in inter.iteritems():
189 if key not in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER'):
190 f.write('%s="%s"\n' % (key, val))
192 # print the configuration specific option values (if any)
193 if 'CFGOPTIONS' in inter:
194 cfgoptions = inter['CFGOPTIONS']
195 f.write('#CFGOPTIONS are %s\n' % cfgoptions)
196 for cfgoption in cfgoptions.split():
197 key,val = cfgoption.split('=')
201 f.write('%s="%s"\n' % (key,val))
204 # compare whether two files are the same
205 def comparefiles(a,b):
207 logger.verbose("net:InitInterfaces comparing %s with %s" % (a,b))
208 if not os.path.exists(a): return False
213 if not os.path.exists(b): return False
218 return buf_a == buf_b
222 path = "%s/ifcfg-%s" % (sysconfig,dev)
223 if not os.path.exists(path):
224 logger.verbose('net:InitInterfaces adding configuration for %s' % dev)
225 # add ifcfg-$dev configuration file
226 os.rename(tmpnam,path)
230 elif not comparefiles(tmpnam,path):
231 logger.verbose('net:InitInterfaces Configuration change for %s' % dev)
232 logger.verbose('net:InitInterfaces ifdown %s' % dev)
233 # invoke ifdown for the old configuration
234 os.system("/sbin/ifdown %s" % dev)
235 # wait a few secs for ifdown to complete
238 logger.log('replacing configuration for %s' % dev)
239 # replace ifcfg-$dev configuration file
240 os.rename(tmpnam,path)
244 # tmpnam & path are identical
249 fb = file("%s/ifcfg-%s"%(sysconfig,dev),"r")
250 for line in fb.readlines():
252 if parts[0][0]=="#":continue
253 if parts[0].find('='):
254 name,value = parts[0].split('=')
255 # clean up name & value
257 value = value.strip()
258 value = value.strip("'")
259 value = value.strip('"')
260 cfgvariables[name]=value
264 if name in cfgvariables:
265 value=cfgvariables[name]
266 value = value.lower()
270 # skip over device configs with ONBOOT=no
271 if getvar("ONBOOT") == 'no': continue
273 # don't bring up slave devices, the network scripts will
274 # handle those correctly
275 if getvar("SLAVE") == 'yes': continue
277 logger.verbose('net:InitInterfaces bringing up %s' % dev)
278 os.system("/sbin/ifup %s" % dev)