Skip to content

Commit b24de36

Browse files
many: some refactoring and name changes
1 parent 5a5548a commit b24de36

File tree

6 files changed

+128
-58
lines changed

6 files changed

+128
-58
lines changed

cmd/snap-bootstrap/cmd_initramfs_mounts.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ func readSnapInfo(sysSnaps map[snap.Type]*seed.Snap, snapType snap.Type) (*snap.
259259
if err != nil {
260260
return nil, err
261261
}
262+
// Comes from the seed and it might be unasserted, set revision in that case
262263
if info.Revision.Unset() {
263264
info.Revision = snap.R(-1)
264265
}
@@ -272,6 +273,7 @@ func readComponentInfo(seedComp *seed.Component, mntPt string, snapInfo *snap.In
272273
if err != nil {
273274
return nil, err
274275
}
276+
// Comes from the seed and it might be unasserted, set revision in that case
275277
if ci.Revision.Unset() {
276278
ci.Revision = snap.R(-1)
277279
}
@@ -353,7 +355,7 @@ func doInstall(mst *initramfsMountsState, model *asserts.Model, sysSnaps map[sna
353355

354356
kernelSeed := sysSnaps[snap.TypeKernel]
355357
kernCompsMntPts := make(map[string]string)
356-
compSeedInfos := []install.CompSeedInfo{}
358+
compSeedInfos := []install.ComponentSeedInfo{}
357359
for _, sc := range kernelSeed.Components {
358360
seedComp := sc
359361
comp, ok := kernCompsByName[seedComp.CompSideInfo.Component.ComponentName]
@@ -365,7 +367,9 @@ func doInstall(mst *initramfsMountsState, model *asserts.Model, sysSnaps map[sna
365367
continue
366368
}
367369

368-
// Mount ephemerally the kernel-modules components
370+
// Mount ephemerally the kernel-modules components to read
371+
// their metadata and also to make them accessible if building
372+
// the drivers tree.
369373
mntPt := filepath.Join(filepath.Join(boot.InitramfsRunMntDir, "snap-content",
370374
seedComp.CompSideInfo.Component.String()))
371375
if err := doSystemdMount(seedComp.Path, mntPt, &systemdMountOptions{
@@ -388,18 +392,18 @@ func doInstall(mst *initramfsMountsState, model *asserts.Model, sysSnaps map[sna
388392
if err != nil {
389393
return err
390394
}
391-
compSeedInfos = append(compSeedInfos, install.CompSeedInfo{
392-
CompInfo: compInfo,
393-
CompSeed: &seedComp,
395+
compSeedInfos = append(compSeedInfos, install.ComponentSeedInfo{
396+
Info: compInfo,
397+
Seed: &seedComp,
394398
})
395399
}
396400

397-
preseed := false
398401
currentSeed, err := mst.LoadSeed(mst.recoverySystem)
399402
if err != nil {
400403
return err
401404
}
402405
preseedSeed, ok := currentSeed.(seed.PreseedCapable)
406+
preseed := false
403407
if ok && preseedSeed.HasArtifact("preseed.tgz") {
404408
preseed = true
405409
}
@@ -409,7 +413,7 @@ func doInstall(mst *initramfsMountsState, model *asserts.Model, sysSnaps map[sna
409413
isCore := !model.Classic()
410414
kernelBootInfo := install.BuildKernelBootInfo(
411415
kernelSnap, compSeedInfos, kernelMountDir, kernCompsMntPts,
412-
isCore, needsKernelSetup)
416+
install.BuildKernelBootInfoOpts{IsCore: isCore, NeedsDriversTree: needsKernelSetup})
413417

414418
bootDevice := ""
415419
installedSystem, err := gadgetInstallRun(model, gadgetMountDir, kernelBootInfo.KSnapInfo, bootDevice, options, installObserver, timings.New(nil))
@@ -459,14 +463,15 @@ func doInstall(mst *initramfsMountsState, model *asserts.Model, sysSnaps map[sna
459463
}
460464

461465
if preseed {
466+
// Extract pre-seed tarball
462467
runMode := false
463468
if err := installApplyPreseededData(preseedSeed,
464469
boot.InitramfsWritableDir(model, runMode)); err != nil {
465470
return err
466471
}
467472
}
468473

469-
// Create drivers tree mount units
474+
// Create drivers tree mount units to make it available before switch root
470475
rootfsDir := filepath.Join(boot.InitramfsDataDir, "system-data")
471476
hasDriversTree, err := createKernelMounts(
472477
rootfsDir, kernelSnap.SnapName(), kernelSnap.Revision, !isCore)

cmd/snap-bootstrap/cmd_initramfs_mounts_test.go

Lines changed: 84 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package main_test
2222
import (
2323
"bytes"
2424
"encoding/json"
25+
"errors"
2526
"fmt"
2627
"os"
2728
"path/filepath"
@@ -787,6 +788,16 @@ grade=signed
787788
}
788789

789790
func (s *initramfsMountsSuite) TestInitramfsMountsInstallModeWithCompsHappy(c *C) {
791+
failMount := false
792+
s.testInitramfsMountsInstallModeWithCompsHappy(c, failMount)
793+
}
794+
795+
func (s *initramfsMountsSuite) TestInitramfsMountsInstallModeWithCompsFailMount(c *C) {
796+
failMount := true
797+
s.testInitramfsMountsInstallModeWithCompsHappy(c, failMount)
798+
}
799+
800+
func (s *initramfsMountsSuite) testInitramfsMountsInstallModeWithCompsHappy(c *C, failMount bool) {
790801
var efiArch string
791802
switch runtime.GOARCH {
792803
case "amd64":
@@ -963,7 +974,7 @@ func (s *initramfsMountsSuite) TestInitramfsMountsInstallModeWithCompsHappy(c *C
963974
return mockObserver, mockObserver, nil
964975
})()
965976

966-
restore := s.mockSystemdMountSequence(c, []systemdMount{
977+
mounts := []systemdMount{
967978
s.ubuntuLabelMount("ubuntu-seed", "install"),
968979
s.makeSeedSnapSystemdMount(snap.TypeSnapd),
969980
s.makeSeedSnapSystemdMount(snap.TypeKernel),
@@ -990,8 +1001,33 @@ func (s *initramfsMountsSuite) TestInitramfsMountsInstallModeWithCompsHappy(c *C
9901001
boot.InitramfsDataDir,
9911002
bindOpts,
9921003
nil,
993-
},
994-
}, nil)
1004+
}}
1005+
if failMount {
1006+
mounts = []systemdMount{
1007+
s.ubuntuLabelMount("ubuntu-seed", "install"),
1008+
s.makeSeedSnapSystemdMount(snap.TypeSnapd),
1009+
s.makeSeedSnapSystemdMount(snap.TypeKernel),
1010+
s.makeSeedSnapSystemdMountForBase(snap.TypeBase, "core24"),
1011+
s.makeSeedSnapSystemdMount(snap.TypeGadget),
1012+
{
1013+
filepath.Join(s.seedDir, "snaps/pc-kernel+kcomp1_77.comp"),
1014+
filepath.Join(boot.InitramfsRunMntDir, "snap-content/pc-kernel+kcomp1"),
1015+
&main.SystemdMountOptions{ReadOnly: true,
1016+
Private: true,
1017+
Ephemeral: true},
1018+
nil,
1019+
},
1020+
{
1021+
filepath.Join(s.seedDir, "snaps/pc-kernel+kcomp2_77.comp"),
1022+
filepath.Join(boot.InitramfsRunMntDir, "snap-content/pc-kernel+kcomp2"),
1023+
&main.SystemdMountOptions{ReadOnly: true,
1024+
Private: true,
1025+
Ephemeral: true},
1026+
errors.New("error mounting"),
1027+
},
1028+
}
1029+
}
1030+
restore := s.mockSystemdMountSequence(c, mounts, nil)
9951031
defer restore()
9961032

9971033
cmd := testutil.MockCommand(c, "systemd-mount", ``)
@@ -1000,35 +1036,55 @@ func (s *initramfsMountsSuite) TestInitramfsMountsInstallModeWithCompsHappy(c *C
10001036
c.Assert(os.Remove(filepath.Join(boot.InitramfsUbuntuBootDir, "device/model")), IsNil)
10011037

10021038
_, err = main.Parser().ParseArgs([]string{"initramfs-mounts"})
1003-
c.Assert(err, IsNil)
1039+
result := true
1040+
expectedCallsObserve := 1
1041+
if failMount {
1042+
c.Assert(err, ErrorMatches, "error mounting")
1043+
result = false
1044+
expectedCallsObserve = 0
1045+
} else {
1046+
c.Assert(err, IsNil)
1047+
}
10041048
c.Check(sealedKeysLocked, Equals, true)
10051049

1006-
c.Assert(applyPreseedCalled, Equals, true)
1007-
c.Assert(makeRunnableCalled, Equals, true)
1008-
c.Assert(gadgetInstallCalled, Equals, true)
1009-
c.Assert(nextBootEnsured, Equals, true)
1010-
c.Check(observeExistingTrustedRecoveryAssetsCalled, Equals, 1)
1050+
c.Assert(applyPreseedCalled, Equals, result)
1051+
c.Assert(makeRunnableCalled, Equals, result)
1052+
c.Assert(gadgetInstallCalled, Equals, result)
1053+
c.Assert(nextBootEnsured, Equals, result)
1054+
c.Check(observeExistingTrustedRecoveryAssetsCalled, Equals, expectedCallsObserve)
10111055

1012-
checkKernelMounts(c, "/run/mnt/data/system-data", "/writable/system-data",
1013-
[]string{"kcomp1", "kcomp2"}, []string{"77", "77"}, nil, nil)
1056+
if !failMount {
1057+
checkKernelMounts(c, "/run/mnt/data/system-data", "/writable/system-data",
1058+
[]string{"kcomp1", "kcomp2"}, []string{"77", "77"}, nil, nil)
1059+
}
10141060

1015-
c.Assert(cmd.Calls(), DeepEquals, [][]string{
1016-
{
1017-
"systemd-mount",
1018-
"--umount",
1019-
filepath.Join(s.tmpDir, "/run/mnt/kernel"),
1020-
},
1021-
{
1022-
"systemd-mount",
1023-
"--umount",
1024-
filepath.Join(s.tmpDir, "/run/mnt/snap-content/pc-kernel+kcomp2"),
1025-
},
1026-
{
1027-
"systemd-mount",
1028-
"--umount",
1029-
filepath.Join(s.tmpDir, "/run/mnt/snap-content/pc-kernel+kcomp1"),
1030-
},
1031-
})
1061+
if failMount {
1062+
c.Assert(cmd.Calls(), DeepEquals, [][]string{
1063+
{
1064+
"systemd-mount",
1065+
"--umount",
1066+
filepath.Join(s.tmpDir, "/run/mnt/snap-content/pc-kernel+kcomp1"),
1067+
},
1068+
})
1069+
} else {
1070+
c.Assert(cmd.Calls(), DeepEquals, [][]string{
1071+
{
1072+
"systemd-mount",
1073+
"--umount",
1074+
filepath.Join(s.tmpDir, "/run/mnt/kernel"),
1075+
},
1076+
{
1077+
"systemd-mount",
1078+
"--umount",
1079+
filepath.Join(s.tmpDir, "/run/mnt/snap-content/pc-kernel+kcomp2"),
1080+
},
1081+
{
1082+
"systemd-mount",
1083+
"--umount",
1084+
filepath.Join(s.tmpDir, "/run/mnt/snap-content/pc-kernel+kcomp1"),
1085+
},
1086+
})
1087+
}
10321088
}
10331089

10341090
func (s *initramfsMountsSuite) TestInitramfsMountsInstallModeBootFlagsSet(c *C) {

kernel/kernel_drivers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func EnsureKernelDriversTree(kMntPts MountPoints, compsMntPts []ModulesCompMount
410410
return nil
411411
}
412412

413-
// NeedsKernelDriversTree tells if we need a kernel drivers tree for this model.
413+
// NeedsKernelDriversTree returns true if we need a kernel drivers tree for this model.
414414
func NeedsKernelDriversTree(mod *asserts.Model) bool {
415415
// Checking if it has modeenv - it must be UC20+ or hybrid
416416
if mod.Grade() == asserts.ModelGradeUnset {

overlord/devicestate/devicemgr.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,7 +2248,7 @@ type systemAndEssentialSnaps struct {
22482248
*System
22492249
Seed seed.Seed
22502250
InfosByType map[snap.Type]*snap.Info
2251-
CompsByType map[snap.Type][]install.CompSeedInfo
2251+
CompsByType map[snap.Type][]install.ComponentSeedInfo
22522252
SeedSnapsByType map[snap.Type]*seed.Snap
22532253
}
22542254

@@ -2305,7 +2305,7 @@ func (m *DeviceManager) loadSystemAndEssentialSnaps(wantedSystemLabel string, ty
23052305
// like "snapd" will be skipped and not part of the EssentialSnaps list
23062306
//
23072307
snapInfos := make(map[snap.Type]*snap.Info)
2308-
compInfos := make(map[snap.Type][]install.CompSeedInfo)
2308+
compInfos := make(map[snap.Type][]install.ComponentSeedInfo)
23092309
seedSnaps := make(map[snap.Type]*seed.Snap)
23102310
for _, seedSnap := range s.EssentialSnaps() {
23112311
typ := seedSnap.EssentialType
@@ -2329,9 +2329,9 @@ func (m *DeviceManager) loadSystemAndEssentialSnaps(wantedSystemLabel string, ty
23292329
return nil, fmt.Errorf("internal error while retrieving %s for %s mode: %v",
23302330
seedSnap.SnapName(), modeForComps, err)
23312331
}
2332-
var compInfosForType []install.CompSeedInfo
2332+
var compInfosForType []install.ComponentSeedInfo
23332333
if len(snapForMode.Components) > 0 {
2334-
compInfosForType = make([]install.CompSeedInfo, 0, len(snapForMode.Components))
2334+
compInfosForType = make([]install.ComponentSeedInfo, 0, len(snapForMode.Components))
23352335
for _, sc := range snapForMode.Components {
23362336
seedComp := sc
23372337
compf, err := snapfile.Open(seedComp.Path)
@@ -2343,9 +2343,9 @@ func (m *DeviceManager) loadSystemAndEssentialSnaps(wantedSystemLabel string, ty
23432343
if err != nil {
23442344
return nil, err
23452345
}
2346-
compInfosForType = append(compInfosForType, install.CompSeedInfo{
2347-
CompInfo: compInfo,
2348-
CompSeed: &seedComp,
2346+
compInfosForType = append(compInfosForType, install.ComponentSeedInfo{
2347+
Info: compInfo,
2348+
Seed: &seedComp,
23492349
})
23502350
}
23512351
}

overlord/devicestate/handlers_install.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -947,18 +947,18 @@ func (m *DeviceManager) loadAndMountSystemLabelSnaps(systemLabel string, essenti
947947

948948
mntPtForComp := make(map[string]string)
949949
for _, seedComp := range systemAndSnaps.CompsByType[snap.TypeKernel] {
950-
if seedComp.CompInfo.Type != snap.KernelModulesComponent {
950+
if seedComp.Info.Type != snap.KernelModulesComponent {
951951
continue
952952
}
953953

954-
mntPt, unmountComp, err := mountSeedContainer(seedComp.CompSeed.Path,
955-
seedComp.CompInfo.FullName())
954+
mntPt, unmountComp, err := mountSeedContainer(seedComp.Seed.Path,
955+
seedComp.Info.FullName())
956956
if err != nil {
957957
unmount()
958958
return nil, nil, nil, nil, err
959959
}
960960
unmountFuncs = append(unmountFuncs, unmountComp)
961-
mntPtForComp[seedComp.CompInfo.FullName()] = mntPt
961+
mntPtForComp[seedComp.Info.FullName()] = mntPt
962962
}
963963

964964
return systemAndSnaps, mntPtForType, mntPtForComp, unmount, nil
@@ -968,7 +968,8 @@ func kBootInfo(systemAndSnaps *systemAndEssentialSnaps, kernMntPoint string, mnt
968968
kernInfo := systemAndSnaps.InfosByType[snap.TypeKernel]
969969
compSeedInfos := systemAndSnaps.CompsByType[snap.TypeKernel]
970970
return installLogic.BuildKernelBootInfo(kernInfo, compSeedInfos, kernMntPoint,
971-
mntPtForComps, isCore, kernel.NeedsKernelDriversTree(systemAndSnaps.Model))
971+
mntPtForComps, installLogic.BuildKernelBootInfoOpts{
972+
IsCore: isCore, NeedsDriversTree: kernel.NeedsKernelDriversTree(systemAndSnaps.Model)})
972973
}
973974

974975
// doInstallFinish performs the finish step of the install. It will

overlord/install/install.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ type EncryptionSupportInfo struct {
8282
UnavailableWarning string
8383
}
8484

85-
// CompSeedInfo contains information for a component from the seed and
85+
// ComponentSeedInfo contains information for a component from the seed and
8686
// from its metadata.
87-
type CompSeedInfo struct {
88-
CompInfo *snap.ComponentInfo
89-
CompSeed *seed.Component
87+
type ComponentSeedInfo struct {
88+
Info *snap.ComponentInfo
89+
Seed *seed.Component
9090
}
9191

9292
// KernelBootInfo contains information related to the kernel used on installation.
@@ -102,12 +102,20 @@ var (
102102
sysconfigConfigureTargetSystem = sysconfig.ConfigureTargetSystem
103103
)
104104

105+
// BuildKernelBootInfoOpts contains options for BuildKernelBootInfo.
106+
type BuildKernelBootInfoOpts struct {
107+
// IsCore is true for UC, and false for hybrid systems
108+
IsCore bool
109+
// NeedsDriversTree is true if we need a drivers tree (UC/hybrid 24+)
110+
NeedsDriversTree bool
111+
}
112+
105113
// BuildKernelBootInfo constructs a KernelBootInfo.
106-
func BuildKernelBootInfo(kernInfo *snap.Info, compSeedInfos []CompSeedInfo, kernMntPoint string, mntPtForComps map[string]string, isCore, needsDriversTree bool) KernelBootInfo {
114+
func BuildKernelBootInfo(kernInfo *snap.Info, compSeedInfos []ComponentSeedInfo, kernMntPoint string, mntPtForComps map[string]string, opts BuildKernelBootInfoOpts) KernelBootInfo {
107115
bootKMods := make([]boot.BootableKModsComponents, 0, len(compSeedInfos))
108116
modulesComps := make([]gadgetInstall.KernelModulesComponentInfo, 0, len(compSeedInfos))
109117
for _, compSeedInfo := range compSeedInfos {
110-
ci := compSeedInfo.CompInfo
118+
ci := compSeedInfo.Info
111119
if ci.Type == snap.KernelModulesComponent {
112120
cpi := snap.MinimalComponentContainerPlaceInfo(ci.Component.ComponentName,
113121
ci.Revision, kernInfo.SnapName())
@@ -118,7 +126,7 @@ func BuildKernelBootInfo(kernInfo *snap.Info, compSeedInfos []CompSeedInfo, kern
118126
})
119127
bootKMods = append(bootKMods, boot.BootableKModsComponents{
120128
CompPlaceInfo: cpi,
121-
CompPath: compSeedInfo.CompSeed.Path,
129+
CompPath: compSeedInfo.Seed.Path,
122130
})
123131
}
124132
}
@@ -127,9 +135,9 @@ func BuildKernelBootInfo(kernInfo *snap.Info, compSeedInfos []CompSeedInfo, kern
127135
Name: kernInfo.SnapName(),
128136
Revision: kernInfo.Revision,
129137
MountPoint: kernMntPoint,
130-
IsCore: isCore,
138+
IsCore: opts.IsCore,
131139
ModulesComps: modulesComps,
132-
NeedsDriversTree: needsDriversTree,
140+
NeedsDriversTree: opts.NeedsDriversTree,
133141
}
134142

135143
return KernelBootInfo{

0 commit comments

Comments
 (0)