Skip to content

Commit bba6095

Browse files
committed
TMP: Hardlink sfdisk to iterate against failing tests
1 parent 564c1ab commit bba6095

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

internal/exec/stages/disks/partitions.go

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ import (
3434
"github.com/coreos/ignition/v2/config/v3_5_experimental/types"
3535
"github.com/coreos/ignition/v2/internal/distro"
3636
"github.com/coreos/ignition/v2/internal/exec/util"
37-
"github.com/coreos/ignition/v2/internal/sgdisk"
37+
"github.com/coreos/ignition/v2/internal/sfdisk"
3838
iutil "github.com/coreos/ignition/v2/internal/util"
3939
)
4040

4141
var (
42-
ErrBadSgdiskOutput = errors.New("sgdisk had unexpected output")
42+
ErrBadsfdiskOutput = errors.New("sfdisk had unexpected output")
4343
)
4444

4545
// createPartitions creates the partitions described in config.Storage.Disks.
@@ -75,7 +75,7 @@ func (s stage) createPartitions(config types.Config) error {
7575

7676
// partitionMatches determines if the existing partition matches the spec given. See doc/operator notes for what
7777
// what it means for an existing partition to match the spec. spec must have non-zero Start and Size.
78-
func partitionMatches(existing util.PartitionInfo, spec sgdisk.Partition) error {
78+
func partitionMatches(existing util.PartitionInfo, spec sfdisk.Partition) error {
7979
if err := partitionMatchesCommon(existing, spec); err != nil {
8080
return err
8181
}
@@ -87,13 +87,13 @@ func partitionMatches(existing util.PartitionInfo, spec sgdisk.Partition) error
8787

8888
// partitionMatchesResize returns if the existing partition should be resized by evaluating if
8989
// `resize`field is true and partition matches in all respects except size.
90-
func partitionMatchesResize(existing util.PartitionInfo, spec sgdisk.Partition) bool {
90+
func partitionMatchesResize(existing util.PartitionInfo, spec sfdisk.Partition) bool {
9191
return cutil.IsTrue(spec.Resize) && partitionMatchesCommon(existing, spec) == nil
9292
}
9393

9494
// partitionMatchesCommon handles the common tests (excluding the partition size) to determine
9595
// if the existing partition matches the spec given.
96-
func partitionMatchesCommon(existing util.PartitionInfo, spec sgdisk.Partition) error {
96+
func partitionMatchesCommon(existing util.PartitionInfo, spec sfdisk.Partition) error {
9797
if spec.Number != existing.Number {
9898
return fmt.Errorf("partition numbers did not match (specified %d, got %d). This should not happen, please file a bug.", spec.Number, existing.Number)
9999
}
@@ -113,7 +113,7 @@ func partitionMatchesCommon(existing util.PartitionInfo, spec sgdisk.Partition)
113113
}
114114

115115
// partitionShouldBeInspected returns if the partition has zeroes that need to be resolved to sectors.
116-
func partitionShouldBeInspected(part sgdisk.Partition) bool {
116+
func partitionShouldBeInspected(part sfdisk.Partition) bool {
117117
if part.Number == 0 {
118118
return false
119119
}
@@ -131,19 +131,19 @@ func convertMiBToSectors(mib *int, sectorSize int) *int64 {
131131
}
132132

133133
// getRealStartAndSize returns a map of partition numbers to a struct that contains what their real start
134-
// and end sector should be. It runs sgdisk --pretend to determine what the partitions would look like if
134+
// and end sector should be. It runs sfdisk --pretend to determine what the partitions would look like if
135135
// everything specified were to be (re)created.
136-
func (s stage) getRealStartAndSize(dev types.Disk, devAlias string, diskInfo util.DiskInfo) ([]sgdisk.Partition, error) {
137-
partitions := []sgdisk.Partition{}
136+
func (s stage) getRealStartAndSize(dev types.Disk, devAlias string, diskInfo util.DiskInfo) ([]sfdisk.Partition, error) {
137+
partitions := []sfdisk.Partition{}
138138
for _, cpart := range dev.Partitions {
139-
partitions = append(partitions, sgdisk.Partition{
139+
partitions = append(partitions, sfdisk.Partition{
140140
Partition: cpart,
141141
StartSector: convertMiBToSectors(cpart.StartMiB, diskInfo.LogicalSectorSize),
142142
SizeInSectors: convertMiBToSectors(cpart.SizeMiB, diskInfo.LogicalSectorSize),
143143
})
144144
}
145145

146-
op := sgdisk.Begin(s.Logger, devAlias)
146+
op := sfdisk.Begin(s.Logger, devAlias)
147147
for _, part := range partitions {
148148
if info, exists := diskInfo.GetPartition(part.Number); exists {
149149
// delete all existing partitions
@@ -157,8 +157,6 @@ func (s stage) getRealStartAndSize(dev types.Disk, devAlias string, diskInfo uti
157157
}
158158
}
159159
if partitionShouldExist(part) {
160-
// Clear the label. sgdisk doesn't escape control characters. This makes parsing easier
161-
part.Label = nil
162160
op.CreatePartition(part)
163161
}
164162
}
@@ -177,12 +175,12 @@ func (s stage) getRealStartAndSize(dev types.Disk, devAlias string, diskInfo uti
177175
return nil, err
178176
}
179177

180-
realDimensions, err := parseSgdiskPretend(output, partitionsToInspect)
178+
realDimensions, err := parsesfdiskPretend(output, partitionsToInspect)
181179
if err != nil {
182180
return nil, err
183181
}
184182

185-
result := []sgdisk.Partition{}
183+
result := []sfdisk.Partition{}
186184
for _, part := range partitions {
187185
if dims, ok := realDimensions[part.Number]; ok {
188186
if part.StartSector != nil {
@@ -197,7 +195,7 @@ func (s stage) getRealStartAndSize(dev types.Disk, devAlias string, diskInfo uti
197195
return result, nil
198196
}
199197

200-
type sgdiskOutput struct {
198+
type sfdiskOutput struct {
201199
start int64
202200
size int64
203201
}
@@ -213,17 +211,17 @@ func parseLine(r *regexp.Regexp, line string) (int64, error) {
213211
case 2:
214212
return strconv.ParseInt(matches[1], 10, 64)
215213
default:
216-
return 0, ErrBadSgdiskOutput
214+
return 0, ErrBadsfdiskOutput
217215
}
218216
}
219217

220-
// parseSgdiskPretend parses the output of running sgdisk pretend with --info specified for each partition
221-
// number specified in partitionNumbers. E.g. if paritionNumbers is [1,4,5], it is expected that the sgdisk
222-
// output was from running `sgdisk --pretend <commands> --info=1 --info=4 --info=5`. It assumes the the
218+
// parsesfdiskPretend parses the output of running sfdisk pretend with --info specified for each partition
219+
// number specified in partitionNumbers. E.g. if paritionNumbers is [1,4,5], it is expected that the sfdisk
220+
// output was from running `sfdisk --pretend <commands> --info=1 --info=4 --info=5`. It assumes the the
223221
// partition labels are well behaved (i.e. contain no control characters). It returns a list of partitions
224-
// matching the partition numbers specified, but with the start and size information as determined by sgdisk.
225-
// The partition numbers need to passed in because sgdisk includes them in its output.
226-
func parseSgdiskPretend(sgdiskOut string, partitionNumbers []int) (map[int]sgdiskOutput, error) {
222+
// matching the partition numbers specified, but with the start and size information as determined by sfdisk.
223+
// The partition numbers need to passed in because sfdisk includes them in its output.
224+
func parsesfdiskPretend(sfdiskOut string, partitionNumbers []int) (map[int]sfdiskOutput, error) {
227225
if len(partitionNumbers) == 0 {
228226
return nil, nil
229227
}
@@ -235,12 +233,12 @@ func parseSgdiskPretend(sgdiskOut string, partitionNumbers []int) (map[int]sgdis
235233
FAIL_ON_START_END = iota
236234
)
237235

238-
output := map[int]sgdiskOutput{}
236+
output := map[int]sfdiskOutput{}
239237
state := START
240-
current := sgdiskOutput{}
238+
current := sfdiskOutput{}
241239
i := 0
242240

243-
lines := strings.Split(sgdiskOut, "\n")
241+
lines := strings.Split(sfdiskOut, "\n")
244242
for _, line := range lines {
245243
switch state {
246244
case START:
@@ -264,29 +262,29 @@ func parseSgdiskPretend(sgdiskOut string, partitionNumbers []int) (map[int]sgdis
264262
if i == len(partitionNumbers) {
265263
state = FAIL_ON_START_END
266264
} else {
267-
current = sgdiskOutput{}
265+
current = sfdiskOutput{}
268266
state = START
269267
}
270268
}
271269
case FAIL_ON_START_END:
272270
if len(startRegex.FindStringSubmatch(line)) != 0 ||
273271
len(endRegex.FindStringSubmatch(line)) != 0 {
274-
return nil, ErrBadSgdiskOutput
272+
return nil, ErrBadsfdiskOutput
275273
}
276274
}
277275
}
278276

279277
if state != FAIL_ON_START_END {
280278
// We stopped parsing in the middle of a info block. Something is wrong
281-
return nil, ErrBadSgdiskOutput
279+
return nil, ErrBadsfdiskOutput
282280
}
283281

284282
return output, nil
285283
}
286284

287285
// partitionShouldExist returns whether a bool is indicating if a partition should exist or not.
288286
// nil (unspecified in json) is treated the same as true.
289-
func partitionShouldExist(part sgdisk.Partition) bool {
287+
func partitionShouldExist(part sfdisk.Partition) bool {
290288
return !cutil.IsFalse(part.ShouldExist)
291289
}
292290

@@ -438,17 +436,17 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error {
438436
return fmt.Errorf("refusing to operate on directly active disk %q", devAlias)
439437
}
440438
if cutil.IsTrue(dev.WipeTable) {
441-
op := sgdisk.Begin(s.Logger, devAlias)
439+
op := sfdisk.Begin(s.Logger, devAlias)
442440
s.Logger.Info("wiping partition table requested on %q", devAlias)
443441
if len(activeParts) > 0 {
444442
return fmt.Errorf("refusing to wipe active disk %q", devAlias)
445443
}
446444
op.WipeTable(true)
447-
if err := op.Commit(); err != nil {
448-
// `sgdisk --zap-all` will exit code 2 if the table was corrupted; retry it
445+
if err := op.SgdiskCommit(); err != nil {
446+
// `sfdisk --zap-all` will exit code 2 if the table was corrupted; retry it
449447
// https://github.com/coreos/fedora-coreos-tracker/issues/1596
450448
s.Logger.Info("potential error encountered while wiping table... retrying")
451-
if err := op.Commit(); err != nil {
449+
if err := op.SgdiskCommit(); err != nil {
452450
return err
453451
}
454452
}
@@ -457,7 +455,7 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error {
457455
// Ensure all partitions with number 0 are last
458456
sort.Stable(PartitionList(dev.Partitions))
459457

460-
op := sgdisk.Begin(s.Logger, devAlias)
458+
op := sfdisk.Begin(s.Logger, devAlias)
461459

462460
diskInfo, err := s.getPartitionMap(devAlias)
463461
if err != nil {
@@ -538,11 +536,11 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error {
538536
}
539537
}
540538

541-
if err := op.Commit(); err != nil {
539+
if err := op.SgdiskCommit(); err != nil {
542540
return fmt.Errorf("commit failure: %v", err)
543541
}
544542

545-
// In contrast to similar tools, sgdisk does not trigger the update of the
543+
// In contrast to similar tools, sfdisk does not trigger the update of the
546544
// kernel partition table with BLKPG but only uses BLKRRPART which fails
547545
// as soon as one partition of the disk is mounted
548546
if len(activeParts) > 0 {

0 commit comments

Comments
 (0)