forked from Code-Hex/vz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
109 lines (91 loc) · 3.69 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package vz
/*
#cgo darwin CFLAGS: -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
# include "virtualization.h"
*/
import "C"
import "runtime"
type baseStorageDeviceAttachment struct{}
func (*baseStorageDeviceAttachment) storageDeviceAttachment() {}
// StorageDeviceAttachment for a storage device attachment.
//
// A storage device attachment defines how a virtual machine storage device interfaces with the host system.
// see: https://developer.apple.com/documentation/virtualization/vzstoragedeviceattachment?language=objc
type StorageDeviceAttachment interface {
NSObject
storageDeviceAttachment()
}
var _ StorageDeviceAttachment = (*DiskImageStorageDeviceAttachment)(nil)
// DiskImageStorageDeviceAttachment is a storage device attachment using a disk image to implement the storage.
//
// This storage device attachment uses a disk image on the host file system as the drive of the storage device.
// Only raw data disk images are supported.
// see: https://developer.apple.com/documentation/virtualization/vzdiskimagestoragedeviceattachment?language=objc
type DiskImageStorageDeviceAttachment struct {
pointer
*baseStorageDeviceAttachment
}
// NewDiskImageStorageDeviceAttachment initialize the attachment from a local file path.
// Returns error is not nil, assigned with the error if the initialization failed.
//
// - diskPath is local file URL to the disk image in RAW format.
// - readOnly if YES, the device attachment is read-only, otherwise the device can write data to the disk image.
func NewDiskImageStorageDeviceAttachment(diskPath string, readOnly bool) (*DiskImageStorageDeviceAttachment, error) {
nserr := newNSErrorAsNil()
nserrPtr := nserr.Ptr()
diskPathChar := charWithGoString(diskPath)
defer diskPathChar.Free()
attachment := &DiskImageStorageDeviceAttachment{
pointer: pointer{
ptr: C.newVZDiskImageStorageDeviceAttachment(
diskPathChar.CString(),
C.bool(readOnly),
&nserrPtr,
),
},
}
if err := newNSError(nserrPtr); err != nil {
return nil, err
}
runtime.SetFinalizer(attachment, func(self *DiskImageStorageDeviceAttachment) {
self.Release()
})
return attachment, nil
}
// StorageDeviceConfiguration for a storage device configuration.
type StorageDeviceConfiguration interface {
NSObject
storageDeviceConfiguration()
}
type baseStorageDeviceConfiguration struct{}
func (*baseStorageDeviceConfiguration) storageDeviceConfiguration() {}
var _ StorageDeviceConfiguration = (*VirtioBlockDeviceConfiguration)(nil)
// VirtioBlockDeviceConfiguration is a configuration of a paravirtualized storage device of type Virtio Block Device.
//
// This device configuration creates a storage device using paravirtualization.
// The emulated device follows the Virtio Block Device specification.
//
// The host implementation of the device is done through an attachment subclassing VZStorageDeviceAttachment
// like VZDiskImageStorageDeviceAttachment.
// see: https://developer.apple.com/documentation/virtualization/vzvirtioblockdeviceconfiguration?language=objc
type VirtioBlockDeviceConfiguration struct {
pointer
*baseStorageDeviceConfiguration
}
// NewVirtioBlockDeviceConfiguration initialize a VZVirtioBlockDeviceConfiguration with a device attachment.
//
// - attachment The storage device attachment. This defines how the virtualized device operates on the host side.
func NewVirtioBlockDeviceConfiguration(attachment StorageDeviceAttachment) *VirtioBlockDeviceConfiguration {
config := &VirtioBlockDeviceConfiguration{
pointer: pointer{
ptr: C.newVZVirtioBlockDeviceConfiguration(
attachment.Ptr(),
),
},
}
runtime.SetFinalizer(config, func(self *VirtioBlockDeviceConfiguration) {
self.Release()
})
return config
}