Skip to content

Commit

Permalink
feat: BlockDevices symlink aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
slntopp committed Oct 22, 2024
1 parent 301452a commit 61ba6f0
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion providers/os/connection/snapshot/blockdevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"math"
"os"
"sort"
"strings"

Expand All @@ -27,6 +28,8 @@ type BlockDevice struct {
MountPoint string `json:"mountpoint,omitempty"`
Children []BlockDevice `json:"children,omitempty"`
Size int `json:"size,omitempty"`

Aliases []string `json:"-"`
}

type PartitionInfo struct {
Expand Down Expand Up @@ -54,9 +57,44 @@ 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
}

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

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

log.Info().Any("target", target).Msg("file")
targetName := strings.TrimPrefix(target, "/dev/")
for i := range blockEntries.BlockDevices {
device := blockEntries.BlockDevices[i]
if targetName == device.Name {
device.Aliases = append(device.Aliases, path)
blockEntries.BlockDevices[i] = device
continue process_symlinks
}
}
}
}

func (blockEntries BlockDevices) GetRootBlockEntry() (*PartitionInfo, error) {
log.Debug().Msg("get root block entry")
for i := range blockEntries.BlockDevices {
Expand Down Expand Up @@ -168,7 +206,16 @@ func (blockEntries BlockDevices) FindDevice(requested string) (BlockDevice, erro
return blockEntries.BlockDevices[i], nil
}

if LongestMatchingSuffix(lmsCache, requested, devices[i].Name) < LongestMatchingSuffix(lmsCache, requested, devices[i+1].Name) {
lms := LongestMatchingSuffix(lmsCache, requested, devices[i].Name)
for _, alias := range devices[i].Aliases {
aliasLms := LongestMatchingSuffix(map[string]int{}, requested, alias)
if aliasLms > lms {
lms = aliasLms
lmsCache[devices[i].Name] = aliasLms
}
}

if lms < LongestMatchingSuffix(lmsCache, requested, devices[i+1].Name) {
devices[i], devices[i+1] = devices[i+1], devices[i]
sorted = false
}
Expand Down

0 comments on commit 61ba6f0

Please sign in to comment.