[libvirt] [PATCH] network_conf: check sysfs if the bridge exists on host for user created bridges

The patch fixes the below problem. ============================== If the bridge name is not mentioned in the <network> xml, the bridge name is auto generated from virNetworkAllocateBridge(). If the default template named bridge is created manually by a user, the bridge start will fail with "File exists". bash-4.3$ sudo brctl addbr virbr1 bash-4.3$ brctl show bridge name bridge id STP enabled interfaces br0 8000.000000000000 no virbr0 8000.525400a91d03 yes virbr0-nic virbr1 8000.000000000000 no bash-4.3$ sudo virsh net-list --all Name State Autostart Persistent ---------------------------------------------------------- default active no yes bash-4.3$ cat /tmp/isolated # Notice that the <bridge> intentionally not given. <network> <name>isolated</name> <forward/> <ip address="192.168.123.1" netmask="255.255.255.0"> <dhcp> <range start="192.168.123.2" end="192.168.123.254"/> </dhcp> </ip> </network> bash-4.3$ sudo virsh net-create /tmp/isolated error: Failed to create network from isolated error: Unable to create bridge virbr1: File exists =============================== --- The following series implements... Shivaprasad G Bhat (1): network_conf: check sysfs if the bridge exists on host for user created bridges src/conf/network_conf.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) -- Signature

virNetworkBridgeInUse() doesn't check if the bridge is manually created outside of libvirt. Check the bridge name file in the sysfs to see if the bridge is in use. Signed-off-by: Shivaprasad G Bhat <sbhat@linux.vnet.ibm.com> --- src/conf/network_conf.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index d0e7e1b..4f90425 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -3074,16 +3074,28 @@ int virNetworkBridgeInUse(virNetworkObjListPtr nets, { size_t i; unsigned int ret = 0; + int defined_bridge = 0; + char *path = NULL; for (i = 0; i < nets->count; i++) { virNetworkObjLock(nets->objs[i]); if (nets->objs[i]->def->bridge && - STREQ(nets->objs[i]->def->bridge, bridge) && - !(skipname && STREQ(nets->objs[i]->def->name, skipname))) + STREQ(nets->objs[i]->def->bridge, bridge)) { + defined_bridge = 1; + if (!(skipname && STREQ(nets->objs[i]->def->name, skipname))) ret = 1; + } virNetworkObjUnlock(nets->objs[i]); } + /* Bridge might have been created by a user manually outside libvirt */ + if (!defined_bridge && !ret) { + if (virAsprintf(&path, "/sys/class/net/%s/", bridge) < 0) + return -1; + ret = virFileExists(path) ? 1 : 0; + VIR_FREE(path); + } + return ret; }

On Mon, Mar 09, 2015 at 08:30:08PM +0530, Shivaprasad G Bhat wrote:
virNetworkBridgeInUse() doesn't check if the bridge is manually created outside of libvirt. Check the bridge name file in the sysfs to see if the bridge is in use.
Signed-off-by: Shivaprasad G Bhat <sbhat@linux.vnet.ibm.com> --- src/conf/network_conf.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index d0e7e1b..4f90425 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -3074,16 +3074,28 @@ int virNetworkBridgeInUse(virNetworkObjListPtr nets, { size_t i; unsigned int ret = 0; + int defined_bridge = 0; + char *path = NULL;
for (i = 0; i < nets->count; i++) { virNetworkObjLock(nets->objs[i]); if (nets->objs[i]->def->bridge && - STREQ(nets->objs[i]->def->bridge, bridge) && - !(skipname && STREQ(nets->objs[i]->def->name, skipname))) + STREQ(nets->objs[i]->def->bridge, bridge)) { + defined_bridge = 1; + if (!(skipname && STREQ(nets->objs[i]->def->name, skipname))) ret = 1; + } virNetworkObjUnlock(nets->objs[i]); }
+ /* Bridge might have been created by a user manually outside libvirt */ + if (!defined_bridge && !ret) { + if (virAsprintf(&path, "/sys/class/net/%s/", bridge) < 0) + return -1; + ret = virFileExists(path) ? 1 : 0; + VIR_FREE(path); + }
You should use virNetDevExists() instead of poking in sysfs Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

On Mon, Mar 9, 2015 at 8:32 PM, Daniel P. Berrange <berrange@redhat.com> wrote:
On Mon, Mar 09, 2015 at 08:30:08PM +0530, Shivaprasad G Bhat wrote:
virNetworkBridgeInUse() doesn't check if the bridge is manually created outside of libvirt. Check the bridge name file in the sysfs to see if the bridge is in use.
Signed-off-by: Shivaprasad G Bhat <sbhat@linux.vnet.ibm.com> --- src/conf/network_conf.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index d0e7e1b..4f90425 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -3074,16 +3074,28 @@ int virNetworkBridgeInUse(virNetworkObjListPtr nets, { size_t i; unsigned int ret = 0; + int defined_bridge = 0; + char *path = NULL;
for (i = 0; i < nets->count; i++) { virNetworkObjLock(nets->objs[i]); if (nets->objs[i]->def->bridge && - STREQ(nets->objs[i]->def->bridge, bridge) && - !(skipname && STREQ(nets->objs[i]->def->name, skipname))) + STREQ(nets->objs[i]->def->bridge, bridge)) { + defined_bridge = 1; + if (!(skipname && STREQ(nets->objs[i]->def->name, skipname))) ret = 1; + } virNetworkObjUnlock(nets->objs[i]); }
+ /* Bridge might have been created by a user manually outside libvirt */ + if (!defined_bridge && !ret) { + if (virAsprintf(&path, "/sys/class/net/%s/", bridge) < 0) + return -1; + ret = virFileExists(path) ? 1 : 0; + VIR_FREE(path); + }
You should use virNetDevExists() instead of poking in sysfs
Thanks Dan. I'll send the v2 using virNetDevExists() as suggested. Regards, Shiva
Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
participants (3)
-
Daniel P. Berrange
-
Shivaprasad bhat
-
Shivaprasad G Bhat