Skip to content

Commit

Permalink
Rework it
Browse files Browse the repository at this point in the history
Signed-off-by: Itxaka <itxaka@kairos.io>
  • Loading branch information
Itxaka committed Sep 20, 2024
1 parent 009c412 commit 584bc88
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
28 changes: 28 additions & 0 deletions pkg/config/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ const (
TiB
)

// TODO: Move it to the sdk
func resolveTarget(fs v1.FS, target string) (string, error) {
// Accept that the target can be a /dev/disk/by-{label,uuid,path,etc..} and resolve it into a /dev/device
if strings.HasPrefix(target, "/dev/disk/by-") {
// we dont accept partitions as target so check and fail earlier for those that are partuuid or parlabel
if strings.Contains(target, "partlabel") || strings.Contains(target, "partuuid") {
return "", fmt.Errorf("target contains 'parlabel' or 'partuuid', looks like its a partition instead of a disk: %s", target)
}
device, err := fs.Readlink(target)
if err != nil {
return "", fmt.Errorf("failed to read device link for %s: %w", target, err)
}
if !strings.HasPrefix(device, "/dev/") {
return "", fmt.Errorf("device %s is not a valid device path", device)
}
return device, nil
}
// If we dont resolve and dont fail, just return the original target
return target, nil
}

// NewInstallSpec returns an InstallSpec struct all based on defaults and basic host checks (e.g. EFI vs BIOS)
func NewInstallSpec(cfg *Config) (*v1.InstallSpec, error) {
var firmware string
Expand All @@ -67,6 +88,13 @@ func NewInstallSpec(cfg *Config) (*v1.InstallSpec, error) {
firmware = v1.BIOS
}

dev, err := resolveTarget(cfg.Fs, cfg.Install.Device)
if err != nil {
return nil, err
}

cfg.Install.Device = dev

activeImg.Label = constants.ActiveLabel
activeImg.Size = constants.ImgSize
activeImg.File = filepath.Join(constants.StateDir, "cOS", constants.ActiveImgFile)
Expand Down
33 changes: 2 additions & 31 deletions pkg/types/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ package v1

import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/kairos-io/kairos-agent/v2/pkg/constants"
"github.com/kairos-io/kairos-sdk/ghw"
"github.com/kairos-io/kairos-sdk/types"
"gopkg.in/yaml.v3"
"path/filepath"
"sort"
)

const (
Expand Down Expand Up @@ -75,35 +72,9 @@ type InstallSpec struct {
GrubConf string
}

// TODO: Move it to the sdk, make it use a vfs to read the link
func resolveTarget(target string) (string, error) {
// Accept that the target can be a /dev/disk/by-{label,uuid,path,etc..} and resolve it into a /dev/device
if strings.HasPrefix(target, "/dev/disk/by-") {
// we dont accept partitions as target so check and fail earlier for those that are partuuid or parlabel
if strings.Contains(target, "partlabel") || strings.Contains(target, "partuuid") {
return "", fmt.Errorf("target contains 'parlabel' or 'partuuid', looks like its a partition instead of a disk: %s", target)
}
device, err := os.Readlink(target)
if err != nil {
return "", fmt.Errorf("failed to read device link for %s: %w", target, err)
}
if !strings.HasPrefix(device, "/dev/") {
return "", fmt.Errorf("device %s is not a valid device path", device)
}
return device, nil
}
// If we dont resolve and dont fail, just return the original target
return target, nil
}

// Sanitize checks the consistency of the struct, returns error
// if unsolvable inconsistencies are found
func (i *InstallSpec) Sanitize() error {
var err error
i.Target, err = resolveTarget(i.Target)
if err != nil {
return err
}
// Check if the target device has mounted partitions
for _, disk := range ghw.GetDisks(ghw.NewPaths(""), nil) {
if fmt.Sprintf("/dev/%s", disk.Name) == i.Target {
Expand Down

0 comments on commit 584bc88

Please sign in to comment.