Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Karakasilis <[email protected]>
  • Loading branch information
jimmykarily committed Feb 28, 2024
1 parent c8cab51 commit 822b873
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 33 deletions.
48 changes: 48 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#cloud-config

strict: true
debug: true

install:
no-format: true
auto: true
poweroff: false
reboot: false
grub_options:
extra_cmdline: "rd.immucore.debug"
users:
- name: "kairos"
passwd: "kairos"

stages:
kairos-install.pre.before:
- if: '[ -e "/dev/vdb" ]'
name: "Create partitions"
commands:
- |
parted --script --machine -- "/dev/vdb" mklabel gpt
#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
- fsLabel: COS_RECOVERY
size: 8500
pLabel: recovery
- fsLabel: COS_STATE
size: 18000
pLabel: state
- fsLabel: COS_PERSISTENT
pLabel: persistent
size: 0
filesystem: "ext4"
boot:
- systemd_firstboot:
keymap: us
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

// This versions require go1.20
replace (
github.com/kairos-io/kairos-sdk => /home/dimitris/workspace/kairos/kairos-sdk
github.com/onsi/ginkgo/v2 v2.15.0 => github.com/onsi/ginkgo/v2 v2.12.1
github.com/onsi/gomega v1.31.1 => github.com/onsi/gomega v1.28.0
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,6 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kairos-io/kairos-sdk v0.0.25 h1:/wQ8lHmmqJgKPaxzLllTgMQNWRje80XKdj8F0XlFXl0=
github.com/kairos-io/kairos-sdk v0.0.25/go.mod h1:btSB2QAds/WSyIyPxnQ3jueMbkkZ75pHUUCj+yHpthQ=
github.com/kairos-io/kcrypt v0.9.1 h1:bMDXZ8MiPY/fgLe5CXQPfUIRchzxErlJ8nfZM/JPfEU=
github.com/kairos-io/kcrypt v0.9.1/go.mod h1:odsVL3ab2GJUgDnvDGJi54jrWUe6tjfW9875knT/ltU=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
Expand Down
11 changes: 4 additions & 7 deletions internal/agent/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ func ManualInstall(c, sourceImgURL, device string, reboot, poweroff, strictValid
cliConf := generateInstallConfForCLIArgs(sourceImgURL)
cliConfManualArgs := generateInstallConfForManualCLIArgs(device, reboot, poweroff)

fmt.Printf("configSource = %+v\n", configSource)
cc, err := config.Scan(collector.Directories(configSource),
collector.Readers(strings.NewReader(cliConf), strings.NewReader(cliConfManualArgs)),
collector.MergeBootLine,
collector.StrictValidation(strictValidations), collector.NoLogs)
if err != nil {
return err
}
fmt.Printf("cc.Install = %+v\n", cc.Install)

return RunInstall(cc)
}
Expand Down Expand Up @@ -121,7 +123,7 @@ func Install(sourceImgURL string, dir ...string) error {
return err
}

if cc.Install.Reboot == false && cc.Install.Poweroff == false {
if !cc.Install.Reboot && !cc.Install.Poweroff {
pterm.DefaultInteractiveContinue.Show("Installation completed, press enter to go back to the shell.")
svc, err := machine.Getty(1)
if err == nil {
Expand Down Expand Up @@ -194,7 +196,7 @@ func Install(sourceImgURL string, dir ...string) error {

// If neither reboot and poweroff are enabled let the user insert enter to go back to a new shell
// This is helpful to see the installation messages instead of just cleaning the screen with a new tty
if cc.Install.Reboot == false && cc.Install.Poweroff == false {
if !cc.Install.Reboot && !cc.Install.Poweroff {
pterm.DefaultInteractiveContinue.Show("Installation completed, press enter to go back to the shell.")

utils.Prompt("") //nolint:errcheck
Expand All @@ -213,11 +215,6 @@ func RunInstall(c *config.Config) error {
utils.SetEnv(c.Env)
utils.SetEnv(c.Install.Env)

// if c.Install.Device == "" || c.Install.Device == "auto" {
// c.Install.Device = detectDevice()
// }
c.Install.Device = "/dev/vdb"

// UKI path. Check if we are on UKI AND if we are running off a cd, otherwise it makes no sense to run the install
// From the installed system
if internalutils.IsUkiWithFs(c.Fs) {
Expand Down
17 changes: 0 additions & 17 deletions internal/agent/interactive_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,6 @@ func promptToUnstructured(p events.YAMLPrompt, unstructuredYAML map[string]inter
return unstructuredYAML, nil
}

func detectDevice() string {
preferedDevice := "/dev/sda"
maxSize := float64(0)

block, err := ghw.Block()
if err == nil {
for _, disk := range block.Disks {
size := float64(disk.SizeBytes) / float64(GiB)
if size > maxSize {
maxSize = size
preferedDevice = "/dev/" + disk.Name
}
}
}
return preferedDevice
}

func InteractiveInstall(debug, spawnShell bool, sourceImgURL string) error {
var sshUsers []string
bus.Manager.Initialize()
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
type Install struct {
Auto bool `yaml:"auto,omitempty"`
Reboot bool `yaml:"reboot,omitempty"`
NoFormat bool `yaml:"no_format,omitempty"`
NoFormat bool `yaml:"no-format,omitempty"`
Device string `yaml:"device,omitempty"`
Poweroff bool `yaml:"poweroff,omitempty"`
GrubOptions map[string]string `yaml:"grub_options,omitempty"`
Expand Down Expand Up @@ -336,8 +336,8 @@ func Scan(opts ...collector.Option) (c *Config, err error) {
genericConfig, err := collector.Scan(o, FilterKeys)
if err != nil {
return result, err

}

result.Config = *genericConfig
configStr, err := genericConfig.String()
if err != nil {
Expand Down
79 changes: 74 additions & 5 deletions pkg/config/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/jaypipes/ghw"
"golang.org/x/sys/unix"

"github.com/kairos-io/kairos-agent/v2/internal/common"
Expand All @@ -38,6 +39,14 @@ import (
"github.com/spf13/viper"
)

const (
_ = 1 << (10 * iota)
KiB
MiB
GiB
TiB
)

// 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 Down Expand Up @@ -558,18 +567,33 @@ func ReadInstallSpecFromConfig(c *Config) (*v1.InstallSpec, error) {
return &v1.InstallSpec{}, err
}
installSpec := sp.(*v1.InstallSpec)

// TODO: Do the same for UKI
if installSpec.Target == "" || installSpec.Target == "auto" {
installSpec.Target = detectLargestDevice()
}

fmt.Printf("installSpec before = %+v\n", installSpec)
fmt.Printf("c = %+v\n", c)
if installSpec.NoFormat {
installSpec.Target = ""
}
fmt.Printf("installSpec after = %+v\n", installSpec)

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

// Workaround!
// If we set the "auto" for the device in the cloudconfig the value will be proper in the Config.Install.Device
// But on the cloud-config it will still appear as "auto" as we dont modify that
// Unfortunately as we load the full cloud-config and unmarshall it into our spec, we cannot infer from there
// 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
// }
return installSpec, nil
}

Expand Down Expand Up @@ -660,6 +684,7 @@ func ReadUkiInstallSpecFromConfig(c *Config) (*v1.InstallUkiSpec, error) {
return &v1.InstallUkiSpec{}, err
}
installSpec := sp.(*v1.InstallUkiSpec)

// Workaround!
// If we set the "auto" for the device in the cloudconfig the value will be proper in the Config.Install.Device
// But on the cloud-config it will still appear as "auto" as we dont modify that
Expand All @@ -670,6 +695,7 @@ func ReadUkiInstallSpecFromConfig(c *Config) (*v1.InstallUkiSpec, error) {
if installSpec.Target == "auto" {
installSpec.Target = c.Install.Device
}

return installSpec, nil
}

Expand Down Expand Up @@ -1017,3 +1043,46 @@ func unmarshallFullSpec(r *Config, subkey string, sp v1.Spec) error {

return nil
}

// detectLargestDevice returns the largest disk found
func detectLargestDevice() string {
preferedDevice := "/dev/sda"
maxSize := float64(0)

block, err := ghw.Block()
if err == nil {
for _, disk := range block.Disks {
size := float64(disk.SizeBytes) / float64(GiB)
if size > maxSize {
maxSize = size
preferedDevice = "/dev/" + disk.Name
}
}
}
return preferedDevice
}

// 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 {
block, err := ghw.Block()
if err != nil {
logger.Errorf("failed getting block devices: %w", err)
return ""
}

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 ""
}

0 comments on commit 822b873

Please sign in to comment.