@@ -2,6 +2,7 @@ package gceGCEDriver
2
2
3
3
import (
4
4
"fmt"
5
+ "strconv"
5
6
"strings"
6
7
7
8
csi "github.com/container-storage-interface/spec/lib/go/csi"
@@ -13,11 +14,15 @@ import (
13
14
14
15
const cacheSuffix = "csi-fast"
15
16
const mainLvSuffix = "csi-main"
17
+ const raidedLocalSsdName = "csi-driver-data-cache"
18
+ const raidMode = "0"
19
+ const raidedLssdPrefix = "/dev/md/"
16
20
17
- func SetupCaching (devicePath string , req * csi.NodeStageVolumeRequest , nodeId string ) (string , error ) {
21
+ func setupCaching (devicePath string , req * csi.NodeStageVolumeRequest , nodeId string ) (string , error ) {
18
22
volumeId := req .GetVolumeId ()
19
23
volumeGroupName := getVolumeGroupName (nodeId )
20
24
mainDevicePath := "/dev/" + volumeGroupName + "/" + getLvName (mainLvSuffix , volumeId )
25
+ mainLvName := getLvName (mainLvSuffix , volumeId )
21
26
klog .V (2 ).Infof ("====== Start LVM PoC NodeStageVolume Steps ======" )
22
27
klog .V (2 ).Infof ("====== volumeGroupName is %v ======" , volumeGroupName )
23
28
@@ -34,7 +39,7 @@ func SetupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
34
39
klog .V (2 ).Infof ("============= VG exists, now check if PD is part of VG============" )
35
40
36
41
// Clean up Volume Group before adding the PD
37
- reduceVolumeGroup (volumeGroupName )
42
+ reduceVolumeGroup (volumeGroupName , true )
38
43
} else {
39
44
err := createVg (volumeGroupName , devicePath )
40
45
if err != nil {
@@ -89,16 +94,26 @@ func SetupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
89
94
if err != nil {
90
95
klog .Errorf ("Errored while deactivating VG %v: err: %v: %s" , vgNameForPv , err , info )
91
96
}
97
+ // Uncache LV
98
+ args = []string {
99
+ "--uncache" ,
100
+ vgNameForPv + "/" + mainLvName ,
101
+ }
102
+ info , err = common .RunCommand ("lvconvert" , args ... )
103
+ if err != nil {
104
+ klog .Errorf ("errored while uncaching main LV %v: %s" , err , info )
105
+ // On error info contains the error message which we cannot use for further steps
106
+ }
92
107
93
- reduceVolumeGroup (vgNameForPv )
108
+ reduceVolumeGroup (vgNameForPv , false )
94
109
klog .V (2 ).Infof ("==========Merge VG %v to Node VG %v==========" , vgNameForPv , volumeGroupName )
95
110
info , err = common .RunCommand ("vgmerge" , []string {volumeGroupName , vgNameForPv }... )
96
111
if err != nil {
97
112
klog .Errorf ("Errored while merging Volume group %s into %s %v: %s" , vgNameForPv , volumeGroupName , err , info )
98
113
}
99
114
100
115
klog .V (2 ).Infof ("==========Remove VG from node %v ==========" , vgNameForPv )
101
- info , err = common .RunCommand ("cgremove " , []string {vgNameForPv , "-y" }... )
116
+ info , err = common .RunCommand ("vgremove " , []string {vgNameForPv , "-y" }... )
102
117
if err != nil {
103
118
klog .Errorf ("Errored while removing Volume group %s: info:%s, error:%v" , vgNameForPv , err , info )
104
119
}
@@ -111,7 +126,6 @@ func SetupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
111
126
}
112
127
}
113
128
114
- mainLvName := getLvName (mainLvSuffix , volumeId )
115
129
// Create LV if not already created
116
130
args = []string {
117
131
"--select" ,
@@ -124,7 +138,7 @@ func SetupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
124
138
klog .V (2 ).Infof ("====== lvs error %v: %s ======" , err , info )
125
139
return mainDevicePath , fmt .Errorf ("lv list error %w: %s" , err , info )
126
140
}
127
- klog .Info ("=============== Got LVs %s on Volume group %s ============" , lvList , volumeGroupName )
141
+ klog .Info ("=============== Got LVs %s on Volume group %s ============" , string ( lvList ) , volumeGroupName )
128
142
if ! strings .Contains (string (lvList ), mainLvName ) {
129
143
// lvcreate -n main -l 100%PVS cachegroup /dev/sdb
130
144
klog .V (2 ).Infof ("====== lvcreate main cache layer ======" )
@@ -215,7 +229,7 @@ func SetupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
215
229
return mainDevicePath , nil
216
230
}
217
231
218
- func CleanupCache (volumeId string , nodeId string ) error {
232
+ func cleanupCache (volumeId string , nodeId string ) error {
219
233
220
234
volumeGroupName := getVolumeGroupName (nodeId )
221
235
klog .V (2 ).Infof ("=============Deactivating volume %s/%s=====" , volumeGroupName , volumeId )
@@ -236,7 +250,6 @@ func CleanupCache(volumeId string, nodeId string) error {
236
250
klog .Errorf ("Errored while uncaching the disk %v: %s" , err , info )
237
251
return fmt .Errorf ("errored while uncaching the disk %w: %s" , err , info )
238
252
}
239
- reduceVolumeGroup (volumeGroupName )
240
253
return nil
241
254
}
242
255
@@ -278,15 +291,80 @@ func createVg(volumeGroupName string, devicePath string) error {
278
291
return nil
279
292
}
280
293
281
- func reduceVolumeGroup (volumeGroupName string ) {
294
+ func reduceVolumeGroup (volumeGroupName string , force bool ) {
282
295
klog .V (2 ).Infof ("=========Cleanup VG========" )
283
296
args := []string {
284
297
"--removemissing" ,
285
- "--force" ,
286
298
volumeGroupName ,
287
299
}
300
+ if force {
301
+ args = append (args , "--force" )
302
+ }
288
303
info , err := common .RunCommand ("vgreduce" , args ... )
289
304
if err != nil {
290
305
klog .Errorf ("Errored while cleaning up volume group %v: %s" , err , info )
291
306
}
292
307
}
308
+
309
+ func RaidLocalSsds () error {
310
+ isRaided , err := isRaided ()
311
+ if err != nil {
312
+ klog .V (2 ).Info ("======Errored while scanning for available LocalSSDs err:%v; continuing Raiding=======" , err )
313
+ } else if isRaided {
314
+ klog .V (2 ).Infof ("===============Local SSDs are already RAIDed==============" )
315
+ return nil
316
+ }
317
+ info , err := common .RunCommand ("nvme" , []string {"list" }... )
318
+ if err != nil {
319
+ klog .Errorf ("nvme list error %v: %s" , err , info )
320
+ return fmt .Errorf ("errored while scanning available NVME disks info: %v; err:%v" , info , err )
321
+ }
322
+ klog .V (2 ).Infof ("==========NVME list %v========" , string (info ))
323
+ infoString := strings .TrimSpace (strings .ReplaceAll (string (info ), "\n " , " " ))
324
+ infoString = strings .ReplaceAll (infoString , "\" " , "" )
325
+ infoSlice := strings .Split (strings .TrimSpace (infoString ), " " )
326
+ diskList := []string {}
327
+ for _ , diskInfo := range infoSlice {
328
+ diskName := strings .TrimSpace (diskInfo )
329
+ if strings .HasPrefix (diskName , "/dev/n" ) {
330
+ diskList = append (diskList , diskName )
331
+ }
332
+ }
333
+ nvmeDiskCount := len (diskList )
334
+ nvmeDiskList := strings .Join (diskList , " " )
335
+ klog .V (2 ).Infof ("========= nvmeDiskCount %v; nvmeDislList: %v ================" , nvmeDiskCount , nvmeDiskList )
336
+ args := []string {
337
+ "--create" ,
338
+ raidedLssdPrefix + raidedLocalSsdName ,
339
+ "-l=" + raidMode ,
340
+ // Force RAIDing as sometime it might fail for caution if there is just 1 LSSD present as 1 LSSD need not be RAIDed
341
+ "--force" ,
342
+ "-n" ,
343
+ strconv .Itoa (nvmeDiskCount ),
344
+ nvmeDiskList ,
345
+ }
346
+ info , err = common .RunCommand ("mdadm" , args ... )
347
+ if err != nil {
348
+ klog .Errorf ("Errored while RAIDing LSSDs %v: %s" , err , info )
349
+ 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===========" )
352
+ }
353
+ return nil
354
+ }
355
+
356
+ func isRaided () (bool , error ) {
357
+ args := []string {
358
+ "--detail" ,
359
+ "--scan" ,
360
+ }
361
+ info , err := common .RunCommand ("mdadm" , args ... )
362
+ if err != nil {
363
+ return false , fmt .Errorf ("errored while scanning for raided LSSD %v: %s" , err , info )
364
+ }
365
+ klog .V (2 ).Infof ("=========== Got LSSDs %v===========" , string (info ))
366
+ if strings .Contains (string (info ), raidedLssdPrefix + raidedLocalSsdName ) {
367
+ return true , nil
368
+ }
369
+ return false , nil
370
+ }
0 commit comments