Skip to content

Commit

Permalink
✨ Support searching /dev/disk/by-id aliases.
Browse files Browse the repository at this point in the history
Signed-off-by: Preslav <[email protected]>
  • Loading branch information
preslavgerchev committed Nov 1, 2024
1 parent 8e25e5c commit 5a74832
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion providers/gcp/connection/gcpinstancesnapshot/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func NewGcpSnapshotConnection(id uint32, conf *inventory.Config, asset *inventor
// setup disk image so and attach it to the instance
var diskUrl string
mi := mountInfo{
deviceName: "/dev/sdh",
deviceName: "cnspec",
}
switch target.TargetType {
case "instance":
Expand Down
31 changes: 31 additions & 0 deletions providers/os/connection/snapshot/blockdevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ func (cmdRunner *LocalCommandRunner) GetBlockDevices() (*BlockDevices, error) {
if err := json.Unmarshal(data, blockEntries); err != nil {
return nil, err
}

// looks into /dev for aliases
blockEntries.findAliases()
// looks into /dev/disk/by-id for aliases
blockEntries.findAliasesById()

return blockEntries, nil
}
Expand Down Expand Up @@ -99,6 +103,33 @@ func (blockEntries *BlockDevices) findAliases() {
}
}

func (blockEntries *BlockDevices) findAliasesById() {
entries, err := os.ReadDir("/dev/disk/by-id")
if err != nil {
log.Warn().Err(err).Msg("Can't read /dev/disk/by-id directory")
return
}

for _, entry := range entries {
if entry.Type().Type() != os.ModeSymlink {
continue
}
path := fmt.Sprintf("/dev/disk/by-id/%s", entry.Name())
target, err := os.Readlink(path)
if err != nil {
log.Warn().Err(err).Str("path", path).Msg("Can't read link target")
continue
}

parts := strings.Split(target, "/")
if len(parts) == 0 {
continue
}
target = parts[len(parts)-1]
blockEntries.findAlias(target, path)
}
}

// Searches for a device by name
func (blockEntries BlockDevices) FindDevice(requested string) (BlockDevice, error) {
log.Debug().Str("device", requested).Msg("searching for device")
Expand Down

0 comments on commit 5a74832

Please sign in to comment.