@@ -5,7 +5,6 @@ package device
55
66import (
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
166146func (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
241216func TryDetectAssetFromPath (connId uint32 , path string , conf * inventory.Config , asset * inventory.Asset ) (* fs.FileSystemConnection , error ) {
242217 // create and initialize fs provider
0 commit comments