2017-07-31 02:38:11 +00:00
|
|
|
// +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"
|
|
|
|
|
2018-11-09 18:49:10 +00:00
|
|
|
"k8s.io/klog"
|
2017-07-31 02:38:11 +00:00
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
|
|
|
)
|
|
|
|
|
|
|
|
func scsiHostRescan(io ioHandler, exec mount.Exec) {
|
|
|
|
cmd := "Update-HostStorageCache"
|
|
|
|
output, err := exec.Run("powershell", "/c", cmd)
|
|
|
|
if err != nil {
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.Errorf("Update-HostStorageCache failed in scsiHostRescan, error: %v, output: %q", err, string(output))
|
2017-07-31 02:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// search Windows disk number by LUN
|
|
|
|
func findDiskByLun(lun int, iohandler ioHandler, exec mount.Exec) (string, error) {
|
|
|
|
cmd := `Get-Disk | select number, location | ConvertTo-Json`
|
|
|
|
output, err := exec.Run("powershell", "/c", cmd)
|
|
|
|
if err != nil {
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.Errorf("Get-Disk failed in findDiskByLun, error: %v, output: %q", err, string(output))
|
2017-07-31 02:38:11 +00:00
|
|
|
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 {
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.Errorf("Get-Disk output is not a json array, output: %q", string(output))
|
2017-07-31 02:38:11 +00:00
|
|
|
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 {
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.Warningf("unexpected json structure from Get-Disk, location: %q", jsonLocation)
|
2017-07-31 02:38:11 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.V(4).Infof("found a disk, locatin: %q, lun: %q", location, arr[arrLen-1])
|
2017-07-31 02:38:11 +00:00
|
|
|
//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 {
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.Warningf("cannot parse element from data structure, location: %q, element: %q", location, arr[arrLen-1])
|
2017-07-31 02:38:11 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if l == lun {
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.V(4).Infof("found a disk and lun, locatin: %q, lun: %d", location, lun)
|
2017-07-31 02:38:11 +00:00
|
|
|
if d, ok := v["number"]; ok {
|
|
|
|
if diskNum, ok := d.(float64); ok {
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.V(2).Infof("azureDisk Mount: got disk number(%d) by LUN(%d)", int(diskNum), lun)
|
2017-07-31 02:38:11 +00:00
|
|
|
return strconv.Itoa(int(diskNum)), nil
|
|
|
|
}
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.Warningf("LUN(%d) found, but could not get disk number(%q), location: %q", lun, d, location)
|
2017-07-31 02:38:11 +00:00
|
|
|
}
|
|
|
|
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 mount.Exec) {
|
|
|
|
if err := mount.ValidateDiskNumber(disk); err != nil {
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.Errorf("azureDisk Mount: formatIfNotFormatted failed, err: %v\n", err)
|
2017-07-31 02:38:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-16 08:25:03 +00:00
|
|
|
if len(fstype) == 0 {
|
|
|
|
// Use 'NTFS' as the default
|
|
|
|
fstype = "NTFS"
|
|
|
|
}
|
2017-07-31 02:38:11 +00:00
|
|
|
cmd := fmt.Sprintf("Get-Disk -Number %s | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru", disk)
|
2018-03-16 08:25:03 +00:00
|
|
|
cmd += fmt.Sprintf(" | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false", fstype)
|
2017-07-31 02:38:11 +00:00
|
|
|
output, err := exec.Run("powershell", "/c", cmd)
|
|
|
|
if err != nil {
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.Errorf("azureDisk Mount: Get-Disk failed, error: %v, output: %q", err, string(output))
|
2017-07-31 02:38:11 +00:00
|
|
|
} else {
|
2018-11-09 18:49:10 +00:00
|
|
|
klog.Infof("azureDisk Mount: Disk successfully formatted, disk: %q, fstype: %q\n", disk, fstype)
|
2017-07-31 02:38:11 +00:00
|
|
|
}
|
|
|
|
}
|