@@ -235,14 +235,35 @@ def randomMAC():
235
235
return ':' .join (map (lambda x : "%02x" % x , mac ))
236
236
237
237
238
- def getMacAddress (network , name ):
238
+ def getNetwork (network ):
239
+ if network .lower () == 'NAT' :
240
+ return 'NAT' , 'default'
241
+ elif network .lower ().startswith ('pub' ):
242
+ return 'PUBLIC' , PUBLIC_BRIDGE
243
+
244
+ if ':' in network :
245
+ net_type , net_name = network .split (':' , 1 )
246
+ net_type = net_type .lower ()
247
+ else :
248
+ net_type = 'net'
249
+ net_name = network
250
+ if net_type == 'br' :
251
+ return 'BRIDGE' , net_name
252
+ elif net_type == 'net' :
253
+ return 'NETWORK' , net_name
254
+ else :
255
+ print 'Unknow network_type.'
256
+ sys .exit (4 )
257
+
258
+
259
+ def getMacAddress (net_type , net_name , name ):
239
260
global mac_dict
240
- if (network == PUBLIC_BRIDGE and name in mac_dict ):
261
+ if (net_type == 'PUBLIC' and name in mac_dict ):
241
262
mac = mac_dict [name ]
242
- print 'Use %s for nic connected to %s' % (mac , network )
263
+ print 'Use %s for nic connected to %s' % (mac , net_name )
243
264
else :
244
265
mac = randomMAC ()
245
- print 'Generate random MAC address %s for network %s' % (mac , network )
266
+ print 'Generate random MAC address %s for network %s' % (mac , net_name )
246
267
return mac
247
268
248
269
@@ -268,10 +289,12 @@ def generateLibvirtXML(args, libvirt_xml):
268
289
if len (args .nic ) == 0 :
269
290
args .nic .append ('NAT' )
270
291
for i , network in enumerate (args .nic ):
271
- mac = getMacAddress (network , args .NAME )
292
+ net_type , net_name = getNetwork (network )
293
+ mac = getMacAddress (net_type , net_name , args .NAME )
272
294
targetdev = getDeviceName (args .NAME , i )
273
295
slot = '0x%02x' % (BASE_SLOT + i )
274
- param = {'network' : network , 'mac' : mac , 'slot' : slot }
296
+ param = {'net_type' : net_type , 'net_name' : net_name ,
297
+ 'mac' : mac , 'slot' : slot }
275
298
if targetdev :
276
299
param ['targetdev' ] = targetdev
277
300
params ['nics' ].append (param )
@@ -283,7 +306,7 @@ def generateLibvirtXML(args, libvirt_xml):
283
306
f .write (tmpl .render (params ))
284
307
285
308
for m in params ['nics' ]:
286
- msg = "%s: %s" % (m ['network ' ], m ['mac' ])
309
+ msg = "%s(%s) : %s" % (m ['net_type' ], m [ 'net_name ' ], m ['mac' ])
287
310
if m .get ('targetdev' ):
288
311
msg += ' (%s)' % m ['targetdev' ]
289
312
print msg
0 commit comments