[Libvir] Added 'domuuid' to virsh & --connect command lin arg

Daniel P. Berrange berrange at redhat.com
Fri May 26 15:53:24 UTC 2006


The attached patch makes two changes to the virsh command:

 * Adds a 'domuuid' command to display the printable UUID of a domain
 * Adds a '--connect NAME' argument allowing the name of a hypervisor
   to be passed through to 'virConnect' / 'virConnectReadOnly'

Although libvirt only has a Xen backend at the moment, I'm working on a 
'mock' hypervisor backend to enable unit testing of applications built on
libvirt without requiring the test suite to interface with the real Xen
hypervisor.

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  -=| 
-------------- next part --------------
# HG changeset patch
# User "Daniel P. Berrange <berrange at redhat.com>"
# Node ID 100a72c0d10c8ebc883be6c71855d739246b16f6
# Parent  c5dddba3aa2b75e7fe8ce46a83f47afc559585f9
Added 'domuuid' command and --connect flag

diff -r c5dddba3aa2b -r 100a72c0d10c ChangeLog
--- a/ChangeLog	Mon May 22 13:38:33 2006 +0000
+++ b/ChangeLog	Fri May 26 11:42:35 2006 -0400
@@ -1,3 +1,9 @@ Mon May 22 15:34:20 CEST 2006 Karel Zak 
+Fri May 26 11:40:20 EDT 2006 Daniel P. Berrange <berrange at redhat.com>
+
+	* src/virsh.c: added 'domuuid' command to display printable UUID
+	  string for a domain. Added '--connect' argument to allow the name
+	  of the hypervisor connection passed to virConnect to be set.
+
 Mon May 22 15:34:20 CEST 2006 Karel Zak <kzak at redhat.com>
 
 	* src/virsh.c: added UUID: to the dominfo command, vshPrint() refactoring,
diff -r c5dddba3aa2b -r 100a72c0d10c src/virsh.c
--- a/src/virsh.c	Mon May 22 13:38:33 2006 +0000
+++ b/src/virsh.c	Fri May 26 11:42:35 2006 -0400
@@ -154,6 +154,7 @@ typedef struct __vshCmd {
  * vshControl
  */
 typedef struct __vshControl {
+    char *name;                 /* connection name */
     virConnectPtr conn;         /* connection to hypervisor */
     vshCmd *cmd;                /* the current command */
     char *cmdstr;               /* string with command */
@@ -272,9 +273,9 @@ cmdConnect(vshControl * ctl, vshCmd * cm
         ctl->conn = NULL;
     }
     if (!ro)
-        ctl->conn = virConnectOpen(NULL);
+        ctl->conn = virConnectOpen(ctl->name);
     else
-        ctl->conn = virConnectOpenReadOnly(NULL);
+        ctl->conn = virConnectOpenReadOnly(ctl->name);
 
     if (!ctl->conn)
         vshError(ctl, FALSE, "failed to connect to the hypervisor");
@@ -920,6 +921,44 @@ cmdDomid(vshControl * ctl, vshCmd * cmd)
 }
 
 /*
+ * "domuuid" command
+ */
+static vshCmdInfo info_domuuid[] = {
+    {"syntax", "domuuid <name>"},
+    {"help", "convert a domain name to domain UUID"},
+    {NULL, NULL}
+};
+
+static vshCmdOptDef opts_domuuid[] = {
+    {"name", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name"},
+    {NULL, 0, 0, NULL}
+};
+
+static int
+cmdDomuuid(vshControl * ctl, vshCmd * cmd)
+{
+    char *name = vshCommandOptString(cmd, "name", NULL);
+    virDomainPtr dom;
+    char uuid[37];
+
+    if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+        return FALSE;
+    if (!name)
+        return FALSE;
+
+    dom = virDomainLookupByName(ctl->conn, name);
+    if (dom && virDomainGetUUIDString(dom, uuid) != -1) {
+        vshPrint(ctl, "%s\n", uuid);
+        virDomainFree(dom);
+    } else {
+        vshError(ctl, FALSE, "failed to get domain '%s'", name);
+        return FALSE;
+    }
+    return TRUE;
+}
+
+
+/*
  * "version" command
  */
 static vshCmdInfo info_version[] = {
@@ -1023,6 +1062,7 @@ static vshCmdDef commands[] = {
     {"create", cmdCreate, opts_create, info_create},
     {"destroy", cmdDestroy, opts_destroy, info_destroy},
     {"domid", cmdDomid, opts_domid, info_domid},
+    {"domuuid", cmdDomuuid, opts_domuuid, info_domuuid},
     {"dominfo", cmdDominfo, opts_dominfo, info_dominfo},
     {"domname", cmdDomname, opts_domname, info_domname},
     {"domstate", cmdDomstate, opts_domstate, info_domstate},
@@ -1302,7 +1342,7 @@ vshCommandOptDomain(vshControl * ctl, vs
     if (dom==NULL && strlen(n)==36) {
         vshDebug(ctl, 5, "%s: <%s> tring as domain UUID\n",
                 cmd->def->name, optname);
-        dom = virDomainLookupByUUIDString(ctl->conn, (const unsigned char *) n);
+        dom = virDomainLookupByUUIDString(ctl->conn, n);
     }
     
     /* try it by NAME */
@@ -1715,9 +1755,9 @@ vshInit(vshControl * ctl)
 
     /* basic connection to hypervisor */
     if (ctl->uid == 0)
-        ctl->conn = virConnectOpen(NULL);
+        ctl->conn = virConnectOpen(ctl->name);
     else
-        ctl->conn = virConnectOpenReadOnly(NULL);
+        ctl->conn = virConnectOpenReadOnly(ctl->name);
 
     if (!ctl->conn)
         vshError(ctl, TRUE, "failed to connect to the hypervisor");
@@ -1903,6 +1943,7 @@ vshParseArgv(vshControl * ctl, int argc,
         {"quiet", 0, 0, 'q'},
         {"timing", 0, 0, 't'},
         {"version", 0, 0, 'v'},
+        {"connect", 1, 0, 'c'},
         {0, 0, 0, 0}
     };
 
@@ -1956,6 +1997,9 @@ vshParseArgv(vshControl * ctl, int argc,
                 break;
             case 't':
                 ctl->timing = TRUE;
+                break;
+            case 'c':
+                ctl->name = strdup(optarg);
                 break;
             case 'v':
                 fprintf(stdout, "%s\n", VERSION);


More information about the libvir-list mailing list