From 28894b5c47840075bdf160a6577bb133dfe9cbe7 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Tue, 17 Dec 2024 12:57:28 +0100 Subject: [PATCH] Expand DeployImage to be more flexible Usually we create the system dirs in images by default, but that means that we cannot reuse the DeployImage for deploying random non-system images. This fixes it by adding an extra param to create the dir structure in the created image Signed-off-by: Itxaka --- pkg/elemental/elemental.go | 13 +++++++++---- pkg/elemental/elemental_test.go | 20 ++++++++++---------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/pkg/elemental/elemental.go b/pkg/elemental/elemental.go index 27b47ff4..2218b33d 100644 --- a/pkg/elemental/elemental.go +++ b/pkg/elemental/elemental.go @@ -308,7 +308,10 @@ func (e Elemental) CreateFileSystemImage(img *v1.Image) error { // DeployImage will deploy the given image into the target. This method // creates the filesystem image file, mounts it and unmounts it as needed. -func (e *Elemental) DeployImage(img *v1.Image, leaveMounted bool) (info interface{}, err error) { +// Set leaveMounted to leave the image mounted, otherwise it unmounts before returning +// Set createDirStructure to create the directory structure in the target, which creates the expected dirs +// for a running system. This is so we can reuse this method for creating random images, not only system ones +func (e *Elemental) DeployImage(img *v1.Image, leaveMounted, createDirStructure bool) (info interface{}, err error) { target := img.MountPoint if !img.Source.IsFile() { if img.FS != cnst.SquashFs { @@ -338,9 +341,11 @@ func (e *Elemental) DeployImage(img *v1.Image, leaveMounted bool) (info interfac return nil, err } if !img.Source.IsFile() { - err = utils.CreateDirStructure(e.config.Fs, target) - if err != nil { - return nil, err + if createDirStructure { + err = utils.CreateDirStructure(e.config.Fs, target) + if err != nil { + return nil, err + } } if img.FS == cnst.SquashFs { squashOptions := append(cnst.GetDefaultSquashfsOptions(), e.config.SquashFsCompressionConfig...) diff --git a/pkg/elemental/elemental_test.go b/pkg/elemental/elemental_test.go index 34ac5a82..df7afe87 100644 --- a/pkg/elemental/elemental_test.go +++ b/pkg/elemental/elemental_test.go @@ -474,14 +474,14 @@ var _ = Describe("Elemental", Label("elemental"), func() { } }) It("Deploys an image from a directory and leaves it mounted", func() { - Expect(el.DeployImage(img, true)).To(BeNil()) + Expect(el.DeployImage(img, true, true)).To(BeNil()) }) It("Deploys an image from a directory and leaves it unmounted", func() { - Expect(el.DeployImage(img, false)).To(BeNil()) + Expect(el.DeployImage(img, false, true)).To(BeNil()) }) It("Deploys an squashfs image from a directory", func() { img.FS = cnst.SquashFs - Expect(el.DeployImage(img, true)).To(BeNil()) + Expect(el.DeployImage(img, true, true)).To(BeNil()) Expect(runner.MatchMilestones([][]string{ { "mksquashfs", "/tmp/elemental-tmp", "/tmp/elemental/image.img", @@ -497,7 +497,7 @@ var _ = Describe("Elemental", Label("elemental"), func() { Expect(err).To(BeNil()) img.Source = v1.NewFileSrc(sourceImg) img.MountPoint = destDir - Expect(el.DeployImage(img, true)).To(BeNil()) + Expect(el.DeployImage(img, true, true)).To(BeNil()) }) It("Deploys a file image and fails to mount it", func() { sourceImg := "/source.img" @@ -508,7 +508,7 @@ var _ = Describe("Elemental", Label("elemental"), func() { img.Source = v1.NewFileSrc(sourceImg) img.MountPoint = destDir mounter.ErrorOnMount = true - _, err = el.DeployImage(img, true) + _, err = el.DeployImage(img, true, true) Expect(err).NotTo(BeNil()) }) It("Deploys a file image and fails to label it", func() { @@ -520,13 +520,13 @@ var _ = Describe("Elemental", Label("elemental"), func() { img.Source = v1.NewFileSrc(sourceImg) img.MountPoint = destDir cmdFail = "tune2fs" - _, err = el.DeployImage(img, true) + _, err = el.DeployImage(img, true, true) Expect(err).NotTo(BeNil()) }) It("Fails creating the squashfs filesystem", func() { cmdFail = "mksquashfs" img.FS = cnst.SquashFs - _, err := el.DeployImage(img, true) + _, err := el.DeployImage(img, true, true) Expect(err).NotTo(BeNil()) Expect(runner.MatchMilestones([][]string{ { @@ -537,17 +537,17 @@ var _ = Describe("Elemental", Label("elemental"), func() { }) It("Fails formatting the image", func() { cmdFail = "mkfs.ext2" - _, err := el.DeployImage(img, true) + _, err := el.DeployImage(img, true, true) Expect(err).NotTo(BeNil()) }) It("Fails mounting the image", func() { mounter.ErrorOnMount = true - _, err := el.DeployImage(img, true) + _, err := el.DeployImage(img, true, true) Expect(err).NotTo(BeNil()) }) It("Fails unmounting the image after copying", func() { mounter.ErrorOnUnmount = true - _, err := el.DeployImage(img, false) + _, err := el.DeployImage(img, false, true) Expect(err).NotTo(BeNil()) }) })