[libvirt] KVM/qemu: problems with autostart of vms with non-bridged nets

Hi, I'm using libvirt 0.5.0 to manage my kvm domains. Everything works fine except autostarting vms which use a network device not directly bridged to hw. I have the following net defined (and set to autostart): <network> <name>local</name> <uuid>7ec7a457-0b8e-4670-3b4a-2d0914288daa</uuid> <bridge stp='on' forwardDelay='0' /> <ip address='192.168.100.1' netmask='255.255.255.0'> <dhcp> <range start='192.168.100.128' end='192.168.100.254' /> </dhcp> </ip> </network> When a vm using this net is autostarted I get the following error: libvir: error : invalid connection pointer in virNetworkLookupByName libvir: QEMU error : internal error Network 'local' not found Failed to autostart VM 'testlocalnet': internal error Network 'local' not found I took a dive into the code and found two problems causing this: 1. initialization order: qemuStateDriver is registered (and thus initialized) before networkStateDriver. This means the network is not up yet when qemu does it's autostart. I could fix this with the attached patch. 2. missing virConnectPtr during autostart of the vm: in src/qemu_driver.c:145, qemudAutostartConfigs() you can find this call: int ret = qemudStartVMDaemon(NULL, driver, vm, NULL); This means virConnectPtr conn is NULL within qemudStartVMDaemon(). Following the calls: qemudStartVMDaemon() -> qemudBuildCommandLine() -> qemudNetworkIfaceConnect() -> virNetworkLookupByName() virNetworkLookupByName bails out if called with invalid virConnectPtr. This means starting this vm fails. Currently I'm not familiar enough with the libvirt structures to come up with an idea how to fix this. Could anybody with a deeper knowledge of the code take a look at this? Thank you very much. Kind regards, Gerd diff -r -u libvirt-0.5.0.orig/qemud/qemud.c libvirt-0.5.0/qemud/qemud.c --- libvirt-0.5.0.orig/qemud/qemud.c 2008-11-21 13:47:32.000000000 +0100 +++ libvirt-0.5.0/qemud/qemud.c 2008-11-30 21:27:06.000000000 +0100 @@ -761,22 +761,13 @@ * If they try to use a open a connection for a module that * is not loaded they'll get a suitable error at that point */ - virDriverLoadModule("qemu"); - virDriverLoadModule("lxc"); - virDriverLoadModule("uml"); virDriverLoadModule("network"); virDriverLoadModule("storage"); virDriverLoadModule("nodedev"); + virDriverLoadModule("qemu"); + virDriverLoadModule("lxc"); + virDriverLoadModule("uml"); #else -#ifdef WITH_QEMU - qemuRegister(); -#endif -#ifdef WITH_LXC - lxcRegister(); -#endif -#ifdef WITH_UML - umlRegister(); -#endif #ifdef WITH_NETWORK networkRegister(); #endif @@ -786,6 +777,15 @@ #if defined(HAVE_HAL) || defined(HAVE_DEVKIT) nodedevRegister(); #endif +#ifdef WITH_QEMU + qemuRegister(); +#endif +#ifdef WITH_LXC + lxcRegister(); +#endif +#ifdef WITH_UML + umlRegister(); +#endif #endif virEventRegisterImpl(virEventAddHandleImpl, -- Address (better: trap) for people I really don't want to get mail from: james@cactusamerica.com

Hi,
2. missing virConnectPtr during autostart of the vm:
in src/qemu_driver.c:145, qemudAutostartConfigs() you can find this call: int ret = qemudStartVMDaemon(NULL, driver, vm, NULL);
This means virConnectPtr conn is NULL within qemudStartVMDaemon(). Following the calls: qemudStartVMDaemon() -> qemudBuildCommandLine() -> qemudNetworkIfaceConnect() -> virNetworkLookupByName()
virNetworkLookupByName bails out if called with invalid virConnectPtr. This means starting this vm fails.
I came up with the attached patch to fix this for me, but a) I'm not sure if it is a clean use of the api to call virConnectOpen() from within a state-initializer function b) This is just for qemu/kvm, I haven't looked at any other drivers It would be nice if an experienced libvirt-developer could take a look at this. Thank you very much. Kind regards, Gerd diff -r -u libvirt-0.5.0.orig/src/qemu_driver.c libvirt-0.5.0/src/qemu_driver.c --- libvirt-0.5.0.orig/src/qemu_driver.c 2008-11-21 13:47:32.000000000 +0100 +++ libvirt-0.5.0/src/qemu_driver.c 2008-12-01 01:49:16.000000000 +0100 @@ -138,11 +138,14 @@ qemudAutostartConfigs(struct qemud_driver *driver) { unsigned int i; + /* we need a valid virConnectPtr for qemudStartVMDaemon to be able to connect to other drivers */ + virConnectPtr conn=virConnectOpen(getuid() ? "qemu:///session" : "qemu:///system"); + for (i = 0 ; i < driver->domains.count ; i++) { virDomainObjPtr vm = driver->domains.objs[i]; if (vm->autostart && !virDomainIsActive(vm)) { - int ret = qemudStartVMDaemon(NULL, driver, vm, NULL); + int ret = qemudStartVMDaemon(conn, driver, vm, NULL); if (ret < 0) { virErrorPtr err = virGetLastError(); qemudLog(QEMUD_ERR, _("Failed to autostart VM '%s': %s\n"), @@ -154,6 +157,8 @@ } } } + + virConnectClose(conn); } /** -- Address (better: trap) for people I really don't want to get mail from: james@cactusamerica.com

On Mon, Dec 01, 2008 at 02:07:37AM +0100, Gerd von Egidy wrote:
Hi,
2. missing virConnectPtr during autostart of the vm:
in src/qemu_driver.c:145, qemudAutostartConfigs() you can find this call: int ret = qemudStartVMDaemon(NULL, driver, vm, NULL);
This means virConnectPtr conn is NULL within qemudStartVMDaemon(). Following the calls: qemudStartVMDaemon() -> qemudBuildCommandLine() -> qemudNetworkIfaceConnect() -> virNetworkLookupByName()
virNetworkLookupByName bails out if called with invalid virConnectPtr. This means starting this vm fails.
I came up with the attached patch to fix this for me, but a) I'm not sure if it is a clean use of the api to call virConnectOpen() from within a state-initializer function b) This is just for qemu/kvm, I haven't looked at any other drivers
It would be nice if an experienced libvirt-developer could take a look at this. Thank you very much.
Thanks for the bug report & patch. Did this patch actually make it work for you ? AFAICT, there's a problem even earlier, which is that we are doing autostart of the virtual machines, before autostart of the networks and storage pools, so I'm not sure that this patch is sufficient. Regards, Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
participants (2)
-
Daniel P. Berrange
-
Gerd von Egidy