Skip to content

Commit

Permalink
Fix partitioner on disks with secotor size other than 512
Browse files Browse the repository at this point in the history
We were assuming all disks in the world would have a sector size of 512
but that its not true.

This patch will not force to open the disk with a hardcoded sector size
and will use the sector size values of the disk to set the sizes,
partition table and such.

this fixes partitions on disks with 4k sector size

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka committed Oct 24, 2024
1 parent f24511b commit f30f290
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions pkg/partitioner/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ func (d *Disk) NewPartitionTable(partType string, parts sdkTypes.PartitionList)
switch partType {
case v1.GPT:
table = &gpt.Table{
ProtectiveMBR: true,
GUID: cnst.DiskUUID, // Set know predictable UUID
Partitions: kairosPartsToDiskfsGPTParts(parts, d.Size),
ProtectiveMBR: true,
GUID: cnst.DiskUUID, // Set know predictable UUID
Partitions: kairosPartsToDiskfsGPTParts(parts, d.Size, d.LogicalBlocksize),
LogicalSectorSize: int(d.LogicalBlocksize),
PhysicalSectorSize: int(d.PhysicalBlocksize),
}
default:
return fmt.Errorf("invalid partition type: %s", partType)
Expand All @@ -39,19 +41,19 @@ func (d *Disk) NewPartitionTable(partType string, parts sdkTypes.PartitionList)
return nil
}

func getSectorEndFromSize(start, size uint64) uint64 {
return (size / uint64(diskfs.SectorSize512)) + start - 1
func getSectorEndFromSize(start, size uint64, sectorSize int64) uint64 {
return (size / uint64(sectorSize)) + start - 1

Check failure

Code scanning / Golang security checks by gosec

integer overflow conversion uint -> int64 Error

integer overflow conversion int64 -> uint64
}

func kairosPartsToDiskfsGPTParts(parts sdkTypes.PartitionList, diskSize int64) []*gpt.Partition {
func kairosPartsToDiskfsGPTParts(parts sdkTypes.PartitionList, diskSize int64, sectorSize int64) []*gpt.Partition {
var partitions []*gpt.Partition
for index, part := range parts {
var start uint64
var end uint64
var size uint64
if len(partitions) == 0 {
// first partition, align to 1Mb
start = 1024 * 1024 / uint64(diskfs.SectorSize512)
start = 1024 * 1024 / uint64(sectorSize)

Check failure

Code scanning / Golang security checks by gosec

integer overflow conversion uint -> int64 Error

integer overflow conversion int64 -> uint64
} else {
// get latest partition end, sum 1
start = partitions[len(partitions)-1].End + 1
Expand All @@ -78,7 +80,7 @@ func kairosPartsToDiskfsGPTParts(parts sdkTypes.PartitionList, diskSize int64) [

}

end = getSectorEndFromSize(start, size)
end = getSectorEndFromSize(start, size, sectorSize)

if part.Name == cnst.EfiPartName && part.FS == cnst.EfiFs {
// EFI boot partition
Expand Down Expand Up @@ -127,7 +129,7 @@ func WithLogger(logger sdkTypes.KairosLogger) func(d *Disk) error {
}

func NewDisk(device string, opts ...DiskOptions) (*Disk, error) {
d, err := diskfs.Open(device, diskfs.WithSectorSize(512))
d, err := diskfs.Open(device)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit f30f290

Please sign in to comment.