[libvirt] [PATCH 0/3] make containers memory settings API consistent

Typical HVM hypervisor has 2 memory settings - maximum and current memory. (there could be more memory options of course but let's consider these two and their correlation). Current limit is implemented via ballooning and is not greater than maximum. Containers are different, we need only one parameter to control their memory consumption - current memory. Memory settings API, domain XML schema and domain internal representation arises obviously for HVMs where we have 2 different parameters. How one should adopt it for containers? This patch series makes these 2 parameters synonyms for containers. 1. API - make maximum memory API behaviour same to just memory. 2. internally keep cur_balloon and initial (or total) the same. 3. ignore cur_ballon in XML (we do it already, just keep internal representation consistent) Nikolay Shirokovskiy (3): lxc: make maximum and current settings same conf: keep cur_balloon and initial_memory the same docs: update memory setting descripitons docs/formatdomain.html.in | 6 ++--- src/conf/domain_conf.c | 8 +++++-- src/libvirt-domain.c | 6 ++++- src/lxc/lxc_driver.c | 58 ++++++++++++----------------------------------- tools/virsh.pod | 6 +++++ 5 files changed, 34 insertions(+), 50 deletions(-) -- 1.8.3.1

And keep cur_balloon and initial_memory the same internally. Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com> --- src/lxc/lxc_driver.c | 58 +++++++++++++--------------------------------------- 1 file changed, 14 insertions(+), 44 deletions(-) diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 24b9622..6e298c6 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -725,54 +725,24 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, &persistentDef) < 0) goto cleanup; - if (flags & VIR_DOMAIN_MEM_MAXIMUM) { - if (flags & VIR_DOMAIN_AFFECT_LIVE) { - virReportError(VIR_ERR_OPERATION_INVALID, "%s", - _("Cannot resize the max memory " - "on an active domain")); + if (flags & VIR_DOMAIN_AFFECT_CONFIG) { + virDomainDefSetMemoryTotal(persistentDef, newmem); + persistentDef->mem.cur_balloon = newmem; + if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) goto cleanup; - } - - if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - virDomainDefSetMemoryTotal(persistentDef, newmem); - if (persistentDef->mem.cur_balloon > newmem) - 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 = virDomainDefGetMemoryActual(vm->def); - if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - if (!oldmax || oldmax > virDomainDefGetMemoryActual(persistentDef)) - oldmax = virDomainDefGetMemoryActual(persistentDef); - } + } - if (newmem > oldmax) { - virReportError(VIR_ERR_INVALID_ARG, - "%s", _("Cannot set memory higher than max memory")); + 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_LIVE) { - if (virCgroupSetMemory(priv->cgroup, newmem) < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, - "%s", _("Failed to set memory for domain")); - goto cleanup; - } - - vm->def->mem.cur_balloon = newmem; - if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0) - goto cleanup; - } - - if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - persistentDef->mem.cur_balloon = newmem; - if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) - goto cleanup; - } + virDomainDefSetMemoryTotal(vm->def, newmem); + vm->def->mem.cur_balloon = newmem; + if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0) + goto cleanup; } ret = 0; @@ -792,7 +762,7 @@ static int lxcDomainSetMemory(virDomainPtr dom, unsigned long newmem) static int lxcDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) { - return lxcDomainSetMemoryFlags(dom, newmax, VIR_DOMAIN_MEM_MAXIMUM); + return lxcDomainSetMemoryFlags(dom, newmax, VIR_DOMAIN_MEM_CURRENT); } static int -- 1.8.3.1

Apply this rule on domain definitions parsing for all containers. Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com> --- src/conf/domain_conf.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index a9706b0..1557bf9 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -3641,9 +3641,13 @@ virDomainDefPostParseMemory(virDomainDefPtr def, return -1; } - if (def->mem.cur_balloon > virDomainDefGetMemoryActual(def) || - def->mem.cur_balloon == 0) + if (def->os.type != VIR_DOMAIN_OSTYPE_EXE) { + if (def->mem.cur_balloon > virDomainDefGetMemoryActual(def) || + def->mem.cur_balloon == 0) + def->mem.cur_balloon = virDomainDefGetMemoryActual(def); + } else { def->mem.cur_balloon = virDomainDefGetMemoryActual(def); + } if ((def->mem.max_memory || def->mem.memory_slots) && !(def->mem.max_memory && def->mem.memory_slots)) { -- 1.8.3.1

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com> --- docs/formatdomain.html.in | 6 +++--- src/libvirt-domain.c | 6 +++++- tools/virsh.pod | 6 ++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in index b3187bb..fe0b00e 100644 --- a/docs/formatdomain.html.in +++ b/docs/formatdomain.html.in @@ -802,9 +802,9 @@ <dd>The actual allocation of memory for the guest. This value can be less than the maximum allocation, to allow for ballooning up the guests memory on the fly. If this is omitted, it defaults - to the same value as the <code>memory</code> element. - The <code>unit</code> attribute behaves the same as - for <code>memory</code>.</dd> + to the same value as the <code>memory</code> element. For containers + this value is ignored. The <code>unit</code> attribute behaves + the same as for <code>memory</code>.</dd> </dl> diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c index 9491845..fc8818c 100644 --- a/src/libvirt-domain.c +++ b/src/libvirt-domain.c @@ -1833,6 +1833,9 @@ virDomainGetMaxMemory(virDomainPtr domain) * or both configurations are changed; for more control, use * virDomainSetMemoryFlags(). * + * For containers (domain os type VIR_DOMAIN_OSTYPE_EXE) this is not + * maximum but rather amount of physical memory as in virDomainSetMemory. + * * Returns 0 in case of success and -1 in case of failure. */ int @@ -1945,7 +1948,8 @@ virDomainSetMemory(virDomainPtr domain, unsigned long memory) * modifies persistent setup, while an active domain is hypervisor-dependent * on whether just live or both live and persistent state is changed. * If VIR_DOMAIN_MEM_MAXIMUM is set, the change affects domain's maximum memory - * size rather than current memory size. + * size rather than current memory size, except for containers (domain os + * type is VIR_DOMAIN_OSTYPE_EXE) for which ones this flag is ignored. * Not all hypervisors can support all flag combinations. * * Returns 0 in case of success, -1 in case of failure. diff --git a/tools/virsh.pod b/tools/virsh.pod index e830c59..94af367 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -2029,6 +2029,9 @@ the value from the host, use the B<virsh memtune> command. In order to view the current memory in use and the maximum value allowed to set memory, use the B<virsh dominfo> command. +For container guests (LXC, vz containers) total memory (--hard-limit of +memtune) will be set. + =item B<set-user-password> I<domain> I<user> I<password> [I<--encrypted>] Set the password for the I<user> account in the guest domain. @@ -2063,6 +2066,9 @@ up to the nearest kibibyte. Some hypervisors require a larger granularity than KiB, and requests that are not an even multiple will be rounded up. For example, vSphere/ESX rounds the parameter up to mebibytes (1024 kibibytes). +For container guests (LXC, vz containers) this will set total memory +(--hard-limit of memtune) just as B<virsh setmem>. + =item B<memtune> I<domain> [I<--hard-limit> B<size>] [I<--soft-limit> B<size>] [I<--swap-hard-limit> B<size>] [I<--min-guarantee> B<size>] [[I<--config>] [I<--live>] | [I<--current>]] -- 1.8.3.1
participants (1)
-
Nikolay Shirokovskiy