diff --git a/pkg/uki/common.go b/pkg/uki/common.go index 206079d7..8c75aae4 100644 --- a/pkg/uki/common.go +++ b/pkg/uki/common.go @@ -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 + }) +} diff --git a/pkg/uki/install.go b/pkg/uki/install.go index 07ac6649..e6d7b234 100644 --- a/pkg/uki/install.go +++ b/pkg/uki/install.go @@ -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 { diff --git a/pkg/uki/reset.go b/pkg/uki/reset.go index 244ef3dc..ed40fed1 100644 --- a/pkg/uki/reset.go +++ b/pkg/uki/reset.go @@ -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 { diff --git a/pkg/uki/upgrade.go b/pkg/uki/upgrade.go index 9bc3f0d4..f9ad79e4 100644 --- a/pkg/uki/upgrade.go +++ b/pkg/uki/upgrade.go @@ -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 {