Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/cidata/cidata.TEMPLATE.d/boot/06-enable-mdns-on-systemd.sh
2678 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
# 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
37
38