Skip to content

Commit

Permalink
Expand DeployImage to be more flexible
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Itxaka committed Dec 17, 2024
1 parent 248c622 commit 5a28754
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
6 changes: 3 additions & 3 deletions pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/action/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions pkg/elemental/elemental.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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...)
Expand Down
20 changes: 10 additions & 10 deletions pkg/elemental/elemental_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand All @@ -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() {
Expand All @@ -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{
{
Expand All @@ -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())
})
})
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/grub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5a28754

Please sign in to comment.