From e8340a8b7994d3ad932b80ec8499bb92393ebc35 Mon Sep 17 00:00:00 2001 From: Taku Izumi Date: Wed, 2 Mar 2011 17:07:48 +0900 Subject: [PATCH] setmem: introduce a new libvirt API (virDomainSetMemoryFlags) This patch introduces a new libvirt API (virDomainSetMemoryFlags) and a flag (virDomainMemoryModFlags). Signed-off-by: Taku Izumi --- AUTHORS | 1 + include/libvirt/libvirt.h.in | 10 ++++++ src/driver.h | 5 +++ src/esx/esx_driver.c | 1 + src/libvirt.c | 62 ++++++++++++++++++++++++++++++++++++ src/libvirt_public.syms | 1 + src/lxc/lxc_driver.c | 1 + src/opennebula/one_driver.c | 1 + src/openvz/openvz_driver.c | 1 + src/phyp/phyp_driver.c | 1 + src/qemu/qemu_driver.c | 1 + src/remote/remote_driver.c | 1 + src/test/test_driver.c | 1 + src/uml/uml_driver.c | 1 + src/vbox/vbox_tmpl.c | 1 + src/vmware/vmware_driver.c | 1 + src/xen/xen_driver.c | 1 + src/xenapi/xenapi_driver.c | 1 + 18 files changed, 92 insertions(+) diff --git a/AUTHORS b/AUTHORS index f2292074e3..c0c1780a09 100644 --- a/AUTHORS +++ b/AUTHORS @@ -157,6 +157,7 @@ Patches have also been contributed by: Christophe Fergeau Markus Groß Phil Petty + Taku Izumi [....send patches to get your name here....] diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in index 618b350c35..10f8966045 100644 --- a/include/libvirt/libvirt.h.in +++ b/include/libvirt/libvirt.h.in @@ -780,6 +780,13 @@ int virDomainGetMemoryParameters(virDomainPtr domain, virMemoryParameterPtr params, int *nparams, unsigned int flags); +/* Memory size modification flags. */ +typedef enum { + VIR_DOMAIN_MEM_LIVE = (1 << 0), /* affect active domain */ + VIR_DOMAIN_MEM_CONFIG = (1 << 1), /* affect next boot */ +} virDomainMemoryModFlags; + + /* * Dynamic control of domains */ @@ -795,6 +802,9 @@ int virDomainSetMaxMemory (virDomainPtr domain, unsigned long memory); int virDomainSetMemory (virDomainPtr domain, unsigned long memory); +int virDomainSetMemoryFlags (virDomainPtr domain, + unsigned long memory, + unsigned int flags); int virDomainGetMaxVcpus (virDomainPtr domain); int virDomainGetSecurityLabel (virDomainPtr domain, virSecurityLabelPtr seclabel); diff --git a/src/driver.h b/src/driver.h index c65a4b9946..da792d0040 100644 --- a/src/driver.h +++ b/src/driver.h @@ -133,6 +133,10 @@ typedef int typedef int (*virDrvDomainSetMemory) (virDomainPtr domain, unsigned long memory); +typedef int + (*virDrvDomainSetMemoryFlags) (virDomainPtr domain, + unsigned long memory, + unsigned int flags); typedef int (*virDrvDomainSetMemoryParameters) (virDomainPtr domain, @@ -536,6 +540,7 @@ struct _virDriver { virDrvDomainGetMaxMemory domainGetMaxMemory; virDrvDomainSetMaxMemory domainSetMaxMemory; virDrvDomainSetMemory domainSetMemory; + virDrvDomainSetMemoryFlags domainSetMemoryFlags; virDrvDomainGetInfo domainGetInfo; virDrvDomainSave domainSave; virDrvDomainRestore domainRestore; diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c index 13374b75e4..37e104e902 100644 --- a/src/esx/esx_driver.c +++ b/src/esx/esx_driver.c @@ -4593,6 +4593,7 @@ static virDriver esxDriver = { esxDomainGetMaxMemory, /* domainGetMaxMemory */ esxDomainSetMaxMemory, /* domainSetMaxMemory */ esxDomainSetMemory, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ esxDomainGetInfo, /* domainGetInfo */ NULL, /* domainSave */ NULL, /* domainRestore */ diff --git a/src/libvirt.c b/src/libvirt.c index e4b451e3b3..1b3b884b21 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -2846,6 +2846,68 @@ error: return -1; } +/* + * virDomainSetMemoryFlags + * @domain: a domain object or NULL + * @memory: the memory size in kilobytes + * @flags: an OR'ed set of virDomainMemoryModFlags + * + * Dynamically change the target amount of physical memory allocated to a + * domain. If domain is NULL, then this change the amount of memory reserved + * to Domain0 i.e. the domain where the application runs. + * This funcation may requires privileged access to the hypervisor. + * + * @flags must include VIR_DOMAIN_MEM_LIVE to affect a running + * domain (which may fail if domain is not active), or + * VIR_DOMAIN_MEM_CONFIG to affect the next boot via the XML + * description of the domain. Both flags may be set. + * + * Returns 0 in case of success, -1 in case of failure. + */ + +int +virDomainSetMemoryFlags(virDomainPtr domain, unsigned long memory, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "memory=%lu flags=%u", memory, flags); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_DOMAIN(domain)) { + virLibDomainError(VIR_ERR_INVALID_DOMAIN, __FUNCTION__); + virDispatchError(NULL); + return -1; + } + + if (domain->conn->flags & VIR_CONNECT_RO) { + virLibDomainError(VIR_ERR_OPERATION_DENIED, __FUNCTION__); + goto error; + } + + if (memory < 4096 || + (flags & (VIR_DOMAIN_MEM_LIVE | VIR_DOMAIN_MEM_CONFIG)) == 0) { + virLibDomainError(VIR_ERR_INVALID_ARG, __FUNCTION__); + goto error; + } + + conn = domain->conn; + + if (conn->driver->domainSetMemoryFlags) { + int ret; + ret = conn->driver->domainSetMemoryFlags(domain, memory, flags); + if (ret < 0) + goto error; + return ret; + } + +error: + virDispatchError(domain->conn); + return -1; +} + + /** * virDomainSetMemoryParameters: * @domain: pointer to domain object diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index cca8d085e7..cfbd8dfe29 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -426,6 +426,7 @@ LIBVIRT_0.8.8 { LIBVIRT_0.9.0 { global: + virDomainSetMemoryFlags; virEventRegisterDefaultImpl; virEventRunDefaultImpl; } LIBVIRT_0.8.8; diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 5b6f784710..4ddeffd50e 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -2851,6 +2851,7 @@ static virDriver lxcDriver = { lxcDomainGetMaxMemory, /* domainGetMaxMemory */ lxcDomainSetMaxMemory, /* domainSetMaxMemory */ lxcDomainSetMemory, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ lxcDomainGetInfo, /* domainGetInfo */ NULL, /* domainSave */ NULL, /* domainRestore */ diff --git a/src/opennebula/one_driver.c b/src/opennebula/one_driver.c index a0654e245a..163d008149 100644 --- a/src/opennebula/one_driver.c +++ b/src/opennebula/one_driver.c @@ -750,6 +750,7 @@ static virDriver oneDriver = { NULL, /* domainGetMaxMemory */ NULL, /* domainSetMaxMemory */ NULL, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ oneDomainGetInfo, /* domainGetInfo */ NULL, /* domainSave */ NULL, /* domainRestore */ diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index 9ee6de517f..7119bde620 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -1571,6 +1571,7 @@ static virDriver openvzDriver = { NULL, /* domainGetMaxMemory */ NULL, /* domainSetMaxMemory */ NULL, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ openvzDomainGetInfo, /* domainGetInfo */ NULL, /* domainSave */ NULL, /* domainRestore */ diff --git a/src/phyp/phyp_driver.c b/src/phyp/phyp_driver.c index d954f2a964..84323a7e0b 100644 --- a/src/phyp/phyp_driver.c +++ b/src/phyp/phyp_driver.c @@ -3973,6 +3973,7 @@ static virDriver phypDriver = { NULL, /* domainGetMaxMemory */ NULL, /* domainSetMaxMemory */ NULL, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ phypDomainGetInfo, /* domainGetInfo */ NULL, /* domainSave */ NULL, /* domainRestore */ diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 2208b12d37..2f151445b4 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -6854,6 +6854,7 @@ static virDriver qemuDriver = { qemudDomainGetMaxMemory, /* domainGetMaxMemory */ NULL, /* domainSetMaxMemory */ qemudDomainSetMemory, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ qemudDomainGetInfo, /* domainGetInfo */ qemudDomainSave, /* domainSave */ qemudDomainRestore, /* domainRestore */ diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 58d7f9642b..e2e00eb1b9 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -10876,6 +10876,7 @@ static virDriver remote_driver = { remoteDomainGetMaxMemory, /* domainGetMaxMemory */ remoteDomainSetMaxMemory, /* domainSetMaxMemory */ remoteDomainSetMemory, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ remoteDomainGetInfo, /* domainGetInfo */ remoteDomainSave, /* domainSave */ remoteDomainRestore, /* domainRestore */ diff --git a/src/test/test_driver.c b/src/test/test_driver.c index b168570298..438f5a3cb6 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -5365,6 +5365,7 @@ static virDriver testDriver = { testGetMaxMemory, /* domainGetMaxMemory */ testSetMaxMemory, /* domainSetMaxMemory */ testSetMemory, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ testGetDomainInfo, /* domainGetInfo */ testDomainSave, /* domainSave */ testDomainRestore, /* domainRestore */ diff --git a/src/uml/uml_driver.c b/src/uml/uml_driver.c index 7cacadbbbd..671fcc5c86 100644 --- a/src/uml/uml_driver.c +++ b/src/uml/uml_driver.c @@ -2167,6 +2167,7 @@ static virDriver umlDriver = { umlDomainGetMaxMemory, /* domainGetMaxMemory */ umlDomainSetMaxMemory, /* domainSetMaxMemory */ umlDomainSetMemory, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ umlDomainGetInfo, /* domainGetInfo */ NULL, /* domainSave */ NULL, /* domainRestore */ diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index 8848cc0933..342ab5ec97 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -8555,6 +8555,7 @@ virDriver NAME(Driver) = { NULL, /* domainGetMaxMemory */ NULL, /* domainSetMaxMemory */ vboxDomainSetMemory, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ vboxDomainGetInfo, /* domainGetInfo */ vboxDomainSave, /* domainSave */ NULL, /* domainRestore */ diff --git a/src/vmware/vmware_driver.c b/src/vmware/vmware_driver.c index 2b07b13c37..064b4cbd05 100644 --- a/src/vmware/vmware_driver.c +++ b/src/vmware/vmware_driver.c @@ -925,6 +925,7 @@ static virDriver vmwareDriver = { NULL, /* domainGetMaxMemory */ NULL, /* domainSetMaxMemory */ NULL, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ vmwareDomainGetInfo, /* domainGetInfo */ NULL, /* domainSave */ NULL, /* domainRestore */ diff --git a/src/xen/xen_driver.c b/src/xen/xen_driver.c index 979f9e085f..a777225204 100644 --- a/src/xen/xen_driver.c +++ b/src/xen/xen_driver.c @@ -2034,6 +2034,7 @@ static virDriver xenUnifiedDriver = { xenUnifiedDomainGetMaxMemory, /* domainGetMaxMemory */ xenUnifiedDomainSetMaxMemory, /* domainSetMaxMemory */ xenUnifiedDomainSetMemory, /* domainSetMemory */ + NULL, /*domainSetMemoryFlags */ xenUnifiedDomainGetInfo, /* domainGetInfo */ xenUnifiedDomainSave, /* domainSave */ xenUnifiedDomainRestore, /* domainRestore */ diff --git a/src/xenapi/xenapi_driver.c b/src/xenapi/xenapi_driver.c index 7851e931d7..91c91cd73e 100644 --- a/src/xenapi/xenapi_driver.c +++ b/src/xenapi/xenapi_driver.c @@ -1803,6 +1803,7 @@ static virDriver xenapiDriver = { xenapiDomainGetMaxMemory, /* domainGetMaxMemory */ xenapiDomainSetMaxMemory, /* domainSetMaxMemory */ NULL, /* domainSetMemory */ + NULL, /* domainSetMemoryFlags */ xenapiDomainGetInfo, /* domainGetInfo */ NULL, /* domainSave */ NULL, /* domainRestore */