Skip to content

Commit

Permalink
Add sort-key during install based on the entry name (#609)
Browse files Browse the repository at this point in the history
* Add sort-key during install based on the entry name

Signed-off-by: Itxaka <[email protected]>

* Fix logger output

Signed-off-by: Itxaka <[email protected]>

---------

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka authored Nov 27, 2024
1 parent 7be897c commit 07480ab
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
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 @@ func copyFile(src, dst string) error {

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", conf, 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
// 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), 0600)
}

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

0 comments on commit 07480ab

Please sign in to comment.