
Errors from staticcheck: pkg/volume/azure_dd/azure_common.go:68:2: var winDiskNumFormat is unused (U1000) pkg/volume/csi/csi_block.go:97:2: field volumeInfo is unused (U1000) pkg/volume/csi/csi_block_test.go:56:6: func prepareBlockUnmapperTest is unused (U1000) pkg/volume/csi/csi_client.go:108:2: const initialDuration is unused (U1000) pkg/volume/csi/csi_client.go:109:2: const factor is unused (U1000) pkg/volume/csi/csi_client.go:110:2: const steps is unused (U1000) pkg/volume/csi/csi_client_test.go:83:8: this value of err is never used (SA4006) pkg/volume/csi/csi_mounter.go:76:2: field options is unused (U1000) pkg/volume/csi/csi_mounter_test.go:454:13: this value of err is never used (SA4006) pkg/volume/csi/csi_plugin_test.go:766:16: this value of err is never used (SA4006) pkg/volume/csi/csi_plugin_test.go:861:13: this value of err is never used (SA4006) pkg/volume/csi/csi_plugin_test.go:1186:13: this value of err is never used (SA4006) pkg/volume/csi/csi_plugin_test.go:1249:13: this value of err is never used (SA4006) pkg/volume/csi/csi_test.go:305:5: the goroutine calls T.Fatalf, which must be called in the same goroutine as the test (SA2002) pkg/volume/flexvolume/probe_test.go:67:10: this value of err is never used (SA4006) pkg/volume/iscsi/iscsi_test.go:95:2: field attachCalled is unused (U1000) pkg/volume/iscsi/iscsi_test.go:96:2: field detachCalled is unused (U1000) pkg/volume/iscsi/iscsi_test.go:501:24: this value of err is never used (SA4006) pkg/volume/iscsi/iscsi_util_test.go:159:2: this value of exist is never used (SA4006) pkg/volume/local/local.go:351:57: argument devicePath is overwritten before first use (SA4009) pkg/volume/plugins_test.go:119:2: this value of plug is never used (SA4006) pkg/volume/plugins_test.go:125:2: this value of plug is never used (SA4006) pkg/volume/quobyte/quobyte.go:474:23: this result of append is never used, except maybe in other appends (SA4010) pkg/volume/quobyte/quobyte.go:477:23: this result of append is never used, except maybe in other appends (SA4010) pkg/volume/quobyte/quobyte.go:480:23: this result of append is never used, except maybe in other appends (SA4010) pkg/volume/rbd/rbd.go:886:2: field adminSecret is unused (U1000) pkg/volume/rbd/rbd.go:887:2: field adminID is unused (U1000) pkg/volume/rbd/rbd.go:888:2: field imageFormat is unused (U1000) pkg/volume/rbd/rbd.go:889:2: field imageFeatures is unused (U1000) pkg/volume/storageos/storageos.go:302:2: field secretName is unused (U1000) pkg/volume/storageos/storageos_util_test.go:43:2: field apiAddr is unused (U1000) pkg/volume/storageos/storageos_util_test.go:44:2: field apiUser is unused (U1000) pkg/volume/storageos/storageos_util_test.go:45:2: field apiPass is unused (U1000) pkg/volume/storageos/storageos_util_test.go:46:2: field apiVersion is unused (U1000) pkg/volume/util/atomic_writer_test.go:756:49: argument err is overwritten before first use (SA4009) pkg/volume/util/fsquota/common/quota_linux_common.go:37:2: const acct is unused (U1000) pkg/volume/util/fsquota/common/quota_linux_common.go:38:2: const enforcing is unused (U1000) pkg/volume/util/fsquota/project.go:168:31: identical expressions on the left and right side of the '==' operator (SA4000) pkg/volume/util/fsquota/quota_linux.go:306:50: argument poduid is overwritten before first use (SA4009) pkg/volume/util/fsquota/quota_linux_test.go:558:16: this value of err is never used (SA4006) pkg/volume/util/subpath/subpath_linux.go:232:81: argument err is overwritten before first use (SA4009) pkg/volume/util/subpath/subpath_linux_test.go:579:73: argument err is overwritten before first use (SA4009)
122 lines
3.9 KiB
Go
122 lines
3.9 KiB
Go
// +build !providerless
|
|
// +build windows
|
|
|
|
/*
|
|
Copyright 2017 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 azure_dd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"k8s.io/klog"
|
|
utilexec "k8s.io/utils/exec"
|
|
"k8s.io/utils/mount"
|
|
)
|
|
|
|
var winDiskNumFormat = "/dev/disk%d"
|
|
|
|
func scsiHostRescan(io ioHandler, exec utilexec.Interface) {
|
|
cmd := "Update-HostStorageCache"
|
|
output, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
|
|
if err != nil {
|
|
klog.Errorf("Update-HostStorageCache failed in scsiHostRescan, error: %v, output: %q", err, string(output))
|
|
}
|
|
}
|
|
|
|
// search Windows disk number by LUN
|
|
func findDiskByLun(lun int, iohandler ioHandler, exec utilexec.Interface) (string, error) {
|
|
cmd := `Get-Disk | select number, location | ConvertTo-Json`
|
|
output, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
|
|
if err != nil {
|
|
klog.Errorf("Get-Disk failed in findDiskByLun, error: %v, output: %q", err, string(output))
|
|
return "", err
|
|
}
|
|
|
|
if len(output) < 10 {
|
|
return "", fmt.Errorf("Get-Disk output is too short, output: %q", string(output))
|
|
}
|
|
|
|
var data []map[string]interface{}
|
|
if err = json.Unmarshal(output, &data); err != nil {
|
|
klog.Errorf("Get-Disk output is not a json array, output: %q", string(output))
|
|
return "", err
|
|
}
|
|
|
|
for _, v := range data {
|
|
if jsonLocation, ok := v["location"]; ok {
|
|
if location, ok := jsonLocation.(string); ok {
|
|
if !strings.Contains(location, " LUN ") {
|
|
continue
|
|
}
|
|
|
|
arr := strings.Split(location, " ")
|
|
arrLen := len(arr)
|
|
if arrLen < 3 {
|
|
klog.Warningf("unexpected json structure from Get-Disk, location: %q", jsonLocation)
|
|
continue
|
|
}
|
|
|
|
klog.V(4).Infof("found a disk, location: %q, lun: %q", location, arr[arrLen-1])
|
|
//last element of location field is LUN number, e.g.
|
|
// "location": "Integrated : Adapter 3 : Port 0 : Target 0 : LUN 1"
|
|
l, err := strconv.Atoi(arr[arrLen-1])
|
|
if err != nil {
|
|
klog.Warningf("cannot parse element from data structure, location: %q, element: %q", location, arr[arrLen-1])
|
|
continue
|
|
}
|
|
|
|
if l == lun {
|
|
klog.V(4).Infof("found a disk and lun, location: %q, lun: %d", location, lun)
|
|
if d, ok := v["number"]; ok {
|
|
if diskNum, ok := d.(float64); ok {
|
|
klog.V(2).Infof("azureDisk Mount: got disk number(%d) by LUN(%d)", int(diskNum), lun)
|
|
return fmt.Sprintf(winDiskNumFormat, int(diskNum)), nil
|
|
}
|
|
klog.Warningf("LUN(%d) found, but could not get disk number(%q), location: %q", lun, d, location)
|
|
}
|
|
return "", fmt.Errorf("LUN(%d) found, but could not get disk number, location: %q", lun, location)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return "", nil
|
|
}
|
|
|
|
func formatIfNotFormatted(disk string, fstype string, exec utilexec.Interface) {
|
|
if err := mount.ValidateDiskNumber(disk); err != nil {
|
|
klog.Errorf("azureDisk Mount: formatIfNotFormatted failed, err: %v\n", err)
|
|
return
|
|
}
|
|
|
|
if len(fstype) == 0 {
|
|
// Use 'NTFS' as the default
|
|
fstype = "NTFS"
|
|
}
|
|
cmd := fmt.Sprintf("Get-Disk -Number %s | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru", disk)
|
|
cmd += fmt.Sprintf(" | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false", fstype)
|
|
output, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
|
|
if err != nil {
|
|
klog.Errorf("azureDisk Mount: Get-Disk failed, error: %v, output: %q", err, string(output))
|
|
} else {
|
|
klog.Infof("azureDisk Mount: Disk successfully formatted, disk: %q, fstype: %q\n", disk, fstype)
|
|
}
|
|
}
|