Skip to content

Commit

Permalink
✨device manager: extended partitions info (#4820)
Browse files Browse the repository at this point in the history
* feat: provide volume uuid and label with partition info

* feat: provide partition infos via device connection

* fix: unit tests data

* cleanup
  • Loading branch information
slntopp authored Nov 7, 2024
1 parent d9eeec1 commit 9c866a6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
13 changes: 13 additions & 0 deletions providers/os/connection/device/device_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type DeviceConnection struct {
deviceManager DeviceManager

MountedDirs []string
// map of mountpoints to partition infos
partitions map[string]*snapshot.PartitionInfo
}

func getDeviceManager(conf *inventory.Config) (DeviceManager, error) {
Expand Down Expand Up @@ -76,12 +78,15 @@ func NewDeviceConnection(connId uint32, conf *inventory.Config, asset *inventory
asset.IdDetector = []string{ids.IdDetector_Hostname, ids.IdDetector_SshHostkey}
}

res.partitions = make(map[string]*snapshot.PartitionInfo)

// we iterate over all the blocks and try to run OS detection on each one of them
// we only return one asset, if we find the right block (e.g. the one with the root FS)
for _, block := range blocks {
fsConn, scanDir, err := tryDetectAsset(connId, block, manager, conf, asset)
if scanDir != "" {
res.MountedDirs = append(res.MountedDirs, scanDir)
res.partitions[scanDir] = block
}
if err != nil {
log.Error().Err(err).Msg("partition did not return an asset, continuing")
Expand Down Expand Up @@ -158,6 +163,14 @@ func (p *DeviceConnection) Conf() *inventory.Config {
return p.FileSystemConnection.Conf
}

func (p *DeviceConnection) Partitions() map[string]*snapshot.PartitionInfo {
if p.partitions == nil {
p.partitions = make(map[string]*snapshot.PartitionInfo)
}

return p.partitions
}

// tryDetectAsset tries to detect the OS on a given block device
func tryDetectAsset(connId uint32, partition *snapshot.PartitionInfo, manager DeviceManager, conf *inventory.Config, asset *inventory.Asset) (*fs.FileSystemConnection, string, error) {
log.Debug().Str("name", partition.Name).Str("type", partition.FsType).Msg("mounting partition")
Expand Down
5 changes: 4 additions & 1 deletion providers/os/connection/snapshot/blockdevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ func (device BlockDevice) GetMountablePartitions(includeAll bool) ([]*PartitionI
if filter(partition) {
log.Debug().Str("name", partition.Name).Msg("found suitable partition")
devFsName := "/dev/" + partition.Name
partitions = append(partitions, &PartitionInfo{Name: devFsName, FsType: partition.FsType})
partitions = append(partitions, &PartitionInfo{
Name: devFsName, FsType: partition.FsType,
Label: partition.Label, Uuid: partition.Uuid,
})
} else {
log.Debug().
Str("name", partition.Name).
Expand Down
8 changes: 4 additions & 4 deletions providers/os/connection/snapshot/blockdevices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func TestGetMountablePartition(t *testing.T) {
}
partition, err := block.GetMountablePartition()
require.Nil(t, err)
require.Equal(t, &PartitionInfo{FsType: "xfs", Name: "/dev/sde1"}, partition)
require.Equal(t, &PartitionInfo{FsType: "xfs", Name: "/dev/sde1", Uuid: "12346", Label: "ROOT"}, partition)
})

t.Run("largest suitable partition", func(t *testing.T) {
Expand All @@ -260,7 +260,7 @@ func TestGetMountablePartition(t *testing.T) {
}
partition, err := block.GetMountablePartition()
require.Nil(t, err)
require.Equal(t, &PartitionInfo{FsType: "xfs", Name: "/dev/sda2"}, partition)
require.Equal(t, &PartitionInfo{FsType: "xfs", Name: "/dev/sda2", Uuid: "12346", Label: "ROOT"}, partition)
})
}

Expand All @@ -280,8 +280,8 @@ func TestGetMountablePartitions(t *testing.T) {
parts, err := block.GetMountablePartitions(true)
require.NoError(t, err)
expected := []*PartitionInfo{
{Name: "/dev/sda2", FsType: "xfs"},
{Name: "/dev/sda3", FsType: "xfs"},
{Name: "/dev/sda2", FsType: "xfs", Uuid: "12345", Label: "ROOT"},
{Name: "/dev/sda3", FsType: "xfs", Uuid: "12346", Label: "ROOT"},
}
require.ElementsMatch(t, expected, parts)
})
Expand Down
5 changes: 5 additions & 0 deletions providers/os/connection/snapshot/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
type PartitionInfo struct {
Name string
FsType string

// (optional) Label is the partition label
Label string
// (optional) UUID is the partition UUID
Uuid string
}

func (entry BlockDevice) isNoBootVolume() bool {
Expand Down

0 comments on commit 9c866a6

Please sign in to comment.