immucore/internal/utils/upgrade_kcrypt.go

62 lines
1.9 KiB
Go
Raw Normal View History

package utils
import (
"fmt"
"path/filepath"
"strings"
"github.com/gofrs/uuid"
"github.com/jaypipes/ghw"
"github.com/kairos-io/kairos-sdk/utils"
)
// UpgradeKcryptPartitions will try check for the uuid of the persistent partition and upgrade its uuid.
func UpgradeKcryptPartitions() error {
// Generate the predictable UUID
persistentUUID := uuid.NewV5(uuid.NamespaceURL, "COS_PERSISTENT")
// Check if there are any LUKS partitions, otherwise ignore
blk, err := ghw.Block()
if err != nil {
return err
}
for _, disk := range blk.Disks {
for _, p := range disk.Partitions {
if p.Type == "crypto_LUKS" {
// Check against known partition label on persistent
Log.Debug().Str("label", p.Label).Str("dev", p.Name).Msg("found luks partition")
if p.Label == "persistent" {
// Get current UUID
volumeUUID, err := utils.SH(fmt.Sprintf("cryptsetup luksUUID %s", filepath.Join("/dev", p.Name)))
if err != nil {
Log.Err(err).Send()
return err
}
volumeUUID = strings.TrimSpace(volumeUUID)
volumeUUIDParsed, err := uuid.FromString(volumeUUID)
Log.Debug().Interface("volumeUUID", volumeUUIDParsed).Send()
Log.Debug().Interface("persistentUUID", persistentUUID).Send()
if err != nil {
Log.Err(err).Send()
return err
}
// Check to see if it's the same already to not do anything
if volumeUUIDParsed.String() != persistentUUID.String() {
Log.Debug().Str("old", volumeUUIDParsed.String()).Str("new", persistentUUID.String()).Msg("Uuid is different, updating")
out, err := utils.SH(fmt.Sprintf("cryptsetup luksUUID -q --uuid %s %s", persistentUUID, filepath.Join("/dev", p.Name)))
if err != nil {
Log.Err(err).Str("out", out).Msg("Updating uuid failed")
return err
}
} else {
Log.Debug().Msg("UUIDs are the same, not updating")
}
}
}
}
}
return nil
}