Skip to content

Commit c968fca

Browse files
Allow collect to chroot itself (#1658)
* Enable chroot * typo * platform specific chroot functions * Add friendly chroot warning if running without elevated permissions
1 parent 0fb0a07 commit c968fca

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

cmd/collect/cli/chroot_darwin.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cli
2+
3+
import (
4+
"errors"
5+
"syscall"
6+
7+
"github.com/replicatedhq/troubleshoot/internal/util"
8+
)
9+
10+
func checkAndSetChroot(newroot string) error {
11+
if newroot == "" {
12+
return nil
13+
}
14+
if !util.IsRunningAsRoot() {
15+
return errors.New("Can only chroot when run as root")
16+
}
17+
if err := syscall.Chroot(newroot); err != nil {
18+
return err
19+
}
20+
return nil
21+
}

cmd/collect/cli/chroot_linux.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cli
2+
3+
import (
4+
"errors"
5+
"syscall"
6+
7+
"github.com/replicatedhq/troubleshoot/internal/util"
8+
)
9+
10+
func checkAndSetChroot(newroot string) error {
11+
if newroot == "" {
12+
return nil
13+
}
14+
if !util.IsRunningAsRoot() {
15+
return errors.New("Can only chroot when run as root")
16+
}
17+
if err := syscall.Chroot(newroot); err != nil {
18+
return err
19+
}
20+
return nil
21+
}

cmd/collect/cli/chroot_windows.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cli
2+
3+
import (
4+
"errors"
5+
)
6+
7+
func checkAndSetChroot(newroot string) error {
8+
return errors.New("chroot is only implimented in linux/darwin")
9+
}

cmd/collect/cli/root.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func RootCmd() *cobra.Command {
3232
RunE: func(cmd *cobra.Command, args []string) error {
3333
v := viper.GetViper()
3434

35+
if err := checkAndSetChroot(v.GetString("chroot")); err != nil {
36+
return err
37+
}
38+
3539
return runCollect(v, args[0])
3640
},
3741
PostRun: func(cmd *cobra.Command, args []string) {
@@ -53,6 +57,7 @@ func RootCmd() *cobra.Command {
5357
cmd.Flags().String("selector", "", "selector (label query) to filter remote collection nodes on.")
5458
cmd.Flags().Bool("collect-without-permissions", false, "always generate a support bundle, even if it some require additional permissions")
5559
cmd.Flags().Bool("debug", false, "enable debug logging")
60+
cmd.Flags().String("chroot", "", "Chroot to path")
5661

5762
// hidden in favor of the `insecure-skip-tls-verify` flag
5863
cmd.Flags().Bool("allow-insecure-connections", false, "when set, do not verify TLS certs when retrieving spec and reporting results")

0 commit comments

Comments
 (0)