added wwctl container shell

This commit is contained in:
Christian Goll
2022-02-09 10:45:56 +01:00
parent 100cfe6a13
commit 10fdacee77
5 changed files with 86 additions and 4 deletions

View File

@@ -28,9 +28,6 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
allargs = append(allargs, "--bind", b) allargs = append(allargs, "--bind", b)
} }
allargs = append(allargs, args...) allargs = append(allargs, args...)
if len(args) == 1 {
allargs = append(allargs, "/usr/bin/bash")
}
c := exec.Command("/proc/self/exe", append([]string{"container", "exec", "__child"}, allargs...)...) c := exec.Command("/proc/self/exe", append([]string{"container", "exec", "__child"}, allargs...)...)

View File

@@ -15,7 +15,7 @@ var (
"This is commonly used with an interactive shell such as /bin/bash\n" + "This is commonly used with an interactive shell such as /bin/bash\n" +
"to run a virtual environment within the container.", "to run a virtual environment within the container.",
RunE: CobraRunE, RunE: CobraRunE,
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(2),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 { if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp return nil, cobra.ShellCompDirectiveNoFileComp

View File

@@ -6,6 +6,7 @@ import (
"github.com/hpcng/warewulf/internal/app/wwctl/container/exec" "github.com/hpcng/warewulf/internal/app/wwctl/container/exec"
"github.com/hpcng/warewulf/internal/app/wwctl/container/imprt" "github.com/hpcng/warewulf/internal/app/wwctl/container/imprt"
"github.com/hpcng/warewulf/internal/app/wwctl/container/list" "github.com/hpcng/warewulf/internal/app/wwctl/container/list"
"github.com/hpcng/warewulf/internal/app/wwctl/container/shell"
"github.com/hpcng/warewulf/internal/app/wwctl/container/show" "github.com/hpcng/warewulf/internal/app/wwctl/container/show"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -27,6 +28,7 @@ func init() {
baseCmd.AddCommand(list.GetCommand()) baseCmd.AddCommand(list.GetCommand())
baseCmd.AddCommand(imprt.GetCommand()) baseCmd.AddCommand(imprt.GetCommand())
baseCmd.AddCommand(exec.GetCommand()) baseCmd.AddCommand(exec.GetCommand())
baseCmd.AddCommand(shell.GetCommand())
baseCmd.AddCommand(delete.GetCommand()) baseCmd.AddCommand(delete.GetCommand())
baseCmd.AddCommand(show.GetCommand()) baseCmd.AddCommand(show.GetCommand())

View File

@@ -0,0 +1,48 @@
//go:build linux
// +build linux
package shell
import (
"fmt"
"os"
"os/exec"
"syscall"
"github.com/hpcng/warewulf/internal/pkg/container"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/spf13/cobra"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
containerName := args[0]
var allargs []string
if !container.ValidSource(containerName) {
wwlog.Printf(wwlog.ERROR, "Unknown Warewulf container: %s\n", containerName)
os.Exit(1)
}
for _, b := range binds {
allargs = append(allargs, "--bind", b)
}
allargs = append(allargs, args...)
allargs = append(allargs, "/usr/bin/bash")
c := exec.Command("/proc/self/exe", append([]string{"container", "exec"}, allargs...)...)
//c := exec.Command("/bin/sh")
c.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWNS,
}
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
}

View File

@@ -0,0 +1,35 @@
package shell
import (
"github.com/hpcng/warewulf/internal/pkg/container"
"github.com/spf13/cobra"
)
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "shell [OPTIONS] CONTAINER",
Short: "Run a shell inside of a Warewulf container",
Long: "Run a interactive shell inside of a warewulf CONTAINER.\n",
RunE: CobraRunE,
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
list, _ := container.ListSources()
return list, cobra.ShellCompDirectiveNoFileComp
},
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
}
binds []string
)
func init() {
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.
func GetCommand() *cobra.Command {
return baseCmd
}