[lxc-devel] [distrobuilder/master] upstart tty generator and various fixes

stgraber on Github lxc-bot at linuxcontainers.org
Tue Mar 13 22:25:57 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 309 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180313/83014b4b/attachment.bin>
-------------- next part --------------
From b6cb7c915006a23ecc0b0fae377bffb0f134f279 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 13 Mar 2018 16:55:06 -0400
Subject: [PATCH 1/4] generators: Add upstart-tty generator
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>
---
 generators/generators.go  |   2 +
 generators/upstart_tty.go | 126 ++++++++++++++++++++++++++++++++++++++++++++++
 shared/definition.go      |   3 +-
 3 files changed, 130 insertions(+), 1 deletion(-)
 create mode 100644 generators/upstart_tty.go

diff --git a/generators/generators.go b/generators/generators.go
index 4ffb064..7c8de66 100644
--- a/generators/generators.go
+++ b/generators/generators.go
@@ -30,6 +30,8 @@ func Get(generator string) Generator {
 		return RemoveGenerator{}
 	case "dump":
 		return DumpGenerator{}
+	case "upstart-tty":
+		return UpstartTTYGenerator{}
 	}
 
 	return nil
diff --git a/generators/upstart_tty.go b/generators/upstart_tty.go
new file mode 100644
index 0000000..45e31ba
--- /dev/null
+++ b/generators/upstart_tty.go
@@ -0,0 +1,126 @@
+package generators
+
+import (
+	"fmt"
+	"os"
+	"path/filepath"
+
+	lxd "github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/api"
+
+	"github.com/lxc/distrobuilder/image"
+	"github.com/lxc/distrobuilder/shared"
+)
+
+var upstartTTYJob = `start on starting tty1 or starting tty2 or starting tty3 or starting tty4 or starting tty5 or starting tty6
+instance $JOB
+
+script
+    set -eu
+
+    # Check that we're inside a container
+    [ -e "/run/container_type" ] || exit 0
+    [ "$(cat /run/container_type)" = "lxc" ] || exit 0
+
+    # Load PID1's environment
+    LXC_TTY=$(tr '\0' '\n' < /proc/1/environ | grep ^container_ttys= | cut -d= -f2-)
+
+    # Check if we have any consoles setup
+    if [ -z "${LXC_TTY}" ]; then
+        # No TTYs setup in this container
+        stop "${JOB}" >/dev/null 2>&1
+        exit 0
+    fi
+
+    TTY_COUNT=$(echo ${LXC_TTY} | wc -w)
+    JOB_ID="${JOB#tty}"
+
+    if [ "${JOB_ID}" -gt "${TTY_COUNT}" ]; then
+        # This console isn't available in the container
+        stop "${JOB}" >/dev/null 2>&1
+        exit 0
+    fi
+
+    # Allow the tty to start
+    exit 0
+end script
+`
+
+// UpstartTTYGenerator represents the UpstartTTY generator.
+type UpstartTTYGenerator struct{}
+
+// RunLXC creates a hostname template.
+func (g UpstartTTYGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
+	defFile shared.DefinitionFile) error {
+
+	// Skip if the file exists
+	if lxd.PathExists(filepath.Join(sourceDir, defFile.Path)) {
+		return nil
+	}
+
+	// Store original file
+	err := StoreFile(cacheDir, sourceDir, defFile.Path)
+	if err != nil {
+		return err
+	}
+
+	// Create new hostname file
+	file, err := os.Create(filepath.Join(sourceDir, defFile.Path))
+	if err != nil {
+		return err
+	}
+	defer file.Close()
+
+	// Write LXC specific string to the hostname file
+	_, err = file.WriteString(upstartTTYJob)
+	if err != nil {
+		return fmt.Errorf("Failed to write to upstart job file: %s", err)
+	}
+
+	// Add hostname path to LXC's templates file
+	return img.AddTemplate(defFile.Path)
+}
+
+// RunLXD creates a hostname template.
+func (g UpstartTTYGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
+	defFile shared.DefinitionFile) error {
+
+	// Skip if the file exists
+	if lxd.PathExists(filepath.Join(sourceDir, defFile.Path)) {
+		return nil
+	}
+
+	templateDir := filepath.Join(cacheDir, "templates")
+
+	err := os.MkdirAll(templateDir, 0755)
+	if err != nil {
+		return err
+	}
+
+	file, err := os.Create(filepath.Join(templateDir, "upstart-tty.tpl"))
+	if err != nil {
+		return err
+	}
+	defer file.Close()
+
+	_, err = file.WriteString(upstartTTYJob)
+	if err != nil {
+		return fmt.Errorf("Failed to write to upstart job file: %s", err)
+	}
+
+	// Add to LXD templates
+	img.Metadata.Templates[defFile.Path] = &api.ImageMetadataTemplate{
+		Template: "upstart-tty.tpl",
+		When: []string{
+			"create",
+		},
+	}
+
+	return err
+}
+
+// Run does nothing.
+func (g UpstartTTYGenerator) Run(cacheDir, sourceDir string,
+	defFile shared.DefinitionFile) error {
+	return nil
+}
diff --git a/shared/definition.go b/shared/definition.go
index 1cecf79..2256495 100644
--- a/shared/definition.go
+++ b/shared/definition.go
@@ -159,10 +159,11 @@ func ValidateDefinition(def Definition) error {
 	}
 
 	validGenerators := []string{
+		"dump",
 		"hostname",
 		"hosts",
 		"remove",
-		"dump",
+		"upstart-tty",
 	}
 
 	for _, file := range def.Files {

From cd307cdec48bad2aab992957e922ad15726d7fa4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 13 Mar 2018 16:55:27 -0400
Subject: [PATCH 2/4] chroot: Setup policy.rc.d to prevent daemon startup
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>
---
 distrobuilder/chroot.go | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/distrobuilder/chroot.go b/distrobuilder/chroot.go
index 5ca4f22..8fae569 100644
--- a/distrobuilder/chroot.go
+++ b/distrobuilder/chroot.go
@@ -2,12 +2,15 @@ package main
 
 import (
 	"fmt"
+	"io/ioutil"
 	"os"
 	"path/filepath"
 	"regexp"
 	"strconv"
 	"syscall"
 
+	lxd "github.com/lxc/lxd/shared"
+
 	"github.com/lxc/distrobuilder/managers"
 	"github.com/lxc/distrobuilder/shared"
 )
@@ -223,9 +226,30 @@ func setupChroot(rootfs string) (func() error, error) {
 			},
 		})
 
