Skip to content

Commit

Permalink
✨ Support searching /dev/disk/by-id aliases. (#4795)
Browse files Browse the repository at this point in the history
Signed-off-by: Preslav <[email protected]>
  • Loading branch information
preslavgerchev authored Nov 1, 2024
1 parent 79704b3 commit 9f6c79d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
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
42 changes: 25 additions & 17 deletions providers/os/connection/snapshot/blockdevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,40 @@ func (cmdRunner *LocalCommandRunner) GetBlockDevices() (*BlockDevices, error) {
if err := json.Unmarshal(data, blockEntries); err != nil {
return nil, err
}

blockEntries.findAliases()

return blockEntries, nil
}

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

for _, entry := range entries {
if entry.Type().Type() != os.ModeSymlink {
continue
}

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

targetName := strings.TrimPrefix(target, "/dev/")
blockEntries.findAlias(targetName, path)
for _, entry := range entries {
if entry.Type().Type() != os.ModeSymlink {
continue
}

path := fmt.Sprintf("%s/%s", path, 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)
}
}
}

Expand Down

0 comments on commit 9f6c79d

Please sign in to comment.