diff --git a/CHANGELOG.md b/CHANGELOG.md index 4edb4d07..ebc2196d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Reduce the number of times syncuser walks the container file system. #1209 - Create ssh key also when calling `wwctl configure --all` #1250 - Remove the temporary overlay dir. #1180 +- Remove the temporary overlayfs dir and create them besides rootfs #1180 ### Security @@ -71,6 +72,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Allow specification of the ssh-keys to be to be created. #1185 +### Changed + +- The command `wwctl container exec` locks now this container during execution. #830 + ### Fixed - Fix nightly release build failure issue. #1195 diff --git a/internal/app/wwctl/container/exec/child/main.go b/internal/app/wwctl/container/exec/child/main.go index bf409508..3052faa2 100644 --- a/internal/app/wwctl/container/exec/child/main.go +++ b/internal/app/wwctl/container/exec/child/main.go @@ -21,7 +21,7 @@ import ( "github.com/warewulf/warewulf/internal/pkg/wwlog" ) -const exitEval = `$(VALU="$?" ; if [ $VALU == 0 ]; then echo write; else echo discard; fi)` +const exitEval = `$(VALU="$?" ; if [ $VALU == 0 ]; then echo create new image; else echo no new image; fi)` func CobraRunE(cmd *cobra.Command, args []string) (err error) { if os.Getpid() != 1 { diff --git a/internal/app/wwctl/container/exec/main.go b/internal/app/wwctl/container/exec/main.go index 80140a67..8f16120e 100644 --- a/internal/app/wwctl/container/exec/main.go +++ b/internal/app/wwctl/container/exec/main.go @@ -4,15 +4,18 @@ package exec import ( + "errors" "fmt" "os" "os/exec" "path" + "path/filepath" "syscall" "time" - "github.com/spf13/cobra" warewulfconf "github.com/warewulf/warewulf/internal/pkg/config" + + "github.com/spf13/cobra" "github.com/warewulf/warewulf/internal/pkg/container" "github.com/warewulf/warewulf/internal/pkg/util" "github.com/warewulf/warewulf/internal/pkg/wwlog" @@ -22,22 +25,21 @@ import ( fork off a process with a new PID space */ func runContainedCmd(args []string) (err error) { - if overlayDir == "" { - conf := warewulfconf.Get() - overlayDir, err = os.MkdirTemp(conf.Paths.WWChrootdir, "overlays-") - if err != nil { - wwlog.Warn("couldn't create temp dir for overlay", err) - } - defer func() { - err = os.RemoveAll(overlayDir) - if err != nil { - wwlog.Warn("Couldn't remove temp dir for ephermal mounts:", err) - } - }() + conf := warewulfconf.Get() + if matches, _ := filepath.Glob(path.Join(conf.Paths.WWChrootdir, args[0], args[0]) + "-run-*"); len(matches) > 0 { + return fmt.Errorf("found lock directories for container: %v", matches) } + overlayDir, err = os.MkdirTemp(path.Join(conf.Paths.WWChrootdir, args[0]), args[0]+"-run-") + if err != nil { + wwlog.Warn("couldn't create temp dir for overlay", err) + } + defer func() { + err = errors.Join(os.RemoveAll(overlayDir), err) + }() + logStr := fmt.Sprint(wwlog.GetLogLevel()) wwlog.Verbose("Running contained command: %s", args[1:]) - c := exec.Command("/proc/self/exe", append([]string{"--loglevel", logStr, "--overlaydir", overlayDir, "container", "exec", "__child"}, args...)...) + c := exec.Command("/proc/self/exe", append([]string{"--warewulfconf", conf.GetWarewulfConf(), "--loglevel", logStr, "--overlaydir", overlayDir, "container", "exec", "__child"}, args...)...) c.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWNS, diff --git a/internal/pkg/config/root.go b/internal/pkg/config/root.go index c24e84e7..89c6b944 100644 --- a/internal/pkg/config/root.go +++ b/internal/pkg/config/root.go @@ -42,14 +42,14 @@ type RootConf struct { MountsContainer []*MountEntry `yaml:"container mounts" default:"[{\"source\": \"/etc/resolv.conf\", \"dest\": \"/etc/resolv.conf\"}]"` Paths *BuildConfig `yaml:"paths"` - fromFile bool + warewulfconf string } // New caches and returns a new [RootConf] initialized with empty // values, clearing replacing any previously cached value. func New() *RootConf { cachedConf = RootConf{} - cachedConf.fromFile = false + cachedConf.warewulfconf = "" cachedConf.Warewulf = new(WarewulfConf) cachedConf.DHCP = new(DHCPConf) cachedConf.TFTP = new(TFTPConf) @@ -77,12 +77,12 @@ func Get() *RootConf { // file. func (conf *RootConf) Read(confFileName string) error { wwlog.Debug("Reading warewulf.conf from: %s", confFileName) + conf.warewulfconf = confFileName if data, err := os.ReadFile(confFileName); err != nil { return err } else if err := conf.Parse(data); err != nil { return err } else { - conf.fromFile = true return nil } } @@ -200,5 +200,9 @@ func (conf *RootConf) SetDynamicDefaults() (err error) { // InitializedFromFile returns true if [RootConf] memory was read from // a file, or false otherwise. func (conf *RootConf) InitializedFromFile() bool { - return conf.fromFile + return conf.warewulfconf != "" +} + +func (conf *RootConf) GetWarewulfConf() string { + return conf.warewulfconf } diff --git a/internal/pkg/config/root_test.go b/internal/pkg/config/root_test.go index 7cf0812c..4511e47d 100644 --- a/internal/pkg/config/root_test.go +++ b/internal/pkg/config/root_test.go @@ -67,6 +67,7 @@ func TestInitializedFromFile(t *testing.T) { assert.False(t, conf.InitializedFromFile()) assert.NoError(t, conf.Read(tempWarewulfConf.Name())) assert.True(t, conf.InitializedFromFile()) + assert.Equal(t, conf.GetWarewulfConf(), tempWarewulfConf.Name()) } func TestExampleRootConf(t *testing.T) {