+	// Setup policy-rc.d override
+	policyCleanup := false
+	if lxd.PathExists("/usr/sbin/") && !lxd.PathExists("/usr/sbin/policy-rc.d") {
+		err = ioutil.WriteFile("/usr/sbin/policy-rc.d", []byte(`#!/bin/sh
+exit 101
+`), 0755)
+		if err != nil {
+			return nil, err
+		}
+
+		policyCleanup = true
+	}
+
 	return func() error {
 		defer root.Close()
 
+		// Cleanup policy-rc.d
+		if policyCleanup {
+			err = os.Remove("/usr/sbin/policy-rc.d")
+			if err != nil {
+				return err
+			}
+		}
+
 		// Reset old environment variables
 		shared.SetEnvVariables(oldEnvVariables)
 

From d248b6782d9dc3e3fc913df81a875c7ad99c03ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 13 Mar 2018 17:59:15 -0400
Subject: [PATCH 3/4] generators: Make more robust
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>
---
 distrobuilder/main_lxc.go |  2 +-
 generators/generators.go  | 36 +++++++++++++++++++++++++++---------
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/distrobuilder/main_lxc.go b/distrobuilder/main_lxc.go
index ba3a201..b3d57a9 100644
--- a/distrobuilder/main_lxc.go
+++ b/distrobuilder/main_lxc.go
@@ -63,7 +63,7 @@ func (c *cmdLXC) run(cmd *cobra.Command, args []string) error {
 		err := generator.RunLXC(c.global.flagCacheDir, c.global.sourceDir, img,
 			file)
 		if err != nil {
-			continue
+			return err
 		}
 	}
 
diff --git a/generators/generators.go b/generators/generators.go
index 7c8de66..60f2995 100644
--- a/generators/generators.go
+++ b/generators/generators.go
@@ -4,7 +4,6 @@ import (
 	"os"
 	p "path"
 	"path/filepath"
-	"strings"
 
 	lxd "github.com/lxc/lxd/shared"
 
@@ -37,30 +36,49 @@ func Get(generator string) Generator {
 	return nil
 }
 
+var storedFiles = map[string]string{}
+
 // StoreFile caches a file which can be restored with the RestoreFiles function.
 func StoreFile(cacheDir, sourceDir, path string) error {
+	// Record newly created files
+	if !lxd.PathExists(filepath.Join(sourceDir, path)) {
+		storedFiles[filepath.Join(sourceDir, path)] = ""
+		return nil
+	}
+
 	// create temporary directory containing old files
 	err := os.MkdirAll(filepath.Join(cacheDir, "tmp", p.Dir(path)), 0755)
 	if err != nil {
 		return err
 	}
 
+	storedFiles[filepath.Join(sourceDir, path)] = filepath.Join(cacheDir, "tmp", path)
+
 	return lxd.FileCopy(filepath.Join(sourceDir, path),
 		filepath.Join(cacheDir, "tmp", path))
 }
 
 // RestoreFiles restores original files which were cached by StoreFile.
 func RestoreFiles(cacheDir, sourceDir string) error {
-	f := func(path string, info os.FileInfo, err error) error {
-		if info.IsDir() {
-			// We don't care about directories. They should be present so there's
-			// no need to create them.
-			return nil
+	for origPath, tmpPath := range storedFiles {
+		// Deal with newly created files
+		if tmpPath == "" {
+			err := os.Remove(origPath)
+			if err != nil {
+				return err
+			}
+
+			continue
 		}
 
-		return lxd.FileCopy(path, filepath.Join(sourceDir,
-			strings.TrimPrefix(path, filepath.Join(cacheDir, "tmp"))))
+		err := lxd.FileCopy(tmpPath, origPath)
+		if err != nil {
+			return err
+		}
 	}
 
-	return filepath.Walk(filepath.Join(cacheDir, "tmp"), f)
+	// Reset the list of stored files
+	storedFiles = map[string]string{}
+
+	return nil
 }

From 6ac3777f9681cda46d83aa60dc9180a19122c3d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 13 Mar 2018 18:05:59 -0400
Subject: [PATCH 4/4] image: Avoid trailing whiteline in excludes-user
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>
---
 image/lxc.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/image/lxc.go b/image/lxc.go
index d91fed0..a8e2e3f 100644
--- a/image/lxc.go
+++ b/image/lxc.go
@@ -163,7 +163,7 @@ func (l *LXCImage) createMetadata() error {
 		}
 	}
 
-	err = l.writeMetadata(filepath.Join(metaDir, "excludes-user"), excludesUser,
+	err = l.writeMetadata(filepath.Join(metaDir, "excludes-user"), strings.TrimSuffix(excludesUser, "\n"),
 		false)
 	if err != nil {
 		return fmt.Errorf("Error writing 'excludes-user': %s", err)


More information about the lxc-devel mailing list