forked from project-machine/disko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.go
303 lines (239 loc) · 6.25 KB
/
system.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package linux
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"regexp"
"strings"
"syscall"
"golang.org/x/sys/unix"
"machinerun.io/disko"
"machinerun.io/disko/megaraid"
"machinerun.io/disko/smartpqi"
)
type linuxSystem struct {
raidctrls []RAIDController
}
// System returns an linux specific implementation of disko.System interface.
func System() disko.System {
return &linuxSystem{
raidctrls: []RAIDController{
megaraid.CachingStorCli(),
smartpqi.ArcConf(),
},
}
}
// example below, of an azure vmbus disk that is ephemeral.
// matching intent of /lib/udev/rules.d/66-azure-ephemeral.rules
// /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00/device:07/VMBUS:01/00000000-0001-8899-0000-000000000000/
//
// host1/target1:0:1/1:0:1:0/block/sdb
var vmbusSyspathEphemeral = regexp.MustCompile(`.*/VMBUS:\d\d/00000000-0001-\d{4}-\d{4}-\d{12}/host.*`)
func (ls *linuxSystem) ScanAllDisks(filter disko.DiskFilter) (disko.DiskSet, error) {
var err error
var dpaths = []string{}
names, err := getDiskNames()
if err != nil {
return disko.DiskSet{}, err
}
for _, name := range names {
dpath := path.Join("/dev", name)
f, err := os.Open(dpath)
if err != nil {
// ENOMEDIUM will occur on a empty sd reader.
if e, ok := err.(*os.PathError); ok {
if e.Err == syscall.ENOMEDIUM {
continue
}
}
log.Printf("Skipping device %s: %v", name, err)
continue
}
f.Close()
dpaths = append(dpaths, dpath)
}
return ls.ScanDisks(filter, dpaths...)
}
func (ls *linuxSystem) ScanDisks(filter disko.DiskFilter,
dpaths ...string) (disko.DiskSet, error) {
disks := disko.DiskSet{}
for _, dpath := range dpaths {
disk, err := ls.ScanDisk(dpath)
if err != nil {
return disks, err
}
if filter(disk) {
// Accepted so add to the set
disks[disk.Name] = disk
}
}
return disks, nil
}
func getDiskReadOnly(kname string) (bool, error) {
syspath, err := getSysPathForBlockDevicePath(kname)
if err != nil {
return false, err
}
syspathReadOnly := syspath + "/ro"
content, err := ioutil.ReadFile(syspathReadOnly)
if err != nil {
return false, err
}
val := strings.TrimRight(string(content), "\n")
if val == "1" {
return true, nil
} else if val == "0" {
return false, nil
}
return false, fmt.Errorf("unexpected value '%s' found in %s", syspathReadOnly, val)
}
func getDiskProperties(d disko.UdevInfo) disko.PropertySet {
props := disko.PropertySet{}
if vmbusSyspathEphemeral.MatchString(d.SysPath) {
props[disko.Ephemeral] = true
}
if d.Properties["ID_MODEL"] == "Amazon EC2 NVMe Instance Storage" {
props[disko.Ephemeral] = true
}
return props
}
//nolint:funlen
func (ls *linuxSystem) ScanDisk(devicePath string) (disko.Disk, error) {
var err error
var blockdev = true
var ssize uint = sectorSize512
var diskType disko.DiskType
var attachType disko.AttachmentType
var ro bool
name, err := getKnameForBlockDevicePath(devicePath)
if err != nil {
name = path.Base(devicePath)
blockdev = false
} else {
bss, err := getBlockSize(name)
if err != nil {
return disko.Disk{}, err
}
ssize = uint(bss)
}
udInfo := disko.UdevInfo{}
if blockdev {
udInfo, err = GetUdevInfo(name)
if err != nil {
return disko.Disk{}, err
}
attachType = getAttachType(udInfo)
for _, ctrl := range ls.raidctrls {
if IsSysPathRAID(udInfo.Properties["DEVPATH"], ctrl.DriverSysfsPath()) {
// we know this is device is part of a raid, so if we cannot get
// disk type we must return an error
dType, err := ctrl.GetDiskType(devicePath)
if err != nil {
return disko.Disk{}, fmt.Errorf("failed to get diskType of %q from RAID controller: %s", devicePath, err)
}
attachType = disko.RAID
diskType = dType
break
}
}
ro, err = getDiskReadOnly(name)
if err != nil {
return disko.Disk{}, err
}
} else {
diskType = disko.TYPEFILE
attachType = disko.FILESYSTEM
ro = false
if err := unix.Access(devicePath, unix.W_OK); err == unix.EACCES {
ro = true
} else if err != nil {
return disko.Disk{}, err
}
}
properties := getDiskProperties(udInfo)
disk := disko.Disk{
Name: name,
Path: devicePath,
SectorSize: ssize,
ReadOnly: ro,
UdevInfo: udInfo,
Type: diskType,
Attachment: attachType,
Properties: properties,
}
fh, err := os.Open(devicePath)
if err != nil {
return disk, err
}
defer fh.Close()
size, err := getFileSize(fh)
if err != nil {
return disk, err
}
disk.Size = size
parts, tType, ssize, err := findPartitions(fh)
if err != nil {
return disk, err
}
disk.Table = tType
if tType == disko.GPT && ssize != disk.SectorSize {
if blockdev {
return disk, fmt.Errorf(
"disk %s has sector size %d and partition table sector size %d",
disk.Path, disk.SectorSize, ssize)
}
disk.SectorSize = ssize
}
disk.Partitions = parts
return disk, nil
}
func (ls *linuxSystem) CreatePartition(d disko.Disk, p disko.Partition) error {
if err := addPartitionSet(d, disko.PartitionSet{p.Number: p}); err != nil {
return err
}
return udevSettle()
}
func (ls *linuxSystem) CreatePartitions(d disko.Disk, pSet disko.PartitionSet) error {
if err := addPartitionSet(d, pSet); err != nil {
return err
}
return udevSettle()
}
func (ls *linuxSystem) DeletePartition(d disko.Disk, number uint) error {
if err := deletePartitions(d, []uint{number}); err != nil {
return err
}
return udevSettle()
}
func (ls *linuxSystem) UpdatePartition(d disko.Disk, p disko.Partition) error {
if err := updatePartitions(d, disko.PartitionSet{p.Number: p}); err != nil {
return err
}
return udevSettle()
}
func (ls *linuxSystem) UpdatePartitions(d disko.Disk, pSet disko.PartitionSet) error {
if err := updatePartitions(d, pSet); err != nil {
return err
}
return udevSettle()
}
func (ls *linuxSystem) Wipe(d disko.Disk) error {
if err := wipeDisk(d); err != nil {
return err
}
return udevSettle()
}
func (ls *linuxSystem) GetDiskType(path string, udInfo disko.UdevInfo) (disko.DiskType, error) {
for _, ctrl := range ls.raidctrls {
if IsSysPathRAID(udInfo.Properties["DEVPATH"], ctrl.DriverSysfsPath()) {
dType, err := ctrl.GetDiskType(path)
if err != nil {
return disko.HDD, fmt.Errorf("failed to get diskType of %q from RAID controller: %s", path, err)
}
return dType, nil
}
}
return getDiskType(udInfo)
}