Skip to content

Commit

Permalink
Fixes squashfs images creation (bsc#1233289)
Browse files Browse the repository at this point in the history
If upgrading from a container including the root of the host
mounted in /host the upgrade process does not exclude the /host
path and other stateful paths such as /run.

This commit sets the default excludes used in rsync calls
to also apply for mksquashfs.

Signed-off-by: David Cassany <[email protected]>
  • Loading branch information
davidcassany committed Nov 12, 2024
1 parent 5f996b5 commit a56f1d2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/elemental/elemental.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,25 @@ 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 := append([]string{"-e"}, excludes...)
args = append(args, excludesOpt...)
}
out, err := runner.Run("mksquashfs", args...)
if err != nil {
logger.Debugf("Error running squashfs creation, stdout: %s", out)
Expand Down
24 changes: 24 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down

0 comments on commit a56f1d2

Please sign in to comment.