2022-09-08 15:39:26 +02:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
2025-04-25 10:43:21 +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-25 10:43:21 +02:00
|
|
|
"github.com/kairos-io/kairos-agent/v2/pkg/utils"
|
|
|
|
"github.com/kairos-io/kairos-sdk/machine"
|
|
|
|
"path/filepath"
|
2022-09-08 15:39:26 +02:00
|
|
|
)
|
|
|
|
|
2025-06-04 09:25:11 +02:00
|
|
|
// GrubPostInstallOptions is a hook that runs after the install process to add grub options.
|
|
|
|
type GrubPostInstallOptions struct{}
|
2022-09-08 15:39:26 +02:00
|
|
|
|
2025-06-04 09:25:11 +02:00
|
|
|
func (b GrubPostInstallOptions) Run(c config.Config, _ v1.Spec) error {
|
2024-03-01 12:27:26 +01:00
|
|
|
if len(c.Install.GrubOptions) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2025-06-04 19:14:54 +02:00
|
|
|
c.Logger.Logger.Info().Msg("Running GrubOptions hook")
|
2023-09-26 09:38:58 +02:00
|
|
|
c.Logger.Debugf("Setting grub options: %s", c.Install.GrubOptions)
|
2025-04-25 10:43:21 +02:00
|
|
|
err := grubOptions(c, c.Install.GrubOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-08 15:39:26 +02:00
|
|
|
}
|
2025-06-04 19:14:54 +02:00
|
|
|
c.Logger.Logger.Info().Msg("Finish GrubOptions hook")
|
2022-09-08 15:39:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-24 08:34:49 +02:00
|
|
|
|
2025-06-04 09:25:11 +02:00
|
|
|
// GrubFirstBootOptions is a hook that runs on the first boot to add grub options.
|
|
|
|
type GrubFirstBootOptions struct{}
|
2022-10-24 08:34:49 +02:00
|
|
|
|
2025-06-04 09:25:11 +02:00
|
|
|
func (b GrubFirstBootOptions) Run(c config.Config, _ v1.Spec) error {
|
2024-03-07 15:11:51 +01:00
|
|
|
if len(c.GrubOptions) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2025-06-04 19:14:54 +02:00
|
|
|
c.Logger.Logger.Info().Msg("Running GrubOptions hook")
|
2025-04-25 10:43:21 +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-25 10:43:21 +02:00
|
|
|
return err
|
2022-10-24 08:34:49 +02:00
|
|
|
}
|
2025-06-04 19:14:54 +02:00
|
|
|
c.Logger.Logger.Info().Msg("Finish GrubOptions hook")
|
2022-10-24 08:34:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
2025-04-25 10:43:21 +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 {
|
2025-06-05 16:28:19 +02:00
|
|
|
_ = machine.Umount(cnst.OEMDir)
|
|
|
|
_ = machine.Umount(cnst.OEMPath)
|
|
|
|
|
|
|
|
c.Logger.Logger.Debug().Msg("Mounting OEM partition")
|
|
|
|
_ = machine.Mount(cnst.OEMLabel, cnst.OEMPath)
|
|
|
|
defer func() {
|
|
|
|
c.Logger.Logger.Debug().Msg("Unmounting OEM partition")
|
|
|
|
_ = machine.Umount(cnst.OEMPath)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := utils.SetPersistentVariables(filepath.Join(cnst.OEMPath, "grubenv"), opts, &c)
|
2025-04-25 10:43:21 +02:00
|
|
|
if err != nil {
|
2025-06-05 09:29:24 +02:00
|
|
|
c.Logger.Logger.Error().Err(err).Str("grubfile", filepath.Join(cnst.OEMPath, "grubenv")).Msg("Failed to set grub options")
|
2025-04-25 10:43:21 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|