Added code to detect 'shared' physical NICs

This commit is contained in:
Daniel P. Berrange 2007-04-10 18:27:37 -04:00
parent a389933464
commit eb68f9321f
3 changed files with 56 additions and 28 deletions

View File

@ -20,7 +20,8 @@
import gobject import gobject
import libvirt import libvirt
import logging import logging
import os import os, sys
import traceback
from time import time from time import time
import logging import logging
from socket import gethostbyaddr, gethostname from socket import gethostbyaddr, gethostname
@ -92,8 +93,11 @@ class vmmConnection(gobject.GObject):
# Find info about all current present media # Find info about all current present media
for path in self.hal_iface.FindDeviceByCapability("net"): for path in self.hal_iface.FindDeviceByCapability("net"):
self._device_added(path) self._device_added(path)
except Exception, e: except:
logging.error("Unable to connect to HAL to list network devices: '%s'", e) (type, value, stacktrace) = sys.exc_info ()
logging.error("Unable to connect to HAL to list network devices: '%s'" + \
str(type) + " " + str(value) + "\n" + \
traceback.format_exc (stacktrace))
self.bus = None self.bus = None
self.hal_iface = None self.hal_iface = None
@ -104,7 +108,28 @@ class vmmConnection(gobject.GObject):
name = obj.GetPropertyString("net.interface") name = obj.GetPropertyString("net.interface")
mac = obj.GetPropertyString("net.address") mac = obj.GetPropertyString("net.address")
dev = vmmNetDevice(self.config, self, name, mac, False) # Now magic to determine if the device is part of a bridge
shared = False
bridge = None
try:
# XXX Linux specific - needs porting for other OS - patches
# welcomed...
sysfspath = obj.GetPropertyString("linux.sysfs_path")
brportpath = os.path.join(sysfspath, "brport")
if os.path.exists(brportpath):
shared = True
brlinkpath = os.path.join(brportpath, "bridge")
dest = os.readlink(brlinkpath)
(head,tail) = os.path.split(dest)
bridge = tail
except:
(type, value, stacktrace) = sys.exc_info ()
logging.error("Unable to determine if device is shared:" +
str(type) + " " + str(value) + "\n" + \
traceback.format_exc (stacktrace))
dev = vmmNetDevice(self.config, self, name, mac, shared, bridge)
self.netdevs[path] = dev self.netdevs[path] = dev
self.emit("netdev-added", dev.get_name()) self.emit("netdev-added", dev.get_name())
@ -159,8 +184,8 @@ class vmmConnection(gobject.GObject):
def get_net(self, uuid): def get_net(self, uuid):
return self.nets[uuid] return self.nets[uuid]
def get_net_device(self, name): def get_net_device(self, path):
return self.netdevs[name] return self.netdevs[path]
def close(self): def close(self):
if self.vmm == None: if self.vmm == None:
@ -176,11 +201,8 @@ class vmmConnection(gobject.GObject):
def list_net_uuids(self): def list_net_uuids(self):
return self.nets.keys() return self.nets.keys()
def list_net_device_names(self): def list_net_device_paths(self):
names = [] return self.netdevs.keys()
for path in self.netdevs:
names.append(self.netdevs[path].get_name())
return names
def get_host_info(self): def get_host_info(self):
return self.hostinfo return self.hostinfo

View File

@ -955,8 +955,10 @@ class vmmCreate(gobject.GObject):
def populate_device_model(self, model): def populate_device_model(self, model):
model.clear() model.clear()
br = virtinst.util.default_bridge() for name in self.connection.list_net_device_paths():
model.append([br]) net = self.connection.get_net_device(name)
if net.is_shared():
model.append([net.get_bridge()])
def change_os_type(self, box): def change_os_type(self, box):
model = box.get_model() model = box.get_model()

View File

@ -19,26 +19,30 @@
import gobject import gobject
class vmmNetDevice(gobject.GObject): class vmmNetDevice(gobject.GObject):
__gsignals__ = {} __gsignals__ = {}
def __init__(self, config, connection, name, mac, shared): def __init__(self, config, connection, name, mac, shared, bridge=None):
self.__gobject_init__() self.__gobject_init__()
self.conn = connection self.conn = connection
self.name = name self.name = name
self.mac = mac self.mac = mac
self.shared = shared self.shared = shared
self.bridge = bridge
def get_connection(self): def get_connection(self):
return self.conn return self.conn
def get_name(self): def get_name(self):
return self.name return self.name
def is_shared(self): def is_shared(self):
return self.shared return self.shared
def get_mac(self): def get_bridge(self):
return self.mac return self.bridge
def get_mac(self):
return self.mac
gobject.type_register(vmmNetDevice) gobject.type_register(vmmNetDevice)