Skip to content

Commit

Permalink
gadget/install: use gadget volume instead of laidout where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsosanchezbeato committed Apr 5, 2023
1 parent f919f91 commit 20dc020
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
21 changes: 12 additions & 9 deletions gadget/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,42 +222,45 @@ func createPartitions(model gadget.Model, gadgetRoot, kernelRoot, bootDevice str
logger.Noticef("installing a new system")
logger.Noticef(" gadget data from: %v", gadgetRoot)
logger.Noticef(" encryption: %v", options.EncryptionType)

if gadgetRoot == "" {
return "", nil, nil, 0, fmt.Errorf("cannot use empty gadget root directory")
}

if model.Grade() == asserts.ModelGradeUnset {
return "", nil, nil, 0, fmt.Errorf("cannot run install mode on pre-UC20 system")
return "", nil, nil, 0,
fmt.Errorf("cannot run install mode on pre-UC20 system")
}

// Find boot laid out data
laidOutBootVol, allLaidOutVols, err := gadget.LaidOutVolumesFromGadget(gadgetRoot, kernelRoot, model, options.EncryptionType)
if err != nil {
return "", nil, nil, 0, fmt.Errorf("cannot layout volumes: %v", err)
}
// TODO: resolve content paths from gadget here
bootVol := laidOutBootVol.Volume

// auto-detect device if no device is forced
// device forcing is used for (spread) testing only
if bootDevice == "" {
bootDevice, err = diskWithSystemSeed(laidOutBootVol.Volume)
bootDevice, err = diskWithSystemSeed(bootVol)
if err != nil {
return "", nil, nil, 0, fmt.Errorf("cannot find device to create partitions on: %v", err)
}
}

diskLayout, err := gadget.OnDiskVolumeFromDevice(bootDevice)
diskVolume, err := gadget.OnDiskVolumeFromDevice(bootDevice)
if err != nil {
return "", nil, nil, 0, fmt.Errorf("cannot read %v partitions: %v", bootDevice, err)
}

// check if the current partition table is compatible with the gadget,
// ignoring partitions added by the installer (will be removed later)
if err := gadget.EnsureVolumeCompatibility(laidOutBootVol.Volume, diskLayout, nil); err != nil {
if err := gadget.EnsureVolumeCompatibility(bootVol, diskVolume, nil); err != nil {
return "", nil, nil, 0, fmt.Errorf("gadget and system-boot device %v partition table not compatible: %v", bootDevice, err)
}

// remove partitions added during a previous install attempt
if err := removeCreatedPartitions(gadgetRoot, laidOutBootVol, diskLayout); err != nil {
if err := removeCreatedPartitions(gadgetRoot, bootVol, diskVolume); err != nil {
return "", nil, nil, 0, fmt.Errorf("cannot remove partitions from previous install: %v", err)
}
// at this point we removed any existing partition, nuke any
Expand All @@ -274,14 +277,14 @@ func createPartitions(model gadget.Model, gadgetRoot, kernelRoot, bootDevice str
opts := &CreateOptions{
GadgetRootDir: gadgetRoot,
}
created, err = createMissingPartitions(diskLayout, laidOutBootVol, opts)
created, err = createMissingPartitions(diskVolume, laidOutBootVol, opts)
})
if err != nil {
return "", nil, nil, 0, fmt.Errorf("cannot create the partitions: %v", err)
}

bootVolGadgetName = laidOutBootVol.Name
bootVolSectorSize = diskLayout.SectorSize
bootVolGadgetName = bootVol.Name
bootVolSectorSize = diskVolume.SectorSize
return bootVolGadgetName, created, allLaidOutVols, bootVolSectorSize, nil
}

Expand Down
18 changes: 9 additions & 9 deletions gadget/install/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ func deviceName(name string, index int) string {
}

// removeCreatedPartitions removes partitions added during a previous install.
func removeCreatedPartitions(gadgetRoot string, lv *gadget.LaidOutVolume, dl *gadget.OnDiskVolume) error {
func removeCreatedPartitions(gadgetRoot string, gv *gadget.Volume, dl *gadget.OnDiskVolume) error {
sfdiskIndexes := make([]string, 0, len(dl.Structure))
// up to 3 possible partitions are creatable and thus removable:
// ubuntu-data, ubuntu-boot, and ubuntu-save
deletedIndexes := make(map[int]bool, 3)
for i, s := range dl.Structure {
if wasCreatedDuringInstall(lv, s) {
if wasCreatedDuringInstall(gv, s) {
logger.Noticef("partition %s was created during previous install", s.Node)
sfdiskIndexes = append(sfdiskIndexes, strconv.Itoa(i+1))
deletedIndexes[i] = true
Expand Down Expand Up @@ -294,7 +294,7 @@ func removeCreatedPartitions(gadgetRoot string, lv *gadget.LaidOutVolume, dl *ga
dl.Structure = newStructure

// Ensure all created partitions were removed
if remaining := createdDuringInstall(lv, dl); len(remaining) > 0 {
if remaining := createdDuringInstall(gv, dl); len(remaining) > 0 {
return fmt.Errorf("cannot remove partitions: %s", strings.Join(remaining, ", "))
}

Expand Down Expand Up @@ -407,21 +407,21 @@ func udevTrigger(device string) error {
// structure such as filesystem information since this may be incomplete due to
// a failed installation, or due to the partial layout that is created by some
// ARM tools (i.e. ptool and fastboot) when flashing images to internal MMC.
func wasCreatedDuringInstall(lv *gadget.LaidOutVolume, s gadget.OnDiskStructure) bool {
func wasCreatedDuringInstall(gv *gadget.Volume, s gadget.OnDiskStructure) bool {
// for a structure to have been created during install, it must be one of
// the system-boot, system-data, or system-save roles from the gadget, and
// as such the on disk structure must exist in the exact same location as
// the role from the gadget, so only return true if the provided structure
// has the exact same StartOffset as one of those roles
for _, gs := range lv.LaidOutStructure {
for _, gs := range gv.Structure {
// TODO: how to handle ubuntu-save here? maybe a higher level function
// should decide whether to delete it or not?
switch gs.Role() {
switch gs.Role {
case gadget.SystemSave, gadget.SystemData, gadget.SystemBoot:
// then it was created during install or is to be created during
// install, see if the offset matches the provided on disk structure
// has
if s.StartOffset == gs.StartOffset {
if s.StartOffset == *gs.Offset {
return true
}
}
Expand All @@ -432,10 +432,10 @@ func wasCreatedDuringInstall(lv *gadget.LaidOutVolume, s gadget.OnDiskStructure)

// createdDuringInstall returns a list of partitions created during the
// install process.
func createdDuringInstall(lv *gadget.LaidOutVolume, layout *gadget.OnDiskVolume) (created []string) {
func createdDuringInstall(gv *gadget.Volume, layout *gadget.OnDiskVolume) (created []string) {
created = make([]string, 0, len(layout.Structure))
for _, s := range layout.Structure {
if wasCreatedDuringInstall(lv, s) {
if wasCreatedDuringInstall(gv, s) {
created = append(created, s.Node)
}
}
Expand Down
20 changes: 10 additions & 10 deletions gadget/install/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,13 @@ func (s *partitionTestSuite) TestRemovePartitionsTrivial(c *C) {

err := gadgettest.MakeMockGadget(s.gadgetRoot, gadgetContent)
c.Assert(err, IsNil)
pv, err := gadgettest.MustLayOutSingleVolumeFromGadget(s.gadgetRoot, "", uc20Mod)
gInfo, err := gadget.ReadInfoAndValidate(s.gadgetRoot, uc20Mod, nil)
c.Assert(err, IsNil)

dl, err := gadget.OnDiskVolumeFromDevice("/dev/node")
c.Assert(err, IsNil)

err = install.RemoveCreatedPartitions(s.gadgetRoot, pv, dl)
err = install.RemoveCreatedPartitions(s.gadgetRoot, gInfo.Volumes["pc"], dl)
c.Assert(err, IsNil)
}

Expand Down Expand Up @@ -494,10 +494,10 @@ func (s *partitionTestSuite) TestRemovePartitions(c *C) {

err = gadgettest.MakeMockGadget(s.gadgetRoot, gadgetContent)
c.Assert(err, IsNil)
pv, err := gadgettest.MustLayOutSingleVolumeFromGadget(s.gadgetRoot, "", uc20Mod)
gInfo, err := gadget.ReadInfoAndValidate(s.gadgetRoot, uc20Mod, nil)
c.Assert(err, IsNil)

err = install.RemoveCreatedPartitions(s.gadgetRoot, pv, dl)
err = install.RemoveCreatedPartitions(s.gadgetRoot, gInfo.Volumes["pc"], dl)
c.Assert(err, IsNil)

c.Assert(cmdSfdisk.Calls(), DeepEquals, [][]string{
Expand Down Expand Up @@ -618,10 +618,10 @@ func (s *partitionTestSuite) TestRemovePartitionsWithDeviceRescan(c *C) {
err = ioutil.WriteFile(filepath.Join(s.gadgetRoot, "meta", "force-partition-table-reload-via-device-rescan"), nil, 0755)
c.Assert(err, IsNil)

pv, err := gadgettest.MustLayOutSingleVolumeFromGadget(s.gadgetRoot, "", uc20Mod)
gInfo, err := gadget.ReadInfoAndValidate(s.gadgetRoot, uc20Mod, nil)
c.Assert(err, IsNil)

err = install.RemoveCreatedPartitions(s.gadgetRoot, pv, dl)
err = install.RemoveCreatedPartitions(s.gadgetRoot, gInfo.Volumes["pc"], dl)
c.Assert(err, IsNil)

c.Assert(cmdSfdisk.Calls(), DeepEquals, [][]string{
Expand Down Expand Up @@ -759,10 +759,10 @@ func (s *partitionTestSuite) TestRemovePartitionsNonAdjacent(c *C) {

err = gadgettest.MakeMockGadget(s.gadgetRoot, gadgetContentDifferentOrder)
c.Assert(err, IsNil)
pv, err := gadgettest.MustLayOutSingleVolumeFromGadget(s.gadgetRoot, "", uc20Mod)
gInfo, err := gadget.ReadInfoAndValidate(s.gadgetRoot, uc20Mod, nil)
c.Assert(err, IsNil)

err = install.RemoveCreatedPartitions(s.gadgetRoot, pv, dl)
err = install.RemoveCreatedPartitions(s.gadgetRoot, gInfo.Volumes["pc"], dl)
c.Assert(err, IsNil)

c.Assert(cmdSfdisk.Calls(), DeepEquals, [][]string{
Expand Down Expand Up @@ -954,7 +954,7 @@ func (s *partitionTestSuite) TestCreatedDuringInstallGPT(c *C) {
dl, err := gadget.OnDiskVolumeFromDevice("node")
c.Assert(err, IsNil)

list := install.CreatedDuringInstall(pv, dl)
list := install.CreatedDuringInstall(pv.Volume, dl)
// only save and writable should show up
c.Check(list, DeepEquals, []string{"/dev/node3", "/dev/node4"})
}
Expand Down Expand Up @@ -1076,6 +1076,6 @@ func (s *partitionTestSuite) TestCreatedDuringInstallMBR(c *C) {
pv, err := gadgettest.MustLayOutSingleVolumeFromGadget(s.gadgetRoot, "", uc20Mod)
c.Assert(err, IsNil)

list := install.CreatedDuringInstall(pv, dl)
list := install.CreatedDuringInstall(pv.Volume, dl)
c.Assert(list, DeepEquals, []string{"/dev/node2", "/dev/node3", "/dev/node4"})
}

0 comments on commit 20dc020

Please sign in to comment.