From e56244d8de1d6e5ae1e4510728cd108823c990b8 Mon Sep 17 00:00:00 2001 From: David Cassany Date: Thu, 29 Feb 2024 15:30:20 +0100 Subject: [PATCH] Keep previous behavior for persistent binded paths Signed-off-by: David Cassany --- pkg/action/mount.go | 17 +++++++---------- pkg/elemental/elemental.go | 4 ++-- pkg/snapshotter/btrfs.go | 2 +- pkg/utils/common.go | 27 +++++++++++++++++++++------ 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/pkg/action/mount.go b/pkg/action/mount.go index 968083a0abe..83c3ec3541d 100644 --- a/pkg/action/mount.go +++ b/pkg/action/mount.go @@ -225,17 +225,14 @@ func MountBindPath(cfg *v1.RunConfig, sysroot, overlayDir, path string) error { pathName := strings.ReplaceAll(trimmed, "/", "-") + ".bind" stateDir := fmt.Sprintf("%s/%s", overlayDir, pathName) - // Only sync data once, otherwise it could modify persistent data from a previous boot - if ok, _ := utils.Exists(cfg.Fs, stateDir); !ok { - if err := utils.MkdirAll(cfg.Fs, stateDir, constants.DirPerm); err != nil { - cfg.Logger.Errorf("Error creating upperdir %s: %s", stateDir, err.Error()) - return err - } + if err := utils.MkdirAll(cfg.Fs, stateDir, constants.DirPerm); err != nil { + cfg.Logger.Errorf("Error creating upperdir %s: %s", stateDir, err.Error()) + return err + } - if err := utils.SyncData(cfg.Logger, cfg.Runner, cfg.Fs, base, stateDir); err != nil { - cfg.Logger.Errorf("Error shuffling data: %s", err.Error()) - return err - } + if err := utils.SyncData(cfg.Logger, cfg.Runner, cfg.Fs, base, stateDir); err != nil { + cfg.Logger.Errorf("Error shuffling data: %s", err.Error()) + return err } if err := cfg.Mounter.Mount(stateDir, base, "none", []string{"defaults", "bind"}); err != nil { diff --git a/pkg/elemental/elemental.go b/pkg/elemental/elemental.go index 2a1fc635ed2..da23cfbc757 100644 --- a/pkg/elemental/elemental.go +++ b/pkg/elemental/elemental.go @@ -498,7 +498,7 @@ func DumpSource(c v1.Config, target string, imgSrc *v1.ImageSource) error { // n imgSrc.SetDigest(digest) } else if imgSrc.IsDir() { excludes := cnst.GetDefaultSystemExcludes() - err = utils.SyncData(c.Logger, c.Runner, c.Fs, imgSrc.Value(), target, excludes...) + err = utils.MirrorData(c.Logger, c.Runner, c.Fs, imgSrc.Value(), target, excludes...) if err != nil { return err } @@ -513,7 +513,7 @@ func DumpSource(c v1.Config, target string, imgSrc *v1.ImageSource) error { // n return err } defer UnmountFileSystemImage(c, img) // nolint:errcheck - err = utils.SyncData(c.Logger, c.Runner, c.Fs, cnst.ImgSrcDir, target) + err = utils.MirrorData(c.Logger, c.Runner, c.Fs, cnst.ImgSrcDir, target) if err != nil { return err } diff --git a/pkg/snapshotter/btrfs.go b/pkg/snapshotter/btrfs.go index 43d37762b16..cfe6a7eff9d 100644 --- a/pkg/snapshotter/btrfs.go +++ b/pkg/snapshotter/btrfs.go @@ -299,7 +299,7 @@ func (b *Btrfs) CloseTransaction(snapshot *v1.Snapshot) (err error) { } if !b.installing { - err = utils.SyncData(b.cfg.Logger, b.cfg.Runner, b.cfg.Fs, snapshot.WorkDir, snapshot.Path) + err = utils.MirrorData(b.cfg.Logger, b.cfg.Runner, b.cfg.Fs, snapshot.WorkDir, snapshot.Path) if err != nil { b.cfg.Logger.Errorf("failed syncing working directory with snapshot directory") return err diff --git a/pkg/utils/common.go b/pkg/utils/common.go index ead8e661f79..b7c4c43c06c 100644 --- a/pkg/utils/common.go +++ b/pkg/utils/common.go @@ -156,6 +156,26 @@ func CreateDirStructure(fs v1.FS, target string) error { // SyncData rsync's source folder contents to a target folder content, // both are expected to exist before hand. func SyncData(log v1.Logger, runner v1.Runner, fs v1.FS, source string, target string, excludes ...string) error { + flags := []string{"--progress", "--partial", "--human-readable", "--archive", "--xattrs", "--acls"} + for _, e := range excludes { + flags = append(flags, fmt.Sprintf("--exclude=%s", e)) + } + + return rsyncWrapper(log, runner, fs, source, target, flags) +} + +// MirrorData rsync's source folder contents to a target folder content, in contrast, to SyncData this +// method includes the --delete flag which forces the deletion of files in target that are missing in source. +func MirrorData(log v1.Logger, runner v1.Runner, fs v1.FS, source string, target string, excludes ...string) error { + flags := []string{"--progress", "--partial", "--human-readable", "--archive", "--xattrs", "--acls", "--delete"} + for _, e := range excludes { + flags = append(flags, fmt.Sprintf("--exclude=%s", e)) + } + + return rsyncWrapper(log, runner, fs, source, target, flags) +} + +func rsyncWrapper(log v1.Logger, runner v1.Runner, fs v1.FS, source string, target string, flags []string) error { if fs != nil { if s, err := fs.RawPath(source); err == nil { source = s @@ -175,12 +195,7 @@ func SyncData(log v1.Logger, runner v1.Runner, fs v1.FS, source string, target s log.Infof("Starting rsync...") - args := []string{"--progress", "--partial", "--human-readable", "--archive", "--xattrs", "--acls", "--delete"} - for _, e := range excludes { - args = append(args, fmt.Sprintf("--exclude=%s", e)) - } - - args = append(args, source, target) + args := append(flags, source, target) done := displayProgress(log, 5*time.Second, "Syncing data...")