Skip to content

Commit 5e53248

Browse files
committed
feature: use Rosetta AOT Caching with CDI
This change introduces device configuration to enable Rosetta AOT Caching in Docker VMs. - Modify Rosetta Caching Options from Abstract Socket to Unix Domain Socket: Unix Domain Socket can be mounted within a container using the Container Device Interface (CDI) mechanism. This requires merging the following pull request: Code-Hex/vz#195. - Register Rosettad AOT Caching Daemon as a service: - `/etc/systemd/system/rosettad.service` on systemd - `/etc/init.d/rosettad` on OpenRC - Add "lima.io/rosetta=cached" device specification to `/etc/cdi/rosetta.yaml` see: https://github.com/cncf-tags/container-device-interface/blob/main/SPEC.md - Add `{~/.config,/etc}/docker/daemon.json` to `docker{,-rootful}.yaml` - `.features.cdi = true` to enable CDI - `.features."containerd-snapshotter" = true` to enable multi-arch builds To enable Rosetta AOT Caching in docker, use `--device=lima.io/rosetta=cached`. see: https://docs.docker.com/build/building/cdi/ Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent 5e35d67 commit 5e53248

File tree

6 files changed

+104
-3
lines changed

6 files changed

+104
-3
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,7 @@ require (
135135
sigs.k8s.io/structured-merge-diff/v4 v4.5.0 // indirect
136136
sigs.k8s.io/yaml v1.4.0 // indirect
137137
)
138+
139+
// To use Roseetta AOT Caching with CDI, it requires to fix vz.NewLinuxRosettaUnixSocketCachingOptions.
140+
// see: https://github.com/Code-Hex/vz/pull/195
141+
replace github.com/Code-Hex/vz/v3 => github.com/norio-nomura/vz/v3 v3.7.1-0.20250815045701-95b4b8c6ff35

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkk
44
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
55
github.com/Code-Hex/go-infinity-channel v1.0.0 h1:M8BWlfDOxq9or9yvF9+YkceoTkDI1pFAqvnP87Zh0Nw=
66
github.com/Code-Hex/go-infinity-channel v1.0.0/go.mod h1:5yUVg/Fqao9dAjcpzoQ33WwfdMWmISOrQloDRn3bsvY=
7-
github.com/Code-Hex/vz/v3 v3.7.0 h1:VEkfq5TVKnv85M81gQVPzLH9JzHrUJN/QQMpDZ+odPA=
8-
github.com/Code-Hex/vz/v3 v3.7.0/go.mod h1:1LsW0jqW0r0cQ+IeR4hHbjdqOtSidNCVMWhStMHGho8=
97
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
108
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
119
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
@@ -197,6 +195,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
197195
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
198196
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
199197
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
198+
github.com/norio-nomura/vz/v3 v3.7.1-0.20250815045701-95b4b8c6ff35 h1:HHEmYEMeSeJ+rGWZu2QQOnQAPAmzTMEzn2EeW77RHV4=
199+
github.com/norio-nomura/vz/v3 v3.7.1-0.20250815045701-95b4b8c6ff35/go.mod h1:1LsW0jqW0r0cQ+IeR4hHbjdqOtSidNCVMWhStMHGho8=
200200
github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY=
201201
github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc=
202202
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=

