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

Add sort-key during install based on the entry name #609

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 44 additions & 0 deletions pkg/uki/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,47 @@

return destinationFile.Close()
}

func AddSystemdConfSortKey(fs v1.FS, artifactDir string, log sdkTypes.KairosLogger) error {
return fsutils.WalkDirFs(fs, artifactDir, func(path string, info os.DirEntry, err error) error {
if err != nil {
return err
}
// Only do files that are conf files but dont match the loader.conf
if !info.IsDir() && filepath.Ext(path) == ".conf" && !strings.Contains(info.Name(), "loader.conf") {
log.Logger.Debug().Str("path", path).Msg("Adding sort key to file")
conf, err := sdkutils.SystemdBootConfReader(path)
if err != nil {
log.Errorf("Error reading conf file to extract values %s: %s", path)
}
// Now check and put the proper sort key
var sortKey string
// If we have 2 different files that start with active, like with the extra-cmdline, how do we set this?
// Ideally if they both have the same sort key, they will be sorted by name so the single one will be first
// and the extra-cmdline will be second. This is the best we can do currently without making this a mess
Copy link
Contributor

Choose a reason for hiding this comment

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

If the extra was meant to be the "default", it would not be set as extra cmdline. I think extra cmdlines were always meant to be alternative boot entries, not part of the "active", "passive", "recovery" dance.

Copy link
Contributor

Choose a reason for hiding this comment

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

Having said that, if "active" fails to boot, I don't think we would want to try "active_myops" next, would we? The next thing we would want to try would be "passive". Unless I'm missing some user flow.

Copy link
Member Author

Choose a reason for hiding this comment

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

Im not sure, maybe active and active_debug should be tried one after the other?

Thats why this has a big explanation, I guess we can add this then move into a more detailed thing or explain it better?

In any case, this is just the first implementation, usually this would not matter much (the order) as we select explicit entries, so this will come later down the line when people start implementing assessment and such?

// Maybe we need the bootentry command to also set the sort key somehow?
switch {
case strings.Contains(info.Name(), "active"):
sortKey = "0001"
case strings.Contains(info.Name(), "passive"):
sortKey = "0002"
case strings.Contains(info.Name(), "recovery"):
sortKey = "0003"
case strings.Contains(info.Name(), "statereset"):
sortKey = "0004"
default: // Anything that dont matches, goes to the bottom
sortKey = "0010"
}
conf["sort-key"] = sortKey
newContents := ""
for k, v := range conf {
newContents = fmt.Sprintf("%s%s %s\n", newContents, k, v)
}
log.Logger.Trace().Str("contents", litter.Sdump(conf)).Str("path", path).Msg("Final values for conf file")

return os.WriteFile(path, []byte(newContents), os.ModePerm)
Fixed Show fixed Hide fixed
}

return nil
})
}
6 changes: 6 additions & 0 deletions pkg/uki/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ func (i *InstallAction) Run() (err error) {
return fmt.Errorf("removing artifact set with role %s: %w", UnassignedArtifactRole, err)
}

// add sort key to all files
err = AddSystemdConfSortKey(i.cfg.Fs, i.spec.Partitions.EFI.MountPoint, i.cfg.Logger)
if err != nil {
i.cfg.Logger.Warnf("adding sort key: %s", err.Error())
}

// Add boot assessment to files by appending +3 to the name
err = utils.AddBootAssessment(i.cfg.Fs, i.spec.Partitions.EFI.MountPoint, i.cfg.Logger)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/uki/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func (r *ResetAction) Run() (err error) {
return fmt.Errorf("copying recovery to active: %w", err)
}

// add sort key to all files
err = AddSystemdConfSortKey(r.cfg.Fs, r.spec.Partitions.EFI.MountPoint, r.cfg.Logger)
if err != nil {
r.cfg.Logger.Warnf("adding sort key: %s", err.Error())
}

// Add boot assessment to files by appending +3 to the name
err = elementalUtils.AddBootAssessment(r.cfg.Fs, r.spec.Partitions.EFI.MountPoint, r.cfg.Logger)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/uki/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ func (i *UpgradeAction) Run() (err error) {
i.cfg.Logger.Errorf("removing artifact set: %s", err.Error())
return fmt.Errorf("removing artifact set: %w", err)
}

// add sort key to all files
err = AddSystemdConfSortKey(i.cfg.Fs, i.spec.EfiPartition.MountPoint, i.cfg.Logger)
if err != nil {
i.cfg.Logger.Warnf("adding sort key: %s", err.Error())
}

// Add boot assessment to files by appending +3 to the name
err = elementalUtils.AddBootAssessment(i.cfg.Fs, i.spec.EfiPartition.MountPoint, i.cfg.Logger)
if err != nil {
Expand Down
Loading