Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/driver/vz/boot/05-rosetta-volume.sh
2649 views
1
#!/bin/bash
2
3
# SPDX-FileCopyrightText: Copyright The Lima Authors
4
# SPDX-License-Identifier: Apache-2.0
5
6
set -eux -o pipefail
7
8
if [ "$LIMA_CIDATA_ROSETTA_ENABLED" != "true" ]; then
9
exit 0
10
fi
11
12
if [ -f /etc/alpine-release ]; then
13
rc-service procfs start --ifnotstarted
14
rc-service qemu-binfmt stop --ifexists --ifstarted
15
fi
16
17
binfmt_entry=/proc/sys/fs/binfmt_misc/rosetta
18
binfmtd_conf=/usr/lib/binfmt.d/rosetta.conf
19
if [ "$LIMA_CIDATA_ROSETTA_BINFMT" = "true" ]; then
20
rosetta_binfmt=":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"
21
22
# If rosetta is not registered in binfmt_misc, register it.
23
[ -f "$binfmt_entry" ] || echo "$rosetta_binfmt" >/proc/sys/fs/binfmt_misc/register
24
25
# Create binfmt.d(5) configuration to prioritize rosetta even if qemu-user-static is installed on systemd based systems.
26
# If the binfmt.d directory exists, consider systemd-binfmt.service(8) to be enabled and create the configuration file.
27
[ ! -d "$(dirname "$binfmtd_conf")" ] || [ -f "$binfmtd_conf" ] || echo "$rosetta_binfmt" >"$binfmtd_conf"
28
else
29
# unregister rosetta from binfmt_misc if it exists
30
[ ! -f "$binfmt_entry" ] || echo -1 >"$binfmt_entry"
31
# remove binfmt.d(5) configuration if it exists
32
[ ! -f "$binfmtd_conf" ] || rm "$binfmtd_conf"
33
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-vm.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
# nerdctl requires user-specific CDI configuration directories
104
mkdir -p "${LIMA_CIDATA_HOME}/.config/cdi"
105
ln -sf /etc/cdi/rosetta.yaml "${LIMA_CIDATA_HOME}/.config/cdi/"
106
chown -R "${LIMA_CIDATA_USER}" "${LIMA_CIDATA_HOME}/.config"
107
else
108
# Remove CDI configuration for Rosetta AOT Caching
109
[ ! -f /etc/cdi/rosetta.yaml ] || rm /etc/cdi/rosetta.yaml
110
[ ! -d "${LIMA_CIDATA_HOME}/.config/cdi/rosetta.yaml" ] || rm "${LIMA_CIDATA_HOME}/.config/cdi/rosetta.yaml"
111
fi
112
113