Fix partitioner on disks with secotor size other than 512 (#582)

This commit is contained in:
Itxaka 2024-10-28 20:23:18 +01:00 committed by GitHub
parent c7df8f3f0e
commit 8184c366ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -26,7 +26,9 @@ func (d *Disk) NewPartitionTable(partType string, parts sdkTypes.PartitionList)
table = &gpt.Table{ table = &gpt.Table{
ProtectiveMBR: true, ProtectiveMBR: true,
GUID: cnst.DiskUUID, // Set know predictable UUID GUID: cnst.DiskUUID, // Set know predictable UUID
Partitions: kairosPartsToDiskfsGPTParts(parts, d.Size), Partitions: kairosPartsToDiskfsGPTParts(parts, d.Size, d.LogicalBlocksize),
LogicalSectorSize: int(d.LogicalBlocksize),
PhysicalSectorSize: int(d.PhysicalBlocksize),
} }
default: default:
return fmt.Errorf("invalid partition type: %s", partType) return fmt.Errorf("invalid partition type: %s", partType)
@ -39,11 +41,11 @@ func (d *Disk) NewPartitionTable(partType string, parts sdkTypes.PartitionList)
return nil return nil
} }
func getSectorEndFromSize(start, size uint64) uint64 { func getSectorEndFromSize(start, size uint64, sectorSize int64) uint64 {
return (size / uint64(diskfs.SectorSize512)) + start - 1 return (size / uint64(sectorSize)) + start - 1
} }
func kairosPartsToDiskfsGPTParts(parts sdkTypes.PartitionList, diskSize int64) []*gpt.Partition { func kairosPartsToDiskfsGPTParts(parts sdkTypes.PartitionList, diskSize int64, sectorSize int64) []*gpt.Partition {
var partitions []*gpt.Partition var partitions []*gpt.Partition
for index, part := range parts { for index, part := range parts {
var start uint64 var start uint64
@ -51,7 +53,7 @@ func kairosPartsToDiskfsGPTParts(parts sdkTypes.PartitionList, diskSize int64) [
var size uint64 var size uint64
if len(partitions) == 0 { if len(partitions) == 0 {
// first partition, align to 1Mb // first partition, align to 1Mb
start = 1024 * 1024 / uint64(diskfs.SectorSize512) start = 1024 * 1024 / uint64(sectorSize)
} else { } else {
// get latest partition end, sum 1 // get latest partition end, sum 1
start = partitions[len(partitions)-1].End + 1 start = partitions[len(partitions)-1].End + 1
@ -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 { if part.Name == cnst.EfiPartName && part.FS == cnst.EfiFs {
// EFI boot partition // EFI boot partition
@ -127,7 +129,7 @@ func WithLogger(logger sdkTypes.KairosLogger) func(d *Disk) error {
} }
func NewDisk(device string, opts ...DiskOptions) (*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 { if err != nil {
return nil, err return nil, err
} }