@@ -12,11 +12,14 @@ import (
12
12
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
13
13
)
14
14
15
- const cacheSuffix = "csi-fast"
16
- const mainLvSuffix = "csi-main"
17
- const raidedLocalSsdName = "csi-driver-data-cache"
18
- const raidMode = "0"
19
- const raidedLssdPrefix = "/dev/md/"
15
+ const (
16
+ cacheSuffix = "csi-fast"
17
+ mainLvSuffix = "csi-main"
18
+ raidedLocalSsdName = "csi-driver-data-cache"
19
+ raidMode = "0"
20
+ raidedLssdPrefix = "/dev/md/"
21
+ raidedLocalSsdPath = raidedLssdPrefix + raidedLocalSsdName
22
+ )
20
23
21
24
func setupCaching (devicePath string , req * csi.NodeStageVolumeRequest , nodeId string ) (string , error ) {
22
25
volumeId := req .GetVolumeId ()
@@ -32,7 +35,6 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
32
35
if err != nil {
33
36
klog .Errorf ("vgscan error %v: %s" , err , info )
34
37
}
35
- klog .V (2 ).Infof ("====== vgscan info %v ======" , string (info ))
36
38
klog .V (2 ).Infof ("====== vgscan info contains volumeGroupName or not %v ======" , strings .Contains (string (info ), volumeGroupName ))
37
39
// Check if the required volume group already exists
38
40
if strings .Contains (string (info ), volumeGroupName ) {
@@ -41,28 +43,12 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
41
43
// Clean up Volume Group before adding the PD
42
44
reduceVolumeGroup (volumeGroupName , true )
43
45
} else {
44
- err := createVg (volumeGroupName , devicePath )
46
+ err := createVg (volumeGroupName , devicePath , raidedLocalSsdPath )
45
47
if err != nil {
46
48
return mainDevicePath , err
47
49
}
48
- // VG doesn't exist so throw error
49
- klog .Errorf ("No VG on Node, ensure Volume group on node" )
50
50
}
51
51
52
- // Verify if the PD is already in the required volume group
53
- // args = []string{
54
- // "--select",
55
- // "vg_name",
56
- // volumeGroupName
57
- // }
58
- // vgPvs, err := common.RunCommand("pvs", args...)
59
- // if err != nil {
60
- // klog.Errorf("Errored while checking Physical Volume(PV)s for the volume group %v: %s", err, vgPvs)
61
- // }
62
- // if strings.Contains(string(vgPvs), devicePath) {
63
- // klog.V(2).Infof("====Physical Volume(PV) already exists in the Volume Group=====")
64
- // } else {
65
-
66
52
// Check if the Physical Volume(PV) is part of some other volume group
67
53
args = []string {
68
54
"--select" ,
@@ -82,7 +68,6 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
82
68
infoString = strings .ReplaceAll (infoString , "." , "" )
83
69
infoString = strings .ReplaceAll (infoString , "\" " , "" )
84
70
infoSlice := strings .Split (strings .TrimSpace (infoString ), " " )
85
- klog .V (2 ).Info ("============ infoSlice %s =============" , infoSlice [(len (infoSlice )- 1 )], infoSlice )
86
71
vgNameForPv := strings .TrimSpace (infoSlice [(len (infoSlice ) - 1 )])
87
72
klog .V (2 ).Infof ("============ Physical volume is part of Volume group: %v=======" , vgNameForPv )
88
73
if vgNameForPv == volumeGroupName {
@@ -94,10 +79,13 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
94
79
if err != nil {
95
80
klog .Errorf ("Errored while deactivating VG %v: err: %v: %s" , vgNameForPv , err , info )
96
81
}
82
+
83
+ reduceVolumeGroup (vgNameForPv , false )
97
84
// Uncache LV
98
85
args = []string {
99
86
"--uncache" ,
100
87
vgNameForPv + "/" + mainLvName ,
88
+ "--force" ,
101
89
}
102
90
info , err = common .RunCommand ("lvconvert" , args ... )
103
91
if err != nil {
@@ -112,12 +100,6 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
112
100
klog .Errorf ("Errored while merging Volume group %s into %s %v: %s" , vgNameForPv , volumeGroupName , err , info )
113
101
}
114
102
115
- klog .V (2 ).Infof ("==========Remove VG from node %v ==========" , vgNameForPv )
116
- info , err = common .RunCommand ("vgremove" , []string {vgNameForPv , "-y" }... )
117
- if err != nil {
118
- klog .Errorf ("Errored while removing Volume group %s: info:%s, error:%v" , vgNameForPv , err , info )
119
- }
120
-
121
103
} else {
122
104
klog .V (2 ).Infof ("==========Extend Node VG %v for PV %v==========" , volumeGroupName , devicePath )
123
105
info , err := common .RunCommand ("vgextend" , []string {volumeGroupName , devicePath }... )
@@ -135,7 +117,6 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
135
117
}
136
118
lvList , err := common .RunCommand ("lvs" , args ... )
137
119
if err != nil {
138
- klog .V (2 ).Infof ("====== lvs error %v: %s ======" , err , info )
139
120
return mainDevicePath , fmt .Errorf ("lv list error %w: %s" , err , info )
140
121
}
141
122
klog .Info ("=============== Got LVs %s on Volume group %s ============" , string (lvList ), volumeGroupName )
@@ -153,13 +134,11 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
153
134
}
154
135
info , err = common .RunCommand ("lvcreate" , args ... )
155
136
if err != nil {
156
- klog .V (2 ).Infof ("====== lvcreate error %v: %s ======" , err , info )
157
137
return mainDevicePath , fmt .Errorf ("lvcreate error %w: %s" , err , info )
158
138
}
159
139
160
140
}
161
- // Replace this with RAIDed Local SSD
162
- cachePoolName := "dev/nvme0n1"
141
+ cachePoolName := raidedLocalSsdPath
163
142
// Verify caching is setup for PD
164
143
args = []string {
165
144
"--select" ,
@@ -169,17 +148,16 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
169
148
}
170
149
poolName , err := common .RunCommand ("lvs" , args ... )
171
150
if err != nil {
172
- klog .V (2 ).Infof ("====== lvcreate error %v: %s ======" , err , info )
173
151
return mainDevicePath , fmt .Errorf ("lvcreate error %w: %s" , err , info )
174
152
}
175
153
cacheLvName := getLvName (cacheSuffix , volumeId )
176
154
if strings .Contains (string (poolName ), "csi-fast" ) {
177
155
// Validate that cache is setup for required size
178
156
klog .V (2 ).Infof ("================Validate Cache is setup for correct size and mode===============" )
179
157
} else {
180
- fastCacheSize := req .GetPublishContext ()[contexLocalSsdCacheSize ] + "Gi"
158
+ fastCacheSize := req .GetPublishContext ()[common .ContexLocalSsdCacheSize ]
159
+ chunkSize := "960" // Cannot use default chunk size(64KiB) as it errors on maxChunksAllowed. Unit - KiB
181
160
klog .V (2 ).Infof ("====== fastCacheSize is %v ======" , fastCacheSize )
182
- // lvcreate -n fast -L 50G cachegroup /dev/nvme0n1
183
161
klog .V (2 ).Infof ("====== lvcreate fast cache layer again with the VolumeGroup %v======" , volumeGroupName )
184
162
args = []string {
185
163
"--yes" ,
@@ -197,7 +175,6 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
197
175
}
198
176
199
177
// Once caching is setup, link the PD to cache
200
- // lvconvert --type cache --cachevol fast --zero y --cachemode writethrough cachegroup/main --force -y
201
178
klog .V (2 ).Infof ("====== lvconvert fast and main to cache ======" )
202
179
args = []string {
203
180
"--type" ,
@@ -207,8 +184,10 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
207
184
"--zero" ,
208
185
"y" ,
209
186
"--cachemode" ,
210
- req .GetPublishContext ()[contextDataCacheMode ],
187
+ req .GetPublishContext ()[common . ContextDataCacheMode ],
211
188
volumeGroupName + "/" + mainLvName ,
189
+ "--chunksize" ,
190
+ string (chunkSize ),
212
191
"--force" ,
213
192
"-y" ,
214
193
}
@@ -247,7 +226,6 @@ func cleanupCache(volumeId string, nodeId string) error {
247
226
}
248
227
info , err = common .RunCommand ("lvconvert" , args ... )
249
228
if err != nil {
250
- klog .Errorf ("Errored while uncaching the disk %v: %s" , err , info )
251
229
return fmt .Errorf ("errored while uncaching the disk %w: %s" , err , info )
252
230
}
253
231
return nil
@@ -265,14 +243,14 @@ func getLvName(suffix string, volumeId string) string {
265
243
return fmt .Sprintf ("%s-%s" , suffix , pvcName )
266
244
}
267
245
268
- func createVg (volumeGroupName string , devicePath string ) error {
246
+ func createVg (volumeGroupName string , devicePath string , raidedLocalSsds string ) error {
269
247
// No existing volume group
270
248
klog .V (2 ).Infof ("====== vgcreate ======" )
271
249
args := []string {
272
250
"--zero" ,
273
251
"y" ,
274
252
volumeGroupName ,
275
- "/dev/nvme0n1" ,
253
+ raidedLocalSsds ,
276
254
}
277
255
info , err := common .RunCommand ("vgcreate" , args ... )
278
256
if err != nil {
@@ -307,48 +285,56 @@ func reduceVolumeGroup(volumeGroupName string, force bool) {
307
285
}
308
286
309
287
func RaidLocalSsds () error {
310
- isRaided , err := isRaided ()
288
+ isAlreadyRaided , err := isRaided ()
311
289
if err != nil {
312
290
klog .V (2 ).Info ("======Errored while scanning for available LocalSSDs err:%v; continuing Raiding=======" , err )
313
- } else if isRaided {
291
+ } else if isAlreadyRaided {
314
292
klog .V (2 ).Infof ("===============Local SSDs are already RAIDed==============" )
315
293
return nil
316
294
}
317
- info , err := common .RunCommand ("nvme" , []string {"list" }... )
295
+ info , err := common .RunCommand ("nvme" , []string {"list" , "-o" , "json" }... )
318
296
if err != nil {
319
- klog .Errorf ("nvme list error %v: %s" , err , info )
320
297
return fmt .Errorf ("errored while scanning available NVME disks info: %v; err:%v" , info , err )
321
298
}
322
- klog .V (2 ).Infof ("==========NVME list %v========" , string (info ))
323
299
infoString := strings .TrimSpace (strings .ReplaceAll (string (info ), "\n " , " " ))
300
+ klog .V (2 ).Infof ("=============NVME list %v ============" , infoString )
324
301
infoString = strings .ReplaceAll (infoString , "\" " , "" )
325
- infoSlice := strings .Split (strings .TrimSpace (infoString ), " " )
302
+ infoString = strings .ReplaceAll (infoString , " :" , ":" )
303
+ infoString = strings .ReplaceAll (infoString , ": " , ":" )
304
+ infoString = strings .ReplaceAll (infoString , "," , " " )
305
+ infoSlice := strings .Split (infoString , " " )
326
306
diskList := []string {}
327
307
for _ , diskInfo := range infoSlice {
328
308
diskName := strings .TrimSpace (diskInfo )
329
- if strings .HasPrefix (diskName , "/dev/n" ) {
309
+ if strings .Contains (diskName , "DevicePath" ) {
310
+ diskName := strings .TrimSpace (strings .Split (diskName , ":" )[1 ])
330
311
diskList = append (diskList , diskName )
331
312
}
332
313
}
333
314
nvmeDiskCount := len (diskList )
334
315
nvmeDiskList := strings .Join (diskList , " " )
335
- klog .V (2 ).Infof ("========= nvmeDiskCount %v; nvmeDislList : %v ================" , nvmeDiskCount , nvmeDiskList )
316
+ klog .V (2 ).Infof ("========= nvmeDiskCount %v; nvmeDiskList : %v; diskList %v ================" , nvmeDiskCount , nvmeDiskList , diskList )
336
317
args := []string {
337
318
"--create" ,
338
319
raidedLssdPrefix + raidedLocalSsdName ,
339
- "-l= " + raidMode ,
320
+ "-l" + raidMode ,
340
321
// Force RAIDing as sometime it might fail for caution if there is just 1 LSSD present as 1 LSSD need not be RAIDed
341
322
"--force" ,
342
323
"-n" ,
343
324
strconv .Itoa (nvmeDiskCount ),
344
- nvmeDiskList ,
345
325
}
326
+ args = append (args , diskList ... )
346
327
info , err = common .RunCommand ("mdadm" , args ... )
347
328
if err != nil {
348
- klog .Errorf ("Errored while RAIDing LSSDs %v: %s" , err , info )
349
329
return fmt .Errorf ("errored while RAIDing LSSDs info: %v; err:%v" , info , err )
350
- } else {
351
- klog .V (2 ).Infof ("========RAIDed Local SSDs in mode 0===========" )
330
+ }
331
+ // Validate if Raided successfully
332
+ isAlreadyRaided , err = isRaided ()
333
+ if err != nil {
334
+ klog .V (2 ).Info ("======Errored while scanning for available raided LocalSSDs err:%v=======" , err )
335
+ }
336
+ if ! isAlreadyRaided {
337
+ return fmt .Errorf ("failed raiding, raided device not found on scanning" )
352
338
}
353
339
return nil
354
340
}
@@ -363,7 +349,7 @@ func isRaided() (bool, error) {
363
349
return false , fmt .Errorf ("errored while scanning for raided LSSD %v: %s" , err , info )
364
350
}
365
351
klog .V (2 ).Infof ("=========== Got LSSDs %v===========" , string (info ))
366
- if strings .Contains (string (info ), raidedLssdPrefix + raidedLocalSsdName ) {
352
+ if info != nil && strings .Contains (string (info ), raidedLocalSsdName ) {
367
353
return true , nil
368
354
}
369
355
return false , nil
0 commit comments