提交 bf7f7983 编写于 作者: M Markus Groß 提交者: Jim Fehlig

Support maximum and current memory flags in libxl driver

Add support to set the maximum memory of the domain.
Also add support to change the memory of the current
state of the domain, which translates to a running
domain or the config of the domain.

Based on the code from the qemu driver.
上级 db0274c9
......@@ -1493,22 +1493,19 @@ cleanup:
}
static int
libxlDomainSetMemoryFlags(virDomainPtr dom, unsigned long memory,
libxlDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
unsigned int flags)
{
libxlDriverPrivatePtr driver = dom->conn->privateData;
libxlDomainObjPrivatePtr priv;
virDomainObjPtr vm;
virDomainDefPtr def = NULL;
virDomainDefPtr persistentDef = NULL;
bool isActive;
int ret = -1;
virCheckFlags(VIR_DOMAIN_MEM_LIVE |
VIR_DOMAIN_MEM_CONFIG, -1);
if ((flags & (VIR_DOMAIN_MEM_LIVE | VIR_DOMAIN_MEM_CONFIG)) == 0) {
libxlError(VIR_ERR_INVALID_ARG,
_("invalid flag combination: (0x%x)"), flags);
}
VIR_DOMAIN_MEM_CONFIG |
VIR_DOMAIN_MEM_MAXIMUM, -1);
libxlDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid);
......@@ -1519,13 +1516,22 @@ libxlDomainSetMemoryFlags(virDomainPtr dom, unsigned long memory,
goto cleanup;
}
if (memory > vm->def->mem.max_balloon) {
libxlError(VIR_ERR_INVALID_ARG, "%s",
_("cannot set memory higher than max memory"));
goto cleanup;
isActive = virDomainObjIsActive(vm);
if (flags == VIR_DOMAIN_MEM_CURRENT) {
if (isActive)
flags = VIR_DOMAIN_MEM_LIVE;
else
flags = VIR_DOMAIN_MEM_CONFIG;
}
if (flags == VIR_DOMAIN_MEM_MAXIMUM) {
if (isActive)
flags = VIR_DOMAIN_MEM_LIVE | VIR_DOMAIN_MEM_MAXIMUM;
else
flags = VIR_DOMAIN_MEM_CONFIG | VIR_DOMAIN_MEM_MAXIMUM;
}
if (!virDomainObjIsActive(vm) && (flags & VIR_DOMAIN_MEM_LIVE)) {
if (!isActive && (flags & VIR_DOMAIN_MEM_LIVE)) {
libxlError(VIR_ERR_OPERATION_INVALID, "%s",
_("cannot set memory on an inactive domain"));
goto cleanup;
......@@ -1537,31 +1543,63 @@ libxlDomainSetMemoryFlags(virDomainPtr dom, unsigned long memory,
_("cannot change persistent config of a transient domain"));
goto cleanup;
}
if (!(def = virDomainObjGetPersistentDef(driver->caps, vm)))
if (!(persistentDef = virDomainObjGetPersistentDef(driver->caps, vm)))
goto cleanup;
}
if (flags & VIR_DOMAIN_MEM_LIVE) {
priv = vm->privateData;
if (flags & VIR_DOMAIN_MEM_MAXIMUM) {
/* resize the maximum memory */
if (libxl_set_memory_target(&priv->ctx, dom->id, memory, 0,
/* force */ 1) < 0) {
libxlError(VIR_ERR_INTERNAL_ERROR,
_("Failed to set memory for domain '%d'"
" with libxenlight"), dom->id);
if (flags & VIR_DOMAIN_MEM_LIVE) {
priv = vm->privateData;
if (libxl_domain_setmaxmem(&priv->ctx, dom->id, newmem) < 0) {
libxlError(VIR_ERR_INTERNAL_ERROR,
_("Failed to set maximum memory for domain '%d'"
" with libxenlight"), dom->id);
goto cleanup;
}
}
if (flags & VIR_DOMAIN_MEM_CONFIG) {
/* Help clang 2.8 decipher the logic flow. */
sa_assert(persistentDef);
persistentDef->mem.max_balloon = newmem;
if (persistentDef->mem.cur_balloon > newmem)
persistentDef->mem.cur_balloon = newmem;
ret = virDomainSaveConfig(driver->configDir, persistentDef);
goto cleanup;
}
}
ret = 0;
} else {
/* resize the current memory */
if (flags & VIR_DOMAIN_MEM_CONFIG) {
/* clang 2.9 needs an extra hint. */
sa_assert(def);
def->mem.cur_balloon = memory;
ret = virDomainSaveConfig(driver->configDir, def);
if (newmem > vm->def->mem.max_balloon) {
libxlError(VIR_ERR_INVALID_ARG, "%s",
_("cannot set memory higher than max memory"));
goto cleanup;
}
if (flags & VIR_DOMAIN_MEM_LIVE) {
priv = vm->privateData;
if (libxl_set_memory_target(&priv->ctx, dom->id, newmem, 0,
/* force */ 1) < 0) {
libxlError(VIR_ERR_INTERNAL_ERROR,
_("Failed to set memory for domain '%d'"
" with libxenlight"), dom->id);
goto cleanup;
}
}
if (flags & VIR_DOMAIN_MEM_CONFIG) {
sa_assert(persistentDef);
persistentDef->mem.cur_balloon = newmem;
ret = virDomainSaveConfig(driver->configDir, persistentDef);
goto cleanup;
}
}
ret = 0;
cleanup:
if (vm)
virDomainObjUnlock(vm);
......@@ -1574,6 +1612,12 @@ libxlDomainSetMemory(virDomainPtr dom, unsigned long memory)
return libxlDomainSetMemoryFlags(dom, memory, VIR_DOMAIN_MEM_LIVE);
}
static int
libxlDomainSetMaxMemory(virDomainPtr dom, unsigned long memory)
{
return libxlDomainSetMemoryFlags(dom, memory, VIR_DOMAIN_MEM_MAXIMUM);
}
static int
libxlDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
{
......@@ -3552,6 +3596,7 @@ static virDriver libxlDriver = {
.domainDestroy = libxlDomainDestroy, /* 0.9.0 */
.domainGetOSType = libxlDomainGetOSType, /* 0.9.0 */
.domainGetMaxMemory = libxlDomainGetMaxMemory, /* 0.9.0 */
.domainSetMaxMemory = libxlDomainSetMaxMemory, /* 0.9.2 */
.domainSetMemory = libxlDomainSetMemory, /* 0.9.0 */
.domainSetMemoryFlags = libxlDomainSetMemoryFlags, /* 0.9.0 */
.domainGetInfo = libxlDomainGetInfo, /* 0.9.0 */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册