2022-09-08 15:39:26 +02:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
2025-04-15 13:03:18 +02:00
|
|
|
"github.com/kairos-io/kairos-agent/v2/pkg/config"
|
|
|
|
cnst "github.com/kairos-io/kairos-agent/v2/pkg/constants"
|
2023-07-24 12:28:59 +02:00
|
|
|
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
|
2025-04-15 13:03:18 +02:00
|
|
|
"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"
|
2022-09-08 15:39:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type GrubOptions struct{}
|
|
|
|
|
2023-07-24 12:28:59 +02:00
|
|
|
func (b GrubOptions) Run(c config.Config, _ v1.Spec) error {
|
2024-03-01 12:27:26 +01:00
|
|
|
if len(c.Install.GrubOptions) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-07 15:11:51 +01:00
|
|
|
c.Logger.Logger.Debug().Msg("Running GrubOptions hook")
|
2023-09-26 09:38:58 +02:00
|
|
|
c.Logger.Debugf("Setting grub options: %s", c.Install.GrubOptions)
|
2025-04-15 13:03:18 +02:00
|
|
|
err := grubOptions(c, c.Install.GrubOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-08 15:39:26 +02:00
|
|
|
}
|
2024-03-07 15:11:51 +01:00
|
|
|
c.Logger.Logger.Debug().Msg("Finish GrubOptions hook")
|
2022-09-08 15:39:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-24 08:34:49 +02:00
|
|
|
|
|
|
|
type GrubPostInstallOptions struct{}
|
|
|
|
|
2023-07-24 12:28:59 +02:00
|
|
|
func (b GrubPostInstallOptions) Run(c config.Config, _ v1.Spec) error {
|
2024-03-07 15:11:51 +01:00
|
|
|
if len(c.GrubOptions) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.Logger.Logger.Debug().Msg("Running GrubOptions hook")
|
2025-04-15 13:03:18 +02:00
|
|
|
c.Logger.Debugf("Setting grub options: %s", c.GrubOptions)
|
|
|
|
err := grubOptions(c, c.GrubOptions)
|
2022-10-24 08:34:49 +02:00
|
|
|
if err != nil {
|
2025-04-15 13:03:18 +02:00
|
|
|
return err
|
2022-10-24 08:34:49 +02:00
|
|
|
}
|
2025-04-15 13:03:18 +02:00
|
|
|
c.Logger.Logger.Debug().Msg("Finish GrubOptions hook")
|
2022-10-24 08:34:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
2025-04-15 13:03:18 +02:00
|
|
|
|
|
|
|
// 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.Fs)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger.Logger.Error().Err(err).Msg("Failed to set grub options")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|