mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-09-02 09:36:19 +00:00
Remove entries on install (#213)
This commit is contained in:
@@ -571,6 +571,8 @@ func NewUkiInstallSpec(cfg *Config) (*v1.InstallUkiSpec, error) {
|
||||
// TODO: Which key to use? install or install-uki?
|
||||
err := unmarshallFullSpec(cfg, "install", spec)
|
||||
// TODO: Get the actual source size to calculate the image size and partitions size for at least 3 UKI images
|
||||
// Add default values for the skip partitions for our default entries
|
||||
spec.SkipEntries = append(spec.SkipEntries, constants.UkiDefaultSkipEntries()...)
|
||||
return spec, err
|
||||
}
|
||||
|
||||
|
@@ -114,6 +114,10 @@ const (
|
||||
UkiMaxEntries = 3
|
||||
)
|
||||
|
||||
func UkiDefaultSkipEntries() []string {
|
||||
return []string{"interactive-install", "manual-install"}
|
||||
}
|
||||
|
||||
func GetCloudInitPaths() []string {
|
||||
return []string{"/system/oem", "/oem/", "/usr/local/cloud-config/"}
|
||||
}
|
||||
|
@@ -510,6 +510,7 @@ type InstallUkiSpec struct {
|
||||
Partitions ElementalPartitions `yaml:"partitions,omitempty" mapstructure:"partitions"`
|
||||
ExtraPartitions PartitionList `yaml:"extra-partitions,omitempty" mapstructure:"extra-partitions"`
|
||||
CloudInit []string `yaml:"cloud-init,omitempty" mapstructure:"cloud-init"`
|
||||
SkipEntries []string `yaml:"skip-entries,omitempty" mapstructure:"skip-entries"`
|
||||
}
|
||||
|
||||
func (i *InstallUkiSpec) Sanitize() error {
|
||||
|
@@ -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 {
|
||||
|
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -527,3 +528,31 @@ func UkiBootMode() state.Boot {
|
||||
}
|
||||
return state.Unknown
|
||||
}
|
||||
|
||||
// SystemdBootConfReader reads a systemd-boot conf file and returns a map with the key/value pairs
|
||||
func SystemdBootConfReader(filePath string) (map[string]string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
result := make(map[string]string)
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
parts := strings.SplitN(line, " ", 2)
|
||||
if len(parts) == 2 {
|
||||
result[parts[0]] = parts[1]
|
||||
}
|
||||
if len(parts) == 1 {
|
||||
result[parts[0]] = ""
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user