Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented blockDeviceIdentifier #104

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions osversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ func TestAvailableVersion(t *testing.T) {
}
}
})

t.Run("macOS 12.3", func(t *testing.T) {
majorMinorVersion = 12
cases := map[string]func() error{
"BlockDeviceIdentifier": func() error {
_, err := (*VirtioBlockDeviceConfiguration)(nil).BlockDeviceIdentifier()
return err
},
"SetBlockDeviceIdentifier": func() error {
return (*VirtioBlockDeviceConfiguration)(nil).SetBlockDeviceIdentifier("")
},
}
for name, fn := range cases {
err := fn()
if !errors.Is(err, ErrUnsupportedOSVersion) {
t.Fatalf("unexpected error %v in %s", err, name)
}
}
})
}

func Test_fetchMajorMinorVersion(t *testing.T) {
Expand Down
45 changes: 45 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package vz
#cgo darwin CFLAGS: -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
# include "virtualization.h"
# include "virtualization_12_3.h"
# include "virtualization_13.h"
*/
import "C"
Expand Down Expand Up @@ -104,6 +105,8 @@ type VirtioBlockDeviceConfiguration struct {
*pointer

*baseStorageDeviceConfiguration

blockDeviceIdentifier string
}

// NewVirtioBlockDeviceConfiguration initialize a VZVirtioBlockDeviceConfiguration with a device attachment.
Expand All @@ -130,6 +133,48 @@ func NewVirtioBlockDeviceConfiguration(attachment StorageDeviceAttachment) (*Vir
return config, nil
}

// BlockDeviceIdentifier returns the device identifier is a string identifying the Virtio block device.
// Empty string by default.
//
// The identifier can be retrieved in the guest via a VIRTIO_BLK_T_GET_ID request.
//
// This is only supported on macOS 12.3 and newer, error will be returned on older versions.
//
// see: https://developer.apple.com/documentation/virtualization/vzvirtioblockdeviceconfiguration/3917717-blockdeviceidentifier
func (v *VirtioBlockDeviceConfiguration) BlockDeviceIdentifier() (string, error) {
if err := macOSAvailable(12.3); err != nil {
return "", err
}
return v.blockDeviceIdentifier, nil
}

// SetBlockDeviceIdentifier sets the device identifier is a string identifying the Virtio block device.
//
// The device identifier must be at most 20 bytes in length and ASCII-encodable.
//
// This is only supported on macOS 12.3 and newer, error will be returned on older versions.
//
// see: https://developer.apple.com/documentation/virtualization/vzvirtioblockdeviceconfiguration/3917717-blockdeviceidentifier
func (v *VirtioBlockDeviceConfiguration) SetBlockDeviceIdentifier(identifier string) error {
if err := macOSAvailable(12.3); err != nil {
return err
}
idChar := charWithGoString(identifier)
defer idChar.Free()

nserrPtr := newNSErrorAsNil()
C.setBlockDeviceIdentifierVZVirtioBlockDeviceConfiguration(
objc.Ptr(v),
idChar.CString(),
&nserrPtr,
)
if err := newNSError(nserrPtr); err != nil {
return err
}
v.blockDeviceIdentifier = identifier
return nil
}

// USBMassStorageDeviceConfiguration is a configuration of a USB Mass Storage storage device.
//
// This device configuration creates a storage device that conforms to the USB Mass Storage specification.
Expand Down
64 changes: 64 additions & 0 deletions storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package vz_test

import (
"path/filepath"
"strings"
"testing"

"github.com/Code-Hex/vz/v2"
)

func TestBlockDeviceIdentifier(t *testing.T) {
if vz.Available(12.3) {
t.Skip("VirtioBlockDeviceConfiguration.SetBlockDeviceIdentifier is supported from macOS 12.3")
}
dir := t.TempDir()
path := filepath.Join(dir, "disk.img")
if err := vz.CreateDiskImage(path, 512); err != nil {
t.Fatal(err)
}

attachment, err := vz.NewDiskImageStorageDeviceAttachment(path, false)
if err != nil {
t.Fatal(err)
}
config, err := vz.NewVirtioBlockDeviceConfiguration(attachment)
if err != nil {
t.Fatal(err)
}
got1, err := config.BlockDeviceIdentifier()
if err != nil {
t.Fatal(err)
}
if got1 != "" {
t.Fatalf("want empty by default: %q", got1)
}

invalidID := strings.Repeat("h", 25)
if err := config.SetBlockDeviceIdentifier(invalidID); err == nil {
t.Fatal("want error")
} else {
nserr, ok := err.(*vz.NSError)
if !ok {
t.Fatalf("unexpected error: %v", err)
}
if nserr.Domain != "VZErrorDomain" {
t.Errorf("unexpected NSError domain: %v", nserr)
}
if nserr.Code != int(vz.ErrorInvalidVirtualMachineConfiguration) {
t.Errorf("unexpected NSError code: %v", nserr)
}
}

want := "hello"
if err := config.SetBlockDeviceIdentifier(want); err != nil {
t.Fatal(err)
}
got2, err := config.BlockDeviceIdentifier()
if err != nil {
t.Fatal(err)
}
if got2 != want {
t.Fatalf("want %q but got %q", want, got2)
}
}
16 changes: 16 additions & 0 deletions virtualization_12_3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// virtualization_12_3.h
//
// Created by codehex.
//

#pragma once

#import "virtualization_helper.h"
#import <Virtualization/Virtualization.h>

// FIXME(codehex): this is dirty hack to avoid clang-format error like below
// "Configuration file(s) do(es) not support C++: /github.com/Code-Hex/vz/.clang-format"
#define NSURLComponents NSURLComponents

void setBlockDeviceIdentifierVZVirtioBlockDeviceConfiguration(void *blockDeviceConfig, const char *identifier, void **error);
24 changes: 24 additions & 0 deletions virtualization_12_3.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// virtualization_12_3.m
//
// Created by codehex.
//

#import "virtualization_12_3.h"

void setBlockDeviceIdentifierVZVirtioBlockDeviceConfiguration(void *blockDeviceConfig, const char *identifier, void **error)
{
if (@available(macOS 12.3, *)) {
NSString *identifierNSString = [NSString stringWithUTF8String:identifier];
BOOL valid = [VZVirtioBlockDeviceConfiguration
validateBlockDeviceIdentifier:identifierNSString
error:(NSError *_Nullable *_Nullable)error];
if (!valid) {
return;
}
[(VZVirtioBlockDeviceConfiguration *)blockDeviceConfig setBlockDeviceIdentifier:identifierNSString];
return;
}

RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}