[lxc-devel] [lxd/master] lxd/container_lxc: check if container is running

brauner on Github lxc-bot at linuxcontainers.org
Fri Oct 14 10:03:34 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 671 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161014/79413708/attachment.bin>
-------------- next part --------------
From 5d719db0b3c4e1f23cba4a6e63e863f9a50cea62 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at canonical.com>
Date: Fri, 14 Oct 2016 11:59:37 +0200
Subject: [PATCH] lxd/container_lxc: check if container is running

Only try to Freeze() or Stop() if the container is running. Otherwise we will
cause unnecessary errors when users e.g. reboot the container via

	lxc exec foo reboot

in which case the init systems does the work and we would be trying to
Freeze()/Stop() a container that is not running anymore.

Signed-off-by: Christian Brauner <christian.brauner at canonical.com>
---
 lxd/container_lxc.go | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 830265d..49254c3 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -1690,6 +1690,11 @@ func (c *containerLXC) Stop(stateful bool) error {
 		return err
 	}
 
+	if !c.IsRunning() {
+		shared.LogDebug("Not stopping container as it is not running.", ctxMap)
+		return nil
+	}
+
 	// Attempt to freeze the container first, helps massively with fork bombs
 	c.Freeze()
 
@@ -1883,6 +1888,11 @@ func (c *containerLXC) Freeze() error {
 		return err
 	}
 
+	if !c.IsRunning() {
+		shared.LogDebug("Not freezing container as it is not running.", ctxMap)
+		return nil
+	}
+
 	err = c.c.Freeze()
 	if err != nil {
 		ctxMap["err"] = err


More information about the lxc-devel mailing list