diff --git a/pkg/elemental/elemental.go b/pkg/elemental/elemental.go index cef5628fe78..7618a9dfe80 100644 --- a/pkg/elemental/elemental.go +++ b/pkg/elemental/elemental.go @@ -355,6 +355,7 @@ func CreateImageFromTree(c types.Config, img *types.Image, rootDir string, prelo } }() + excludes := cnst.GetDefaultSystemExcludes(rootDir) if img.FS == cnst.SquashFs { c.Logger.Infof("Creating squashfs image for file %s", img.File) @@ -369,13 +370,12 @@ func CreateImageFromTree(c types.Config, img *types.Image, rootDir string, prelo c.Logger.Warnf("failed SELinux labelling at %s: %v", rootDir, err) } - err = utils.CreateSquashFS(c.Runner, c.Logger, rootDir, img.File, c.SquashFsCompressionConfig) + err = utils.CreateSquashFS(c.Runner, c.Logger, rootDir, img.File, c.SquashFsCompressionConfig, excludes...) if err != nil { c.Logger.Errorf("failed creating squashfs image for %s: %v", img.File, err) return err } } else { - excludes := cnst.GetDefaultSystemExcludes(rootDir) err = CreateFileSystemImage(c, img, rootDir, preload, excludes...) if err != nil { c.Logger.Errorf("failed creating filesystem image: %v", err) diff --git a/pkg/utils/common.go b/pkg/utils/common.go index b42c58bced9..44433db2745 100644 --- a/pkg/utils/common.go +++ b/pkg/utils/common.go @@ -279,17 +279,26 @@ func CosignVerify(fs types.FS, runner types.Runner, image string, publicKey stri } // CreateSquashFS creates a squash file at destination from a source, with options -// TODO: Check validity of source maybe? -func CreateSquashFS(runner types.Runner, logger types.Logger, source string, destination string, options []string) error { +func CreateSquashFS(runner types.Runner, logger types.Logger, source string, destination string, options []string, excludes ...string) error { // create args args := []string{source, destination} // append options passed to args in order to have the correct order // protect against options passed together in the same string , i.e. "-x add" instead of "-x", "add" var optionsExpanded []string for _, op := range options { - optionsExpanded = append(optionsExpanded, strings.Split(op, " ")...) + opExpanded := strings.Split(op, " ") + if opExpanded[0] == "-e" { + logger.Warnf("Ignoring option '%s', exclude directories must be passed as excludes argument", op) + continue + } + optionsExpanded = append(optionsExpanded, opExpanded...) } args = append(args, optionsExpanded...) + if len(excludes) >= 0 { + excludesOpt := []string{"-e"} + excludesOpt = append(excludesOpt, excludes...) + args = append(args, excludesOpt...) + } out, err := runner.Run("mksquashfs", args...) if err != nil { logger.Debugf("Error running squashfs creation, stdout: %s", out) diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index c64f497349a..b34278b2c4e 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -948,6 +948,30 @@ var _ = Describe("Utils", Label("utils"), func() { })).To(BeNil()) Expect(err).ToNot(HaveOccurred()) }) + It("ignores any '-e' option", func() { + args := append(constants.GetDefaultSquashfsCompressionOptions(), "-e /some/path") + err := utils.CreateSquashFS(runner, logger, "source", "dest", args) + cmd := []string{"mksquashfs", "source", "dest"} + cmd = append(cmd, constants.GetDefaultSquashfsCompressionOptions()...) + Expect(runner.IncludesCmds([][]string{ + cmd, + })).To(BeNil()) + Expect(err).ToNot(HaveOccurred()) + }) + It("excludes given paths", func() { + + err := utils.CreateSquashFS( + runner, logger, "source", "dest", constants.GetDefaultSquashfsCompressionOptions(), + "/root/some/path", "/root/another/path", + ) + cmd := []string{"mksquashfs", "source", "dest"} + cmd = append(cmd, constants.GetDefaultSquashfsCompressionOptions()...) + cmd = append(cmd, "-e", "/root/some/path", "/root/another/path") + Expect(runner.IncludesCmds([][]string{ + cmd, + })).To(BeNil()) + Expect(err).ToNot(HaveOccurred()) + }) It("returns an error if it fails", func() { runner.ReturnError = errors.New("error") err := utils.CreateSquashFS(runner, logger, "source", "dest", []string{})