forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
108 lines (100 loc) · 2.07 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
package ghw
import (
"fmt"
"math"
)
type Disk struct {
Name string
SizeBytes uint64
SectorSizeBytes uint64
BusType string
Vendor string
SerialNumber string
Partitions []*Partition
}
type Partition struct {
Disk *Disk
Name string
Label string
MountPoint string
SizeBytes uint64
Type string
IsReadOnly bool
}
type BlockInfo struct {
TotalPhysicalBytes uint64
Disks []*Disk
Partitions []*Partition
}
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 {
vendor := ""
if d.Vendor != "" {
vendor = " " + d.Vendor
}
serial := ""
if d.SerialNumber != "" {
serial = " - SN #" + d.SerialNumber
}
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)
}
return fmt.Sprintf(
"/dev/%s (%s) [%s]%s%s",
d.Name,
sizeStr,
d.BusType,
vendor,
serial,
)
}
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,
)
}