Skip to content

Commit

Permalink
Merge boot assesment into one
Browse files Browse the repository at this point in the history
Make it simpler by making it work for both install and upgrade by doing
the proper thing:
check if conf file have it enabled, if not enable it for the conf file

This should work with the install as well

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka committed Nov 21, 2024
1 parent af4b942 commit d7454bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
20 changes: 0 additions & 20 deletions pkg/uki/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,6 @@ func removeArtifactSetWithRole(fs v1.FS, artifactDir, role string) error {
})
}

func addBootAssessment(fs v1.FS, artifactDir string, logger sdkTypes.KairosLogger) error {
return fsutils.WalkDirFs(fs, artifactDir, func(path string, info os.DirEntry, err error) error {
if !info.IsDir() && filepath.Ext(path) == ".conf" && !strings.Contains(info.Name(), "loader.conf") {
dir := filepath.Dir(path)
ext := filepath.Ext(path)
base := strings.TrimSuffix(filepath.Base(path), ext)
newBase := fmt.Sprintf("%s+3%s", base, ext)
newPath := filepath.Join(dir, newBase)
logger.Logger.Debug().Str("from", path).Str("to", newPath).Msg("Enabling boot assessment")
err = os.Rename(path, newPath)
if err != nil {
logger.Logger.Err(err).Str("from", path).Str("to", newPath).Msg("Error renaming file")
return err
}
}

return nil
})
}

func copyArtifactSetRole(fs v1.FS, artifactDir, oldRole, newRole string, logger sdkTypes.KairosLogger) error {
return fsutils.WalkDirFs(fs, artifactDir, func(path string, info os.DirEntry, err error) error {
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions pkg/uki/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package uki

import (
"fmt"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
sdkTypes "github.com/kairos-io/kairos-sdk/types"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/kairos-io/kairos-agent/v2/pkg/action"
"github.com/kairos-io/kairos-agent/v2/pkg/config"
Expand Down Expand Up @@ -99,6 +103,10 @@ func (i *UpgradeAction) Run() (err error) {
return fmt.Errorf("installing the new artifacts as active: %w", err)
}

err = addBootAssessment(i.cfg.Fs, constants.UkiEfiDir, i.cfg.Logger)
if err != nil {
return err
}
loaderConfPath := filepath.Join(constants.UkiEfiDir, "loader", "loader.conf")
if err = replaceRoleInKey(loaderConfPath, "default", UnassignedArtifactRole, "active", i.cfg.Logger); err != nil {
i.cfg.Logger.Errorf("replacing role in key: %s", err.Error())
Expand Down Expand Up @@ -129,6 +137,39 @@ func (i *UpgradeAction) Run() (err error) {
return nil
}

// addBootAssessment adds boot assessment to files by appending +3 to the name
// Only for files that dont have it already as those are the ones upgraded
// Existing files that have a boot assessment will be left as is
// This should be called during install, upgrade and reset
// Mainly everything that updates teh config file to point to a new artifact we need to reset the boot assessment
// as its a new artifact that needs to be assessed
func addBootAssessment(fs v1.FS, artifactDir string, logger sdkTypes.KairosLogger) error {
return fsutils.WalkDirFs(fs, artifactDir, func(path string, info os.DirEntry, err error) error {
// 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") {
dir := filepath.Dir(path)
ext := filepath.Ext(path)
base := strings.TrimSuffix(filepath.Base(path), ext)
// Lets check if the file has a boot assessment already. If it does, we dont need to do anything
// If it matches continue
re := regexp.MustCompile(`\+\d+(-\d+)?$`)
if re.MatchString(base) {
return nil
}
newBase := fmt.Sprintf("%s+3%s", base, ext)
newPath := filepath.Join(dir, newBase)
logger.Logger.Debug().Str("from", path).Str("to", newPath).Msg("Enabling boot assessment")
err = os.Rename(path, newPath)
if err != nil {
logger.Logger.Err(err).Str("from", path).Str("to", newPath).Msg("Error renaming file")
return err
}
}

return nil
})
}

func (i *UpgradeAction) installEntry(entry string) error {
targetEntryFile := filepath.Join(constants.UkiEfiDir, "EFI", "kairos", fmt.Sprintf("%s.efi", entry))
if _, err := os.Stat(targetEntryFile); err != nil {
Expand Down

0 comments on commit d7454bc

Please sign in to comment.