vsphere_volume_util_windows.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // +build !providerless
  2. // +build windows
  3. /*
  4. Copyright 2019 The Kubernetes Authors.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package vsphere_volume
  16. import (
  17. "encoding/json"
  18. "fmt"
  19. "os/exec"
  20. "strings"
  21. "k8s.io/klog"
  22. )
  23. type diskInfoResult struct {
  24. Number json.Number
  25. SerialNumber string
  26. }
  27. func verifyDevicePath(path string) (string, error) {
  28. if !strings.Contains(path, diskByIDPath) {
  29. // If this volume has already been mounted then
  30. // its devicePath will have already been converted to a disk number
  31. klog.V(4).Infof("Found vSphere disk attached with disk number %v", path)
  32. return path, nil
  33. }
  34. cmd := exec.Command("powershell", "/c", "Get-Disk | Select Number, SerialNumber | ConvertTo-JSON")
  35. output, err := cmd.Output()
  36. if err != nil {
  37. klog.Errorf("Get-Disk failed, error: %v, output: %q", err, string(output))
  38. return "", err
  39. }
  40. var results []diskInfoResult
  41. if err = json.Unmarshal(output, &results); err != nil {
  42. klog.Errorf("Failed to unmarshal Get-Disk json, output: %q", string(output))
  43. return "", err
  44. }
  45. serialNumber := strings.TrimPrefix(path, diskByIDPath+diskSCSIPrefix)
  46. for _, v := range results {
  47. if v.SerialNumber == serialNumber {
  48. klog.V(4).Infof("Found vSphere disk attached with serial %v", serialNumber)
  49. return v.Number.String(), nil
  50. }
  51. }
  52. return "", fmt.Errorf("unable to find vSphere disk with serial %v", serialNumber)
  53. }