Skip to content

Commit

Permalink
many: don't depend on RecoverySystemDir when RecoverySystemLabel is e…
Browse files Browse the repository at this point in the history
…nough
  • Loading branch information
pedronis committed Oct 4, 2022
1 parent 8d4d6b2 commit 8e12d68
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 82 deletions.
12 changes: 7 additions & 5 deletions boot/makebootable.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func MakeBootableImage(model *asserts.Model, rootdir string, bootWith *BootableS
// using information from bootWith and bootFlags. Contrarily to
// MakeBootableImage this happens in a live system.
func MakeBootablePartition(partDir string, opts *bootloader.Options, bootWith *BootableSet, bootMode string, bootFlags []string) error {
if bootWith.RecoverySystemDir != "" {
return fmt.Errorf("internal error: RecoverySystemDir unexpectedly set for MakeBootablePartition")
}
return configureBootloader(partDir, opts, bootWith, bootMode, bootFlags)
}

Expand Down Expand Up @@ -335,6 +338,9 @@ func makeRunnableSystem(model *asserts.Model, bootWith *BootableSet, sealer *Tru
if model.Grade() == asserts.ModelGradeUnset {
return fmt.Errorf("internal error: cannot make pre-UC20 system runnable")
}
if bootWith.RecoverySystemDir != "" {
return fmt.Errorf("internal error: RecoverySystemDir unexpectedly set for MakeRunnableSystem")
}

// TODO:UC20:
// - figure out what to do for uboot gadgets, currently we require them to
Expand Down Expand Up @@ -369,11 +375,7 @@ func makeRunnableSystem(model *asserts.Model, bootWith *BootableSet, sealer *Tru
currentTrustedBootAssets = sealer.currentTrustedBootAssetsMap()
currentTrustedRecoveryBootAssets = sealer.currentTrustedRecoveryBootAssetsMap()
}
// filepath.Base("") returns ".", so we need to be a bit careful here
recoverySystemLabel := ""
if bootWith.RecoverySystemDir != "" {
recoverySystemLabel = filepath.Base(bootWith.RecoverySystemDir)
}
recoverySystemLabel := bootWith.RecoverySystemLabel
// write modeenv on the ubuntu-data partition
modeenv := &Modeenv{
Mode: "run",
Expand Down
112 changes: 55 additions & 57 deletions boot/makebootable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,15 +554,15 @@ version: 5.0
c.Assert(err, IsNil)

bootWith := &boot.BootableSet{
RecoverySystemDir: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
RecoverySystemLabel: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
}

// set up observer state
Expand Down Expand Up @@ -924,15 +924,15 @@ version: 5.0
c.Assert(err, IsNil)

bootWith := &boot.BootableSet{
RecoverySystemDir: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
RecoverySystemLabel: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
}

// no grub marker in gadget directory raises an error
Expand Down Expand Up @@ -1033,15 +1033,15 @@ version: 5.0
c.Assert(err, IsNil)

bootWith := &boot.BootableSet{
RecoverySystemDir: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
RecoverySystemLabel: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
}

// set up observer state
Expand Down Expand Up @@ -1228,15 +1228,15 @@ version: 5.0
c.Assert(err, IsNil)

bootWith := &boot.BootableSet{
RecoverySystemDir: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
RecoverySystemLabel: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
}

// set up observer state
Expand Down Expand Up @@ -1469,15 +1469,15 @@ version: 5.0
c.Assert(err, IsNil)

bootWith := &boot.BootableSet{
RecoverySystemDir: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
RecoverySystemLabel: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
}

// set a mock recovery kernel
Expand Down Expand Up @@ -1671,15 +1671,15 @@ version: 5.0
c.Assert(err, IsNil)

bootWith := &boot.BootableSet{
RecoverySystemDir: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
RecoverySystemLabel: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
}
err = boot.MakeRunnableSystem(model, bootWith, nil)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -1857,7 +1857,6 @@ version: 5.0
KernelPath: kernelInSeed,
Gadget: gadgetInfo,
GadgetPath: gadgetInSeed,
RecoverySystemDir: "",
RecoverySystemLabel: "",
UnpackedGadgetDir: unpackedGadgetDir,
Recovery: false,
Expand Down Expand Up @@ -1962,7 +1961,6 @@ version: 5.0
c.Assert(err, IsNil)

bootWith := &boot.BootableSet{
RecoverySystemDir: "",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Expand All @@ -1976,7 +1974,7 @@ version: 5.0
err = boot.MakeRunnableSystem(model, bootWith, nil)
c.Assert(err, IsNil)

// ensure that there are no good recovery systems as RecoverySystemDir was empty
// ensure that there are no good recovery systems as RecoverySystemLabel was empty
mockSeedGrubenv := filepath.Join(mockSeedGrubDir, "grubenv")
c.Check(mockSeedGrubenv, testutil.FilePresent)
systemGenv := grubenv.NewEnv(mockSeedGrubenv)
Expand Down
6 changes: 4 additions & 2 deletions overlord/devicestate/devicestate_install_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ func (s *deviceMgrInstallModeSuite) doRunChangeTestWithEncryption(c *C, grade st
c.Check(model, DeepEquals, mockModel)
c.Check(bootWith.KernelPath, Matches, ".*/var/lib/snapd/snaps/pc-kernel_1.snap")
c.Check(bootWith.BasePath, Matches, ".*/var/lib/snapd/snaps/core20_2.snap")
c.Check(bootWith.RecoverySystemDir, Matches, "/systems/20191218")
c.Check(bootWith.RecoverySystemLabel, Equals, "20191218")
c.Check(bootWith.RecoverySystemDir, Equals, "")
c.Check(bootWith.UnpackedGadgetDir, Equals, filepath.Join(dirs.SnapMountDir, "pc/1"))
if tc.encrypt {
c.Check(seal, NotNil)
Expand Down Expand Up @@ -2445,7 +2446,8 @@ func (s *deviceMgrInstallModeSuite) doRunFactoryResetChange(c *C, model *asserts
c.Check(makeRunnableModel, DeepEquals, model)
c.Check(bootWith.KernelPath, Matches, ".*/var/lib/snapd/snaps/pc-kernel_1.snap")
c.Check(bootWith.BasePath, Matches, ".*/var/lib/snapd/snaps/core20_2.snap")
c.Check(bootWith.RecoverySystemDir, Matches, "/systems/20191218")
c.Check(bootWith.RecoverySystemLabel, Equals, "20191218")
c.Check(bootWith.RecoverySystemDir, Equals, "")
c.Check(bootWith.UnpackedGadgetDir, Equals, filepath.Join(dirs.SnapMountDir, "pc/1"))
if tc.encrypt {
c.Check(seal, NotNil)
Expand Down
31 changes: 13 additions & 18 deletions overlord/devicestate/handlers_install.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2021 Canonical Ltd
* Copyright (C) 2021-2022 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -379,16 +379,16 @@ func (m *DeviceManager) doSetupRunSystem(t *state.Task, _ *tomb.Tomb) error {
if err != nil {
return fmt.Errorf("cannot get boot base info: %v", err)
}
recoverySystemDir := filepath.Join("/systems", modeEnv.RecoverySystem)
bootWith := &boot.BootableSet{
Base: bootBaseInfo,
BasePath: bootBaseInfo.MountFile(),
Gadget: gadgetInfo,
GadgetPath: gadgetInfo.MountFile(),
Kernel: kernelInfo,
KernelPath: kernelInfo.MountFile(),
RecoverySystemDir: recoverySystemDir,
UnpackedGadgetDir: gadgetDir,

RecoverySystemLabel: modeEnv.RecoverySystem,
}
timings.Run(perfTimings, "boot-make-runnable", "Make target system runnable", func(timings.Measurer) {
err = bootMakeRunnable(deviceCtx.Model(), bootWith, trustedInstallObserver)
Expand Down Expand Up @@ -989,16 +989,16 @@ func (m *DeviceManager) doFactoryResetRunSystem(t *state.Task, _ *tomb.Tomb) err
if err != nil {
return fmt.Errorf("cannot get boot base info: %v", err)
}
recoverySystemDir := filepath.Join("/systems", modeEnv.RecoverySystem)
bootWith := &boot.BootableSet{
Base: bootBaseInfo,
BasePath: bootBaseInfo.MountFile(),
Gadget: gadgetInfo,
GadgetPath: gadgetInfo.MountFile(),
Kernel: kernelInfo,
KernelPath: kernelInfo.MountFile(),
RecoverySystemDir: recoverySystemDir,
UnpackedGadgetDir: gadgetDir,

RecoverySystemLabel: modeEnv.RecoverySystem,
}
timings.Run(perfTimings, "boot-make-runnable", "Make target system runnable", func(timings.Measurer) {
err = bootMakeRunnableAfterDataReset(deviceCtx.Model(), bootWith, trustedInstallObserver)
Expand Down Expand Up @@ -1314,20 +1314,15 @@ func (m *DeviceManager) doInstallFinish(t *state.Task, _ *tomb.Tomb) error {
defer unmount()

bootWith := &boot.BootableSet{
Base: snapInfos[snap.TypeBase],
BasePath: snapSeeds[snap.TypeBase].Path,
Kernel: snapInfos[snap.TypeKernel],
KernelPath: snapSeeds[snap.TypeKernel].Path,
Gadget: snapInfos[snap.TypeGadget],
GadgetPath: snapSeeds[snap.TypeGadget].Path,

// modeenv:recovery_system will be set based on
// RecoverySystemDir and seeding reads this var
// from modeenv to know what it's seeding from
RecoverySystemDir: systemLabel,
RecoverySystemLabel: systemLabel,

Base: snapInfos[snap.TypeBase],
BasePath: snapSeeds[snap.TypeBase].Path,
Kernel: snapInfos[snap.TypeKernel],
KernelPath: snapSeeds[snap.TypeKernel].Path,
Gadget: snapInfos[snap.TypeGadget],
GadgetPath: snapSeeds[snap.TypeGadget].Path,
UnpackedGadgetDir: mntPtForType[snap.TypeGadget],

RecoverySystemLabel: systemLabel,
}

// installs in ESP: grub.cfg, grubenv
Expand Down

0 comments on commit 8e12d68

Please sign in to comment.