Skip to content

Commit

Permalink
Add support for Rosetta with vmType: vz
Browse files Browse the repository at this point in the history
Signed-off-by: Chance Zibolski <chance.zibolski@gmail.com>
  • Loading branch information
chancez committed Nov 16, 2022
1 parent 1fafcca commit b53ba06
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 1 deletion.
6 changes: 5 additions & 1 deletion examples/experimental/vz.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Example to run ubuntu using vmType: vz instead of qemu (Default)
# This example requires Lima v0.14.0 or later.
# This example requires Lima v0.14.0 or later and MacOS Ventura.
vmType: "vz"
rosetta:
enabled: true
binfmt: true

images:
- location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
arch: "x86_64"
Expand Down
16 changes: 16 additions & 0 deletions pkg/cidata/cidata.TEMPLATE.d/boot/05-rosetta-volume.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

set -eu

if [ "$LIMA_CIDATA_ROSETTA_ENABLED" != "true" ]; then
exit 0
fi

mkdir -p /mnt/lima-rosetta
mount -t virtiofs vz-rosetta /mnt/lima-rosetta

if [ "$LIMA_CIDATA_ROSETTA_BINFMT" = "true" ]; then
echo \
':rosetta:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x3e\x00:\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/mnt/lima-rosetta/rosetta:OCF' \
>/proc/sys/fs/binfmt_misc/register
fi
2 changes: 2 additions & 0 deletions pkg/cidata/cidata.TEMPLATE.d/lima.env
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ LIMA_CIDATA_SLIRP_GATEWAY={{.SlirpGateway}}
LIMA_CIDATA_SLIRP_IP_ADDRESS={{.SlirpIPAddress}}
LIMA_CIDATA_UDP_DNS_LOCAL_PORT={{.UDPDNSLocalPort}}
LIMA_CIDATA_TCP_DNS_LOCAL_PORT={{.TCPDNSLocalPort}}
LIMA_CIDATA_ROSETTA_ENABLED={{.RosettaEnabled}}
LIMA_CIDATA_ROSETTA_BINFMT={{.RosettaBinFmt}}
2 changes: 2 additions & 0 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
SlirpGateway: networks.SlirpGateway,
SlirpDNS: networks.SlirpDNS,
SlirpIPAddress: networks.SlirpIPAddress,
RosettaEnabled: y.Rosetta.Enabled,
RosettaBinFmt: y.Rosetta.BinFmt,
}

// change instance id on every boot so network config will be processed again
Expand Down
2 changes: 2 additions & 0 deletions pkg/cidata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ type TemplateArgs struct {
CACerts CACerts
HostHomeMountPoint string
BootCmds []BootCmds
RosettaEnabled bool
RosettaBinFmt bool
}

func ValidateTemplateArgs(args TemplateArgs) error {
Expand Down
7 changes: 7 additions & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type LimaYAML struct {
UseHostResolver *bool `yaml:"useHostResolver,omitempty" json:"useHostResolver,omitempty"` // DEPRECATED, use `HostResolver.Enabled` instead
PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty"`
CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"`
Rosetta Rosetta `yaml:"rosetta,omitempty" json:"rosetta,omitempty"`
}

type Arch = string
Expand All @@ -52,6 +53,12 @@ const (
VZ VMType = "vz"
)

type Rosetta struct {
Enabled bool `yaml:"enabled" json:"enabled"`
Install bool `yaml:"install" json:"install"`
BinFmt bool `yaml:"binfmt" json:"binfmt"`
}

type File struct {
Location string `yaml:"location" json:"location"` // REQUIRED
Arch Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
Expand Down
14 changes: 14 additions & 0 deletions pkg/vz/rosetta_directory_share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build darwin && !arm64
// +build darwin,!arm64

package vz

import (
"fmt"

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

func createRosettaDirectoryShareConfiguration(install bool) (*vz.VirtioFileSystemDeviceConfiguration, error) {
return nil, errRosettaUnsupported
}
42 changes: 42 additions & 0 deletions pkg/vz/rosseta_directory_share_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//go:build darwin && arm64
// +build darwin,arm64

package vz

import (
"fmt"

"github.com/Code-Hex/vz/v3"
"github.com/sirupsen/logrus"
)

func createRosettaDirectoryShareConfiguration(install bool) (*vz.VirtioFileSystemDeviceConfiguration, error) {
config, err := vz.NewVirtioFileSystemDeviceConfiguration("vz-rosetta")
if err != nil {
return nil, fmt.Errorf("failed to create a new virtio file system configuration: %w", err)
}
availability := vz.LinuxRosettaDirectoryShareAvailability()
switch availability {
case vz.LinuxRosettaAvailabilityNotSupported:
return nil, errRosettaUnsupported
case vz.LinuxRosettaAvailabilityNotInstalled:
if !install {
return nil, fmt.Errorf("Rosetta is not installed")
}
logrus.Info("Installing rosetta...")
if err := vz.LinuxRosettaDirectoryShareInstallRosetta(); err != nil {
return nil, fmt.Errorf("failed to install rosetta: %w", err)
}
logrus.Info("Rosetta installation complete.")
case vz.LinuxRosettaAvailabilityInstalled:
// nothing to do
}

rosettaShare, err := vz.NewLinuxRosettaDirectoryShare()
if err != nil {
return nil, fmt.Errorf("failed to create a new rosetta directory share: %w", err)
}
config.SetDirectoryShare(rosettaShare)

return config, nil
}
15 changes: 15 additions & 0 deletions pkg/vz/vm_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/sirupsen/logrus"
)

var errRosettaUnsupported = errors.New("Rosetta is unsupported on non-ARM64 hosts")

func startVM(ctx context.Context, driver *driver.BaseDriver) (*vz.VirtualMachine, chan error, error) {
server, client, err := createSockPair()
if err != nil {
Expand Down Expand Up @@ -344,6 +346,19 @@ func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineCo
config.SetDirectoryShare(share)
mounts[i] = config
}

if driver.Yaml.Rosetta.Enabled {
logrus.Info("Setting up Rosetta share")
directorySharingDeviceConfig, err := createRosettaDirectoryShareConfiguration(driver.Yaml.Rosetta.Install)
if errors.Is(err, errRosettaUnsupported) {
logrus.Warnf("Unable to configure Rosetta: %s", err)
} else if err != nil {
return err
} else {
mounts = append(mounts, directorySharingDeviceConfig)
}
}

vmConfig.SetDirectorySharingDevicesVirtualMachineConfiguration(mounts)
return nil
}
Expand Down

0 comments on commit b53ba06

Please sign in to comment.