[lxc-devel] [distrobuilder/master] main.go: Check for root user as the first thing in main

marcosps on Github lxc-bot at linuxcontainers.org
Sat Apr 7 16:27:34 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 791 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180407/864b8936/attachment.bin>
-------------- next part --------------
From be0540af614bb4fcc0c45112a346b93418effa7c Mon Sep 17 00:00:00 2001
From: Marcos Paulo de Souza <marcos.souza.org at gmail.com>
Date: Sat, 7 Apr 2018 13:19:22 -0300
Subject: [PATCH] main.go: Check for root user as the first thing in main

Just have a PersistentPreRun hook for cobra isn't enough, because right
before the app.Execute() we call ioutil.TempDir, which tries to create a
new distrory under /var/cache, so it fails as bellow:

distrobuilder help
Failed to create cache directory: mkdir /var/cache/distrobuilder.685166231: permission denied

By moving the check for root user as the first thing distrobuilder does
solves the problem.

Signed-off-by: Marcos Paulo de Souza <marcos.souza.org at gmail.com>
---
 distrobuilder/main.go | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/distrobuilder/main.go b/distrobuilder/main.go
index 2c0512d..05a7243 100644
--- a/distrobuilder/main.go
+++ b/distrobuilder/main.go
@@ -80,19 +80,18 @@ type cmdGlobal struct {
 }
 
 func main() {
+	// Sanity checks
+	if os.Geteuid() != 0 {
+		fmt.Fprintf(os.Stderr, "You must be root to run this tool\n")
+		os.Exit(1)
+	}
+
 	// Global flags
 	globalCmd := cmdGlobal{}
 
 	app := &cobra.Command{
-		Use:   "distrobuilder",
-		Short: "System container image builder for LXC and LXD",
-		PersistentPreRun: func(cmd *cobra.Command, args []string) {
-			// Sanity checks
-			if os.Geteuid() != 0 {
-				fmt.Fprintf(os.Stderr, "You must be root to run this tool\n")
-				os.Exit(1)
-			}
-		},
+		Use:                "distrobuilder",
+		Short:              "System container image builder for LXC and LXD",
 		PersistentPostRunE: globalCmd.postRun,
 	}
 


More information about the lxc-devel mailing list