Skip to content

Commit

Permalink
Detect the preconfigured device when no-format is true
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Karakasilis <[email protected]>
  • Loading branch information
jimmykarily committed Apr 5, 2024
1 parent 19aeef1 commit 2249a22
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
4 changes: 1 addition & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ stages:
commands:
- |
parted --script --machine -- "/dev/vdb" mklabel gpt
sgdisk --new=1:2048:+1M --change-name=1:'bios' --typecode=1:EF02 /dev/vdb # for grub
#parted --script "/dev/vdb" mkpart primary fat32 0 1MB
#mkfs.fat32 -L COS_GRUB /dev/vdb1
layout:
device:
path: "/dev/vdb"
add_partitions:
- fsLabel: COS_GRUB
size: 1
pLabel: grub
- fsLabel: COS_OEM
size: 64
pLabel: oem
Expand Down
10 changes: 10 additions & 0 deletions pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,19 @@ func (i InstallAction) Run() (err error) {
fmt.Println("!!!!!!!!!!!! won't format")
// Check force flag against current device
labels := []string{i.spec.Active.Label, i.spec.Recovery.Label}
fmt.Printf("!!!! check active deployment = %+v\n", e.CheckActiveDeployment(labels))
if e.CheckActiveDeployment(labels) && !i.spec.Force {
return fmt.Errorf("use `force` flag to run an installation over the current running deployment")
}

if i.spec.Target == "" {
device, err := config.DetectPreConfiguredDevice(i.cfg.Logger)
if err != nil {
return fmt.Errorf("no target device specified and no device found: %s", err)
}
fmt.Printf("!!! device = %+v\n", device)
i.spec.Target = device
}
} else {
// Deactivate any active volume on target
err = e.DeactivateDevices()
Expand Down
38 changes: 23 additions & 15 deletions pkg/config/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package config

import (
"fmt"
sdkTypes "github.com/kairos-io/kairos-sdk/types"
"io/fs"
"os"
"path/filepath"
"reflect"
"strings"

sdkTypes "github.com/kairos-io/kairos-sdk/types"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/jaypipes/ghw"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -580,6 +581,7 @@ func ReadInstallSpecFromConfig(c *Config) (*v1.InstallSpec, error) {
fmt.Printf("installSpec after = %+v\n", installSpec)

fmt.Printf("!!!!!()()()()()!!!! installSPec.Target = %+v\n", installSpec.Target)
fmt.Printf("!!!!!()()()()()!!!! c.Install.Device = %+v\n", c.Install.Device)

// Workaround!
// If we set the "auto" for the device in the cloudconfig the value will be proper in the Config.Install.Device
Expand All @@ -588,11 +590,22 @@ func ReadInstallSpecFromConfig(c *Config) (*v1.InstallSpec, error) {
// What device was choosen, and re-choosing again could lead to different results
// So instead we do the check here and override the installSpec.Target with the Config.Install.Device
// as its the soonest we have access to both
// if installSpec.Target == "auto" {
// fmt.Printf("!!!!!!! installSpec.Target = %+v\n", installSpec.Target)
// fmt.Printf("!!!!!!! c.Install.Device = %+v\n", c.Install.Device)
// installSpec.Target = c.Install.Device
if installSpec.Target == "auto" {
// fmt.Printf("!!!!!!! installSpec.Target = %+v\n", installSpec.Target)
// fmt.Printf("!!!!!!! c.Install.Device = %+v\n", c.Install.Device)
installSpec.Target = c.Install.Device
}

// if installSpec.Target == "" {
// device, err := DetectPreConfiguredDevice(c.Logger)
// if err != nil {
// return installSpec, err
// }
// installSpec.Target = device
// }

// fmt.Printf("!!!!!(after)()()()()!!!! installSPec.Target = %+v\n", installSpec.Target)
// fmt.Printf("!!!!!(after)()()()()!!!! c.Install.Device = %+v\n", c.Install.Device)
return installSpec, nil
}

Expand Down Expand Up @@ -1017,27 +1030,22 @@ func detectLargestDevice() string {
return preferedDevice
}

// detectPreConfiguredDevice returns a disk that has partitions labeled with
// DetectPreConfiguredDevice returns a disk that has partitions labeled with
// Kairos labels. It can be used to detect a pre-configured device.
func detectPreConfiguredDevice(logger v1.Logger) string {
func DetectPreConfiguredDevice(logger sdkTypes.KairosLogger) (string, error) {
block, err := ghw.Block()
if err != nil {
logger.Errorf("failed getting block devices: %w", err)
return ""
return "", err
}

fmt.Println("!!!!!***!!!! detecting preconfigured")
for _, disk := range block.Disks {
fmt.Printf("!!!!!!!!!!!!!!! disk = %+v\n", disk)
for _, p := range disk.Partitions {
fmt.Printf("!!!!!!!!!!!!!!!! p = %+v\n", p)
fmt.Printf("p.FilesystemLabel = %+v\n", p.FilesystemLabel)
fmt.Printf("p.Label = %+v\n", p.Label)
if p.FilesystemLabel == "COS_STATE" {
return disk.Name
return filepath.Join("/", "dev", disk.Name), nil
}
}
}

return ""
return "", nil
}

0 comments on commit 2249a22

Please sign in to comment.