forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
140 lines (131 loc) · 2.99 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import (
"fmt"
"math"
)
// Disk describes a single disk drive on the host system. Disk drives provide
// raw block storage resources.
type Disk struct {
Name string
SizeBytes uint64
PhysicalBlockSizeBytes uint64
BusType string
BusPath string
NUMANodeID int
Vendor string
Model string
SerialNumber string
WWN string
Partitions []*Partition
}
// Partition describes a logical division of a Disk.
type Partition struct {
Disk *Disk
Name string
Label string
MountPoint string
SizeBytes uint64
Type string
IsReadOnly bool
}
// BlockInfo describes all disk drives and partitions in the host system.
type BlockInfo struct {
TotalPhysicalBytes uint64
Disks []*Disk
Partitions []*Partition
}
// Block returns a BlockInfo struct that describes the block storage resources
// of the host system.
func Block() (*BlockInfo, error) {
info := &BlockInfo{}
err := blockFillInfo(info)
if err != nil {
return nil, err
}
return info, nil
}
func (i *BlockInfo) String() string {
tpbs := UNKNOWN
if i.TotalPhysicalBytes > 0 {
tpb := i.TotalPhysicalBytes
unit, unitStr := unitWithString(int64(tpb))
tpb = uint64(math.Ceil(float64(tpb) / float64(unit)))
tpbs = fmt.Sprintf("%d%s", tpb, unitStr)
}
dplural := "disks"
if len(i.Disks) == 1 {
dplural = "disk"
}
return fmt.Sprintf("block storage (%d %s, %s physical storage)",
len(i.Disks), dplural, tpbs)
}
func (d *Disk) String() string {
sizeStr := UNKNOWN
if d.SizeBytes > 0 {
size := d.SizeBytes
unit, unitStr := unitWithString(int64(size))
size = uint64(math.Ceil(float64(size) / float64(unit)))
sizeStr = fmt.Sprintf("%d%s", size, unitStr)
}
atNode := ""
if d.NUMANodeID >= 0 {
atNode = fmt.Sprintf(" (node #%d)", d.NUMANodeID)
}
vendor := ""
if d.Vendor != "" {
vendor = " vendor=" + d.Vendor
}
model := ""
if d.Model != UNKNOWN {
model = " model=" + d.Model
}
serial := ""
if d.SerialNumber != UNKNOWN {
serial = " serial=" + d.SerialNumber
}
wwn := ""
if d.WWN != UNKNOWN {
wwn = " WWN=" + d.WWN
}
return fmt.Sprintf(
"/dev/%s (%s) [%s @ %s%s]%s%s%s%s",
d.Name,
sizeStr,
d.BusType,
d.BusPath,
atNode,
vendor,
model,
serial,
wwn,
)
}
func (p *Partition) String() string {
typeStr := ""
if p.Type != "" {
typeStr = fmt.Sprintf("[%s]", p.Type)
}
mountStr := ""
if p.MountPoint != "" {
mountStr = fmt.Sprintf(" mounted@%s", p.MountPoint)
}
sizeStr := UNKNOWN
if p.SizeBytes > 0 {
size := p.SizeBytes
unit, unitStr := unitWithString(int64(size))
size = uint64(math.Ceil(float64(size) / float64(unit)))
sizeStr = fmt.Sprintf("%d%s", size, unitStr)
}
return fmt.Sprintf(
"/dev/%s (%s) %s%s",
p.Name,
sizeStr,
typeStr,
mountStr,
)
}