[lxc-devel] [lxd/master] Port fuidshift to cobra

stgraber on Github lxc-bot at linuxcontainers.org
Thu Feb 22 23:12:55 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180222/7b72c986/attachment.bin>
-------------- next part --------------
From dac9f6f11e1a72d4e5d44655b73579bbb36b6c24 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 22 Feb 2018 17:32:54 -0500
Subject: [PATCH 1/2] fuidshift: Drop specific Makefile
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>
---
 fuidshift/Makefile | 7 -------
 1 file changed, 7 deletions(-)
 delete mode 100644 fuidshift/Makefile

diff --git a/fuidshift/Makefile b/fuidshift/Makefile
deleted file mode 100644
index 8799b5810..000000000
--- a/fuidshift/Makefile
+++ /dev/null
@@ -1,7 +0,0 @@
-# we let go build figure out dependency changes
-.PHONY: fuidmap
-lxc:
-	go build
-
-clean:
-	-rm -f fuidshift

From e1dd2e56fa3d9d046a958018378f4220de9421d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 22 Feb 2018 17:59:51 -0500
Subject: [PATCH 2/2] fuidshift: Port to cobra
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>
---
 fuidshift/main.go        | 106 +++++++++++++++++++++--------------------------
 fuidshift/main_shift.go  |  64 ++++++++++++++++++++++++++++
 test/suites/fuidshift.sh |   2 -
 3 files changed, 111 insertions(+), 61 deletions(-)
 create mode 100644 fuidshift/main_shift.go

diff --git a/fuidshift/main.go b/fuidshift/main.go
index a9fa1382a..3bf1f18b4 100644
--- a/fuidshift/main.go
+++ b/fuidshift/main.go
@@ -1,72 +1,60 @@
 package main
 
 import (
-	"fmt"
 	"os"
 
-	"github.com/lxc/lxd/shared/idmap"
+	"github.com/spf13/cobra"
+
+	"github.com/lxc/lxd/shared/version"
 )
 
