mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-09-02 09:36:19 +00:00
We were basically overwriting the file if it existed which is bad. Now it will read the file if it exists and ingest the existing ones, then in will override witht he given vars if they match, warn the user and then store the full processed vars. This means it will never overwrite any vars again Signed-off-by: Itxaka <itxaka@kairos.io>
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package hook
|
|
|
|
import (
|
|
"github.com/kairos-io/kairos-agent/v2/pkg/config"
|
|
cnst "github.com/kairos-io/kairos-agent/v2/pkg/constants"
|
|
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
|
|
"github.com/kairos-io/kairos-agent/v2/pkg/utils"
|
|
"github.com/kairos-io/kairos-sdk/mounts"
|
|
"github.com/kairos-io/kairos-sdk/state"
|
|
"path/filepath"
|
|
)
|
|
|
|
type GrubOptions struct{}
|
|
|
|
func (b GrubOptions) Run(c config.Config, _ v1.Spec) error {
|
|
if len(c.Install.GrubOptions) == 0 {
|
|
return nil
|
|
}
|
|
c.Logger.Logger.Debug().Msg("Running GrubOptions hook")
|
|
c.Logger.Debugf("Setting grub options: %s", c.Install.GrubOptions)
|
|
err := grubOptions(c, c.Install.GrubOptions)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.Logger.Logger.Debug().Msg("Finish GrubOptions hook")
|
|
return nil
|
|
}
|
|
|
|
type GrubPostInstallOptions struct{}
|
|
|
|
func (b GrubPostInstallOptions) Run(c config.Config, _ v1.Spec) error {
|
|
if len(c.GrubOptions) == 0 {
|
|
return nil
|
|
}
|
|
c.Logger.Logger.Debug().Msg("Running GrubOptions hook")
|
|
c.Logger.Debugf("Setting grub options: %s", c.GrubOptions)
|
|
err := grubOptions(c, c.GrubOptions)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.Logger.Logger.Debug().Msg("Finish GrubOptions hook")
|
|
return nil
|
|
}
|
|
|
|
// grubOptions sets the grub options in the grubenv file
|
|
// It mounts the OEM partition if not already mounted
|
|
// If its mounted but RO, it remounts it as RW
|
|
func grubOptions(c config.Config, opts map[string]string) error {
|
|
runtime, err := state.NewRuntime()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mounts.Umount(state.PartitionState{Mounted: true, MountPoint: runtime.OEM.MountPoint})
|
|
if !runtime.OEM.Mounted {
|
|
if err := mounts.PrepareWrite(runtime.OEM, cnst.OEMPath); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
err = utils.SetPersistentVariables(filepath.Join(runtime.OEM.MountPoint, "grubenv"), opts, &c)
|
|
if err != nil {
|
|
c.Logger.Logger.Error().Err(err).Msg("Failed to set grub options")
|
|
}
|
|
return err
|
|
}
|