From 5a28754e4f632c62fe7d81287b37b8a6ffa6198f 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/action/install.go | 6 +++--- pkg/action/reset.go | 4 ++-- pkg/action/upgrade.go | 2 +- pkg/elemental/elemental.go | 13 +++++++++---- pkg/elemental/elemental_test.go | 20 ++++++++++---------- pkg/utils/grub.go | 3 +++ 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/pkg/action/install.go b/pkg/action/install.go index 380ff9fe..bb218611 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -195,7 +195,7 @@ func (i InstallAction) Run() (err error) { } // Deploy active image - systemMeta, err := e.DeployImage(&i.spec.Active, true) + systemMeta, err := e.DeployImage(&i.spec.Active, true, true) if err != nil { return err } @@ -260,12 +260,12 @@ func (i InstallAction) Run() (err error) { return err } // Install Recovery - recoveryMeta, err := e.DeployImage(&i.spec.Recovery, false) + recoveryMeta, err := e.DeployImage(&i.spec.Recovery, false, true) if err != nil { return err } // Install Passive - _, err = e.DeployImage(&i.spec.Passive, false) + _, err = e.DeployImage(&i.spec.Passive, false, true) if err != nil { return err } diff --git a/pkg/action/reset.go b/pkg/action/reset.go index 08d784ef..7f1ef0d1 100644 --- a/pkg/action/reset.go +++ b/pkg/action/reset.go @@ -190,7 +190,7 @@ func (r ResetAction) Run() (err error) { cleanup.Push(func() error { return e.UnmountPartition(r.spec.Partitions.State) }) // Deploy active image - meta, err := e.DeployImage(&r.spec.Active, true) + meta, err := e.DeployImage(&r.spec.Active, true, true) if err != nil { return err } @@ -264,7 +264,7 @@ func (r ResetAction) Run() (err error) { } // Install Passive - _, err = e.DeployImage(&r.spec.Passive, false) + _, err = e.DeployImage(&r.spec.Passive, false, true) if err != nil { return err } diff --git a/pkg/action/upgrade.go b/pkg/action/upgrade.go index b2ea6eeb..9752f630 100644 --- a/pkg/action/upgrade.go +++ b/pkg/action/upgrade.go @@ -189,7 +189,7 @@ func (u *UpgradeAction) Run() (err error) { } u.Info("deploying image %s to %s", upgradeImg.Source.Value(), upgradeImg.File) - upgradeMeta, err := e.DeployImage(&upgradeImg, true) + upgradeMeta, err := e.DeployImage(&upgradeImg, true, true) if err != nil { u.Error("Failed deploying image to file '%s': %s", upgradeImg.File, err) return err 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()) }) }) diff --git a/pkg/utils/grub.go b/pkg/utils/grub.go index cb08ecb5..90b2da62 100644 --- a/pkg/utils/grub.go +++ b/pkg/utils/grub.go @@ -46,6 +46,9 @@ func NewGrub(config *agentConfig.Config) *Grub { } // Install installs grub into the device, copy the config file and add any extra TTY to grub +// TODO: Make it more generic to be able to call it from other places +// i.e.: filepath.Join(cnst.ActiveDir, "etc/kairos-release") seraches for the file in the active dir, we should be looking into the rootdir? +// filepath.Join(cnst.EfiDir, "EFI/boot/grub.cfg") we also write into the efi dir directly, this should be the a var maybe? func (g Grub) Install(target, rootDir, bootDir, grubConf, tty string, efi bool, stateLabel string) (err error) { // nolint:gocyclo var grubargs []string var grubdir, finalContent string