[lxc-devel] [lxd/master] Properly revert memory limits on failure

stgraber on Github lxc-bot at linuxcontainers.org
Sat Mar 4 23:21:27 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 370 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170304/0b518b14/attachment.bin>
-------------- next part --------------
From 3ac6d174807b371336b50ab3a134f282f8216e6e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Sat, 4 Mar 2017 18:19:43 -0500
Subject: [PATCH] Properly revert memory limits on failure
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #3017

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/container_lxc.go | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index acb5217..efd7c72 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -3367,21 +3367,57 @@ func (c *containerLXC) Update(args containerArgs, userRequested bool) error {
 					memory = fmt.Sprintf("%d", valueInt)
 				}
 
+				// Store the old values for revert
+				oldMemswLimit := ""
+				if cgSwapAccounting {
+					oldMemswLimit, err = c.CGroupGet("memory.memsw.limit_in_bytes")
+					if err != nil {
+						oldMemswLimit = ""
+					}
+				}
+
+				oldLimit, err := c.CGroupGet("memory.limit_in_bytes")
+				if err != nil {
+					oldLimit = ""
+				}
+
+				oldSoftLimit, err := c.CGroupGet("memory.soft_limit_in_bytes")
+				if err != nil {
+					oldSoftLimit = ""
+				}
+
+				revertMemory := func() {
+					if oldSoftLimit != "" {
+						c.CGroupSet("memory.soft_limit_in_bytes", oldSoftLimit)
+					}
+
+					if oldLimit != "" {
+						c.CGroupSet("memory.limit_in_bytes", oldLimit)
+					}
+
+					if oldMemswLimit != "" {
+						c.CGroupSet("memory.memsw.limit_in_bytes", oldMemswLimit)
+					}
+				}
+
 				// Reset everything
 				if cgSwapAccounting {
 					err = c.CGroupSet("memory.memsw.limit_in_bytes", "-1")
 					if err != nil {
+						revertMemory()
 						return err
 					}
 				}
 
 				err = c.CGroupSet("memory.limit_in_bytes", "-1")
 				if err != nil {
+					revertMemory()
 					return err
 				}
 
 				err = c.CGroupSet("memory.soft_limit_in_bytes", "-1")
 				if err != nil {
+					revertMemory()
 					return err
 				}
 
@@ -3390,21 +3426,26 @@ func (c *containerLXC) Update(args containerArgs, userRequested bool) error {
 					// Set new limit
 					err = c.CGroupSet("memory.soft_limit_in_bytes", memory)
 					if err != nil {
+						revertMemory()
 						return err
 					}
 				} else {
 					if cgSwapAccounting && (memorySwap == "" || shared.IsTrue(memorySwap)) {
 						err = c.CGroupSet("memory.limit_in_bytes", memory)
 						if err != nil {
+							revertMemory()
 							return err
 						}
+
 						err = c.CGroupSet("memory.memsw.limit_in_bytes", memory)
 						if err != nil {
+							revertMemory()
 							return err
 						}
 					} else {
 						err = c.CGroupSet("memory.limit_in_bytes", memory)
 						if err != nil {
+							revertMemory()
 							return err
 						}
 					}


More information about the lxc-devel mailing list