Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract recovery kernel #2016

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ build-iso:
mkdir -p $(ROOT_DIR)/build
$(DOCKER) run --rm -v $(DOCKER_SOCK):$(DOCKER_SOCK) -v $(ROOT_DIR)/build:/build \
--entrypoint /usr/bin/elemental $(TOOLKIT_REPO):$(VERSION) --debug build-iso --bootloader-in-rootfs -n elemental-$(FLAVOR).$(ARCH) \
--local --platform $(PLATFORM) --squash-no-compression -o /build $(REPO):$(VERSION)
--local --platform $(PLATFORM) -o /build $(REPO):$(VERSION)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it default to compressed squashfs images?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only for our example isos/disks since I noticed we saved ~50MB on the final image which could speed up CI sligthly.


.PHONY: build-disk
build-disk:
Expand All @@ -99,7 +99,7 @@ build-disk:
$(DOCKER) run --rm -v $(DOCKER_SOCK):$(DOCKER_SOCK) -v $(ROOT_DIR)/build:/build \
--entrypoint /usr/bin/elemental \
$(TOOLKIT_REPO):$(VERSION) --debug build-disk --platform $(PLATFORM) --expandable -n elemental-$(FLAVOR).$(ARCH) --local \
--squash-no-compression -o /build --system $(REPO):$(VERSION)
-o /build --system $(REPO):$(VERSION)
qemu-img convert -O qcow2 $(ROOT_DIR)/build/elemental-$(FLAVOR).$(ARCH).raw $(ROOT_DIR)/build/elemental-$(FLAVOR).$(ARCH).qcow2
qemu-img resize $(ROOT_DIR)/build/elemental-$(FLAVOR).$(ARCH).qcow2 $(DISKSIZE)

Expand Down
2 changes: 2 additions & 0 deletions cmd/upgrade-recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func NewUpgradeRecoveryCmd(root *cobra.Command, addCheckRoot bool) *cobra.Comman
}
root.AddCommand(c)
addRecoverySystemFlag(c)
addPowerFlags(c)
addSquashFsCompressionFlags(c)
return c
}

Expand Down
20 changes: 14 additions & 6 deletions pkg/action/build-disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,24 @@ func (b *BuildDiskAction) BuildDiskRun() (err error) { //nolint:gocyclo
return elementalError.NewFromError(err, elementalError.HookAfterDisk)
}

// Create recovery image and removes recovery root when done
err = elemental.CreateImageFromTree(
b.cfg.Config, &b.spec.RecoverySystem, recRoot, b.spec.Expandable,
func() error { return b.cfg.Fs.RemoveAll(recRoot) },
)
// Create recovery image
bootDir := filepath.Join(b.roots[constants.RecoveryPartName], "boot")
if err = utils.MkdirAll(b.cfg.Fs, bootDir, constants.DirPerm); err != nil {
b.cfg.Logger.Errorf("failed creating recovery boot dir: %v", err)
return err
}

tmpSrc := b.spec.RecoverySystem.Source
b.spec.RecoverySystem.Source = types.NewDirSrc(recRoot)
err = elemental.DeployRecoverySystem(b.cfg.Config, &b.spec.RecoverySystem, bootDir)
if err != nil {
b.cfg.Logger.Errorf("failed creating recovery image from root-tree: %s", err.Error())
b.cfg.Logger.Errorf("failed deploying recovery system: %v", err)
return err
}

// reset source so the correct one will be used for the state.yaml
b.spec.RecoverySystem.Source = tmpSrc

