From d3650244f9660454558a6ea02ad4fd952ca45f1a Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Fri, 28 Apr 2023 12:11:36 +0200 Subject: [PATCH] Epehermal container mounts on same fs as chroot Previously ephemeral container mounts were performed in /tmp. Now these mounts are overlayed on the same fs as is used for chroots. Signed-off-by: Christian Goll --- CHANGELOG.md | 1 + .../app/wwctl/container/exec/child/main.go | 61 ++++++++----------- .../app/wwctl/container/exec/child/root.go | 6 +- 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efb38a74..7a47d5d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add EL9 Quickstart guide to index.rst - Container file gids are now updated properly during syncuser. #840 - Fix build for API. +- Bindd mounts for `wwctl exec` are now on the same fs ### Changed diff --git a/internal/app/wwctl/container/exec/child/main.go b/internal/app/wwctl/container/exec/child/main.go index ee2b97b9..ddb7fa18 100644 --- a/internal/app/wwctl/container/exec/child/main.go +++ b/internal/app/wwctl/container/exec/child/main.go @@ -38,41 +38,30 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) { mountPts = append(container.InitMountPnts(binds), mountPts...) // check for valid mount points lowerObjects := checkMountPoints(containerName, mountPts) - if len(lowerObjects) != 0 { - if tempDir == "" { - tempDir, err = os.MkdirTemp(os.TempDir(), "overlay") - if err != nil { - wwlog.Warn("couldn't create temp dir for overlay", err) - lowerObjects = []string{} - tempDir = "" - } + overlayDir := conf.Paths.WWChrootdir + "/overlays" + // need to create a overlay, where the lower layer contains + // the missing mount points + wwlog.Verbose("for ephermal mount use tempdir %s", overlayDir) + // ignore errors as we are doomed if a tmp dir couldn't be written + _ = os.MkdirAll(path.Join(overlayDir, "work"), os.ModePerm) + _ = os.MkdirAll(path.Join(overlayDir, "lower"), os.ModePerm) + _ = os.MkdirAll(path.Join(overlayDir, "nodeoverlay"), os.ModePerm) + for _, obj := range lowerObjects { + newFile := "" + if !strings.HasSuffix(obj, "/") { + newFile = filepath.Base(obj) + obj = filepath.Dir(obj) } - // need to create a overlay, where the lower layer contains - // the missing mount points - 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) - _ = os.Mkdir(path.Join(tempDir, "lower"), os.ModePerm) - _ = os.Mkdir(path.Join(tempDir, "nodeoverlay"), os.ModePerm) - for _, obj := range lowerObjects { - newFile := "" - if !strings.HasSuffix(obj, "/") { - newFile = filepath.Base(obj) - obj = filepath.Dir(obj) - } - err = os.MkdirAll(filepath.Join(tempDir, "lower", obj), os.ModePerm) - if err != nil { - wwlog.Warn("couldn't create directory for mounts: %s", err) - } - if newFile != "" { - desc, err := os.Create(filepath.Join(tempDir, "lower", obj, newFile)) - if err != nil { - wwlog.Warn("couldn't create directory for mounts: %s", err) - } - defer desc.Close() - } + err = os.MkdirAll(filepath.Join(overlayDir, "lower", obj), os.ModePerm) + if err != nil { + wwlog.Warn("couldn't create directory for mounts: %s", err) + } + if newFile != "" { + desc, err := os.Create(filepath.Join(overlayDir, "lower", obj, newFile)) + if err != nil { + wwlog.Warn("couldn't create directory for mounts: %s", err) } + defer desc.Close() } } containerPath := container.RootFsDir(containerName) @@ -83,7 +72,7 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) { ps1Str := fmt.Sprintf("[%s] Warewulf> ", containerName) if len(lowerObjects) != 0 && nodename == "" { options := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", - path.Join(tempDir, "lower"), containerPath, path.Join(tempDir, "work")) + path.Join(overlayDir, "lower"), containerPath, path.Join(overlayDir, "work")) wwlog.Debug("overlay options: %s", options) err = syscall.Mount("overlay", containerPath, "overlay", 0, options) if err != nil { @@ -108,13 +97,13 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) { } overlays := nodes[0].SystemOverlay.GetSlice() overlays = append(overlays, nodes[0].RuntimeOverlay.GetSlice()...) - err = overlay.BuildOverlayIndir(nodes[0], overlays, path.Join(tempDir, "nodeoverlay")) + err = overlay.BuildOverlayIndir(nodes[0], overlays, path.Join(overlayDir, "nodeoverlay")) if err != nil { wwlog.Error("Could not build overlay: %s", err) os.Exit(1) } options := fmt.Sprintf("lowerdir=%s:%s:%s", - path.Join(tempDir, "lower"), containerPath, path.Join(tempDir, "nodeoverlay")) + path.Join(overlayDir, "lower"), containerPath, path.Join(overlayDir, "nodeoverlay")) wwlog.Debug("overlay options: %s", options) err = syscall.Mount("overlay", containerPath, "overlay", 0, options) if err != nil { diff --git a/internal/app/wwctl/container/exec/child/root.go b/internal/app/wwctl/container/exec/child/root.go index d7060995..ad807a6f 100644 --- a/internal/app/wwctl/container/exec/child/root.go +++ b/internal/app/wwctl/container/exec/child/root.go @@ -1,6 +1,8 @@ package child -import "github.com/spf13/cobra" +import ( + "github.com/spf13/cobra" +) var ( baseCmd = &cobra.Command{ @@ -12,14 +14,12 @@ var ( FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true}, } binds []string - tempDir string nodename string ) func init() { baseCmd.Flags().StringVarP(&nodename, "node", "n", "", "create ro overlay for given node") baseCmd.Flags().StringArrayVarP(&binds, "bind", "b", []string{}, "bind points") - baseCmd.Flags().StringVar(&tempDir, "tempdir", "", "tempdir") } // GetRootCommand returns the root cobra.Command for the application.