Skip to content

Commit 003edee

Browse files
⭐️ Refactor device connection and partition mounting (#5859)
* ✨ Refactor device connection: TBD on description. Signed-off-by: Preslav <preslav@mondoo.com> * impl ostree. Signed-off-by: Preslav <preslav@mondoo.com> * fix snapshot/blocks tests. Signed-off-by: Preslav <preslav@mondoo.com> * fix other tests, iprove structs. Signed-off-by: Preslav <preslav@mondoo.com> * Fix fstab tests. Signed-off-by: Preslav <preslav@mondoo.com> * Add requestedAs. Signed-off-by: Preslav <preslav@mondoo.com> * Update providers/os/connection/snapshot/blockdevices.go Co-authored-by: Mikita Iwanowski <info@slnt-opp.xyz> * Update providers/os/connection/device/device_manager.go Co-authored-by: Mikita Iwanowski <info@slnt-opp.xyz> --------- Signed-off-by: Preslav <preslav@mondoo.com> Co-authored-by: Mikita Iwanowski <info@slnt-opp.xyz>
1 parent f878de1 commit 003edee

File tree

10 files changed

+529
-629
lines changed

10 files changed

+529
-629
lines changed

providers/os/connection/device/device_connection.go

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package device
55

66
import (
77
"errors"
8-
"maps"
98
"runtime"
109
"slices"
1110
"strings"
@@ -41,7 +40,7 @@ type DeviceConnection struct {
4140

4241
MountedDirs []string
4342
// map of mountpoints to partition infos
44-
partitions map[string]*snapshot.PartitionInfo
43+
partitions map[string]*snapshot.MountedPartition
4544

4645
// whether to keep the devices mounted after the connection is closed
4746
keepMounted bool
@@ -78,13 +77,13 @@ func NewDeviceConnection(connId uint32, conf *inventory.Config, asset *inventory
7877
}
7978
log.Debug().Str("manager", manager.Name()).Msg("device manager created")
8079

81-
blocks, err := manager.IdentifyMountTargets(conf.Options)
80+
partitions, err := manager.IdentifyMountTargets(conf.Options)
8281
if err != nil {
8382
log.Warn().Err(err).Msg("device connection> unable to identify some mount targets, proceeding with the rest")
8483
}
8584

86-
if len(blocks) == 0 {
87-
return nil, errors.New("device connection> no blocks found, cannot perform a scan")
85+
if len(partitions) == 0 {
86+
return nil, errors.New("device connection> no partitions found, cannot perform a scan")
8887
}
8988

9089
deviceConnection := &DeviceConnection{
@@ -109,45 +108,26 @@ func NewDeviceConnection(connId uint32, conf *inventory.Config, asset *inventory
109108
asset.IdDetector = append(asset.IdDetector, ids.IdDetector_CloudDetect)
110109
}
111110

112-
deviceConnection.partitions = make(map[string]*snapshot.PartitionInfo)
113-
111+
deviceConnection.partitions = make(map[string]*snapshot.MountedPartition)
114112
skipAssetDetection := conf.Options[SkipAssetDetection] == "true"
115113

116-
// first, iterate over all blocks and mount them, if needed.
117-
for _, block := range blocks {
118-
logBuilder := log.Debug().
119-
Str("name", block.Name).
120-
Str("type", block.FsType).
121-
Str("mountpoint", block.MountPoint)
122-
if block.MountPoint == "" {
123-
logBuilder.Msg("device connection> mounting block device")
124-
scanDir, err := manager.Mount(block)
125-
if err != nil {
126-
log.Error().Err(err).Msg("unable to complete mount step")
127-
continue
128-
}
129-
block.MountPoint = scanDir
130-
} else {
131-
logBuilder.Msg("device connection> already mounted block device")
132-
}
133-
if !stringx.Contains(deviceConnection.MountedDirs, block.MountPoint) {
134-
log.Debug().
135-
Str("name", block.Name).
136-
Str("mountpoint", block.MountPoint).
137-
Msg("device connection> adding mountpoint to mounted dirs")
138-
deviceConnection.MountedDirs = append(deviceConnection.MountedDirs, block.MountPoint)
139-
}
140-
141-
deviceConnection.partitions[block.RootDir()] = block
114+
mountedPartitions, err := manager.Mount(partitions)
115+
if err != nil {
116+
log.Warn().Err(err).Msg("device connection> unable to mount some partitions, proceeding with the rest")
117+
}
118+
partNames := []string{}
119+
for _, part := range mountedPartitions {
120+
deviceConnection.MountedDirs = append(deviceConnection.MountedDirs, part.MountPoint)
121+
deviceConnection.partitions[part.MountPoint] = part
122+
partNames = append(partNames, part.Partition.Name)
142123
}
143-
144124
if skipAssetDetection {
145125
log.Debug().Msg("device connection> skipping asset detection as requested")
146126
return deviceConnection, nil
147127
}
148128

149129
log.Debug().
150-
Strs("partitions", slices.Collect(maps.Keys(deviceConnection.partitions))).
130+
Strs("partitions", partNames).
151131
Strs("mountedDirs", deviceConnection.MountedDirs).
152132
Msg("device connection> mounted partitions, proceeding with asset detection")
153133

@@ -164,15 +144,15 @@ func NewDeviceConnection(connId uint32, conf *inventory.Config, asset *inventory
164144
}
165145

166146
func (c *DeviceConnection) tryDetectAsset(conf *inventory.Config, asset *inventory.Asset) {
167-
for partition, block := range c.partitions {
168-
log.Debug().Str("partition", partition).Str("path", block.RootDir()).Str("name", block.Name).Msg("device connection> trying to detect asset")
169-
fsConn, err := TryDetectAssetFromPartition(c.ID(), block, conf, asset)
147+
for mp, partition := range c.partitions {
148+
log.Debug().Str("mountpoint", mp).Str("path", partition.RootFsPath()).Str("name", partition.Partition.Name).Msg("device connection> trying to detect asset")
149+
fsConn, err := TryDetectAssetFromPath(c.ID(), partition.RootFsPath(), conf, asset)
170150
if fsConn != nil {
171151
c.FileSystemConnection = fsConn
172152
return
173153
}
174154
if err != nil {
175-
log.Error().Err(err).Str("partition", partition).Msg("partition did not return an asset, continuing")
155+
log.Error().Err(err).Str("mountpoint", mp).Str("name", partition.Partition.Name).Msg("partition did not return an asset, continuing")
176156
}
177157
}
178158
}
@@ -224,19 +204,14 @@ func (p *DeviceConnection) Conf() *inventory.Config {
224204
return p.FileSystemConnection.Conf
225205
}
226206

227-
func (p *DeviceConnection) Partitions() map[string]*snapshot.PartitionInfo {
207+
func (p *DeviceConnection) Partitions() map[string]*snapshot.MountedPartition {
228208
if p.partitions == nil {
229-
p.partitions = make(map[string]*snapshot.PartitionInfo)
209+
p.partitions = make(map[string]*snapshot.MountedPartition)
230210
}
231211

232212
return p.partitions
233213
}
234214

235-
// TryDetectAsset tries to detect the OS on a given block device and returns the connection itself if an asset was detected
236-
func TryDetectAssetFromPartition(connId uint32, partition *snapshot.PartitionInfo, conf *inventory.Config, asset *inventory.Asset) (*fs.FileSystemConnection, error) {
237-
return TryDetectAssetFromPath(connId, partition.RootDir(), conf, asset)
238-
}
239-
240215
// TryDetectAssetFromPath tries to detect the OS on a given path and returns the connection itself if an asset was detected
241216
func TryDetectAssetFromPath(connId uint32, path string, conf *inventory.Config, asset *inventory.Asset) (*fs.FileSystemConnection, error) {
242217
// create and initialize fs provider

providers/os/connection/device/device_manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ type DeviceManager interface {
1111
// Name returns the name of the device manager
1212
Name() string
1313
// IdentifyMountTargets returns a list of partitions that match the given options and can be mounted
14-
IdentifyMountTargets(opts map[string]string) ([]*snapshot.PartitionInfo, error)
15-
// Mounts the partition and returns the directory it was mounted to
16-
Mount(pi *snapshot.PartitionInfo) (string, error)
14+
IdentifyMountTargets(opts map[string]string) ([]*snapshot.Partition, error)
15+
// Mounts partitions and returns the directories they were mounted to
16+
Mount(partitions []*snapshot.Partition) ([]*snapshot.MountedPartition, error)
1717
// UnmountAndClose unmounts the partitions from the specified dirs and closes the device manager
1818
UnmountAndClose()
1919
}

0 commit comments

Comments
 (0)