Skip to content

Commit 4584ef8

Browse files
committed
pkg/driver/vz: Enable mDNS in guest using systemd on macOS host
This change allows resolving `<guest's hostname>.local` to guest ip by mDNS on macOS host. e.g. ```console $ limactl start --name shared-and-vznat --tty=false --network=lima:shared,vzNAT --containerd=none --log-level error ... $ dns-sd -q lima-shared-and-vznat.local DATE: ---Sat 01 Nov 2025--- 15:31:14.555 ...STARTING... Timestamp A/R Flags IF Name Type Class Rdata 15:31:14.555 Add 40000003 27 lima-shared-and-vznat.local. Addr IN 192.168.64.2 15:31:14.555 Add 40000002 31 lima-shared-and-vznat.local. Addr IN 192.168.105.3 ^C $ ping -c 1 lima-shared-and-vznat.local PING lima-shared-and-vznat.local (192.168.64.2): 56 data bytes 64 bytes from 192.168.64.2: icmp_seq=0 ttl=64 time=0.568 ms --- lima-shared-and-vznat.local ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 0.568/0.568/0.568/0.000 ms ``` Signed-off-by: Norio Nomura <[email protected]>
1 parent e14ee7b commit 4584ef8

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
# Do nothing on OpenRC systems (e.g., Alpine Linux)
9+
if [[ -f /sbin/openrc-run ]]; then
10+
exit 0
11+
fi
12+
13+
# It depends on systemd-resolved
14+
command -v systemctl >/dev/null 2>&1 || exit 0
15+
command -v resolvectl >/dev/null 2>&1 || exit 0
16+
17+
# Configure systemd-resolved to enable mDNS resolution globally
18+
enable_mdns_conf_path=/etc/systemd/resolved.conf.d/00-lima-enable-mdns.conf
19+
enable_mdns_conf_content="[Resolve]
20+
MulticastDNS=yes
21+
"
22+
# Create /etc/systemd/resolved.conf.d/00-lima-enable-mdns.conf if its content is different
23+
if ! diff -q <(echo "${enable_mdns_conf_content}") "${enable_mdns_conf_path}" >/dev/null 2>&1; then
24+
mkdir -p "$(dirname "${enable_mdns_conf_path}")"
25+
echo "${enable_mdns_conf_content}" >"${enable_mdns_conf_path}"
26+
systemctl daemon-reload
27+
systemctl restart systemd-resolved.service
28+
fi
29+
30+
# On Ubuntu, systemd.network's configuration won't work.
31+
# See: https://unix.stackexchange.com/a/652582
32+
# So we need to enable mDNS per-link using resolvectl.
33+
for iface in $(resolvectl status | sed -n -E 's/^Link +[0-9]+ \(([^)]+)\)/\1/p'); do
34+
# This setting is volatile and will be lost on reboot, so we need to set it every time
35+
resolvectl mdns "${iface}" yes
36+
done

0 commit comments

Comments
 (0)