提交 cfed9ad4 编写于 作者: D Daniel P. Berrange

Store a virCgroupPtr instance in virLXCDomainObjPrivatePtr

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: NDaniel P. Berrange <berrange@redhat.com>
上级 632f78ca
...@@ -527,7 +527,6 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def) ...@@ -527,7 +527,6 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def)
{ {
virCgroupPtr driver = NULL; virCgroupPtr driver = NULL;
virCgroupPtr cgroup = NULL; virCgroupPtr cgroup = NULL;
int ret = -1;
int rc; int rc;
rc = virCgroupForDriver("lxc", &driver, 1, 0, -1); rc = virCgroupForDriver("lxc", &driver, 1, 0, -1);
...@@ -545,6 +544,21 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def) ...@@ -545,6 +544,21 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def)
goto cleanup; 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()); rc = virCgroupAddTask(cgroup, getpid());
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
...@@ -556,7 +570,6 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def) ...@@ -556,7 +570,6 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def)
ret = 0; ret = 0;
cleanup: cleanup:
virCgroupFree(&driver);
if (ret < 0) { if (ret < 0) {
virCgroupFree(&cgroup); virCgroupFree(&cgroup);
return NULL; return NULL;
......
...@@ -22,11 +22,13 @@ ...@@ -22,11 +22,13 @@
#ifndef __VIR_LXC_CGROUP_H__ #ifndef __VIR_LXC_CGROUP_H__
# define __VIR_LXC_CGROUP_H__ # define __VIR_LXC_CGROUP_H__
# include "vircgroup.h"
# include "domain_conf.h" # include "domain_conf.h"
# include "lxc_fuse.h" # include "lxc_fuse.h"
# include "virusb.h" # include "virusb.h"
virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def); virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def);
virCgroupPtr virLXCCgroupJoin(virDomainDefPtr def);
int virLXCCgroupSetup(virDomainDefPtr def, int virLXCCgroupSetup(virDomainDefPtr def,
virCgroupPtr cgroup, virCgroupPtr cgroup,
virBitmapPtr nodemask); virBitmapPtr nodemask);
......
...@@ -32,9 +32,9 @@ ...@@ -32,9 +32,9 @@
# include "domain_event.h" # include "domain_event.h"
# include "capabilities.h" # include "capabilities.h"
# include "virthread.h" # include "virthread.h"
# include "vircgroup.h"
# include "security/security_manager.h" # include "security/security_manager.h"
# include "configmake.h" # include "configmake.h"
# include "vircgroup.h"
# include "virsysinfo.h" # include "virsysinfo.h"
# include "virusb.h" # include "virusb.h"
......
...@@ -1434,7 +1434,7 @@ virLXCControllerRun(virLXCControllerPtr ctrl) ...@@ -1434,7 +1434,7 @@ virLXCControllerRun(virLXCControllerPtr ctrl)
if (virLXCControllerSetupPrivateNS() < 0) if (virLXCControllerSetupPrivateNS() < 0)
goto cleanup; goto cleanup;
if (!(cgroup = virLXCCgroupCreate(ctrl->def))) if (!(cgroup = virLXCCgroupJoin(ctrl->def)))
goto cleanup; goto cleanup;
if (virLXCControllerSetupLoopDevices(ctrl) < 0) if (virLXCControllerSetupLoopDevices(ctrl) < 0)
......
...@@ -43,6 +43,8 @@ static void virLXCDomainObjPrivateFree(void *data) ...@@ -43,6 +43,8 @@ static void virLXCDomainObjPrivateFree(void *data)
{ {
virLXCDomainObjPrivatePtr priv = data; virLXCDomainObjPrivatePtr priv = data;
virCgroupFree(&priv->cgroup);
VIR_FREE(priv); VIR_FREE(priv);
} }
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#ifndef __LXC_DOMAIN_H__ #ifndef __LXC_DOMAIN_H__
# define __LXC_DOMAIN_H__ # define __LXC_DOMAIN_H__
# include "vircgroup.h"
# include "lxc_conf.h" # include "lxc_conf.h"
# include "lxc_monitor.h" # include "lxc_monitor.h"
...@@ -36,6 +37,8 @@ struct _virLXCDomainObjPrivate { ...@@ -36,6 +37,8 @@ struct _virLXCDomainObjPrivate {
bool wantReboot; bool wantReboot;
pid_t initpid; pid_t initpid;
virCgroupPtr cgroup;
}; };
extern virDomainXMLPrivateDataCallbacks virLXCDriverPrivateDataCallbacks; extern virDomainXMLPrivateDataCallbacks virLXCDriverPrivateDataCallbacks;
......
此差异已折叠。
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "lxc_process.h" #include "lxc_process.h"
#include "lxc_domain.h" #include "lxc_domain.h"
#include "lxc_container.h" #include "lxc_container.h"
#include "lxc_cgroup.h"
#include "lxc_fuse.h" #include "lxc_fuse.h"
#include "datatypes.h" #include "datatypes.h"
#include "virfile.h" #include "virfile.h"
...@@ -219,7 +220,6 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, ...@@ -219,7 +220,6 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
virDomainObjPtr vm, virDomainObjPtr vm,
virDomainShutoffReason reason) virDomainShutoffReason reason)
{ {
virCgroupPtr cgroup;
int i; int i;
virLXCDomainObjPrivatePtr priv = vm->privateData; virLXCDomainObjPrivatePtr priv = vm->privateData;
virNetDevVPortProfilePtr vport = NULL; virNetDevVPortProfilePtr vport = NULL;
...@@ -277,10 +277,9 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, ...@@ -277,10 +277,9 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
virDomainConfVMNWFilterTeardown(vm); virDomainConfVMNWFilterTeardown(vm);
if (driver->cgroup && if (priv->cgroup) {
virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) == 0) { virCgroupRemove(priv->cgroup);
virCgroupRemove(cgroup); virCgroupFree(&priv->cgroup);
virCgroupFree(&cgroup);
} }
/* now that we know it's stopped call the hook if present */ /* now that we know it's stopped call the hook if present */
...@@ -742,8 +741,8 @@ int virLXCProcessStop(virLXCDriverPtr driver, ...@@ -742,8 +741,8 @@ int virLXCProcessStop(virLXCDriverPtr driver,
virDomainObjPtr vm, virDomainObjPtr vm,
virDomainShutoffReason reason) virDomainShutoffReason reason)
{ {
virCgroupPtr group = NULL;
int rc; int rc;
virLXCDomainObjPrivatePtr priv;
VIR_DEBUG("Stopping VM name=%s pid=%d reason=%d", VIR_DEBUG("Stopping VM name=%s pid=%d reason=%d",
vm->def->name, (int)vm->pid, (int)reason); vm->def->name, (int)vm->pid, (int)reason);
...@@ -752,6 +751,8 @@ int virLXCProcessStop(virLXCDriverPtr driver, ...@@ -752,6 +751,8 @@ int virLXCProcessStop(virLXCDriverPtr driver,
return 0; return 0;
} }
priv = vm->privateData;
if (vm->pid <= 0) { if (vm->pid <= 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Invalid PID %d for container"), vm->pid); _("Invalid PID %d for container"), vm->pid);
...@@ -769,8 +770,8 @@ int virLXCProcessStop(virLXCDriverPtr driver, ...@@ -769,8 +770,8 @@ int virLXCProcessStop(virLXCDriverPtr driver,
VIR_FREE(vm->def->seclabels[0]->imagelabel); VIR_FREE(vm->def->seclabels[0]->imagelabel);
} }
if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) == 0) { if (priv->cgroup) {
rc = virCgroupKillPainfully(group); rc = virCgroupKillPainfully(priv->cgroup);
if (rc < 0) { if (rc < 0) {
virReportSystemError(-rc, "%s", virReportSystemError(-rc, "%s",
_("Failed to kill container PIDs")); _("Failed to kill container PIDs"));
...@@ -794,7 +795,6 @@ int virLXCProcessStop(virLXCDriverPtr driver, ...@@ -794,7 +795,6 @@ int virLXCProcessStop(virLXCDriverPtr driver,
rc = 0; rc = 0;
cleanup: cleanup:
virCgroupFree(&group);
return rc; return rc;
} }
...@@ -1047,26 +1047,28 @@ int virLXCProcessStart(virConnectPtr conn, ...@@ -1047,26 +1047,28 @@ int virLXCProcessStart(virConnectPtr conn,
virLXCDomainObjPrivatePtr priv = vm->privateData; virLXCDomainObjPrivatePtr priv = vm->privateData;
virErrorPtr err = NULL; virErrorPtr err = NULL;
if (!lxc_driver->cgroup) { virCgroupFree(&priv->cgroup);
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("The 'cpuacct', 'devices' & 'memory' cgroups controllers must be mounted")); if (!(priv->cgroup = virLXCCgroupCreate(vm->def)))
return -1; return -1;
}
if (!virCgroupHasController(lxc_driver->cgroup, if (!virCgroupHasController(priv->cgroup,
VIR_CGROUP_CONTROLLER_CPUACCT)) { VIR_CGROUP_CONTROLLER_CPUACCT)) {
virCgroupFree(&priv->cgroup);
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to find 'cpuacct' cgroups controller mount")); _("Unable to find 'cpuacct' cgroups controller mount"));
return -1; return -1;
} }
if (!virCgroupHasController(lxc_driver->cgroup, if (!virCgroupHasController(priv->cgroup,
VIR_CGROUP_CONTROLLER_DEVICES)) { VIR_CGROUP_CONTROLLER_DEVICES)) {
virCgroupFree(&priv->cgroup);
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to find 'devices' cgroups controller mount")); _("Unable to find 'devices' cgroups controller mount"));
return -1; return -1;
} }
if (!virCgroupHasController(lxc_driver->cgroup, if (!virCgroupHasController(priv->cgroup,
VIR_CGROUP_CONTROLLER_MEMORY)) { VIR_CGROUP_CONTROLLER_MEMORY)) {
virCgroupFree(&priv->cgroup);
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to find 'memory' cgroups controller mount")); _("Unable to find 'memory' cgroups controller mount"));
return -1; return -1;
...@@ -1462,6 +1464,9 @@ virLXCProcessReconnectDomain(virDomainObjPtr vm, ...@@ -1462,6 +1464,9 @@ virLXCProcessReconnectDomain(virDomainObjPtr vm,
if (!(priv->monitor = virLXCProcessConnectMonitor(driver, vm))) if (!(priv->monitor = virLXCProcessConnectMonitor(driver, vm)))
goto error; goto error;
if (!(priv->cgroup = virLXCCgroupCreate(vm->def)))
goto error;
if (virLXCUpdateActiveUsbHostdevs(driver, vm->def) < 0) if (virLXCUpdateActiveUsbHostdevs(driver, vm->def) < 0)
goto error; goto error;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册