[libvirt] [PATCH 07/13] vsh: Make use of introduced private data

Erik Skultety eskultet at redhat.com
Mon Jun 29 15:37:41 UTC 2015


---
 tools/virsh-console.c        |   3 +-
 tools/virsh-domain-monitor.c |  23 +++++----
 tools/virsh-domain.c         | 110 +++++++++++++++++++++++++++----------------
 tools/virsh-host.c           |  78 ++++++++++++++++++------------
 tools/virsh-interface.c      |  46 +++++++++++-------
 tools/virsh-network.c        |  36 ++++++++------
 tools/virsh-nodedev.c        |  31 +++++++-----
 tools/virsh-nwfilter.c       |  21 +++++----
 tools/virsh-pool.c           |  44 ++++++++++-------
 tools/virsh-secret.c         |  15 +++---
 tools/virsh-snapshot.c       |  25 +++++-----
 tools/virsh-volume.c         |  17 ++++---
 tools/virsh.c                |  57 +++++++++++++---------
 13 files changed, 311 insertions(+), 195 deletions(-)

diff --git a/tools/virsh-console.c b/tools/virsh-console.c
index 86ba456..c1927c2 100644
--- a/tools/virsh-console.c
+++ b/tools/virsh-console.c
@@ -311,6 +311,7 @@ virshRunConsole(vshControl *ctl,
                 unsigned int flags)
 {
     virConsolePtr con = NULL;
+    virshControlPtr priv = ctl->privData;
     int ret = -1;
 
     struct sigaction old_sigquit;
@@ -341,7 +342,7 @@ virshRunConsole(vshControl *ctl,
     if (VIR_ALLOC(con) < 0)
         goto cleanup;
 
-    con->escapeChar = virshGetEscapeChar(ctl->escapeChar);
+    con->escapeChar = virshGetEscapeChar(priv->escapeChar);
     con->st = virStreamNew(virDomainGetConnect(dom),
                            VIR_STREAM_NONBLOCK);
     if (!con->st)
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 1f53428..0762d6e 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1210,6 +1210,7 @@ cmdDominfo(vshControl *ctl, const vshCmd *cmd)
     unsigned int id;
     char *str, uuid[VIR_UUID_STRING_BUFLEN];
     int has_managed_save = 0;
+    virshControlPtr priv = ctl->privData;
 
     if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
         return false;
@@ -1281,7 +1282,7 @@ cmdDominfo(vshControl *ctl, const vshCmd *cmd)
 
     /* Security model and label information */
     memset(&secmodel, 0, sizeof(secmodel));
-    if (virNodeGetSecurityModel(ctl->conn, &secmodel) == -1) {
+    if (virNodeGetSecurityModel(priv->conn, &secmodel) == -1) {
         if (last_error->code != VIR_ERR_NO_SUPPORT) {
             virDomainFree(dom);
             return false;
@@ -1568,9 +1569,10 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags)
     int state;
     int nsnap;
     int mansave;
+    virshControlPtr priv = ctl->privData;
 
     /* try the list with flags support (0.9.13 and later) */
-    if ((ret = virConnectListAllDomains(ctl->conn, &list->domains,
+    if ((ret = virConnectListAllDomains(priv->conn, &list->domains,
                                         flags)) >= 0) {
         list->ndomains = ret;
         goto finished;
@@ -1588,7 +1590,7 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags)
                                          VIR_CONNECT_LIST_DOMAINS_INACTIVE);
 
         vshResetLibvirtError();
-        if ((ret = virConnectListAllDomains(ctl->conn, &list->domains,
+        if ((ret = virConnectListAllDomains(priv->conn, &list->domains,
                                             newflags)) >= 0) {
             list->ndomains = ret;
             goto filter;
@@ -1607,7 +1609,7 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags)
     /* list active domains, if necessary */
     if (!VSH_MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE) ||
         VSH_MATCH(VIR_CONNECT_LIST_DOMAINS_ACTIVE)) {
-        if ((nids = virConnectNumOfDomains(ctl->conn)) < 0) {
+        if ((nids = virConnectNumOfDomains(priv->conn)) < 0) {
             vshError(ctl, "%s", _("Failed to list active domains"));
             goto cleanup;
         }
@@ -1615,7 +1617,7 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags)
         if (nids) {
             ids = vshMalloc(ctl, sizeof(int) * nids);
 
-            if ((nids = virConnectListDomains(ctl->conn, ids, nids)) < 0) {
+            if ((nids = virConnectListDomains(priv->conn, ids, nids)) < 0) {
                 vshError(ctl, "%s", _("Failed to list active domains"));
                 goto cleanup;
             }
@@ -1624,7 +1626,7 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags)
 
     if (!VSH_MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE) ||
         VSH_MATCH(VIR_CONNECT_LIST_DOMAINS_INACTIVE)) {
-        if ((nnames = virConnectNumOfDefinedDomains(ctl->conn)) < 0) {
+        if ((nnames = virConnectNumOfDefinedDomains(priv->conn)) < 0) {
             vshError(ctl, "%s", _("Failed to list inactive domains"));
             goto cleanup;
         }
@@ -1632,7 +1634,7 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags)
         if (nnames) {
             names = vshMalloc(ctl, sizeof(char *) * nnames);
 
-            if ((nnames = virConnectListDefinedDomains(ctl->conn, names,
+            if ((nnames = virConnectListDefinedDomains(priv->conn, names,
                                                       nnames)) < 0) {
                 vshError(ctl, "%s", _("Failed to list inactive domains"));
                 goto cleanup;
@@ -1645,14 +1647,14 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags)
 
     /* get active domains */
     for (i = 0; i < nids; i++) {
-        if (!(dom = virDomainLookupByID(ctl->conn, ids[i])))
+        if (!(dom = virDomainLookupByID(priv->conn, ids[i])))
             continue;
         list->domains[list->ndomains++] = dom;
     }
 
     /* get inactive domains */
     for (i = 0; i < nnames; i++) {
-        if (!(dom = virDomainLookupByName(ctl->conn, names[i])))
+        if (!(dom = virDomainLookupByName(priv->conn, names[i])))
             continue;
         list->domains[list->ndomains++] = dom;
     }
@@ -2106,6 +2108,7 @@ cmdDomstats(vshControl *ctl, const vshCmd *cmd)
     int flags = 0;
     const vshCmdOpt *opt = NULL;
     bool ret = false;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptBool(cmd, "state"))
         stats |= VIR_DOMAIN_STATS_STATE;
@@ -2175,7 +2178,7 @@ cmdDomstats(vshControl *ctl, const vshCmd *cmd)
                                   flags) < 0)
             goto cleanup;
     } else {
-       if ((virConnectGetAllDomainStats(ctl->conn,
+       if ((virConnectGetAllDomainStats(priv->conn,
                                         stats,
                                         &records,
                                         flags)) < 0)
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index bf53a24..cbe213e 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -71,13 +71,14 @@ virshLookupDomainInternal(vshControl *ctl,
     virDomainPtr dom = NULL;
     int id;
     virCheckFlags(VSH_BYID | VSH_BYUUID | VSH_BYNAME, NULL);
+    virshControlPtr priv = ctl->privData;
 
     /* try it by ID */
     if (flags & VSH_BYID) {
         if (virStrToLong_i(name, NULL, 10, &id) == 0 && id >= 0) {
             vshDebug(ctl, VSH_ERR_DEBUG, "%s: <domain> looks like ID\n",
                      cmdname);
-            dom = virDomainLookupByID(ctl->conn, id);
+            dom = virDomainLookupByID(priv->conn, id);
         }
     }
 
@@ -86,14 +87,14 @@ virshLookupDomainInternal(vshControl *ctl,
         strlen(name) == VIR_UUID_STRING_BUFLEN-1) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <domain> trying as domain UUID\n",
                  cmdname);
-        dom = virDomainLookupByUUIDString(ctl->conn, name);
+        dom = virDomainLookupByUUIDString(priv->conn, name);
     }
 
     /* try it by NAME */
     if (!dom && (flags & VSH_BYNAME)) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <domain> trying as domain NAME\n",
                  cmdname);
-        dom = virDomainLookupByName(ctl->conn, name);
+        dom = virDomainLookupByName(priv->conn, name);
     }
 
     if (!dom)
@@ -1898,6 +1899,7 @@ cmdBlockCommit(vshControl *ctl, const vshCmd *cmd)
     int abort_flags = 0;
     int status = -1;
     int cb_id = -1;
+    virshControlPtr priv = ctl->privData;
 
     blocking |= vshCommandOptBool(cmd, "timeout") || pivot || finish;
     if (blocking) {
@@ -1930,7 +1932,7 @@ cmdBlockCommit(vshControl *ctl, const vshCmd *cmd)
     virConnectDomainEventGenericCallback cb =
         VIR_DOMAIN_EVENT_CALLBACK(virshBlockJobStatusHandler);
 
-    if ((cb_id = virConnectDomainEventRegisterAny(ctl->conn,
+    if ((cb_id = virConnectDomainEventRegisterAny(priv->conn,
                                                   dom,
                                                   VIR_DOMAIN_EVENT_ID_BLOCK_JOB,
                                                   cb,
@@ -2025,7 +2027,7 @@ cmdBlockCommit(vshControl *ctl, const vshCmd *cmd)
     if (blocking)
         sigaction(SIGINT, &old_sig_action, NULL);
     if (cb_id >= 0)
-        virConnectDomainEventDeregisterAny(ctl->conn, cb_id);
+        virConnectDomainEventDeregisterAny(priv->conn, cb_id);
     return ret;
 }
 
@@ -2151,6 +2153,7 @@ cmdBlockCopy(vshControl *ctl, const vshCmd *cmd)
     int nparams = 0;
     int status = -1;
     int cb_id = -1;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0)
         return false;
@@ -2194,7 +2197,7 @@ cmdBlockCopy(vshControl *ctl, const vshCmd *cmd)
     virConnectDomainEventGenericCallback cb =
         VIR_DOMAIN_EVENT_CALLBACK(virshBlockJobStatusHandler);
 
-    if ((cb_id = virConnectDomainEventRegisterAny(ctl->conn,
+    if ((cb_id = virConnectDomainEventRegisterAny(priv->conn,
                                                   dom,
                                                   VIR_DOMAIN_EVENT_ID_BLOCK_JOB,
                                                   cb,
@@ -2376,7 +2379,7 @@ cmdBlockCopy(vshControl *ctl, const vshCmd *cmd)
     if (blocking)
         sigaction(SIGINT, &old_sig_action, NULL);
     if (cb_id >= 0)
-        virConnectDomainEventDeregisterAny(ctl->conn, cb_id);
+        virConnectDomainEventDeregisterAny(priv->conn, cb_id);
     return ret;
 }
 
@@ -2468,6 +2471,7 @@ cmdBlockJob(vshControl *ctl, const vshCmd *cmd)
     const char *path;
     unsigned int flags = 0;
     unsigned long long speed;
+    virshControlPtr priv = ctl->privData;
 
     if (abortMode + infoMode + bandwidth > 1) {
         vshError(ctl, "%s",
@@ -2495,14 +2499,14 @@ cmdBlockJob(vshControl *ctl, const vshCmd *cmd)
 
     /* If bytes were requested, or if raw mode is not forcing a MiB/s
      * query and cache can't prove failure, then query bytes/sec.  */
-    if (bytes || !(raw || ctl->blockJobNoBytes)) {
+    if (bytes || !(raw || priv->blockJobNoBytes)) {
         flags |= VIR_DOMAIN_BLOCK_JOB_INFO_BANDWIDTH_BYTES;
         rc = virDomainGetBlockJobInfo(dom, path, &info, flags);
         if (rc < 0) {
             /* Check for particular errors, let all the rest be fatal. */
             switch (last_error->code) {
             case VIR_ERR_INVALID_ARG:
-                ctl->blockJobNoBytes = true;
+                priv->blockJobNoBytes = true;
                 /* fallthrough */
             case VIR_ERR_OVERFLOW:
                 if (!bytes && !raw) {
@@ -2636,6 +2640,7 @@ cmdBlockPull(vshControl *ctl, const vshCmd *cmd)
     int abort_flags = 0;
     int status = -1;
     int cb_id = -1;
+    virshControlPtr priv = ctl->privData;
 
     if (blocking) {
         if (virshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
@@ -2664,7 +2669,7 @@ cmdBlockPull(vshControl *ctl, const vshCmd *cmd)
     virConnectDomainEventGenericCallback cb =
         VIR_DOMAIN_EVENT_CALLBACK(virshBlockJobStatusHandler);
 
-    if ((cb_id = virConnectDomainEventRegisterAny(ctl->conn,
+    if ((cb_id = virConnectDomainEventRegisterAny(priv->conn,
                                                   dom,
                                                   VIR_DOMAIN_EVENT_ID_BLOCK_JOB,
                                                   cb,
@@ -2736,7 +2741,7 @@ cmdBlockPull(vshControl *ctl, const vshCmd *cmd)
     if (blocking)
         sigaction(SIGINT, &old_sig_action, NULL);
     if (cb_id >= 0)
-        virConnectDomainEventDeregisterAny(ctl->conn, cb_id);
+        virConnectDomainEventDeregisterAny(priv->conn, cb_id);
     return ret;
 }
 
@@ -2849,6 +2854,7 @@ cmdRunConsole(vshControl *ctl, virDomainPtr dom,
 {
     bool ret = false;
     int state;
+    virshControlPtr priv = ctl->privData;
 
     if ((state = virshDomainState(ctl, dom, NULL)) < 0) {
         vshError(ctl, "%s", _("Unable to get domain status"));
@@ -2866,7 +2872,7 @@ cmdRunConsole(vshControl *ctl, virDomainPtr dom,
     }
 
     vshPrintExtra(ctl, _("Connected to domain %s\n"), virDomainGetName(dom));
-    vshPrintExtra(ctl, _("Escape character is %s\n"), ctl->escapeChar);
+    vshPrintExtra(ctl, _("Escape character is %s\n"), priv->escapeChar);
     fflush(stdout);
     if (virshRunConsole(ctl, dom, name, flags) == 0)
         ret = true;
@@ -3562,6 +3568,7 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
     char *pool = NULL;
     size_t i;
     size_t j;
+    virshControlPtr priv = ctl->privData;
 
     ignore_value(vshCommandOptString(ctl, cmd, "storage", &vol_string));
 
@@ -3717,7 +3724,7 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
                     continue;
                 }
 
-                if (!(storagepool = virStoragePoolLookupByName(ctl->conn,
+                if (!(storagepool = virStoragePoolLookupByName(priv->conn,
                                                                pool))) {
                     vshPrint(ctl,
                              _("Storage pool '%s' for volume '%s' not found."),
@@ -3730,7 +3737,7 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
                 virStoragePoolFree(storagepool);
 
             } else {
-               vol.vol = virStorageVolLookupByPath(ctl->conn, source);
+               vol.vol = virStorageVolLookupByPath(priv->conn, source);
             }
 
             if (!vol.vol) {
@@ -4357,6 +4364,7 @@ cmdSaveImageDumpxml(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     unsigned int flags = 0;
     char *xml = NULL;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptBool(cmd, "security-info"))
         flags |= VIR_DOMAIN_XML_SECURE;
@@ -4364,7 +4372,7 @@ cmdSaveImageDumpxml(vshControl *ctl, const vshCmd *cmd)
     if (vshCommandOptStringReq(ctl, cmd, "file", &file) < 0)
         return false;
 
-    xml = virDomainSaveImageGetXMLDesc(ctl->conn, file, flags);
+    xml = virDomainSaveImageGetXMLDesc(priv->conn, file, flags);
     if (!xml)
         goto cleanup;
 
@@ -4419,6 +4427,7 @@ cmdSaveImageDefine(vshControl *ctl, const vshCmd *cmd)
     const char *xmlfile = NULL;
     char *xml = NULL;
     unsigned int flags = 0;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptBool(cmd, "running"))
         flags |= VIR_DOMAIN_SAVE_RUNNING;
@@ -4434,7 +4443,7 @@ cmdSaveImageDefine(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(xmlfile, VIRSH_MAX_XML_FILE, &xml) < 0)
         goto cleanup;
 
-    if (virDomainSaveImageDefineXML(ctl->conn, file, xml, flags) < 0) {
+    if (virDomainSaveImageDefineXML(priv->conn, file, xml, flags) < 0) {
         vshError(ctl, _("Failed to update %s"), file);
         goto cleanup;
     }
@@ -4484,6 +4493,7 @@ cmdSaveImageEdit(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     unsigned int getxml_flags = VIR_DOMAIN_XML_SECURE;
     unsigned int define_flags = 0;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptBool(cmd, "running"))
         define_flags |= VIR_DOMAIN_SAVE_RUNNING;
@@ -4503,7 +4513,7 @@ cmdSaveImageEdit(vshControl *ctl, const vshCmd *cmd)
         return false;
 
 #define EDIT_GET_XML \
-    virDomainSaveImageGetXMLDesc(ctl->conn, file, getxml_flags)
+    virDomainSaveImageGetXMLDesc(priv->conn, file, getxml_flags)
 #define EDIT_NOT_CHANGED                                        \
     do {                                                        \
         vshPrint(ctl, _("Saved image %s XML configuration "     \
@@ -4512,7 +4522,7 @@ cmdSaveImageEdit(vshControl *ctl, const vshCmd *cmd)
         goto edit_cleanup;                                      \
     } while (0)
 #define EDIT_DEFINE \
-    (virDomainSaveImageDefineXML(ctl->conn, file, doc_edited, define_flags) == 0)
+    (virDomainSaveImageDefineXML(priv->conn, file, doc_edited, define_flags) == 0)
 #include "virsh-edit.c"
 
     vshPrint(ctl, _("State file %s edited.\n"), file);
@@ -5002,6 +5012,7 @@ cmdRestore(vshControl *ctl, const vshCmd *cmd)
     unsigned int flags = 0;
     const char *xmlfile = NULL;
     char *xml = NULL;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -5021,8 +5032,8 @@ cmdRestore(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
 
     if (((flags || xml)
-         ? virDomainRestoreFlags(ctl->conn, from, xml, flags)
-         : virDomainRestore(ctl->conn, from)) < 0) {
+         ? virDomainRestoreFlags(priv->conn, from, xml, flags)
+         : virDomainRestore(priv->conn, from)) < 0) {
         vshError(ctl, _("Failed to restore domain from %s"), from);
         goto cleanup;
     }
@@ -5298,6 +5309,7 @@ cmdScreenshot(vshControl *ctl, const vshCmd *cmd)
     bool created = false;
     bool generated = false;
     char *mime = NULL;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", (const char **) &file) < 0)
         return false;
@@ -5308,7 +5320,7 @@ cmdScreenshot(vshControl *ctl, const vshCmd *cmd)
     if (!(dom = virshCommandOptDomain(ctl, cmd, &name)))
         return false;
 
-    if (!(st = virStreamNew(ctl->conn, 0)))
+    if (!(st = virStreamNew(priv->conn, 0)))
         goto cleanup;
 
     mime = virDomainScreenshot(dom, st, screen, flags);
@@ -6264,11 +6276,12 @@ cmdVcpuinfo(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     bool pretty = vshCommandOptBool(cmd, "pretty");
     int n, m;
+    virshControlPtr priv = ctl->privData;
 
     if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
         return false;
 
-    if ((maxcpu = virshNodeGetCPUCount(ctl->conn)) < 0)
+    if ((maxcpu = virshNodeGetCPUCount(priv->conn)) < 0)
         goto cleanup;
 
     if (virDomainGetInfo(dom, &info) != 0)
@@ -6447,6 +6460,7 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
     bool current = vshCommandOptBool(cmd, "current");
     int got_vcpu;
     unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
+    virshControlPtr priv = ctl->privData;
 
     VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
     VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
@@ -6474,7 +6488,7 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
         return false;
     }
 
-    if ((maxcpu = virshNodeGetCPUCount(ctl->conn)) < 0)
+    if ((maxcpu = virshNodeGetCPUCount(priv->conn)) < 0)
         return false;
 
     if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
@@ -6589,6 +6603,7 @@ cmdEmulatorPin(vshControl *ctl, const vshCmd *cmd)
     bool current = vshCommandOptBool(cmd, "current");
     bool query = false; /* Query mode if no cpulist */
     unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
+    virshControlPtr priv = ctl->privData;
 
     VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
     VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
@@ -6610,7 +6625,7 @@ cmdEmulatorPin(vshControl *ctl, const vshCmd *cmd)
     }
     query = !cpulist;
 
-    if ((maxcpu = virshNodeGetCPUCount(ctl->conn)) < 0) {
+    if ((maxcpu = virshNodeGetCPUCount(priv->conn)) < 0) {
         virDomainFree(dom);
         return false;
     }
@@ -6794,6 +6809,7 @@ cmdIOThreadInfo(vshControl *ctl, const vshCmd *cmd)
     size_t i;
     int maxcpu;
     unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
+    virshControlPtr priv = ctl->privData;
 
     VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
     VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
@@ -6806,7 +6822,7 @@ cmdIOThreadInfo(vshControl *ctl, const vshCmd *cmd)
     if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
         return false;
 
-    if ((maxcpu = virshNodeGetCPUCount(ctl->conn)) < 0)
+    if ((maxcpu = virshNodeGetCPUCount(priv->conn)) < 0)
         goto cleanup;
 
     if ((niothreads = virDomainGetIOThreadInfo(dom, &info, flags)) < 0) {
@@ -6894,6 +6910,7 @@ cmdIOThreadPin(vshControl *ctl, const vshCmd *cmd)
     unsigned char *cpumap = NULL;
     int cpumaplen;
     unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
+    virshControlPtr priv = ctl->privData;
 
     VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
     VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
@@ -6914,7 +6931,7 @@ cmdIOThreadPin(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
     }
 
-    if ((maxcpu = virshNodeGetCPUCount(ctl->conn)) < 0)
+    if ((maxcpu = virshNodeGetCPUCount(priv->conn)) < 0)
         goto cleanup;
 
     if (!(cpumap = vshParseCPUList(ctl, &cpumaplen, cpulist, maxcpu)))
@@ -7126,6 +7143,7 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd)
     xmlDocPtr xml = NULL;
     xmlXPathContextPtr ctxt = NULL;
     xmlNodePtr node;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptBool(cmd, "error"))
         flags |= VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE;
@@ -7153,7 +7171,7 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
     }
 
-    result = virConnectCompareCPU(ctl->conn, snippet, flags);
+    result = virConnectCompareCPU(priv->conn, snippet, flags);
 
     switch (result) {
     case VIR_CPU_COMPARE_INCOMPATIBLE:
@@ -7235,6 +7253,7 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd)
     xmlXPathContextPtr ctxt = NULL;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     size_t i;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptBool(cmd, "features"))
         flags |= VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES;
@@ -7277,7 +7296,7 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd)
         }
     }
 
-    result = virConnectBaselineCPU(ctl->conn,
+    result = virConnectBaselineCPU(priv->conn,
                                    (const char **)list, count, flags);
 
     if (result) {
@@ -7551,6 +7570,7 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
     unsigned int flags = 0;
     size_t nfds = 0;
     int *fds = NULL;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -7569,9 +7589,9 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
         flags |= VIR_DOMAIN_START_VALIDATE;
 
     if (nfds)
-        dom = virDomainCreateXMLWithFiles(ctl->conn, buffer, nfds, fds, flags);
+        dom = virDomainCreateXMLWithFiles(priv->conn, buffer, nfds, fds, flags);
     else
-        dom = virDomainCreateXML(ctl->conn, buffer, flags);
+        dom = virDomainCreateXML(priv->conn, buffer, flags);
 
     if (!dom) {
         vshError(ctl, _("Failed to create domain from %s"), from);
@@ -7627,6 +7647,7 @@ cmdDefine(vshControl *ctl, const vshCmd *cmd)
     bool ret = true;
     char *buffer;
     unsigned int flags = 0;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -7638,9 +7659,9 @@ cmdDefine(vshControl *ctl, const vshCmd *cmd)
         return false;
 
     if (flags)
-        dom = virDomainDefineXMLFlags(ctl->conn, buffer, flags);
+        dom = virDomainDefineXMLFlags(priv->conn, buffer, flags);
     else
-        dom = virDomainDefineXML(ctl->conn, buffer);
+        dom = virDomainDefineXML(priv->conn, buffer);
     VIR_FREE(buffer);
 
     if (dom != NULL) {
@@ -9016,6 +9037,7 @@ cmdQemuMonitorEvent(vshControl *ctl, const vshCmd *cmd)
     int timeout = 0;
     const char *event = NULL;
     virshQemuEventData data;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptBool(cmd, "regex"))
         flags |= VIR_CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_REGEX;
@@ -9036,7 +9058,7 @@ cmdQemuMonitorEvent(vshControl *ctl, const vshCmd *cmd)
     if (vshEventStart(ctl, timeout) < 0)
         goto cleanup;
 
-    if ((eventId = virConnectDomainQemuMonitorEventRegister(ctl->conn, dom,
+    if ((eventId = virConnectDomainQemuMonitorEventRegister(priv->conn, dom,
                                                             event,
                                                             vshEventPrint,
                                                             &data, NULL,
@@ -9061,7 +9083,7 @@ cmdQemuMonitorEvent(vshControl *ctl, const vshCmd *cmd)
  cleanup:
     vshEventCleanup(ctl);
     if (eventId >= 0 &&
-        virConnectDomainQemuMonitorEventDeregister(ctl->conn, eventId) < 0)
+        virConnectDomainQemuMonitorEventDeregister(priv->conn, eventId) < 0)
         ret = false;
     if (dom)
         virDomainFree(dom);
@@ -9098,11 +9120,12 @@ cmdQemuAttach(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     unsigned int flags = 0;
     unsigned int pid_value; /* API uses unsigned int, not pid_t */
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptUInt(ctl, cmd, "pid", &pid_value) <= 0)
         goto cleanup;
 
-    if (!(dom = virDomainQemuAttach(ctl->conn, pid_value, flags))) {
+    if (!(dom = virDomainQemuAttach(priv->conn, pid_value, flags))) {
         vshError(ctl, _("Failed to attach to pid %u"), pid_value);
         goto cleanup;
     }
@@ -9289,6 +9312,7 @@ cmdLxcEnterNamespace(vshControl *ctl, const vshCmd *cmd)
     bool setlabel = true;
     virSecurityModelPtr secmodel = NULL;
     virSecurityLabelPtr seclabel = NULL;
+    virshControlPtr priv = ctl->privData;
 
     dom = virshCommandOptDomain(ctl, cmd, NULL);
     if (dom == NULL)
@@ -9322,7 +9346,7 @@ cmdLxcEnterNamespace(vshControl *ctl, const vshCmd *cmd)
             vshError(ctl, "%s", _("Failed to allocate security label"));
             goto cleanup;
         }
-        if (virNodeGetSecurityModel(ctl->conn, secmodel) < 0)
+        if (virNodeGetSecurityModel(priv->conn, secmodel) < 0)
             goto cleanup;
         if (virDomainGetSecurityLabel(dom, seclabel) < 0)
             goto cleanup;
@@ -9495,6 +9519,7 @@ cmdDomXMLFromNative(vshControl *ctl, const vshCmd *cmd)
     char *configData;
     char *xmlData;
     unsigned int flags = 0;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "format", &format) < 0 ||
         vshCommandOptStringReq(ctl, cmd, "config", &configFile) < 0)
@@ -9503,7 +9528,7 @@ cmdDomXMLFromNative(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(configFile, VIRSH_MAX_XML_FILE, &configData) < 0)
         return false;
 
-    xmlData = virConnectDomainXMLFromNative(ctl->conn, format, configData, flags);
+    xmlData = virConnectDomainXMLFromNative(priv->conn, format, configData, flags);
     if (xmlData != NULL) {
         vshPrint(ctl, "%s", xmlData);
         VIR_FREE(xmlData);
@@ -9551,6 +9576,7 @@ cmdDomXMLToNative(vshControl *ctl, const vshCmd *cmd)
     char *configData;
     char *xmlData;
     unsigned int flags = 0;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "format", &format) < 0 ||
         vshCommandOptStringReq(ctl, cmd, "xml", &xmlFile) < 0)
@@ -9559,7 +9585,7 @@ cmdDomXMLToNative(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(xmlFile, VIRSH_MAX_XML_FILE, &xmlData) < 0)
         return false;
 
-    configData = virConnectDomainXMLToNative(ctl->conn, format, xmlData, flags);
+    configData = virConnectDomainXMLToNative(priv->conn, format, xmlData, flags);
     if (configData != NULL) {
         vshPrint(ctl, "%s", configData);
         VIR_FREE(configData);
@@ -11528,6 +11554,7 @@ cmdEdit(vshControl *ctl, const vshCmd *cmd)
     virDomainPtr dom_edited = NULL;
     unsigned int query_flags = VIR_DOMAIN_XML_SECURE | VIR_DOMAIN_XML_INACTIVE;
     unsigned int define_flags = VIR_DOMAIN_DEFINE_VALIDATE;
+    virshControlPtr priv = ctl->privData;
 
     dom = virshCommandOptDomain(ctl, cmd, NULL);
     if (dom == NULL)
@@ -11545,7 +11572,7 @@ cmdEdit(vshControl *ctl, const vshCmd *cmd)
         goto edit_cleanup;                                              \
     } while (0)
 #define EDIT_DEFINE \
-    (dom_edited = virshDomainDefine(ctl->conn, doc_edited, define_flags))
+    (dom_edited = virshDomainDefine(priv->conn, doc_edited, define_flags))
 #define EDIT_RELAX                                      \
     do {                                                \
         define_flags &= ~VIR_DOMAIN_DEFINE_VALIDATE;    \
@@ -12243,6 +12270,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd)
     bool all = vshCommandOptBool(cmd, "all");
     bool loop = vshCommandOptBool(cmd, "loop");
     int count = 0;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptBool(cmd, "list")) {
         for (event = 0; event < VIR_DOMAIN_EVENT_ID_LAST; event++)
@@ -12294,7 +12322,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
 
     for (i = 0; i < (all ? VIR_DOMAIN_EVENT_ID_LAST : 1); i++) {
-        if ((data[i].id = virConnectDomainEventRegisterAny(ctl->conn, dom,
+        if ((data[i].id = virConnectDomainEventRegisterAny(priv->conn, dom,
                                                            all ? i : event,
                                                            data[i].cb->cb,
                                                            &data[i],
@@ -12330,7 +12358,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd)
     if (data) {
         for (i = 0; i < (all ? VIR_DOMAIN_EVENT_ID_LAST : 1); i++) {
             if (data[i].id >= 0 &&
-                virConnectDomainEventDeregisterAny(ctl->conn, data[i].id) < 0)
+                virConnectDomainEventDeregisterAny(priv->conn, data[i].id) < 0)
                 ret = false;
         }
         VIR_FREE(data);
diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index 24341ae..d725ee0 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -57,8 +57,9 @@ static bool
 cmdCapabilities(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
     char *caps;
+    virshControlPtr priv = ctl->privData;
 
-    if ((caps = virConnectGetCapabilities(ctl->conn)) == NULL) {
+    if ((caps = virConnectGetCapabilities(priv->conn)) == NULL) {
         vshError(ctl, "%s", _("failed to get capabilities"));
         return false;
     }
@@ -111,6 +112,7 @@ cmdDomCapabilities(vshControl *ctl, const vshCmd *cmd)
     const char *arch = NULL;
     const char *machine = NULL;
     const unsigned int flags = 0; /* No flags so far */
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "virttype", &virttype) < 0 ||
         vshCommandOptStringReq(ctl, cmd, "emulatorbin", &emulatorbin) < 0 ||
@@ -118,7 +120,7 @@ cmdDomCapabilities(vshControl *ctl, const vshCmd *cmd)
         vshCommandOptStringReq(ctl, cmd, "machine", &machine) < 0)
         return ret;
 
-    caps = virConnectGetDomainCapabilities(ctl->conn, emulatorbin,
+    caps = virConnectGetDomainCapabilities(priv->conn, emulatorbin,
                                            arch, machine, virttype, flags);
     if (!caps) {
         vshError(ctl, "%s", _("failed to get emulator capabilities"));
@@ -173,6 +175,7 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
     char *cap_xml = NULL;
     xmlDocPtr xml = NULL;
     xmlXPathContextPtr ctxt = NULL;
+    virshControlPtr priv = ctl->privData;
 
     VSH_EXCLUSIVE_OPTIONS_VAR(all, cellno);
 
@@ -180,7 +183,7 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
         return false;
 
     if (all) {
-        if (!(cap_xml = virConnectGetCapabilities(ctl->conn))) {
+        if (!(cap_xml = virConnectGetCapabilities(priv->conn))) {
             vshError(ctl, "%s", _("unable to get node capabilities"));
             goto cleanup;
         }
@@ -213,7 +216,7 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
             }
             VIR_FREE(val);
             nodes_id[i] = id;
-            if (virNodeGetCellsFreeMemory(ctl->conn, &(nodes_free[i]),
+            if (virNodeGetCellsFreeMemory(priv->conn, &(nodes_free[i]),
                                           id, 1) != 1) {
                 vshError(ctl, _("failed to get free memory for NUMA node "
                                 "number: %lu"), id);
@@ -231,12 +234,12 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
         vshPrintExtra(ctl, "%5s: %10llu KiB\n", _("Total"), memory/1024);
     } else {
         if (cellno) {
-            if (virNodeGetCellsFreeMemory(ctl->conn, &memory, cell, 1) != 1)
+            if (virNodeGetCellsFreeMemory(priv->conn, &memory, cell, 1) != 1)
                 goto cleanup;
 
             vshPrint(ctl, "%d: %llu KiB\n", cell, (memory/1024));
         } else {
-            if ((memory = virNodeGetFreeMemory(ctl->conn)) == 0)
+            if ((memory = virNodeGetFreeMemory(priv->conn)) == 0)
                 goto cleanup;
 
             vshPrint(ctl, "%s: %llu KiB\n", _("Total"), (memory/1024));
@@ -304,6 +307,7 @@ cmdFreepages(vshControl *ctl, const vshCmd *cmd)
     bool all = vshCommandOptBool(cmd, "all");
     bool cellno = vshCommandOptBool(cmd, "cellno");
     bool pagesz = vshCommandOptBool(cmd, "pagesize");
+    virshControlPtr priv = ctl->privData;
 
     VSH_EXCLUSIVE_OPTIONS_VAR(all, cellno);
 
@@ -312,7 +316,7 @@ cmdFreepages(vshControl *ctl, const vshCmd *cmd)
     kibibytes = VIR_DIV_UP(bytes, 1024);
 
     if (all) {
-        if (!(cap_xml = virConnectGetCapabilities(ctl->conn))) {
+        if (!(cap_xml = virConnectGetCapabilities(priv->conn))) {
             vshError(ctl, "%s", _("unable to get node capabilities"));
             goto cleanup;
         }
@@ -367,7 +371,7 @@ cmdFreepages(vshControl *ctl, const vshCmd *cmd)
             }
             VIR_FREE(val);
 
-            if (virNodeGetFreePages(ctl->conn, npages, pagesize,
+            if (virNodeGetFreePages(priv->conn, npages, pagesize,
                                     cell, 1, counts, 0) < 0)
                 goto cleanup;
 
@@ -403,7 +407,8 @@ cmdFreepages(vshControl *ctl, const vshCmd *cmd)
 
         counts = vshMalloc(ctl, sizeof(*counts));
 
-        if (virNodeGetFreePages(ctl->conn, 1, pagesize, cell, 1, counts, 0) < 0)
+        if (virNodeGetFreePages(priv->conn, 1, pagesize,
+                                cell, 1, counts, 0) < 0)
             goto cleanup;
 
         vshPrint(ctl, "%uKiB: %lld\n", *pagesize, counts[0]);
@@ -475,6 +480,7 @@ cmdAllocpages(vshControl *ctl, const vshCmd *cmd)
     xmlDocPtr xml = NULL;
     xmlXPathContextPtr ctxt = NULL;
     xmlNodePtr *nodes = NULL;
+    virshControlPtr priv = ctl->privData;
 
     VSH_EXCLUSIVE_OPTIONS_VAR(all, cellno);
 
@@ -494,7 +500,7 @@ cmdAllocpages(vshControl *ctl, const vshCmd *cmd)
         unsigned long nodes_cnt;
         size_t i;
 
-        if (!(cap_xml = virConnectGetCapabilities(ctl->conn))) {
+        if (!(cap_xml = virConnectGetCapabilities(priv->conn))) {
             vshError(ctl, "%s", _("unable to get node capabilities"));
             goto cleanup;
         }
@@ -524,12 +530,12 @@ cmdAllocpages(vshControl *ctl, const vshCmd *cmd)
             }
             VIR_FREE(val);
 
-            if (virNodeAllocPages(ctl->conn, 1, pageSizes,
+            if (virNodeAllocPages(priv->conn, 1, pageSizes,
                                   pageCounts, id, 1, flags) < 0)
                 goto cleanup;
         }
     } else {
-        if (virNodeAllocPages(ctl->conn, 1, pageSizes, pageCounts,
+        if (virNodeAllocPages(priv->conn, 1, pageSizes, pageCounts,
                               startCell, cellCount, flags) < 0)
             goto cleanup;
     }
@@ -570,11 +576,12 @@ cmdMaxvcpus(vshControl *ctl, const vshCmd *cmd)
 {
     const char *type = NULL;
     int vcpus;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0)
         return false;
 
-    if ((vcpus = virConnectGetMaxVcpus(ctl->conn, type)) < 0)
+    if ((vcpus = virConnectGetMaxVcpus(priv->conn, type)) < 0)
         return false;
 
     vshPrint(ctl, "%d\n", vcpus);
@@ -599,8 +606,9 @@ static bool
 cmdNodeinfo(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
     virNodeInfo info;
+    virshControlPtr priv = ctl->privData;
 
-    if (virNodeGetInfo(ctl->conn, &info) < 0) {
+    if (virNodeGetInfo(priv->conn, &info) < 0) {
         vshError(ctl, "%s", _("failed to get node information"));
         return false;
     }
@@ -646,8 +654,9 @@ cmdNodeCpuMap(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
     unsigned int online;
     bool pretty = vshCommandOptBool(cmd, "pretty");
     bool ret = false;
+    virshControlPtr priv = ctl->privData;
 
-    cpunum = virNodeGetCPUMap(ctl->conn, &cpumap, &online, 0);
+    cpunum = virNodeGetCPUMap(priv->conn, &cpumap, &online, 0);
     if (cpunum < 0) {
         vshError(ctl, "%s", _("Unable to get cpu map"));
         goto cleanup;
@@ -741,11 +750,12 @@ cmdNodeCpuStats(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     unsigned long long cpu_stats[VIRSH_CPU_LAST] = { 0 };
     bool present[VIRSH_CPU_LAST] = { false };
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptInt(ctl, cmd, "cpu", &cpuNum) < 0)
         return false;
 
-    if (virNodeGetCPUStats(ctl->conn, cpuNum, NULL, &nparams, 0) != 0) {
+    if (virNodeGetCPUStats(priv->conn, cpuNum, NULL, &nparams, 0) != 0) {
         vshError(ctl, "%s",
                  _("Unable to get number of cpu stats"));
         return false;
@@ -759,7 +769,7 @@ cmdNodeCpuStats(vshControl *ctl, const vshCmd *cmd)
     params = vshCalloc(ctl, nparams, sizeof(*params));
 
     for (i = 0; i < 2; i++) {
-        if (virNodeGetCPUStats(ctl->conn, cpuNum, params, &nparams, 0) != 0) {
+        if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, 0) != 0) {
             vshError(ctl, "%s", _("Unable to get node cpu stats"));
             goto cleanup;
         }
@@ -851,12 +861,13 @@ cmdNodeMemStats(vshControl *ctl, const vshCmd *cmd)
     int cellNum = VIR_NODE_MEMORY_STATS_ALL_CELLS;
     virNodeMemoryStatsPtr params = NULL;
     bool ret = false;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptInt(ctl, cmd, "cell", &cellNum) < 0)
         return false;
 
     /* get the number of memory parameters */
-    if (virNodeGetMemoryStats(ctl->conn, cellNum, NULL, &nparams, 0) != 0) {
+    if (virNodeGetMemoryStats(priv->conn, cellNum, NULL, &nparams, 0) != 0) {
         vshError(ctl, "%s",
                  _("Unable to get number of memory stats"));
         goto cleanup;
@@ -870,7 +881,7 @@ cmdNodeMemStats(vshControl *ctl, const vshCmd *cmd)
 
     /* now go get all the memory parameters */
     params = vshCalloc(ctl, nparams, sizeof(*params));
-    if (virNodeGetMemoryStats(ctl->conn, cellNum, params, &nparams, 0) != 0) {
+    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, 0) != 0) {
         vshError(ctl, "%s", _("Unable to get memory stats"));
         goto cleanup;
     }
@@ -920,6 +931,7 @@ cmdNodeSuspend(vshControl *ctl, const vshCmd *cmd)
     const char *target = NULL;
     unsigned int suspendTarget;
     long long duration;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "target", &target) < 0)
         return false;
@@ -943,7 +955,7 @@ cmdNodeSuspend(vshControl *ctl, const vshCmd *cmd)
         return false;
     }
 
-    if (virNodeSuspendForDuration(ctl->conn, suspendTarget, duration, 0) < 0) {
+    if (virNodeSuspendForDuration(priv->conn, suspendTarget, duration, 0) < 0) {
         vshError(ctl, "%s", _("The host was not suspended"));
         return false;
     }
@@ -967,8 +979,9 @@ static bool
 cmdSysinfo(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
     char *sysinfo;
+    virshControlPtr priv = ctl->privData;
 
-    sysinfo = virConnectGetSysinfo(ctl->conn, 0);
+    sysinfo = virConnectGetSysinfo(priv->conn, 0);
     if (sysinfo == NULL) {
         vshError(ctl, "%s", _("failed to get sysinfo"));
         return false;
@@ -997,8 +1010,9 @@ static bool
 cmdHostname(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
     char *hostname;
+    virshControlPtr priv = ctl->privData;
 
-    hostname = virConnectGetHostname(ctl->conn);
+    hostname = virConnectGetHostname(priv->conn);
     if (hostname == NULL) {
         vshError(ctl, "%s", _("failed to get hostname"));
         return false;
@@ -1027,8 +1041,9 @@ static bool
 cmdURI(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
     char *uri;
+    virshControlPtr priv = ctl->privData;
 
-    uri = virConnectGetURI(ctl->conn);
+    uri = virConnectGetURI(priv->conn);
     if (uri == NULL) {
         vshError(ctl, "%s", _("failed to get URI"));
         return false;
@@ -1069,11 +1084,12 @@ cmdCPUModelNames(vshControl *ctl, const vshCmd *cmd)
     size_t i;
     int nmodels;
     const char *arch = NULL;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "arch", &arch) < 0)
         return false;
 
-    nmodels = virConnectGetCPUModelNames(ctl->conn, arch, &models, 0);
+    nmodels = virConnectGetCPUModelNames(priv->conn, arch, &models, 0);
     if (nmodels < 0) {
         vshError(ctl, "%s", _("failed to get CPU model names"));
         return false;
@@ -1122,8 +1138,9 @@ cmdVersion(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
     unsigned int major;
     unsigned int minor;
     unsigned int rel;
+    virshControlPtr priv = ctl->privData;
 
-    hvType = virConnectGetType(ctl->conn);
+    hvType = virConnectGetType(priv->conn);
     if (hvType == NULL) {
         vshError(ctl, "%s", _("failed to get hypervisor type"));
         return false;
@@ -1156,7 +1173,7 @@ cmdVersion(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
     vshPrint(ctl, _("Using API: %s %d.%d.%d\n"), hvType,
              major, minor, rel);
 
-    ret = virConnectGetVersion(ctl->conn, &hvVersion);
+    ret = virConnectGetVersion(priv->conn, &hvVersion);
     if (ret < 0) {
         vshError(ctl, "%s", _("failed to get the hypervisor version"));
         return false;
@@ -1175,7 +1192,7 @@ cmdVersion(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
     }
 
     if (vshCommandOptBool(cmd, "daemon")) {
-        ret = virConnectGetLibVersion(ctl->conn, &daemonVersion);
+        ret = virConnectGetLibVersion(priv->conn, &daemonVersion);
         if (ret < 0) {
             vshError(ctl, "%s", _("failed to get the daemon version"));
         } else {
@@ -1228,6 +1245,7 @@ cmdNodeMemoryTune(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     int rc = -1;
     size_t i;
+    virshControlPtr priv = ctl->privData;
 
     if ((rc = vshCommandOptUInt(ctl, cmd, "shm-pages-to-scan", &value)) < 0) {
         goto cleanup;
@@ -1258,7 +1276,7 @@ cmdNodeMemoryTune(vshControl *ctl, const vshCmd *cmd)
 
     if (nparams == 0) {
         /* Get the number of memory parameters */
-        if (virNodeGetMemoryParameters(ctl->conn, NULL, &nparams, flags) != 0) {
+        if (virNodeGetMemoryParameters(priv->conn, NULL, &nparams, flags) != 0) {
             vshError(ctl, "%s",
                      _("Unable to get number of memory parameters"));
             goto cleanup;
@@ -1271,7 +1289,7 @@ cmdNodeMemoryTune(vshControl *ctl, const vshCmd *cmd)
 
         /* Now go get all the memory parameters */
         params = vshCalloc(ctl, nparams, sizeof(*params));
-        if (virNodeGetMemoryParameters(ctl->conn, params, &nparams, flags) != 0) {
+        if (virNodeGetMemoryParameters(priv->conn, params, &nparams, flags) != 0) {
             vshError(ctl, "%s", _("Unable to get memory parameters"));
             goto cleanup;
         }
@@ -1286,7 +1304,7 @@ cmdNodeMemoryTune(vshControl *ctl, const vshCmd *cmd)
             VIR_FREE(str);
         }
     } else {
-        if (virNodeSetMemoryParameters(ctl->conn, params, nparams, flags) != 0)
+        if (virNodeSetMemoryParameters(priv->conn, params, nparams, flags) != 0)
             goto error;
     }
 
diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c
index 8b085cd..9a00070 100644
--- a/tools/virsh-interface.c
+++ b/tools/virsh-interface.c
@@ -50,6 +50,7 @@ virshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd,
     bool is_mac = false;
     virMacAddr dummy;
     virCheckFlags(VSH_BYNAME | VSH_BYMAC, NULL);
+    virshControlPtr priv = ctl->privData;
 
     if (!optname)
        optname = "interface";
@@ -70,13 +71,13 @@ virshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd,
     if (!is_mac && (flags & VSH_BYNAME)) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as interface NAME\n",
                  cmd->def->name, optname);
-        iface = virInterfaceLookupByName(ctl->conn, n);
+        iface = virInterfaceLookupByName(priv->conn, n);
 
     /* try it by MAC */
     } else if (is_mac && (flags & VSH_BYMAC)) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as interface MAC\n",
                  cmd->def->name, optname);
-        iface = virInterfaceLookupByMACString(ctl->conn, n);
+        iface = virInterfaceLookupByMACString(priv->conn, n);
     }
 
     if (!iface)
@@ -114,6 +115,7 @@ cmdInterfaceEdit(vshControl *ctl, const vshCmd *cmd)
     virInterfacePtr iface = NULL;
     virInterfacePtr iface_edited = NULL;
     unsigned int flags = VIR_INTERFACE_XML_INACTIVE;
+    virshControlPtr priv = ctl->privData;
 
     iface = virshCommandOptInterface(ctl, cmd, NULL);
     if (iface == NULL)
@@ -128,7 +130,7 @@ cmdInterfaceEdit(vshControl *ctl, const vshCmd *cmd)
         goto edit_cleanup;                                              \
     } while (0)
 #define EDIT_DEFINE \
-    (iface_edited = virInterfaceDefineXML(ctl->conn, doc_edited, 0))
+    (iface_edited = virInterfaceDefineXML(priv->conn, doc_edited, 0))
 #include "virsh-edit.c"
 
     vshPrint(ctl, _("Interface %s XML configuration edited.\n"),
@@ -197,9 +199,10 @@ vshInterfaceListCollect(vshControl *ctl,
     int nActiveIfaces = 0;
     int nInactiveIfaces = 0;
     int nAllIfaces = 0;
+    virshControlPtr priv = ctl->privData;
 
     /* try the list with flags support (0.10.2 and later) */
-    if ((ret = virConnectListAllInterfaces(ctl->conn,
+    if ((ret = virConnectListAllInterfaces(priv->conn,
                                            &list->ifaces,
                                            flags)) >= 0) {
         list->nifaces = ret;
@@ -220,7 +223,7 @@ vshInterfaceListCollect(vshControl *ctl,
     vshResetLibvirtError();
 
     if (flags & VIR_CONNECT_LIST_INTERFACES_ACTIVE) {
-        nActiveIfaces = virConnectNumOfInterfaces(ctl->conn);
+        nActiveIfaces = virConnectNumOfInterfaces(priv->conn);
         if (nActiveIfaces < 0) {
             vshError(ctl, "%s", _("Failed to list active interfaces"));
             goto cleanup;
@@ -228,7 +231,7 @@ vshInterfaceListCollect(vshControl *ctl,
         if (nActiveIfaces) {
             activeNames = vshMalloc(ctl, sizeof(char *) * nActiveIfaces);
 
-            if ((nActiveIfaces = virConnectListInterfaces(ctl->conn, activeNames,
+            if ((nActiveIfaces = virConnectListInterfaces(priv->conn, activeNames,
                                                           nActiveIfaces)) < 0) {
                 vshError(ctl, "%s", _("Failed to list active interfaces"));
                 goto cleanup;
@@ -237,7 +240,7 @@ vshInterfaceListCollect(vshControl *ctl,
     }
 
     if (flags & VIR_CONNECT_LIST_INTERFACES_INACTIVE) {
-        nInactiveIfaces = virConnectNumOfDefinedInterfaces(ctl->conn);
+        nInactiveIfaces = virConnectNumOfDefinedInterfaces(priv->conn);
         if (nInactiveIfaces < 0) {
             vshError(ctl, "%s", _("Failed to list inactive interfaces"));
             goto cleanup;
@@ -246,7 +249,7 @@ vshInterfaceListCollect(vshControl *ctl,
             inactiveNames = vshMalloc(ctl, sizeof(char *) * nInactiveIfaces);
 
             if ((nInactiveIfaces =
-                     virConnectListDefinedInterfaces(ctl->conn, inactiveNames,
+                     virConnectListDefinedInterfaces(priv->conn, inactiveNames,
                                                      nInactiveIfaces)) < 0) {
                 vshError(ctl, "%s", _("Failed to list inactive interfaces"));
                 goto cleanup;
@@ -266,7 +269,7 @@ vshInterfaceListCollect(vshControl *ctl,
 
     /* get active interfaces */
     for (i = 0; i < nActiveIfaces; i++) {
-        if (!(iface = virInterfaceLookupByName(ctl->conn, activeNames[i]))) {
+        if (!(iface = virInterfaceLookupByName(priv->conn, activeNames[i]))) {
             vshResetLibvirtError();
             continue;
         }
@@ -275,7 +278,7 @@ vshInterfaceListCollect(vshControl *ctl,
 
     /* get inactive interfaces */
     for (i = 0; i < nInactiveIfaces; i++) {
-        if (!(iface = virInterfaceLookupByName(ctl->conn, inactiveNames[i]))) {
+        if (!(iface = virInterfaceLookupByName(priv->conn, inactiveNames[i]))) {
             vshResetLibvirtError();
             continue;
         }
@@ -530,6 +533,7 @@ cmdInterfaceDefine(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     bool ret = true;
     char *buffer;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -537,7 +541,7 @@ cmdInterfaceDefine(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    iface = virInterfaceDefineXML(ctl->conn, buffer, 0);
+    iface = virInterfaceDefineXML(priv->conn, buffer, 0);
     VIR_FREE(buffer);
 
     if (iface != NULL) {
@@ -702,7 +706,9 @@ static const vshCmdOptDef opts_interface_begin[] = {
 static bool
 cmdInterfaceBegin(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
-    if (virInterfaceChangeBegin(ctl->conn, 0) < 0) {
+    virshControlPtr priv = ctl->privData;
+
+    if (virInterfaceChangeBegin(priv->conn, 0) < 0) {
         vshError(ctl, "%s", _("Failed to begin network config change transaction"));
         return false;
     }
@@ -731,7 +737,9 @@ static const vshCmdOptDef opts_interface_commit[] = {
 static bool
 cmdInterfaceCommit(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
-    if (virInterfaceChangeCommit(ctl->conn, 0) < 0) {
+    virshControlPtr priv = ctl->privData;
+
+    if (virInterfaceChangeCommit(priv->conn, 0) < 0) {
         vshError(ctl, "%s", _("Failed to commit network config change transaction"));
         return false;
     }
@@ -760,7 +768,9 @@ static const vshCmdOptDef opts_interface_rollback[] = {
 static bool
 cmdInterfaceRollback(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
-    if (virInterfaceChangeRollback(ctl->conn, 0) < 0) {
+    virshControlPtr priv = ctl->privData;
+
+    if (virInterfaceChangeRollback(priv->conn, 0) < 0) {
         vshError(ctl, "%s", _("Failed to rollback network config change transaction"));
         return false;
     }
@@ -823,6 +833,7 @@ cmdInterfaceBridge(vshControl *ctl, const vshCmd *cmd)
     xmlDocPtr xml_doc = NULL;
     xmlXPathContextPtr ctxt = NULL;
     xmlNodePtr top_node, br_node, if_node, cur;
+    virshControlPtr priv = ctl->privData;
 
     /* Get a handle to the original device */
     if (!(if_handle = virshCommandOptInterfaceBy(ctl, cmd, "interface",
@@ -835,7 +846,7 @@ cmdInterfaceBridge(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
 
     /* make sure "new" device doesn't already exist */
-    if ((br_handle = virInterfaceLookupByName(ctl->conn, br_name))) {
+    if ((br_handle = virInterfaceLookupByName(priv->conn, br_name))) {
         vshError(ctl, _("Network device %s already exists"), br_name);
         goto cleanup;
     }
@@ -969,7 +980,7 @@ cmdInterfaceBridge(vshControl *ctl, const vshCmd *cmd)
     /* br_xml is the new interface to define. It will automatically undefine the
      * independent original interface.
      */
-    if (!(br_handle = virInterfaceDefineXML(ctl->conn, (char *) br_xml, 0))) {
+    if (!(br_handle = virInterfaceDefineXML(priv->conn, (char *) br_xml, 0))) {
         vshError(ctl, _("Failed to define new bridge interface %s"),
                  br_name);
         goto cleanup;
@@ -1043,6 +1054,7 @@ cmdInterfaceUnbridge(vshControl *ctl, const vshCmd *cmd)
     xmlDocPtr xml_doc = NULL;
     xmlXPathContextPtr ctxt = NULL;
     xmlNodePtr top_node, if_node, cur;
+    virshControlPtr priv = ctl->privData;
 
     /* Get a handle to the original device */
     if (!(br_handle = virshCommandOptInterfaceBy(ctl, cmd, "bridge",
@@ -1170,7 +1182,7 @@ cmdInterfaceUnbridge(vshControl *ctl, const vshCmd *cmd)
 
     /* if_xml is the new interface to define.
      */
-    if (!(if_handle = virInterfaceDefineXML(ctl->conn, (char *) if_xml, 0))) {
+    if (!(if_handle = virInterfaceDefineXML(priv->conn, (char *) if_xml, 0))) {
         vshError(ctl, _("Failed to define new interface %s"), if_name);
         goto cleanup;
     }
diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 79909d9..4e3eee5 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -41,6 +41,7 @@ virshCommandOptNetworkBy(vshControl *ctl, const vshCmd *cmd,
     const char *n = NULL;
     const char *optname = "network";
     virCheckFlags(VSH_BYUUID | VSH_BYNAME, NULL);
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
         return NULL;
@@ -55,13 +56,13 @@ virshCommandOptNetworkBy(vshControl *ctl, const vshCmd *cmd,
     if ((flags & VSH_BYUUID) && strlen(n) == VIR_UUID_STRING_BUFLEN-1) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as network UUID\n",
                  cmd->def->name, optname);
-        network = virNetworkLookupByUUIDString(ctl->conn, n);
+        network = virNetworkLookupByUUIDString(priv->conn, n);
     }
     /* try it by NAME */
     if (!network && (flags & VSH_BYNAME)) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as network NAME\n",
                  cmd->def->name, optname);
-        network = virNetworkLookupByName(ctl->conn, n);
+        network = virNetworkLookupByName(priv->conn, n);
     }
 
     if (!network)
@@ -155,6 +156,7 @@ cmdNetworkCreate(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     bool ret = true;
     char *buffer;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -162,7 +164,7 @@ cmdNetworkCreate(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    network = virNetworkCreateXML(ctl->conn, buffer);
+    network = virNetworkCreateXML(priv->conn, buffer);
     VIR_FREE(buffer);
 
     if (network != NULL) {
@@ -206,6 +208,7 @@ cmdNetworkDefine(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     bool ret = true;
     char *buffer;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -213,7 +216,7 @@ cmdNetworkDefine(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    network = virNetworkDefineXML(ctl->conn, buffer);
+    network = virNetworkDefineXML(priv->conn, buffer);
     VIR_FREE(buffer);
 
     if (network != NULL) {
@@ -442,9 +445,10 @@ vshNetworkListCollect(vshControl *ctl,
     int nActiveNets = 0;
     int nInactiveNets = 0;
     int nAllNets = 0;
+    virshControlPtr priv = ctl->privData;
 
     /* try the list with flags support (0.10.2 and later) */
-    if ((ret = virConnectListAllNetworks(ctl->conn,
+    if ((ret = virConnectListAllNetworks(priv->conn,
                                          &list->nets,
                                          flags)) >= 0) {
         list->nnets = ret;
@@ -461,7 +465,7 @@ vshNetworkListCollect(vshControl *ctl,
                                          VIR_CONNECT_LIST_NETWORKS_INACTIVE);
 
         vshResetLibvirtError();
-        if ((ret = virConnectListAllNetworks(ctl->conn, &list->nets,
+        if ((ret = virConnectListAllNetworks(priv->conn, &list->nets,
                                              newflags)) >= 0) {
             list->nnets = ret;
             goto filter;
@@ -480,7 +484,7 @@ vshNetworkListCollect(vshControl *ctl,
     /* Get the number of active networks */
     if (!VSH_MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE) ||
         VSH_MATCH(VIR_CONNECT_LIST_NETWORKS_ACTIVE)) {
-        if ((nActiveNets = virConnectNumOfNetworks(ctl->conn)) < 0) {
+        if ((nActiveNets = virConnectNumOfNetworks(priv->conn)) < 0) {
             vshError(ctl, "%s", _("Failed to get the number of active networks"));
             goto cleanup;
         }
@@ -489,7 +493,7 @@ vshNetworkListCollect(vshControl *ctl,
     /* Get the number of inactive networks */
     if (!VSH_MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE) ||
         VSH_MATCH(VIR_CONNECT_LIST_NETWORKS_INACTIVE)) {
-        if ((nInactiveNets = virConnectNumOfDefinedNetworks(ctl->conn)) < 0) {
+        if ((nInactiveNets = virConnectNumOfDefinedNetworks(priv->conn)) < 0) {
             vshError(ctl, "%s", _("Failed to get the number of inactive networks"));
             goto cleanup;
         }
@@ -505,7 +509,7 @@ vshNetworkListCollect(vshControl *ctl,
     /* Retrieve a list of active network names */
     if (!VSH_MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE) ||
         VSH_MATCH(VIR_CONNECT_LIST_NETWORKS_ACTIVE)) {
-        if (virConnectListNetworks(ctl->conn,
+        if (virConnectListNetworks(priv->conn,
                                    names, nActiveNets) < 0) {
             vshError(ctl, "%s", _("Failed to list active networks"));
             goto cleanup;
@@ -515,7 +519,7 @@ vshNetworkListCollect(vshControl *ctl,
     /* Add the inactive networks to the end of the name list */
     if (!VSH_MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE) ||
         VSH_MATCH(VIR_CONNECT_LIST_NETWORKS_ACTIVE)) {
-        if (virConnectListDefinedNetworks(ctl->conn,
+        if (virConnectListDefinedNetworks(priv->conn,
                                           &names[nActiveNets],
                                           nInactiveNets) < 0) {
             vshError(ctl, "%s", _("Failed to list inactive networks"));
@@ -528,14 +532,14 @@ vshNetworkListCollect(vshControl *ctl,
 
     /* get active networks */
     for (i = 0; i < nActiveNets; i++) {
-        if (!(net = virNetworkLookupByName(ctl->conn, names[i])))
+        if (!(net = virNetworkLookupByName(priv->conn, names[i])))
             continue;
         list->nets[list->nnets++] = net;
     }
 
     /* get inactive networks */
     for (i = 0; i < nInactiveNets; i++) {
-        if (!(net = virNetworkLookupByName(ctl->conn, names[i])))
+        if (!(net = virNetworkLookupByName(priv->conn, names[i])))
             continue;
         list->nets[list->nnets++] = net;
     }
@@ -1123,6 +1127,7 @@ cmdNetworkEdit(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     virNetworkPtr network = NULL;
     virNetworkPtr network_edited = NULL;
+    virshControlPtr priv = ctl->privData;
 
     network = virshCommandOptNetwork(ctl, cmd, NULL);
     if (network == NULL)
@@ -1137,7 +1142,7 @@ cmdNetworkEdit(vshControl *ctl, const vshCmd *cmd)
         goto edit_cleanup;                                              \
     } while (0)
 #define EDIT_DEFINE \
-    (network_edited = virNetworkDefineXML(ctl->conn, doc_edited))
+    (network_edited = virNetworkDefineXML(priv->conn, doc_edited))
 #include "virsh-edit.c"
 
     vshPrint(ctl, _("Network %s XML configuration edited.\n"),
@@ -1247,6 +1252,7 @@ cmdNetworkEvent(vshControl *ctl, const vshCmd *cmd)
     vshNetEventData data;
     const char *eventName = NULL;
     int event;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptBool(cmd, "list")) {
         size_t i;
@@ -1278,7 +1284,7 @@ cmdNetworkEvent(vshControl *ctl, const vshCmd *cmd)
     if (vshEventStart(ctl, timeout) < 0)
         goto cleanup;
 
-    if ((eventId = virConnectNetworkEventRegisterAny(ctl->conn, net, event,
+    if ((eventId = virConnectNetworkEventRegisterAny(priv->conn, net, event,
                                                      VIR_NETWORK_EVENT_CALLBACK(vshEventLifecyclePrint),
                                                      &data, NULL)) < 0)
         goto cleanup;
@@ -1301,7 +1307,7 @@ cmdNetworkEvent(vshControl *ctl, const vshCmd *cmd)
  cleanup:
     vshEventCleanup(ctl);
     if (eventId >= 0 &&
-        virConnectNetworkEventDeregisterAny(ctl->conn, eventId) < 0)
+        virConnectNetworkEventDeregisterAny(priv->conn, eventId) < 0)
         ret = false;
     if (net)
         virNetworkFree(net);
diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c
index adf4423..d81afcf 100644
--- a/tools/virsh-nodedev.c
+++ b/tools/virsh-nodedev.c
@@ -65,6 +65,7 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     bool ret = true;
     char *buffer;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -72,7 +73,7 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    dev = virNodeDeviceCreateXML(ctl->conn, buffer, 0);
+    dev = virNodeDeviceCreateXML(priv->conn, buffer, 0);
     VIR_FREE(buffer);
 
     if (dev != NULL) {
@@ -123,6 +124,7 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
     const char *device_value = NULL;
     char **arr = NULL;
     int narr;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
         return false;
@@ -137,9 +139,9 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
         if (!virValidateWWN(arr[0]) || !virValidateWWN(arr[1]))
             goto cleanup;
 
-        dev = virNodeDeviceLookupSCSIHostByWWN(ctl->conn, arr[0], arr[1], 0);
+        dev = virNodeDeviceLookupSCSIHostByWWN(priv->conn, arr[0], arr[1], 0);
     } else {
-        dev = virNodeDeviceLookupByName(ctl->conn, device_value);
+        dev = virNodeDeviceLookupByName(priv->conn, device_value);
     }
 
     if (!dev) {
@@ -227,9 +229,10 @@ vshNodeDeviceListCollect(vshControl *ctl,
     size_t deleted = 0;
     int ndevices = 0;
     char **names = NULL;
+    virshControlPtr priv = ctl->privData;
 
     /* try the list with flags support (0.10.2 and later) */
-    if ((ret = virConnectListAllNodeDevices(ctl->conn,
+    if ((ret = virConnectListAllNodeDevices(priv->conn,
                                             &list->devices,
                                             flags)) >= 0) {
         list->ndevices = ret;
@@ -249,7 +252,7 @@ vshNodeDeviceListCollect(vshControl *ctl,
     /* fall back to old method (0.10.1 and older) */
     vshResetLibvirtError();
 
-    ndevices = virNodeNumOfDevices(ctl->conn, NULL, 0);
+    ndevices = virNodeNumOfDevices(priv->conn, NULL, 0);
     if (ndevices < 0) {
         vshError(ctl, "%s", _("Failed to count node devices"));
         goto cleanup;
@@ -260,7 +263,7 @@ vshNodeDeviceListCollect(vshControl *ctl,
 
     names = vshMalloc(ctl, sizeof(char *) * ndevices);
 
-    ndevices = virNodeListDevices(ctl->conn, NULL, names, ndevices, 0);
+    ndevices = virNodeListDevices(priv->conn, NULL, names, ndevices, 0);
     if (ndevices < 0) {
         vshError(ctl, "%s", _("Failed to list node devices"));
         goto cleanup;
@@ -271,7 +274,7 @@ vshNodeDeviceListCollect(vshControl *ctl,
 
     /* get the node devices */
     for (i = 0; i < ndevices; i++) {
-        if (!(device = virNodeDeviceLookupByName(ctl->conn, names[i])))
+        if (!(device = virNodeDeviceLookupByName(priv->conn, names[i])))
             continue;
         list->devices[list->ndevices++] = device;
     }
@@ -534,6 +537,7 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
     char **arr = NULL;
     int narr;
     bool ret = false;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
          return false;
@@ -548,9 +552,9 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
         if (!virValidateWWN(arr[0]) || !virValidateWWN(arr[1]))
             goto cleanup;
 
-        device = virNodeDeviceLookupSCSIHostByWWN(ctl->conn, arr[0], arr[1], 0);
+        device = virNodeDeviceLookupSCSIHostByWWN(priv->conn, arr[0], arr[1], 0);
     } else {
-        device = virNodeDeviceLookupByName(ctl->conn, device_value);
+        device = virNodeDeviceLookupByName(priv->conn, device_value);
     }
 
     if (!device) {
@@ -606,13 +610,14 @@ cmdNodeDeviceDetach(vshControl *ctl, const vshCmd *cmd)
     const char *driverName = NULL;
     virNodeDevicePtr device;
     bool ret = true;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "device", &name) < 0)
         return false;
 
     ignore_value(vshCommandOptString(ctl, cmd, "driver", &driverName));
 
-    if (!(device = virNodeDeviceLookupByName(ctl->conn, name))) {
+    if (!(device = virNodeDeviceLookupByName(priv->conn, name))) {
         vshError(ctl, _("Could not find matching device '%s'"), name);
         return false;
     }
@@ -666,11 +671,12 @@ cmdNodeDeviceReAttach(vshControl *ctl, const vshCmd *cmd)
     const char *name = NULL;
     virNodeDevicePtr device;
     bool ret = true;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "device", &name) < 0)
         return false;
 
-    if (!(device = virNodeDeviceLookupByName(ctl->conn, name))) {
+    if (!(device = virNodeDeviceLookupByName(priv->conn, name))) {
         vshError(ctl, _("Could not find matching device '%s'"), name);
         return false;
     }
@@ -715,11 +721,12 @@ cmdNodeDeviceReset(vshControl *ctl, const vshCmd *cmd)
     const char *name = NULL;
     virNodeDevicePtr device;
     bool ret = true;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "device", &name) < 0)
         return false;
 
-    if (!(device = virNodeDeviceLookupByName(ctl->conn, name))) {
+    if (!(device = virNodeDeviceLookupByName(priv->conn, name))) {
         vshError(ctl, _("Could not find matching device '%s'"), name);
         return false;
     }
diff --git a/tools/virsh-nwfilter.c b/tools/virsh-nwfilter.c
index d2c8a79..8c64ac4 100644
--- a/tools/virsh-nwfilter.c
+++ b/tools/virsh-nwfilter.c
@@ -39,6 +39,8 @@ virshCommandOptNWFilterBy(vshControl *ctl, const vshCmd *cmd,
     virNWFilterPtr nwfilter = NULL;
     const char *n = NULL;
     const char *optname = "nwfilter";
+    virshControlPtr priv = ctl->privData;
+
     virCheckFlags(VSH_BYUUID | VSH_BYNAME, NULL);
 
     if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
@@ -54,13 +56,13 @@ virshCommandOptNWFilterBy(vshControl *ctl, const vshCmd *cmd,
     if ((flags & VSH_BYUUID) && strlen(n) == VIR_UUID_STRING_BUFLEN-1) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as nwfilter UUID\n",
                  cmd->def->name, optname);
-        nwfilter = virNWFilterLookupByUUIDString(ctl->conn, n);
+        nwfilter = virNWFilterLookupByUUIDString(priv->conn, n);
     }
     /* try it by NAME */
     if (!nwfilter && (flags & VSH_BYNAME)) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as nwfilter NAME\n",
                  cmd->def->name, optname);
-        nwfilter = virNWFilterLookupByName(ctl->conn, n);
+        nwfilter = virNWFilterLookupByName(priv->conn, n);
     }
 
     if (!nwfilter)
@@ -98,6 +100,7 @@ cmdNWFilterDefine(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     bool ret = true;
     char *buffer;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -105,7 +108,7 @@ cmdNWFilterDefine(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    nwfilter = virNWFilterDefineXML(ctl->conn, buffer);
+    nwfilter = virNWFilterDefineXML(priv->conn, buffer);
     VIR_FREE(buffer);
 
     if (nwfilter != NULL) {
@@ -255,9 +258,10 @@ vshNWFilterListCollect(vshControl *ctl,
     size_t deleted = 0;
     int nfilters = 0;
     char **names = NULL;
+    virshControlPtr priv = ctl->privData;
 
     /* try the list with flags support (0.10.2 and later) */
-    if ((ret = virConnectListAllNWFilters(ctl->conn,
+    if ((ret = virConnectListAllNWFilters(priv->conn,
                                           &list->filters,
                                           flags)) >= 0) {
         list->nfilters = ret;
@@ -279,7 +283,7 @@ vshNWFilterListCollect(vshControl *ctl,
     /* fall back to old method (0.9.13 and older) */
     vshResetLibvirtError();
 
-    nfilters = virConnectNumOfNWFilters(ctl->conn);
+    nfilters = virConnectNumOfNWFilters(priv->conn);
     if (nfilters < 0) {
         vshError(ctl, "%s", _("Failed to count network filters"));
         goto cleanup;
@@ -290,7 +294,7 @@ vshNWFilterListCollect(vshControl *ctl,
 
     names = vshMalloc(ctl, sizeof(char *) * nfilters);
 
-    nfilters = virConnectListNWFilters(ctl->conn, names, nfilters);
+    nfilters = virConnectListNWFilters(priv->conn, names, nfilters);
     if (nfilters < 0) {
         vshError(ctl, "%s", _("Failed to list network filters"));
         goto cleanup;
@@ -301,7 +305,7 @@ vshNWFilterListCollect(vshControl *ctl,
 
     /* get the network filters */
     for (i = 0; i < nfilters; i++) {
-        if (!(filter = virNWFilterLookupByName(ctl->conn, names[i])))
+        if (!(filter = virNWFilterLookupByName(priv->conn, names[i])))
             continue;
         list->filters[list->nfilters++] = filter;
     }
@@ -406,6 +410,7 @@ cmdNWFilterEdit(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     virNWFilterPtr nwfilter = NULL;
     virNWFilterPtr nwfilter_edited = NULL;
+    virshControlPtr priv = ctl->privData;
 
     nwfilter = virshCommandOptNWFilter(ctl, cmd, NULL);
     if (nwfilter == NULL)
@@ -421,7 +426,7 @@ cmdNWFilterEdit(vshControl *ctl, const vshCmd *cmd)
         goto edit_cleanup;                                      \
     } while (0)
 #define EDIT_DEFINE \
-    (nwfilter_edited = virNWFilterDefineXML(ctl->conn, doc_edited))
+    (nwfilter_edited = virNWFilterDefineXML(priv->conn, doc_edited))
 #include "virsh-edit.c"
 
     vshPrint(ctl, _("Network filter %s XML configuration edited.\n"),
diff --git a/tools/virsh-pool.c b/tools/virsh-pool.c
index 19c1f28..f87b73c 100644
--- a/tools/virsh-pool.c
+++ b/tools/virsh-pool.c
@@ -39,6 +39,8 @@ virshCommandOptPoolBy(vshControl *ctl, const vshCmd *cmd, const char *optname,
 {
     virStoragePoolPtr pool = NULL;
     const char *n = NULL;
+    virshControlPtr priv = ctl->privData;
+
     virCheckFlags(VSH_BYUUID | VSH_BYNAME, NULL);
 
     if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
@@ -54,13 +56,13 @@ virshCommandOptPoolBy(vshControl *ctl, const vshCmd *cmd, const char *optname,
     if ((flags & VSH_BYUUID) && strlen(n) == VIR_UUID_STRING_BUFLEN-1) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as pool UUID\n",
                  cmd->def->name, optname);
-        pool = virStoragePoolLookupByUUIDString(ctl->conn, n);
+        pool = virStoragePoolLookupByUUIDString(priv->conn, n);
     }
     /* try it by NAME */
     if (!pool && (flags & VSH_BYNAME)) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as pool NAME\n",
                  cmd->def->name, optname);
-        pool = virStoragePoolLookupByName(ctl->conn, n);
+        pool = virStoragePoolLookupByName(priv->conn, n);
     }
 
     if (!pool)
@@ -154,6 +156,7 @@ cmdPoolCreate(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     bool ret = true;
     char *buffer;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -161,7 +164,7 @@ cmdPoolCreate(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    pool = virStoragePoolCreateXML(ctl->conn, buffer, 0);
+    pool = virStoragePoolCreateXML(priv->conn, buffer, 0);
     VIR_FREE(buffer);
 
     if (pool != NULL) {
@@ -365,6 +368,7 @@ cmdPoolCreateAs(vshControl *ctl, const vshCmd *cmd)
     const char *name;
     char *xml;
     bool printXML = vshCommandOptBool(cmd, "print-xml");
+    virshControlPtr priv = ctl->privData;
 
     if (!vshBuildPoolXML(ctl, cmd, &name, &xml))
         return false;
@@ -373,7 +377,7 @@ cmdPoolCreateAs(vshControl *ctl, const vshCmd *cmd)
         vshPrint(ctl, "%s", xml);
         VIR_FREE(xml);
     } else {
-        pool = virStoragePoolCreateXML(ctl->conn, xml, 0);
+        pool = virStoragePoolCreateXML(priv->conn, xml, 0);
         VIR_FREE(xml);
 
         if (pool != NULL) {
@@ -417,6 +421,7 @@ cmdPoolDefine(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     bool ret = true;
     char *buffer;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -424,7 +429,7 @@ cmdPoolDefine(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    pool = virStoragePoolDefineXML(ctl->conn, buffer, 0);
+    pool = virStoragePoolDefineXML(priv->conn, buffer, 0);
     VIR_FREE(buffer);
 
     if (pool != NULL) {
@@ -458,6 +463,7 @@ cmdPoolDefineAs(vshControl *ctl, const vshCmd *cmd)
     const char *name;
     char *xml;
     bool printXML = vshCommandOptBool(cmd, "print-xml");
+    virshControlPtr priv = ctl->privData;
 
     if (!vshBuildPoolXML(ctl, cmd, &name, &xml))
         return false;
@@ -466,7 +472,7 @@ cmdPoolDefineAs(vshControl *ctl, const vshCmd *cmd)
         vshPrint(ctl, "%s", xml);
         VIR_FREE(xml);
     } else {
-        pool = virStoragePoolDefineXML(ctl->conn, xml, 0);
+        pool = virStoragePoolDefineXML(priv->conn, xml, 0);
         VIR_FREE(xml);
 
         if (pool != NULL) {
@@ -774,9 +780,10 @@ vshStoragePoolListCollect(vshControl *ctl,
     int nActivePools = 0;
     int nInactivePools = 0;
     int nAllPools = 0;
+    virshControlPtr priv = ctl->privData;
 
     /* try the list with flags support (0.10.2 and later) */
-    if ((ret = virConnectListAllStoragePools(ctl->conn,
+    if ((ret = virConnectListAllStoragePools(priv->conn,
                                              &list->pools,
                                              flags)) >= 0) {
         list->npools = ret;
@@ -792,7 +799,7 @@ vshStoragePoolListCollect(vshControl *ctl,
         unsigned int newflags = flags & (VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE |
                                          VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE);
         vshResetLibvirtError();
-        if ((ret = virConnectListAllStoragePools(ctl->conn, &list->pools,
+        if ((ret = virConnectListAllStoragePools(priv->conn, &list->pools,
                                                  newflags)) >= 0) {
             list->npools = ret;
             goto filter;
@@ -818,7 +825,7 @@ vshStoragePoolListCollect(vshControl *ctl,
     /* Get the number of active pools */
     if (!VSH_MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE) ||
         VSH_MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE)) {
-        if ((nActivePools = virConnectNumOfStoragePools(ctl->conn)) < 0) {
+        if ((nActivePools = virConnectNumOfStoragePools(priv->conn)) < 0) {
             vshError(ctl, "%s", _("Failed to get the number of active pools "));
             goto cleanup;
         }
@@ -827,7 +834,7 @@ vshStoragePoolListCollect(vshControl *ctl,
     /* Get the number of inactive pools */
     if (!VSH_MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE) ||
         VSH_MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE)) {
-        if ((nInactivePools = virConnectNumOfDefinedStoragePools(ctl->conn)) < 0) {
+        if ((nInactivePools = virConnectNumOfDefinedStoragePools(priv->conn)) < 0) {
             vshError(ctl, "%s", _("Failed to get the number of inactive pools"));
             goto cleanup;
         }
@@ -843,7 +850,7 @@ vshStoragePoolListCollect(vshControl *ctl,
     /* Retrieve a list of active storage pool names */
     if (!VSH_MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE) ||
         VSH_MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE)) {
-        if (virConnectListStoragePools(ctl->conn,
+        if (virConnectListStoragePools(priv->conn,
                                        names, nActivePools) < 0) {
             vshError(ctl, "%s", _("Failed to list active pools"));
             goto cleanup;
@@ -853,7 +860,7 @@ vshStoragePoolListCollect(vshControl *ctl,
     /* Add the inactive storage pools to the end of the name list */
     if (!VSH_MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE) ||
         VSH_MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE)) {
-        if (virConnectListDefinedStoragePools(ctl->conn,
+        if (virConnectListDefinedStoragePools(priv->conn,
                                               &names[nActivePools],
                                               nInactivePools) < 0) {
             vshError(ctl, "%s", _("Failed to list inactive pools"));
@@ -866,14 +873,14 @@ vshStoragePoolListCollect(vshControl *ctl,
 
     /* get active pools */
     for (i = 0; i < nActivePools; i++) {
-        if (!(pool = virStoragePoolLookupByName(ctl->conn, names[i])))
+        if (!(pool = virStoragePoolLookupByName(priv->conn, names[i])))
             continue;
         list->pools[list->npools++] = pool;
     }
 
     /* get inactive pools */
     for (i = 0; i < nInactivePools; i++) {
-        if (!(pool = virStoragePoolLookupByName(ctl->conn, names[i])))
+        if (!(pool = virStoragePoolLookupByName(priv->conn, names[i])))
             continue;
         list->pools[list->npools++] = pool;
     }
@@ -1416,6 +1423,7 @@ cmdPoolDiscoverSourcesAs(vshControl * ctl, const vshCmd * cmd ATTRIBUTE_UNUSED)
     char *srcSpec = NULL;
     char *srcList;
     const char *initiator = NULL;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0 ||
         vshCommandOptStringReq(ctl, cmd, "host", &host) < 0 ||
@@ -1453,7 +1461,7 @@ cmdPoolDiscoverSourcesAs(vshControl * ctl, const vshCmd * cmd ATTRIBUTE_UNUSED)
         srcSpec = virBufferContentAndReset(&buf);
     }
 
-    srcList = virConnectFindStoragePoolSources(ctl->conn, type, srcSpec, 0);
+    srcList = virConnectFindStoragePoolSources(priv->conn, type, srcSpec, 0);
     VIR_FREE(srcSpec);
     if (srcList == NULL) {
         vshError(ctl, _("Failed to find any %s pool sources"), type);
@@ -1496,6 +1504,7 @@ cmdPoolDiscoverSources(vshControl * ctl, const vshCmd * cmd ATTRIBUTE_UNUSED)
 {
     const char *type = NULL, *srcSpecFile = NULL;
     char *srcSpec = NULL, *srcList;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0)
         return false;
@@ -1507,7 +1516,7 @@ cmdPoolDiscoverSources(vshControl * ctl, const vshCmd * cmd ATTRIBUTE_UNUSED)
                                       &srcSpec) < 0)
         return false;
 
-    srcList = virConnectFindStoragePoolSources(ctl->conn, type, srcSpec, 0);
+    srcList = virConnectFindStoragePoolSources(priv->conn, type, srcSpec, 0);
     VIR_FREE(srcSpec);
     if (srcList == NULL) {
         vshError(ctl, _("Failed to find any %s pool sources"), type);
@@ -1790,6 +1799,7 @@ cmdPoolEdit(vshControl *ctl, const vshCmd *cmd)
     virStoragePoolPtr pool_edited = NULL;
     unsigned int flags = VIR_STORAGE_XML_INACTIVE;
     char *tmp_desc = NULL;
+    virshControlPtr priv = ctl->privData;
 
     pool = virshCommandOptPool(ctl, cmd, "pool", NULL);
     if (pool == NULL)
@@ -1816,7 +1826,7 @@ cmdPoolEdit(vshControl *ctl, const vshCmd *cmd)
         goto edit_cleanup;                                              \
     } while (0)
 #define EDIT_DEFINE \
-    (pool_edited = virStoragePoolDefineXML(ctl->conn, doc_edited, 0))
+    (pool_edited = virStoragePoolDefineXML(priv->conn, doc_edited, 0))
 #include "virsh-edit.c"
 
     vshPrint(ctl, _("Pool %s XML configuration edited.\n"),
diff --git a/tools/virsh-secret.c b/tools/virsh-secret.c
index d78ef9d..e1fec56 100644
--- a/tools/virsh-secret.c
+++ b/tools/virsh-secret.c
@@ -40,6 +40,7 @@ virshCommandOptSecret(vshControl *ctl, const vshCmd *cmd, const char **name)
     virSecretPtr secret = NULL;
     const char *n = NULL;
     const char *optname = "secret";
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
         return NULL;
@@ -50,7 +51,7 @@ virshCommandOptSecret(vshControl *ctl, const vshCmd *cmd, const char **name)
     if (name != NULL)
         *name = n;
 
-    secret = virSecretLookupByUUIDString(ctl->conn, n);
+    secret = virSecretLookupByUUIDString(priv->conn, n);
 
     if (secret == NULL)
         vshError(ctl, _("failed to get secret '%s'"), n);
@@ -88,6 +89,7 @@ cmdSecretDefine(vshControl *ctl, const vshCmd *cmd)
     virSecretPtr res;
     char uuid[VIR_UUID_STRING_BUFLEN];
     bool ret = false;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -95,7 +97,7 @@ cmdSecretDefine(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    if (!(res = virSecretDefineXML(ctl->conn, buffer, 0))) {
+    if (!(res = virSecretDefineXML(priv->conn, buffer, 0))) {
         vshError(ctl, _("Failed to set attributes from %s"), from);
         goto cleanup;
     }
@@ -383,9 +385,10 @@ vshSecretListCollect(vshControl *ctl,
     size_t deleted = 0;
     int nsecrets = 0;
     char **uuids = NULL;
+    virshControlPtr priv = ctl->privData;
 
     /* try the list with flags support (0.10.2 and later) */
-    if ((ret = virConnectListAllSecrets(ctl->conn,
+    if ((ret = virConnectListAllSecrets(priv->conn,
                                         &list->secrets,
                                         flags)) >= 0) {
         list->nsecrets = ret;
@@ -410,7 +413,7 @@ vshSecretListCollect(vshControl *ctl,
         goto cleanup;
     }
 
-    nsecrets = virConnectNumOfSecrets(ctl->conn);
+    nsecrets = virConnectNumOfSecrets(priv->conn);
     if (nsecrets < 0) {
         vshError(ctl, "%s", _("Failed to count secrets"));
         goto cleanup;
@@ -421,7 +424,7 @@ vshSecretListCollect(vshControl *ctl,
 
     uuids = vshMalloc(ctl, sizeof(char *) * nsecrets);
 
-    nsecrets = virConnectListSecrets(ctl->conn, uuids, nsecrets);
+    nsecrets = virConnectListSecrets(priv->conn, uuids, nsecrets);
     if (nsecrets < 0) {
         vshError(ctl, "%s", _("Failed to list secrets"));
         goto cleanup;
@@ -432,7 +435,7 @@ vshSecretListCollect(vshControl *ctl,
 
     /* get the secrets */
     for (i = 0; i < nsecrets; i++) {
-        if (!(secret = virSecretLookupByUUIDString(ctl->conn, uuids[i])))
+        if (!(secret = virSecretLookupByUUIDString(priv->conn, uuids[i])))
             continue;
         list->secrets[list->nsecrets++] = secret;
     }
diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c
index 5130479..839a322 100644
--- a/tools/virsh-snapshot.c
+++ b/tools/virsh-snapshot.c
@@ -763,11 +763,12 @@ vshGetSnapshotParent(vshControl *ctl, virDomainSnapshotPtr snapshot,
     xmlDocPtr xmldoc = NULL;
     xmlXPathContextPtr ctxt = NULL;
     int ret = -1;
+    virshControlPtr priv = ctl->privData;
 
     *parent_name = NULL;
 
     /* Try new API, since it is faster. */
-    if (!ctl->useSnapshotOld) {
+    if (!priv->useSnapshotOld) {
         parent = virDomainSnapshotGetParent(snapshot, 0);
         if (parent) {
             /* API works, and virDomainSnapshotGetName will succeed */
@@ -781,7 +782,7 @@ vshGetSnapshotParent(vshControl *ctl, virDomainSnapshotPtr snapshot,
             goto cleanup;
         }
         /* API didn't work, fall back to XML scraping. */
-        ctl->useSnapshotOld = true;
+        priv->useSnapshotOld = true;
     }
 
     xml = virDomainSnapshotGetXMLDesc(snapshot, 0);
@@ -914,6 +915,7 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd)
     unsigned int flags;
     int current;
     int metadata;
+    virshControlPtr priv = ctl->privData;
 
     dom = virshCommandOptDomain(ctl, cmd, NULL);
     if (dom == NULL)
@@ -996,7 +998,7 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd)
     /* Children, Descendants.  After this point, the fallback to
      * compute children is too expensive, so we gracefully quit if the
      * APIs don't exist.  */
-    if (ctl->useSnapshotOld) {
+    if (priv->useSnapshotOld) {
         ret = true;
         goto cleanup;
     }
@@ -1108,9 +1110,10 @@ vshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
     int deleted = 0;
     bool filter_fallback = false;
     unsigned int flags = orig_flags;
+    virshControlPtr priv = ctl->privData;
 
     /* Try the interface available in 0.9.13 and newer.  */
-    if (!ctl->useSnapshotOld) {
+    if (!priv->useSnapshotOld) {
         if (from)
             count = virDomainSnapshotListAllChildren(from, &snaps, flags);
         else
@@ -1212,13 +1215,13 @@ vshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
             flags |= VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS;
 
         /* Determine if we can use the new child listing API.  */
-        if (ctl->useSnapshotOld ||
+        if (priv->useSnapshotOld ||
             ((count = virDomainSnapshotNumChildren(from, flags)) < 0 &&
              last_error->code == VIR_ERR_NO_SUPPORT)) {
             /* We can emulate --from.  */
             /* XXX can we also emulate --leaves? */
             vshResetLibvirtError();
-            ctl->useSnapshotOld = true;
+            priv->useSnapshotOld = true;
             flags &= ~VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS;
             goto global;
         }
@@ -1253,7 +1256,7 @@ vshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
     names = vshCalloc(ctl, sizeof(*names), count);
 
     /* Now that we have a count, collect the list.  */
-    if (from && !ctl->useSnapshotOld) {
+    if (from && !priv->useSnapshotOld) {
         if (tree) {
             if (count)
                 count = virDomainSnapshotListChildrenNames(from, names + 1,
@@ -1285,9 +1288,9 @@ vshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
      * --from together put from as the first element without a parent;
      * with the old API we still need to do a post-process filtering
      * based on all parent information.  */
-    if (tree || (from && ctl->useSnapshotOld) || roots) {
-        for (i = (from && !ctl->useSnapshotOld); i < count; i++) {
-            if (from && ctl->useSnapshotOld && STREQ(names[i], fromname)) {
+    if (tree || (from && priv->useSnapshotOld) || roots) {
+        for (i = (from && !priv->useSnapshotOld); i < count; i++) {
+            if (from && priv->useSnapshotOld && STREQ(names[i], fromname)) {
                 start_index = i;
                 if (tree)
                     continue;
@@ -1310,7 +1313,7 @@ vshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
     if (tree)
         goto success;
 
-    if (ctl->useSnapshotOld && descendants) {
+    if (priv->useSnapshotOld && descendants) {
         bool changed = false;
         bool remaining = false;
 
diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c
index 21b7fb0..11b54ce 100644
--- a/tools/virsh-volume.c
+++ b/tools/virsh-volume.c
@@ -51,6 +51,8 @@ virshCommandOptVolBy(vshControl *ctl, const vshCmd *cmd,
     virStorageVolPtr vol = NULL;
     virStoragePoolPtr pool = NULL;
     const char *n = NULL, *p = NULL;
+    virshControlPtr priv = ctl->privData;
+
     virCheckFlags(VSH_BYUUID | VSH_BYNAME, NULL);
 
     if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
@@ -87,13 +89,13 @@ virshCommandOptVolBy(vshControl *ctl, const vshCmd *cmd,
     if (!vol && (flags & VSH_BYUUID)) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as vol key\n",
                  cmd->def->name, optname);
-        vol = virStorageVolLookupByKey(ctl->conn, n);
+        vol = virStorageVolLookupByKey(priv->conn, n);
     }
     /* try it by path */
     if (!vol && (flags & VSH_BYUUID)) {
         vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as vol path\n",
                  cmd->def->name, optname);
-        vol = virStorageVolLookupByPath(ctl->conn, n);
+        vol = virStorageVolLookupByPath(priv->conn, n);
     }
 
     if (!vol) {
@@ -201,6 +203,7 @@ cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd)
     unsigned long long capacity, allocation = 0;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     unsigned long flags = 0;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptBool(cmd, "prealloc-metadata"))
         flags |= VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA;
@@ -265,7 +268,7 @@ cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd)
             vshDebug(ctl, VSH_ERR_DEBUG,
                      "%s: Look up backing store volume '%s' as key\n",
                      cmd->def->name, snapshotStrVol);
-            snapVol = virStorageVolLookupByKey(ctl->conn, snapshotStrVol);
+            snapVol = virStorageVolLookupByKey(priv->conn, snapshotStrVol);
             if (snapVol)
                 vshDebug(ctl, VSH_ERR_DEBUG,
                          "%s: Backing store volume found using '%s' as key\n",
@@ -277,7 +280,7 @@ cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd)
             vshDebug(ctl, VSH_ERR_DEBUG,
                      "%s: Look up backing store volume '%s' as path\n",
                      cmd->def->name, snapshotStrVol);
-            snapVol = virStorageVolLookupByPath(ctl->conn, snapshotStrVol);
+            snapVol = virStorageVolLookupByPath(priv->conn, snapshotStrVol);
             if (snapVol)
                 vshDebug(ctl, VSH_ERR_DEBUG,
                          "%s: Backing store volume found using '%s' as path\n",
@@ -692,6 +695,7 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd)
     virStreamPtr st = NULL;
     const char *name = NULL;
     unsigned long long offset = 0, length = 0;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0)
         return false;
@@ -710,7 +714,7 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
     }
 
-    if (!(st = virStreamNew(ctl->conn, 0))) {
+    if (!(st = virStreamNew(priv->conn, 0))) {
         vshError(ctl, _("cannot create a new stream"));
         goto cleanup;
     }
@@ -797,6 +801,7 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
     const char *name = NULL;
     unsigned long long offset = 0, length = 0;
     bool created = false;
+    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0)
         return false;
@@ -820,7 +825,7 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
         created = true;
     }
 
-    if (!(st = virStreamNew(ctl->conn, 0))) {
+    if (!(st = virStreamNew(priv->conn, 0))) {
         vshError(ctl, _("cannot create a new stream"));
         goto cleanup;
     }
diff --git a/tools/virsh.c b/tools/virsh.c
index 9280b40..404deb8 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -192,10 +192,10 @@ static void
 virshReconnect(vshControl *ctl)
 {
     bool connected = false;
+    virshControlPtr priv = ctl->privData;
 
-    if (ctl->conn) {
+    if (priv->conn) {
         int ret;
-
         connected = true;
 
         virConnectUnregisterCloseCallback(priv->conn, virshCatchDisconnect);
@@ -207,24 +207,24 @@ virshReconnect(vshControl *ctl)
                                   "disconnect from the hypervisor"));
     }
 
-    ctl->conn = virshConnect(ctl, ctl->name, ctl->readonly);
+    priv->conn = virshConnect(ctl, ctl->name, priv->readonly);
 
-    if (!ctl->conn) {
+    if (!priv->conn) {
         if (disconnected)
             vshError(ctl, "%s", _("Failed to reconnect to the hypervisor"));
         else
             vshError(ctl, "%s", _("failed to connect to the hypervisor"));
     } else {
-        if (virConnectRegisterCloseCallback(ctl->conn, virshCatchDisconnect,
+        if (virConnectRegisterCloseCallback(priv->conn, virshCatchDisconnect,
                                             NULL, NULL) < 0)
             vshError(ctl, "%s", _("Unable to register disconnect callback"));
         if (connected)
             vshError(ctl, "%s", _("Reconnected to the hypervisor"));
     }
     disconnected = 0;
-    ctl->useGetInfo = false;
-    ctl->useSnapshotOld = false;
-    ctl->blockJobNoBytes = false;
+    priv->useGetInfo = false;
+    priv->useSnapshotOld = false;
+    priv->blockJobNoBytes = false;
 }
 
 
@@ -260,18 +260,19 @@ cmdConnect(vshControl *ctl, const vshCmd *cmd)
 {
     bool ro = vshCommandOptBool(cmd, "readonly");
     const char *name = NULL;
+    virshControlPtr priv = ctl->privData;
 
-    if (ctl->conn) {
+    if (priv->conn) {
         int ret;
 
-        ret = virConnectClose(ctl->conn);
         virConnectUnregisterCloseCallback(priv->conn, virshCatchDisconnect);
+        ret = virConnectClose(priv->conn);
         if (ret < 0)
             vshError(ctl, "%s", _("Failed to disconnect from the hypervisor"));
         else if (ret > 0)
             vshError(ctl, "%s", _("One or more references were leaked after "
                                   "disconnect from the hypervisor"));
-        ctl->conn = NULL;
+        priv->conn = NULL;
     }
 
     VIR_FREE(ctl->name);
@@ -280,19 +281,19 @@ cmdConnect(vshControl *ctl, const vshCmd *cmd)
 
     ctl->name = vshStrdup(ctl, name);
 
-    ctl->useGetInfo = false;
-    ctl->useSnapshotOld = false;
-    ctl->blockJobNoBytes = false;
-    ctl->readonly = ro;
+    priv->useGetInfo = false;
+    priv->useSnapshotOld = false;
+    priv->blockJobNoBytes = false;
+    priv->readonly = ro;
 
-    ctl->conn = virshConnect(ctl, ctl->name, ctl->readonly);
+    priv->conn = virshConnect(ctl, ctl->name, priv->readonly);
 
-    if (!ctl->conn) {
+    if (!priv->conn) {
         vshError(ctl, "%s", _("Failed to connect to the hypervisor"));
         return false;
     }
 
-    if (virConnectRegisterCloseCallback(ctl->conn, virshCatchDisconnect,
+    if (virConnectRegisterCloseCallback(priv->conn, virshCatchDisconnect,
                                         NULL, NULL) < 0)
         vshError(ctl, "%s", _("Unable to register disconnect callback"));
 
@@ -1267,6 +1268,7 @@ virshParseArgv(vshControl *ctl, int argc, char **argv)
     int arg, len, debug, keepalive;
     size_t i;
     int longindex = -1;
+    virshControlPtr priv = ctl->privData;
     struct option opt[] = {
         {"connect", required_argument, NULL, 'c'},
         {"debug", required_argument, NULL, 'd'},
@@ -1309,7 +1311,7 @@ virshParseArgv(vshControl *ctl, int argc, char **argv)
             if ((len == 2 && *optarg == '^' &&
                  virshAllowedEscapeChar(optarg[1])) ||
                 (len == 1 && *optarg != '^')) {
-                ctl->escapeChar = optarg;
+                priv->escapeChar = optarg;
             } else {
                 vshError(ctl, _("Invalid string '%s' for escape sequence"),
                          optarg);
@@ -1364,7 +1366,7 @@ virshParseArgv(vshControl *ctl, int argc, char **argv)
             ctl->timing = true;
             break;
         case 'r':
-            ctl->readonly = true;
+            priv->readonly = true;
             break;
         case 'v':
             if (STRNEQ_NULLABLE(optarg, "long")) {
@@ -1474,18 +1476,23 @@ static const vshCmdGrp cmdGroups[] = {
     {NULL, NULL, NULL}
 };
 
+static const vshClientHooks hooks = {
+    .connHandler = virshConnectionHandler
+};
+
 int
 main(int argc, char **argv)
 {
     vshControl _ctl, *ctl = &_ctl;
+    virshControl virshCtl;
     const char *defaultConn;
     bool ret = true;
 
     memset(ctl, 0, sizeof(vshControl));
+    memset(&virshCtl, 0, sizeof(virshControl));
     ctl->imode = true;          /* default is interactive mode */
     ctl->log_fd = -1;           /* Initialize log file descriptor */
     ctl->debug = VSH_DEBUG_DEFAULT;
-    ctl->escapeChar = "^]";     /* Same default as telnet */
 
     /* In order to distinguish default from setting to 0 */
     ctl->keepalive_interval = -1;
@@ -1494,6 +1501,14 @@ main(int argc, char **argv)
     ctl->eventPipe[0] = -1;
     ctl->eventPipe[1] = -1;
     ctl->eventTimerId = -1;
+    virshCtl.escapeChar = "^]";     /* Same default as telnet */
+    ctl->privData = &virshCtl;
+
+    if (!(progname = strrchr(argv[0], '/')))
+        progname = argv[0];
+    else
+        progname++;
+    ctl->progname = progname;
 
     if (!setlocale(LC_ALL, "")) {
         perror("setlocale");
-- 
1.9.3




More information about the libvir-list mailing list