Skip to content

Commit

Permalink
πŸ› Normalize partition labels before checking them and sort by size (#…
Browse files Browse the repository at this point in the history
…4314)

* πŸ› Normalize partition labels before checking them. Return first match from findVolume.

Signed-off-by: Preslav <[email protected]>

* Sort candidate partitions.

---------

Signed-off-by: Preslav <[email protected]>
  • Loading branch information
preslavgerchev authored Jul 8, 2024
1 parent 792ba97 commit bc3c52d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
15 changes: 10 additions & 5 deletions providers/os/connection/snapshot/blockdevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,25 @@ func (blockEntries BlockDevices) GetUnmountedBlockEntry() (*PartitionInfo, error
}

func findVolume(children []BlockDevice) *PartitionInfo {
var fs *PartitionInfo
candidates := []BlockDevice{}
for i := range children {
entry := children[i]
if entry.IsNotBootOrRootVolumeAndUnmounted() {
// we are NOT searching for the root volume here, so we can exclude the "sda" and "xvda" volumes
devFsName := "/dev/" + entry.Name
fs = &PartitionInfo{Name: devFsName, FsType: entry.FsType}
candidates = append(candidates, entry)
}
}
return fs
if len(candidates) == 0 {
return nil
}
sortPartitionsBySize(candidates)
return &PartitionInfo{Name: "/dev/" + candidates[0].Name, FsType: candidates[0].FsType}
}

func (entry BlockDevice) IsNoBootVolume() bool {
return entry.Uuid != "" && entry.FsType != "" && entry.FsType != "vfat" && entry.Label != "EFI" && entry.Label != "boot"
typ := strings.ToLower(entry.FsType)
label := strings.ToLower(entry.Label)
return entry.Uuid != "" && typ != "" && typ != "vfat" && label != "efi" && label != "boot"
}

func (entry BlockDevice) IsNoBootVolumeAndUnmounted() bool {
Expand Down
4 changes: 2 additions & 2 deletions providers/os/connection/snapshot/blockdevices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestGetMountablePartitionByDevice(t *testing.T) {
Name: "sde",
Children: []BlockDevice{
{Uuid: "12346", FsType: "xfs", Size: 110, Label: "ROOT", Name: "sde1"},
{Uuid: "12345", FsType: "xfs", Size: 120, Label: "boot", Name: "sde2"},
{Uuid: "12345", FsType: "xfs", Size: 120, Label: "BOOT", Name: "sde2"},
},
},
},
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestAttachedBlockEntryMultipleMatch(t *testing.T) {
info, err := blockEntries.GetUnnamedBlockEntry()
require.NoError(t, err)
require.Equal(t, "xfs", info.FsType)
require.True(t, strings.Contains(info.Name, "xvdh4"))
require.True(t, strings.Contains(info.Name, "xvdh3"))
}

func TestAttachedBlockEntryFedora(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions providers/os/connection/snapshot/testdata/alma9_attached.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
{
"name": "xvdh3",
"fstype": "xfs",
"size": 101,
"fsver": null,
"label": null,
"uuid": "31f80bf2-f73c-4350-bd3e-1dfe82e691f9",
Expand All @@ -102,6 +103,7 @@
"name": "xvdh4",
"fstype": "xfs",
"fsver": null,
"size": 100,
"label": null,
"uuid": "b6b03ca5-4431-4db9-a1fb-f13c8566f779",
"fsavail": null,
Expand Down

0 comments on commit bc3c52d

Please sign in to comment.