Skip to content

Commit 7638316

Browse files
committed
Add support for Rosetta with vmType: vz
Signed-off-by: Chance Zibolski <[email protected]>
1 parent 32baa62 commit 7638316

File tree

10 files changed

+105
-1
lines changed

10 files changed

+105
-1
lines changed

examples/experimental/vz.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Example to run ubuntu using vmType: vz instead of qemu (Default)
2-
# This example requires Lima v0.14.0 or later.
2+
# This example requires Lima v0.14.0 or later and MacOS Ventura.
33
vmType: "vz"
4+
rosetta:
5+
enabled: true
6+
binfmt: true
7+
48
images:
59
- location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
610
arch: "x86_64"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
if [ "$LIMA_CIDATA_ROSETTA_ENABLED" != "true" ]; then
6+
exit 0
7+
fi
8+
9+
mkdir -p /mnt/lima-rosetta
10+
mount -t virtiofs vz-rosetta /mnt/lima-rosetta
11+
12+
if [ "$LIMA_CIDATA_ROSETTA_BINFMT" = "true" ]; then
13+
echo \
14+
':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' \
15+
>/proc/sys/fs/binfmt_misc/register
16+
fi

pkg/cidata/cidata.TEMPLATE.d/lima.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ LIMA_CIDATA_SLIRP_GATEWAY={{.SlirpGateway}}
2727
LIMA_CIDATA_SLIRP_IP_ADDRESS={{.SlirpIPAddress}}
2828
LIMA_CIDATA_UDP_DNS_LOCAL_PORT={{.UDPDNSLocalPort}}
2929
LIMA_CIDATA_TCP_DNS_LOCAL_PORT={{.TCPDNSLocalPort}}
30+
LIMA_CIDATA_ROSETTA_ENABLED={{.RosettaEnabled}}
31+
LIMA_CIDATA_ROSETTA_BINFMT={{.RosettaBinFmt}}

pkg/cidata/cidata.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
126126
SlirpGateway: networks.SlirpGateway,
127127
SlirpDNS: networks.SlirpDNS,
128128
SlirpIPAddress: networks.SlirpIPAddress,
129+
RosettaEnabled: y.Rosetta.Enabled,
130+
RosettaBinFmt: y.Rosetta.BinFmt,
129131
}
130132

131133
// change instance id on every boot so network config will be processed again

pkg/cidata/template.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ type TemplateArgs struct {
7171
CACerts CACerts
7272
HostHomeMountPoint string
7373
BootCmds []BootCmds
74+
RosettaEnabled bool
75+
RosettaBinFmt bool
7476
}
7577

7678
func ValidateTemplateArgs(args TemplateArgs) error {

pkg/limayaml/limayaml.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type LimaYAML struct {
3333
// `useHostResolver` was deprecated in Lima v0.8.1, removed in Lima v0.14.0. Use `hostResolver.enabled` instead.
3434
PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty"`
3535
CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"`
36+
Rosetta Rosetta `yaml:"rosetta,omitempty" json:"rosetta,omitempty"`
3637
}
3738

3839
type Arch = string
@@ -52,6 +53,11 @@ const (
5253
VZ VMType = "vz"
5354
)
5455

56+
type Rosetta struct {
57+
Enabled bool `yaml:"enabled" json:"enabled"`
58+
BinFmt bool `yaml:"binfmt" json:"binfmt"`
59+
}
60+
5561
type File struct {
5662
Location string `yaml:"location" json:"location"` // REQUIRED
5763
Arch Arch `yaml:"arch,omitempty" json:"arch,omitempty"`

pkg/vz/errors_darwin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build darwin && !no_vz
2+
// +build darwin,!no_vz
3+
4+
package vz
5+
6+
import "errors"
7+
8+
var errRosettaUnsupported = errors.New("Rosetta is unsupported on non-ARM64 hosts")

pkg/vz/rosetta_directory_share.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build darwin && !arm64 && !no_vz
2+
// +build darwin,!arm64,!no_vz
3+
4+
package vz
5+
6+
import (
7+
"github.com/Code-Hex/vz/v3"
8+
)
9+
10+
func createRosettaDirectoryShareConfiguration() (*vz.VirtioFileSystemDeviceConfiguration, error) {
11+
return nil, errRosettaUnsupported
12+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//go:build darwin && arm64 && !no_vz
2+
// +build darwin,arm64,!no_vz
3+
4+
package vz
5+
6+
import (
7+
"fmt"
8+
9+
"github.com/Code-Hex/vz/v3"
10+
"github.com/sirupsen/logrus"
11+
)
12+
13+
func createRosettaDirectoryShareConfiguration() (*vz.VirtioFileSystemDeviceConfiguration, error) {
14+
config, err := vz.NewVirtioFileSystemDeviceConfiguration("vz-rosetta")
15+
if err != nil {
16+
return nil, fmt.Errorf("failed to create a new virtio file system configuration: %w", err)
17+
}
18+
availability := vz.LinuxRosettaDirectoryShareAvailability()
19+
switch availability {
20+
case vz.LinuxRosettaAvailabilityNotSupported:
21+
return nil, errRosettaUnsupported
22+
case vz.LinuxRosettaAvailabilityNotInstalled:
23+
logrus.Info("Installing rosetta...")
24+
if err := vz.LinuxRosettaDirectoryShareInstallRosetta(); err != nil {
25+
return nil, fmt.Errorf("failed to install rosetta: %w", err)
26+
}
27+
logrus.Info("Rosetta installation complete.")
28+
case vz.LinuxRosettaAvailabilityInstalled:
29+
// nothing to do
30+
}
31+
32+
rosettaShare, err := vz.NewLinuxRosettaDirectoryShare()
33+
if err != nil {
34+
return nil, fmt.Errorf("failed to create a new rosetta directory share: %w", err)
35+
}
36+
config.SetDirectoryShare(rosettaShare)
37+
38+
return config, nil
39+
}

pkg/vz/vm_darwin.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,19 @@ func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineCo
345345
config.SetDirectoryShare(share)
346346
mounts[i] = config
347347
}
348+
349+
if driver.Yaml.Rosetta.Enabled {
350+
logrus.Info("Setting up Rosetta share")
351+
directorySharingDeviceConfig, err := createRosettaDirectoryShareConfiguration()
352+
if errors.Is(err, errRosettaUnsupported) {
353+
logrus.Warnf("Unable to configure Rosetta: %s", err)
354+
} else if err != nil {
355+
return err
356+
} else {
357+
mounts = append(mounts, directorySharingDeviceConfig)
358+
}
359+
}
360+
348361
vmConfig.SetDirectorySharingDevicesVirtualMachineConfiguration(mounts)
349362
return nil
350363
}

0 commit comments

Comments
 (0)