Get information about block devices present on the system. Uses the passed in system interface to retrieve the low level OS information.
(sysfs sysfs.SysFs)
| 45 | // Get information about block devices present on the system. |
| 46 | // Uses the passed in system interface to retrieve the low level OS information. |
| 47 | func GetBlockDeviceInfo(sysfs sysfs.SysFs) (map[string]info.DiskInfo, error) { |
| 48 | disks, err := sysfs.GetBlockDevices() |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | diskMap := make(map[string]info.DiskInfo) |
| 54 | for _, disk := range disks { |
| 55 | name := disk.Name() |
| 56 | // Ignore non-disk devices. |
| 57 | // TODO(rjnagal): Maybe just match hd, sd, and dm prefixes. |
| 58 | if strings.HasPrefix(name, "loop") || strings.HasPrefix(name, "ram") || strings.HasPrefix(name, "sr") { |
| 59 | continue |
| 60 | } |
| 61 | // Ignore "hidden" devices (i.e. nvme path device sysfs entries). |
| 62 | // These devices are in the form of /dev/nvme$Xc$Yn$Z and will |
| 63 | // not have a device handle (i.e. "hidden") |
| 64 | isHidden, err := sysfs.IsBlockDeviceHidden(name) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | if isHidden { |
| 69 | continue |
| 70 | } |
| 71 | diskInfo := info.DiskInfo{ |
| 72 | Name: name, |
| 73 | } |
| 74 | dev, err := sysfs.GetBlockDeviceNumbers(name) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | n, err := fmt.Sscanf(dev, "%d:%d", &diskInfo.Major, &diskInfo.Minor) |
| 79 | if err != nil || n != 2 { |
| 80 | return nil, fmt.Errorf("could not parse device numbers from %s for device %s", dev, name) |
| 81 | } |
| 82 | out, err := sysfs.GetBlockDeviceSize(name) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | // Remove trailing newline before conversion. |
| 87 | size, err := strconv.ParseUint(strings.TrimSpace(out), 10, 64) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | // size is in 512 bytes blocks. |
| 92 | diskInfo.Size = size * 512 |
| 93 | |
| 94 | diskInfo.Scheduler = "none" |
| 95 | blkSched, err := sysfs.GetBlockDeviceScheduler(name) |
| 96 | if err == nil { |
| 97 | matches := schedulerRegExp.FindSubmatch([]byte(blkSched)) |
| 98 | if len(matches) >= 2 { |
| 99 | diskInfo.Scheduler = string(matches[1]) |
| 100 | } |
| 101 | } |
| 102 | device := fmt.Sprintf("%d:%d", diskInfo.Major, diskInfo.Minor) |
| 103 | diskMap[device] = diskInfo |
| 104 | } |
searching dependent graphs…