2023-02-03 10:04:02 +00:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-07-24 10:28:59 +00:00
|
|
|
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
|
2023-02-03 10:04:02 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-07-10 12:39:48 +00:00
|
|
|
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
|
2023-07-24 10:28:59 +00:00
|
|
|
"github.com/kairos-io/kairos-sdk/machine"
|
2023-02-03 10:04:02 +00:00
|
|
|
"github.com/mudler/yip/pkg/schema"
|
|
|
|
yip "github.com/mudler/yip/pkg/schema"
|
2024-03-20 15:53:42 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-02-03 10:04:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CustomMounts struct{}
|
|
|
|
|
|
|
|
func saveCloudConfig(name config.Stage, yc yip.YipConfig) error {
|
|
|
|
yipYAML, err := yaml.Marshal(yc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.WriteFile(filepath.Join("/oem", fmt.Sprintf("10_%s.yaml", name)), yipYAML, 0400)
|
|
|
|
}
|
|
|
|
|
2025-03-24 15:05:39 +00:00
|
|
|
// Run Read the keys sections ephemeral_mounts and bind mounts from install key in the cloud config.
|
2023-02-03 10:04:02 +00:00
|
|
|
// If not empty write an environment file to /run/cos/custom-layout.env.
|
|
|
|
// That env file is in turn read by /overlay/files/system/oem/11_persistency.yaml in fs.after stage.
|
2023-07-24 10:28:59 +00:00
|
|
|
func (cm CustomMounts) Run(c config.Config, _ v1.Spec) error {
|
2023-02-03 10:04:02 +00:00
|
|
|
if len(c.Install.BindMounts) == 0 && len(c.Install.EphemeralMounts) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-07 14:11:51 +00:00
|
|
|
c.Logger.Logger.Debug().Msg("Running CustomMounts hook")
|
2023-02-03 10:04:02 +00:00
|
|
|
|
2025-03-24 15:05:39 +00:00
|
|
|
err := machine.Mount("COS_OEM", "/oem")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-03 10:04:02 +00:00
|
|
|
defer func() {
|
2025-03-24 15:05:39 +00:00
|
|
|
_ = machine.Umount("/oem")
|
2023-02-03 10:04:02 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
var mountsList = map[string]string{}
|
|
|
|
|
|
|
|
mountsList["CUSTOM_BIND_MOUNTS"] = strings.Join(c.Install.BindMounts, " ")
|
|
|
|
mountsList["CUSTOM_EPHEMERAL_MOUNTS"] = strings.Join(c.Install.EphemeralMounts, " ")
|
|
|
|
|
2025-03-24 15:05:39 +00:00
|
|
|
cfg := yip.YipConfig{
|
|
|
|
Stages: map[string][]schema.Stage{
|
|
|
|
"rootfs": {
|
|
|
|
{
|
|
|
|
Name: "user_custom_mounts",
|
|
|
|
EnvironmentFile: "/run/cos/custom-layout.env",
|
|
|
|
Environment: mountsList,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-02-03 10:04:02 +00:00
|
|
|
|
2025-03-24 15:05:39 +00:00
|
|
|
err = saveCloudConfig("user_custom_mounts", cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-07 14:11:51 +00:00
|
|
|
c.Logger.Logger.Debug().Msg("Finish CustomMounts hook")
|
2023-02-03 10:04:02 +00:00
|
|
|
return nil
|
|
|
|
}
|