[lxc-devel] [lxd/master] Add config key for container force shutdown timeout

ctrlrsf on Github lxc-bot at linuxcontainers.org
Wed May 25 04:12:04 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 1009 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160525/b15eee55/attachment.bin>
-------------- next part --------------
From c986ce4b69197214faf776808a9affdd611577b9 Mon Sep 17 00:00:00 2001
From: Rene Fragoso <ctrlrsf at gmail.com>
Date: Tue, 24 May 2016 23:48:09 -0400
Subject: [PATCH] Add config key for container force shutdown timeout

Currently if a container takes more than 30 seconds to shutdown, it is stopped
anyway. The 30 second timeout is hardcoded and this change makes the
timeout configurable.

This boot.force_shutdown_timeout container config key sets how long to wait
(in seconds) for a container to shutdown before it is force stopped.

This addresses issue https://github.com/lxc/lxd/issues/2027

Signed-off-by: Rene Fragoso <ctrlrsf at gmail.com>
---
 config/bash/lxd-client |  4 ++--
 doc/configuration.md   |  1 +
 lxd/container.go       |  2 ++
 lxd/containers.go      | 11 ++++++++++-
 test/suites/basic.sh   |  7 +++++++
 5 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/config/bash/lxd-client b/config/bash/lxd-client
index ac14dea..1482730 100644
--- a/config/bash/lxd-client
+++ b/config/bash/lxd-client
@@ -47,8 +47,8 @@ _have lxc && {
       images.auto_update_cached"
 
     container_keys="boot.autostart boot.autostart.delay boot.autostart.priority \
-      limits.cpu limits.cpu.allowance limits.cpu.priority limits.disk.priority \
-      limits.memory limits.memory.enforce limits.memory.swap \
+      boot.force_shutdown_timeout limits.cpu limits.cpu.allowance limits.cpu.priority \
+      limits.disk.priority limits.memory limits.memory.enforce limits.memory.swap \
       limits.memory.swap.priority limits.network.priority limits.processes \
       linux.kernel_modules raw.apparmor raw.lxc security.nesting \
       security.privileged volatile.apply_template volatile.base_image \
diff --git a/doc/configuration.md b/doc/configuration.md
index 9c94db7..079656f 100644
--- a/doc/configuration.md
+++ b/doc/configuration.md
@@ -69,6 +69,7 @@ Key                         | Type      | Default       | Live update   | Descri
 boot.autostart              | boolean   | false         | n/a           | Always start the container when LXD starts
 boot.autostart.delay        | integer   | 0             | n/a           | Number of seconds to wait after the container started before starting the next one
 boot.autostart.priority     | integer   | 0             | n/a           | What order to start the containers in (starting with highest)
+boot.force_shutdown_timeout | integer   | 30            | yes           | Seconds to wait for container to shutdown before it is force stopped
 environment.\*              | string    | -             | yes (exec)    | key/value environment variables to export to the container and set on exec
 limits.cpu                  | string    | - (all)       | yes           | Number or range of CPUs to expose to the container
 limits.cpu.allowance        | string    | 100%          | yes           | How much of the CPU can be used. Can be a percentage (e.g. 50%) for a soft limit or hard a chunk of time (25ms/100ms)
diff --git a/lxd/container.go b/lxd/container.go
index 9c196ca..bc6df58 100644
--- a/lxd/container.go
+++ b/lxd/container.go
@@ -83,6 +83,8 @@ func containerValidConfigKey(key string, value string) error {
 		return isInt64(key, value)
 	case "boot.autostart.priority":
 		return isInt64(key, value)
+	case "boot.force_shutdown_timeout":
+		return isInt64(key, value)
 	case "limits.cpu":
 		return nil
 	case "limits.cpu.allowance":
diff --git a/lxd/containers.go b/lxd/containers.go
index 16707ef..6152c14 100644
--- a/lxd/containers.go
+++ b/lxd/containers.go
@@ -152,10 +152,19 @@ func containersShutdown(d *Daemon) error {
 			return err
 		}
 
+		var timeoutSeconds int
+
+		value, ok := c.ExpandedConfig()["boot.force_shutdown_timeout"]
+		if ok {
+			timeoutSeconds, _ = strconv.Atoi(value)
+		} else {
+			timeoutSeconds = 30
+		}
+
 		if c.IsRunning() {
 			wg.Add(1)
 			go func() {
-				c.Shutdown(time.Second * 30)
+				c.Shutdown(time.Second * time.Duration(timeoutSeconds))
 				c.Stop(false)
 				wg.Done()
 			}()
diff --git a/test/suites/basic.sh b/test/suites/basic.sh
index b2f3eef..33b79c1 100644
--- a/test/suites/basic.sh
+++ b/test/suites/basic.sh
@@ -258,6 +258,13 @@ test_basic_usage() {
   lxc delete foo2
   lxc profile delete unconfined
 
+  # Test boot.force_shutdown_timeout config setting
+  lxc init testimage configtest --config boot.force_shutdown_timeout=45
+  [ "$(lxc config get configtest boot.force_shutdown_timeout)" -eq 45 ]
+  lxc config set configtest boot.force_shutdown_timeout 15
+  [ "$(lxc config get configtest boot.force_shutdown_timeout)" -eq 15 ]
+  lxc delete configtest
+
   # Ephemeral
   lxc launch testimage foo -e
 


More information about the lxc-devel mailing list