@@ -273,41 +273,66 @@ def __init__(self, macaddr = None, type="bridge", bridge = None, network=None):
273273 else :
274274 raise ValueError , _ ("Unknown network type %s" ) % (type ,)
275275
276- def setup (self , conn ):
276+ def is_conflict_net (self , conn ):
277+ """is_conflict_net: determines if mac conflicts with others in system
278+
279+ returns a two element tuple:
280+ first element is True if fatal collision occured
281+ second element is a string description of the collision.
282+ Non fatal collisions (mac addr collides with inactive guest) will
283+ return (False, "description of collision")"""
277284 # get Running Domains
278285 ids = conn .listDomainsID ();
279286 vms = []
280287 for id in ids :
281- vm = conn .lookupByID (id )
282- vms .append (vm )
288+ try :
289+ vm = conn .lookupByID (id )
290+ vms .append (vm )
291+ except libvirt .libvirtError :
292+ # guest probably in process of dieing
293+ logging .warn ("conflict_net: Failed to lookup domain id %d" % id )
283294 # get inactive Domains
284295 inactive_vm = []
285296 names = conn .listDefinedDomains ()
286297 for name in names :
287- vm = conn .lookupByName (name )
288- inactive_vm .append (vm )
298+ try :
299+ vm = conn .lookupByName (name )
300+ inactive_vm .append (vm )
301+ except :
302+ # guest probably in process of dieing
303+ logging .warn ("conflict_net: Failed to lookup domain %d" % name )
289304
290305 # get the Host's NIC MACaddress
291306 hostdevs = util .get_host_network_devices ()
292307
308+ if self .countMACaddr (vms ) > 0 :
309+ return (True , _ ("The MAC address you entered is already in use by another virtual machine!" ))
310+ for (dummy , dummy , dummy , dummy , host_macaddr ) in hostdevs :
311+ if self .macaddr .upper () == host_macaddr .upper ():
312+ return (True , _ ("The MAC address you entered conflicts with the physical NIC." ))
313+ if self .countMACaddr (inactive_vm ) > 0 :
314+ return (False , _ ("The MAC address you entered is already in use by another inactive virtual machine!" ))
315+ return (False , None )
316+
317+ def setup (self , conn ):
318+
293319 # check conflict MAC address
294320 if self .macaddr is None :
295321 while 1 :
296322 self .macaddr = util .randomMAC ()
297- if self .countMACaddr ( vms ) > 0 :
323+ if self .is_conflict_net ( conn )[ 1 ] is not None :
298324 continue
299325 else :
300326 break
301327 else :
302- if self .countMACaddr (vms ) > 0 :
303- raise RuntimeError , _ ("The MAC address you entered is already in use by another virtual machine!" )
304- for (dummy , dummy , dummy , dummy , host_macaddr ) in hostdevs :
305- if self .macaddr .upper () == host_macaddr .upper ():
306- raise RuntimeError , _ ("The MAC address you entered conflicts with the physical NIC." )
307- if self .countMACaddr (inactive_vm ) > 0 :
308- msg = _ ("The MAC address you entered is already in use by another inactive virtual machine!" )
309- print >> sys .stderr , msg
310- logging .warning (msg )
328+ ret , msg = self .is_conflict_net (conn )
329+ if msg is not None :
330+ # Error message found
331+ if ret is False :
332+ # Not fatal
333+ logging .warning (msg )
334+ else :
335+ raise RuntimeError (msg )
311336
312337 if not self .bridge and self .type == "bridge" :
313338 self .bridge = util .default_bridge ()
0 commit comments