Skip to content

Commit 4338519

Browse files
committed
update metadata for consistency
1 parent c5c4c87 commit 4338519

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

Diff for: pkg/gce-pd-csi-driver/cache.go

+51-33
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func fetchRAIDedLocalSsdPath() (string, error) {
4444
}
4545
info, err := common.RunCommand("grep", []string{raidedLocalSsdName}, "mdadm", args...)
4646
if err != nil || len(info) == 0 {
47-
return "", fmt.Errorf("Error getting RAIDed device path for Data Cache %v, output:%v", err, string(info))
47+
return "", fmt.Errorf("Error getting RAIDed device path for Data Cache %v, output:%s", err, string(info))
4848
}
4949
infoString := strings.TrimSpace(string(info))
5050
infoSlice := strings.Fields(infoString)
@@ -55,6 +55,9 @@ func fetchRAIDedLocalSsdPath() (string, error) {
5555
}
5656

5757
func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId string) (string, error) {
58+
var args []string
59+
var err error
60+
var info []byte
5861

5962
// The device path may have changed after rebooting, so we need to fetch the path again
6063
raidedLocalSsdPath, err := fetchRAIDedLocalSsdPath()
@@ -77,32 +80,13 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
7780
return mainDevicePath, err
7881
}
7982
}
80-
81-
// Check if the Physical Volume(PV) is part of some other volume group
82-
args := []string{
83-
"--select",
84-
"pv_name=" + devicePath,
85-
"-o",
86-
"vg_name",
87-
}
88-
info, err := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "pvs", args...)
89-
if err != nil {
90-
klog.Errorf("errored while checking physical volume details %v: %s", err, info)
91-
// On error info contains the error message which we cannot use for further steps
92-
info = nil
93-
}
94-
95-
infoString := strings.TrimSpace(strings.ReplaceAll(string(info), "\n", " "))
96-
infoString = strings.ReplaceAll(infoString, ".", "")
97-
infoString = strings.ReplaceAll(infoString, "\"", "")
98-
infoSlice := strings.Split(strings.TrimSpace(infoString), " ")
99-
vgNameForPv := strings.TrimSpace(infoSlice[(len(infoSlice) - 1)])
83+
vgNameForPv := fetchVgNameForPv(devicePath)
10084
klog.V(4).Infof("Physical volume is part of Volume group: %v", vgNameForPv)
10185
if vgNameForPv == volumeGroupName {
10286
klog.V(4).Infof("Physical Volume(PV) already exists in the Volume Group %v", volumeGroupName)
10387
} else if vgNameForPv != "VG" && vgNameForPv != "" {
10488

105-
info, err = common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "vgchange", []string{"-an", vgNameForPv}...)
89+
info, err := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "vgchange", []string{"-an", vgNameForPv}...)
10690
if err != nil {
10791
klog.Errorf("Errored while deactivating VG %v: err: %v: %s", vgNameForPv, err, info)
10892
}
@@ -113,7 +97,7 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
11397

