Skip to content

Commit

Permalink
gadget: Add a Size method to Volume
Browse files Browse the repository at this point in the history
This method is needed to determine the size of the disk image to create in ubuntu-image.

Signed-off-by: Paul Mars <[email protected]>
  • Loading branch information
upils committed Oct 3, 2024
1 parent 534502f commit 9f4c785
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
24 changes: 19 additions & 5 deletions gadget/gadget.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,35 @@ func (v *Volume) HasPartial(pp PartialProperty) bool {
return false
}

// MinSize returns the minimum size required by a volume, as implicitly
// defined by the size structures. It assumes sorted structures.
func (v *Volume) MinSize() quantity.Size {
// size returns the size of the volume using the structureSizer function to calculate
// structures size. It assumes sorted structures.
func (v *Volume) size(structureSizer func(VolumeStructure) quantity.Size) quantity.Size {
endVol := quantity.Offset(0)
for _, s := range v.Structure {
if s.Offset != nil {
endVol = *s.Offset + quantity.Offset(s.MinSize)
endVol = *s.Offset + quantity.Offset(structureSizer(s))
} else {
endVol += quantity.Offset(s.MinSize)
endVol += quantity.Offset(structureSizer(s))
}
}

return quantity.Size(endVol)
}

// MinSize returns the minimum size required by a volume.
func (v *Volume) MinSize() quantity.Size {
return v.size(func(s VolumeStructure) quantity.Size {
return s.MinSize
})
}

// Size returns the current size required by a volume
func (v *Volume) Size() quantity.Size {
return v.size(func(s VolumeStructure) quantity.Size {
return s.Size
})
}

// StructFromYamlIndex returns the structure defined at a given yaml index from
// the original yaml file.
func (v *Volume) StructFromYamlIndex(yamlIdx int) *VolumeStructure {
Expand Down
41 changes: 33 additions & 8 deletions gadget/gadget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4425,69 +4425,94 @@ kernel-cmdline:
}
}

func (s *gadgetYamlTestSuite) testVolumeMinSize(c *C, gadgetYaml []byte, volSizes map[string]quantity.Size) {
func (s *gadgetYamlTestSuite) testVolumeSize(c *C, gadgetYaml []byte, volSizes map[string]quantity.Size, volumeSizer func(*gadget.Volume) quantity.Size) {
ginfo, err := gadget.InfoFromGadgetYaml(gadgetYaml, nil)
c.Assert(err, IsNil)

c.Assert(len(ginfo.Volumes), Equals, len(volSizes))
for k, v := range ginfo.Volumes {
c.Logf("checking size of volume %s", k)
c.Check(v.MinSize(), Equals, quantity.Size(volSizes[k]))
c.Check(volumeSizer(v), Equals, quantity.Size(volSizes[k]))
}
}

func (s *gadgetYamlTestSuite) TestVolumeMinSize(c *C) {
func (s *gadgetYamlTestSuite) TestVolumeSizes(c *C) {
for _, tc := range []struct {
gadgetYaml []byte
volsSizes map[string]quantity.Size
gadgetYaml []byte
volsMinSizes map[string]quantity.Size
volsSizes map[string]quantity.Size
}{
{
gadgetYaml: gadgetYamlUnorderedParts,
volsMinSizes: map[string]quantity.Size{
"myvol": 1300 * quantity.SizeMiB,
},
volsSizes: map[string]quantity.Size{
"myvol": 1300 * quantity.SizeMiB,
},
},
{
gadgetYaml: mockMultiVolumeUC20GadgetYaml,
volsMinSizes: map[string]quantity.Size{
"frobinator-image": (1 + 500 + 10 + 500 + 1024) * quantity.SizeMiB,
"u-boot-frobinator": 24576 + 623000,
},
volsSizes: map[string]quantity.Size{
"frobinator-image": (1 + 500 + 10 + 500 + 1024) * quantity.SizeMiB,
"u-boot-frobinator": 24576 + 623000,
},
},
{
gadgetYaml: mockMultiVolumeGadgetYaml,
volsMinSizes: map[string]quantity.Size{
"frobinator-image": (1 + 128 + 380) * quantity.SizeMiB,
"u-boot-frobinator": 24576 + 623000,
},
volsSizes: map[string]quantity.Size{
"frobinator-image": (1 + 128 + 380) * quantity.SizeMiB,
"u-boot-frobinator": 24576 + 623000,
},
},
{
gadgetYaml: mockVolumeUpdateGadgetYaml,
volsMinSizes: map[string]quantity.Size{
"bootloader": 12345 + 88888,
},
volsSizes: map[string]quantity.Size{
"bootloader": 12345 + 88888,
},
},
{
gadgetYaml: gadgetYamlPC,
volsMinSizes: map[string]quantity.Size{
"pc": (1 + 1 + 50) * quantity.SizeMiB,
},
volsSizes: map[string]quantity.Size{
"pc": (1 + 1 + 50) * quantity.SizeMiB,
},
},
{
gadgetYaml: gadgetYamlUC20PC,
volsMinSizes: map[string]quantity.Size{
"pc": (1 + 1 + 1200 + 750 + 16 + 1024) * quantity.SizeMiB,
},
volsSizes: map[string]quantity.Size{
"pc": (1 + 1 + 1200 + 750 + 16 + 1024) * quantity.SizeMiB,
},
},
{
gadgetYaml: gadgetYamlMinSizePC,
volsSizes: map[string]quantity.Size{
volsMinSizes: map[string]quantity.Size{
"pc": (1 + 1 + 1200 + 750 + 16 + 1024) * quantity.SizeMiB,
},
volsSizes: map[string]quantity.Size{
"pc": (1 + 1 + 1200 + 750 + 32 + 1024) * quantity.SizeMiB,
},
},
} {
c.Logf("test min size for %s", tc.gadgetYaml)
s.testVolumeMinSize(c, tc.gadgetYaml, tc.volsSizes)
c.Logf("test sizes for %s", tc.gadgetYaml)
s.testVolumeSize(c, tc.gadgetYaml, tc.volsMinSizes, (*gadget.Volume).MinSize)
s.testVolumeSize(c, tc.gadgetYaml, tc.volsSizes, (*gadget.Volume).Size)
}
}

Expand Down

0 comments on commit 9f4c785

Please sign in to comment.