Support binds in container exec command

This addresses issue #88.
This commit is contained in:
Gregory Kurtzer
2021-08-20 14:16:16 -07:00
parent 82696410ed
commit 7826f69656
4 changed files with 37 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
//go:build linux
// +build linux
package child
@@ -6,6 +7,7 @@ import (
"fmt"
"os"
"path"
"strings"
"syscall"
"github.com/hpcng/warewulf/internal/pkg/container"
@@ -37,6 +39,27 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
syscall.Mount("/etc/resolv.conf", path.Join(containerPath, "/etc/resolv.conf"), "", syscall.MS_BIND, "")
}
fmt.Printf("Evaluating bind points\n")
for _, b := range binds {
var source string
var dest string
bind := strings.Split(b, ":")
source = bind[0]
if len(bind) == 1 {
dest = source
} else {
dest = bind[1]
}
err := syscall.Mount(source, path.Join(containerPath, dest), "", syscall.MS_BIND, "")
if err != nil {
fmt.Printf("BIND ERROR: %s\n", err)
os.Exit(1)
}
}
syscall.Chroot(containerPath)
os.Chdir("/")

View File

@@ -10,10 +10,11 @@ var (
Args: cobra.MinimumNArgs(1),
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
}
binds []string
)
func init() {
baseCmd.PersistentFlags().StringArrayVarP(&binds, "bind", "b", []string{}, "bind points")
}
// GetRootCommand returns the root cobra.Command for the application.

View File

@@ -1,3 +1,4 @@
//go:build linux
// +build linux
package exec
@@ -16,13 +17,21 @@ import (
func CobraRunE(cmd *cobra.Command, args []string) error {
containerName := args[0]
var allargs []string
if container.ValidSource(containerName) == false {
wwlog.Printf(wwlog.ERROR, "Unknown Warewulf container: %s\n", containerName)
os.Exit(1)
}
c := exec.Command("/proc/self/exe", append([]string{"container", "exec", "__child"}, args...)...)
fmt.Println(args)
for _, b := range binds {
allargs = append(allargs, "--bind", b)
}
allargs = append(allargs, args...)
c := exec.Command("/proc/self/exe", append([]string{"container", "exec", "__child"}, allargs...)...)
//c := exec.Command("/bin/sh")
c.SysProcAttr = &syscall.SysProcAttr{

View File

@@ -16,10 +16,12 @@ var (
Args: cobra.MinimumNArgs(1),
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
}
binds []string
)
func init() {
baseCmd.AddCommand(child.GetCommand())
baseCmd.PersistentFlags().StringArrayVarP(&binds, "bind", "b", []string{}, "Bind a local path into the container (must exist)")
}
// GetRootCommand returns the root cobra.Command for the application.