2022-10-07 11:36:32 +00:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-01-18 13:14:57 +00:00
|
|
|
"strings"
|
2022-10-07 11:36:32 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
config "github.com/kairos-io/kairos/pkg/config"
|
2022-10-18 05:45:07 +00:00
|
|
|
"github.com/kairos-io/kairos/pkg/machine"
|
2022-10-07 11:36:32 +00:00
|
|
|
"github.com/kairos-io/kairos/pkg/utils"
|
2022-11-16 11:35:59 +00:00
|
|
|
|
2023-01-18 13:14:57 +00:00
|
|
|
kcryptconfig "github.com/kairos-io/kcrypt/pkg/config"
|
2022-10-07 11:36:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Kcrypt struct{}
|
|
|
|
|
|
|
|
func (k Kcrypt) Run(c config.Config) error {
|
2022-10-18 05:45:07 +00:00
|
|
|
|
|
|
|
if len(c.Install.Encrypt) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
machine.Mount("COS_OEM", "/oem") //nolint:errcheck
|
|
|
|
defer func() {
|
2022-10-23 18:22:32 +00:00
|
|
|
machine.Umount("/oem") //nolint:errcheck
|
2022-10-18 05:45:07 +00:00
|
|
|
}()
|
|
|
|
|
2023-01-18 13:14:57 +00:00
|
|
|
kcryptc, err := kcryptconfig.GetConfiguration(kcryptconfig.ConfigScanDirs)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed getting kcrypt configuration: ", err.Error())
|
|
|
|
if c.FailOnBundleErrors {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 11:36:32 +00:00
|
|
|
for _, p := range c.Install.Encrypt {
|
|
|
|
out, err := utils.SH(fmt.Sprintf("kcrypt encrypt %s", p))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not encrypt partition: %s\n", out+err.Error())
|
|
|
|
if c.FailOnBundleErrors {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Give time to show the error
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
return nil // do not error out
|
|
|
|
}
|
2022-11-16 11:35:59 +00:00
|
|
|
|
2023-01-18 13:14:57 +00:00
|
|
|
err = kcryptc.SetMapping(strings.TrimSpace(out))
|
2022-11-16 11:35:59 +00:00
|
|
|
if err != nil {
|
2023-01-18 13:14:57 +00:00
|
|
|
fmt.Println("Failed updating the kcrypt configuration file: ", err.Error())
|
2022-11-16 11:35:59 +00:00
|
|
|
if c.FailOnBundleErrors {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-01-18 13:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = kcryptc.WriteMappings(kcryptconfig.MappingsFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed writing kcrypt partition mappings: ", err.Error())
|
|
|
|
if c.FailOnBundleErrors {
|
|
|
|
return err
|
2022-11-16 11:35:59 +00:00
|
|
|
}
|
2022-10-07 11:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|