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

@@ -571,6 +571,8 @@ func NewUkiInstallSpec(cfg *Config) (*v1.InstallUkiSpec, error) {
// TODO: Which key to use? install or install-uki? // TODO: Which key to use? install or install-uki?
err := unmarshallFullSpec(cfg, "install", spec) 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 // 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 return spec, err
} }

View File

@@ -114,6 +114,10 @@ const (
UkiMaxEntries = 3 UkiMaxEntries = 3
) )
func UkiDefaultSkipEntries() []string {
return []string{"interactive-install", "manual-install"}
}
func GetCloudInitPaths() []string { func GetCloudInitPaths() []string {
return []string{"/system/oem", "/oem/", "/usr/local/cloud-config/"} return []string{"/system/oem", "/oem/", "/usr/local/cloud-config/"}
} }

View File

@@ -510,6 +510,7 @@ type InstallUkiSpec struct {
Partitions ElementalPartitions `yaml:"partitions,omitempty" mapstructure:"partitions"` Partitions ElementalPartitions `yaml:"partitions,omitempty" mapstructure:"partitions"`
ExtraPartitions PartitionList `yaml:"extra-partitions,omitempty" mapstructure:"extra-partitions"` ExtraPartitions PartitionList `yaml:"extra-partitions,omitempty" mapstructure:"extra-partitions"`
CloudInit []string `yaml:"cloud-init,omitempty" mapstructure:"cloud-init"` CloudInit []string `yaml:"cloud-init,omitempty" mapstructure:"cloud-init"`
SkipEntries []string `yaml:"skip-entries,omitempty" mapstructure:"skip-entries"`
} }
func (i *InstallUkiSpec) Sanitize() error { func (i *InstallUkiSpec) Sanitize() error {

View File

@@ -1,6 +1,10 @@
package uki package uki
import ( import (
"os"
"path/filepath"
"strings"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks" 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/config"
"github.com/kairos-io/kairos-agent/v2/pkg/constants" "github.com/kairos-io/kairos-agent/v2/pkg/constants"
@@ -9,8 +13,7 @@ import (
"github.com/kairos-io/kairos-agent/v2/pkg/utils" "github.com/kairos-io/kairos-agent/v2/pkg/utils"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs" fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
events "github.com/kairos-io/kairos-sdk/bus" events "github.com/kairos-io/kairos-sdk/bus"
"os" "github.com/sanity-io/litter"
"path/filepath"
) )
type InstallAction struct { type InstallAction struct {
@@ -120,6 +123,58 @@ func (i *InstallAction) Run() (err error) {
return err 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) // 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) err = Hook(i.cfg, constants.AfterInstallHook)
if err != nil { if err != nil {

View File

@@ -17,6 +17,7 @@ limitations under the License.
package utils package utils
import ( import (
"bufio"
"crypto/sha256" "crypto/sha256"
"errors" "errors"
"fmt" "fmt"
@@ -527,3 +528,31 @@ func UkiBootMode() state.Boot {
} }
return state.Unknown 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
}