[lxc-devel] [distrobuilder/master] generators/dump: Allow changing mode, gid and uid

monstermunchkin on Github lxc-bot at linuxcontainers.org
Mon May 11 15:56:47 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 384 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200511/13722ef9/attachment.bin>
-------------- next part --------------
From 97ac364c4611e7848ec9e7b02fc185dd7786936f Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 11 May 2020 17:19:14 +0200
Subject: [PATCH 1/5] shared/definition: Add Mode, GID and UID to files

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/definition.go | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/shared/definition.go b/shared/definition.go
index 66e9bee..5df49c3 100644
--- a/shared/definition.go
+++ b/shared/definition.go
@@ -180,6 +180,9 @@ type DefinitionFile struct {
 	Name             string                 `yaml:"name,omitempty"`
 	Template         DefinitionFileTemplate `yaml:"template,omitempty"`
 	Templated        bool                   `yaml:"templated,omitempty"`
+	Mode             string                 `yaml:"mode,omitempty"`
+	GID              string                 `yaml:"gid,omitempty"`
+	UID              string                 `yaml:"uid,omitempty"`
 }
 
 // A DefinitionFileTemplate represents the settings used by generators

From a48118828b0dbb26f8fe842003ac2d596c256a0c Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 11 May 2020 17:21:17 +0200
Subject: [PATCH 2/5] generators: Add file access handler

This adds a function which handles both file permission and ownership.

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 generators/utils.go | 53 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 generators/utils.go

diff --git a/generators/utils.go b/generators/utils.go
new file mode 100644
index 0000000..79c3d92
--- /dev/null
+++ b/generators/utils.go
@@ -0,0 +1,53 @@
+package generators
+
+import (
+	"os"
+	"strconv"
+
+	"github.com/pkg/errors"
+
+	"github.com/lxc/distrobuilder/shared"
+)
+
+func updateFileAccess(file *os.File, defFile shared.DefinitionFile) error {
+	// Change file mode if needed
+	if defFile.Mode != "" {
+		mode, err := strconv.ParseUint(defFile.Mode, 8, 64)
+		if err != nil {
+			return errors.Wrap(err, "Failed to parse file mode")
+		}
+
+		err = file.Chmod(os.FileMode(mode))
+		if err != nil {
+			return errors.Wrap(err, "Failed to change file mode")
+		}
+	}
+
+	// Change gid if needed
+	if defFile.GID != "" {
+		gid, err := strconv.Atoi(defFile.GID)
+		if err != nil {
+			return errors.Wrap(err, "Failed to parse GID")
+		}
+
+		err = file.Chown(-1, gid)
+		if err != nil {
+			return errors.Wrap(err, "Failed to change GID")
+		}
+	}
+
+	// Change uid if needed
+	if defFile.Mode != "" {
+		uid, err := strconv.Atoi(defFile.UID)
+		if err != nil {
+			return errors.Wrap(err, "Failed to parse UID")
+		}
+
+		err = file.Chown(uid, -1)
+		if err != nil {
+			return errors.Wrap(err, "Failed to change UID")
+		}
+	}
+
+	return nil
+}

From e4ed18a2c25eb67ff3253ff9dfb0f4ee9b43b076 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 11 May 2020 17:21:54 +0200
Subject: [PATCH 3/5] generators/dump: Allow changing mode, UID and GID

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 generators/dump.go | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/generators/dump.go b/generators/dump.go
index 526b191..d829a55 100644
--- a/generators/dump.go
+++ b/generators/dump.go
@@ -35,10 +35,9 @@ func (g DumpGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
 
 // Run dumps content to a file.
 func (g DumpGenerator) Run(cacheDir, sourceDir string, defFile shared.DefinitionFile) error {
-	return g.dumpFile(filepath.Join(sourceDir, defFile.Path), defFile.Content)
-}
+	path := filepath.Join(sourceDir, defFile.Path)
+	content := defFile.Content
 
-func (g DumpGenerator) dumpFile(path, content string) error {
 	// Create any missing directory
 	err := os.MkdirAll(filepath.Dir(path), 0755)
 	if err != nil {
@@ -63,5 +62,5 @@ func (g DumpGenerator) dumpFile(path, content string) error {
 		return err
 	}
 
-	return nil
+	return updateFileAccess(file, defFile)
 }

From 2c016a74a9c5b8fe8be0a05e098686cd0ee05bee Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 11 May 2020 17:47:46 +0200
Subject: [PATCH 4/5] doc/generators: Update dump generator

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 doc/generators.md | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/doc/generators.md b/doc/generators.md
index ec69212..66d37c5 100644
--- a/doc/generators.md
+++ b/doc/generators.md
@@ -24,6 +24,9 @@ files:
           properties: <map>
           when: <array>
       templated: <boolean>
+      mode: <string>
+      gid: <string>
+      uid: <string>
       architectures: <array> # filter
       releases: <array> # filter
       variants: <array> # filter
@@ -45,6 +48,7 @@ Setting `path`, `content` or `template.properties` will override the default val
 ## dump
 
 The `dump` generator writes the provided `content` to a file set in `path`.
+If provided, it will set the `mode` (octal format), `gid` (integer) and/or `uid` (integer).
 
 ## hostname
 

From 3aa989ddbd8b31021018c87b14296109f0508075 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 11 May 2020 17:48:22 +0200
Subject: [PATCH 5/5] doc/examples/schema: Add mode, gid and uid

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 doc/examples/scheme.yaml | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/doc/examples/scheme.yaml b/doc/examples/scheme.yaml
index ea8dae5..5a08afe 100644
--- a/doc/examples/scheme.yaml
+++ b/doc/examples/scheme.yaml
@@ -75,6 +75,9 @@ files:
     content: |-
       here goes the content
     name: name
+    mode: 0644
+    gid: 1000
+    uid: 1000
     template:
       properties:
         key: value


More information about the lxc-devel mailing list