Skip to content

Commit 598f45f

Browse files
authored
Merge pull request #2474 from norio-nomura/prioritize-rosetta-over-qemu
[Rosetta] Prioritize rosetta over `qemu-user-static` by creating `binfmt.d(5)` configuration
2 parents 0f21216 + 5ef067b commit 598f45f

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,20 @@ if [ -f /etc/alpine-release ]; then
1010
rc-service qemu-binfmt stop --ifstarted
1111
fi
1212

13-
mkdir -p /mnt/lima-rosetta
13+
binfmt_entry=/proc/sys/fs/binfmt_misc/rosetta
14+
binfmtd_conf=/usr/lib/binfmt.d/rosetta.conf
15+
if [ "$LIMA_CIDATA_ROSETTA_BINFMT" = "true" ]; then
16+
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"
1417

15-
#Check selinux is enabled by kernel
16-
if [ -d /sys/fs/selinux ]; then
17-
##########################################################################################
18-
## When using vz & virtiofs, initially container_file_t selinux label
19-
## was considered which works perfectly for container work loads
20-
## but it might break for other work loads if the process is running with
21-
## different label. Also these are the remote mounts from the host machine,
22-
## so keeping the label as nfs_t fits right. Package container-selinux by
23-
## default adds rules for nfs_t context which allows container workloads to work as well.
24-
## https://github.com/lima-vm/lima/pull/1965
25-
##########################################################################################
26-
mount -t virtiofs vz-rosetta /mnt/lima-rosetta -o context="system_u:object_r:nfs_t:s0"
27-
else
28-
mount -t virtiofs vz-rosetta /mnt/lima-rosetta
29-
fi
18+
# If rosetta is not registered in binfmt_misc, register it.
19+
[ -f "$binfmt_entry" ] || echo "$rosetta_binfmt" >/proc/sys/fs/binfmt_misc/register
3020

31-
if [ "$LIMA_CIDATA_ROSETTA_BINFMT" = "true" ]; then
32-
echo \
33-
':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' \
34-
>/proc/sys/fs/binfmt_misc/register
21+
# Create binfmt.d(5) configuration to prioritize rosetta even if qemu-user-static is installed on systemd based systems.
22+
# If the binfmt.d directory exists, consider systemd-binfmt.service(8) to be enabled and create the configuration file.
23+
[ ! -d "$(dirname "$binfmtd_conf")" ] || [ -f "$binfmtd_conf" ] || echo "$rosetta_binfmt" >"$binfmtd_conf"
24+
else
25+
# unregister rosetta from binfmt_misc if it exists
26+
[ ! -f "$binfmt_entry" ] || echo -1 | "$binfmt_entry"
27+
# remove binfmt.d(5) configuration if it exists
28+
[ ! -f "$binfmtd_conf" ] || rm "$binfmtd_conf"
3529
fi

pkg/cidata/cidata.TEMPLATE.d/user-data

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ package_upgrade: true
1111
package_reboot_if_required: true
1212
{{- end }}
1313

14-
{{- if or (eq .MountType "9p") (eq .MountType "virtiofs") }}
15-
{{- if .Mounts }}
14+
{{- if or .RosettaEnabled (or (eq .MountType "9p") (eq .MountType "virtiofs")) }}
1615
mounts:
17-
{{- range $m := $.Mounts}}
16+
{{- if .RosettaEnabled }}{{/* Mount the rosetta volume before systemd-binfmt.service(8) starts */}}
17+
- [vz-rosetta, /mnt/lima-rosetta, virtiofs]
18+
{{- end }}
19+
{{- if .Mounts }}
20+
{{- range $m := $.Mounts}}
1821
- [{{$m.Tag}}, {{$m.MountPoint}}, {{$m.Type}}, "{{$m.Options}}", "0", "0"]
22+
{{- end }}
1923
{{- end }}
2024
{{- end }}
21-
{{- end }}
2225

2326
{{- if .TimeZone }}
2427
timezone: {{.TimeZone}}

0 commit comments

Comments
 (0)