mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
LXC: resolve issues in lxcDomainSetMaxMemory
This patch changes the setmaxmem function to support the '--live', '--config', and '--current' flags by revectoring the code through the setmem function using the VIR_DOMAIN_MEM_MAXIMUM flag. The setmem code is refactored to handle both cases depending on the flag. The changed maxmem code for the MEM_MAXIMUM path will not allow modification to the memory values of an active guest unless the --config switch is used. Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
This commit is contained in:
parent
c018efa863
commit
bd3b76e355
@ -680,37 +680,6 @@ lxcDomainGetMaxMemory(virDomainPtr dom)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lxcDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax)
|
|
||||||
{
|
|
||||||
virDomainObjPtr vm;
|
|
||||||
int ret = -1;
|
|
||||||
|
|
||||||
if (!(vm = lxcDomObjFromDomain(dom)))
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
if (virDomainSetMaxMemoryEnsureACL(dom->conn, vm->def) < 0)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
if (newmax < vm->def->mem.cur_balloon) {
|
|
||||||
if (!virDomainObjIsActive(vm)) {
|
|
||||||
vm->def->mem.cur_balloon = newmax;
|
|
||||||
} else {
|
|
||||||
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
|
||||||
_("Cannot set max memory lower than current"
|
|
||||||
" memory for an active domain"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vm->def->mem.max_balloon = newmax;
|
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
if (vm)
|
|
||||||
virObjectUnlock(vm);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
|
static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
|
||||||
unsigned int flags)
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
@ -721,10 +690,10 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
|
|||||||
virLXCDomainObjPrivatePtr priv;
|
virLXCDomainObjPrivatePtr priv;
|
||||||
virLXCDriverPtr driver = dom->conn->privateData;
|
virLXCDriverPtr driver = dom->conn->privateData;
|
||||||
virLXCDriverConfigPtr cfg = NULL;
|
virLXCDriverConfigPtr cfg = NULL;
|
||||||
unsigned long oldmax = 0;
|
|
||||||
|
|
||||||
virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
|
virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
|
||||||
VIR_DOMAIN_AFFECT_CONFIG, -1);
|
VIR_DOMAIN_AFFECT_CONFIG |
|
||||||
|
VIR_DOMAIN_MEM_MAXIMUM, -1);
|
||||||
|
|
||||||
if (!(vm = lxcDomObjFromDomain(dom)))
|
if (!(vm = lxcDomObjFromDomain(dom)))
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -743,32 +712,50 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
|
|||||||
&persistentDef) < 0)
|
&persistentDef) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (flags & VIR_DOMAIN_AFFECT_LIVE)
|
if (flags & VIR_DOMAIN_MEM_MAXIMUM) {
|
||||||
oldmax = vm->def->mem.max_balloon;
|
if (flags & VIR_DOMAIN_AFFECT_LIVE) {
|
||||||
if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
||||||
if (!oldmax || oldmax > persistentDef->mem.max_balloon)
|
_("Cannot resize the max memory "
|
||||||
oldmax = persistentDef->mem.max_balloon;
|
"on an active domain"));
|
||||||
}
|
|
||||||
|
|
||||||
if (newmem > oldmax) {
|
|
||||||
virReportError(VIR_ERR_INVALID_ARG,
|
|
||||||
"%s", _("Cannot set memory higher than max memory"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (flags & VIR_DOMAIN_AFFECT_LIVE) {
|
|
||||||
if (virCgroupSetMemory(priv->cgroup, newmem) < 0) {
|
|
||||||
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
||||||
"%s", _("Failed to set memory for domain"));
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
|
if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
|
||||||
sa_assert(persistentDef);
|
persistentDef->mem.max_balloon = newmem;
|
||||||
persistentDef->mem.cur_balloon = newmem;
|
if (persistentDef->mem.cur_balloon > newmem)
|
||||||
if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0)
|
persistentDef->mem.cur_balloon = newmem;
|
||||||
|
if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unsigned long oldmax = 0;
|
||||||
|
|
||||||
|
if (flags & VIR_DOMAIN_AFFECT_LIVE)
|
||||||
|
oldmax = vm->def->mem.max_balloon;
|
||||||
|
if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
|
||||||
|
if (!oldmax || oldmax > persistentDef->mem.max_balloon)
|
||||||
|
oldmax = persistentDef->mem.max_balloon;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newmem > oldmax) {
|
||||||
|
virReportError(VIR_ERR_INVALID_ARG,
|
||||||
|
"%s", _("Cannot set memory higher than max memory"));
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags & VIR_DOMAIN_AFFECT_LIVE) {
|
||||||
|
if (virCgroupSetMemory(priv->cgroup, newmem) < 0) {
|
||||||
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
||||||
|
"%s", _("Failed to set memory for domain"));
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
|
||||||
|
persistentDef->mem.cur_balloon = newmem;
|
||||||
|
if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
@ -786,6 +773,11 @@ static int lxcDomainSetMemory(virDomainPtr dom, unsigned long newmem)
|
|||||||
return lxcDomainSetMemoryFlags(dom, newmem, VIR_DOMAIN_AFFECT_LIVE);
|
return lxcDomainSetMemoryFlags(dom, newmem, VIR_DOMAIN_AFFECT_LIVE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int lxcDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax)
|
||||||
|
{
|
||||||
|
return lxcDomainSetMemoryFlags(dom, newmax, VIR_DOMAIN_MEM_MAXIMUM);
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
lxcDomainSetMemoryParameters(virDomainPtr dom,
|
lxcDomainSetMemoryParameters(virDomainPtr dom,
|
||||||
virTypedParameterPtr params,
|
virTypedParameterPtr params,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user