diff --git a/pkg/volume/azure_dd/azure_common.go b/pkg/volume/azure_dd/azure_common.go index 7c36b3984a6..458fb007d30 100644 --- a/pkg/volume/azure_dd/azure_common.go +++ b/pkg/volume/azure_dd/azure_common.go @@ -21,7 +21,6 @@ import ( "io/ioutil" "os" "path" - "regexp" "strconv" libstrings "strings" @@ -158,6 +157,7 @@ type ioHandler interface { ReadDir(dirname string) ([]os.FileInfo, error) WriteFile(filename string, data []byte, perm os.FileMode) error Readlink(name string) (string, error) + ReadFile(filename string) ([]byte, error) } //TODO: check if priming the iscsi interface is actually needed @@ -176,6 +176,10 @@ func (handler *osIOHandler) Readlink(name string) (string, error) { return os.Readlink(name) } +func (handler *osIOHandler) ReadFile(filename string) ([]byte, error) { + return ioutil.ReadFile(filename) +} + // exclude those used by azure as resource and OS root in /dev/disk/azure func listAzureDiskPath(io ioHandler) []string { azureDiskPath := "/dev/disk/azure/" @@ -237,18 +241,30 @@ func findDiskByLunWithConstraint(lun int, io ioHandler, exe exec.Interface, azur if lun == l { // find the matching LUN // read vendor and model to ensure it is a VHD disk - vendor := path.Join(sys_path, name, "vendor") - model := path.Join(sys_path, name, "model") - out, err := exe.Command("cat", vendor, model).CombinedOutput() + vendorPath := path.Join(sys_path, name, "vendor") + vendorBytes, err := io.ReadFile(vendorPath) if err != nil { - glog.V(4).Infof("azure disk - failed to cat device vendor and model, err: %v", err) + glog.Errorf("failed to read device vendor, err: %v", err) continue } - matched, err := regexp.MatchString("^MSFT[ ]{0,}\nVIRTUAL DISK[ ]{0,}\n$", libstrings.ToUpper(string(out))) - if err != nil || !matched { - glog.V(4).Infof("azure disk - doesn't match VHD, output %v, error %v", string(out), err) + vendor := libstrings.TrimSpace(string(vendorBytes)) + if libstrings.ToUpper(vendor) != "MSFT" { + glog.V(4).Infof("vendor doesn't match VHD, got %s", vendor) continue } + + modelPath := path.Join(sys_path, name, "model") + modelBytes, err := io.ReadFile(modelPath) + if err != nil { + glog.Errorf("failed to read device model, err: %v", err) + continue + } + model := libstrings.TrimSpace(string(modelBytes)) + if libstrings.ToUpper(model) != "VIRTUAL DISK" { + glog.V(4).Infof("model doesn't match VHD, got %s", model) + continue + } + // find a disk, validate name dir := path.Join(sys_path, name, "block") if dev, err := io.ReadDir(dir); err == nil { diff --git a/pkg/volume/azure_dd/azure_common_test.go b/pkg/volume/azure_dd/azure_common_test.go index d026e2b92ee..63e81088abb 100644 --- a/pkg/volume/azure_dd/azure_common_test.go +++ b/pkg/volume/azure_dd/azure_common_test.go @@ -19,6 +19,7 @@ package azure_dd import ( "fmt" "os" + "strings" "testing" "time" @@ -107,6 +108,16 @@ func (handler *fakeIOHandler) Readlink(name string) (string, error) { return "/dev/azure/disk/sda", nil } +func (handler *fakeIOHandler) ReadFile(filename string) ([]byte, error) { + if strings.HasSuffix(filename, "vendor") { + return []byte("Msft \n"), nil + } + if strings.HasSuffix(filename, "model") { + return []byte("Virtual Disk \n"), nil + } + return nil, fmt.Errorf("unknown file") +} + func TestIoHandler(t *testing.T) { var fcmd fakeexec.FakeCmd fcmd = fakeexec.FakeCmd{