if b.spec.Expandable {
err = b.SetExpandableCloudInitStage()
if err != nil {
Expand Down
44 changes: 14 additions & 30 deletions pkg/action/build-iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,20 @@ func (b *BuildISOAction) ISORun() error {
return err
}

err = b.prepareISORoot(isoDir, rootDir)
bootDir := filepath.Join(isoDir, constants.ISOLoaderPath(b.cfg.Platform.Arch))
err = utils.MkdirAll(b.cfg.Fs, bootDir, constants.DirPerm)
if err != nil {
b.cfg.Logger.Errorf("Failed creating boot dir: %v", err)
return err
}

image := &types.Image{
Source: types.NewDirSrc(rootDir),
File: filepath.Join(isoDir, constants.ISORootFile),
FS: constants.SquashFs,
}

err = elemental.DeployRecoverySystem(b.cfg.Config, image, bootDir)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this method 👍

if err != nil {
b.cfg.Logger.Errorf("Failed preparing ISO's root tree: %v", err)
return err
Expand Down Expand Up @@ -212,35 +225,6 @@ func (b *BuildISOAction) renderGrubTemplate(rootDir string) error {
)
}

func (b BuildISOAction) prepareISORoot(isoDir string, rootDir string) error {
kernel, initrd, err := utils.FindKernelInitrd(b.cfg.Fs, rootDir)
if err != nil {
b.cfg.Logger.Error("Could not find kernel and/or initrd")
return elementalError.NewFromError(err, elementalError.StatFile)
}
err = utils.MkdirAll(b.cfg.Fs, filepath.Join(isoDir, constants.ISOLoaderPath(b.cfg.Platform.Arch)), constants.DirPerm)
if err != nil {
return elementalError.NewFromError(err, elementalError.CreateDir)
}
//TODO document boot/kernel and boot/initrd expectation in bootloader config
b.cfg.Logger.Debugf("Copying Kernel file %s to iso root tree", kernel)
err = utils.CopyFile(b.cfg.Fs, kernel, filepath.Join(isoDir, constants.ISOKernelPath(b.cfg.Platform.Arch)))
if err != nil {
return elementalError.NewFromError(err, elementalError.CopyFile)
}

b.cfg.Logger.Debugf("Copying initrd file %s to iso root tree", initrd)
err = utils.CopyFile(b.cfg.Fs, initrd, filepath.Join(isoDir, constants.ISOInitrdPath(b.cfg.Platform.Arch)))
if err != nil {
return elementalError.NewFromError(err, elementalError.CopyFile)
}

b.cfg.Logger.Info("Creating squashfs...")
squashOptions := append(constants.GetDefaultSquashfsOptions(), b.cfg.SquashFsCompressionConfig...)
err = utils.CreateSquashFS(b.cfg.Runner, b.cfg.Logger, rootDir, filepath.Join(isoDir, constants.ISORootFile), squashOptions)
return elementalError.NewFromError(err, elementalError.MKFSCall)
}

func (b BuildISOAction) createEFI(root string, img string) error {
efiSize, err := utils.DirSize(b.cfg.Fs, root)
if err != nil {
Expand Down
16 changes: 12 additions & 4 deletions pkg/action/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,21 @@ var _ = Describe("Build Actions", func() {
tmpDir, err := utils.TempDir(fs, "", "test")
Expect(err).ShouldNot(HaveOccurred())

recDir := filepath.Join(tmpDir, "build/recovery.img.root")
Expect(utils.MkdirAll(fs, filepath.Join(recDir, "boot"), constants.DirPerm)).To(Succeed())
Expect(utils.MkdirAll(fs, filepath.Join(recDir, "/lib/modules/6.7"), constants.DirPerm)).To(Succeed())
_, err = fs.Create(filepath.Join(recDir, "/boot/vmlinuz-6.7"))
Expect(err).To(Succeed())
_, err = fs.Create(filepath.Join(recDir, "/boot/elemental.initrd-6.7"))
Expect(err).To(Succeed())

cfg.Date = false
cfg.OutDir = tmpDir
disk = config.NewDisk(cfg)
disk.System = types.NewDockerSrc("some/image/ref:tag")
disk.RecoverySystem.Source = disk.System
disk.Partitions.Recovery.Size = constants.MinPartSize
disk.Partitions.State.Size = constants.MinPartSize
disk.RecoverySystem.Source = types.NewDirSrc(recDir)
})
It("Successfully builds a full raw disk", func() {
buildDisk, err := action.NewBuildDiskAction(cfg, disk, action.WithDiskBootloader(bootloader))
Expand All @@ -220,7 +228,7 @@ var _ = Describe("Build Actions", func() {
Expect(buildDisk.BuildDiskRun()).To(Succeed())

Expect(runner.MatchMilestones([][]string{
{"mkfs.ext2", "-L", "COS_SYSTEM", "/tmp/test/build/recovery/recovery.img"},
{"mksquashfs", "/tmp/test/build/recovery.img.root", "/tmp/test/build/recovery/recovery.img"},
{"mkfs.ext4", "-L", "COS_STATE"},
{"losetup", "--show", "-f", "/tmp/test/build/state.part"},
{"mkfs.vfat", "-n", "COS_GRUB"},
Expand All @@ -247,7 +255,7 @@ var _ = Describe("Build Actions", func() {
Expect(buildDisk.BuildDiskRun()).To(Succeed())

Expect(runner.MatchMilestones([][]string{
{"mkfs.ext2", "-L", "COS_SYSTEM", "-d", "/tmp/test/build/recovery.img.root", "/tmp/test/build/recovery/recovery.img"},
{"mksquashfs", "/tmp/test/build/recovery.img.root", "/tmp/test/build/recovery/recovery.img"},
{"mkfs.vfat", "-n", "COS_GRUB"},
{"mkfs.ext4", "-L", "COS_OEM"},
{"mkfs.ext4", "-L", "COS_RECOVERY"},
Expand All @@ -266,7 +274,7 @@ var _ = Describe("Build Actions", func() {
Expect(buildDisk.BuildDiskRun()).NotTo(Succeed())

Expect(runner.MatchMilestones([][]string{
{"mkfs.ext2", "-L", "COS_SYSTEM", "-d", "/tmp/test/build/recovery.img.root", "/tmp/test/build/recovery/recovery.img"},
{"mksquashfs", "/tmp/test/build/recovery.img.root", "/tmp/test/build/recovery/recovery.img"},
})).To(Succeed())

// failed before preparing partitions images
Expand Down
15 changes: 11 additions & 4 deletions pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,26 @@ func (i InstallAction) Run() (err error) {
}

// Install recovery
recoveryBootDir := filepath.Join(i.spec.Partitions.Recovery.MountPoint, "boot")
err = utils.MkdirAll(i.cfg.Fs, recoveryBootDir, constants.DirPerm)
if err != nil {
i.cfg.Logger.Errorf("failed creating recovery boot dir: %v", err)
return err
}

recoverySystem := i.spec.RecoverySystem
i.cfg.Logger.Info("Deploying recovery system")
if recoverySystem.Source.String() == i.spec.System.String() {
// Reuse already deployed root-tree from actice snapshot
// Reuse already deployed root-tree from active snapshot
recoverySystem.Source, err = i.snapshotter.SnapshotToImageSource(i.snapshot)
if err != nil {
return err
}
i.spec.RecoverySystem.Source.SetDigest(i.spec.System.GetDigest())
recoverySystem.Source.SetDigest(i.spec.System.GetDigest())
}
err = elemental.DeployImage(i.cfg.Config, &recoverySystem)
err = elemental.DeployRecoverySystem(i.cfg.Config, &recoverySystem, recoveryBootDir)
if err != nil {
i.cfg.Logger.Error("failed deploying recovery image")
i.cfg.Logger.Errorf("Failed deploying recovery image: %v", err)
return elementalError.NewFromError(err, elementalError.DeployImage)
}

Expand Down
13 changes: 11 additions & 2 deletions pkg/action/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ var _ = Describe("Install action tests", func() {
return []byte{}, nil
}
}

// Need to create the IsoBaseTree, like if we are booting from iso
Expect(utils.MkdirAll(fs, constants.ISOBaseTree, constants.DirPerm)).To(Succeed())

Expand All @@ -155,9 +156,17 @@ var _ = Describe("Install action tests", func() {
loopCfg.Size = 16
Expect(spec.System.Value()).To(Equal(constants.ISOBaseTree))
Expect(spec.System.IsDir()).To(BeTrue())
Expect(spec.RecoverySystem.Source.Value()).To(Equal(constants.ISOBaseTree))
//spec.System = types.NewDockerSrc("fake/image:tag")

// Create minimal recovery system source
spec.RecoverySystem.Source = types.NewDirSrc("/run/elemental/recovery/recovery.imgTree")
Expect(utils.MkdirAll(fs, "/run/elemental/recovery/recovery.imgTree/boot", constants.DirPerm)).To(Succeed())
Expect(utils.MkdirAll(fs, "/run/elemental/recovery/recovery.imgTree/lib/modules/6.7", constants.DirPerm)).To(Succeed())
_, err = fs.Create("/run/elemental/recovery/recovery.imgTree/boot/vmlinuz-6.7")
Expect(err).To(Succeed())
_, err = fs.Create("/run/elemental/recovery/recovery.imgTree/boot/elemental.initrd-6.7")
Expect(err).To(Succeed())

// Write grub config
grubCfg := filepath.Join(constants.WorkingImgDir, constants.GrubCfgPath, constants.GrubCfg)
Expect(utils.MkdirAll(fs, filepath.Dir(grubCfg), constants.DirPerm)).To(Succeed())
_, err = fs.Create(grubCfg)
Expand Down
13 changes: 10 additions & 3 deletions pkg/action/upgrade-recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,17 @@ func (u *UpgradeRecoveryAction) Run() (err error) {
return err
}

// Create recovery /boot dir if not exists
bootDir := filepath.Join(u.spec.Partitions.Recovery.MountPoint, "boot")
if err := utils.MkdirAll(u.cfg.Fs, bootDir, constants.DirPerm); err != nil {
u.cfg.Logger.Errorf("failed creating recovery boot dir: %v", err)
return elementalError.NewFromError(err, elementalError.CreateDir)
}

// Upgrade recovery
err = elemental.DeployImage(u.cfg.Config, &u.spec.RecoverySystem)
err = elemental.DeployRecoverySystem(u.cfg.Config, &u.spec.RecoverySystem, bootDir)
if err != nil {
u.cfg.Logger.Error("failed deploying recovery image")
u.cfg.Logger.Errorf("failed deploying recovery image: %v", err)
return elementalError.NewFromError(err, elementalError.DeployImage)
}
recoveryFile := filepath.Join(u.spec.Partitions.Recovery.MountPoint, constants.RecoveryImgFile)
Expand Down Expand Up @@ -184,5 +191,5 @@ func (u *UpgradeRecoveryAction) Run() (err error) {
return elementalError.NewFromError(err, elementalError.Cleanup)
}

return nil
return PowerAction(u.cfg)
}
14 changes: 11 additions & 3 deletions pkg/action/upgrade-recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ var _ = Describe("Upgrade Recovery Actions", func() {
})
It("Successfully upgrades recovery from docker image", Label("docker"), func() {
recoveryImgPath := filepath.Join(constants.LiveDir, constants.RecoveryImgFile)
spec := PrepareTestRecoveryImage(config, recoveryImgPath, fs, runner)
spec := PrepareTestRecoveryImage(config, constants.LiveDir, fs, runner)

// This should be the old image
info, err := fs.Stat(recoveryImgPath)
Expand Down Expand Up @@ -213,7 +213,7 @@ var _ = Describe("Upgrade Recovery Actions", func() {
})
It("Successfully skips updateInstallState", Label("docker"), func() {
recoveryImgPath := filepath.Join(constants.LiveDir, constants.RecoveryImgFile)
spec := PrepareTestRecoveryImage(config, recoveryImgPath, fs, runner)
spec := PrepareTestRecoveryImage(config, constants.LiveDir, fs, runner)

// This should be the old image
info, err := fs.Stat(recoveryImgPath)
Expand Down Expand Up @@ -252,7 +252,7 @@ var _ = Describe("Upgrade Recovery Actions", func() {
})
})

func PrepareTestRecoveryImage(config *types.RunConfig, recoveryImgPath string, fs vfs.FS, runner *mocks.FakeRunner) *types.UpgradeSpec {
func PrepareTestRecoveryImage(config *types.RunConfig, recoveryPath string, fs vfs.FS, runner *mocks.FakeRunner) *types.UpgradeSpec {
GinkgoHelper()
// Create installState with squashed recovery
statePath := filepath.Join(constants.RunningStateDir, constants.InstallStateFile)
Expand All @@ -270,8 +270,16 @@ func PrepareTestRecoveryImage(config *types.RunConfig, recoveryImgPath string, f
}
Expect(config.WriteInstallState(installState, statePath, statePath)).ShouldNot(HaveOccurred())

recoveryImgPath := filepath.Join(recoveryPath, constants.RecoveryImgFile)
Expect(fs.WriteFile(recoveryImgPath, []byte("recovery"), constants.FilePerm)).ShouldNot(HaveOccurred())

transitionDir := filepath.Join(recoveryPath, "transition.imgTree")
Expect(utils.MkdirAll(fs, filepath.Join(transitionDir, "lib/modules/6.6"), constants.DirPerm)).ShouldNot(HaveOccurred())
bootDir := filepath.Join(transitionDir, "boot")
Expect(utils.MkdirAll(fs, bootDir, constants.DirPerm)).ShouldNot(HaveOccurred())
Expect(fs.WriteFile(filepath.Join(bootDir, "vmlinuz-6.6"), []byte("kernel"), constants.FilePerm)).ShouldNot(HaveOccurred())
Expect(fs.WriteFile(filepath.Join(bootDir, "elemental.initrd-6.6"), []byte("initrd"), constants.FilePerm)).ShouldNot(HaveOccurred())

spec, err := conf.NewUpgradeSpec(config.Config)
Expect(err).ShouldNot(HaveOccurred())

Expand Down
2 changes: 1 addition & 1 deletion pkg/action/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ var _ = Describe("Runtime Actions", func() {
})
It("Successfully upgrades recovery from docker image", Label("docker"), func() {
recoveryImgPath := filepath.Join(constants.LiveDir, constants.RecoveryImgFile)
spec := PrepareTestRecoveryImage(config, recoveryImgPath, fs, runner)
spec := PrepareTestRecoveryImage(config, constants.LiveDir, fs, runner)

// This should be the old image
info, err := fs.Stat(recoveryImgPath)
Expand Down
6 changes: 2 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ func NewInstallSpec(cfg types.Config) *types.InstallSpec {
}

recoverySystem.Source = system
recoverySystem.FS = constants.LinuxImgFs
recoverySystem.Label = constants.SystemLabel
recoverySystem.FS = constants.SquashFs
recoverySystem.File = filepath.Join(constants.RecoveryDir, constants.RecoveryImgFile)
recoverySystem.MountPoint = constants.TransitionDir

Expand Down Expand Up @@ -516,8 +515,7 @@ func NewDisk(cfg *types.BuildConfig) *types.DiskSpec {

recoveryImg.Size = constants.ImgSize
recoveryImg.File = filepath.Join(workdir, constants.RecoveryPartName, constants.RecoveryImgFile)
recoveryImg.FS = constants.LinuxImgFs
recoveryImg.Label = constants.SystemLabel
recoveryImg.FS = constants.SquashFs
recoveryImg.Source = types.NewEmptySrc()
recoveryImg.MountPoint = filepath.Join(
workdir, strings.TrimSuffix(
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const (
EfiImgArm64 = "bootaa64.efi"
EfiImgRiscv64 = "bootriscv64.efi"
GrubCfg = "grub.cfg"
BootargsCfg = "bootargs.cfg"
GrubCfgPath = "/etc/elemental"
GrubOEMEnv = "grub_oem_env"
GrubEnv = "grubenv"
Expand Down
Loading
Loading