From 5a748323a19e49c4466d2b512b6e35b9b53172d2 Mon Sep 17 00:00:00 2001 From: Preslav Date: Wed, 30 Oct 2024 10:19:15 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Support=20searching=20/dev/disk/by-?= =?UTF-8?q?id=20aliases.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Preslav --- .../gcpinstancesnapshot/provider.go | 2 +- .../os/connection/snapshot/blockdevices.go | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/providers/gcp/connection/gcpinstancesnapshot/provider.go b/providers/gcp/connection/gcpinstancesnapshot/provider.go index d3328b2c6..4065f1448 100644 --- a/providers/gcp/connection/gcpinstancesnapshot/provider.go +++ b/providers/gcp/connection/gcpinstancesnapshot/provider.go @@ -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": diff --git a/providers/os/connection/snapshot/blockdevices.go b/providers/os/connection/snapshot/blockdevices.go index 55a50ce87..e73e85442 100644 --- a/providers/os/connection/snapshot/blockdevices.go +++ b/providers/os/connection/snapshot/blockdevices.go @@ -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 } @@ -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")