2024-03-01 11:27:26 +00:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
2024-03-07 14:11:51 +00:00
|
|
|
"fmt"
|
2024-03-01 11:27:26 +00:00
|
|
|
"github.com/kairos-io/kairos-agent/v2/pkg/config"
|
|
|
|
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
|
|
|
|
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
|
|
|
|
internalutils "github.com/kairos-io/kairos-agent/v2/pkg/utils"
|
|
|
|
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
|
|
|
|
"github.com/kairos-io/kairos-sdk/machine"
|
2024-03-07 14:11:51 +00:00
|
|
|
"github.com/kairos-io/kairos-sdk/utils"
|
|
|
|
kcrypt "github.com/kairos-io/kcrypt/pkg/lib"
|
2024-03-01 11:27:26 +00:00
|
|
|
"path/filepath"
|
2024-03-07 14:11:51 +00:00
|
|
|
"strings"
|
2024-03-01 11:27:26 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2025-03-13 10:22:26 +00:00
|
|
|
// CopyLogs copies all current logs to the persistent partition.
|
2024-03-01 11:27:26 +00:00
|
|
|
// useful during install to keep the livecd logs
|
|
|
|
// best effort, no error handling
|
|
|
|
type CopyLogs struct{}
|
|
|
|
|
|
|
|
func (k CopyLogs) Run(c config.Config, _ v1.Spec) error {
|
2024-03-07 14:11:51 +00:00
|
|
|
c.Logger.Logger.Debug().Msg("Running CopyLogs hook")
|
2024-03-01 11:27:26 +00:00
|
|
|
_ = machine.Umount(constants.PersistentDir)
|
|
|
|
|
2025-03-13 10:22:26 +00:00
|
|
|
// Config passed during install ends up here, kcrypt challenger needs to read it if we are using a server for encryption
|
|
|
|
_ = machine.Mount(constants.OEMLabel, constants.OEMPath)
|
|
|
|
defer func() {
|
|
|
|
_ = machine.Umount(constants.OEMPath)
|
|
|
|
}()
|
|
|
|
|
2024-03-07 14:11:51 +00:00
|
|
|
// Path if we have encrypted persistent
|
|
|
|
if len(c.Install.Encrypt) != 0 {
|
|
|
|
err := kcrypt.UnlockAll(false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-03-13 10:22:26 +00:00
|
|
|
// Close all the unencrypted partitions at the end!
|
2024-03-07 14:11:51 +00:00
|
|
|
defer func() {
|
2025-03-13 10:22:26 +00:00
|
|
|
for _, p := range c.Install.Encrypt {
|
2024-03-07 14:11:51 +00:00
|
|
|
c.Logger.Debugf("Closing unencrypted /dev/disk/by-label/%s", p)
|
|
|
|
out, err := utils.SH(fmt.Sprintf("cryptsetup close /dev/disk/by-label/%s", p))
|
|
|
|
// There is a known error with cryptsetup that it can't close the device because of a semaphore
|
|
|
|
// doesnt seem to affect anything as the device is closed as expected so we ignore it if it matches the
|
|
|
|
// output of the error
|
|
|
|
if err != nil && !strings.Contains(out, "incorrect semaphore state") {
|
|
|
|
c.Logger.Errorf("could not close /dev/disk/by-label/%s: %s", p, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2025-03-13 10:22:26 +00:00
|
|
|
_, _ = utils.SH("udevadm trigger --type=all || udevadm trigger")
|
|
|
|
err := c.Syscall.Mount(filepath.Join("/dev/disk/by-label", constants.PersistentLabel), constants.PersistentDir, "ext4", 0, "")
|
2024-03-01 11:27:26 +00:00
|
|
|
if err != nil {
|
2025-03-13 10:22:26 +00:00
|
|
|
fmt.Printf("could not mount persistent: %s\n", err)
|
|
|
|
return err
|
2024-03-01 11:27:26 +00:00
|
|
|
}
|
|
|
|
|
2025-03-13 10:22:26 +00:00
|
|
|
defer func() {
|
|
|
|
err := machine.Umount(constants.PersistentDir)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger.Errorf("could not unmount persistent partition: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-03-01 11:27:26 +00:00
|
|
|
// Create the directory on persistent
|
|
|
|
varLog := filepath.Join(constants.PersistentDir, ".state", "var-log.bind")
|
|
|
|
|
|
|
|
err = fsutils.MkdirAll(c.Fs, varLog, 0755)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger.Errorf("could not create directory on persistent partition: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Copy all current logs to the persistent partition
|
|
|
|
err = internalutils.SyncData(c.Logger, c.Runner, c.Fs, "/var/log/", varLog, []string{}...)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger.Errorf("could not copy logs to persistent partition: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
syscall.Sync()
|
|
|
|
c.Logger.Debugf("Logs copied to persistent partition")
|
2024-03-07 14:11:51 +00:00
|
|
|
c.Logger.Logger.Debug().Msg("Finish CopyLogs hook")
|
2024-03-01 11:27:26 +00:00
|
|
|
return nil
|
|
|
|
}
|