From 275ee76c20ed3075b14ab77f64b17b9d685a0bc5 Mon Sep 17 00:00:00 2001 From: John Howard Date: Thu, 23 Aug 2018 15:00:38 -0700 Subject: [PATCH] Add DetachVhd entrypoint Signed-off-by: John Howard --- vhd/vhd.go | 26 ++++++++++++++++++++++++++ vhd/zvhd.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/vhd/vhd.go b/vhd/vhd.go index fb9add41..8fa90e91 100644 --- a/vhd/vhd.go +++ b/vhd/vhd.go @@ -7,6 +7,8 @@ import "syscall" //go:generate go run mksyscall_windows.go -output zvhd.go vhd.go //sys createVirtualDisk(virtualStorageType *virtualStorageType, path string, virtualDiskAccessMask uint32, securityDescriptor *uintptr, flags uint32, providerSpecificFlags uint32, parameters *createVirtualDiskParameters, o *syscall.Overlapped, handle *syscall.Handle) (err error) [failretval != 0] = VirtDisk.CreateVirtualDisk +//sys openVirtualDisk(virtualStorageType *virtualStorageType, path string, virtualDiskAccessMask uint32, flags uint32, parameters *uintptr, handle *syscall.Handle) (err error) [failretval != 0] = VirtDisk.OpenVirtualDisk +//sys detachVirtualDisk(handle syscall.Handle, flags uint32, providerSpecificFlags uint32) (err error) [failretval != 0] = VirtDisk.DetachVirtualDisk type virtualStorageType struct { DeviceID uint32 @@ -80,3 +82,27 @@ func CreateVhdx(path string, maxSizeInGb, blockSizeInMb uint32) error { return nil } + +// DetachVhd detaches a VHD attached at the given path. +func DetachVhd(path string) error { + var ( + defaultType virtualStorageType + handle syscall.Handle + ) + + if err := openVirtualDisk( + &defaultType, + path, + virtualDiskAccessDETACH, + 0, + nil, + &handle); err != nil { + return err + } + defer syscall.CloseHandle(handle) + + if err := detachVirtualDisk(handle, 0, 0); err != nil { + return err + } + return nil +} diff --git a/vhd/zvhd.go b/vhd/zvhd.go index c450955a..73f52596 100644 --- a/vhd/zvhd.go +++ b/vhd/zvhd.go @@ -40,6 +40,8 @@ var ( modVirtDisk = windows.NewLazySystemDLL("VirtDisk.dll") procCreateVirtualDisk = modVirtDisk.NewProc("CreateVirtualDisk") + procOpenVirtualDisk = modVirtDisk.NewProc("OpenVirtualDisk") + procDetachVirtualDisk = modVirtDisk.NewProc("DetachVirtualDisk") ) func createVirtualDisk(virtualStorageType *virtualStorageType, path string, virtualDiskAccessMask uint32, securityDescriptor *uintptr, flags uint32, providerSpecificFlags uint32, parameters *createVirtualDiskParameters, o *syscall.Overlapped, handle *syscall.Handle) (err error) { @@ -62,3 +64,36 @@ func _createVirtualDisk(virtualStorageType *virtualStorageType, path *uint16, vi } return } + +func openVirtualDisk(virtualStorageType *virtualStorageType, path string, virtualDiskAccessMask uint32, flags uint32, parameters *uintptr, handle *syscall.Handle) (err error) { + var _p0 *uint16 + _p0, err = syscall.UTF16PtrFromString(path) + if err != nil { + return + } + return _openVirtualDisk(virtualStorageType, _p0, virtualDiskAccessMask, flags, parameters, handle) +} + +func _openVirtualDisk(virtualStorageType *virtualStorageType, path *uint16, virtualDiskAccessMask uint32, flags uint32, parameters *uintptr, handle *syscall.Handle) (err error) { + r1, _, e1 := syscall.Syscall6(procOpenVirtualDisk.Addr(), 6, uintptr(unsafe.Pointer(virtualStorageType)), uintptr(unsafe.Pointer(path)), uintptr(virtualDiskAccessMask), uintptr(flags), uintptr(unsafe.Pointer(parameters)), uintptr(unsafe.Pointer(handle))) + if r1 != 0 { + if e1 != 0 { + err = errnoErr(e1) + } else { + err = syscall.EINVAL + } + } + return +} + +func detachVirtualDisk(handle syscall.Handle, flags uint32, providerSpecificFlags uint32) (err error) { + r1, _, e1 := syscall.Syscall(procDetachVirtualDisk.Addr(), 3, uintptr(handle), uintptr(flags), uintptr(providerSpecificFlags)) + if r1 != 0 { + if e1 != 0 { + err = errnoErr(e1) + } else { + err = syscall.EINVAL + } + } + return +}