|
| 1 | +package volmount |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + "syscall" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/Microsoft/go-winio/vhd" |
| 13 | + "github.com/hpe-storage/common-host-libs/windows/wmi" |
| 14 | + "github.com/pkg/errors" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +// fullObjectIDFromObjectID turns the given class and objectID into an objectID suitable for calling |
| 19 | +// wmi.ExecWmiMethod on. |
| 20 | +// There should be an __PATH property on the object for this, according to wbemtest.exe, but I cannot see |
| 21 | +// how to get it from the objects exposed by wmi. |
| 22 | +func fullObjectIDFromObjectID(class, objectID string) string { |
| 23 | + return class + `.ObjectId="` + strings.ReplaceAll(strings.ReplaceAll(objectID, `\`, `\\`), `"`, `\"`) + `"` |
| 24 | +} |
| 25 | + |
| 26 | +// turnRawDiskIntoAVolume will take the given PhysicalDisk path, **wipe its partition table** and |
| 27 | +// create a single new partition, returning the Volume path needed by the mount-point APIs. |
| 28 | +func turnRawDiskIntoAVolume(physPath string) (string, error) { |
| 29 | + // This is all implemented via WMI because that turned out to be easier. |
| 30 | + const wmiNamespace = "Root\\Microsoft\\Windows\\Storage" |
| 31 | + |
| 32 | + // If go-winio ever grows support for DRIVE_LAYOUT_INFORMATION_EX and PARTITION_INFORMATION_EX, |
| 33 | + // this could be done using DeviceIoControl instead, to exercise those APIs. |
| 34 | + |
| 35 | + physicalDiskRE := regexp.MustCompile(`\\\\.\\PhysicalDrive(.*)`) |
| 36 | + matches := physicalDiskRE.FindStringSubmatch(physPath) |
| 37 | + if len(matches) != 2 { |
| 38 | + return "", errors.Errorf("Bad drive physical path %s", physPath) |
| 39 | + } |
| 40 | + driveNumber := matches[1] |
| 41 | + |
| 42 | + disks, err := wmi.GetMSFTDisk("Number = " + driveNumber) |
| 43 | + if err != nil { |
| 44 | + return "", errors.Wrapf(err, "wmi.GetMSFTDIsk(%s) failed", "Number = "+driveNumber) |
| 45 | + } |
| 46 | + |
| 47 | + if len(disks) != 1 { |
| 48 | + return "", errors.Errorf("wmi.GetMSFTDIsk returned unexpected disk list: %v", disks) |
| 49 | + } |
| 50 | + |
| 51 | + disk := disks[0] |
| 52 | + |
| 53 | + const diskClass = "MSFT_Disk" |
| 54 | + |
| 55 | + fullDiskObjectID := fullObjectIDFromObjectID(diskClass, disk.ObjectId) |
| 56 | + |
| 57 | + // https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/initialize-msft-disk |
| 58 | + result, err := wmi.ExecWmiMethod(fullDiskObjectID, "Initialize", wmiNamespace, 1) |
| 59 | + if err != nil { |
| 60 | + return "", errors.Wrapf(err, "wmi.ExecWmiMethod(%s, Initialize) failed", fullDiskObjectID) |
| 61 | + } |
| 62 | + if result.Val != 0 { |
| 63 | + return "", errors.Errorf("Unexpected %s::Initialize Result: %d", diskClass, result.Val) |
| 64 | + } |
| 65 | + |
| 66 | + // https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/createpartition-msft-disk |
| 67 | + // In theory the next arg is an output UTF-16 string to save me enumerating partitions below, but |
| 68 | + // I don't know how to make this work with out-params. |
| 69 | + result, err = wmi.ExecWmiMethod(fullDiskObjectID, "CreatePartition", wmiNamespace, nil, true, nil, nil, nil, false, 7, nil, false, false) |
| 70 | + if err != nil { |
| 71 | + return "", errors.Wrapf(err, "ExecWmiMethod(%v, CreatePartition) failed", fullDiskObjectID) |
| 72 | + } |
| 73 | + if result.Val != 0 { |
| 74 | + return "", errors.Errorf("Unexpected %s::CreatePartition return value: %d", diskClass, result.Val) |
| 75 | + } |
| 76 | + |
| 77 | + if err = wmi.RescanDisks(); err != nil { |
| 78 | + return "", errors.Wrap(err, "RescanDisks failed") |
| 79 | + } |
| 80 | + |
| 81 | + // https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-partition |
| 82 | + partitions, err := wmi.GetMSFTPartitionForDiskNumber(disk.Number) |
| 83 | + if err != nil { |
| 84 | + return "", errors.Wrapf(err, "GetMSFTPartitionForDiskNumber(%d) failed", disk.Number) |
| 85 | + } |
| 86 | + |
| 87 | + if len(partitions) != 1 { |
| 88 | + return "", errors.Errorf("GetMSFTPartitionForDiskNumber failed to find our new partition: %v", partitions) |
| 89 | + } |
| 90 | + |
| 91 | + partition := partitions[0] |
| 92 | + |
| 93 | + if len(partition.AccessPaths) != 1 { |
| 94 | + return "", errors.Errorf("New partition has unexpectedly access paths (expecting only one): %v", partition.AccessPaths) |
| 95 | + } |
| 96 | + |
| 97 | + volumePath := partition.AccessPaths[0] |
| 98 | + |
| 99 | + volumeRE := regexp.MustCompile(`\\\\\?\\Volume{.*}\\`) |
| 100 | + if !volumeRE.MatchString(volumePath) { |
| 101 | + return "", errors.Errorf("New partition access path not a volume path: %s", volumePath) |
| 102 | + } |
| 103 | + |
| 104 | + // Apparently, we don't need to format this volume, SetVolumeMountPoint doesn't care. |
| 105 | + return volumePath, nil |
| 106 | +} |
| 107 | + |
| 108 | +func mountAtAndCheck(t *testing.T, volumePath, mountPoint string) { |
| 109 | + err := os.MkdirAll(mountPoint, 0) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + |
| 114 | + err = SetVolumeMountPoint(mountPoint, volumePath) |
| 115 | + if err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + defer func() { |
| 119 | + if t.Failed() { |
| 120 | + deleteErr := DeleteVolumeMountPoint(mountPoint) |
| 121 | + if deleteErr != nil { |
| 122 | + // Assuming if we're already failing, failing more isn't wrong. |
| 123 | + t.Fatal(deleteErr) |
| 124 | + } |
| 125 | + } |
| 126 | + }() |
| 127 | + |
| 128 | + mountPointVolumePath, err := GetVolumeNameForVolumeMountPoint(mountPoint) |
| 129 | + if err != nil { |
| 130 | + t.Fatal(err) |
| 131 | + } |
| 132 | + |
| 133 | + if mountPointVolumePath != volumePath { |
| 134 | + t.Fatalf("Mount read-back incorrectly, expected %s; got %s", volumePath, mountPointVolumePath) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// TestVolumeMountAPIs creates and attaches a small VHD, and then exercises the |
| 139 | +// various mount-point APIs against it. |
| 140 | +func TestVolumeMountSuccess(t *testing.T) { |
| 141 | + |
| 142 | + dir, err := ioutil.TempDir("", "volmountapis") |
| 143 | + if err != nil { |
| 144 | + t.Fatal(err) |
| 145 | + } |
| 146 | + defer os.RemoveAll(dir) |
| 147 | + |
| 148 | + vhdPath := filepath.Join(dir, "small.vhdx") |
| 149 | + |
| 150 | + createParams := vhd.CreateVirtualDiskParameters{ |
| 151 | + Version: 2, |
| 152 | + Version2: vhd.CreateVersion2{ |
| 153 | + MaximumSize: 5 * 1024 * 1024, |
| 154 | + BlockSizeInBytes: 1024 * 1024, |
| 155 | + }, |
| 156 | + } |
| 157 | + |
| 158 | + vhdHandle, err := vhd.CreateVirtualDisk(vhdPath, vhd.VirtualDiskAccessNone, vhd.CreateVirtualDiskFlagNone, &createParams) |
| 159 | + if err != nil { |
| 160 | + t.Fatal(err) |
| 161 | + } |
| 162 | + defer os.Remove(vhdPath) |
| 163 | + defer syscall.CloseHandle(vhdHandle) |
| 164 | + |
| 165 | + // This needs to be done elevated, or with some specific permission anyway. |
| 166 | + attachParams := vhd.AttachVirtualDiskParameters{Version: 2} |
| 167 | + err = vhd.AttachVirtualDisk(vhdHandle, vhd.AttachVirtualDiskFlagNoDriveLetter, &attachParams) |
| 168 | + if err != nil { |
| 169 | + if errno, ok := errors.Cause(err).(syscall.Errno); ok && errno == syscall.ERROR_PRIVILEGE_NOT_HELD { |
| 170 | + t.Skip(err) |
| 171 | + } |
| 172 | + t.Fatal(err) |
| 173 | + } |
| 174 | + defer func() { |
| 175 | + detachErr := vhd.DetachVirtualDisk(vhdHandle) |
| 176 | + if detachErr != nil { |
| 177 | + // assuming if we're already failing, failing more isn't wrong |
| 178 | + t.Fatal(detachErr) |
| 179 | + } |
| 180 | + }() |
| 181 | + |
| 182 | + physPath, err := vhd.GetVirtualDiskPhysicalPath(vhdHandle) |
| 183 | + if err != nil { |
| 184 | + t.Fatal(err) |
| 185 | + } |
| 186 | + |
| 187 | + volumePath, err := turnRawDiskIntoAVolume(physPath) |
| 188 | + if err != nil { |
| 189 | + t.Fatal(err) |
| 190 | + } |
| 191 | + |
| 192 | + mountPoints, err := GetMountPathsFromVolumeName(volumePath) |
| 193 | + if err != nil { |
| 194 | + t.Fatal(err) |
| 195 | + } |
| 196 | + |
| 197 | + if len(mountPoints) != 0 { |
| 198 | + t.Fatalf("Brand new volume was unexpectedly mounted at: %v", mountPoints) |
| 199 | + } |
| 200 | + |
| 201 | + mountPoint0 := filepath.Join(dir, "mount0") |
| 202 | + mountAtAndCheck(t, volumePath, mountPoint0) |
| 203 | + defer func() { |
| 204 | + deleteErr := DeleteVolumeMountPoint(mountPoint0) |
| 205 | + if deleteErr != nil { |
| 206 | + // Assuming if we're already failing, failing more isn't wrong. |
| 207 | + t.Fatal(deleteErr) |
| 208 | + } |
| 209 | + }() |
| 210 | + |
| 211 | + mountPoints, err = GetMountPathsFromVolumeName(volumePath) |
| 212 | + if err != nil { |
| 213 | + t.Fatal(err) |
| 214 | + } |
| 215 | + |
| 216 | + expectedMountPoints := []string{mountPoint0 + string(filepath.Separator)} |
| 217 | + require.ElementsMatch(t, expectedMountPoints, mountPoints, "Mount apparently failed, expected %v (order irrelevant): got %v", expectedMountPoints, mountPoints) |
| 218 | + |
| 219 | + mountPoint1 := filepath.Join(dir, "mount1") |
| 220 | + mountAtAndCheck(t, volumePath, mountPoint1) |
| 221 | + defer func() { |
| 222 | + deleteErr := DeleteVolumeMountPoint(mountPoint1) |
| 223 | + if deleteErr != nil { |
| 224 | + // Assuming if we're already failing, failing more isn't wrong. |
| 225 | + t.Fatal(deleteErr) |
| 226 | + } |
| 227 | + }() |
| 228 | + |
| 229 | + mountPoints, err = GetMountPathsFromVolumeName(volumePath) |
| 230 | + if err != nil { |
| 231 | + t.Fatal(err) |
| 232 | + } |
| 233 | + |
| 234 | + // No order guarantee on the mounts |
| 235 | + expectedMountPoints = []string{mountPoint0 + string(filepath.Separator), mountPoint1 + string(filepath.Separator)} |
| 236 | + require.ElementsMatch(t, expectedMountPoints, mountPoints, "Mount apparently failed, expected %v (order irrelevant): got %v", expectedMountPoints, mountPoints) |
| 237 | + |
| 238 | + // Note: DeleteVolumeMountPoint is tested in the defers above. |
| 239 | +} |
0 commit comments