Accept more paths to devices for install (#552)

This commit is contained in:
Itxaka 2024-09-23 17:13:07 +02:00 committed by GitHub
parent ddd5d884cf
commit 60af8246ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 1 deletions

View File

@ -47,6 +47,27 @@ const (
TiB
)
// TODO: Move it to the sdk
func resolveTarget(fs v1.FS, 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)
}
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
@ -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)

View File

@ -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 {