當前位置: 首頁>>代碼示例>>Golang>>正文


Golang SysFs.GetBlockDeviceNumbers方法代碼示例

本文整理匯總了Golang中github.com/google/cadvisor/utils/sysfs.SysFs.GetBlockDeviceNumbers方法的典型用法代碼示例。如果您正苦於以下問題:Golang SysFs.GetBlockDeviceNumbers方法的具體用法?Golang SysFs.GetBlockDeviceNumbers怎麽用?Golang SysFs.GetBlockDeviceNumbers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/google/cadvisor/utils/sysfs.SysFs的用法示例。


在下文中一共展示了SysFs.GetBlockDeviceNumbers方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: GetBlockDeviceInfo

// Get information about block devices present on the system.
// Uses the passed in system interface to retrieve the low level OS information.
func GetBlockDeviceInfo(sysfs sysfs.SysFs) (map[string]info.DiskInfo, error) {
	disks, err := sysfs.GetBlockDevices()
	if err != nil {
		return nil, err
	}

	diskMap := make(map[string]info.DiskInfo)
	for _, disk := range disks {
		name := disk.Name()
		// Ignore non-disk devices.
		// TODO(rjnagal): Maybe just match hd, sd, and dm prefixes.
		if strings.HasPrefix(name, "loop") || strings.HasPrefix(name, "ram") || strings.HasPrefix(name, "sr") {
			continue
		}
		disk_info := info.DiskInfo{
			Name: name,
		}
		dev, err := sysfs.GetBlockDeviceNumbers(name)
		if err != nil {
			return nil, err
		}
		n, err := fmt.Sscanf(dev, "%d:%d", &disk_info.Major, &disk_info.Minor)
		if err != nil || n != 2 {
			return nil, fmt.Errorf("could not parse device numbers from %s for device %s", dev, name)
		}
		out, err := sysfs.GetBlockDeviceSize(name)
		if err != nil {
			return nil, err
		}
		// Remove trailing newline before conversion.
		size, err := strconv.ParseUint(strings.TrimSpace(out), 10, 64)
		if err != nil {
			return nil, err
		}
		// size is in 512 bytes blocks.
		disk_info.Size = size * 512

		sched, err := sysfs.GetBlockDeviceScheduler(name)
		if err != nil {
			sched = "none"
		} else {
			matches := schedulerRegExp.FindSubmatch([]byte(sched))
			if len(matches) < 2 {
				sched = "none"
			} else {
				sched = string(matches[1])
			}
		}
		disk_info.Scheduler = sched
		device := fmt.Sprintf("%d:%d", disk_info.Major, disk_info.Minor)
		diskMap[device] = disk_info
	}
	return diskMap, nil
}
開發者ID:johnmccawley,項目名稱:origin,代碼行數:56,代碼來源:sysinfo.go


注:本文中的github.com/google/cadvisor/utils/sysfs.SysFs.GetBlockDeviceNumbers方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。