[disk][linux] add label based on devicemapper name of disk

This commit is contained in:
Ofek Lev 2018-04-26 18:27:15 -04:00
parent 63047d776e
commit 35ed9c11c7
2 changed files with 24 additions and 0 deletions

View File

@ -42,6 +42,7 @@ type IOCountersStat struct {
WeightedIO uint64 `json:"weightedIO"`
Name string `json:"name"`
SerialNumber string `json:"serialNumber"`
Label string `json:"label"`
}
func (d UsageStat) String() string {

View File

@ -5,6 +5,7 @@ package disk
import (
"context"
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
"strconv"
@ -370,6 +371,8 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
d.Name = name
d.SerialNumber = GetDiskSerialNumber(name)
d.Label = GetLabel(name)
ret[name] = d
}
return ret, nil
@ -406,6 +409,26 @@ func GetDiskSerialNumberWithContext(ctx context.Context, name string) string {
return ""
}
// GetLabel returns label of given device or empty string on error.
// Name of device is expected, eg. /dev/sda
// Supports label based on devicemapper name
// See https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm
func GetLabel(name string) string {
// Try label based on devicemapper name
dmname_filename := fmt.Sprintf("/sys/block/%s/dm/name", name)
if !common.PathExists(dmname_filename) {
return ""
}
dmname, err := ioutil.ReadFile(dmname_filename)
if err != nil {
return ""
} else {
return dmname
}
}
func getFsType(stat unix.Statfs_t) string {
t := int64(stat.Type)
ret, ok := fsTypeMap[t]