From 571f10d9005b556eb3c9fcae218509399f6f0b9e Mon Sep 17 00:00:00 2001 From: Dimitris Karakasilis Date: Mon, 8 Apr 2024 15:58:36 +0300 Subject: [PATCH] Remove unecessary assignments and add NoFormat to UKI Signed-off-by: Dimitris Karakasilis --- pkg/action/install.go | 5 +++-- pkg/config/spec.go | 29 +++-------------------------- pkg/types/v1/config.go | 1 + pkg/uki/install.go | 37 ++++++++++++++++++++++++++----------- 4 files changed, 33 insertions(+), 39 deletions(-) diff --git a/pkg/action/install.go b/pkg/action/install.go index 84313c7..f0f960a 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -144,7 +144,6 @@ func (i InstallAction) Run() (err error) { } } - // Check no-format flag if i.spec.NoFormat { i.cfg.Logger.Infof("NoFormat is true, skipping format and partitioning") // Check force flag against current device @@ -153,7 +152,9 @@ func (i InstallAction) Run() (err error) { return fmt.Errorf("use `force` flag to run an installation over the current running deployment") } - if i.spec.Target == "" { + if i.spec.Target == "" || i.spec.Target == "auto" { + // This needs to run after the pre-install stage to give the user the + // opportunity to prepare the target disk in the pre-install stage. device, err := config.DetectPreConfiguredDevice(i.cfg.Logger) if err != nil { return fmt.Errorf("no target device specified and no device found: %s", err) diff --git a/pkg/config/spec.go b/pkg/config/spec.go index 978661b..6f49dda 100644 --- a/pkg/config/spec.go +++ b/pkg/config/spec.go @@ -567,26 +567,10 @@ func ReadInstallSpecFromConfig(c *Config) (*v1.InstallSpec, error) { } installSpec := sp.(*v1.InstallSpec) - // TODO: Do the same for UKI - if installSpec.Target == "" || installSpec.Target == "auto" { + if (installSpec.Target == "" || installSpec.Target == "auto") && !installSpec.NoFormat { installSpec.Target = detectLargestDevice() } - if installSpec.NoFormat { - 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" { - installSpec.Target = c.Install.Device - } - return installSpec, nil } @@ -678,15 +662,8 @@ func ReadUkiInstallSpecFromConfig(c *Config) (*v1.InstallUkiSpec, error) { } 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 - // 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" { - installSpec.Target = c.Install.Device + if (installSpec.Target == "" || installSpec.Target == "auto") && !installSpec.NoFormat { + installSpec.Target = detectLargestDevice() } return installSpec, nil diff --git a/pkg/types/v1/config.go b/pkg/types/v1/config.go index 9553a78..e1c6eb5 100644 --- a/pkg/types/v1/config.go +++ b/pkg/types/v1/config.go @@ -510,6 +510,7 @@ type InstallUkiSpec struct { PowerOff bool `yaml:"poweroff,omitempty" mapstructure:"poweroff"` Partitions ElementalPartitions `yaml:"partitions,omitempty" mapstructure:"partitions"` ExtraPartitions PartitionList `yaml:"extra-partitions,omitempty" mapstructure:"extra-partitions"` + NoFormat bool `yaml:"no-format,omitempty" mapstructure:"no-format"` CloudInit []string `yaml:"cloud-init,omitempty" mapstructure:"cloud-init"` SkipEntries []string `yaml:"skip-entries,omitempty" mapstructure:"skip-entries"` } diff --git a/pkg/uki/install.go b/pkg/uki/install.go index ec957c7..c245da0 100644 --- a/pkg/uki/install.go +++ b/pkg/uki/install.go @@ -40,17 +40,32 @@ func (i *InstallAction) Run() (err error) { i.cfg.Logger.Errorf("running kairos-uki-install.pre hook script: %s", err.Error()) } - // Deactivate any active volume on target - err = e.DeactivateDevices() - if err != nil { - i.cfg.Logger.Errorf("deactivating devices: %s", err.Error()) - return err - } - // Partition device - err = e.PartitionAndFormatDevice(i.spec) - if err != nil { - i.cfg.Logger.Errorf("partitioning and formating devices: %s", err.Error()) - return err + if i.spec.NoFormat { + i.cfg.Logger.Infof("NoFormat is true, skipping format and partitioning") + if i.spec.Target == "" || i.spec.Target == "auto" { + // This needs to run after the pre-install stage to give the user the + // opportunity to prepare the target disk in the pre-install stage. + device, err := config.DetectPreConfiguredDevice(i.cfg.Logger) + if err != nil { + return fmt.Errorf("no target device specified and no device found: %s", err) + } + i.cfg.Logger.Infof("No target device specified, using pre-configured device: %s", device) + i.spec.Target = device + } + } else { + // Deactivate any active volume on target + err = e.DeactivateDevices() + if err != nil { + i.cfg.Logger.Errorf("deactivating devices: %s", err.Error()) + return err + } + + // Partition device + err = e.PartitionAndFormatDevice(i.spec) + if err != nil { + i.cfg.Logger.Errorf("partitioning and formating devices: %s", err.Error()) + return err + } } err = e.MountPartitions(i.spec.GetPartitions().PartitionsByMountPoint(false))