diff --git a/config/shared/errors/errors.go b/config/shared/errors/errors.go index 0c1500b3c..0d8ba629b 100644 --- a/config/shared/errors/errors.go +++ b/config/shared/errors/errors.go @@ -93,6 +93,7 @@ var ( ErrPathConflictsSystemd = errors.New("path conflicts with systemd unit or dropin") ErrCexWithClevis = errors.New("cannot use cex with clevis") ErrCexWithKeyFile = errors.New("cannot use key file with cex") + ErrBadSfdiskPretend = errors.New("sfdisk had unexpected output while pretending partition configuration on device") // Systemd section errors ErrInvalidSystemdExt = errors.New("invalid systemd unit extension") diff --git a/internal/distro/distro.go b/internal/distro/distro.go index 94dd86973..d98e14ec7 100644 --- a/internal/distro/distro.go +++ b/internal/distro/distro.go @@ -17,6 +17,7 @@ package distro import ( "fmt" "os" + "strings" ) // Distro-specific settings that can be overridden at link time with e.g. @@ -42,6 +43,7 @@ var ( mountCmd = "mount" partxCmd = "partx" sgdiskCmd = "sgdisk" + sfdiskCmd = "sfdisk" modprobeCmd = "modprobe" udevadmCmd = "udevadm" usermodCmd = "usermod" @@ -113,6 +115,7 @@ func MdadmCmd() string { return mdadmCmd } func MountCmd() string { return mountCmd } func PartxCmd() string { return partxCmd } func SgdiskCmd() string { return sgdiskCmd } +func SfdiskCmd() string { return sfdiskCmd } func ModprobeCmd() string { return modprobeCmd } func UdevadmCmd() string { return udevadmCmd } func UsermodCmd() string { return usermodCmd } @@ -149,6 +152,36 @@ func WriteAuthorizedKeysFragment() bool { return bakedStringToBool(fromEnv("WRITE_AUTHORIZED_KEYS_FRAGMENT", writeAuthorizedKeysFragment)) } +var partitionerBackend string + +func PartitionerBackend() string { + if partitionerBackend == "" { + partitionerBackend = readPartitionerFromCmdline() + } + return partitionerBackend +} + +func readPartitionerFromCmdline() string { + // Allow override via environment variable for testing + if env := os.Getenv("IGNITION_PARTITIONER"); env == "sfdisk" || env == "sgdisk" { + return env + } + cmdline, err := os.ReadFile(kernelCmdlinePath) + if err != nil { + return "sfdisk" + } + for _, arg := range strings.Split(strings.TrimSpace(string(cmdline)), " ") { + parts := strings.SplitN(arg, "=", 2) + if parts[0] == "ignition.partitioner" && len(parts) == 2 { + switch parts[1] { + case "sfdisk", "sgdisk": + return parts[1] + } + } + } + return "sfdisk" +} + func fromEnv(nameSuffix, defaultValue string) string { value := os.Getenv("IGNITION_" + nameSuffix) if value != "" { diff --git a/internal/exec/stages/disks/partitions.go b/internal/exec/stages/disks/partitions.go index 9a7ae9cad..b6915a12e 100644 --- a/internal/exec/stages/disks/partitions.go +++ b/internal/exec/stages/disks/partitions.go @@ -20,12 +20,10 @@ package disks import ( "bufio" - "errors" "fmt" "os" "os/exec" "path/filepath" - "regexp" "sort" "strconv" "strings" @@ -34,13 +32,21 @@ import ( "github.com/coreos/ignition/v2/config/v3_7_experimental/types" "github.com/coreos/ignition/v2/internal/distro" "github.com/coreos/ignition/v2/internal/exec/util" - "github.com/coreos/ignition/v2/internal/sgdisk" + "github.com/coreos/ignition/v2/internal/log" + "github.com/coreos/ignition/v2/internal/partitioners" + "github.com/coreos/ignition/v2/internal/partitioners/sfdisk" + "github.com/coreos/ignition/v2/internal/partitioners/sgdisk" iutil "github.com/coreos/ignition/v2/internal/util" ) -var ( - ErrBadSgdiskOutput = errors.New("sgdisk had unexpected output") -) +func getDeviceManager(logger *log.Logger, dev string) partitioners.DeviceManager { + switch distro.PartitionerBackend() { + case "sfdisk": + return sfdisk.Begin(logger, dev) + default: + return sgdisk.Begin(logger, dev) + } +} // createPartitions creates the partitions described in config.Storage.Disks. func (s stage) createPartitions(config types.Config) error { @@ -75,7 +81,7 @@ func (s stage) createPartitions(config types.Config) error { // partitionMatches determines if the existing partition matches the spec given. See doc/operator notes for what // what it means for an existing partition to match the spec. spec must have non-zero Start and Size. -func partitionMatches(existing util.PartitionInfo, spec sgdisk.Partition) error { +func partitionMatches(existing util.PartitionInfo, spec partitioners.Partition) error { if err := partitionMatchesCommon(existing, spec); err != nil { return err } @@ -87,13 +93,13 @@ func partitionMatches(existing util.PartitionInfo, spec sgdisk.Partition) error // partitionMatchesResize returns if the existing partition should be resized by evaluating if // `resize`field is true and partition matches in all respects except size. -func partitionMatchesResize(existing util.PartitionInfo, spec sgdisk.Partition) bool { +func partitionMatchesResize(existing util.PartitionInfo, spec partitioners.Partition) bool { return cutil.IsTrue(spec.Resize) && partitionMatchesCommon(existing, spec) == nil } // partitionMatchesCommon handles the common tests (excluding the partition size) to determine // if the existing partition matches the spec given. -func partitionMatchesCommon(existing util.PartitionInfo, spec sgdisk.Partition) error { +func partitionMatchesCommon(existing util.PartitionInfo, spec partitioners.Partition) error { if spec.Number != existing.Number { return fmt.Errorf("partition numbers did not match (specified %d, got %d). This should not happen, please file a bug", spec.Number, existing.Number) } @@ -113,7 +119,7 @@ func partitionMatchesCommon(existing util.PartitionInfo, spec sgdisk.Partition) } // partitionShouldBeInspected returns if the partition has zeroes that need to be resolved to sectors. -func partitionShouldBeInspected(part sgdisk.Partition) bool { +func partitionShouldBeInspected(part partitioners.Partition) bool { if part.Number == 0 { return false } @@ -133,17 +139,17 @@ func convertMiBToSectors(mib *int, sectorSize int) *int64 { // getRealStartAndSize returns a map of partition numbers to a struct that contains what their real start // and end sector should be. It runs sgdisk --pretend to determine what the partitions would look like if // everything specified were to be (re)created. -func (s stage) getRealStartAndSize(dev types.Disk, devAlias string, diskInfo util.DiskInfo) ([]sgdisk.Partition, error) { - partitions := []sgdisk.Partition{} +func (s stage) getRealStartAndSize(dev types.Disk, devAlias string, diskInfo util.DiskInfo) ([]partitioners.Partition, error) { + partitions := []partitioners.Partition{} for _, cpart := range dev.Partitions { - partitions = append(partitions, sgdisk.Partition{ + partitions = append(partitions, partitioners.Partition{ Partition: cpart, StartSector: convertMiBToSectors(cpart.StartMiB, diskInfo.LogicalSectorSize), SizeInSectors: convertMiBToSectors(cpart.SizeMiB, diskInfo.LogicalSectorSize), }) } - op := sgdisk.Begin(s.Logger, devAlias) + op := getDeviceManager(s.Logger, devAlias) for _, part := range partitions { if info, exists := diskInfo.GetPartition(part.Number); exists { // delete all existing partitions @@ -177,19 +183,19 @@ func (s stage) getRealStartAndSize(dev types.Disk, devAlias string, diskInfo uti return nil, err } - realDimensions, err := parseSgdiskPretend(output, partitionsToInspect) + realDimensions, err := op.ParseOutput(output, partitionsToInspect) if err != nil { return nil, err } - result := []sgdisk.Partition{} + result := []partitioners.Partition{} for _, part := range partitions { if dims, ok := realDimensions[part.Number]; ok { if part.StartSector != nil { - part.StartSector = &dims.start + part.StartSector = &dims.Start } if part.SizeInSectors != nil { - part.SizeInSectors = &dims.size + part.SizeInSectors = &dims.Size } } result = append(result, part) @@ -197,96 +203,9 @@ func (s stage) getRealStartAndSize(dev types.Disk, devAlias string, diskInfo uti return result, nil } -type sgdiskOutput struct { - start int64 - size int64 -} - -// parseLine takes a regexp that captures an int64 and a string to match on. On success it returns -// the captured int64 and nil. If the regexp does not match it returns -1 and nil. If it encountered -// an error it returns 0 and the error. -func parseLine(r *regexp.Regexp, line string) (int64, error) { - matches := r.FindStringSubmatch(line) - switch len(matches) { - case 0: - return -1, nil - case 2: - return strconv.ParseInt(matches[1], 10, 64) - default: - return 0, ErrBadSgdiskOutput - } -} - -// parseSgdiskPretend parses the output of running sgdisk pretend with --info specified for each partition -// number specified in partitionNumbers. E.g. if paritionNumbers is [1,4,5], it is expected that the sgdisk -// output was from running `sgdisk --pretend --info=1 --info=4 --info=5`. It assumes the the -// partition labels are well behaved (i.e. contain no control characters). It returns a list of partitions -// matching the partition numbers specified, but with the start and size information as determined by sgdisk. -// The partition numbers need to passed in because sgdisk includes them in its output. -func parseSgdiskPretend(sgdiskOut string, partitionNumbers []int) (map[int]sgdiskOutput, error) { - if len(partitionNumbers) == 0 { - return nil, nil - } - startRegex := regexp.MustCompile(`^First sector: (\d*) \(.*\)$`) - endRegex := regexp.MustCompile(`^Last sector: (\d*) \(.*\)$`) - const ( - START = iota - END = iota - FAIL_ON_START_END = iota - ) - - output := map[int]sgdiskOutput{} - state := START - current := sgdiskOutput{} - i := 0 - - lines := strings.Split(sgdiskOut, "\n") - for _, line := range lines { - switch state { - case START: - start, err := parseLine(startRegex, line) - if err != nil { - return nil, err - } - if start != -1 { - current.start = start - state = END - } - case END: - end, err := parseLine(endRegex, line) - if err != nil { - return nil, err - } - if end != -1 { - current.size = 1 + end - current.start - output[partitionNumbers[i]] = current - i++ - if i == len(partitionNumbers) { - state = FAIL_ON_START_END - } else { - current = sgdiskOutput{} - state = START - } - } - case FAIL_ON_START_END: - if len(startRegex.FindStringSubmatch(line)) != 0 || - len(endRegex.FindStringSubmatch(line)) != 0 { - return nil, ErrBadSgdiskOutput - } - } - } - - if state != FAIL_ON_START_END { - // We stopped parsing in the middle of a info block. Something is wrong - return nil, ErrBadSgdiskOutput - } - - return output, nil -} - // partitionShouldExist returns whether a bool is indicating if a partition should exist or not. // nil (unspecified in json) is treated the same as true. -func partitionShouldExist(part sgdisk.Partition) bool { +func partitionShouldExist(part partitioners.Partition) bool { return !cutil.IsFalse(part.ShouldExist) } @@ -450,7 +369,7 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error { return fmt.Errorf("refusing to operate on directly active disk %q", devAlias) } if cutil.IsTrue(dev.WipeTable) { - op := sgdisk.Begin(s.Logger, devAlias) + op := getDeviceManager(s.Logger, devAlias) s.Info("wiping partition table requested on %q", devAlias) if len(activeParts) > 0 { return fmt.Errorf("refusing to wipe active disk %q", devAlias) @@ -464,12 +383,15 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error { return err } } + if err := s.waitForUdev(blockDevResolved); err != nil { + return fmt.Errorf("failed to wait for udev after wipe on %q: %v", blockDevResolved, err) + } } // Ensure all partitions with number 0 are last sort.Stable(PartitionList(dev.Partitions)) - op := sgdisk.Begin(s.Logger, devAlias) + op := getDeviceManager(s.Logger, devAlias) diskInfo, err := s.getPartitionMap(devAlias) if err != nil { @@ -519,6 +441,14 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error { partxDelete = append(partxDelete, uint64(part.Number)) case exists && shouldExist && matches: s.Info("partition %d found with correct specifications", part.Number) + if op.WritesCompleteTable() { + part.StartSector = &info.StartSector + part.SizeInSectors = &info.SizeInSectors + part.TypeGUID = &info.TypeGUID + part.GUID = &info.GUID + part.Label = &info.Label + op.CreatePartition(part) + } case exists && shouldExist && !wipeEntry && !matches: if partitionMatchesResize(info, part) { s.Info("resizing partition %d", part.Number) @@ -554,10 +484,10 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error { return fmt.Errorf("commit failure: %v", err) } - // In contrast to similar tools, sgdisk does not trigger the update of the - // kernel partition table with BLKPG but only uses BLKRRPART which fails - // as soon as one partition of the disk is mounted - if len(activeParts) > 0 { + // sgdisk does not trigger the update of the kernel partition table with + // BLKPG but only uses BLKRRPART which fails as soon as one partition of + // the disk is mounted. sfdisk handles this natively. + if op.NeedsPartx() && len(activeParts) > 0 { runPartxCommand := func(op string, partitions []uint64) error { for _, partNr := range partitions { cmd := exec.Command(distro.PartxCmd(), "--"+op, "--nr", strconv.FormatUint(partNr, 10), blockDevResolved) diff --git a/internal/partitioners/partitioners.go b/internal/partitioners/partitioners.go new file mode 100644 index 000000000..82411551f --- /dev/null +++ b/internal/partitioners/partitioners.go @@ -0,0 +1,47 @@ +// Copyright 2024 Red Hat, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package partitioners + +import ( + "github.com/coreos/ignition/v2/config/v3_7_experimental/types" +) + +type DeviceManager interface { + CreatePartition(p Partition) + DeletePartition(num int) + Info(num int) + WipeTable(wipe bool) + Pretend() (string, error) + Commit() error + ParseOutput(string, []int) (map[int]Output, error) + NeedsPartx() bool + WritesCompleteTable() bool +} + +// Partition wraps config types with sector-level positioning. +// StartMiB/SizeMiB shadow the config fields to prevent accidental use; +// callers should use StartSector/SizeInSectors instead. +type Partition struct { + types.Partition + StartSector *int64 + SizeInSectors *int64 + StartMiB string + SizeMiB string +} + +type Output struct { + Start int64 + Size int64 +} diff --git a/internal/partitioners/sfdisk/sfdisk.go b/internal/partitioners/sfdisk/sfdisk.go new file mode 100644 index 000000000..89ebd02fc --- /dev/null +++ b/internal/partitioners/sfdisk/sfdisk.go @@ -0,0 +1,534 @@ +// Copyright 2024 Red Hat, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package sfdisk + +import ( + "bytes" + "fmt" + "io" + "os/exec" + "regexp" + "sort" + "strconv" + "strings" + + sharedErrors "github.com/coreos/ignition/v2/config/shared/errors" + "github.com/coreos/ignition/v2/config/util" + "github.com/coreos/ignition/v2/internal/distro" + "github.com/coreos/ignition/v2/internal/log" + "github.com/coreos/ignition/v2/internal/partitioners" +) + +type Operation struct { + logger *log.Logger + dev string + wipe bool + parts []partitioners.Partition + deletions []int + infos []int + lastLBA int64 +} + +// Begin begins an sfdisk operation +func Begin(logger *log.Logger, dev string) *Operation { + return &Operation{logger: logger, dev: dev} +} + +// CreatePartition adds the supplied partition to the list of partitions to be created as part of an operation. +func (op *Operation) CreatePartition(p partitioners.Partition) { + op.parts = append(op.parts, p) +} + +func (op *Operation) DeletePartition(num int) { + op.deletions = append(op.deletions, num) +} + +func (op *Operation) Info(num int) { + op.infos = append(op.infos, num) +} + +// WipeTable toggles if the table is to be wiped first when committing this operation. +func (op *Operation) WipeTable(wipe bool) { + op.wipe = wipe +} + +func (op *Operation) NeedsPartx() bool { + return false +} + +func (op *Operation) WritesCompleteTable() bool { + return true +} + +// getLastLBA reads the last usable LBA from the device's GPT header using +// sfdisk --dump. If the device has no partition table yet, it computes +// the value from the device size (matching the GPT spec: last LBA minus +// the 34-sector backup header). +func (op *Operation) getLastLBA() int64 { + cmd := exec.Command(distro.SfdiskCmd(), "--dump", op.dev) + stdout, err := cmd.Output() + if err == nil { + re := regexp.MustCompile(`(?m)^last-lba:\s*(\d+)`) + match := re.FindSubmatch(stdout) + if len(match) >= 2 { + v, err := strconv.ParseInt(string(match[1]), 10, 64) + if err == nil { + return v + } + } + } + + // No GPT header — compute from device size. + sizeCmd := exec.Command("blockdev", "--getsize64", op.dev) + sizeOut, err := sizeCmd.Output() + if err != nil { + return -1 + } + deviceBytes, err := strconv.ParseInt(strings.TrimSpace(string(sizeOut)), 10, 64) + if err != nil || deviceBytes == 0 { + return -1 + } + sectors := deviceBytes / 512 + // GPT backup: 33 partition-entry sectors + 1 header sector = 34 + return sectors - 34 +} + +// readExistingPartitions reads the current partition table from the device +// using sfdisk --dump. +func (op *Operation) readExistingPartitions() ([]partitioners.Partition, error) { + cmd := exec.Command(distro.SfdiskCmd(), "--dump", op.dev) + stdout, err := cmd.Output() + if err != nil { + if op.logger != nil { + stderr := "" + if exitErr, ok := err.(*exec.ExitError); ok { + stderr = string(exitErr.Stderr) + } + op.logger.Warning("sfdisk --dump %q failed: %v stderr: %s", op.dev, err, stderr) + } + return nil, nil + } + if op.logger != nil { + op.logger.Info("sfdisk --dump %q output:\n%s", op.dev, string(stdout)) + } + + var partitions []partitioners.Partition + lineRegex := regexp.MustCompile(`^(/\S+)\s*:\s*(.*)$`) + numRegex := regexp.MustCompile(`(\d+)$`) + + for _, line := range strings.Split(string(stdout), "\n") { + line = strings.TrimSpace(line) + matches := lineRegex.FindStringSubmatch(line) + if matches == nil { + continue + } + + devName := matches[1] + attrs := matches[2] + + numMatch := numRegex.FindStringSubmatch(devName) + if numMatch == nil { + continue + } + partNum, err := strconv.Atoi(numMatch[1]) + if err != nil { + continue + } + + p := partitioners.Partition{} + p.Number = partNum + + for _, field := range strings.Split(attrs, ",") { + field = strings.TrimSpace(field) + kv := strings.SplitN(field, "=", 2) + if len(kv) != 2 { + continue + } + key := strings.TrimSpace(kv[0]) + value := strings.TrimSpace(kv[1]) + + switch key { + case "start": + v, err := strconv.ParseInt(value, 10, 64) + if err == nil { + p.StartSector = &v + } + case "size": + v, err := strconv.ParseInt(value, 10, 64) + if err == nil { + p.SizeInSectors = &v + } + case "type": + p.TypeGUID = util.StrToPtr(value) + case "uuid": + p.GUID = util.StrToPtr(value) + case "name": + value = strings.Trim(value, "\"") + p.Label = util.StrToPtr(value) + } + } + + partitions = append(partitions, p) + } + + return partitions, nil +} + +// writePartitionLine writes a single partition line to the script buffer. +// fillEnd is the last sector available for fill-remaining (exclusive of the +// next partition); pass -1 to use sfdisk's default size=+. +func writePartitionLine(script *bytes.Buffer, p partitioners.Partition, fillEnd int64) { + var line bytes.Buffer + + if p.Number > 0 { + fmt.Fprintf(&line, "%d :", p.Number) + } else { + line.WriteString(":") + } + + if p.StartSector != nil && *p.StartSector != 0 { + fmt.Fprintf(&line, " start=%d,", *p.StartSector) + } + + if p.SizeInSectors != nil && *p.SizeInSectors != 0 { + fmt.Fprintf(&line, " size=%d,", *p.SizeInSectors) + } else if fillEnd >= 0 && p.StartSector != nil && *p.StartSector > 0 { + fmt.Fprintf(&line, " size=%d,", fillEnd-*p.StartSector+1) + } else { + line.WriteString(" size=+,") + } + + if util.NotEmpty(p.TypeGUID) { + fmt.Fprintf(&line, " type=%s,", *p.TypeGUID) + } + + if util.NotEmpty(p.GUID) { + fmt.Fprintf(&line, " uuid=%s,", *p.GUID) + } + + if p.Label != nil { + fmt.Fprintf(&line, " name=\"%s\",", *p.Label) + } + + script.WriteString(strings.TrimSuffix(line.String(), ",")) + script.WriteString("\n") +} + +func isFillRemaining(p partitioners.Partition) bool { + return p.SizeInSectors == nil || *p.SizeInSectors == 0 +} + +// buildCompleteScript constructs an sfdisk script from the given partition list. +// Numbered partitions (Number > 0) are written first in ascending order, +// followed by auto-numbered partitions (Number == 0). +func buildCompleteScript(partitions []partitioners.Partition, lastLBA int64) string { + script := &bytes.Buffer{} + script.WriteString("label: gpt\n") + script.WriteString("grain: 512\n") + if lastLBA >= 0 { + fmt.Fprintf(script, "last-lba: %d\n", lastLBA) + } + script.WriteString("\n") + + sorted := make([]partitioners.Partition, len(partitions)) + copy(sorted, partitions) + sort.SliceStable(sorted, func(i, j int) bool { + if sorted[i].Number == 0 { + return false + } + if sorted[j].Number == 0 { + return true + } + return sorted[i].Number < sorted[j].Number + }) + + for i, p := range sorted { + // For fill-remaining partitions, compute the end sector as the + // start of the next partition minus 1, or lastLBA for the last. + fillEnd := lastLBA + if isFillRemaining(p) { + for _, next := range sorted[i+1:] { + if next.StartSector != nil && *next.StartSector > 0 { + fillEnd = *next.StartSector - 1 + break + } + } + // When start is unset, infer it from the previous partition + // so we can compute an explicit size. + if p.StartSector == nil || *p.StartSector == 0 { + inferredStart := int64(2048) + for j := i - 1; j >= 0; j-- { + prev := sorted[j] + if prev.StartSector != nil && *prev.StartSector > 0 && prev.SizeInSectors != nil && *prev.SizeInSectors > 0 { + inferredStart = *prev.StartSector + *prev.SizeInSectors + break + } + } + p.StartSector = &inferredStart + } + } + writePartitionLine(script, p, fillEnd) + } + + return script.String() +} + +// Pretend is like Commit() but uses the --no-act flag and returns the output +// on stdout for parsing. +func (op *Operation) Pretend() (string, error) { + merged, err := op.mergePartitions() + if err != nil { + return "", err + } + + op.lastLBA = op.getLastLBA() + scriptContent := buildCompleteScript(merged, op.lastLBA) + + if op.logger != nil { + op.logger.Info("running sfdisk --no-act with script:\n%s", scriptContent) + } + + cmd := exec.Command(distro.SfdiskCmd(), "--no-act", "--wipe-partitions", "always", "-X", "gpt", op.dev) + cmd.Stdin = strings.NewReader(scriptContent) + + stdout, err := cmd.StdoutPipe() + if err != nil { + return "", err + } + stderr, err := cmd.StderrPipe() + if err != nil { + return "", err + } + + if err := cmd.Start(); err != nil { + return "", err + } + + output, err := io.ReadAll(stdout) + if err != nil { + return "", err + } + + errors, err := io.ReadAll(stderr) + if err != nil { + return "", err + } + + if err := cmd.Wait(); err != nil { + return "", fmt.Errorf("failed to pretend to create partitions. Err: %v. Stderr: %v", err, string(errors)) + } + + return string(output), nil +} + +// Commit commits a partitioning operation. +func (op *Operation) Commit() error { + if op.wipe { + if op.logger != nil { + op.logger.Info("wiping partition table on %q", op.dev) + } + cmd := exec.Command(distro.SfdiskCmd(), "--wipe", "always", "--label", "gpt", op.dev) + cmd.Stdin = strings.NewReader("label: gpt\n") + if output, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("failed to wipe partition table on %q: %v: %s", op.dev, err, string(output)) + } + } + + merged, err := op.mergePartitions() + if err != nil { + return err + } + + if len(merged) == 0 { + return nil + } + + if err := op.handleInfo(); err != nil { + return err + } + + lastLBA := op.getLastLBA() + scriptContent := buildCompleteScript(merged, lastLBA) + + if op.logger != nil { + op.logger.Info("running sfdisk with script:\n%s", scriptContent) + } + + cmd := exec.Command(distro.SfdiskCmd(), "--wipe", "auto", "--wipe-partitions", "always", "-X", "gpt", op.dev) + cmd.Stdin = strings.NewReader(scriptContent) + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("sfdisk failed on %q: %v: %s", op.dev, err, string(output)) + } + + return nil +} + +// mergePartitions builds the final partition list by reading existing partitions +// from disk (unless wiped), applying deletions, then applying creations. +// Auto-numbered partitions (Number == 0) are always appended; numbered +// partitions replace any existing entry with the same number. +func (op *Operation) mergePartitions() ([]partitioners.Partition, error) { + var merged []partitioners.Partition + + if !op.wipe { + existing, err := op.readExistingPartitions() + if err != nil { + return nil, err + } + if op.logger != nil { + op.logger.Info("readExistingPartitions returned %d partitions for %q", len(existing), op.dev) + for _, p := range existing { + op.logger.Info(" existing partition %d: start=%v size=%v label=%v guid=%v type=%v", + p.Number, p.StartSector, p.SizeInSectors, p.Label, p.GUID, p.TypeGUID) + } + } + merged = existing + } + + if op.logger != nil { + op.logger.Info("deletions: %v, op.parts count: %d", op.deletions, len(op.parts)) + } + + for _, delNum := range op.deletions { + var filtered []partitioners.Partition + for _, p := range merged { + if p.Number != delNum { + filtered = append(filtered, p) + } + } + merged = filtered + } + + for _, p := range op.parts { + if p.Number == 0 { + merged = append(merged, p) + } else { + replaced := false + for i := range merged { + if merged[i].Number == p.Number { + merged[i] = p + replaced = true + break + } + } + if !replaced { + merged = append(merged, p) + } + } + } + + if op.logger != nil { + op.logger.Info("mergePartitions result: %d partitions for %q", len(merged), op.dev) + for _, p := range merged { + op.logger.Info(" merged partition %d: start=%v size=%v label=%v", + p.Number, p.StartSector, p.SizeInSectors, p.Label) + } + } + + return merged, nil +} + +// ParseOutput parses the table-format output from sfdisk (the "New situation:" +// section after --no-act) and returns a map of partition number to Output. +func (op *Operation) ParseOutput(sfdiskOutput string, partitionNumbers []int) (map[int]partitioners.Output, error) { + // Check for error/failure keywords only in the preamble before the + // partition table. The table itself can contain type names like + // "Linux filesystem" that are harmless, and user-supplied partition + // labels could also contain "error" or "failed". + preamble := sfdiskOutput + if idx := strings.Index(sfdiskOutput, "Device"); idx >= 0 { + preamble = sfdiskOutput[:idx] + } + lower := strings.ToLower(preamble) + if strings.Contains(lower, "failed") || strings.Contains(lower, "error") { + return nil, fmt.Errorf("%w: %s", sharedErrors.ErrBadSfdiskPretend, sfdiskOutput) + } + + result := make(map[int]partitioners.Output) + + // Match partition table lines: device path, optional boot flag, start, end, sectors + partitionRegex := regexp.MustCompile(`^/\S+\s+\*?\s*(\d+)\s+(\d+)\s+(\d+)\s+`) + + numRegex := regexp.MustCompile(`^(/\S+)`) + devNumRegex := regexp.MustCompile(`(\d+)$`) + + for _, line := range strings.Split(sfdiskOutput, "\n") { + line = strings.TrimSpace(line) + matches := partitionRegex.FindStringSubmatch(line) + if matches == nil { + continue + } + + // Get device name to extract partition number + devMatch := numRegex.FindStringSubmatch(line) + if devMatch == nil { + continue + } + devName := devMatch[1] + partNumMatch := devNumRegex.FindStringSubmatch(devName) + if partNumMatch == nil { + continue + } + partNum, err := strconv.Atoi(partNumMatch[1]) + if err != nil { + continue + } + + start, err := strconv.ParseInt(matches[1], 10, 64) + if err != nil { + continue + } + + end, err := strconv.ParseInt(matches[2], 10, 64) + if err != nil { + continue + } + + size := end - start + 1 + + // sfdisk's fill-remaining (size=+) ends 1 sector before lastLBA. + // Correct this to match sgdisk which fills to the exact lastLBA. + if op.lastLBA > 0 && end == op.lastLBA-1 { + size++ + } + + result[partNum] = partitioners.Output{ + Start: start, + Size: size, + } + } + + return result, nil +} + +// handleInfo logs partition information for each requested partition number. +func (op *Operation) handleInfo() error { + if len(op.infos) == 0 { + return nil + } + + cmd := exec.Command(distro.SfdiskCmd(), "--list", op.dev) + stdout, err := cmd.Output() + if err != nil { + return fmt.Errorf("sfdisk --list failed on %q: %v", op.dev, err) + } + + if op.logger != nil { + op.logger.Info("partition info for %q (requested partitions %v):\n%s", op.dev, op.infos, string(stdout)) + } + + return nil +} diff --git a/internal/partitioners/sfdisk/sfdisk_test.go b/internal/partitioners/sfdisk/sfdisk_test.go new file mode 100644 index 000000000..8de70d516 --- /dev/null +++ b/internal/partitioners/sfdisk/sfdisk_test.go @@ -0,0 +1,314 @@ +// Copyright 2024 Red Hat, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package sfdisk + +import ( + "bytes" + "errors" + "strings" + "testing" + + sharedErrors "github.com/coreos/ignition/v2/config/shared/errors" + "github.com/coreos/ignition/v2/internal/partitioners" +) + +func TestParseOutputSinglePartition(t *testing.T) { + op := Begin(nil, "") + + sfdiskOutput := `Disk /dev/vda: 2 GiB, 2147483648 bytes, 4194304 sectors +Units: sectors of 1 * 512 = 512 bytes +Sector size (logical/physical): 512 bytes / 512 bytes +I/O size (minimum/optimal): 512 bytes / 512 bytes + +>>> Created a new GPT disklabel. +/dev/vda1: Created a new partition 1 of type 'Linux filesystem' and of size 32 MiB. +/dev/vda2: Done. + +New situation: +Disklabel type: gpt + +Device Start End Sectors Size Type +/dev/vda1 2048 67583 65536 32M Linux filesystem + +The partition table is unchanged (--no-act).` + + result, err := op.ParseOutput(sfdiskOutput, []int{1}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if len(result) != 1 { + t.Fatalf("expected 1 partition, got %d", len(result)) + } + + assertOutput(t, result, 1, partitioners.Output{Start: 2048, Size: 65536}) +} + +func TestParseOutputTwoPartitions(t *testing.T) { + op := Begin(nil, "") + + sfdiskOutput := `Disk /dev/vda: 2 GiB, 2147483648 bytes, 4194304 sectors + +New situation: +Disklabel type: gpt + +Device Start End Sectors Size Type +/dev/vda1 2048 67583 65536 32M Linux filesystem +/dev/vda2 67584 133119 65536 32M Linux filesystem + +The partition table is unchanged (--no-act).` + + result, err := op.ParseOutput(sfdiskOutput, []int{1, 2}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if len(result) != 2 { + t.Fatalf("expected 2 partitions, got %d", len(result)) + } + + assertOutput(t, result, 1, partitioners.Output{Start: 2048, Size: 65536}) + assertOutput(t, result, 2, partitioners.Output{Start: 67584, Size: 65536}) +} + +func TestParseOutputError(t *testing.T) { + op := Begin(nil, "") + + sfdiskOutput := `/dev/vda1: Start sector 0 out of range. +Failed to add #1 partition: Numerical result out of range` + + _, err := op.ParseOutput(sfdiskOutput, []int{1}) + if err == nil { + t.Fatal("expected error, got nil") + } + + if !errors.Is(err, sharedErrors.ErrBadSfdiskPretend) { + t.Fatalf("expected error wrapping ErrBadSfdiskPretend, got: %v", err) + } +} + +func TestParseOutputNoFalsePositiveOnTableContent(t *testing.T) { + op := Begin(nil, "") + + sfdiskOutput := `Disk /dev/vda: 2 GiB, 2147483648 bytes, 4194304 sectors + +New situation: +Disklabel type: gpt + +Device Start End Sectors Size Type +/dev/vda1 2048 67583 65536 32M Linux filesystem + +The partition table is unchanged (--no-act).` + + result, err := op.ParseOutput(sfdiskOutput, []int{1}) + if err != nil { + t.Fatalf("unexpected error (false positive on table content): %v", err) + } + + assertOutput(t, result, 1, partitioners.Output{Start: 2048, Size: 65536}) +} + +func TestBuildCompleteScriptFillRemaining(t *testing.T) { + p1 := partitioners.Partition{StartSector: int64Ptr(2048), SizeInSectors: int64Ptr(65536)} + p1.Number = 1 + p2 := partitioners.Partition{StartSector: int64Ptr(67584), SizeInSectors: int64Ptr(0)} + p2.Number = 2 + + script := buildCompleteScript([]partitioners.Partition{p1, p2}, 204766) + + if !strings.Contains(script, "last-lba: 204766") { + t.Errorf("expected script to contain last-lba header, got:\n%s", script) + } + // Fill-remaining partition 2 starts at 67584, lastLBA=204766: 204766-67584+1=137183 + if expected := "size=137183"; !strings.Contains(script, expected) { + t.Errorf("expected script to contain %q for fill-remaining partition, got:\n%s", expected, script) + } +} + +func TestBuildCompleteScriptFillRemainingUnknownLBA(t *testing.T) { + p1 := partitioners.Partition{StartSector: int64Ptr(67584), SizeInSectors: int64Ptr(0)} + p1.Number = 1 + + script := buildCompleteScript([]partitioners.Partition{p1}, -1) + + if strings.Contains(script, "last-lba:") { + t.Errorf("should not contain last-lba header when unknown, got:\n%s", script) + } + if !strings.Contains(script, "size=+") { + t.Errorf("expected size=+ when lastLBA is unknown, got:\n%s", script) + } +} + +func TestParseOutputNvmeDevice(t *testing.T) { + op := Begin(nil, "") + + sfdiskOutput := `Disk /dev/nvme0n1: 100 GiB, 107374182400 bytes, 209715200 sectors + +New situation: +Disklabel type: gpt + +Device Start End Sectors Size Type +/dev/nvme0n1p1 2048 67583 65536 32M Linux filesystem +/dev/nvme0n1p2 67584 133119 65536 32M Linux filesystem + +The partition table is unchanged (--no-act).` + + result, err := op.ParseOutput(sfdiskOutput, []int{1, 2}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + assertOutput(t, result, 1, partitioners.Output{Start: 2048, Size: 65536}) + assertOutput(t, result, 2, partitioners.Output{Start: 67584, Size: 65536}) +} + +func TestParseOutputWithBootFlag(t *testing.T) { + op := Begin(nil, "") + + sfdiskOutput := `Disk /dev/sda: 50 GiB + +New situation: +Disklabel type: gpt + +Device Start End Sectors Size Type +/dev/sda1 * 2048 67583 65536 32M EFI System +/dev/sda2 67584 133119 65536 32M Linux filesystem + +The partition table is unchanged (--no-act).` + + result, err := op.ParseOutput(sfdiskOutput, []int{1, 2}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + assertOutput(t, result, 1, partitioners.Output{Start: 2048, Size: 65536}) + assertOutput(t, result, 2, partitioners.Output{Start: 67584, Size: 65536}) +} + +func TestParseOutputEmptyPartitions(t *testing.T) { + op := Begin(nil, "") + result, err := op.ParseOutput("anything", []int{}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(result) != 0 { + t.Fatalf("expected empty result for empty partitionNumbers, got %v", result) + } +} + +func TestBuildCompleteScriptMixedNumbering(t *testing.T) { + p1 := partitioners.Partition{StartSector: int64Ptr(2048), SizeInSectors: int64Ptr(65536)} + p1.Number = 1 + p3 := partitioners.Partition{StartSector: int64Ptr(67584), SizeInSectors: int64Ptr(65536)} + p3.Number = 3 + p0 := partitioners.Partition{SizeInSectors: int64Ptr(65536)} + p0.Number = 0 + + script := buildCompleteScript([]partitioners.Partition{p1, p3, p0}, -1) + + lines := strings.Split(script, "\n") + var num1Idx, num3Idx, autoIdx int + num1Idx, num3Idx, autoIdx = -1, -1, -1 + for i, line := range lines { + trimmed := strings.TrimSpace(line) + if strings.HasPrefix(trimmed, "1 :") { + num1Idx = i + } + if strings.HasPrefix(trimmed, "3 :") { + num3Idx = i + } + if trimmed == ":" || strings.HasPrefix(trimmed, ": ") { + autoIdx = i + } + } + + if num1Idx < 0 || num3Idx < 0 || autoIdx < 0 { + t.Fatalf("missing partition lines in script:\n%s", script) + } + if num1Idx >= num3Idx || num3Idx >= autoIdx { + t.Errorf("expected partition order: 1, 3, auto-numbered; got lines %d, %d, %d in script:\n%s", + num1Idx, num3Idx, autoIdx, script) + } +} + +func TestBuildCompleteScriptMultipleAutoNumbered(t *testing.T) { + p1 := partitioners.Partition{SizeInSectors: int64Ptr(65536)} + p1.Number = 0 + p1.Label = strPtr("uno") + p2 := partitioners.Partition{SizeInSectors: int64Ptr(65536)} + p2.Number = 0 + p2.Label = strPtr("dos") + p3 := partitioners.Partition{SizeInSectors: int64Ptr(65536)} + p3.Number = 0 + p3.Label = strPtr("tres") + + script := buildCompleteScript([]partitioners.Partition{p1, p2, p3}, -1) + + if !strings.Contains(script, `name="uno"`) { + t.Errorf("missing partition 'uno' in script:\n%s", script) + } + if !strings.Contains(script, `name="dos"`) { + t.Errorf("missing partition 'dos' in script:\n%s", script) + } + if !strings.Contains(script, `name="tres"`) { + t.Errorf("missing partition 'tres' in script:\n%s", script) + } + // Count the number of partition lines (lines starting with ":") + count := 0 + for _, line := range strings.Split(script, "\n") { + if strings.HasPrefix(strings.TrimSpace(line), ":") { + count++ + } + } + if count != 3 { + t.Errorf("expected 3 auto-numbered partition lines, got %d in script:\n%s", count, script) + } +} + +func TestWritePartitionLineMinimal(t *testing.T) { + p := partitioners.Partition{} + p.Number = 1 + + var buf bytes.Buffer + writePartitionLine(&buf, p, -1) + line := buf.String() + + if !strings.HasPrefix(line, "1 :") { + t.Errorf("expected line to start with '1 :', got %q", line) + } + if !strings.Contains(line, "size=+") { + t.Errorf("expected size=+ for partition with no size, got %q", line) + } + if !strings.HasSuffix(line, "\n") { + t.Errorf("expected line to end with newline, got %q", line) + } +} + +func int64Ptr(v int64) *int64 { return &v } +func strPtr(v string) *string { return &v } + +func assertOutput(t *testing.T, result map[int]partitioners.Output, num int, expected partitioners.Output) { + t.Helper() + out, ok := result[num] + if !ok { + t.Fatalf("partition %d not found in result", num) + } + if out.Start != expected.Start { + t.Errorf("partition %d: expected start %d, got %d", num, expected.Start, out.Start) + } + if out.Size != expected.Size { + t.Errorf("partition %d: expected size %d, got %d", num, expected.Size, out.Size) + } +} diff --git a/internal/sgdisk/sgdisk.go b/internal/partitioners/sgdisk/sgdisk.go similarity index 56% rename from internal/sgdisk/sgdisk.go rename to internal/partitioners/sgdisk/sgdisk.go index ffd3d9caa..6604b4e28 100644 --- a/internal/sgdisk/sgdisk.go +++ b/internal/partitioners/sgdisk/sgdisk.go @@ -15,44 +15,38 @@ package sgdisk import ( + "errors" "fmt" "io" "os/exec" + "regexp" + "strconv" + "strings" "github.com/coreos/ignition/v2/config/util" - "github.com/coreos/ignition/v2/config/v3_7_experimental/types" "github.com/coreos/ignition/v2/internal/distro" "github.com/coreos/ignition/v2/internal/log" + "github.com/coreos/ignition/v2/internal/partitioners" ) +var ErrBadSgdiskOutput = errors.New("sgdisk had unexpected output") + type Operation struct { logger *log.Logger dev string wipe bool - parts []Partition + parts []partitioners.Partition deletions []int infos []int } -// We ignore types.Partition.StartMiB/SizeMiB in favor of -// StartSector/SizeInSectors. The caller is expected to do the conversion. -type Partition struct { - types.Partition - StartSector *int64 - SizeInSectors *int64 - - // shadow StartMiB/SizeMiB so they're not accidentally used - StartMiB string - SizeMiB string -} - // Begin begins an sgdisk operation func Begin(logger *log.Logger, dev string) *Operation { return &Operation{logger: logger, dev: dev} } // CreatePartition adds the supplied partition to the list of partitions to be created as part of an operation. -func (op *Operation) CreatePartition(p Partition) { +func (op *Operation) CreatePartition(p partitioners.Partition) { op.parts = append(op.parts, p) } @@ -125,6 +119,83 @@ func (op *Operation) Commit() error { return nil } +// NeedsPartx returns true because sgdisk does not trigger the update of the +// kernel partition table with BLKPG but only uses BLKRRPART which fails +// as soon as one partition of the disk is mounted. +func (op *Operation) NeedsPartx() bool { + return true +} + +func (op *Operation) WritesCompleteTable() bool { + return false +} + +// ParseOutput parses the output of running sgdisk pretend with --info specified for each partition +// number specified in partitionNumbers. E.g. if partitionNumbers is [1,4,5], it is expected that the sgdisk +// output was from running `sgdisk --pretend --info=1 --info=4 --info=5`. It assumes the +// partition labels are well behaved (i.e. contain no control characters). It returns a map of partition +// numbers to Output structs with the start and size information as determined by sgdisk. +// The partition numbers need to be passed in because sgdisk includes them in its output. +func (op *Operation) ParseOutput(sgdiskOut string, partitionNumbers []int) (map[int]partitioners.Output, error) { + if len(partitionNumbers) == 0 { + return nil, nil + } + startRegex := regexp.MustCompile(`^First sector: (\d*) \(.*\)$`) + endRegex := regexp.MustCompile(`^Last sector: (\d*) \(.*\)$`) + const ( + START = iota + END = iota + FAIL_ON_START_END = iota + ) + + output := map[int]partitioners.Output{} + state := START + current := partitioners.Output{} + i := 0 + + lines := strings.Split(sgdiskOut, "\n") + for _, line := range lines { + switch state { + case START: + start, err := parseLine(startRegex, line) + if err != nil { + return nil, err + } + if start != -1 { + current.Start = start + state = END + } + case END: + end, err := parseLine(endRegex, line) + if err != nil { + return nil, err + } + if end != -1 { + current.Size = 1 + end - current.Start + output[partitionNumbers[i]] = current + i++ + if i == len(partitionNumbers) { + state = FAIL_ON_START_END + } else { + current = partitioners.Output{} + state = START + } + } + case FAIL_ON_START_END: + if len(startRegex.FindStringSubmatch(line)) != 0 || + len(endRegex.FindStringSubmatch(line)) != 0 { + return nil, ErrBadSgdiskOutput + } + } + } + + if state != FAIL_ON_START_END { + return nil, ErrBadSgdiskOutput + } + + return output, nil +} + func (op Operation) buildOptions() []string { opts := []string{} @@ -162,16 +233,31 @@ func (op Operation) buildOptions() []string { return opts } -func partitionGetStart(p Partition) string { +func partitionGetStart(p partitioners.Partition) string { if p.StartSector != nil { return fmt.Sprintf("%d", *p.StartSector) } return "0" } -func partitionGetSize(p Partition) string { +func partitionGetSize(p partitioners.Partition) string { if p.SizeInSectors != nil { return fmt.Sprintf("%d", *p.SizeInSectors) } return "0" } + +// parseLine takes a regexp that captures an int64 and a string to match on. On success it returns +// the captured int64 and nil. If the regexp does not match it returns -1 and nil. If it encountered +// an error it returns 0 and the error. +func parseLine(r *regexp.Regexp, line string) (int64, error) { + matches := r.FindStringSubmatch(line) + switch len(matches) { + case 0: + return -1, nil + case 2: + return strconv.ParseInt(matches[1], 10, 64) + default: + return 0, ErrBadSgdiskOutput + } +} diff --git a/tests/manual/compare-partitioners.sh b/tests/manual/compare-partitioners.sh new file mode 100644 index 000000000..fbf9fff5e --- /dev/null +++ b/tests/manual/compare-partitioners.sh @@ -0,0 +1,160 @@ +#!/bin/bash +# Compare sgdisk and sfdisk partition output for identical Ignition configs. +# Must run as root on a system with loop device support. +# Usage: sudo ./tests/manual/compare-partitioners.sh [path-to-ignition-binary] +set -euo pipefail + +IGNITION="${1:-./bin/amd64/ignition}" +IMG_SIZE=200M +PASS=0 +FAIL=0 + +if [ "$(id -u)" -ne 0 ]; then + echo "ERROR: must run as root" >&2 + exit 1 +fi + +if ! command -v sgdisk &>/dev/null || ! command -v sfdisk &>/dev/null; then + echo "ERROR: both sgdisk and sfdisk must be installed" >&2 + exit 1 +fi + +tmpdir=$(mktemp -d /var/tmp/partitioner-compare.XXXXXX) +trap 'rm -rf "$tmpdir"' EXIT + +dump_partition_table() { + local dev="$1" + # Normalize output: partition number, start, size, type GUID, partition GUID, label + sfdisk --dump "$dev" 2>/dev/null | grep "^/dev/" | while read -r line; do + local start size type uuid name + start=$(echo "$line" | sed -n 's/.*start=\s*\([0-9]*\).*/\1/p') + size=$(echo "$line" | sed -n 's/.*size=\s*\([0-9]*\).*/\1/p') + type=$(echo "$line" | sed -n 's/.*type=\s*\([^ ,]*\).*/\1/p') + uuid=$(echo "$line" | sed -n 's/.*uuid=\s*\([^ ,]*\).*/\1/p') + name=$(echo "$line" | sed -n 's/.*name="\([^"]*\)".*/\1/p') + echo "start=$start size=$size type=$type name=$name" + done +} + +run_test() { + local test_name="$1" + local config="$2" + local setup_script="${3:-}" + + echo "=== TEST: $test_name ===" + + # --- sgdisk run --- + local img_sg="$tmpdir/sgdisk.img" + truncate -s "$IMG_SIZE" "$img_sg" + local loop_sg + loop_sg=$(losetup --find --show "$img_sg") + trap "losetup -d $loop_sg 2>/dev/null; rm -f $img_sg" RETURN + + if [ -n "$setup_script" ]; then + eval "$setup_script" "$loop_sg" + fi + + local cfg_sg + cfg_sg=$(echo "$config" | sed "s|\\\$disk|$loop_sg|g") + local cwd_sg="$tmpdir/cwd-sg" + mkdir -p "$cwd_sg" + echo "$cfg_sg" > "$cwd_sg/ignition.json" + touch "$cwd_sg/neednet" "$cwd_sg/state" + + IGNITION_PARTITIONER=sgdisk "$IGNITION" \ + -platform file -stage disks -root "$tmpdir/root-sg" \ + -log-to-stdout -config-cache "$cwd_sg/ignition.json" \ + -neednet "$cwd_sg/neednet" -state-file "$cwd_sg/state" \ + 2>&1 | tail -5 || true + + local sg_result + sg_result=$(dump_partition_table "$loop_sg") + losetup -d "$loop_sg" + + # --- sfdisk run --- + local img_sf="$tmpdir/sfdisk.img" + truncate -s "$IMG_SIZE" "$img_sf" + local loop_sf + loop_sf=$(losetup --find --show "$img_sf") + + if [ -n "$setup_script" ]; then + eval "$setup_script" "$loop_sf" + fi + + local cfg_sf + cfg_sf=$(echo "$config" | sed "s|\\\$disk|$loop_sf|g") + local cwd_sf="$tmpdir/cwd-sf" + mkdir -p "$cwd_sf" + echo "$cfg_sf" > "$cwd_sf/ignition.json" + touch "$cwd_sf/neednet" "$cwd_sf/state" + + IGNITION_PARTITIONER=sfdisk "$IGNITION" \ + -platform file -stage disks -root "$tmpdir/root-sf" \ + -log-to-stdout -config-cache "$cwd_sf/ignition.json" \ + -neednet "$cwd_sf/neednet" -state-file "$cwd_sf/state" \ + 2>&1 | tail -5 || true + + local sf_result + sf_result=$(dump_partition_table "$loop_sf") + losetup -d "$loop_sf" + + # --- Compare --- + if [ "$sg_result" = "$sf_result" ]; then + echo " PASS: partition tables match" + echo " Result: $sg_result" + ((PASS++)) + else + echo " FAIL: partition tables differ" + echo " sgdisk: $sg_result" + echo " sfdisk: $sf_result" + ((FAIL++)) + fi + echo + + # Cleanup for this test + rm -f "$img_sg" "$img_sf" + rm -rf "$cwd_sg" "$cwd_sf" "$tmpdir/root-sg" "$tmpdir/root-sf" +} + +# --- Test 1: Simple partition creation --- +run_test "create single partition" '{ + "ignition": {"version": "3.4.0"}, + "storage": {"disks": [{"device": "$disk", "partitions": [ + {"number": 1, "sizeMiB": 32, "label": "testpart", + "typeGuid": "0FC63DAF-8483-4772-8E79-3D69D8477DE4"} + ]}]} +}' + +# --- Test 2: Multiple partitions --- +run_test "create two partitions" '{ + "ignition": {"version": "3.4.0"}, + "storage": {"disks": [{"device": "$disk", "partitions": [ + {"number": 1, "sizeMiB": 32, "label": "part1", + "typeGuid": "0FC63DAF-8483-4772-8E79-3D69D8477DE4"}, + {"number": 2, "sizeMiB": 32, "label": "part2", + "typeGuid": "0FC63DAF-8483-4772-8E79-3D69D8477DE4"} + ]}]} +}' + +# --- Test 3: Wipe table and create --- +run_test "wipe table then create" '{ + "ignition": {"version": "3.4.0"}, + "storage": {"disks": [{"device": "$disk", "wipeTable": true, "partitions": [ + {"number": 1, "sizeMiB": 32, "label": "fresh", + "typeGuid": "0FC63DAF-8483-4772-8E79-3D69D8477DE4"} + ]}]} +}' 'sgdisk --new=1:2048:+65536 --change-name=1:old' + +# --- Test 4: Fill remaining space (sizeMiB 0) --- +run_test "fill remaining space" '{ + "ignition": {"version": "3.4.0"}, + "storage": {"disks": [{"device": "$disk", "partitions": [ + {"number": 1, "sizeMiB": 32, "label": "fixed", + "typeGuid": "0FC63DAF-8483-4772-8E79-3D69D8477DE4"}, + {"number": 2, "sizeMiB": 0, "startMiB": 0, "label": "rest", + "typeGuid": "0FC63DAF-8483-4772-8E79-3D69D8477DE4"} + ]}]} +}' + +echo "=== SUMMARY: $PASS passed, $FAIL failed ===" +[ "$FAIL" -eq 0 ] diff --git a/tests/validator.go b/tests/validator.go index 9e837357e..e9a8d6270 100644 --- a/tests/validator.go +++ b/tests/validator.go @@ -42,25 +42,42 @@ func regexpSearch(itemName, pattern string, data []byte) (string, error) { func getPartitionSet(device string) (map[int]struct{}, error) { sgdiskOverview, err := exec.Command("sgdisk", "-p", device).CombinedOutput() - if err != nil { - return nil, fmt.Errorf("sgdisk -p %s failed: %v", device, err) + if err == nil { + //What this regex means: num start end size,code,name + re := regexp.MustCompile("\n\\W+(\\d+)\\W+\\d+\\W+\\d+\\W+\\d+.*") + ret := map[int]struct{}{} + for _, match := range re.FindAllStringSubmatch(string(sgdiskOverview), -1) { + if len(match) == 0 { + continue + } + if len(match) != 2 { + return nil, fmt.Errorf("invalid regex result from parsing sgdisk") + } + num, err := strconv.Atoi(match[1]) + if err != nil { + return nil, err + } + ret[num] = struct{}{} + } + return ret, nil } - //What this regex means: num start end size,code,name - re := regexp.MustCompile("\n\\W+(\\d+)\\W+\\d+\\W+\\d+\\W+\\d+.*") + // Fall back to sfdisk --list + sfdiskOverview, err := exec.Command("sfdisk", "--list", device).CombinedOutput() + if err != nil { + return nil, fmt.Errorf("neither sgdisk -p nor sfdisk --list %s succeeded", device) + } + re := regexp.MustCompile(`^` + regexp.QuoteMeta(device) + `\S*?(\d+)\s+`) ret := map[int]struct{}{} - for _, match := range re.FindAllStringSubmatch(string(sgdiskOverview), -1) { - if len(match) == 0 { - continue - } - if len(match) != 2 { - return nil, fmt.Errorf("invalid regex result from parsing sgdisk") - } - num, err := strconv.Atoi(match[1]) - if err != nil { - return nil, err + for _, line := range strings.Split(string(sfdiskOverview), "\n") { + match := re.FindStringSubmatch(line) + if match != nil { + num, err := strconv.Atoi(match[1]) + if err != nil { + continue + } + ret[num] = struct{}{} } - ret[num] = struct{}{} } return ret, nil } @@ -81,27 +98,7 @@ func validateDisk(t *testing.T, d types.Disk) error { } delete(partitionSet, e.Number) - sgdiskInfo, err := exec.Command( - "sgdisk", "-i", strconv.Itoa(e.Number), - d.Device).CombinedOutput() - if err != nil { - t.Error("sgdisk -i", strconv.Itoa(e.Number), err) - return nil - } - - actualGUID, err := regexpSearch("GUID", "Partition unique GUID: (?P[\\d\\w-]+)", sgdiskInfo) - if err != nil { - return err - } - actualTypeGUID, err := regexpSearch("type GUID", "Partition GUID code: (?P[\\d\\w-]+)", sgdiskInfo) - if err != nil { - return err - } - actualSectors, err := regexpSearch("partition size", "Partition size: (?P\\d+) sectors", sgdiskInfo) - if err != nil { - return err - } - actualLabel, err := regexpSearch("partition name", "Partition name: '(?P[\\d\\w-_]+)'", sgdiskInfo) + actualGUID, actualTypeGUID, actualSectors, actualLabel, err := getPartitionInfo(d.Device, e.Number) if err != nil { return err } @@ -135,6 +132,54 @@ func validateDisk(t *testing.T, d types.Disk) error { return nil } +func getPartitionInfo(device string, partNum int) (guid, typeGUID, sectors, label string, err error) { + numStr := strconv.Itoa(partNum) + + // Try sgdisk first + sgdiskInfo, sgErr := exec.Command("sgdisk", "-i", numStr, device).CombinedOutput() + if sgErr == nil { + guid, err = regexpSearch("GUID", "Partition unique GUID: (?P[\\d\\w-]+)", sgdiskInfo) + if err != nil { + return + } + typeGUID, err = regexpSearch("type GUID", "Partition GUID code: (?P[\\d\\w-]+)", sgdiskInfo) + if err != nil { + return + } + sectors, err = regexpSearch("partition size", "Partition size: (?P\\d+) sectors", sgdiskInfo) + if err != nil { + return + } + label, err = regexpSearch("partition name", "Partition name: '(?P[\\d\\w-_]+)'", sgdiskInfo) + return + } + + // Fall back to sfdisk + if out, e := exec.Command("sfdisk", "--part-uuid", device, numStr).CombinedOutput(); e == nil { + guid = strings.TrimSpace(string(out)) + } + if out, e := exec.Command("sfdisk", "--part-type", device, numStr).CombinedOutput(); e == nil { + typeGUID = strings.TrimSpace(string(out)) + } + if out, e := exec.Command("sfdisk", "--part-label", device, numStr).CombinedOutput(); e == nil { + label = strings.TrimSpace(string(out)) + } + + // Get sectors from sfdisk --list output + listOut, listErr := exec.Command("sfdisk", "--list", device).CombinedOutput() + if listErr != nil { + err = fmt.Errorf("neither sgdisk -i nor sfdisk commands succeeded for partition %d on %s", partNum, device) + return + } + re := regexp.MustCompile(regexp.QuoteMeta(device) + `\S*?` + numStr + `\s+\*?\s*\d+\s+\d+\s+(\d+)\s+`) + match := re.FindSubmatch(listOut) + if len(match) >= 2 { + sectors = string(match[1]) + } + + return +} + func formatUUID(s string) string { return strings.ToUpper(strings.ReplaceAll(s, "-", "")) }