Skip to content

Commit

Permalink
Properly detect dm devices
Browse files Browse the repository at this point in the history
  • Loading branch information
avishayt committed Jan 31, 2022
1 parent 4d0ed8f commit 1a26844
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
DRIVE_TYPE_FDD // Floppy disk drive
DRIVE_TYPE_ODD // Optical disk drive
DRIVE_TYPE_SSD // Solid-state drive
DRIVE_TYPE_MAPPER // Mapper device
)

var (
Expand All @@ -38,6 +39,7 @@ var (
DRIVE_TYPE_FDD: "FDD",
DRIVE_TYPE_ODD: "ODD",
DRIVE_TYPE_SSD: "SSD",
DRIVE_TYPE_MAPPER: "Mapper",
}

// NOTE(fromani): the keys are all lowercase and do not match
Expand All @@ -51,6 +53,7 @@ var (
"fdd": DRIVE_TYPE_FDD,
"odd": DRIVE_TYPE_ODD,
"ssd": DRIVE_TYPE_SSD,
"mapper": DRIVE_TYPE_MAPPER,
}
)

Expand Down Expand Up @@ -163,6 +166,7 @@ type Disk struct {
SerialNumber string `json:"serial_number"`
WWN string `json:"wwn"`
Partitions []*Partition `json:"partitions"`
Members []string `json:"members"`
// TODO(jaypipes): Add PCI field for accessing PCI device information
// PCI *PCIDevice `json:"pci"`
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/block/block_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,20 @@ func diskIsRemovable(paths *linuxpath.Paths, disk string) bool {
return removable == "1"
}

func diskMembers(ctx *context.Context, paths *linuxpath.Paths, disk string) []string {
out := make([]string, 0)
path := filepath.Join(paths.SysBlock, disk, "slaves")
files, err := ioutil.ReadDir(path)
if err != nil {
ctx.Warn("failed to read slaves: %s\n", err)
return out
}
for _, file := range files {
out = append(out, file.Name())
}
return out
}

func disks(ctx *context.Context, paths *linuxpath.Paths) []*Disk {
// In Linux, we could use the fdisk, lshw or blockdev commands to list disk
// information, however all of these utilities require root privileges to
Expand Down Expand Up @@ -283,6 +297,7 @@ func disks(ctx *context.Context, paths *linuxpath.Paths) []*Disk {
serialNo := diskSerialNumber(paths, dname)
wwn := diskWWN(paths, dname)
removable := diskIsRemovable(paths, dname)
members := diskMembers(ctx, paths, dname)

d := &Disk{
Name: dname,
Expand All @@ -297,6 +312,7 @@ func disks(ctx *context.Context, paths *linuxpath.Paths) []*Disk {
Model: model,
SerialNumber: serialNo,
WWN: wwn,
Members: members,
}

parts := diskPartitions(ctx, paths, dname)
Expand Down Expand Up @@ -345,6 +361,8 @@ func diskTypes(dname string) (
} else if strings.HasPrefix(dname, "mmc") {
driveType = DRIVE_TYPE_SSD
storageController = STORAGE_CONTROLLER_MMC
} else if strings.HasPrefix(dname, "dm-") {
driveType = DRIVE_TYPE_MAPPER
}

return driveType, storageController
Expand Down

0 comments on commit 1a26844

Please sign in to comment.