forked from kubernetes-sigs/ibm-powervs-block-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 5
/
device.go
242 lines (206 loc) · 5.97 KB
/
device.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
/*
Copyright 2023 The Kubernetes 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 device
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"k8s.io/klog/v2"
)
var (
scanLock = &sync.Mutex{}
)
type LinuxDevice interface {
// GetDevice() bool
DeleteDevice() (err error)
CreateDevice() (err error)
GetMapper() string
Populate(bool) error
}
// Device struct
type Device struct {
Mapper string `json:"mapper,omitempty"`
WWN string `json:"wwn,omitempty"`
Slaves int `json:"slaves,omitempty"`
}
// NewLinuxDevice new device with given wwn
func NewLinuxDevice(wwn string) LinuxDevice {
return &Device{
WWN: wwn,
}
}
func (d *Device) GetMapper() string {
return d.Mapper
}
// Populate get all linux Devices
func (d *Device) Populate(needActivePath bool) error {
args := []string{"ls", "--target", "multipath"}
outBytes, err := exec.Command(dmsetupcommand, args...).CombinedOutput()
out := string(outBytes)
if err != nil {
return fmt.Errorf("failed to retrieve multipath devices: %s", out)
}
r, err := regexp.Compile(majorMinorPattern)
if err != nil {
return fmt.Errorf("unable to compile regex with %s", majorMinorPattern)
}
listOut := r.FindAllString(out, -1)
for _, line := range listOut {
result := findStringSubmatchMap(line, r)
tmpPathname := "dm-" + result["Minor"]
uuid, err := getUUID(tmpPathname)
if err != nil {
klog.Warning(err)
continue
}
mapName, err := getMpathName(tmpPathname)
if err != nil {
return err
}
tmpWWID := strings.TrimPrefix(uuid, "mpath-")
tmpWWN := tmpWWID[1:] // truncate scsi-id prefix
// get the active count; if 0 then cleanup the dm
slavesCount, err := getPathsCount(mapName)
if err != nil {
return fmt.Errorf("unable to count slaves for device %s: %v", d.WWN, err)
}
if slavesCount == 0 {
klog.Warningf("cleaning mapper %s as no active disks present", mapName)
_ = multipathRemoveDmDevice(mapName)
// even if wwn matches but no slaves lets skip
} else if strings.EqualFold(d.WWN, tmpWWN) {
// if atleast 1 active slave present then use it
d.Mapper = "/dev/mapper/" + mapName
d.Slaves = slavesCount
break
}
}
return nil
}
// DeleteDevice delete the multipath device
func (d *Device) DeleteDevice() (err error) {
if err := retryCleanupDevice(d); err != nil {
klog.Warningf("error while deleting multipath device %s: %v", d.Mapper, err)
return err
}
d.Mapper = ""
d.Slaves = 0
return nil
}
// CreateDevice attach and create linux devices to host
func (d *Device) CreateDevice() (err error) {
if err = d.createLinuxDevice(); err != nil {
klog.Errorf("unable to create device for wwn %s", d.WWN)
return err
}
if d.Mapper == "" {
return fmt.Errorf("unable to find the device for wwn %s", d.WWN)
}
return nil
}
// scsiHostRescanWithLock: scans all scsi hosts with locks
// This is calling scsiHostRescan
// which works in a way that only 1 scan will run at a time
// and other requests will wait till the scan is complete
// but will not scan again as it is already scanned.
func scsiHostRescanWithLock() (err error) {
start := time.Now()
var scan bool = true
for {
if scanLock.TryLock() {
func() {
defer scanLock.Unlock()
if scan {
// always clean orphan paths before scanning hosts
cleanupOrphanPaths()
err = scsiHostRescan()
}
}()
return
} else {
if time.Since(start) > time.Minute {
// Scanning usually takes < 30s. If wait is more than a min then return.
return
}
// Already locked, wait for it to complete and don't scan again.
scan = false
time.Sleep(5 * time.Second)
}
}
}
// createLinuxDevice: attaches and creates a new linux device
// Try device discovery; retry every 5 sec if no device found or have 0 slaves
// In between checks we will try to cleanup:
// a. faulty and orphan paths (quick)
// b. stale paths (time consuming)
// Since there can be parallel requests for device discovery we are not
// allowing cleanup process to run for each everytime.
// For 'a' we will not cleanup if it was already run within last 10 secs.
// For 'b' we will not cleanup if it was already run within last 25 secs.
func (d *Device) createLinuxDevice() (err error) {
// Start a Countdown ticker
for i := 0; i <= 10; i++ {
if err = scsiHostRescanWithLock(); err != nil {
return err
}
// wait for device to appear after rescan
time.Sleep(time.Second * 1)
err := d.Populate(true)
if err != nil {
return err
}
if d.Slaves > 0 {
// populated device with atleast 1 slave; job done
return nil
}
// some resting time
time.Sleep(time.Second * 5)
}
// Reached here signifies the device was not found, throw an error
return fmt.Errorf("fc device not found for wwn %s", d.WWN)
}
// scsiHostRescan: scans all scsi hosts
func scsiHostRescan() error {
scsiPath := "/sys/class/scsi_host/"
dirs, err := os.ReadDir(scsiPath)
if err != nil {
return err
}
for _, f := range dirs {
name := filepath.Clean(scsiPath + f.Name() + "/scan")
data := []byte("- - -")
if err := os.WriteFile(name, data, 0666); err != nil {
return fmt.Errorf("scsi host rescan failed: %v", err)
}
}
return nil
}
func GetDeviceWWN(pathName string) (wwn string, err error) {
if strings.HasPrefix(pathName, "/dev/mapper/") {
// get dm path
pathName, err = filepath.EvalSymlinks(pathName)
if err != nil {
return "", err
}
}
pathName = strings.TrimPrefix(pathName, "/dev/")
uuid, err := getUUID(pathName)
tmpWWID := strings.TrimPrefix(uuid, "mpath-")
wwn = tmpWWID[1:] // truncate scsi-id prefix
return wwn, err
}