[pve-devel] [PATCH v2 container 4/4] fix growing of a running container's memory limit

Wolfgang Bumiller w.bumiller at proxmox.com
Mon Feb 8 08:48:28 CET 2016


Since the memory cgroup has a memory and a "total" value
depending on whether you're increasing or decreasing the
values you have to set then in a working order. (Eg. you
can't reduce the total amount to less than the swap limit
or grow the swap limit to more than the total one.)
---
 src/PVE/LXC.pm | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index f761f33..6a3489a 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -1308,13 +1308,22 @@ sub update_pct_config {
     my $wanted_swap =  PVE::Tools::extract_param($param, 'swap');
     if (defined($wanted_memory) || defined($wanted_swap)) {
 
-	$wanted_memory //= ($conf->{memory} || 512);
-	$wanted_swap //=  ($conf->{swap} || 0);
+	my $old_memory = ($conf->{memory} || 512);
+	my $old_swap = ($conf->{swap} || 0);
+
+	$wanted_memory //= $old_memory;
+	$wanted_swap //= $old_swap;
 
         my $total = $wanted_memory + $wanted_swap;
 	if ($running) {
-	    write_cgroup_value("memory", $vmid, "memory.limit_in_bytes", int($wanted_memory*1024*1024));
-	    write_cgroup_value("memory", $vmid, "memory.memsw.limit_in_bytes", int($total*1024*1024));
+	    my $old_total = $old_memory + $old_swap;
+	    if ($total > $old_total) {
+		write_cgroup_value("memory", $vmid, "memory.memsw.limit_in_bytes", int($total*1024*1024));
+		write_cgroup_value("memory", $vmid, "memory.limit_in_bytes", int($wanted_memory*1024*1024));
+	    } else {
+		write_cgroup_value("memory", $vmid, "memory.limit_in_bytes", int($wanted_memory*1024*1024));
+		write_cgroup_value("memory", $vmid, "memory.memsw.limit_in_bytes", int($total*1024*1024));
+	    }
 	}
 	$conf->{memory} = $wanted_memory;
 	$conf->{swap} = $wanted_swap;
-- 
2.1.4





More information about the pve-devel mailing list