/
usedbyprobe.go
214 lines (180 loc) · 6.2 KB
/
usedbyprobe.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
/*
Copyright 2020 The OpenEBS Authors
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 probe
import (
"errors"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/openebs/node-disk-manager/blockdevice"
"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
"github.com/openebs/node-disk-manager/pkg/blkid"
"github.com/openebs/node-disk-manager/pkg/spdk"
libudevwrapper "github.com/openebs/node-disk-manager/pkg/udev"
"github.com/openebs/node-disk-manager/pkg/util"
"k8s.io/klog"
)
type usedbyProbe struct {
Controller *controller.Controller
BlkidIdentifier *blkid.DeviceIdentifier
}
const (
usedbyProbeConfigKey = "used-by-probe"
usedbyProbePriority = 5
k8sLocalVolumePath1 = "kubernetes.io/local-volume"
k8sLocalVolumePath2 = "kubernetes.io~local-volume"
zfsFileSystemLabel = "zfs_member"
)
var (
usedbyProbeName = "used-by probe"
usedbyProbeState = defaultEnabled
)
var usedbyProbeRegister = func() {
// Get a controller object
ctrl := <-controller.ControllerBroadcastChannel
if ctrl == nil {
klog.Error("unable to configure", usedbyProbeName)
return
}
if ctrl.NDMConfig != nil {
for _, probeConfig := range ctrl.NDMConfig.ProbeConfigs {
if probeConfig.Key == usedbyProbeConfigKey {
usedbyProbeName = probeConfig.Name
usedbyProbeState = util.CheckTruthy(probeConfig.State)
break
}
}
}
newRegisterProbe := ®isterProbe{
priority: usedbyProbePriority,
name: usedbyProbeName,
state: usedbyProbeState,
pi: &usedbyProbe{Controller: ctrl},
controller: ctrl,
}
// Here we register the used-by probe
newRegisterProbe.register()
}
func newUsedByProbe(devPath string) *usedbyProbe {
usedbyProbe := &usedbyProbe{
BlkidIdentifier: &blkid.DeviceIdentifier{
DevPath: devPath,
},
}
return usedbyProbe
}
func (sp *usedbyProbe) Start() {}
func (sp *usedbyProbe) FillBlockDeviceDetails(blockDevice *blockdevice.BlockDevice) {
if blockDevice.DevPath == "" {
klog.Errorf("device identifier found empty, used-by probe will not fetch information")
return
}
// checking for local PV on the device
for _, mountPoint := range blockDevice.FSInfo.MountPoint {
if strings.Contains(mountPoint, k8sLocalVolumePath1) ||
strings.Contains(mountPoint, k8sLocalVolumePath2) {
blockDevice.DevUse.InUse = true
blockDevice.DevUse.UsedBy = blockdevice.LocalPV
klog.V(4).Infof("device: %s Used by: %s filled by used-by probe", blockDevice.DevPath, blockDevice.DevUse.UsedBy)
return
}
}
// checking for cstor and zfs localPV
// we start with the assumption that device has a zfs file system
lookupZFS := true
devPath := blockDevice.DevPath
// if device is not a partition, check whether the zfs partitions are available on the disk
if blockDevice.DeviceAttributes.DeviceType != libudevwrapper.UDEV_PARTITION {
path, ok := getBlockDeviceZFSPartition(*blockDevice)
if ok {
devPath = path
} else {
lookupZFS = false
klog.V(4).Infof("device: %s is not having any zfs partitions", blockDevice.DevPath)
}
}
// only if lookupZFS is true, we need to check for the zfs filesystem on the disk.
if lookupZFS {
usedByProbe := newUsedByProbe(devPath)
// check for ZFS file system
fstype := usedByProbe.BlkidIdentifier.GetOnDiskFileSystem()
if fstype == zfsFileSystemLabel {
blockDevice.DevUse.InUse = true
// the disk can either be in use by cstor or zfs local PV
ok, err := isBlockDeviceInUseByKernel(blockDevice.DevPath)
if err != nil {
klog.Errorf("error checking block device: %s: %v", blockDevice.DevPath, err)
}
if ok {
blockDevice.DevUse.UsedBy = blockdevice.ZFSLocalPV
} else {
blockDevice.DevUse.UsedBy = blockdevice.CStor
}
klog.V(4).Infof("device: %s Used by: %s filled by used-by probe", blockDevice.DevPath, blockDevice.DevUse.UsedBy)
return
}
}
// create a device identifier for reading the spdk super block from the disk
spdkIdentifier := &spdk.DeviceIdentifier{
DevPath: blockDevice.DevPath,
}
signature, err := spdkIdentifier.GetSPDKSuperBlockSignature()
if err != nil {
klog.Errorf("error reading spdk signature from device: %s, %v", blockDevice.DevPath, err)
}
if spdk.IsSPDKSignatureExist(signature) {
blockDevice.DevUse.InUse = true
blockDevice.DevUse.UsedBy = blockdevice.Mayastor
klog.V(4).Infof("device: %s Used by: %s filled by used-by probe", blockDevice.DevPath, blockDevice.DevUse.UsedBy)
return
}
// TODO jiva disk detection
}
// getBlockDeviceZFSPartition is used to get the zfs partition if it exist in a
// given BD
func getBlockDeviceZFSPartition(bd blockdevice.BlockDevice) (string, bool) {
// check for zfs partitions only if there are 2 partitions on the block device
if len(bd.DependentDevices.Partitions) != 2 {
return "", false
}
zfsDataPartitionNumber := "1"
zfsMetaPartitionNumber := "9"
// to handle cases of devices with nvme drives, the device name will be
// nvme0n1
if util.IsMatchRegex(".+[0-9]+$", bd.DevPath) {
zfsDataPartitionNumber = "p" + zfsDataPartitionNumber
zfsMetaPartitionNumber = "p" + zfsMetaPartitionNumber
}
dataPartition := bd.DevPath + zfsDataPartitionNumber
metaPartition := bd.DevPath + zfsMetaPartitionNumber
// check if device has the 2 partitions
if bd.DependentDevices.Partitions[0] == dataPartition &&
bd.DependentDevices.Partitions[1] == metaPartition {
return dataPartition, true
}
return "", false
}
// isBlockDeviceInUseByKernel tries to open the device exclusively to check if the device is
// being held by some process. eg: If kernel zfs uses the disk, the open will fail
func isBlockDeviceInUseByKernel(path string) (bool, error) {
f, err := os.OpenFile(filepath.Clean(path), os.O_EXCL, 0444)
if errors.Is(err, syscall.EBUSY) {
return true, nil
}
if err != nil {
return false, err
}
defer f.Close()
return false, nil
}