diff --git a/cmd/collect/cli/chroot_darwin.go b/cmd/collect/cli/chroot_darwin.go new file mode 100644 index 000000000..c24bf1120 --- /dev/null +++ b/cmd/collect/cli/chroot_darwin.go @@ -0,0 +1,21 @@ +package cli + +import ( + "errors" + "syscall" + + "github.com/replicatedhq/troubleshoot/internal/util" +) + +func checkAndSetChroot(newroot string) error { + if newroot == "" { + return nil + } + if !util.IsRunningAsRoot() { + return errors.New("Can only chroot when run as root") + } + if err := syscall.Chroot(newroot); err != nil { + return err + } + return nil +} diff --git a/cmd/collect/cli/chroot_linux.go b/cmd/collect/cli/chroot_linux.go new file mode 100644 index 000000000..c24bf1120 --- /dev/null +++ b/cmd/collect/cli/chroot_linux.go @@ -0,0 +1,21 @@ +package cli + +import ( + "errors" + "syscall" + + "github.com/replicatedhq/troubleshoot/internal/util" +) + +func checkAndSetChroot(newroot string) error { + if newroot == "" { + return nil + } + if !util.IsRunningAsRoot() { + return errors.New("Can only chroot when run as root") + } + if err := syscall.Chroot(newroot); err != nil { + return err + } + return nil +} diff --git a/cmd/collect/cli/chroot_windows.go b/cmd/collect/cli/chroot_windows.go new file mode 100644 index 000000000..84b349a20 --- /dev/null +++ b/cmd/collect/cli/chroot_windows.go @@ -0,0 +1,9 @@ +package cli + +import ( + "errors" +) + +func checkAndSetChroot(newroot string) error { + return errors.New("chroot is only implimented in linux/darwin") +} diff --git a/cmd/collect/cli/root.go b/cmd/collect/cli/root.go index dd3b27627..86fdc5c63 100644 --- a/cmd/collect/cli/root.go +++ b/cmd/collect/cli/root.go @@ -32,6 +32,10 @@ func RootCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { v := viper.GetViper() + if err := checkAndSetChroot(v.GetString("chroot")); err != nil { + return err + } + return runCollect(v, args[0]) }, PostRun: func(cmd *cobra.Command, args []string) { @@ -53,6 +57,7 @@ func RootCmd() *cobra.Command { cmd.Flags().String("selector", "", "selector (label query) to filter remote collection nodes on.") cmd.Flags().Bool("collect-without-permissions", false, "always generate a support bundle, even if it some require additional permissions") cmd.Flags().Bool("debug", false, "enable debug logging") + cmd.Flags().String("chroot", "", "Chroot to path") // hidden in favor of the `insecure-skip-tls-verify` flag cmd.Flags().Bool("allow-insecure-connections", false, "when set, do not verify TLS certs when retrieving spec and reporting results")