Skip to content

Commit

Permalink
Really resolve the target for config and spec (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itxaka authored Sep 25, 2024
1 parent 9d63bae commit 6fe3455
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions pkg/config/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ const (
TiB
)

// TODO: Move it to the sdk
func resolveTarget(fs v1.FS, target string) (string, error) {
// resolveTarget will try to resovle a /dev/disk/by-X disk into the final real disk under /dev/X
// We use it to calculate the device on the fly for the Config and the InstallSpec but we leave
// the original value in teh config.Collector so its written down in the final cloud config in the
// installed system, so users can know what parameters it was installed with in case they need to refer
// to it down the line to know what was the original parametes
// If the target is a normal /dev/X we dont do anything and return the original value so normal installs
// should not be affected
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)
}
// Use EvanSymlinks to properly resolve the target
// Use EvanSymlinks to properly resolve the full path to the target
device, err := filepath.EvalSymlinks(target)
if err != nil {
return "", fmt.Errorf("failed to read device link for %s: %w", target, err)
Expand All @@ -65,7 +71,7 @@ func resolveTarget(fs v1.FS, target string) (string, error) {
}
return device, nil
}
// If we dont resolve and dont fail, just return the original target
// If we don't resolve and don't fail, just return the original target
return target, nil
}

Expand All @@ -89,7 +95,8 @@ func NewInstallSpec(cfg *Config) (*v1.InstallSpec, error) {
firmware = v1.BIOS
}

dev, err := resolveTarget(cfg.Fs, cfg.Install.Device)
// Resolve the install target
dev, err := resolveTarget(cfg.Install.Device)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -165,6 +172,12 @@ func NewInstallSpec(cfg *Config) (*v1.InstallSpec, error) {
return nil, fmt.Errorf("failed unmarshalling the full spec: %w", err)
}

// resolve also the target of the spec so we can partition properly
spec.Target, err = resolveTarget(spec.Target)
if err != nil {
return nil, err
}

// Calculate the partitions afterwards so they use the image sizes for the final partition sizes
spec.Partitions = NewInstallElementalPartitions(cfg.Logger, spec)

Expand Down

0 comments on commit 6fe3455

Please sign in to comment.