Remove entries on install (#213)

This commit is contained in:
Itxaka
2024-01-26 17:41:23 +01:00
committed by GitHub
parent 8696eb16d2
commit f6f113128d
5 changed files with 93 additions and 2 deletions

View File

@@ -1,6 +1,10 @@
package uki
import (
"os"
"path/filepath"
"strings"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
@@ -9,8 +13,7 @@ import (
"github.com/kairos-io/kairos-agent/v2/pkg/utils"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
events "github.com/kairos-io/kairos-sdk/bus"
"os"
"path/filepath"
"github.com/sanity-io/litter"
)
type InstallAction struct {
@@ -120,6 +123,58 @@ func (i *InstallAction) Run() (err error) {
return err
}
// Remove entries
// Read all confs
err = fsutils.WalkDirFs(i.cfg.Fs, filepath.Join(i.spec.Partitions.EFI.MountPoint, "loader/entries/"), func(path string, info os.DirEntry, err error) error {
i.cfg.Logger.Debugf("Checking file %s", path)
if info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) != ".conf" {
return nil
}
// Extract the values
conf, err := utils.SystemdBootConfReader(path)
if err != nil {
i.cfg.Logger.Errorf("Error reading conf file %s: %s", path, err)
return err
}
i.cfg.Logger.Debugf("Conf file %s has values %v", path, litter.Sdump(conf))
if len(conf["cmdline"]) == 0 {
return nil
}
// Check if the cmdline matches any of the entries in the skip list
for _, entry := range i.spec.SkipEntries {
// Match the cmdline key against the entry
if strings.Contains(conf["cmdline"], entry) {
i.cfg.Logger.Debugf("Found match for %s in %s", entry, path)
// If match, get the efi file and remove it
if conf["efi"] != "" {
i.cfg.Logger.Debugf("Removing efi file %s", conf["efi"])
// First remove the efi file
err = i.cfg.Fs.Remove(filepath.Join(i.spec.Partitions.EFI.MountPoint, conf["efi"]))
if err != nil {
i.cfg.Logger.Errorf("Error removing efi file %s: %s", conf["efi"], err)
return err
}
// Then remove the conf file
i.cfg.Logger.Debugf("Removing conf file %s", path)
err = i.cfg.Fs.Remove(path)
if err != nil {
i.cfg.Logger.Errorf("Error removing conf file %s: %s", path, err)
return err
}
// Do not continue checking the conf file, we already done all we needed
}
}
}
return err
})
if err != nil {
return err
}
// after install hook happens after install (this is for compatibility with normal install, so users can reuse their configs)
err = Hook(i.cfg, constants.AfterInstallHook)
if err != nil {