pkg/cidata/cidata.TEMPLATE.d/boot/05-rosetta-volume.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,76 @@ else
3131
# remove binfmt.d(5) configuration if it exists
3232
[ ! -f "$binfmtd_conf" ] || rm "$binfmtd_conf"
3333
fi
34+
35+
if [ -x /mnt/lima-rosetta/rosettad ]; then
36+
CACHE_DIRECTORY=/var/cache/rosettad
37+
DEFAULT_SOCKET=${CACHE_DIRECTORY}/uds/rosetta.sock
38+
EXPECTED_SOCKET=/run/rosettad/rosetta.sock
39+
40+
# Create rosettad service
41+
if [ -f /sbin/openrc-run ]; then
42+
cat >/etc/init.d/rosettad <<EOF
43+
#!/sbin/openrc-run
44+
name="rosettad"
45+
description="Rosetta AOT Caching Daemon"
46+
required_dirs=/mnt/lima-rosetta
47+
required_files=/mnt/lima-rosetta/rosettad
48+
command=/mnt/lima-rosetta/rosettad
49+
command_args="daemon ${CACHE_DIRECTORY}"
50+
command_background=true
51+
pidfile="/run/rosettad.pid"
52+
start_pre() {
53+
# To detect creation of the socket by rosettad, remove the old socket before starting
54+
test ! -e "${DEFAULT_SOCKET}" || rm -f "${DEFAULT_SOCKET}"
55+
}
56+
start_post() {
57+
# Set the socket permission to world-writable
58+
while ! chmod -f go+w "${DEFAULT_SOCKET}"; do sleep 1; done
59+
# Create the symlink as expected by the configuration to enable Rosetta AOT caching
60+
mkdir -p "$(dirname "${EXPECTED_SOCKET}")"
61+
ln -sf "${DEFAULT_SOCKET}" "${EXPECTED_SOCKET}"
62+
}
63+
EOF
64+
chmod 755 /etc/init.d/rosettad
65+
rc-update add rosettad default
66+
rc-service rosettad start
67+
else
68+
cat > /etc/systemd/system/rosettad.service <<EOF
69+
[Unit]
70+
Description=Rosetta AOT Caching Daemon
71+
RequiresMountsFor=/mnt/lima-rosetta
72+
[Service]
73+
RuntimeDirectory=rosettad
74+
CacheDirectory=rosettad
75+
# To detect creation of the socket by rosettad, remove the old socket
76+
ExecStartPre=sh -c "test ! -e \"${DEFAULT_SOCKET}\" || rm -f \"${DEFAULT_SOCKET}\""
77+
ExecStart=/mnt/lima-rosetta/rosettad daemon "${CACHE_DIRECTORY}"
78+
# Set the socket permission to world-writable and create the symlink as expected by the configuration to enable Rosetta AOT caching.
79+
ExecStartPost=sh -c "while ! chmod -f go+w \"${DEFAULT_SOCKET}\"; do sleep 1; done; ln -sf \"${DEFAULT_SOCKET}\" \"${EXPECTED_SOCKET}\""
80+
OOMPolicy=continue
81+
OOMScoreAdjust=-500
82+
[Install]
83+
WantedBy=default.target
84+
EOF
85+
systemctl is-enabled rosettad || systemctl enable --now rosettad
86+
fi
87+
88+
# Create CDI configuration for Rosetta
89+
mkdir -p /etc/cdi /var/run/cdi /etc/buildkit/cdi
90+
cat > /etc/cdi/rosetta.yaml <<EOF
91+
cdiVersion: "0.6.0"
92+
kind: "lima.io/rosetta"
93+
devices:
94+
- name: cached
95+
containerEdits:
96+
mounts:
97+
- hostPath: /var/cache/rosettad/uds/rosetta.sock
98+
containerPath: /run/rosettad/rosetta.sock
99+
options: [bind]
100+
annotations:
101+
org.mobyproject.buildkit.device.autoallow: true
102+
EOF
103+
else
104+
# Remove CDI configuration for Rosetta AOT Caching
105+
[ ! -f /etc/cdi/rosetta.yaml ] || rm /etc/cdi/rosetta.yaml
106+
fi

pkg/driver/vz/rosetta_directory_share_arm64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func createRosettaDirectoryShareConfiguration() (*vz.VirtioFileSystemDeviceConfi
4444
return nil, fmt.Errorf("failed to get macOS product version: %w", err)
4545
}
4646
if !macOSProductVersion.LessThan(*semver.New("14.0.0")) {
47-
cachingOption, err := vz.NewLinuxRosettaAbstractSocketCachingOptions("rosetta")
47+
cachingOption, err := vz.NewLinuxRosettaUnixSocketCachingOptions("/run/rosettad/rosetta.sock")
4848
if err != nil {
4949
return nil, fmt.Errorf("failed to create a new rosetta directory share caching option: %w", err)
5050
}

templates/docker-rootful.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ provision:
3838
SocketUser={{.User}}
3939
EOF
4040
fi
41+
daemon_json="/etc/docker/daemon.json"
42+
if [ ! -f "${daemon_json}" ]; then
43+
mkdir -p "$(dirname "${daemon_json}")"
44+
cat > "${daemon_json}" <<EOF
45+
{
46+
"features": {
47+
"cdi": true,
48+
"containerd-snapshotter": true
49+
}
50+
}
51+
EOF
52+
fi
4153
export DEBIAN_FRONTEND=noninteractive
4254
curl -fsSL https://get.docker.com | sh
4355
probes:

templates/docker.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ provision:
3939
script: |
4040
#!/bin/bash
4141
set -eux -o pipefail
42+
daemon_json="{{.Home}}/.config/docker/daemon.json"
43+
if [ ! -f "${daemon_json}" ]; then
44+
mkdir -p "$(dirname "${daemon_json}")"
45+
cat > "${daemon_json}" <<EOF
46+
{
47+
"features": {
48+
"cdi": true,
49+
"containerd-snapshotter": true
50+
}
51+
}
52+
EOF
53+
fi
4254
systemctl --user start dbus
4355
dockerd-rootless-setuptool.sh install
4456
docker context use rootless

0 commit comments

Comments
 (0)