forked from rook/rook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.go
258 lines (220 loc) · 6.65 KB
/
disk.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
/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package clusterd
import (
"errors"
"fmt"
"os"
"path"
"regexp"
"strconv"
"github.com/coreos/pkg/capnslog"
"github.com/rook/rook/pkg/util/exec"
"github.com/rook/rook/pkg/util/sys"
)
var (
logger = capnslog.NewPackageLogger("github.com/rook/rook", "inventory")
isRBD = regexp.MustCompile("^rbd[0-9]+p?[0-9]{0,}$")
listAllDevices = "all"
// We need to allow dm- devices (Kubernetes mountpoints) because dm- devices name are used as names for meta device
allowDeviceNamePattern = regexp.MustCompile("dm-")
)
func supportedDeviceType(device string) bool {
return device == sys.DiskType ||
device == sys.SSDType ||
device == sys.CryptType ||
device == sys.LVMType ||
device == sys.MultiPath ||
device == sys.PartType ||
device == sys.LinearType ||
(getAllowLoopDevices() && device == sys.LoopType)
}
// GetDeviceEmpty check whether a device is completely empty
func GetDeviceEmpty(device *sys.LocalDisk) bool {
return supportedDeviceType(device.Type) && len(device.Partitions) == 0 && device.Filesystem == ""
}
func ignoreDevice(d string) bool {
return isRBD.MatchString(d)
}
func DiscoverDevicesWithFilter(executor exec.Executor, deviceFilter, metaDevice string) ([]*sys.LocalDisk, error) {
var disks []*sys.LocalDisk
devices, err := sys.ListDevices(executor)
if err != nil {
return nil, err
}
for _, d := range devices {
// Ignore RBD device
if ignoreDevice(d) {
// skip device
logger.Warningf("skipping rbd device %q", d)
continue
}
if deviceFilter != "" && !deviceMatchWithFilter(d, deviceFilter, metaDevice) {
logger.Warningf("device %q skipped due to filter %q", d, deviceFilter)
continue
}
// Populate device information coming from lsblk
disk, err := PopulateDeviceInfo(d, executor)
if err != nil {
logger.Warningf("skipping device %q. %v", d, err)
continue
}
// Populate udev information coming from udev
disk, err = PopulateDeviceUdevInfo(d, executor, disk)
if err != nil {
// go on without udev info
// not ideal for our filesystem check later but we can't really fail either...
logger.Warningf("failed to get udev info for device %q. %v", d, err)
}
// Test if device has child, if so we skip it and only consider the partitions
// which will come in later iterations of the loop
// We only test if the type is 'disk', this is a property reported by lsblk
// and means it's a parent block device
if disk.Type == sys.DiskType {
deviceChild, err := sys.ListDevicesChild(executor, fmt.Sprintf("/dev/%s", d))
if err != nil {
logger.Warningf("failed to detect child devices for device %q, assuming they are none. %v", d, err)
}
// lsblk will output at least 2 lines if they are partitions, one for the parent
// and N for the child
if len(deviceChild) > 1 {
logger.Infof("skipping device %q because it has child, considering the child instead.", d)
continue
}
}
disks = append(disks, disk)
}
logger.Debug("discovered disks are:")
for _, disk := range disks {
logger.Debugf("%+v", disk)
}
return disks, nil
}
func deviceMatchWithFilter(device string, filter, metaDevice string) bool {
if filter == listAllDevices || device == metaDevice {
return true
}
if allowDeviceNamePattern.MatchString(device) {
return true
}
pattern, err := regexp.Compile(filter)
if err != nil {
return false
}
if !pattern.MatchString(device) {
return false
}
return true
}
// DiscoverDevices returns all the details of devices available on the local node
func DiscoverDevices(executor exec.Executor) ([]*sys.LocalDisk, error) {
disks, err := DiscoverDevicesWithFilter(executor, "", "")
if err != nil {
return nil, err
}
return disks, nil
}
// PopulateDeviceInfo returns the information of the specified block device
func PopulateDeviceInfo(d string, executor exec.Executor) (*sys.LocalDisk, error) {
diskProps, err := sys.GetDeviceProperties(d, executor)
if err != nil {
return nil, err
}
diskType, ok := diskProps["TYPE"]
if !ok {
return nil, errors.New("diskType is empty")
}
if !supportedDeviceType(diskType) {
return nil, fmt.Errorf("unsupported diskType %+s", diskType)
}
// get the UUID for disks
var diskUUID string
if diskType == sys.DiskType {
uuid, err := sys.GetDiskUUID(d, executor)
if err != nil {
logger.Warning(err)
} else {
diskUUID = uuid
}
}
disk := &sys.LocalDisk{Name: d, UUID: diskUUID}
if val, ok := diskProps["TYPE"]; ok {
disk.Type = val
}
if val, ok := diskProps["SIZE"]; ok {
if size, err := strconv.ParseUint(val, 10, 64); err == nil {
disk.Size = size
}
}
if val, ok := diskProps["ROTA"]; ok {
if rotates, err := strconv.ParseBool(val); err == nil {
disk.Rotational = rotates
}
}
if val, ok := diskProps["RO"]; ok {
if ro, err := strconv.ParseBool(val); err == nil {
disk.Readonly = ro
}
}
if val, ok := diskProps["PKNAME"]; ok {
if val != "" {
disk.Parent = path.Base(val)
}
}
if val, ok := diskProps["NAME"]; ok {
disk.RealPath = val
}
if val, ok := diskProps["KNAME"]; ok {
disk.KernelName = path.Base(val)
}
if val, ok := diskProps["FSTYPE"]; ok && val != "" {
disk.Filesystem = path.Base(val)
}
if val, ok := diskProps["MOUNTPOINT"]; ok && val != "" {
disk.Mountpoint = path.Base(val)
}
return disk, nil
}
// PopulateDeviceUdevInfo fills the udev info into the block device information
func PopulateDeviceUdevInfo(d string, executor exec.Executor, disk *sys.LocalDisk) (*sys.LocalDisk, error) {
udevInfo, err := sys.GetUdevInfo(d, executor)
if err != nil {
return disk, err
}
// parse udev info output
if val, ok := udevInfo["DEVLINKS"]; ok {
disk.DevLinks = val
}
if val, ok := udevInfo["ID_FS_TYPE"]; ok {
disk.Filesystem = val
}
if val, ok := udevInfo["ID_SERIAL"]; ok {
disk.Serial = val
}
if val, ok := udevInfo["ID_VENDOR"]; ok {
disk.Vendor = val
}
if val, ok := udevInfo["ID_MODEL"]; ok {
disk.Model = val
}
if val, ok := udevInfo["ID_WWN_WITH_EXTENSION"]; ok {
disk.WWNVendorExtension = val
}
if val, ok := udevInfo["ID_WWN"]; ok {
disk.WWN = val
}
return disk, nil
}
func getAllowLoopDevices() bool {
return os.Getenv("CEPH_VOLUME_ALLOW_LOOP_DEVICES") == "true"
}