use warewulf.conf from parent on child
The directories for the overlays needed for the bind mount are now created under conf.Paths.WWChrootdir/$CONTAINERNAME with the pattern $CONTAINERNAME-run-xxxxxx. This pattern can be used as lock, so that there can't be congruent shell/exec calls to the same container. Signed-off-by: Christian Goll <cgoll@suse.com>
This commit is contained in:
committed by
Jonathon Anderson
parent
dc6db3d565
commit
0eba837950
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user