Skip to content

Commit

Permalink
storage: fix volume shrink on unaligned volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Aug 17, 2023
1 parent 563a66a commit 72945ed
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions host/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,34 +356,30 @@ func (vm *VolumeManager) shrinkVolume(ctx context.Context, id int, oldMaxSectors
return fmt.Errorf("failed to migrate sectors: %w", err)
}

var batchSize uint64 = resizeBatchSize
for current := oldMaxSectors; current > newMaxSectors; current -= batchSize {
for current := oldMaxSectors; current > newMaxSectors; {
// stop early if the context is cancelled
select {
case <-ctx.Done():
return ctx.Err()
default:
}

var target uint64
if current < batchSize {
target = newMaxSectors
batchSize = current - newMaxSectors
} else {
target = current - batchSize
target := newMaxSectors
if current-target > resizeBatchSize {
target = current - resizeBatchSize
}
// shrink in chunks to prevent holding a lock for too long and to
// track progress.

if err := vm.vs.ShrinkVolume(id, target); err != nil {
return fmt.Errorf("failed to expand volume metadata: %w", err)
} else if err := volume.Resize(target); err != nil {
return fmt.Errorf("failed to shrink volume data to %v sectors: %w", target, err)
}
// update the alert
a.Data["currentSectors"] = target
vm.a.Register(a)
// sleep to allow other operations to run
time.Sleep(time.Millisecond)

if current > resizeBatchSize {
current -= resizeBatchSize
} else {
current = newMaxSectors
}
}
return nil
}
Expand Down

0 comments on commit 72945ed

Please sign in to comment.