2022-10-02 22:16:01 +00:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
2023-11-20 11:54:05 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
|
2023-07-24 10:28:59 +00:00
|
|
|
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
|
|
|
|
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
|
2023-03-15 14:45:00 +00:00
|
|
|
"github.com/kairos-io/kairos-sdk/bundles"
|
2023-03-18 09:27:18 +00:00
|
|
|
"github.com/kairos-io/kairos-sdk/machine"
|
2022-10-02 22:16:01 +00:00
|
|
|
)
|
|
|
|
|
2023-09-15 13:40:27 +00:00
|
|
|
// BundlePostInstall install bundles just after installation
|
|
|
|
type BundlePostInstall struct{}
|
2022-10-02 22:16:01 +00:00
|
|
|
|
2023-09-15 13:40:27 +00:00
|
|
|
func (b BundlePostInstall) Run(c config.Config, _ v1.Spec) error {
|
|
|
|
// system extension are now installed to /var/lib/extensions
|
|
|
|
// https://github.com/kairos-io/kairos/issues/1821
|
|
|
|
// so if we want them to work as expected we need to mount the persistent dir and the bind dir that will
|
|
|
|
// then end up in the system as /usr/lib/extensions is not valid anymore
|
|
|
|
// So after the install from livecd we need to:
|
|
|
|
// - mount persistent in /usr/local
|
|
|
|
// - create the dir that will be binded into /var/lib/extensions after reboot in /usr/local/.state/var-lib-extensions.bind
|
|
|
|
// - rsync whatever is in the /var/lib/extensions to /usr/local/.state/var-lib-extensions.bind so we dont lost thing shipped with the install media
|
|
|
|
// - bind the dir so the extension gets persistent after reboots
|
|
|
|
// - umount the bind dir
|
|
|
|
// Note that the binding of /usr/local/.state/var-lib-extensions.bind to /var/lib/extensions on active/passive its done by inmmucore based on the
|
|
|
|
// 00_rootfs.yaml config which sets the bind and ephemeral paths.
|
2024-03-07 14:11:51 +00:00
|
|
|
c.Logger.Logger.Debug().Msg("Running BundlePostInstall hook")
|
2022-10-02 22:16:01 +00:00
|
|
|
|
|
|
|
machine.Mount("COS_PERSISTENT", "/usr/local") //nolint:errcheck
|
|
|
|
defer func() {
|
2022-10-23 18:22:32 +00:00
|
|
|
machine.Umount("/usr/local") //nolint:errcheck
|
2022-10-02 22:16:01 +00:00
|
|
|
}()
|
|
|
|
|
2023-09-15 13:40:27 +00:00
|
|
|
err := os.MkdirAll("/usr/local/.state/var-lib-extensions.bind", os.ModeDir|os.ModePerm)
|
|
|
|
if c.FailOnBundleErrors && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-20 11:54:05 +00:00
|
|
|
|
2025-01-14 09:59:45 +00:00
|
|
|
cmd := exec.Command("rsync", "-aqAX", "/var/lib/extensions/", "/usr/local/.state/var-lib-extensions.bind")
|
2023-09-15 13:40:27 +00:00
|
|
|
_, err = cmd.CombinedOutput()
|
|
|
|
if c.FailOnBundleErrors && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = syscall.Mount("/usr/local/.state/var-lib-extensions.bind", "/var/lib/extensions", "", syscall.MS_BIND, "")
|
|
|
|
if c.FailOnBundleErrors && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = syscall.Unmount("/var/lib/extensions", 0)
|
|
|
|
}()
|
|
|
|
|
2022-10-02 22:16:01 +00:00
|
|
|
machine.Mount("COS_OEM", "/oem") //nolint:errcheck
|
|
|
|
defer func() {
|
2022-10-23 18:22:32 +00:00
|
|
|
machine.Umount("/oem") //nolint:errcheck
|
2022-10-02 22:16:01 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
opts := c.Install.Bundles.Options()
|
2023-09-15 13:40:27 +00:00
|
|
|
err = bundles.RunBundles(opts...)
|
2022-10-02 22:16:01 +00:00
|
|
|
if c.FailOnBundleErrors && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-07 14:11:51 +00:00
|
|
|
c.Logger.Logger.Debug().Msg("Finish BundlePostInstall hook")
|
2022-10-02 22:16:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-24 06:34:49 +00:00
|
|
|
|
2023-09-15 13:40:27 +00:00
|
|
|
// BundleFirstBoot installs bundles during the first boot of the machine
|
|
|
|
type BundleFirstBoot struct{}
|
2022-10-24 06:34:49 +00:00
|
|
|
|
2023-09-15 13:40:27 +00:00
|
|
|
func (b BundleFirstBoot) Run(c config.Config, _ v1.Spec) error {
|
2024-03-07 14:11:51 +00:00
|
|
|
c.Logger.Logger.Debug().Msg("Running BundleFirstBoot hook")
|
2022-10-24 06:34:49 +00:00
|
|
|
opts := c.Bundles.Options()
|
|
|
|
err := bundles.RunBundles(opts...)
|
|
|
|
if c.FailOnBundleErrors && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-07 14:11:51 +00:00
|
|
|
c.Logger.Logger.Debug().Msg("Finish BundleFirstBoot hook")
|
2022-10-24 06:34:49 +00:00
|
|
|
return nil
|
|
|
|
}
|