Skip to content

Commit

Permalink
fix: align partition start to physical sector size
Browse files Browse the repository at this point in the history
Misaligning partition start leads to sub-optimal I/O performance.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira authored and talos-bot committed Mar 5, 2021
1 parent 8f976c2 commit bb3ad73
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
11 changes: 11 additions & 0 deletions blockdevice/lba/lba.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ type LBA struct {

f *os.File
}

// AlignToPhysicalBlockSize aligns LBA value in LogicalBlockSize multiples to be aligned to PhysicalBlockSize.
func (l *LBA) AlignToPhysicalBlockSize(lba uint64) uint64 {
physToLogical := uint64(l.PhysicalBlockSize / l.LogicalBlockSize)

if physToLogical <= 1 {
return lba
}

return (lba + physToLogical - 1) / physToLogical * physToLogical
}
26 changes: 20 additions & 6 deletions blockdevice/lba/lba_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@

package lba_test

import "testing"
import (
"testing"

func TestEmpty(t *testing.T) {
// added for accurate coverage estimation
//
// please remove it once any unit-test is added
// for this package
"github.com/stretchr/testify/assert"

"github.com/talos-systems/go-blockdevice/blockdevice/lba"
)

func TestAlignToPhysicalBlockSize(t *testing.T) {
l := lba.LBA{ //nolint: exhaustivestruct
PhysicalBlockSize: 4096,
LogicalBlockSize: 512,
}

assert.EqualValues(t, 0, l.AlignToPhysicalBlockSize(0))
assert.EqualValues(t, 8, l.AlignToPhysicalBlockSize(1))
assert.EqualValues(t, 8, l.AlignToPhysicalBlockSize(2))
assert.EqualValues(t, 8, l.AlignToPhysicalBlockSize(3))
assert.EqualValues(t, 8, l.AlignToPhysicalBlockSize(4))
assert.EqualValues(t, 8, l.AlignToPhysicalBlockSize(8))
assert.EqualValues(t, 16, l.AlignToPhysicalBlockSize(9))
}
2 changes: 1 addition & 1 deletion blockdevice/partition/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (g *GPT) InsertAt(idx int, size uint64, setters ...PartitionOption) (*Parti
// Find partition boundaries.
var start, end uint64

start = minLBA
start = g.l.AlignToPhysicalBlockSize(minLBA)

if opts.MaximumSize {
end = maxLBA
Expand Down

0 comments on commit bb3ad73

Please sign in to comment.