[PATCH v3 03/16] Store a virCgroupPtr instance in virLXCDomainObjPrivatePtr

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

Instead of calling virCgroupForDomain every time we need
the virCgrouPtr instance, just do it once at Vm startup
and cache a reference to the object in virLXCDomainObjPrivatePtr
until shutdown of the VM. Removing the virCgroupPtr from
the LXC driver state also means we don't have stale mount
info, if someone mounts the cgroups filesystem after libvirtd
has been started

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 src/lxc/lxc_cgroup.c     |  17 ++-
 src/lxc/lxc_cgroup.h     |   2 +
 src/lxc/lxc_conf.h       |   2 +-
 src/lxc/lxc_controller.c |   2 +-
 src/lxc/lxc_domain.c     |   2 +
 src/lxc/lxc_domain.h     |   3 +
 src/lxc/lxc_driver.c     | 354 +++++++++++++----------------------------------
 src/lxc/lxc_process.c    |  39 +++---
 8 files changed, 144 insertions(+), 277 deletions(-)

diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 33641f8..1bad9ec 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -527,7 +527,6 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def)
 {
     virCgroupPtr driver = NULL;
     virCgroupPtr cgroup = NULL;
-    int ret = -1;
     int rc;
 
     rc = virCgroupForDriver("lxc", &driver, 1, 0, -1);
@@ -545,6 +544,21 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def)
         goto cleanup;
     }
 
