>From e7b1d3493af13c5fcbcbf43ff179f863fa09bf7f Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Thu, 15 Oct 2009 12:27:52 +0100 Subject: [PATCH 6/8] Add '-c uri' command line option to specify libvirt connection URI. --- docs/man/vhostmd.8 | 6 ++++++ include/util.h | 4 ++++ vhostmd/vhostmd.c | 7 ++++++- vhostmd/virt-util.c | 7 +++++-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/docs/man/vhostmd.8 b/docs/man/vhostmd.8 index 0054fc7..6eca1dd 100644 --- a/docs/man/vhostmd.8 +++ b/docs/man/vhostmd.8 @@ -19,6 +19,12 @@ Verbose messages .B \-d, --no-daemonize Process will not daemonize .TP +.B \-c, --connect +Set the libvirt URI. If unspecified then we connect to the default +libvirt hypervisor. It is recommended that you specify this since +libvirt's method for choosing the default hypervisor can give +unexpected results. +.TP .B \-u, --user Drop root privileges and run as the named non-root user. .TP diff --git a/include/util.h b/include/util.h index bd347bf..65448c9 100644 --- a/include/util.h +++ b/include/util.h @@ -47,6 +47,10 @@ typedef struct _vu_vm char *uuid; } vu_vm; +/* The libvirt URI to connect to (-c argument on the command line). If + * not set, this will be NULL. + */ +extern const char *libvirt_uri; /* * Init logging interface. If running as daemon messages diff --git a/vhostmd/vhostmd.c b/vhostmd/vhostmd.c index 5c1a975..ea7330b 100644 --- a/vhostmd/vhostmd.c +++ b/vhostmd/vhostmd.c @@ -945,6 +945,7 @@ static void usage(const char *argv0) \n\ Options:\n\ -v | --verbose Verbose messages.\n\ + -c | --connect Set the libvirt URI.\n\ -d | --no-daemonize Process will not daemonize - useful for debugging.\n\ -f | --config Configuration file.\n\ -p | --pid-file PID file.\n\ @@ -972,6 +973,7 @@ int main(int argc, char *argv[]) { "config", required_argument, NULL, 'f'}, { "pid-file", required_argument, NULL, 'p'}, { "user", required_argument, NULL, 'u'}, + { "connect", required_argument, NULL, 'c'}, { "help", no_argument, NULL, '?' }, {0, 0, 0, 0} }; @@ -980,7 +982,7 @@ int main(int argc, char *argv[]) int optidx = 0; int c; - c = getopt_long(argc, argv, "df:p:u:v", opts, &optidx); + c = getopt_long(argc, argv, "c:df:p:u:v", opts, &optidx); if (c == -1) break; @@ -1004,6 +1006,9 @@ int main(int argc, char *argv[]) case 'u': user = optarg; break; + case 'c': + libvirt_uri = optarg; + break; case '?': usage(argv[0]); return 2; diff --git a/vhostmd/virt-util.c b/vhostmd/virt-util.c index 8d59aea..1c31305 100644 --- a/vhostmd/virt-util.c +++ b/vhostmd/virt-util.c @@ -28,13 +28,16 @@ static virConnectPtr conn = NULL; +const char *libvirt_uri = NULL; + static int do_connect (void) { if (conn == NULL) { - conn = virConnectOpenReadOnly (NULL); + conn = virConnectOpenReadOnly (libvirt_uri); if (conn == NULL) { - vu_log (VHOSTMD_ERR, "Unable to open libvirt connection"); + vu_log (VHOSTMD_ERR, "Unable to open libvirt connection to %s", + libvirt_uri ? libvirt_uri : "default hypervisor"); return -1; } } -- 1.6.5.rc2