Re: [Libvir] [patch 1/3] Add UUID generation to qemud
by Richard W.M. Jones
Mark McLoughlin wrote:
> +static int
> +qemudGenerateRandomBytes(unsigned char *buf,
> + int buflen)
> +{
> + int fd;
> +
> + if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
> + return errno;
> +
> + while (buflen > 0) {
> + int n;
> +
> + if ((n = read(fd, buf, buflen)) <= 0) {
> + if (errno == EINTR)
> + continue;
> + close(fd);
> + return n < 0 ? errno : ENODATA;
> + }
> +
> + buf += n;
> + buflen -= n;
> + }
> +
> + close(fd);
> +
> + return 0;
> +}
This function would actually be quite useful as a generally available
function. In the remote patch I have added the following to configure.in:
dnl /dev/urandom
AC_CHECK_FILES([/dev/urandom])
and then in C code I can write:
#if HAVE__DEV_URANDOM /* NB double underscore */
// random blah blah
#else
// pseudorandom blah blah
#endif
Rich.
--
Emerging Technologies, Red Hat http://et.redhat.com/~rjones/
64 Baker Street, London, W1U 7DF Mobile: +44 7866 314 421
"[Negative numbers] darken the very whole doctrines of the equations
and make dark of the things which are in their nature excessively
obvious and simple" (Francis Maseres FRS, mathematician, 1759)
17 years, 10 months
Re: [Libvir] [patch 0/3] Automatically generate UUIDs in qemud
by Richard W.M. Jones
Mark McLoughlin wrote:
> Hey,
> We currently require guests and networks to have a
> UUID defined. This patch automatically generates a UUID
> if one isn't supplied.
>
> This is needed because the default network we install
> won't have a UUID assigned, since we'll want the UUID to be
> different on different hosts.
Good one. The remote patch makes some assumptions that UUID is always
defined too.
Rich.
--
Emerging Technologies, Red Hat http://et.redhat.com/~rjones/
64 Baker Street, London, W1U 7DF Mobile: +44 7866 314 421
"[Negative numbers] darken the very whole doctrines of the equations
and make dark of the things which are in their nature excessively
obvious and simple" (Francis Maseres FRS, mathematician, 1759)
17 years, 10 months
[Libvir] [patch 3/3] Use libuuid to generate UUIDs
by Mark McLoughlin
If libuuid from e2fsprogs is available, we use that to
generate UUIDs.
Signed-off-by: Mark McLoughlin <markmc(a)redhat.com>
Index: libvirt/configure.in
===================================================================
--- libvirt.orig/configure.in
+++ libvirt/configure.in
@@ -189,6 +189,22 @@ dnl
AC_CHECK_HEADERS(linux/param.h linux/sockios.h linux/if_bridge.h linux/if_tun.h,,
AC_MSG_ERROR([You must install kernel-headers in order to compile libvirt]))
+dnl
+dnl check for libuuid
+dnl
+AC_ARG_ENABLE(libuuid,
+ AC_HELP_STRING([--disable-libuuid],
+ [disable support for generating UUIDs using e2fsprogs libuuid [default=no]]),,
+ enable_libuuid=yes)
+
+if test "x$enable_libuuid" == "xyes" ; then
+ AC_CHECK_LIB(uuid, uuid_generate,
+ [AC_CHECK_HEADER(uuid/uuid.h,
+ AC_DEFINE(ENABLE_LIBUUID,, [enable generating UUIDs using libuuid])
+ UUID_LIBS="-luuid" AC_SUBST(UUID_LIBS),
+ AC_MSG_ERROR([You must install libuuid in order to compile libvirt]))])
+fi
+
dnl ==========================================================================
dnl find libxml2 library, borrowed from xmlsec
dnl ==========================================================================
Index: libvirt/qemud/Makefile.am
===================================================================
--- libvirt.orig/qemud/Makefile.am
+++ libvirt/qemud/Makefile.am
@@ -16,7 +16,7 @@ libvirt_qemud_CFLAGS = \
-I$(top_srcdir)/include -I$(top_builddir)/include $(LIBXML_CFLAGS) \
-Wall -Wextra -DLOCAL_STATE_DIR="\"$(localstatedir)\"" \
-DSYSCONF_DIR="\"$(sysconfdir)\""
-libvirt_qemud_LDFLAGS = $(LIBXML_LIBS) $(SYSFS_LIBS)
+libvirt_qemud_LDFLAGS = $(LIBXML_LIBS) $(SYSFS_LIBS) $(UUID_LIBS)
libvirt_qemud_DEPENDENCIES =
libvirt_qemud_LDADD =
Index: libvirt/qemud/uuid.c
===================================================================
--- libvirt.orig/qemud/uuid.c
+++ libvirt/qemud/uuid.c
@@ -23,6 +23,9 @@
#include "uuid.h"
+#ifdef ENABLE_LIBUUID
+#include <uuid/uuid.h>
+#else
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
@@ -34,6 +37,18 @@
#include "protocol.h"
#include "internal.h"
+#endif
+
+#ifdef ENABLE_LIBUUID
+
+int
+qemudGenerateUUID(unsigned char *uuid)
+{
+ uuid_generate(uuid);
+ return 0;
+}
+
+#else /* ENABLE_LIBUUID */
static int
qemudGenerateRandomBytes(unsigned char *buf,
@@ -89,6 +104,8 @@ qemudGenerateUUID(unsigned char *uuid)
return qemudGeneratePseudoRandomBytes(uuid, QEMUD_UUID_RAW_LEN);
}
+#endif /* ENABLE_LIBUUID */
+
int
qemudParseUUID(const char *uuid, unsigned char *rawuuid) {
const char *cur;
--
17 years, 10 months
[Libvir] [patch 2/3] Move qemudParseUUID() to uuid.
by Mark McLoughlin
Seems like the obvious place for it now ...
Signed-off-by: Mark McLoughlin <markmc(a)redhat.com>
Index: libvirt/qemud/conf.c
===================================================================
--- libvirt.orig/qemud/conf.c
+++ libvirt/qemud/conf.c
@@ -47,54 +47,6 @@
#include "iptables.h"
#include "uuid.h"
-static int qemudParseUUID(const char *uuid,
- unsigned char *rawuuid) {
- const char *cur;
- int i;
-
- /*
- * do a liberal scan allowing '-' and ' ' anywhere between character
- * pairs as long as there is 32 of them in the end.
- */
- cur = uuid;
- for (i = 0;i < 16;) {
- rawuuid[i] = 0;
- if (*cur == 0)
- goto error;
- if ((*cur == '-') || (*cur == ' ')) {
- cur++;
- continue;
- }
- if ((*cur >= '0') && (*cur <= '9'))
- rawuuid[i] = *cur - '0';
- else if ((*cur >= 'a') && (*cur <= 'f'))
- rawuuid[i] = *cur - 'a' + 10;
- else if ((*cur >= 'A') && (*cur <= 'F'))
- rawuuid[i] = *cur - 'A' + 10;
- else
- goto error;
- rawuuid[i] *= 16;
- cur++;
- if (*cur == 0)
- goto error;
- if ((*cur >= '0') && (*cur <= '9'))
- rawuuid[i] += *cur - '0';
- else if ((*cur >= 'a') && (*cur <= 'f'))
- rawuuid[i] += *cur - 'a' + 10;
- else if ((*cur >= 'A') && (*cur <= 'F'))
- rawuuid[i] += *cur - 'A' + 10;
- else
- goto error;
- i++;
- cur++;
- }
-
- return 0;
-
- error:
- return -1;
-}
-
/* Free all memory associated with a struct qemud_vm object */
void qemudFreeVMDef(struct qemud_vm_def *def) {
struct qemud_vm_disk_def *disk = def->disks;
Index: libvirt/qemud/uuid.c
===================================================================
--- libvirt.orig/qemud/uuid.c
+++ libvirt/qemud/uuid.c
@@ -89,6 +89,54 @@ qemudGenerateUUID(unsigned char *uuid)
return qemudGeneratePseudoRandomBytes(uuid, QEMUD_UUID_RAW_LEN);
}
+int
+qemudParseUUID(const char *uuid, unsigned char *rawuuid) {
+ const char *cur;
+ int i;
+
+ /*
+ * do a liberal scan allowing '-' and ' ' anywhere between character
+ * pairs as long as there is 32 of them in the end.
+ */
+ cur = uuid;
+ for (i = 0;i < 16;) {
+ rawuuid[i] = 0;
+ if (*cur == 0)
+ goto error;
+ if ((*cur == '-') || (*cur == ' ')) {
+ cur++;
+ continue;
+ }
+ if ((*cur >= '0') && (*cur <= '9'))
+ rawuuid[i] = *cur - '0';
+ else if ((*cur >= 'a') && (*cur <= 'f'))
+ rawuuid[i] = *cur - 'a' + 10;
+ else if ((*cur >= 'A') && (*cur <= 'F'))
+ rawuuid[i] = *cur - 'A' + 10;
+ else
+ goto error;
+ rawuuid[i] *= 16;
+ cur++;
+ if (*cur == 0)
+ goto error;
+ if ((*cur >= '0') && (*cur <= '9'))
+ rawuuid[i] += *cur - '0';
+ else if ((*cur >= 'a') && (*cur <= 'f'))
+ rawuuid[i] += *cur - 'a' + 10;
+ else if ((*cur >= 'A') && (*cur <= 'F'))
+ rawuuid[i] += *cur - 'A' + 10;
+ else
+ goto error;
+ i++;
+ cur++;
+ }
+
+ return 0;
+
+ error:
+ return -1;
+}
+
/*
* Local variables:
* indent-tabs-mode: nil
Index: libvirt/qemud/uuid.h
===================================================================
--- libvirt.orig/qemud/uuid.h
+++ libvirt/qemud/uuid.h
@@ -24,4 +24,7 @@
int qemudGenerateUUID(unsigned char *uuid);
+int qemudParseUUID (const char *uuid,
+ unsigned char *rawuuid);
+
#endif /* __QEMUD_UUID_H__ */
--
17 years, 10 months
[Libvir] [patch 1/3] Add UUID generation to qemud
by Mark McLoughlin
If a guest or network does not have a UUID assigned to it, we
generate a random UUID preferably using /dev/urandom, but falling
back to pseudo-random number generation if that fails.
Signed-off-by: Mark McLoughlin <markmc(a)redhat.com>
Index: libvirt/qemud/Makefile.am
===================================================================
--- libvirt.orig/qemud/Makefile.am
+++ libvirt/qemud/Makefile.am
@@ -9,7 +9,8 @@ libvirt_qemud_SOURCES = qemud.c internal
dispatch.c dispatch.h \
conf.c conf.h \
bridge.c bridge.h \
- iptables.c iptables.h
+ iptables.c iptables.h \
+ uuid.c uuid.h
#-D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED=1 -D_POSIX_C_SOURCE=199506L
libvirt_qemud_CFLAGS = \
-I$(top_srcdir)/include -I$(top_builddir)/include $(LIBXML_CFLAGS) \
Index: libvirt/qemud/conf.c
===================================================================
--- libvirt.orig/qemud/conf.c
+++ libvirt/qemud/conf.c
@@ -45,6 +45,7 @@
#include "conf.h"
#include "driver.h"
#include "iptables.h"
+#include "uuid.h"
static int qemudParseUUID(const char *uuid,
unsigned char *rawuuid) {
@@ -689,11 +690,13 @@ static struct qemud_vm_def *qemudParseXM
obj = xmlXPathEval(BAD_CAST "string(/domain/uuid[1])", ctxt);
if ((obj == NULL) || (obj->type != XPATH_STRING) ||
(obj->stringval == NULL) || (obj->stringval[0] == 0)) {
- /* XXX auto-generate a UUID */
- qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "%s", "missing uuid element");
- goto error;
- }
- if (qemudParseUUID((const char *)obj->stringval, def->uuid) < 0) {
+ int err;
+ if ((err = qemudGenerateUUID(def->uuid))) {
+ qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
+ "Failed to generate UUID: %s", strerror(err));
+ goto error;
+ }
+ } else if (qemudParseUUID((const char *)obj->stringval, def->uuid) < 0) {
qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "%s", "malformed uuid element");
goto error;
}
@@ -1650,11 +1653,13 @@ static struct qemud_network_def *qemudPa
obj = xmlXPathEval(BAD_CAST "string(/network/uuid[1])", ctxt);
if ((obj == NULL) || (obj->type != XPATH_STRING) ||
(obj->stringval == NULL) || (obj->stringval[0] == 0)) {
- /* XXX auto-generate a UUID */
- qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "%s", "missing uuid element");
- goto error;
- }
- if (qemudParseUUID((const char *)obj->stringval, def->uuid) < 0) {
+ int err;
+ if ((err = qemudGenerateUUID(def->uuid))) {
+ qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
+ "Failed to generate UUID: %s", strerror(err));
+ goto error;
+ }
+ } else if (qemudParseUUID((const char *)obj->stringval, def->uuid) < 0) {
qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "%s", "malformed uuid element");
goto error;
}
Index: libvirt/qemud/uuid.c
===================================================================
--- /dev/null
+++ libvirt/qemud/uuid.c
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2007 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
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Authors:
+ * Mark McLoughlin <markmc(a)redhat.com>
+ */
+
+#include "config.h"
+
+#include "uuid.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "protocol.h"
+#include "internal.h"
+
+static int
+qemudGenerateRandomBytes(unsigned char *buf,
+ int buflen)
+{
+ int fd;
+
+ if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
+ return errno;
+
+ while (buflen > 0) {
+ int n;
+
+ if ((n = read(fd, buf, buflen)) <= 0) {
+ if (errno == EINTR)
+ continue;
+ close(fd);
+ return n < 0 ? errno : ENODATA;
+ }
+
+ buf += n;
+ buflen -= n;
+ }
+
+ close(fd);
+
+ return 0;
+}
+
+static int
+qemudGeneratePseudoRandomBytes(unsigned char *buf,
+ int buflen)
+{
+ srand(time(NULL));
+ while (buflen > 0) {
+ *buf = (int) (255.0 * (rand() / (double) RAND_MAX));
+ buflen--;
+ }
+
+ return 0;
+}
+
+int
+qemudGenerateUUID(unsigned char *uuid)
+{
+ int err;
+
+ if ((err = qemudGenerateRandomBytes(uuid, QEMUD_UUID_RAW_LEN)))
+ qemudLog(QEMUD_WARN,
+ "Falling back to pseudorandom UUID, "
+ "failed to generate random bytes: %s", strerror(err));
+
+ return qemudGeneratePseudoRandomBytes(uuid, QEMUD_UUID_RAW_LEN);
+}
+
+/*
+ * Local variables:
+ * indent-tabs-mode: nil
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ */
+
Index: libvirt/qemud/uuid.h
===================================================================
--- /dev/null
+++ libvirt/qemud/uuid.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2007 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
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Authors:
+ * Mark McLoughlin <markmc(a)redhat.com>
+ */
+
+#ifndef __QEMUD_UUID_H__
+#define __QEMUD_UUID_H__
+
+int qemudGenerateUUID(unsigned char *uuid);
+
+#endif /* __QEMUD_UUID_H__ */
--
17 years, 10 months
[Libvir] [patch 0/3] Automatically generate UUIDs in qemud
by Mark McLoughlin
Hey,
We currently require guests and networks to have a
UUID defined. This patch automatically generates a UUID
if one isn't supplied.
This is needed because the default network we install
won't have a UUID assigned, since we'll want the UUID to be
different on different hosts.
Cheers,
Mark.
--
17 years, 10 months
[Libvir] PATCH: Don't auto-set VNC port to 5900+domid
by Daniel P. Berrange
The current code for stuffing the VNC port number into the XML looks in
XenStore for the VNC port number, and if it does not find it there falls
back to using 5900+domid. This is a problem because it leaves open a
race condition where the VNC daemon for a guest may not have started up
yet, and so if an app requests the XML at this time it'll get potentially
bogus port number info. In the best case there will be nothing listening
on the bogus port, in the worst case a completely different domain will
be listening on that port.
This scenario is described here
http://www.redhat.com/archives/et-mgmt-tools/2007-February/msg00115.html
The reason we have the 5900+domid rule in there is to support Xen 3.0.2
or earlier, where the port number was never stored in XenStore. For any
Xen 3.0.3 or later, we should basically never use the 5900+domid fallback
rule. So the attached patch makes it conditional on xendConfigVersion < 2
Regards,
Dan.
--
|=- Red Hat, Engineering, Emerging Technologies, Boston. +1 978 392 2496 -=|
|=- Perl modules: http://search.cpan.org/~danberr/ -=|
|=- Projects: http://freshmeat.net/~danielpb/ -=|
|=- GnuPG: 7D3B9505 F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 -=|
17 years, 10 months
Re: [Libvir] [patch 5/9] Add autostart support to virsh
by Daniel P. Berrange
On Thu, Feb 22, 2007 at 11:04:53AM +0000, Mark McLoughlin wrote:
> Add "autostart" and "net-autostart" commands
>
> Also, cleanup the "list" and "net-list" commands a bit
> and add autostart info to them
I'm wondering whether we need to think about compatability for the virsh
commands. We've previously discussed issues around output format stability
but never really came up against the issue until now. My concern would be
someone grok'ing the 'virsh list' output and now suddenly getting an extra
data item at the end of the line.
I think it's probably sufficient to just add the autostart flag display
to the 'virsh dominfo' command, and not the 'list' command - its not the
kind of data you need to see all that often, so I think we could leave
it out of the 'list' command. If we were to extend the output of 'list'
I reckon I'd prioritise 'memory' and 'vcpu count' above autostart.
Regards,
Dan.
--
|=- Red Hat, Engineering, Emerging Technologies, Boston. +1 978 392 2496 -=|
|=- Perl modules: http://search.cpan.org/~danberr/ -=|
|=- Projects: http://freshmeat.net/~danielpb/ -=|
|=- GnuPG: 7D3B9505 F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 -=|
17 years, 10 months