[libvirt] [PATCH 0/9] eliminate almost all uses of strerror
by Jim Meyering
We know that strerror is not thread-safe, so I'm
eliminating all uses from code that might be multi-threaded.
I started the job with this patch:
http://thread.gmane.org/gmane.comp.emulators.libvirt/11532
Here are the summaries of the upcoming change sets:
eliminate strerror qemu_driver.c: use virReportSystemError instead
This is the patch mentioned above.
remove duplicate *SetCloseExec and *SetNonBlock functions
Changes prompted by Dan's feedback (see thread).
report OOMError
tiny
iptables.c: Use virStrerror, not strerror.
qemud.c: use virStrerror, not strerror
don't include raw errno in diagnostics
remove remainder of offending strerror uses
note: the qemud/remote.c changes that move virDomainFree calls
do that merely to make that code identical to a nearby, nearly-
identical function.
syntax-check: enable prohibit_nonreentrant
makes "make syntax-check" check for all those non-reentrant
functions, but excluding virterror.c, virsh.c and console.c
syntax-check: avoid spurious false-positive
15 years, 9 months
[libvirt] [PATCH] remove useless code
by Jim Meyering
Barely worth posting for review, but who knows...
>From a7cae36a3d966e80d82859d63c60a303b63d6720 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering(a)redhat.com>
Date: Wed, 4 Feb 2009 17:44:24 +0100
Subject: [PATCH] remove useless code
* src/bridge.c (brAddTap): Remove redundant errno=ENOMEM assignment
after failed strdup.
* src/cgroup.c (virCgroupFree): Remove redundant *group=NULL assignment
after VIR_FREE(*group).
---
src/bridge.c | 6 ++----
src/cgroup.c | 1 -
2 files changed, 2 insertions(+), 5 deletions(-)
diff --git a/src/bridge.c b/src/bridge.c
index 9c4ca74..990a567 100644
--- a/src/bridge.c
+++ b/src/bridge.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2007 Red Hat, Inc.
+ * Copyright (C) 2007, 2009 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -539,10 +539,8 @@ brAddTap(brControl *ctl,
if ((errno = brSetInterfaceUp(ctl, try.ifr_name, 1)))
goto error;
VIR_FREE(*ifname);
- if (!(*ifname = strdup(try.ifr_name))) {
- errno = ENOMEM;
+ if (!(*ifname = strdup(try.ifr_name)))
goto error;
- }
*tapfd = fd;
return 0;
}
diff --git a/src/cgroup.c b/src/cgroup.c
index 1b4c27f..8bd6e87 100644
--- a/src/cgroup.c
+++ b/src/cgroup.c
@@ -51,7 +51,6 @@ void virCgroupFree(virCgroupPtr *group)
if (*group != NULL) {
VIR_FREE((*group)->path);
VIR_FREE(*group);
- *group = NULL;
}
}
--
1.6.1.2.467.g081e7
15 years, 9 months
[libvirt] [PATCH] Report error for some xend operations on shutdown domain
by john.levon@sun.com
# HG changeset patch
# User john.levon(a)sun.com
# Date 1233765613 28800
# Node ID 706a43692cceeba796e36c070e2b46f85d8cb4f0
# Parent bda41ea0cbbdea409447686c30b7afb10b9cae85
Report error for some xend operations on shutdown domain
Signed-off-by: John Levon <john.levon(a)sun.com>
diff --git a/src/xend_internal.c b/src/xend_internal.c
--- a/src/xend_internal.c
+++ b/src/xend_internal.c
@@ -2833,8 +2833,13 @@ xenDaemonDomainSuspend(virDomainPtr doma
__FUNCTION__);
return(-1);
}
- if (domain->id < 0)
+
+ if (domain->id < 0) {
+ virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+ _("Domain %s isn't running."), domain->name);
return(-1);
+ }
+
return xend_op(domain->conn, domain->name, "op", "pause", NULL);
}
@@ -2855,8 +2860,13 @@ xenDaemonDomainResume(virDomainPtr domai
__FUNCTION__);
return(-1);
}
- if (domain->id < 0)
+
+ if (domain->id < 0) {
+ virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+ _("Domain %s isn't running."), domain->name);
return(-1);
+ }
+
return xend_op(domain->conn, domain->name, "op", "unpause", NULL);
}
@@ -2878,8 +2888,13 @@ xenDaemonDomainShutdown(virDomainPtr dom
__FUNCTION__);
return(-1);
}
- if (domain->id < 0)
+
+ if (domain->id < 0) {
+ virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+ _("Domain %s isn't running."), domain->name);
return(-1);
+ }
+
return xend_op(domain->conn, domain->name, "op", "shutdown", "reason", "poweroff", NULL);
}
@@ -2902,8 +2917,13 @@ xenDaemonDomainReboot(virDomainPtr domai
__FUNCTION__);
return(-1);
}
- if (domain->id < 0)
+
+ if (domain->id < 0) {
+ virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+ _("Domain %s isn't running."), domain->name);
return(-1);
+ }
+
return xend_op(domain->conn, domain->name, "op", "shutdown", "reason", "reboot", NULL);
}
@@ -2928,8 +2948,13 @@ xenDaemonDomainDestroy(virDomainPtr doma
__FUNCTION__);
return(-1);
}
- if (domain->id < 0)
+
+ if (domain->id < 0) {
+ virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+ _("Domain %s isn't running."), domain->name);
return(-1);
+ }
+
return xend_op(domain->conn, domain->name, "op", "destroy", NULL);
}
@@ -2993,12 +3018,17 @@ xenDaemonDomainSave(virDomainPtr domain,
xenDaemonDomainSave(virDomainPtr domain, const char *filename)
{
if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
- (filename == NULL) || (domain->id < 0)) {
+ (filename == NULL)) {
virXendError((domain ? domain->conn : NULL), VIR_ERR_INVALID_ARG,
__FUNCTION__);
return(-1);
}
+ if (domain->id < 0) {
+ virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+ _("Domain %s isn't running."), domain->name);
+ return(-1);
+ }
/* We can't save the state of Domain-0, that would mean stopping it too */
if (domain->id == 0) {
@@ -3030,8 +3060,13 @@ xenDaemonDomainCoreDump(virDomainPtr dom
__FUNCTION__);
return(-1);
}
- if (domain->id < 0)
+
+ if (domain->id < 0) {
+ virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+ _("Domain %s isn't running."), domain->name);
return(-1);
+ }
+
return xend_op(domain->conn, domain->name, "op", "dump", "file", filename,
"live", "0", "crash", "0", NULL);
}
@@ -3599,8 +3634,12 @@ xenDaemonDomainPinVcpu(virDomainPtr doma
__FUNCTION__);
return (-1);
}
- if (domain->id < 0)
+
+ if (domain->id < 0) {
+ virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+ _("Domain %s isn't running."), domain->name);
return(-1);
+ }
/* from bit map, build character string of mapped CPU numbers */
for (i = 0; i < maplen; i++) for (j = 0; j < 8; j++)
15 years, 9 months
[libvirt] [PATCH] lxc_container: Don't dereference NULL upon failure (x2)
by Jim Meyering
Spotted these while skimming through all VIR_FREE uses.
>From af9e606d97e8321683ba765ba0a4af4c065d3b4b Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering(a)redhat.com>
Date: Wed, 4 Feb 2009 18:44:34 +0100
Subject: [PATCH] lxc_container: Don't dereference NULL upon failure
* src/lxc_container.c (lxcContainerPivotRoot): Free and zero
oldroot *after* printing the diagnostic, both for virFileMakePath
and pivot_root failure.
---
src/lxc_container.c | 5 ++---
1 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/src/lxc_container.c b/src/lxc_container.c
index ea52eed..90c70d5 100644
--- a/src/lxc_container.c
+++ b/src/lxc_container.c
@@ -284,20 +284,20 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
}
if ((rc = virFileMakePath(oldroot)) < 0) {
- VIR_FREE(oldroot);
virReportSystemError(NULL, rc,
_("failed to create %s"),
oldroot);
+ VIR_FREE(oldroot);
return -1;
}
/* The old root directory will live at /.oldroot after
* this and will soon be unmounted completely */
if (pivot_root(root->src, oldroot) < 0) {
- VIR_FREE(oldroot);
virReportSystemError(NULL, errno,
_("failed to pivot root %s to %s"),
oldroot, root->src);
+ VIR_FREE(oldroot);
return -1;
}
VIR_FREE(oldroot);
@@ -677,4 +677,3 @@ int lxcContainerAvailable(int features)
return 0;
}
-
--
1.6.1.2.467.g081e7
15 years, 9 months
[libvirt] [PATCH] trivial libvirt example code
by Dave Allan
The examples directory doesn't have a trivial example of how to connect
to a hypervisor, make a few calls, and disconnect, so I put one
together. I would appreciate any suggestions on anything that I've done
wrong as well as suggestions for other fundamental API calls that should
be illustrated.
Dave
commit 14531c0fa3680890086d61ae4d66aec525f9c9a0
Author: David Allan <dallan(a)redhat.com>
Date: Fri Jan 23 14:27:39 2009 -0500
Added a trivial example program to illustrate connecting to the hypervisor and making some simple API calls.
diff --git a/examples/hellolibvirt/hellolibvirt.c b/examples/hellolibvirt/hellolibvirt.c
new file mode 100644
index 0000000..aae79b8
--- /dev/null
+++ b/examples/hellolibvirt/hellolibvirt.c
@@ -0,0 +1,137 @@
+/* This file contains trivial example code to connect to the running
+ * hypervisor and gather a few bits of information. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <libvirt/libvirt.h>
+
+static int
+showHypervisorInfo(virConnectPtr conn)
+{
+ int ret = 0;
+ unsigned long hvVer, major, minor, release;
+ const char *hvType;
+
+ /* virConnectGetType returns a pointer to a static string, so no
+ * allocation or freeing is necessary; it is possible for the call
+ * to fail if, for example, there is no connection to a
+ * hypervisor, so check what it returns. */
+ hvType = virConnectGetType(conn);
+ if (NULL == hvType) {
+ ret = 1;
+ printf("Failed to get hypervisor type\n");
+ goto out;
+ }
+
+ if (0 != virConnectGetVersion(conn, &hvVer)) {
+ ret = 1;
+ printf("Failed to get hypervisor version\n");
+ goto out;
+ }
+
+ major = hvVer / 1000000;
+ hvVer %= 1000000;
+ minor = hvVer / 1000;
+ release = hvVer % 1000;
+
+ printf("Hypervisor: \"%s\" version: %lu.%lu.%lu\n",
+ hvType,
+ major,
+ minor,
+ release);
+
+out:
+ return ret;
+}
+
+
+static int
+showDomains(virConnectPtr conn)
+{
+ int ret = 0, i, numNames, numInactiveDomains, numActiveDomains;
+ char **nameList = NULL;
+
+ numActiveDomains = virConnectNumOfDomains(conn);
+ numInactiveDomains = virConnectNumOfDefinedDomains(conn);
+
+ printf("There are %d active and %d inactive domains\n",
+ numActiveDomains, numInactiveDomains);
+
+ nameList = malloc(sizeof(char *) * (unsigned int)numInactiveDomains);
+
+ if (NULL == nameList) {
+ ret = 1;
+ printf("Could not allocate memory for list of inactive domains\n");
+ goto out;
+ }
+
+ numNames = virConnectListDefinedDomains(conn,
+ nameList,
+ numInactiveDomains);
+
+ if (-1 == numNames) {
+ ret = 1;
+ printf("Could not get list of defined domains from hypervisor\n");
+ goto out;
+ }
+
+ if (numNames > 0) {
+ printf("Inactive domains:\n");
+ }
+
+ for (i = 0 ; i < numNames ; i++) {
+ printf(" %s\n", *(nameList + i));
+ /* The API documentation doesn't say so, but the names
+ * returned by virConnectListDefinedDomains are strdup'd and
+ * must be freed here. */
+ free(*(nameList + i));
+ }
+
+out:
+ if (NULL != nameList) {
+ free(nameList);
+ }
+
+ return ret;
+}
+
+
+int
+main(void)
+{
+ int ret = 0;
+ virConnectPtr conn = NULL;
+
+ printf("Attempting to connect to hypervisor\n");
+
+ /* virConnectOpenAuth called here with all default parameters */
+ conn = virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0);
+
+ if (NULL == conn) {
+ ret = 1;
+ printf("No connection to hypervisor\n");
+ goto out;
+ }
+
+ printf("Connected to hypervisor\n");
+
+ if (0 != showHypervisorInfo(conn)) {
+ ret = 1;
+ goto disconnect;
+ }
+
+ if (0 != showDomains(conn)) {
+ ret = 1;
+ goto disconnect;
+ }
+
+disconnect:
+ if (0 != virConnectClose(conn)) {
+ printf("Failed to disconnect from hypervisor\n");
+ } else {
+ printf("Disconnected from hypervisor\n");
+ }
+
+out:
+ return ret;
+}
15 years, 9 months
[libvirt] PATCH: Prevent libvirtd 100% CPU usage when --timeout is used
by Daniel P. Berrange
A couple of bugs conspired to make libvirtd use 100% CPU usage when the
--timeout option is used. This is the default for qemu:///session instance
which are auto-spawned, so quite annoying!
eg libvirt --timeout=30
- The qemudRunLoop() method was registering a timeout on every
iteration of the loop, when it considered itself inactive,
even if already registered
- virEventInteruptLocked() was looking at eventLoop.leader
and comparing to pthread_self() before anyone used the
event loop. 'leader' was 0 at this point so it thought it
had to wake someone up even though no one was waiting in
the event loop.
The latter bug conspired with the former, so the act of registering
the timeout, caused it to immediately see a wakeup signal on the
following poll. So it did another loop iteration, adding another
timer, getting woken up again, etc 100% cpu follows.
Another minor problem was that when completing an event loop
iteration, we reset eventloop.leader to '0'. It is not safe to
assume that pthread_t is an integer like this. The solution is
to track when something is using the event loop explicitly, and
not rely on the 'leader' variable, unless we know a thread is
active.
Finally we never de-registered the shutdown timeout if a new
client turned up while we were counting down.
This patch addresses all these flaws
Daniel
Index: qemud/event.c
===================================================================
RCS file: /data/cvs/libvirt/qemud/event.c,v
retrieving revision 1.17
diff -u -p -u -r1.17 event.c
--- qemud/event.c 22 Dec 2008 12:55:47 -0000 1.17
+++ qemud/event.c 4 Feb 2009 14:40:09 -0000
@@ -68,6 +68,7 @@ struct virEventTimeout {
/* State for the main event loop */
struct virEventLoop {
pthread_mutex_t lock;
+ int running;
pthread_t leader;
int wakeupfd[2];
int handlesCount;
@@ -521,6 +522,7 @@ int virEventRunOnce(void) {
int ret, timeout, nfds;
virEventLock();
+ eventLoop.running = 1;
eventLoop.leader = pthread_self();
if ((nfds = virEventMakePollFDs(&fds)) < 0) {
virEventUnlock();
@@ -572,7 +574,7 @@ int virEventRunOnce(void) {
return -1;
}
- eventLoop.leader = 0;
+ eventLoop.running = 0;
virEventUnlock();
return 0;
}
@@ -611,7 +613,9 @@ int virEventInit(void)
static int virEventInterruptLocked(void)
{
char c = '\0';
- if (pthread_self() == eventLoop.leader)
+
+ if (!eventLoop.running ||
+ pthread_self() == eventLoop.leader)
return 0;
if (safewrite(eventLoop.wakeupfd[1], &c, sizeof(c)) != sizeof(c))
Index: qemud/qemud.c
===================================================================
RCS file: /data/cvs/libvirt/qemud/qemud.c,v
retrieving revision 1.138
diff -u -p -u -r1.138 qemud.c
--- qemud/qemud.c 28 Jan 2009 11:31:39 -0000 1.138
+++ qemud/qemud.c 4 Feb 2009 14:40:09 -0000
@@ -2048,9 +2048,18 @@ static void qemudFreeClient(struct qemud
static int qemudRunLoop(struct qemud_server *server) {
int timerid = -1;
int ret = -1, i;
+ int timerActive = 0;
virMutexLock(&server->lock);
+ if (timeout > 0 &&
+ (timerid = virEventAddTimeoutImpl(-1,
+ qemudInactiveTimer,
+ server, NULL)) < 0) {
+ VIR_ERROR0(_("Failed to register shutdown timeout"));
+ return -1;
+ }
+
if (min_workers > max_workers)
max_workers = min_workers;
@@ -2071,11 +2080,21 @@ static int qemudRunLoop(struct qemud_ser
* if any drivers have active state, if not
* shutdown after timeout seconds
*/
- if (timeout > 0 && !virStateActive() && !server->clients) {
- timerid = virEventAddTimeoutImpl(timeout*1000,
- qemudInactiveTimer,
- server, NULL);
- DEBUG("Scheduling shutdown timer %d", timerid);
+ if (timeout > 0) {
+ if (timerActive) {
+ if (server->clients) {
+ DEBUG("Deactivating shutdown timer %d", timerid);
+ virEventUpdateTimeoutImpl(timerid, -1);
+ timerActive = 0;
+ }
+ } else {
+ if (!virStateActive() &&
+ !server->clients) {
+ DEBUG("Activating shutdown timer %d", timerid);
+ virEventUpdateTimeoutImpl(timerid, timeout * 1000);
+ timerActive = 1;
+ }
+ }
}
virMutexUnlock(&server->lock);
--
|: 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 :|
15 years, 9 months
[libvirt] strange bug
by Farkas Levente
hi,
this strange bug always happened with me on centos-5 with virt-manager.
our setup:
- host:
- Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
- Intel S3000AHV
- 8GB RAM
- CentOS-5.2
- kernel-2.6.18-92.1.22.el5 x86_64 64bit
- guest-1:
- CentOS-5.2 - 4 vcpu
- kernel-2.6.18-92.1.22.el5 i386 32bit
- guest-2:
- CentOS-5.2 - 4 vcpu
- kernel-2.6.18-92.1.22.el5 x86_64 64bit
- guest-3:
- Mandrake-9 - 1 vcpu
- kernel-2.4.19.16mdk-1-1mdk 32bit
- guest-4:
- Mandrake-10 - 1 vcpu
- kernel-2.6.14.2-p4-smp 32bit
- guest-5:
- Windows XP Professional 32bit - 2 vcpu
- guest-7:
- Fedora-9 - 4 vcpu
- kernel-2.6.25.14-108.fc9.i686
if i restart libvirt i can start as the first guest the
guest-4/mandrake-10 but if i start any of the other guest i no longer
able to start guest-4.
now the most strange thing happened with guest-7 (fedora-9).
i use these packages build myself:
http://www.lfarkas.org/linux/packages/centos/5/
libvirt-0.6.0-1.x86_64.rpm
libvirt-python-0.6.0-1.x86_64.rpm
virt-manager-0.6.1-1.x86_64.rpm
python-virtinst-0.400.1-1.noarch.rpm
kvm-83-1.x86_64.rpm
and what've got from virt-manager is not too useful for me:
------------------------------------
Traceback (most recent call last):
File "/usr/share/virt-manager/virtManager/engine.py", line 498, in
run_domain
vm.startup()
File "/usr/share/virt-manager/virtManager/domain.py", line 508, in startup
self.vm.create()
File "/usr/lib64/python2.4/site-packages/libvirt.py", line 287, in create
if ret == -1: raise libvirtError ('virDomainCreate() failed', dom=self)
libvirtError: internal error unable to start guest:
------------------------------------
is there any way to find the reason?
thanks.
--
Levente "Si vis pacem para bellum!"
15 years, 9 months
[libvirt] [PATCH] Allow no <source> for bridge interfaces
by john.levon@sun.com
# HG changeset patch
# User john.levon(a)sun.com
# Date 1233586907 28800
# Node ID 68087e3caf39c74797720bde71a8df524ec036c8
# Parent aa50ca252e1419f30eb0e16204b48db42d92c785
Allow no <source> for bridge interfaces
Historically, it was allowed to specify a bridge interface without a
specific bridge source, in which case xend would choose one. Keep
compatible with this behaviour.
Signed-off-by: John Levon <john.levon(a)sun.com>
diff --git a/src/domain_conf.c b/src/domain_conf.c
--- a/src/domain_conf.c
+++ b/src/domain_conf.c
@@ -962,13 +962,10 @@ virDomainNetDefParseXML(virConnectPtr co
break;
case VIR_DOMAIN_NET_TYPE_BRIDGE:
- if (bridge == NULL) {
- virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
- _("No <source> 'dev' attribute specified with <interface type='bridge'/>"));
- goto error;
- }
- def->data.bridge.brname = bridge;
- bridge = NULL;
+ if (bridge != NULL) {
+ def->data.bridge.brname = bridge;
+ bridge = NULL;
+ }
if (script != NULL) {
def->data.bridge.script = script;
script = NULL;
@@ -2920,8 +2917,9 @@ virDomainNetDefFormat(virConnectPtr conn
break;
case VIR_DOMAIN_NET_TYPE_BRIDGE:
- virBufferEscapeString(buf, " <source bridge='%s'/>\n",
- def->data.bridge.brname);
+ if (def->data.bridge.brname)
+ virBufferEscapeString(buf, " <source bridge='%s'/>\n",
+ def->data.bridge.brname);
if (def->data.bridge.ipaddr)
virBufferVSprintf(buf, " <ip address='%s'/>\n",
def->data.bridge.ipaddr);
diff --git a/src/xend_internal.c b/src/xend_internal.c
--- a/src/xend_internal.c
+++ b/src/xend_internal.c
@@ -5149,7 +5149,8 @@ xenDaemonFormatSxprNet(virConnectPtr con
switch (def->type) {
case VIR_DOMAIN_NET_TYPE_BRIDGE:
- virBufferVSprintf(buf, "(bridge '%s')", def->data.bridge.brname);
+ if (def->data.bridge.brname)
+ virBufferVSprintf(buf, "(bridge '%s')", def->data.bridge.brname);
if (def->data.bridge.script)
script = def->data.bridge.script;
15 years, 9 months
[libvirt] Change of CDROM source via virsh
by Radek Hladik
Hi,
I would like to ask what is the simplest way, how to change ISO mounted
in VM's drive. I am using libvirt+kvm+qemu and only way I found is via
virt-manager, but there are some problems with it - it is not working
properly on some Fedora testing guests and it disables the VNC password
for the VM. I'll try to look to the sources of virt-manager if I could
find the cause (I am thinking using dumpxml without SECURE flag) but I
would like to change the ISO via script (i.e. via virsh). There must be
some simple way but I can not find it...
Radek
15 years, 9 months