Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept more paths to devices for install #552

Merged
merged 5 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good here to test it directly on kairos via branches, if it works will move it into the sdk as its a very generic method that can be used everywhere, including immucore

// 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
1 change: 0 additions & 1 deletion pkg/types/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ type InstallSpec struct {
// if unsolvable inconsistencies are found
func (i *InstallSpec) Sanitize() error {
// 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 {
for _, p := range disk.Partitions {
Expand Down