Skip to content

Commit cc9fca7

Browse files
authored
Merge pull request #4272 from norio-nomura/enable-mDNS-in-guest-using-systemd-on-macOS-host
pkg/cidata: Enable mDNS in guest using `systemd`
2 parents 93d4235 + a59ced4 commit cc9fca7

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)