From c14cec2634a34f19a91a1587d4c148e8c22ef14d Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Sun, 15 Mar 2015 23:32:48 +0200 Subject: [PATCH] Windows: Fixes wintypes import issue in vhdutils At the moment, wintypes is accessed as a ctypes attribute. In some environments, this raises an AttributeError. For this reason, the Windows iSCSI and Windows SMBFS drivers fail to initialize. In order to avoid this, wintypes must be explicitly imported in vhdutils. Change-Id: I78bb916b0da3f7540e7a428d0bab10edc6a5b963 Closes-Bug: #1434130 --- cinder/tests/windows/test_vhdutils.py | 11 +-- cinder/volume/drivers/windows/vhdutils.py | 81 ++++++++++++----------- 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/cinder/tests/windows/test_vhdutils.py b/cinder/tests/windows/test_vhdutils.py index 8f7332ef9..ec73315e6 100644 --- a/cinder/tests/windows/test_vhdutils.py +++ b/cinder/tests/windows/test_vhdutils.py @@ -50,7 +50,8 @@ class VHDUtilsTestCase(test.TestCase): mock.patch.multiple( 'cinder.volume.drivers.windows.vhdutils', ctypes=fake_ctypes, kernel32=mock.DEFAULT, - virtdisk=mock.DEFAULT, Win32_GUID=mock.DEFAULT, + wintypes=mock.DEFAULT, virtdisk=mock.DEFAULT, + Win32_GUID=mock.DEFAULT, Win32_RESIZE_VIRTUAL_DISK_PARAMETERS=mock.DEFAULT, Win32_CREATE_VIRTUAL_DISK_PARAMETERS=mock.DEFAULT, Win32_VIRTUAL_STORAGE_TYPE=mock.DEFAULT, @@ -109,7 +110,7 @@ class VHDUtilsTestCase(test.TestCase): vhdutils.VIRTUAL_DISK_ACCESS_NONE, None, vhdutils.CREATE_VIRTUAL_DISK_FLAG_NONE, 0, vhdutils.ctypes.byref(fake_params), None, - vhdutils.ctypes.byref(vhdutils.ctypes.wintypes.HANDLE())) + vhdutils.ctypes.byref(vhdutils.wintypes.HANDLE())) self.assertTrue(self._vhdutils._close.called) def test_create_vhd_exception(self): @@ -150,7 +151,7 @@ class VHDUtilsTestCase(test.TestCase): vhdutils.ctypes.byref(fake_vst), vhdutils.ctypes.c_wchar_p(self._FAKE_VHD_PATH), fake_access_mask, fake_open_flag, fake_params, - vhdutils.ctypes.byref(vhdutils.ctypes.wintypes.HANDLE())) + vhdutils.ctypes.byref(vhdutils.wintypes.HANDLE())) self.assertEqual(fake_device_id, fake_vst.DeviceId) @@ -309,8 +310,8 @@ class VHDUtilsTestCase(test.TestCase): fake_info_member = vhdutils.GET_VIRTUAL_DISK_INFO_SIZE fake_info = mock.Mock() fake_info.VhdInfo.Size._fields_ = [ - ("VirtualSize", vhdutils.ctypes.wintypes.ULARGE_INTEGER), - ("PhysicalSize", vhdutils.ctypes.wintypes.ULARGE_INTEGER)] + ("VirtualSize", vhdutils.wintypes.ULARGE_INTEGER), + ("PhysicalSize", vhdutils.wintypes.ULARGE_INTEGER)] fake_info.VhdInfo.Size.VirtualSize = self._FAKE_VHD_SIZE fake_info.VhdInfo.Size.PhysicalSize = fake_physical_size diff --git a/cinder/volume/drivers/windows/vhdutils.py b/cinder/volume/drivers/windows/vhdutils.py index 64fd511c8..32c95b199 100644 --- a/cinder/volume/drivers/windows/vhdutils.py +++ b/cinder/volume/drivers/windows/vhdutils.py @@ -30,6 +30,7 @@ import ctypes import os if os.name == 'nt': + from ctypes import wintypes kernel32 = ctypes.windll.kernel32 virtdisk = ctypes.windll.virtdisk @@ -43,97 +44,97 @@ LOG = logging.getLogger(__name__) if os.name == 'nt': class Win32_GUID(ctypes.Structure): - _fields_ = [("Data1", ctypes.wintypes.DWORD), - ("Data2", ctypes.wintypes.WORD), - ("Data3", ctypes.wintypes.WORD), - ("Data4", ctypes.wintypes.BYTE * 8)] + _fields_ = [("Data1", wintypes.DWORD), + ("Data2", wintypes.WORD), + ("Data3", wintypes.WORD), + ("Data4", wintypes.BYTE * 8)] class Win32_VIRTUAL_STORAGE_TYPE(ctypes.Structure): _fields_ = [ - ('DeviceId', ctypes.wintypes.ULONG), + ('DeviceId', wintypes.ULONG), ('VendorId', Win32_GUID) ] class Win32_RESIZE_VIRTUAL_DISK_PARAMETERS(ctypes.Structure): _fields_ = [ - ('Version', ctypes.wintypes.DWORD), + ('Version', wintypes.DWORD), ('NewSize', ctypes.c_ulonglong) ] class Win32_OPEN_VIRTUAL_DISK_PARAMETERS_V1(ctypes.Structure): _fields_ = [ - ('Version', ctypes.wintypes.DWORD), + ('Version', wintypes.DWORD), ('RWDepth', ctypes.c_ulong), ] class Win32_OPEN_VIRTUAL_DISK_PARAMETERS_V2(ctypes.Structure): _fields_ = [ - ('Version', ctypes.wintypes.DWORD), - ('GetInfoOnly', ctypes.wintypes.BOOL), - ('ReadOnly', ctypes.wintypes.BOOL), + ('Version', wintypes.DWORD), + ('GetInfoOnly', wintypes.BOOL), + ('ReadOnly', wintypes.BOOL), ('ResiliencyGuid', Win32_GUID) ] class Win32_MERGE_VIRTUAL_DISK_PARAMETERS(ctypes.Structure): _fields_ = [ - ('Version', ctypes.wintypes.DWORD), + ('Version', wintypes.DWORD), ('MergeDepth', ctypes.c_ulong) ] class Win32_CREATE_VIRTUAL_DISK_PARAMETERS(ctypes.Structure): _fields_ = [ - ('Version', ctypes.wintypes.DWORD), + ('Version', wintypes.DWORD), ('UniqueId', Win32_GUID), ('MaximumSize', ctypes.c_ulonglong), - ('BlockSizeInBytes', ctypes.wintypes.ULONG), - ('SectorSizeInBytes', ctypes.wintypes.ULONG), - ('PhysicalSectorSizeInBytes', ctypes.wintypes.ULONG), - ('ParentPath', ctypes.wintypes.LPCWSTR), - ('SourcePath', ctypes.wintypes.LPCWSTR), - ('OpenFlags', ctypes.wintypes.DWORD), + ('BlockSizeInBytes', wintypes.ULONG), + ('SectorSizeInBytes', wintypes.ULONG), + ('PhysicalSectorSizeInBytes', wintypes.ULONG), + ('ParentPath', wintypes.LPCWSTR), + ('SourcePath', wintypes.LPCWSTR), + ('OpenFlags', wintypes.DWORD), ('ParentVirtualStorageType', Win32_VIRTUAL_STORAGE_TYPE), ('SourceVirtualStorageType', Win32_VIRTUAL_STORAGE_TYPE), ('ResiliencyGuid', Win32_GUID) ] class Win32_SIZE(ctypes.Structure): - _fields_ = [("VirtualSize", ctypes.wintypes.ULARGE_INTEGER), - ("PhysicalSize", ctypes.wintypes.ULARGE_INTEGER), - ("BlockSize", ctypes.wintypes.ULONG), - ("SectorSize", ctypes.wintypes.ULONG)] + _fields_ = [("VirtualSize", wintypes.ULARGE_INTEGER), + ("PhysicalSize", wintypes.ULARGE_INTEGER), + ("BlockSize", wintypes.ULONG), + ("SectorSize", wintypes.ULONG)] class Win32_PARENT_LOCATION(ctypes.Structure): - _fields_ = [('ParentResolved', ctypes.wintypes.BOOL), - ('ParentLocationBuffer', ctypes.wintypes.WCHAR * 512)] + _fields_ = [('ParentResolved', wintypes.BOOL), + ('ParentLocationBuffer', wintypes.WCHAR * 512)] class Win32_PHYSICAL_DISK(ctypes.Structure): - _fields_ = [("LogicalSectorSize", ctypes.wintypes.ULONG), - ("PhysicalSectorSize", ctypes.wintypes.ULONG), - ("IsRemote", ctypes.wintypes.BOOL)] + _fields_ = [("LogicalSectorSize", wintypes.ULONG), + ("PhysicalSectorSize", wintypes.ULONG), + ("IsRemote", wintypes.BOOL)] class Win32_VHD_INFO(ctypes.Union): _fields_ = [("Size", Win32_SIZE), ("Identifier", Win32_GUID), ("ParentLocation", Win32_PARENT_LOCATION), ("ParentIdentifier", Win32_GUID), - ("ParentTimestamp", ctypes.wintypes.ULONG), + ("ParentTimestamp", wintypes.ULONG), ("VirtualStorageType", Win32_VIRTUAL_STORAGE_TYPE), - ("ProviderSubtype", ctypes.wintypes.ULONG), - ("Is4kAligned", ctypes.wintypes.BOOL), + ("ProviderSubtype", wintypes.ULONG), + ("Is4kAligned", wintypes.BOOL), ("PhysicalDisk", Win32_PHYSICAL_DISK), - ("VhdPhysicalSectorSize", ctypes.wintypes.ULONG), + ("VhdPhysicalSectorSize", wintypes.ULONG), ("SmallestSafeVirtualSize", - ctypes.wintypes.ULARGE_INTEGER), - ("FragmentationPercentage", ctypes.wintypes.ULONG)] + wintypes.ULARGE_INTEGER), + ("FragmentationPercentage", wintypes.ULONG)] class Win32_GET_VIRTUAL_DISK_INFO_PARAMETERS(ctypes.Structure): - _fields_ = [("VERSION", ctypes.wintypes.UINT), + _fields_ = [("VERSION", wintypes.UINT), ("VhdInfo", Win32_VHD_INFO)] class Win32_SET_VIRTUAL_DISK_INFO_PARAMETERS(ctypes.Structure): _fields_ = [ - ('Version', ctypes.wintypes.DWORD), - ('ParentFilePath', ctypes.wintypes.LPCWSTR) + ('Version', wintypes.DWORD), + ('ParentFilePath', wintypes.LPCWSTR) ] @@ -192,7 +193,7 @@ class VHDUtils(object): guid.Data1 = 0xec984aec guid.Data2 = 0xa0f9 guid.Data3 = 0x47e9 - ByteArray8 = ctypes.wintypes.BYTE * 8 + ByteArray8 = wintypes.BYTE * 8 guid.Data4 = ByteArray8(0x90, 0x1f, 0x71, 0x41, 0x5a, 0x66, 0x34, 0x5b) return guid @@ -205,7 +206,7 @@ class VHDUtils(object): vst.DeviceId = device_id vst.VendorId = self._msft_vendor_id - handle = ctypes.wintypes.HANDLE() + handle = wintypes.HANDLE() ret_val = virtdisk.OpenVirtualDisk(ctypes.byref(vst), ctypes.c_wchar_p(vhd_path), @@ -295,7 +296,7 @@ class VHDUtils(object): params.SourceVirtualStorageType.DeviceId = src_device_id params.SourceVirtualStorageType.VendorId = self._msft_vendor_id - handle = ctypes.wintypes.HANDLE() + handle = wintypes.HANDLE() create_virtual_disk_flag = self.create_virtual_disk_flags.get( new_vhd_type) @@ -335,7 +336,7 @@ class VHDUtils(object): infoSize = ctypes.sizeof(virt_disk_info) - virtdisk.GetVirtualDiskInformation.restype = ctypes.wintypes.DWORD + virtdisk.GetVirtualDiskInformation.restype = wintypes.DWORD ret_val = virtdisk.GetVirtualDiskInformation( vhd_file, ctypes.byref(ctypes.c_ulong(infoSize)), -- 2.45.2