+cleanup:
+    virCgroupFree(&driver);
+    return cgroup;
+}
+
+
+virCgroupPtr virLXCCgroupJoin(virDomainDefPtr def)
+{
+    virCgroupPtr cgroup = NULL;
+    int ret = -1;
+    int rc;
+
+    if (!(cgroup = virLXCCgroupCreate(def)))
+        return NULL;
+
     rc = virCgroupAddTask(cgroup, getpid());
     if (rc != 0) {
         virReportSystemError(-rc,
@@ -556,7 +570,6 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def)
     ret = 0;
 
 cleanup:
-    virCgroupFree(&driver);
     if (ret < 0) {
         virCgroupFree(&cgroup);
         return NULL;
diff --git a/src/lxc/lxc_cgroup.h b/src/lxc/lxc_cgroup.h
index 942e0fc..25a427c 100644
--- a/src/lxc/lxc_cgroup.h
+++ b/src/lxc/lxc_cgroup.h
@@ -22,11 +22,13 @@
 #ifndef __VIR_LXC_CGROUP_H__
 # define __VIR_LXC_CGROUP_H__
 
+# include "vircgroup.h"
 # include "domain_conf.h"
 # include "lxc_fuse.h"
 # include "virusb.h"
 
 virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def);
+virCgroupPtr virLXCCgroupJoin(virDomainDefPtr def);
 int virLXCCgroupSetup(virDomainDefPtr def,
                       virCgroupPtr cgroup,
                       virBitmapPtr nodemask);
diff --git a/src/lxc/lxc_conf.h b/src/lxc/lxc_conf.h
index f7aaff0..4332fb9 100644
--- a/src/lxc/lxc_conf.h
+++ b/src/lxc/lxc_conf.h
@@ -32,9 +32,9 @@
 # include "domain_event.h"
 # include "capabilities.h"
 # include "virthread.h"
-# include "vircgroup.h"
 # include "security/security_manager.h"
 # include "configmake.h"
+# include "vircgroup.h"
 # include "virsysinfo.h"
 # include "virusb.h"
 
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index f2462ef..f46f813 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -1434,7 +1434,7 @@ virLXCControllerRun(virLXCControllerPtr ctrl)
     if (virLXCControllerSetupPrivateNS() < 0)
         goto cleanup;
 
-    if (!(cgroup = virLXCCgroupCreate(ctrl->def)))
+    if (!(cgroup = virLXCCgroupJoin(ctrl->def)))
         goto cleanup;
 
     if (virLXCControllerSetupLoopDevices(ctrl) < 0)
diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c
index e7a85ca..31f6e84 100644
--- a/src/lxc/lxc_domain.c
+++ b/src/lxc/lxc_domain.c
@@ -43,6 +43,8 @@ static void virLXCDomainObjPrivateFree(void *data)
 {
     virLXCDomainObjPrivatePtr priv = data;
 
+    virCgroupFree(&priv->cgroup);
+
     VIR_FREE(priv);
 }
 
diff --git a/src/lxc/lxc_domain.h b/src/lxc/lxc_domain.h
index 12753aa..751aece 100644
--- a/src/lxc/lxc_domain.h
+++ b/src/lxc/lxc_domain.h
@@ -23,6 +23,7 @@
 #ifndef __LXC_DOMAIN_H__
 # define __LXC_DOMAIN_H__
 
+# include "vircgroup.h"
 # include "lxc_conf.h"
 # include "lxc_monitor.h"
 
@@ -36,6 +37,8 @@ struct _virLXCDomainObjPrivate {
     bool wantReboot;
 
     pid_t initpid;
+
+    virCgroupPtr cgroup;
 };
 
 extern virDomainXMLPrivateDataCallbacks virLXCDriverPrivateDataCallbacks;
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 2b47f18..ba0fa0a 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -525,8 +525,8 @@ static int lxcDomainGetInfo(virDomainPtr dom,
 {
     virLXCDriverPtr driver = dom->conn->privateData;
     virDomainObjPtr vm;
-    virCgroupPtr cgroup = NULL;
     int ret = -1, rc;
+    virLXCDomainObjPrivatePtr priv;
 
     lxcDriverLock(driver);
     vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
@@ -539,24 +539,20 @@ static int lxcDomainGetInfo(virDomainPtr dom,
         goto cleanup;
     }
 
+    priv = vm->privateData;
+
     info->state = virDomainObjGetState(vm, NULL);
 
-    if (!virDomainObjIsActive(vm) || driver->cgroup == NULL) {
+    if (!virDomainObjIsActive(vm)) {
         info->cpuTime = 0;
         info->memory = vm->def->mem.cur_balloon;
     } else {
-        if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) != 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Unable to get cgroup for %s"), vm->def->name);
-            goto cleanup;
-        }
-
-        if (virCgroupGetCpuacctUsage(cgroup, &(info->cpuTime)) < 0) {
+        if (virCgroupGetCpuacctUsage(priv->cgroup, &(info->cpuTime)) < 0) {
             virReportError(VIR_ERR_OPERATION_FAILED,
                            "%s", _("Cannot read cputime for domain"));
             goto cleanup;
         }
-        if ((rc = virCgroupGetMemoryUsage(cgroup, &(info->memory))) < 0) {
+        if ((rc = virCgroupGetMemoryUsage(priv->cgroup, &(info->memory))) < 0) {
             virReportError(VIR_ERR_OPERATION_FAILED,
                            "%s", _("Cannot read memory usage for domain"));
             if (rc == -ENOENT) {
@@ -574,8 +570,6 @@ static int lxcDomainGetInfo(virDomainPtr dom,
 
 cleanup:
     lxcDriverUnlock(driver);
-    if (cgroup)
-        virCgroupFree(&cgroup);
     if (vm)
         virObjectUnlock(vm);
     return ret;
@@ -706,8 +700,8 @@ cleanup:
 static int lxcDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
     virLXCDriverPtr driver = dom->conn->privateData;
     virDomainObjPtr vm;
-    virCgroupPtr cgroup = NULL;
     int ret = -1;
+    virLXCDomainObjPrivatePtr priv;
 
     lxcDriverLock(driver);
     vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
@@ -719,6 +713,7 @@ static int lxcDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
                        _("No domain with matching uuid '%s'"), uuidstr);
         goto cleanup;
     }
+    priv = vm->privateData;
 
     if (newmem > vm->def->mem.max_balloon) {
         virReportError(VIR_ERR_INVALID_ARG,
@@ -732,19 +727,7 @@ static int lxcDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
         goto cleanup;
     }
 
-    if (driver->cgroup == NULL) {
-        virReportError(VIR_ERR_OPERATION_INVALID,
-                       "%s", _("cgroups must be configured on the host"));
-        goto cleanup;
-    }
-
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Unable to get cgroup for %s"), vm->def->name);
-        goto cleanup;
-    }
-
-    if (virCgroupSetMemory(cgroup, newmem) < 0) {
+    if (virCgroupSetMemory(priv->cgroup, newmem) < 0) {
         virReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("Failed to set memory for domain"));
         goto cleanup;
@@ -755,8 +738,6 @@ static int lxcDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
 cleanup:
     if (vm)
         virObjectUnlock(vm);
-    if (cgroup)
-        virCgroupFree(&cgroup);
     return ret;
 }
 
@@ -768,10 +749,10 @@ lxcDomainSetMemoryParameters(virDomainPtr dom,
 {
     virLXCDriverPtr driver = dom->conn->privateData;
     int i;
-    virCgroupPtr cgroup = NULL;
     virDomainObjPtr vm = NULL;
     int ret = -1;
     int rc;
+    virLXCDomainObjPrivatePtr priv;
 
     virCheckFlags(0, -1);
     if (virTypedParameterArrayValidate(params, nparams,
@@ -794,33 +775,28 @@ lxcDomainSetMemoryParameters(virDomainPtr dom,
                        _("No domain with matching uuid '%s'"), uuidstr);
         goto cleanup;
     }
-
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot find cgroup for domain %s"), vm->def->name);
-        goto cleanup;
-    }
+    priv = vm->privateData;
 
     ret = 0;
     for (i = 0; i < nparams; i++) {
         virTypedParameterPtr param = &params[i];
 
         if (STREQ(param->field, VIR_DOMAIN_MEMORY_HARD_LIMIT)) {
-            rc = virCgroupSetMemoryHardLimit(cgroup, params[i].value.ul);
+            rc = virCgroupSetMemoryHardLimit(priv->cgroup, params[i].value.ul);
             if (rc != 0) {
                 virReportSystemError(-rc, "%s",
                                      _("unable to set memory hard_limit tunable"));
                 ret = -1;
             }
         } else if (STREQ(param->field, VIR_DOMAIN_MEMORY_SOFT_LIMIT)) {
-            rc = virCgroupSetMemorySoftLimit(cgroup, params[i].value.ul);
+            rc = virCgroupSetMemorySoftLimit(priv->cgroup, params[i].value.ul);
             if (rc != 0) {
                 virReportSystemError(-rc, "%s",
                                      _("unable to set memory soft_limit tunable"));
                 ret = -1;
             }
         } else if (STREQ(param->field, VIR_DOMAIN_MEMORY_SWAP_HARD_LIMIT)) {
-            rc = virCgroupSetMemSwapHardLimit(cgroup, params[i].value.ul);
+            rc = virCgroupSetMemSwapHardLimit(priv->cgroup, params[i].value.ul);
             if (rc != 0) {
                 virReportSystemError(-rc, "%s",
                                      _("unable to set swap_hard_limit tunable"));
@@ -830,8 +806,6 @@ lxcDomainSetMemoryParameters(virDomainPtr dom,
     }
 
 cleanup:
-    if (cgroup)
-        virCgroupFree(&cgroup);
     if (vm)
         virObjectUnlock(vm);
     lxcDriverUnlock(driver);
@@ -846,11 +820,11 @@ lxcDomainGetMemoryParameters(virDomainPtr dom,
 {
     virLXCDriverPtr driver = dom->conn->privateData;
     int i;
-    virCgroupPtr cgroup = NULL;
     virDomainObjPtr vm = NULL;
     unsigned long long val;
     int ret = -1;
     int rc;
+    virLXCDomainObjPrivatePtr priv;
 
     virCheckFlags(0, -1);
 
@@ -864,6 +838,7 @@ lxcDomainGetMemoryParameters(virDomainPtr dom,
                        _("No domain with matching uuid '%s'"), uuidstr);
         goto cleanup;
     }
+    priv = vm->privateData;
 
     if ((*nparams) == 0) {
         /* Current number of memory parameters supported by cgroups */
@@ -872,19 +847,13 @@ lxcDomainGetMemoryParameters(virDomainPtr dom,
         goto cleanup;
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Unable to get cgroup for %s"), vm->def->name);
-        goto cleanup;
-    }
-
     for (i = 0; i < LXC_NB_MEM_PARAM && i < *nparams; i++) {
         virTypedParameterPtr param = &params[i];
         val = 0;
 
         switch (i) {
         case 0: /* fill memory hard limit here */
-            rc = virCgroupGetMemoryHardLimit(cgroup, &val);
+            rc = virCgroupGetMemoryHardLimit(priv->cgroup, &val);
             if (rc != 0) {
                 virReportSystemError(-rc, "%s",
                                      _("unable to get memory hard limit"));
@@ -895,7 +864,7 @@ lxcDomainGetMemoryParameters(virDomainPtr dom,
                 goto cleanup;
             break;
         case 1: /* fill memory soft limit here */
-            rc = virCgroupGetMemorySoftLimit(cgroup, &val);
+            rc = virCgroupGetMemorySoftLimit(priv->cgroup, &val);
             if (rc != 0) {
                 virReportSystemError(-rc, "%s",
                                      _("unable to get memory soft limit"));
@@ -906,7 +875,7 @@ lxcDomainGetMemoryParameters(virDomainPtr dom,
                 goto cleanup;
             break;
         case 2: /* fill swap hard limit here */
-            rc = virCgroupGetMemSwapHardLimit(cgroup, &val);
+            rc = virCgroupGetMemSwapHardLimit(priv->cgroup, &val);
             if (rc != 0) {
                 virReportSystemError(-rc, "%s",
                                      _("unable to get swap hard limit"));
@@ -930,8 +899,6 @@ lxcDomainGetMemoryParameters(virDomainPtr dom,
     ret = 0;
 
 cleanup:
-    if (cgroup)
-        virCgroupFree(&cgroup);
     if (vm)
         virObjectUnlock(vm);
     lxcDriverUnlock(driver);
@@ -1414,7 +1381,6 @@ static int lxcStartup(bool privileged,
                       void *opaque ATTRIBUTE_UNUSED)
 {
     char *ld;
-    int rc;
 
     /* Valgrind gets very annoyed when we clone containers, so
      * disable LXC when under valgrind
@@ -1459,16 +1425,6 @@ static int lxcStartup(bool privileged,
     lxc_driver->log_libvirtd = 0; /* by default log to container logfile */
     lxc_driver->have_netns = lxcCheckNetNsSupport();
 
-    rc = virCgroupForDriver("lxc", &lxc_driver->cgroup, privileged, 1, -1);
-    if (rc < 0) {
-        char buf[1024] ATTRIBUTE_UNUSED;
-        VIR_DEBUG("Unable to create cgroup for LXC driver: %s",
-                  virStrerror(-rc, buf, sizeof(buf)));
-        /* Don't abort startup. We will explicitly report to
-         * the user when they try to start a VM
-         */
-    }
-
     /* Call function to load lxc driver configuration information */
     if (lxcLoadDriverConfig(lxc_driver) < 0)
         goto cleanup;
@@ -1639,30 +1595,32 @@ cleanup:
 }
 
 
-static bool lxcCgroupControllerActive(virLXCDriverPtr driver,
-                                      int controller)
-{
-    return virCgroupHasController(driver->cgroup, controller);
-}
-
-
-
-static char *lxcGetSchedulerType(virDomainPtr domain,
+static char *lxcGetSchedulerType(virDomainPtr dom,
                                  int *nparams)
 {
-    virLXCDriverPtr driver = domain->conn->privateData;
+    virLXCDriverPtr driver = dom->conn->privateData;
     char *ret = NULL;
     int rc;
+    virDomainObjPtr vm;
+    virLXCDomainObjPrivatePtr priv;
 
     lxcDriverLock(driver);
-    if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
+    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
+    if (vm == NULL) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("No such domain %s"), dom->uuid);
+        goto cleanup;
+    }
+    priv = vm->privateData;
+
+    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
         virReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cgroup CPU controller is not mounted"));
         goto cleanup;
     }
 
     if (nparams) {
-        rc = lxcGetCpuBWStatus(driver->cgroup);
+        rc = lxcGetCpuBWStatus(priv->cgroup);
         if (rc < 0)
             goto cleanup;
         else if (rc == 0)
@@ -1676,6 +1634,8 @@ static char *lxcGetSchedulerType(virDomainPtr domain,
         virReportOOMError();
 
 cleanup:
+    if (vm)
+        virObjectUnlock(vm);
     lxcDriverUnlock(driver);
     return ret;
 }
@@ -1762,11 +1722,11 @@ lxcSetSchedulerParametersFlags(virDomainPtr dom,
 {
     virLXCDriverPtr driver = dom->conn->privateData;
     int i;
-    virCgroupPtr group = NULL;
     virDomainObjPtr vm = NULL;
     virDomainDefPtr vmdef = NULL;
     int ret = -1;
     int rc;
+    virLXCDomainObjPrivatePtr priv;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                   VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -1789,6 +1749,7 @@ lxcSetSchedulerParametersFlags(virDomainPtr dom,
                        _("No such domain %s"), dom->uuid);
         goto cleanup;
     }
+    priv = vm->privateData;
 
     if (virDomainLiveConfigHelperMethod(driver->caps, driver->xmlopt,
                                         vm, &flags, &vmdef) < 0)
@@ -1802,17 +1763,11 @@ lxcSetSchedulerParametersFlags(virDomainPtr dom,
     }
 
     if (flags & VIR_DOMAIN_AFFECT_LIVE) {
-        if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
+        if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
             virReportError(VIR_ERR_OPERATION_INVALID,
                            "%s", _("cgroup CPU controller is not mounted"));
             goto cleanup;
         }
-        if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot find cgroup for domain %s"),
-                           vm->def->name);
-            goto cleanup;
-        }
     }
 
     for (i = 0; i < nparams; i++) {
@@ -1820,7 +1775,7 @@ lxcSetSchedulerParametersFlags(virDomainPtr dom,
 
         if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_CPU_SHARES)) {
             if (flags & VIR_DOMAIN_AFFECT_LIVE) {
-                rc = virCgroupSetCpuShares(group, params[i].value.ul);
+                rc = virCgroupSetCpuShares(priv->cgroup, params[i].value.ul);
                 if (rc != 0) {
                     virReportSystemError(-rc, "%s",
                                          _("unable to set cpu shares tunable"));
@@ -1835,7 +1790,7 @@ lxcSetSchedulerParametersFlags(virDomainPtr dom,
             }
         } else if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_VCPU_PERIOD)) {
             if (flags & VIR_DOMAIN_AFFECT_LIVE) {
-                rc = lxcSetVcpuBWLive(group, params[i].value.ul, 0);
+                rc = lxcSetVcpuBWLive(priv->cgroup, params[i].value.ul, 0);
                 if (rc != 0)
                     goto cleanup;
 
@@ -1848,7 +1803,7 @@ lxcSetSchedulerParametersFlags(virDomainPtr dom,
             }
         } else if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_VCPU_QUOTA)) {
             if (flags & VIR_DOMAIN_AFFECT_LIVE) {
-                rc = lxcSetVcpuBWLive(group, 0, params[i].value.l);
+                rc = lxcSetVcpuBWLive(priv->cgroup, 0, params[i].value.l);
                 if (rc != 0)
                     goto cleanup;
 
@@ -1879,7 +1834,6 @@ lxcSetSchedulerParametersFlags(virDomainPtr dom,
 
 cleanup:
     virDomainDefFree(vmdef);
-    virCgroupFree(&group);
     if (vm)
         virObjectUnlock(vm);
     lxcDriverUnlock(driver);
@@ -1901,7 +1855,6 @@ lxcGetSchedulerParametersFlags(virDomainPtr dom,
                                unsigned int flags)
 {
     virLXCDriverPtr driver = dom->conn->privateData;
-    virCgroupPtr group = NULL;
     virDomainObjPtr vm = NULL;
     virDomainDefPtr persistentDef;
     unsigned long long shares = 0;
@@ -1911,19 +1864,13 @@ lxcGetSchedulerParametersFlags(virDomainPtr dom,
     int rc;
     bool cpu_bw_status = false;
     int saved_nparams = 0;
+    virLXCDomainObjPrivatePtr priv;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                   VIR_DOMAIN_AFFECT_CONFIG, -1);
 
     lxcDriverLock(driver);
 
-    if (*nparams > 1) {
-        rc = lxcGetCpuBWStatus(driver->cgroup);
-        if (rc < 0)
-            goto cleanup;
-        cpu_bw_status = !!rc;
-    }
-
     vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
 
     if (vm == NULL) {
@@ -1931,6 +1878,14 @@ lxcGetSchedulerParametersFlags(virDomainPtr dom,
                        _("No such domain %s"), dom->uuid);
         goto cleanup;
     }
+    priv = vm->privateData;
+
+    if (*nparams > 1) {
+        rc = lxcGetCpuBWStatus(priv->cgroup);
+        if (rc < 0)
+            goto cleanup;
+        cpu_bw_status = !!rc;
+    }
 
     if (virDomainLiveConfigHelperMethod(driver->caps, driver->xmlopt,
                                         vm, &flags, &persistentDef) < 0)
@@ -1945,19 +1900,13 @@ lxcGetSchedulerParametersFlags(virDomainPtr dom,
         goto out;
     }
 
-    if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
+    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
         virReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cgroup CPU controller is not mounted"));
         goto cleanup;
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot find cgroup for domain %s"), vm->def->name);
-        goto cleanup;
-    }
-
-    rc = virCgroupGetCpuShares(group, &shares);
+    rc = virCgroupGetCpuShares(priv->cgroup, &shares);
     if (rc != 0) {
         virReportSystemError(-rc, "%s",
                              _("unable to get cpu shares tunable"));
@@ -1965,7 +1914,7 @@ lxcGetSchedulerParametersFlags(virDomainPtr dom,
     }
 
     if (*nparams > 1 && cpu_bw_status) {
-        rc = lxcGetVcpuBWLive(group, &period, &quota);
+        rc = lxcGetVcpuBWLive(priv->cgroup, &period, &quota);
         if (rc != 0)
             goto cleanup;
     }
@@ -1998,7 +1947,6 @@ out:
     ret = 0;
 
 cleanup:
-    virCgroupFree(&group);
     if (vm)
         virObjectUnlock(vm);
     lxcDriverUnlock(driver);
@@ -2022,10 +1970,10 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
 {
     virLXCDriverPtr driver = dom->conn->privateData;
     int i;
-    virCgroupPtr group = NULL;
     virDomainObjPtr vm = NULL;
     virDomainDefPtr persistentDef = NULL;
     int ret = -1;
+    virLXCDomainObjPrivatePtr priv;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                   VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -2044,24 +1992,19 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
                        _("No such domain %s"), dom->uuid);
         goto cleanup;
     }
+    priv = vm->privateData;
 
     if (virDomainLiveConfigHelperMethod(driver->caps, driver->xmlopt,
                                         vm, &flags, &persistentDef) < 0)
         goto cleanup;
 
     if (flags & VIR_DOMAIN_AFFECT_LIVE) {
-        if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_BLKIO)) {
+        if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_BLKIO)) {
             virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                            _("blkio cgroup isn't mounted"));
             goto cleanup;
         }
 
-        if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot find cgroup for domain %s"), vm->def->name);
-            goto cleanup;
-        }
-
         for (i = 0; i < nparams; i++) {
             virTypedParameterPtr param = &params[i];
 
@@ -2074,7 +2017,7 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
                     goto cleanup;
                 }
 
-                rc = virCgroupSetBlkioWeight(group, params[i].value.ui);
+                rc = virCgroupSetBlkioWeight(priv->cgroup, params[i].value.ui);
                 if (rc != 0) {
                     virReportSystemError(-rc, "%s",
                                          _("unable to set blkio weight tunable"));
@@ -2107,7 +2050,6 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
 
     ret = 0;
 cleanup:
-    virCgroupFree(&group);
     if (vm)
         virObjectUnlock(vm);
     lxcDriverUnlock(driver);
@@ -2124,12 +2066,12 @@ lxcDomainGetBlkioParameters(virDomainPtr dom,
 {
     virLXCDriverPtr driver = dom->conn->privateData;
     int i;
-    virCgroupPtr group = NULL;
     virDomainObjPtr vm = NULL;
     virDomainDefPtr persistentDef = NULL;
     unsigned int val;
     int ret = -1;
     int rc;
+    virLXCDomainObjPrivatePtr priv;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                   VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -2142,6 +2084,7 @@ lxcDomainGetBlkioParameters(virDomainPtr dom,
                        _("No such domain %s"), dom->uuid);
         goto cleanup;
     }
+    priv = vm->privateData;
 
     if ((*nparams) == 0) {
         /* Current number of blkio parameters supported by cgroups */
@@ -2155,25 +2098,19 @@ lxcDomainGetBlkioParameters(virDomainPtr dom,
         goto cleanup;
 
     if (flags & VIR_DOMAIN_AFFECT_LIVE) {
-        if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_BLKIO)) {
+        if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_BLKIO)) {
             virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                            _("blkio cgroup isn't mounted"));
             goto cleanup;
         }
 
-        if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot find cgroup for domain %s"), vm->def->name);
-            goto cleanup;
-        }
-
         for (i = 0; i < *nparams && i < LXC_NB_BLKIO_PARAM; i++) {
             virTypedParameterPtr param = &params[i];
             val = 0;
 
             switch (i) {
             case 0: /* fill blkio weight here */
-                rc = virCgroupGetBlkioWeight(group, &val);
+                rc = virCgroupGetBlkioWeight(priv->cgroup, &val);
                 if (rc != 0) {
                     virReportSystemError(-rc, "%s",
                                          _("unable to get blkio weight"));
@@ -2215,8 +2152,6 @@ lxcDomainGetBlkioParameters(virDomainPtr dom,
     ret = 0;
 
 cleanup:
-    if (group)
-        virCgroupFree(&group);
     if (vm)
         virObjectUnlock(vm);
     lxcDriverUnlock(driver);
@@ -2386,7 +2321,7 @@ cleanup:
     return ret;
 }
 
-static int lxcFreezeContainer(virLXCDriverPtr driver, virDomainObjPtr vm)
+static int lxcFreezeContainer(virDomainObjPtr vm)
 {
     int timeout = 1000; /* In milliseconds */
     int check_interval = 1; /* In milliseconds */
@@ -2394,13 +2329,7 @@ static int lxcFreezeContainer(virLXCDriverPtr driver, virDomainObjPtr vm)
     int waited_time = 0;
     int ret = -1;
     char *state = NULL;
-    virCgroupPtr cgroup = NULL;
-
-    if (!(driver->cgroup &&
-          virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) == 0))
-        return -1;
-
-    /* From here on, we know that cgroup != NULL.  */
+    virLXCDomainObjPrivatePtr priv = vm->privateData;
 
     while (waited_time < timeout) {
         int r;
@@ -2411,7 +2340,7 @@ static int lxcFreezeContainer(virLXCDriverPtr driver, virDomainObjPtr vm)
          * to "FROZEN".
          * (see linux-2.6/Documentation/cgroups/freezer-subsystem.txt)
          */
-        r = virCgroupSetFreezerState(cgroup, "FROZEN");
+        r = virCgroupSetFreezerState(priv->cgroup, "FROZEN");
 
         /*
          * Returning EBUSY explicitly indicates that the group is
@@ -2438,7 +2367,7 @@ static int lxcFreezeContainer(virLXCDriverPtr driver, virDomainObjPtr vm)
          */
         usleep(check_interval * 1000);
 
-        r = virCgroupGetFreezerState(cgroup, &state);
+        r = virCgroupGetFreezerState(priv->cgroup, &state);
 
         if (r < 0) {
             VIR_DEBUG("Reading freezer.state failed with errno: %d", r);
@@ -2470,11 +2399,10 @@ error:
      * activate the group again and return an error.
      * This is likely to fall the group back again gracefully.
      */
-    virCgroupSetFreezerState(cgroup, "THAWED");
+    virCgroupSetFreezerState(priv->cgroup, "THAWED");
     ret = -1;
 
 cleanup:
-    virCgroupFree(&cgroup);
     VIR_FREE(state);
     return ret;
 }
@@ -2504,7 +2432,7 @@ static int lxcDomainSuspend(virDomainPtr dom)
     }
 
     if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_PAUSED) {
-        if (lxcFreezeContainer(driver, vm) < 0) {
+        if (lxcFreezeContainer(vm) < 0) {
             virReportError(VIR_ERR_OPERATION_FAILED,
                            "%s", _("Suspend operation failed"));
             goto cleanup;
@@ -2529,27 +2457,13 @@ cleanup:
     return ret;
 }
 
-static int lxcUnfreezeContainer(virLXCDriverPtr driver, virDomainObjPtr vm)
-{
-    int ret;
-    virCgroupPtr cgroup = NULL;
-
-    if (!(driver->cgroup &&
-        virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) == 0))
-        return -1;
-
-    ret = virCgroupSetFreezerState(cgroup, "THAWED");
-
-    virCgroupFree(&cgroup);
-    return ret;
-}
-
 static int lxcDomainResume(virDomainPtr dom)
 {
     virLXCDriverPtr driver = dom->conn->privateData;
     virDomainObjPtr vm;
     virDomainEventPtr event = NULL;
     int ret = -1;
+    virLXCDomainObjPrivatePtr priv;
 
     lxcDriverLock(driver);
     vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
@@ -2562,6 +2476,8 @@ static int lxcDomainResume(virDomainPtr dom)
         goto cleanup;
     }
 
+    priv = vm->privateData;
+
     if (!virDomainObjIsActive(vm)) {
         virReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("Domain is not running"));
@@ -2569,7 +2485,7 @@ static int lxcDomainResume(virDomainPtr dom)
     }
 
     if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_PAUSED) {
-        if (lxcUnfreezeContainer(driver, vm) < 0) {
+        if (virCgroupSetFreezerState(priv->cgroup, "THAWED") < 0) {
             virReportError(VIR_ERR_OPERATION_FAILED,
                            "%s", _("Resume operation failed"));
             goto cleanup;
@@ -3112,7 +3028,6 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
 {
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainDiskDefPtr def = dev->data.disk;
-    virCgroupPtr group = NULL;
     int ret = -1;
     char *dst = NULL;
     struct stat sb;
@@ -3197,19 +3112,13 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
                                         vm->def, def) < 0)
         goto cleanup;
 
-    if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
+    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
         goto cleanup;
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot find cgroup for domain %s"), vm->def->name);
-        goto cleanup;
-    }
-
-    if (virCgroupAllowDevicePath(group, def->src,
+    if (virCgroupAllowDevicePath(priv->cgroup, def->src,
                                  (def->readonly ?
                                   VIR_CGROUP_DEVICE_READ :
                                   VIR_CGROUP_DEVICE_RW) |
@@ -3227,8 +3136,6 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
 cleanup:
     def->src = tmpsrc;
     virDomainAuditDisk(vm, NULL, def->src, "attach", ret == 0);
-    if (group)
-        virCgroupFree(&group);
     if (dst && created && ret < 0)
         unlink(dst);
     return ret;
@@ -3382,7 +3289,6 @@ lxcDomainAttachDeviceHostdevSubsysUSBLive(virLXCDriverPtr driver,
     mode_t mode;
     bool created = false;
     virUSBDevicePtr usb = NULL;
-    virCgroupPtr group = NULL;
 
     if (virDomainHostdevFind(vm->def, def, NULL) >= 0) {
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
@@ -3417,18 +3323,12 @@ lxcDomainAttachDeviceHostdevSubsysUSBLive(virLXCDriverPtr driver,
         goto cleanup;
     }
 
-    if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
+    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
         goto cleanup;
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot find cgroup for domain %s"), vm->def->name);
-        goto cleanup;
-    }
-
     if (!(usb = virUSBDeviceNew(def->source.subsys.u.usb.bus,
                                 def->source.subsys.u.usb.device, vroot)))
         goto cleanup;
@@ -3469,8 +3369,8 @@ lxcDomainAttachDeviceHostdevSubsysUSBLive(virLXCDriverPtr driver,
         goto cleanup;
 
     if (virUSBDeviceFileIterate(usb,
-                             virLXCSetupHostUsbDeviceCgroup,
-                             &group) < 0)
+                                virLXCSetupHostUsbDeviceCgroup,
+                                &priv->cgroup) < 0)
         goto cleanup;
 
     ret = 0;
@@ -3481,7 +3381,6 @@ cleanup:
         unlink(dstfile);
 
     virUSBDeviceFree(usb);
-    virCgroupFree(&group);
     VIR_FREE(src);
     VIR_FREE(dstfile);
     VIR_FREE(dstdir);
@@ -3497,7 +3396,6 @@ lxcDomainAttachDeviceHostdevStorageLive(virLXCDriverPtr driver,
 {
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainHostdevDefPtr def = dev->data.hostdev;
-    virCgroupPtr group = NULL;
     int ret = -1;
     char *dst = NULL;
     char *vroot = NULL;
@@ -3566,19 +3464,13 @@ lxcDomainAttachDeviceHostdevStorageLive(virLXCDriverPtr driver,
                                           vm->def, def, vroot) < 0)
         goto cleanup;
 
-    if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
+    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
         goto cleanup;
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot find cgroup for domain %s"), vm->def->name);
-        goto cleanup;
-    }
-
-    if (virCgroupAllowDevicePath(group, def->source.caps.u.storage.block,
+    if (virCgroupAllowDevicePath(priv->cgroup, def->source.caps.u.storage.block,
                                  VIR_CGROUP_DEVICE_RW |
                                  VIR_CGROUP_DEVICE_MKNOD) != 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -3593,8 +3485,6 @@ lxcDomainAttachDeviceHostdevStorageLive(virLXCDriverPtr driver,
 
 cleanup:
     virDomainAuditHostdev(vm, def, "attach", ret == 0);
-    if (group)
-        virCgroupFree(&group);
     if (dst && created && ret < 0)
         unlink(dst);
     VIR_FREE(dst);
@@ -3610,7 +3500,6 @@ lxcDomainAttachDeviceHostdevMiscLive(virLXCDriverPtr driver,
 {
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainHostdevDefPtr def = dev->data.hostdev;
-    virCgroupPtr group = NULL;
     int ret = -1;
     char *dst = NULL;
     char *vroot = NULL;
@@ -3679,19 +3568,13 @@ lxcDomainAttachDeviceHostdevMiscLive(virLXCDriverPtr driver,
                                           vm->def, def, vroot) < 0)
         goto cleanup;
 
-    if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
+    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
         goto cleanup;
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot find cgroup for domain %s"), vm->def->name);
-        goto cleanup;
-    }
-
-    if (virCgroupAllowDevicePath(group, def->source.caps.u.misc.chardev,
+    if (virCgroupAllowDevicePath(priv->cgroup, def->source.caps.u.misc.chardev,
                                  VIR_CGROUP_DEVICE_RW |
                                  VIR_CGROUP_DEVICE_MKNOD) != 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -3706,8 +3589,6 @@ lxcDomainAttachDeviceHostdevMiscLive(virLXCDriverPtr driver,
 
 cleanup:
     virDomainAuditHostdev(vm, def, "attach", ret == 0);
-    if (group)
-        virCgroupFree(&group);
     if (dst && created && ret < 0)
         unlink(dst);
     VIR_FREE(dst);
@@ -3824,13 +3705,11 @@ lxcDomainAttachDeviceLive(virConnectPtr conn,
 
 
 static int
-lxcDomainDetachDeviceDiskLive(virLXCDriverPtr driver,
-                              virDomainObjPtr vm,
+lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
                               virDomainDeviceDefPtr dev)
 {
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainDiskDefPtr def = NULL;
-    virCgroupPtr group = NULL;
     int i, ret = -1;
     char *dst = NULL;
 
@@ -3856,18 +3735,12 @@ lxcDomainDetachDeviceDiskLive(virLXCDriverPtr driver,
         goto cleanup;
     }
 
-    if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
+    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
         goto cleanup;
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot find cgroup for domain %s"), vm->def->name);
-        goto cleanup;
-    }
-
     VIR_DEBUG("Unlinking %s (backed by %s)", dst, def->src);
     if (unlink(dst) < 0 && errno != ENOENT) {
         virDomainAuditDisk(vm, def->src, NULL, "detach", false);
@@ -3877,7 +3750,7 @@ lxcDomainDetachDeviceDiskLive(virLXCDriverPtr driver,
     }
     virDomainAuditDisk(vm, def->src, NULL, "detach", true);
 
-    if (virCgroupDenyDevicePath(group, def->src, VIR_CGROUP_DEVICE_RWM) != 0)
+    if (virCgroupDenyDevicePath(priv->cgroup, def->src, VIR_CGROUP_DEVICE_RWM) != 0)
         VIR_WARN("cannot deny device %s for domain %s",
                  def->src, vm->def->name);
 
@@ -3888,8 +3761,6 @@ lxcDomainDetachDeviceDiskLive(virLXCDriverPtr driver,
 
 cleanup:
     VIR_FREE(dst);
-    if (group)
-        virCgroupFree(&group);
     return ret;
 }
 
@@ -3967,7 +3838,6 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
 {
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainHostdevDefPtr def = NULL;
-    virCgroupPtr group = NULL;
     int idx, ret = -1;
     char *dst = NULL;
     char *vroot;
@@ -3995,18 +3865,12 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
         goto cleanup;
     }
 
-    if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
+    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
         goto cleanup;
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot find cgroup for domain %s"), vm->def->name);
-        goto cleanup;
-    }
-
     if (!(usb = virUSBDeviceNew(def->source.subsys.u.usb.bus,
                                 def->source.subsys.u.usb.device, vroot)))
         goto cleanup;
@@ -4021,8 +3885,8 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
     virDomainAuditHostdev(vm, def, "detach", true);
 
     if (virUSBDeviceFileIterate(usb,
-                             virLXCTeardownHostUsbDeviceCgroup,
-                             &group) < 0)
+                                virLXCTeardownHostUsbDeviceCgroup,
+                                &priv->cgroup) < 0)
         VIR_WARN("cannot deny device %s for domain %s",
                  dst, vm->def->name);
 
@@ -4036,19 +3900,16 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
 cleanup:
     virUSBDeviceFree(usb);
     VIR_FREE(dst);
-    virCgroupFree(&group);
     return ret;
 }
 
 
 static int
-lxcDomainDetachDeviceHostdevStorageLive(virLXCDriverPtr driver,
-                                        virDomainObjPtr vm,
+lxcDomainDetachDeviceHostdevStorageLive(virDomainObjPtr vm,
                                         virDomainDeviceDefPtr dev)
 {
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainHostdevDefPtr def = NULL;
-    virCgroupPtr group = NULL;
     int i, ret = -1;
     char *dst = NULL;
 
@@ -4074,18 +3935,12 @@ lxcDomainDetachDeviceHostdevStorageLive(virLXCDriverPtr driver,
         goto cleanup;
     }
 
-    if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
+    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
         goto cleanup;
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot find cgroup for domain %s"), vm->def->name);
-        goto cleanup;
-    }
-
     VIR_DEBUG("Unlinking %s", dst);
     if (unlink(dst) < 0 && errno != ENOENT) {
         virDomainAuditHostdev(vm, def, "detach", false);
@@ -4095,7 +3950,7 @@ lxcDomainDetachDeviceHostdevStorageLive(virLXCDriverPtr driver,
     }
     virDomainAuditHostdev(vm, def, "detach", true);
 
-    if (virCgroupDenyDevicePath(group, def->source.caps.u.storage.block, VIR_CGROUP_DEVICE_RWM) != 0)
+    if (virCgroupDenyDevicePath(priv->cgroup, def->source.caps.u.storage.block, VIR_CGROUP_DEVICE_RWM) != 0)
         VIR_WARN("cannot deny device %s for domain %s",
                  def->source.caps.u.storage.block, vm->def->name);
 
@@ -4106,20 +3961,16 @@ lxcDomainDetachDeviceHostdevStorageLive(virLXCDriverPtr driver,
 
 cleanup:
     VIR_FREE(dst);
-    if (group)
-        virCgroupFree(&group);
     return ret;
 }
 
 
 static int
-lxcDomainDetachDeviceHostdevMiscLive(virLXCDriverPtr driver,
-                                     virDomainObjPtr vm,
+lxcDomainDetachDeviceHostdevMiscLive(virDomainObjPtr vm,
                                      virDomainDeviceDefPtr dev)
 {
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainHostdevDefPtr def = NULL;
-    virCgroupPtr group = NULL;
     int i, ret = -1;
     char *dst = NULL;
 
@@ -4145,18 +3996,12 @@ lxcDomainDetachDeviceHostdevMiscLive(virLXCDriverPtr driver,
         goto cleanup;
     }
 
-    if (!lxcCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
+    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
         goto cleanup;
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot find cgroup for domain %s"), vm->def->name);
-        goto cleanup;
-    }
-
     VIR_DEBUG("Unlinking %s", dst);
     if (unlink(dst) < 0 && errno != ENOENT) {
         virDomainAuditHostdev(vm, def, "detach", false);
@@ -4166,7 +4011,7 @@ lxcDomainDetachDeviceHostdevMiscLive(virLXCDriverPtr driver,
     }
     virDomainAuditHostdev(vm, def, "detach", true);
 
-    if (virCgroupDenyDevicePath(group, def->source.caps.u.misc.chardev, VIR_CGROUP_DEVICE_RWM) != 0)
+    if (virCgroupDenyDevicePath(priv->cgroup, def->source.caps.u.misc.chardev, VIR_CGROUP_DEVICE_RWM) != 0)
         VIR_WARN("cannot deny device %s for domain %s",
                  def->source.caps.u.misc.chardev, vm->def->name);
 
@@ -4177,8 +4022,6 @@ lxcDomainDetachDeviceHostdevMiscLive(virLXCDriverPtr driver,
 
 cleanup:
     VIR_FREE(dst);
-    if (group)
-        virCgroupFree(&group);
     return ret;
 }
 
@@ -4202,16 +4045,15 @@ lxcDomainDetachDeviceHostdevSubsysLive(virLXCDriverPtr driver,
 
 
 static int
-lxcDomainDetachDeviceHostdevCapsLive(virLXCDriverPtr driver,
-                                       virDomainObjPtr vm,
-                                       virDomainDeviceDefPtr dev)
+lxcDomainDetachDeviceHostdevCapsLive(virDomainObjPtr vm,
+                                     virDomainDeviceDefPtr dev)
 {
     switch (dev->data.hostdev->source.caps.type) {
     case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE:
-        return lxcDomainDetachDeviceHostdevStorageLive(driver, vm, dev);
+        return lxcDomainDetachDeviceHostdevStorageLive(vm, dev);
 
     case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC:
-        return lxcDomainDetachDeviceHostdevMiscLive(driver, vm, dev);
+        return lxcDomainDetachDeviceHostdevMiscLive(vm, dev);
 
     default:
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -4240,7 +4082,7 @@ lxcDomainDetachDeviceHostdevLive(virLXCDriverPtr driver,
         return lxcDomainDetachDeviceHostdevSubsysLive(driver, vm, dev);
 
     case VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES:
-        return lxcDomainDetachDeviceHostdevCapsLive(driver, vm, dev);
+        return lxcDomainDetachDeviceHostdevCapsLive(vm, dev);
 
     default:
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -4260,7 +4102,7 @@ lxcDomainDetachDeviceLive(virLXCDriverPtr driver,
 
     switch (dev->type) {
     case VIR_DOMAIN_DEVICE_DISK:
-        ret = lxcDomainDetachDeviceDiskLive(driver, vm, dev);
+        ret = lxcDomainDetachDeviceDiskLive(vm, dev);
         break;
 
     case VIR_DOMAIN_DEVICE_NET:
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index b51c880..1bbffa3 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -29,6 +29,7 @@
 #include "lxc_process.h"
 #include "lxc_domain.h"
 #include "lxc_container.h"
+#include "lxc_cgroup.h"
 #include "lxc_fuse.h"
 #include "datatypes.h"
 #include "virfile.h"
@@ -219,7 +220,6 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
                                  virDomainObjPtr vm,
                                  virDomainShutoffReason reason)
 {
-    virCgroupPtr cgroup;
     int i;
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virNetDevVPortProfilePtr vport = NULL;
@@ -277,10 +277,9 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
 
     virDomainConfVMNWFilterTeardown(vm);
 
-    if (driver->cgroup &&
-        virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) == 0) {
-        virCgroupRemove(cgroup);
-        virCgroupFree(&cgroup);
+    if (priv->cgroup) {
+        virCgroupRemove(priv->cgroup);
+        virCgroupFree(&priv->cgroup);
     }
 
     /* now that we know it's stopped call the hook if present */
@@ -742,8 +741,8 @@ int virLXCProcessStop(virLXCDriverPtr driver,
                       virDomainObjPtr vm,
                       virDomainShutoffReason reason)
 {
-    virCgroupPtr group = NULL;
     int rc;
+    virLXCDomainObjPrivatePtr priv;
 
     VIR_DEBUG("Stopping VM name=%s pid=%d reason=%d",
               vm->def->name, (int)vm->pid, (int)reason);
@@ -752,6 +751,8 @@ int virLXCProcessStop(virLXCDriverPtr driver,
         return 0;
     }
 
+    priv = vm->privateData;
+
     if (vm->pid <= 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Invalid PID %d for container"), vm->pid);
@@ -769,8 +770,8 @@ int virLXCProcessStop(virLXCDriverPtr driver,
         VIR_FREE(vm->def->seclabels[0]->imagelabel);
     }
 
-    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) == 0) {
-        rc = virCgroupKillPainfully(group);
+    if (priv->cgroup) {
+        rc = virCgroupKillPainfully(priv->cgroup);
         if (rc < 0) {
             virReportSystemError(-rc, "%s",
                                  _("Failed to kill container PIDs"));
@@ -794,7 +795,6 @@ int virLXCProcessStop(virLXCDriverPtr driver,
     rc = 0;
 
 cleanup:
-    virCgroupFree(&group);
     return rc;
 }
 
@@ -1047,26 +1047,28 @@ int virLXCProcessStart(virConnectPtr conn,
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virErrorPtr err = NULL;
 
-    if (!lxc_driver->cgroup) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("The 'cpuacct', 'devices' & 'memory' cgroups controllers must be mounted"));
+    virCgroupFree(&priv->cgroup);
+
+    if (!(priv->cgroup = virLXCCgroupCreate(vm->def)))
         return -1;
-    }
 
-    if (!virCgroupHasController(lxc_driver->cgroup,
-                          VIR_CGROUP_CONTROLLER_CPUACCT)) {
+    if (!virCgroupHasController(priv->cgroup,
+                                VIR_CGROUP_CONTROLLER_CPUACCT)) {
+        virCgroupFree(&priv->cgroup);
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("Unable to find 'cpuacct' cgroups controller mount"));
         return -1;
     }
-    if (!virCgroupHasController(lxc_driver->cgroup,
+    if (!virCgroupHasController(priv->cgroup,
                                 VIR_CGROUP_CONTROLLER_DEVICES)) {
+        virCgroupFree(&priv->cgroup);
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("Unable to find 'devices' cgroups controller mount"));
         return -1;
     }
-    if (!virCgroupHasController(lxc_driver->cgroup,
+    if (!virCgroupHasController(priv->cgroup,
                                 VIR_CGROUP_CONTROLLER_MEMORY)) {
+        virCgroupFree(&priv->cgroup);
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("Unable to find 'memory' cgroups controller mount"));
         return -1;
@@ -1462,6 +1464,9 @@ virLXCProcessReconnectDomain(virDomainObjPtr vm,
         if (!(priv->monitor = virLXCProcessConnectMonitor(driver, vm)))
             goto error;
 
+        if (!(priv->cgroup = virLXCCgroupCreate(vm->def)))
+            goto error;
+
         if (virLXCUpdateActiveUsbHostdevs(driver, vm->def) < 0)
             goto error;
 
-- 
1.8.1.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]