Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit b1e511a

Browse files
committed
Add is_conflict_net to NIC class.
1 parent e1fe04e commit b1e511a

2 files changed

Lines changed: 49 additions & 60 deletions

File tree

virtinst/CloneManager.py

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,12 @@ def setup_original(self):
223223
#
224224
for i in self._clone_mac:
225225
ret, msg = self._check_mac(i)
226-
if ret == 0:
227-
continue
228-
elif ret == 1 or ret == 2:
229-
raise RuntimeError, msg
230-
elif ret == 3:
231-
print >> sys.stderr, msg
232-
logging.warning(msg)
226+
if msg is not None:
227+
if ret:
228+
raise RuntimeError, msg
229+
else:
230+
print >> sys.stderr, msg
231+
logging.warning(msg)
233232

234233
logging.debug("setup_original out")
235234

@@ -285,7 +284,7 @@ def setup_clone(self):
285284
while 1:
286285
mac = util.randomMAC()
287286
ret, msg = self._check_mac(mac)
288-
if ret != 0:
287+
if msg is not None:
289288
continue
290289
else:
291290
break
@@ -338,45 +337,10 @@ def _check_file(self, conn, disk, size):
338337

339338
#
340339
# check used mac func
341-
# 0 : OK
342-
# 1 : NG Conflict with the physical NIC
343-
# 2 : NG Used by another guest
344-
# 3 : NG Used by another inactive guest
345340
#
346341
def _check_mac(self, mac):
347-
348-
msg0=""
349-
msg1=_("The MAC address you entered conflicts with the physical NIC.")
350-
msg2=_("The MAC address you entered is already in use by another guest!")
351-
msg3=_("The MAC address you entered is already in use by another inactive guest!")
352-
353-
# get Running Domains
354-
ids = self._hyper_conn.listDomainsID();
355-
356-
vms = []
357-
for id in ids:
358-
vm = self._hyper_conn.lookupByID(id)
359-
vms.append(vm)
360-
# get inactive Domains
361-
inactive_vm = []
362-
names = self._hyper_conn.listDefinedDomains()
363-
364-
for name in names:
365-
vm = self._hyper_conn.lookupByName(name)
366-
inactive_vm.append(vm)
367-
368-
# get the Host's NIC MACaddress
369-
hostdevs = util.get_host_network_devices()
370-
371-
if self._count_mac(vms, mac) > 0:
372-
return (2, msg2)
373-
for (dummy, dummy, dummy, dummy, host_macaddr) in hostdevs:
374-
if mac.upper() == host_macaddr.upper():
375-
return (1, msg1)
376-
if self._count_mac(inactive_vm, mac) > 0:
377-
return (3, msg3)
378-
379-
return (0, msg0)
342+
nic = Guest.VirtualNetworkInterface(macaddr=mac)
343+
return nic.is_conflict_net(self._hyper_conn)
380344

381345
#
382346
# get count macaddr

virtinst/Guest.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)