create temporary dir for overlayfs not in __child

This commit is contained in:
Christian Goll
2023-01-27 16:19:30 +01:00
parent 6d560ac02a
commit 71a3867bbe
4 changed files with 39 additions and 31 deletions

View File

@@ -32,14 +32,20 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
os.Exit(1)
}
var err error
tempDir := ""
// check for valid mount points
lowerObjects := checkMountPoints(containerName, binds)
if len(lowerObjects) != 0 {
if tempDir == "" {
tempDir, err = os.MkdirTemp(buildconfig.TMPDIR(), "overlay")
if err != nil {
wwlog.Warn("couldn't create temp dir for overlay", err)
lowerObjects = []string{}
tempDir = ""
}
}
// need to create a overlay, where the lower layer contains
// the missing mount points
tempDir, err = os.MkdirTemp(buildconfig.TMPDIR(), "overlay")
if err == nil {
if tempDir != "" {
wwlog.Verbose("for ephermal mount use tempdir %s", tempDir)
// ignore errors as we are doomed if a tmp dir couldn't be written
_ = os.Mkdir(path.Join(tempDir, "work"), os.ModePerm)
@@ -61,19 +67,8 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
}
defer desc.Close()
}
}
} else {
wwlog.Warn("couldn't create temp dir for overlay", err)
lowerObjects = []string{}
}
/*
\TODO check why defer isn't called at exit
defer func() {
fmt.Println("defer called")
_ = os.RemoveAll(tempDir)
}()
*/
}
containerPath := container.RootFsDir(containerName)
err = syscall.Mount("", "/", "", syscall.MS_PRIVATE|syscall.MS_REC, "")
@@ -148,15 +143,10 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
os.Setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin")
os.Setenv("HISTFILE", "/dev/null")
err = syscall.Exec(args[1], args[1:], os.Environ())
fmt.Println("After exec")
_ = os.RemoveAll(tempDir)
if err != nil {
wwlog.Error("%s", err)
os.Exit(1)
}
_ = syscall.Exec(args[1], args[1:], os.Environ())
/*
Exec replaces the actual program, so nothing to do here afterwards
*/
return nil
}

View File

@@ -5,17 +5,19 @@ import "github.com/spf13/cobra"
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "__child",
Hidden: true,
RunE: CobraRunE,
Args: cobra.MinimumNArgs(1),
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
Use: "__child",
Hidden: true,
RunE: CobraRunE,
Args: cobra.MinimumNArgs(1),
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
}
binds []string
binds []string
tempDir string
)
func init() {
baseCmd.PersistentFlags().StringArrayVarP(&binds, "bind", "b", []string{}, "bind points")
baseCmd.Flags().StringArrayVarP(&binds, "bind", "b", []string{}, "bind points")
baseCmd.Flags().StringVar(&tempDir, "tempdir", "", "tempdir")
}
// GetRootCommand returns the root cobra.Command for the application.

View File

@@ -11,6 +11,7 @@ import (
"syscall"
"time"
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
"github.com/hpcng/warewulf/internal/pkg/container"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
@@ -21,9 +22,22 @@ import (
fork off a process with a new PID space
*/
func runContainedCmd(args []string) error {
var err error
if tempDir == "" {
tempDir, err = os.MkdirTemp(buildconfig.TMPDIR(), "overlay")
if err != nil {
wwlog.Warn("couldn't create temp dir for overlay", err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
wwlog.Warn("Couldn't remove temp dir for ephermal mounts:", err)
}
}()
}
logStr := fmt.Sprint(wwlog.GetLogLevel())
wwlog.Verbose("Running contained command: %s", args[1:])
c := exec.Command("/proc/self/exe", append([]string{"--loglevel", logStr, "container", "exec", "__child"}, args...)...)
c := exec.Command("/proc/self/exe", append([]string{"--loglevel", logStr, "--tempdir", tempDir, "container", "exec", "__child"}, args...)...)
c.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWNS,

View File

@@ -27,12 +27,14 @@ var (
}
SyncUser bool
binds []string
tempDir string
)
func init() {
baseCmd.AddCommand(child.GetCommand())
baseCmd.PersistentFlags().StringArrayVarP(&binds, "bind", "b", []string{}, "Bind a local path into the container (must exist)")
baseCmd.PersistentFlags().BoolVar(&SyncUser, "syncuser", false, "Synchronize UIDs/GIDs from host to container")
baseCmd.PersistentFlags().StringVar(&tempDir, "tempdir", "", "Use tempdir for constructing the overlay fs (only used if mount points don't exist in container)")
}
// GetRootCommand returns the root cobra.Command for the application.