Skip to content

Commit

Permalink
log debug lsblk output.
Browse files Browse the repository at this point in the history
  • Loading branch information
preslavgerchev committed May 27, 2024
1 parent 067ae9e commit 15a043b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
49 changes: 40 additions & 9 deletions providers/os/connection/snapshot/blockdevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type BlockDevices struct {
BlockDevices []BlockDevice `json:"blockDevices,omitempty"`
}

type ExcludeFilter struct {
ExcludePartLabel string
}

type BlockDevice struct {
Name string `json:"name,omitempty"`
FsType string `json:"fstype,omitempty"`
Expand All @@ -26,15 +30,18 @@ type BlockDevice struct {
MountPoint string `json:"mountpoint,omitempty"`
Children []BlockDevice `json:"children,omitempty"`
Size int `json:"size,omitempty"`
PartLabel string `json:"partlabel,omitempty"`
}

type PartitionInfo struct {
Name string
FsType string
Uuid string
Lvm bool
}

func (cmdRunner *LocalCommandRunner) GetBlockDevices() (*BlockDevices, error) {
cmd, err := cmdRunner.RunCommand("lsblk -bo NAME,SIZE,FSTYPE,MOUNTPOINT,LABEL,UUID --json")
cmd, err := cmdRunner.RunCommand("lsblk -bo NAME,SIZE,FSTYPE,MOUNTPOINT,LABEL,UUID,PARTLABEL --json")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -73,8 +80,9 @@ func (blockEntries BlockDevices) GetRootBlockEntry() (*PartitionInfo, error) {
return nil, errors.New("target volume not found on instance")
}

func (blockEntries BlockDevices) GetBlockEntryByName(name string) (*PartitionInfo, error) {
func (blockEntries BlockDevices) GetBlockEntryByName(name string, filters *ExcludeFilter) (*PartitionInfo, error) {
log.Debug().Str("name", name).Msg("get matching block entry")
candidates := []BlockDevice{}
var secondName string
if strings.HasPrefix(name, "/dev/sd") {
// sdh and xvdh are interchangeable
Expand All @@ -94,19 +102,36 @@ func (blockEntries BlockDevices) GetBlockEntryByName(name string) (*PartitionInf
}
}
log.Debug().Str("location", d.Name).Msg("found match")
// sort the children by size, so we can pick the largest one
sort.Slice(d.Children, func(i, j int) bool {
return d.Children[i].Size > d.Children[j].Size
})
for _, entry := range d.Children {
log.Debug().Str("name", entry.Name).Int("size", entry.Size).Msg("checking partition")
if filters != nil && strings.Contains(entry.PartLabel, filters.ExcludePartLabel) {
log.Debug().Str("name", entry.Name).Str("partlabel", entry.PartLabel).Msg("skipping partition, excluded (partlabel)")
continue
}

if entry.IsNotBootOrRootVolumeAndUnmounted() {
devFsName := "/dev/" + entry.Name
return &PartitionInfo{Name: devFsName, FsType: entry.FsType}, nil
if entry.IsLvm() {
log.Debug().Str("name", entry.Name).Msg("found lvm partition")
candidates = append(candidates, entry.Children[0])
} else {
candidates = append(candidates, entry)
}
}
}
}
return nil, errors.New("target volume not found on instance")

if len(candidates) == 0 {
return nil, errors.New("target volume not found on instance")
}

// sort the candidates by size, so we can pick the largest one
sort.Slice(candidates, func(i, j int) bool {
return candidates[i].Size > candidates[j].Size
})

// return the largest partition
devFsName := "/dev/" + candidates[0].Name
return &PartitionInfo{Name: devFsName, FsType: candidates[0].FsType}, nil
}

func (blockEntries BlockDevices) GetUnnamedBlockEntry() (*PartitionInfo, error) {
Expand Down Expand Up @@ -164,3 +189,9 @@ func (entry BlockDevice) IsRootVolume() bool {
func (entry BlockDevice) IsNotBootOrRootVolumeAndUnmounted() bool {
return entry.IsNoBootVolume() && entry.MountPoint == "" && !entry.IsRootVolume()
}

func (entry BlockDevice) IsLvm() bool {
log.Debug().Interface("part", entry).Msg("checking if lvm")

return strings.Contains(entry.FsType, "LVM2") && len(entry.Children) > 0
}
4 changes: 2 additions & 2 deletions providers/os/connection/snapshot/volumemounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (m *VolumeMounter) getFsInfo() (*PartitionInfo, error) {
return blockEntries.GetUnnamedBlockEntry()
}

fsInfo, err = blockEntries.GetBlockEntryByName(m.VolumeAttachmentLoc)
fsInfo, err = blockEntries.GetBlockEntryByName(m.VolumeAttachmentLoc, nil)
if err == nil && fsInfo != nil {
return fsInfo, nil
} else {
Expand Down Expand Up @@ -139,7 +139,7 @@ func (m *VolumeMounter) GetDeviceForMounting(target string) (*PartitionInfo, err
// we need to simplify those
return blockDevices.GetUnnamedBlockEntry()
}
return blockDevices.GetBlockEntryByName(target)
return blockDevices.GetBlockEntryByName(target, nil)
}

func (m *VolumeMounter) mountVolume(fsInfo *PartitionInfo) error {
Expand Down

0 comments on commit 15a043b

Please sign in to comment.