[lxc-devel] [lxd/master] lxd/containers: Only init the config if needed

stgraber on Github lxc-bot at linuxcontainers.org
Wed Nov 15 19:16:26 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171115/f143282b/attachment.bin>
-------------- next part --------------
From d3dca7b2cbb8ca7bc3eed580d9dd20822ab8fe1c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 15 Nov 2017 14:15:28 -0500
Subject: [PATCH] lxd/containers: Only init the config if needed
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 3e52bb11d..f223fb702 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -485,6 +485,7 @@ type containerLXC struct {
 
 	// Cache
 	c        *lxc.Container
+	cConfig  bool
 	db       *db.Node
 	state    *state.State
 	idmapset *idmap.IdmapSet
@@ -827,7 +828,7 @@ func (c *containerLXC) init() error {
 	return nil
 }
 
-func (c *containerLXC) initLXC() error {
+func (c *containerLXC) initLXC(config bool) error {
 	// No need to go through all that for snapshots
 	if c.IsSnapshot() {
 		return nil
@@ -840,7 +841,9 @@ func (c *containerLXC) initLXC() error {
 
 	// Check if already initialized
 	if c.c != nil {
-		return nil
+		if !config || c.cConfig {
+			return nil
+		}
 	}
 
 	// Load the go-lxc struct
@@ -849,6 +852,33 @@ func (c *containerLXC) initLXC() error {
 		return err
 	}
 
+	// Setup logging
+	logfile := c.LogFilePath()
+
+	err = cc.SetLogFile(logfile)
+	if err != nil {
+		return err
+	}
+
+	logLevel := "warn"
+	if debug {
+		logLevel = "trace"
+	} else if verbose {
+		logLevel = "info"
+	}
+
+	err = lxcSetConfigItem(cc, "lxc.log.level", logLevel)
+	if err != nil {
+		return err
+	}
+
+	// Allow for lightweight init
+	c.cConfig = config
+	if !config {
+		c.c = cc
+		return nil
+	}
+
 	// Base config
 	toDrop := "sys_time sys_module sys_rawio"
 	if !c.state.OS.AppArmorStacking || c.state.OS.AppArmorStacked {
@@ -986,26 +1016,6 @@ func (c *containerLXC) initLXC() error {
 		}
 	}
 
-	// Setup logging
-	logfile := c.LogFilePath()
-
-	err = cc.SetLogFile(logfile)
-	if err != nil {
-		return err
-	}
-
-	logLevel := "warn"
-	if debug {
-		logLevel = "trace"
-	} else if verbose {
-		logLevel = "info"
-	}
-
-	err = lxcSetConfigItem(cc, "lxc.log.level", logLevel)
-	if err != nil {
-		return err
-	}
-
 	// Setup architecture
 	personality, err := osarch.ArchitecturePersonality(c.architecture)
 	if err != nil {
@@ -1662,7 +1672,7 @@ func (c *containerLXC) setupUnixDevice(devType string, dev types.Device, major i
 // Start functions
 func (c *containerLXC) startCommon() (string, error) {
 	// Load the go-lxc struct
-	err := c.initLXC()
+	err := c.initLXC(true)
 	if err != nil {
 		return "", err
 	}
@@ -2385,7 +2395,7 @@ func (c *containerLXC) Stop(stateful bool) error {
 	}
 
 	// Load the go-lxc struct
-	err = c.initLXC()
+	err = c.initLXC(false)
 	if err != nil {
 		op.Done(err)
 		logger.Error("Failed stopping container", ctxMap)
@@ -2445,7 +2455,7 @@ func (c *containerLXC) Shutdown(timeout time.Duration) error {
 	logger.Info("Shutting down container", ctxMap)
 
 	// Load the go-lxc struct
-	err = c.initLXC()
+	err = c.initLXC(false)
 	if err != nil {
 		op.Done(err)
 		logger.Error("Failed shutting down container", ctxMap)
@@ -2588,7 +2598,7 @@ func (c *containerLXC) Freeze() error {
 	logger.Info("Freezing container", ctxMap)
 
 	// Load the go-lxc struct
-	err := c.initLXC()
+	err := c.initLXC(false)
 	if err != nil {
 		ctxMap["err"] = err
 		logger.Error("Failed freezing container", ctxMap)
@@ -2626,7 +2636,7 @@ func (c *containerLXC) Unfreeze() error {
 	logger.Info("Unfreezing container", ctxMap)
 
 	// Load the go-lxc struct
-	err := c.initLXC()
+	err := c.initLXC(false)
 	if err != nil {
 		logger.Error("Failed unfreezing container", ctxMap)
 		return err
@@ -2651,6 +2661,12 @@ func (c *containerLXC) getLxcState() (lxc.State, error) {
 		return lxc.StateMap["STOPPED"], nil
 	}
 
+	// Load the go-lxc struct
+	err := c.initLXC(false)
+	if err != nil {
+		return lxc.StateMap["STOPPED"], err
+	}
+
 	monitor := make(chan lxc.State, 1)
 
 	go func(c *lxc.Container) {
@@ -2666,12 +2682,6 @@ func (c *containerLXC) getLxcState() (lxc.State, error) {
 }
 
 func (c *containerLXC) Render() (interface{}, interface{}, error) {
-	// Load the go-lxc struct
-	err := c.initLXC()
-	if err != nil {
-		return nil, nil, err
-	}
-
 	// Ignore err as the arch string on error is correct (unknown)
 	architectureName, _ := osarch.ArchitectureName(c.architecture)
 
@@ -2723,12 +2733,6 @@ func (c *containerLXC) Render() (interface{}, interface{}, error) {
 }
 
 func (c *containerLXC) RenderState() (*api.ContainerState, error) {
-	// Load the go-lxc struct
-	err := c.initLXC()
-	if err != nil {
-		return nil, err
-	}
-
 	cState, err := c.getLxcState()
 	if err != nil {
 		return nil, err
@@ -3114,6 +3118,7 @@ func (c *containerLXC) Rename(newName string) error {
 
 	// Invalidate the go-lxc cache
 	c.c = nil
+	c.cConfig = false
 
 	// Update lease files
 	networkUpdateStatic(c.state, "")
@@ -3125,7 +3130,7 @@ func (c *containerLXC) Rename(newName string) error {
 
 func (c *containerLXC) CGroupGet(key string) (string, error) {
 	// Load the go-lxc struct
-	err := c.initLXC()
+	err := c.initLXC(false)
 	if err != nil {
 		return "", err
 	}
@@ -3141,7 +3146,7 @@ func (c *containerLXC) CGroupGet(key string) (string, error) {
 
 func (c *containerLXC) CGroupSet(key string, value string) error {
 	// Load the go-lxc struct
-	err := c.initLXC()
+	err := c.initLXC(false)
 	if err != nil {
 		return err
 	}
@@ -3396,7 +3401,8 @@ func (c *containerLXC) Update(args db.ContainerArgs, userRequested bool) error {
 			c.localDevices = oldLocalDevices
 			c.profiles = oldProfiles
 			c.c = nil
-			c.initLXC()
+			c.cConfig = false
+			c.initLXC(true)
 			deviceTaskSchedulerTrigger("container", c.name, "changed")
 		}
 	}()
@@ -3455,7 +3461,8 @@ func (c *containerLXC) Update(args db.ContainerArgs, userRequested bool) error {
 
 	// Run through initLXC to catch anything we missed
 	c.c = nil
-	err = c.initLXC()
+	c.cConfig = false
+	err = c.initLXC(true)
 	if err != nil {
 		return err
 	}
@@ -4567,7 +4574,7 @@ func (c *containerLXC) Migrate(cmd uint, stateDir string, function string, stop
 		}
 
 	} else {
-		err := c.initLXC()
+		err := c.initLXC(true)
 		if err != nil {
 			return err
 		}
@@ -6419,7 +6426,7 @@ func (c *containerLXC) removeNetworkFilters() error {
 
 func (c *containerLXC) insertNetworkDevice(name string, m types.Device) error {
 	// Load the go-lxc struct
-	err := c.initLXC()
+	err := c.initLXC(false)
 	if err != nil {
 		return nil
 	}
@@ -6461,14 +6468,8 @@ func (c *containerLXC) insertNetworkDevice(name string, m types.Device) error {
 }
 
 func (c *containerLXC) removeNetworkDevice(name string, m types.Device) error {
-	// Load the go-lxc struct
-	err := c.initLXC()
-	if err != nil {
-		return nil
-	}
-
 	// Fill in some fields from volatile
-	m, err = c.fillNetworkDevice(name, m)
+	m, err := c.fillNetworkDevice(name, m)
 	if err != nil {
 		return nil
 	}
@@ -7022,19 +7023,13 @@ func (c *containerLXC) setNetworkLimits(name string, m types.Device) error {
 		return fmt.Errorf("Network limits are only supported on bridged and p2p interfaces")
 	}
 
-	// Load the go-lxc struct
-	err := c.initLXC()
-	if err != nil {
-		return err
-	}
-
 	// Check that the container is running
 	if !c.IsRunning() {
 		return fmt.Errorf("Can't set network limits on stopped container")
 	}
 
 	// Fill in some fields from volatile
-	m, err = c.fillNetworkDevice(name, m)
+	m, err := c.fillNetworkDevice(name, m)
 	if err != nil {
 		return nil
 	}
@@ -7179,7 +7174,7 @@ func (c *containerLXC) IdmapSet() (*idmap.IdmapSet, error) {
 
 func (c *containerLXC) InitPID() int {
 	// Load the go-lxc struct
-	err := c.initLXC()
+	err := c.initLXC(false)
 	if err != nil {
 		return -1
 	}
@@ -7244,12 +7239,6 @@ func (c *containerLXC) Profiles() []string {
 }
 
 func (c *containerLXC) State() string {
-	// Load the go-lxc struct
-	err := c.initLXC()
-	if err != nil {
-		return "Broken"
-	}
-
 	state, err := c.getLxcState()
 	if err != nil {
 		return api.Error.String()


More information about the lxc-devel mailing list