disk: honour maximum number of partitions

Return an error if the maximum numbers of partitions has been
reached and thus creating further partitions would result in
errors.
Currently we limit MBR partition types to 4 as we dont support
logical partitions and GPT layouts to 128. According to the
UEFI specificatio (2.8) a minimum of 16384 bytes are reserved
for the partition entries array. Each entry is 128 bytes wide
thus resulting in at least 128 entries; we choose this to be
the maximum as well for now.
This commit is contained in:
Christian Kellner 2021-12-31 14:59:07 +01:00 committed by Tom Gundersen
parent 2ee3fd31a1
commit b65ef74cb2
6 changed files with 42 additions and 10 deletions

View file

@ -42,7 +42,10 @@ func CreatePartitionTable(
if part != nil {
part.EnsureSize(sectors)
} else {
table.CreateFilesystem(m.Mountpoint, sectors)
err := table.CreateFilesystem(m.Mountpoint, sectors)
if err != nil {
return PartitionTable{}, err
}
}
}

View file

@ -7,6 +7,7 @@ package disk
import (
"encoding/hex"
"errors"
"fmt"
"io"
"math/rand"
"sort"
@ -279,7 +280,7 @@ func (pt *PartitionTable) BootFilesystem() *Filesystem {
// Create a new filesystem within the partition table at the given mountpoint
// with the given minimum size in sectors.
func (pt *PartitionTable) CreateFilesystem(mountpoint string, size uint64) {
func (pt *PartitionTable) CreateFilesystem(mountpoint string, size uint64) error {
filesystem := Filesystem{
Type: "xfs",
Mountpoint: mountpoint,
@ -293,11 +294,23 @@ func (pt *PartitionTable) CreateFilesystem(mountpoint string, size uint64) {
Filesystem: &filesystem,
}
n := len(pt.Partitions)
var maxNo int
if pt.Type == "gpt" {
partition.Type = FilesystemDataGUID
maxNo = 128
} else {
maxNo = 4
}
if n == maxNo {
return fmt.Errorf("maximum number of partitions reached (%d)", maxNo)
}
pt.Partitions = append(pt.Partitions, partition)
return nil
}
// Generate all needed UUIDs for all the partiton and filesystems

View file

@ -684,10 +684,13 @@ func TestDistro_MountpointsWithArbitraryDepthAllowed(t *testing.T) {
imgType, _ := arch.GetImageType(imgTypeName)
testPackageSpecSets := distro_test_common.GetTestingPackageSpecSets("kernel", arch.Name(), imgType.PayloadPackageSets())
_, err := imgType.Manifest(bp.Customizations, distro.ImageOptions{}, nil, testPackageSpecSets, 0)
if strings.HasPrefix(imgTypeName, "edge-") {
layout := imgType.PartitionType()
if layout == "" || strings.HasPrefix(imgTypeName, "edge-") {
continue
} else {
} else if layout == "gpt" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, "maximum number of partitions reached (4)")
}
}
}

View file

@ -693,10 +693,13 @@ func TestDistro_MountpointsWithArbitraryDepthAllowed(t *testing.T) {
imgType, _ := arch.GetImageType(imgTypeName)
testPackageSpecSets := distro_test_common.GetTestingPackageSpecSets("kernel", arch.Name(), imgType.PayloadPackageSets())
_, err := imgType.Manifest(bp.Customizations, distro.ImageOptions{}, nil, testPackageSpecSets, 0)
if strings.HasPrefix(imgTypeName, "edge-") {
layout := imgType.PartitionType()
if layout == "" || strings.HasPrefix(imgTypeName, "edge-") {
continue
} else {
} else if layout == "gpt" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, "maximum number of partitions reached (4)")
}
}
}

View file

@ -654,8 +654,12 @@ func TestDistro_CustomFileSystemSubDirectories(t *testing.T) {
imgType, _ := arch.GetImageType(imgTypeName)
testPackageSpecSets := distro_test_common.GetTestingPackageSpecSets("kernel", arch.Name(), imgType.PayloadPackageSets())
_, err := imgType.Manifest(bp.Customizations, distro.ImageOptions{}, nil, testPackageSpecSets, 0)
layout := imgType.PartitionType()
if strings.HasPrefix(imgTypeName, "edge-") {
continue
} else if layout == "dos" && arch.Name() == distro.Ppc64leArchName {
// PPC64LE uses mbr and has 3 partitions defined in the base layout
assert.EqualError(t, err, "maximum number of partitions reached (4)")
} else {
assert.NoError(t, err)
}
@ -693,10 +697,13 @@ func TestDistro_MountpointsWithArbitraryDepthAllowed(t *testing.T) {
imgType, _ := arch.GetImageType(imgTypeName)
testPackageSpecSets := distro_test_common.GetTestingPackageSpecSets("kernel", arch.Name(), imgType.PayloadPackageSets())
_, err := imgType.Manifest(bp.Customizations, distro.ImageOptions{}, nil, testPackageSpecSets, 0)
if strings.HasPrefix(imgTypeName, "edge-") {
layout := imgType.PartitionType()
if layout == "" || strings.HasPrefix(imgTypeName, "edge-") {
continue
} else {
} else if layout == "gpt" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, "maximum number of partitions reached (4)")
}
}
}

View file

@ -686,10 +686,13 @@ func TestDistro_MountpointsWithArbitraryDepthAllowed(t *testing.T) {
imgType, _ := arch.GetImageType(imgTypeName)
testPackageSpecSets := distro_test_common.GetTestingPackageSpecSets("kernel", arch.Name(), imgType.PayloadPackageSets())
_, err := imgType.Manifest(bp.Customizations, distro.ImageOptions{}, nil, testPackageSpecSets, 0)
if strings.HasPrefix(imgTypeName, "edge-") {
layout := imgType.PartitionType()
if layout == "" || strings.HasPrefix(imgTypeName, "edge-") {
continue
} else {
} else if layout == "gpt" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, "maximum number of partitions reached (4)")
}
}
}