11498
if isCached {
11599
// Uncache LV
116-
args = []string{
100+
args := []string{
117101
"--uncache",
118102
vgNameForPv + "/" + mainLvName,
119103
"--force",
@@ -181,10 +165,10 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
181165
return mainDevicePath, err
182166
}
183167
// Check if LV exists
184-
info, err = common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "lvs", args...)
168+
info, err := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "lvs", args...)
185169
lvExists := strings.Contains(string(info), cacheLvName)
186170
if !lvExists {
187-
args = []string{
171+
args := []string{
188172
"--yes",
189173
"-n",
190174
cacheLvName,
@@ -194,7 +178,7 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
194178
volumeGroupName,
195179
raidedLocalSsdPath,
196180
}
197-
info, err = common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "lvcreate", args...)
181+
info, err := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "lvcreate", args...)
198182
if err != nil {
199183
if strings.Contains(err.Error(), "insufficient free space") {
200184
return mainDevicePath, status.Error(codes.InvalidArgument, fmt.Sprintf("Error setting up cache: %v", err.Error()))
@@ -204,7 +188,7 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
204188
}
205189

206190
// Once caching is setup, link the PD to cache
207-
args = []string{
191+
args := []string{
208192
"--type",
209193
"cache",
210194
"--cachevol",
@@ -329,7 +313,7 @@ func FetchRaidedLssds() ([]string, error) {
329313

330314
info, err := common.RunCommand("grep", []string{"/dev"}, "mdadm", args...)
331315
if err != nil {
332-
return nil, fmt.Errorf("error fetching RAIDed LSSDs: %v; err:%v", info, err)
316+
return nil, fmt.Errorf("error fetching RAIDed LSSDs: %s; err:%v", info, err)
333317
}
334318

335319
if len(info) != 0 {
@@ -352,7 +336,7 @@ func FetchAllLssds() ([]string, error) {
352336

353337
info, err := common.RunCommand("" /* pipedCmd */, nil /* pipeCmdArg */, "lsblk", []string{"-o", "NAME,MODEL", "-p", "-d", "-n"}...)
354338
if err != nil {
355-
return nil, fmt.Errorf("errored while fetching NVME disks info: %v; err:%v", info, err)
339+
return nil, fmt.Errorf("errored while fetching NVME disks info: %s; err:%v", info, err)
356340
}
357341
infoList := strings.Split(strings.TrimSpace(string(info)), "\n")
358342
re, err := regexp.Compile("nvme_card([0-9]+)?$")
@@ -378,7 +362,7 @@ func FetchAllLssds() ([]string, error) {
378362
func FetchLSSDsWihtEmptyMountPoint() ([]string, error) {
379363
info, err := common.RunCommand("grep", []string{"-E", `^\S+\s*$`} /* pipeCmdArg */, "lsblk", []string{"-o", "NAME,MOUNTPOINT", "-pdn"}...)
380364
if err != nil {
381-
return nil, fmt.Errorf("Error while fetching disks with no mount point: %v; err:%v", info, err)
365+
return nil, fmt.Errorf("Error while fetching disks with no mount point: %s; err:%v", info, err)
382366
}
383367
infoList := strings.Split(string(info), "\n")
384368
diskList := []string{}
@@ -396,7 +380,7 @@ func checkVgExists(volumeGroupName string) bool {
396380
return false
397381
}
398382
// Check if the required volume group already exists
399-
klog.Infof("check vg exists output: %v, volumeGroupName: %v", string(info), volumeGroupName)
383+
klog.Infof("check vg exists output: %s, volumeGroupName: %v", info, volumeGroupName)
400384
return strings.Contains(string(info), volumeGroupName)
401385
}
402386

@@ -487,13 +471,22 @@ func reduceVolumeGroup(volumeGroupName string, force bool) {
487471
return
488472
}
489473
args := []string{
474+
"--updatemetadata",
475+
volumeGroupName,
476+
}
477+
info, err := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "vgck", args...)
478+
if err != nil {
479+
klog.Errorf("Errored while update volume group metadata %v: %s", err, info)
480+
}
481+
482+
args = []string{
490483
"--removemissing",
491484
volumeGroupName,
492485
}
493486
if force {
494487
args = append(args, "--force")
495488
}
496-
info, err := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "vgreduce", args...)
489+
info, err = common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "vgreduce", args...)
497490
if err != nil {
498491
klog.Errorf("Errored while cleaning up volume group %v: %s", err, info)
499492
}
@@ -512,7 +505,7 @@ func RaidLocalSsds(availableLssds []string) error {
512505
args = append(args, availableLssds...)
513506
info, err := common.RunCommand("" /* pipedCmd */, nil /* pipeCmdArg */, "mdadm", args...)
514507
if err != nil {
515-
return fmt.Errorf("errored while RAIDing LSSDs info: %v; err:%v", info, err)
508+
return fmt.Errorf("errored while RAIDing LSSDs info: %s; err:%v", info, err)
516509
}
517510
// Validate if Raided successfully
518511
isAlreadyRaided, err := IsRaided()
@@ -674,3 +667,28 @@ func addRaidedLSSDToVg(vgName, lssdPath string) error {
674667
}
675668
return nil
676669
}
670+
671+
func fetchVgNameForPv(pvName string) string {
672+
// Check if the Physical Volume(PV) is part of some other volume group
673+
args := []string{
674+
"--select",
675+
"pv_name=" + pvName,
676+
"-o",
677+
"vg_name",
678+
"--noheadings",
679+
"2>/dev/null", //NOT RECOMMENDED
680+
}
681+
info, err := common.RunCommand("grep", []string{pvName}, "pvs", args...)
682+
if err != nil {
683+
klog.Errorf("errored while checking physical volume details %v: %s", err, info)
684+
// On error info contains the error message which we cannot use for further steps
685+
info = nil
686+
}
687+
infoString := strings.TrimSpace(strings.ReplaceAll(string(info), "\n", " "))
688+
infoSlice := strings.Fields(infoString)
689+
if len(infoSlice) == 0 {
690+
return ""
691+
}
692+
klog.V(4).Infof("Physical volume is part of Volume group: %v", infoSlice[0])
693+
return infoSlice[0]
694+
}

0 commit comments

Comments
 (0)