diff --git a/cmd/box/exec.go b/cmd/box/exec.go index 7f71e0b8..83a1fac4 100644 --- a/cmd/box/exec.go +++ b/cmd/box/exec.go @@ -25,7 +25,6 @@ import ( . "github.com/mudler/luet/pkg/logger" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func NewBoxExecCommand() *cobra.Command { @@ -34,21 +33,17 @@ func NewBoxExecCommand() *cobra.Command { Short: "Execute a binary in a box", Args: cobra.OnlyValidArgs, PreRun: func(cmd *cobra.Command, args []string) { - viper.BindPFlag("stdin", cmd.Flags().Lookup("stdin")) - viper.BindPFlag("stdout", cmd.Flags().Lookup("stdout")) - viper.BindPFlag("stderr", cmd.Flags().Lookup("stderr")) - viper.BindPFlag("rootfs", cmd.Flags().Lookup("rootfs")) - viper.BindPFlag("decode", cmd.Flags().Lookup("decode")) - viper.BindPFlag("entrypoint", cmd.Flags().Lookup("entrypoint")) }, Run: func(cmd *cobra.Command, args []string) { - stdin := viper.GetBool("stdin") - stdout := viper.GetBool("stdout") - stderr := viper.GetBool("stderr") - rootfs := viper.GetString("rootfs") - base := viper.GetBool("decode") + stdin, _ := cmd.Flags().GetBool("stdin") + stdout, _ := cmd.Flags().GetBool("stdout") + stderr, _ := cmd.Flags().GetBool("stderr") + rootfs, _ := cmd.Flags().GetString("rootfs") + base, _ := cmd.Flags().GetBool("decode") + entrypoint, _ := cmd.Flags().GetString("entrypoint") + envs, _ := cmd.Flags().GetStringArray("env") + mounts, _ := cmd.Flags().GetStringArray("mount") - entrypoint := viper.GetString("entrypoint") if base { var ss []string for _, a := range args { diff --git a/pkg/box/exec.go b/pkg/box/exec.go index 78a5f1d9..e610b0fc 100644 --- a/pkg/box/exec.go +++ b/pkg/box/exec.go @@ -33,23 +33,25 @@ type Box interface { } type DefaultBox struct { - Name string - Root string - Env []string - Cmd string - Args []string - + Name string + Root string + Env []string + Cmd string + Args []string + HostMounts []string Stdin, Stdout, Stderr bool } -func NewBox(cmd string, args []string, rootfs string, stdin, stdout, stderr bool) Box { +func NewBox(cmd string, args, hostmounts, env []string, rootfs string, stdin, stdout, stderr bool) Box { return &DefaultBox{ - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, - Cmd: cmd, - Args: args, - Root: rootfs, + Stdin: stdin, + Stdout: stdout, + Stderr: stderr, + Cmd: cmd, + Args: args, + Root: rootfs, + HostMounts: hostmounts, + Env: env, } } @@ -64,6 +66,11 @@ func (b *DefaultBox) Exec() error { if err := PivotRoot(b.Root); err != nil { return errors.Wrap(err, "Failed switching pivot on rootfs") } + for _, hostMount := range b.HostMounts { + if err := mountBind(hostMount, b.Root); err != nil { + return errors.Wrap(err, fmt.Sprintf("Failed mounting %s on rootfs", hostMount)) + } + } cmd := exec.Command(b.Cmd, b.Args...) @@ -82,7 +89,7 @@ func (b *DefaultBox) Exec() error { cmd.Env = b.Env if err := cmd.Run(); err != nil { - return errors.Wrap(err, fmt.Sprintf("Error running the %s command", b.Cmd)) + return errors.Wrap(err, fmt.Sprintf("Error running the %s command in box.Exec", b.Cmd)) } return nil } @@ -111,6 +118,16 @@ func (b *DefaultBox) Run() error { // Encode the command in base64 to avoid bad input from the args given execCmd = append(execCmd, "--decode") + for _, m := range b.HostMounts { + execCmd = append(execCmd, "--mount") + execCmd = append(execCmd, m) + } + + for _, e := range b.Env { + execCmd = append(execCmd, "--env") + execCmd = append(execCmd, e) + } + for _, a := range b.Args { execCmd = append(execCmd, b64.StdEncoding.EncodeToString([]byte(a))) } @@ -151,7 +168,7 @@ func (b *DefaultBox) Run() error { } if err := cmd.Run(); err != nil { - return errors.Wrap(err, "Failed running Box command") + return errors.Wrap(err, "Failed running Box command in box.Run") } return nil } diff --git a/pkg/box/rootfs.go b/pkg/box/rootfs.go index 415c225d..49bc41f7 100644 --- a/pkg/box/rootfs.go +++ b/pkg/box/rootfs.go @@ -75,10 +75,9 @@ func mountProc(newroot string) error { return nil } -func mountDev(newroot string) error { - - source := "/dev" - target := filepath.Join(newroot, "/dev") +func mountBind(hostfolder, newroot string) error { + source := hostfolder + target := filepath.Join(newroot, hostfolder) fstype := "bind" data := "" @@ -89,3 +88,7 @@ func mountDev(newroot string) error { return nil } + +func mountDev(newroot string) error { + return mountBind("/dev", newroot) +}