-func help(me string, status int) {
-	fmt.Printf("Usage: %s directory [-t] [-r] <range1> [<range2> ...]\n", me)
-	fmt.Printf("  -t implies test mode.  No file ownerships will be changed.\n")
-	fmt.Printf("  -r means reverse, that is shift the uids out of the container.\n")
-	fmt.Printf("\n")
-	fmt.Printf("  A range is [u|b|g]:<first_container_id:first_host_id:range>.\n")
-	fmt.Printf("  where u means shift uids, g means shift gids, b means shift both.\n")
-	fmt.Printf("  For example: %s directory b:0:100000:65536 u:10000:1000:1\n", me)
-	os.Exit(status)
+type cmdGlobal struct {
+	flagVersion bool
+	flagHelp    bool
 }
 
 func main() {
-	if err := run(); err != nil {
-		fmt.Printf("Error: %q\n", err)
-		help(os.Args[0], 1)
-	}
-}
-
-func run() error {
-	if len(os.Args) < 3 {
-		if len(os.Args) > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help" || os.Args[1] == "help") {
-			help(os.Args[0], 0)
-		} else {
-			help(os.Args[0], 1)
-		}
-	}
-
-	directory := os.Args[1]
-	idmapSet := idmap.IdmapSet{}
-	testmode := false
-	reverse := false
-
-	for pos := 2; pos < len(os.Args); pos++ {
-
-		switch os.Args[pos] {
-		case "-r", "--reverse":
-			reverse = true
-		case "t", "-t", "--test", "test":
-			testmode = true
-		default:
-			var err error
-			idmapSet, err = idmapSet.Append(os.Args[pos])
-			if err != nil {
-				return err
-			}
-		}
-	}
-
-	if idmapSet.Len() == 0 {
-		fmt.Printf("No idmaps given\n")
-		help(os.Args[0], 1)
-	}
-
-	if !testmode && os.Geteuid() != 0 {
-		fmt.Printf("This must be run as root\n")
+	app := &cobra.Command{}
+	app.Use = "fuidshift"
+	app.Short = "UID/GID shifter"
+	app.Long = `Description:
+  UID/GID shifter
+
+  This tool lets you remap a filesystem tree, switching it from one
+  set of UID/GID ranges to another.
+
+  This is mostly useful when retrieving a wrongly shifted filesystem tree
+  from a backup or broken system and having to remap everything either to
+  the host UID/GID range (uid/gid 0 is root) or to an existing container's
+  range.
+
+
+  A range is represented as <u|b|g>:<first_container_id>:<first_host_id>:<size>.
+  Where "u" means shift uid, "g" means shift gid and "b" means shift uid and gid.
+`
+	app.Example = `  fuidshift my-dir/ b:0:100000:65536 u:10000:1000:1`
+	app.SilenceUsage = true
+
+	// Global flags
+	globalCmd := cmdGlobal{}
+	app.PersistentFlags().BoolVar(&globalCmd.flagVersion, "version", false, "Print version number")
+	app.PersistentFlags().BoolVarP(&globalCmd.flagHelp, "help", "h", false, "Print help")
+
+	// Version handling
+	app.SetVersionTemplate("{{.Version}}\n")
+	app.Version = version.Version
+
+	// shift command (main)
+	shiftCmd := cmdShift{global: &globalCmd}
+	app.Flags().BoolVarP(&shiftCmd.flagTestMode, "test", "t", false, "Test mode (no change to files)")
+	app.Flags().BoolVarP(&shiftCmd.flagReverse, "reverse", "r", false, "Perform a reverse mapping")
+	app.Use = "fuidshift <directory> <range> [<range>...]"
+	app.RunE = shiftCmd.Run
+	app.Args = cobra.ArbitraryArgs
+
+	// Run the main command and handle errors
+	err := app.Execute()
+	if err != nil {
 		os.Exit(1)
 	}
-
-	if reverse {
-		return idmapSet.UidshiftFromContainer(directory, testmode)
-	}
-	return idmapSet.UidshiftIntoContainer(directory, testmode)
 }
diff --git a/fuidshift/main_shift.go b/fuidshift/main_shift.go
new file mode 100644
index 000000000..32c939bc0
--- /dev/null
+++ b/fuidshift/main_shift.go
@@ -0,0 +1,64 @@
+package main
+
+import (
+	"fmt"
+	"os"
+
+	"github.com/spf13/cobra"
+
+	"github.com/lxc/lxd/shared/idmap"
+)
+
+type cmdShift struct {
+	global *cmdGlobal
+
+	flagReverse  bool
+	flagTestMode bool
+}
+
+func (c *cmdShift) Run(cmd *cobra.Command, args []string) error {
+	// Help and usage
+	if len(args) == 0 {
+		return cmd.Help()
+	}
+
+	// Sanity checks
+	if !c.flagTestMode && os.Geteuid() != 0 {
+		return fmt.Errorf("This tool must be run as root")
+	}
+
+	// Handle mandatory arguments
+	if len(args) < 2 {
+		cmd.Help()
+		return fmt.Errorf("Missing required arguments")
+	}
+	directory := args[0]
+
+	// Parse the maps
+	idmapSet := idmap.IdmapSet{}
+	for _, arg := range args[1:] {
+		var err error
+		idmapSet, err = idmapSet.Append(arg)
+		if err != nil {
+			return err
+		}
+	}
+
+	// Reverse shifting
+	if c.flagReverse {
+		err := idmapSet.UidshiftFromContainer(directory, c.flagTestMode)
+		if err != nil {
+			return err
+		}
+
+		return nil
+	}
+
+	// Normal shifting
+	err := idmapSet.UidshiftIntoContainer(directory, c.flagTestMode)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
diff --git a/test/suites/fuidshift.sh b/test/suites/fuidshift.sh
index 8b3a1f1a5..5dd929548 100644
--- a/test/suites/fuidshift.sh
+++ b/test/suites/fuidshift.sh
@@ -1,8 +1,6 @@
 test_common_fuidshift() {
   # test some bad arguments
   fail=0
-  fuidshift > /dev/null 2>&1 && fail=1
-  fuidshift -t > /dev/null 2>&1 && fail=1
   fuidshift /tmp -t b:0 > /dev/null 2>&1 && fail=1
   fuidshift /tmp -t x:0:0:0 > /dev/null 2>&1 && fail=1
   [ "${fail}" -ne 1 ]


More information about the lxc-devel mailing list