Skip to content

Commit

Permalink
stages/disks: add logic to resize an existing partition
Browse files Browse the repository at this point in the history
Fixes coreos#924
This fix will allow users to resize an existing partition without wiping
out an entry from the partition table.
  • Loading branch information
sohankunkerkar committed Sep 8, 2020
1 parent 17a23db commit 2b015f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
13 changes: 12 additions & 1 deletion internal/exec/stages/disks/partitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error {
}
matches := exists && matchErr == nil
wipeEntry := part.WipePartitionEntry != nil && *part.WipePartitionEntry
resize := part.ResizePartition != nil && *part.ResizePartition

// This is a translation of the matrix in the operator notes.
switch {
Expand All @@ -353,7 +354,17 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error {
case exists && shouldExist && matches:
s.Logger.Info("partition %d found with correct specifications", part.Number)
case exists && shouldExist && !wipeEntry && !matches:
return fmt.Errorf("Partition %d didn't match: %v", part.Number, matchErr)
if resize {
s.Logger.Info("partition %d found with resizePartition option, wiping partition entry and recreating", part.Number)
var ids []string
ids = append(ids, info.GUID, info.TypeGUID)
// need to preserve the partition ids for resizing
op.PreservePartitionIDs(ids)
op.DeletePartition(part.Number)
op.CreatePartition(part)
} else {
return fmt.Errorf("Partition %d didn't match: %v", part.Number, matchErr)
}
case exists && shouldExist && wipeEntry && !matches:
s.Logger.Info("partition %d did not meet specifications, wiping partition entry and recreating", part.Number)
op.DeletePartition(part.Number)
Expand Down
22 changes: 16 additions & 6 deletions internal/sgdisk/sgdisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import (
)

type Operation struct {
logger *log.Logger
dev string
wipe bool
parts []Partition
deletions []int
infos []int
logger *log.Logger
dev string
wipe bool
partitionIDs []string
parts []Partition
deletions []int
infos []int
}

// We ignore types.Partition.StartMiB/SizeMiB in favor of
Expand Down Expand Up @@ -59,6 +60,11 @@ func (op *Operation) DeletePartition(num int) {
op.deletions = append(op.deletions, num)
}

// PreservePartitionIDs preserves the GUID and TypeGUID for a given partition.
func (op *Operation) PreservePartitionIDs(ids []string) {
op.partitionIDs = append(op.partitionIDs, ids...)
}

func (op *Operation) Info(num int) {
op.infos = append(op.infos, num)
}
Expand Down Expand Up @@ -137,6 +143,10 @@ func (op Operation) buildOptions() []string {
}

for _, p := range op.parts {
if len(op.partitionIDs) > 0 {
p.GUID = &op.partitionIDs[0]
p.TypeGUID = &op.partitionIDs[1]
}
opts = append(opts, fmt.Sprintf("--new=%d:%s:+%s", p.Number, partitionGetStart(p), partitionGetSize(p)))
if p.Label != nil {
opts = append(opts, fmt.Sprintf("--change-name=%d:%s", p.Number, *p.Label))
Expand Down

0 comments on commit 2b015f2

Please sign in to comment.