Skip to content

Commit 07aef59

Browse files
committed
update macOS example
Signed-off-by: Luca Stocchi <lstocchi@redhat.com>
1 parent 2e292dd commit 07aef59

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

configuration.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ func (v *VirtualMachineConfiguration) SetStorageDevicesVirtualMachineConfigurati
174174
C.setStorageDevicesVZVirtualMachineConfiguration(objc.Ptr(v), objc.Ptr(array))
175175
}
176176

177+
// StorageDevices return the list of storage device configuration configured in this virtual machine configuration.
178+
// Return an empty array if no storage device configuration is set.
179+
func (v *VirtualMachineConfiguration) StorageDevices() []StorageDeviceConfiguration {
180+
nsArray := objc.NewNSArray(
181+
C.storageDevicesVZVirtualMachineConfiguration(objc.Ptr(v)),
182+
)
183+
ptrs := nsArray.ToPointerSlice()
184+
storageDevices := make([]StorageDeviceConfiguration, len(ptrs))
185+
for i, ptr := range ptrs {
186+
storageDevices[i] = newVirtioBlockDeviceConfiguration(ptr)
187+
}
188+
return storageDevices
189+
}
190+
177191
// SetDirectorySharingDevicesVirtualMachineConfiguration sets list of directory sharing devices. Empty by default.
178192
//
179193
// This is only supported on macOS 12 and newer. Older versions do nothing.

example/macOS/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func runVM(ctx context.Context) error {
7878
}
7979
}()
8080

81+
// it start listening to the NBD server, if any
82+
listenNetworkBlockDevice(config)
83+
8184
// cleanup is this function is useful when finished graphic application.
8285
cleanup := func() {
8386
for i := 1; vm.CanRequestStop(); i++ {
@@ -331,3 +334,24 @@ func setupVMConfiguration(platformConfig vz.PlatformConfiguration) (*vz.VirtualM
331334

332335
return config, nil
333336
}
337+
338+
func listenNetworkBlockDevice(vm *vz.VirtualMachineConfiguration) error {
339+
storages := vm.StorageDevices()
340+
for _, storage := range storages {
341+
attachment := storage.Attachment()
342+
if nbdAttachment, isNbdAttachment := attachment.(*vz.NetworkBlockDeviceStorageDeviceAttachment); isNbdAttachment {
343+
nbdAttachmentStatusCh := make(chan vz.NetworkBlockDeviceStorageDeviceAttachmentStatus)
344+
nbdAttachment.Listen(nbdAttachmentStatusCh)
345+
go func() {
346+
for status := range nbdAttachmentStatusCh {
347+
if status.IsConnected() {
348+
log.Println("Successfully connected to NBD server")
349+
} else {
350+
log.Printf("Disconnected from NBD server. Error %v\n", status.Error().Error())
351+
}
352+
}
353+
}()
354+
}
355+
}
356+
return nil
357+
}

storage.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ func NewVirtioBlockDeviceConfiguration(attachment StorageDeviceAttachment) (*Vir
210210
return config, nil
211211
}
212212

213+
func newVirtioBlockDeviceConfiguration(ptr unsafe.Pointer) *VirtioBlockDeviceConfiguration {
214+
return &VirtioBlockDeviceConfiguration{
215+
pointer: objc.NewPointer(ptr),
216+
}
217+
}
218+
213219
// BlockDeviceIdentifier returns the device identifier is a string identifying the Virtio block device.
214220
// Empty string by default.
215221
//

virtualization_11.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ void setSocketDevicesVZVirtualMachineConfiguration(void *config,
5959
void *socketDevicesVZVirtualMachineConfiguration(void *config);
6060
void setStorageDevicesVZVirtualMachineConfiguration(void *config,
6161
void *storageDevices);
62+
void *storageDevicesVZVirtualMachineConfiguration(void *config);
6263

6364
/* Configurations */
6465
void *newVZFileHandleSerialPortAttachment(int readFileDescriptor, int writeFileDescriptor);

virtualization_11.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ void setStorageDevicesVZVirtualMachineConfiguration(void *config,
327327
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
328328
}
329329

330+
/*!
331+
@abstract Return the list of storage devices configurations for this VZVirtualMachineConfiguration. Return an empty array if no storage device configuration is set.
332+
*/
333+
void *storageDevicesVZVirtualMachineConfiguration(void *config)
334+
{
335+
if (@available(macOS 11, *)) {
336+
return [(VZVirtualMachineConfiguration *)config storageDevices]; // NSArray<VZStorageDeviceConfiguration *>
337+
}
338+
339+
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
340+
}
341+
330342
/*!
331343
@abstract Intialize the VZFileHandleSerialPortAttachment from file descriptors.
332344
@param readFileDescriptor File descriptor for reading from the file.

0 commit comments

Comments
 (0)