azure_common_windows.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // +build windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package azure_dd
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "strconv"
  19. "strings"
  20. "k8s.io/klog"
  21. "k8s.io/kubernetes/pkg/util/mount"
  22. )
  23. func scsiHostRescan(io ioHandler, exec mount.Exec) {
  24. cmd := "Update-HostStorageCache"
  25. output, err := exec.Run("powershell", "/c", cmd)
  26. if err != nil {
  27. klog.Errorf("Update-HostStorageCache failed in scsiHostRescan, error: %v, output: %q", err, string(output))
  28. }
  29. }
  30. // search Windows disk number by LUN
  31. func findDiskByLun(lun int, iohandler ioHandler, exec mount.Exec) (string, error) {
  32. cmd := `Get-Disk | select number, location | ConvertTo-Json`
  33. output, err := exec.Run("powershell", "/c", cmd)
  34. if err != nil {
  35. klog.Errorf("Get-Disk failed in findDiskByLun, error: %v, output: %q", err, string(output))
  36. return "", err
  37. }
  38. if len(output) < 10 {
  39. return "", fmt.Errorf("Get-Disk output is too short, output: %q", string(output))
  40. }
  41. var data []map[string]interface{}
  42. if err = json.Unmarshal(output, &data); err != nil {
  43. klog.Errorf("Get-Disk output is not a json array, output: %q", string(output))
  44. return "", err
  45. }
  46. for _, v := range data {
  47. if jsonLocation, ok := v["location"]; ok {
  48. if location, ok := jsonLocation.(string); ok {
  49. if !strings.Contains(location, " LUN ") {
  50. continue
  51. }
  52. arr := strings.Split(location, " ")
  53. arrLen := len(arr)
  54. if arrLen < 3 {
  55. klog.Warningf("unexpected json structure from Get-Disk, location: %q", jsonLocation)
  56. continue
  57. }
  58. klog.V(4).Infof("found a disk, locatin: %q, lun: %q", location, arr[arrLen-1])
  59. //last element of location field is LUN number, e.g.
  60. // "location": "Integrated : Adapter 3 : Port 0 : Target 0 : LUN 1"
  61. l, err := strconv.Atoi(arr[arrLen-1])
  62. if err != nil {
  63. klog.Warningf("cannot parse element from data structure, location: %q, element: %q", location, arr[arrLen-1])
  64. continue
  65. }
  66. if l == lun {
  67. klog.V(4).Infof("found a disk and lun, locatin: %q, lun: %d", location, lun)
  68. if d, ok := v["number"]; ok {
  69. if diskNum, ok := d.(float64); ok {
  70. klog.V(2).Infof("azureDisk Mount: got disk number(%d) by LUN(%d)", int(diskNum), lun)
  71. return fmt.Sprintf(winDiskNumFormat, int(diskNum)), nil
  72. }
  73. klog.Warningf("LUN(%d) found, but could not get disk number(%q), location: %q", lun, d, location)
  74. }
  75. return "", fmt.Errorf("LUN(%d) found, but could not get disk number, location: %q", lun, location)
  76. }
  77. }
  78. }
  79. }
  80. return "", nil
  81. }
  82. func formatIfNotFormatted(disk string, fstype string, exec mount.Exec) {
  83. if err := mount.ValidateDiskNumber(disk); err != nil {
  84. klog.Errorf("azureDisk Mount: formatIfNotFormatted failed, err: %v\n", err)
  85. return
  86. }
  87. if len(fstype) == 0 {
  88. // Use 'NTFS' as the default
  89. fstype = "NTFS"
  90. }
  91. cmd := fmt.Sprintf("Get-Disk -Number %s | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru", disk)
  92. cmd += fmt.Sprintf(" | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false", fstype)
  93. output, err := exec.Run("powershell", "/c", cmd)
  94. if err != nil {
  95. klog.Errorf("azureDisk Mount: Get-Disk failed, error: %v, output: %q", err, string(output))
  96. } else {
  97. klog.Infof("azureDisk Mount: Disk successfully formatted, disk: %q, fstype: %q\n", disk, fstype)
  98. }
  99. }