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/09-host-dns-setup.sh
2658 views
1
#!/bin/sh
2
3
# SPDX-FileCopyrightText: Copyright The Lima Authors
4
# SPDX-License-Identifier: Apache-2.0
5
6
set -eux
7
8
readonly chain=LIMADNS
9
10
chain_exists() {
11
iptables --table nat -n --list "${chain}" >/dev/null 2>&1
12
}
13
14
# Wait until iptables has been installed; 35-configure-packages.sh will call this script again
15
if command -v iptables >/dev/null 2>&1; then
16
if ! chain_exists; then
17
iptables --table nat --new-chain ${chain}
18
iptables --table nat --insert PREROUTING 1 --jump "${chain}"
19
iptables --table nat --insert OUTPUT 1 --jump "${chain}"
20
fi
21
22
# Remove old rules
23
iptables --table nat --flush ${chain}
24
# Add rules for the existing ip:port
25
if [ -n "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" ] && [ "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" -ne 0 ]; then
26
iptables --table nat --append "${chain}" --destination "${LIMA_CIDATA_SLIRP_DNS}" --protocol udp --dport 53 --jump DNAT \
27
--to-destination "${LIMA_CIDATA_SLIRP_GATEWAY}:${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}"
28
fi
29
if [ -n "${LIMA_CIDATA_TCP_DNS_LOCAL_PORT}" ] && [ "${LIMA_CIDATA_TCP_DNS_LOCAL_PORT}" -ne 0 ]; then
30
iptables --table nat --append "${chain}" --destination "${LIMA_CIDATA_SLIRP_DNS}" --protocol tcp --dport 53 --jump DNAT \
31
--to-destination "${LIMA_CIDATA_SLIRP_GATEWAY}:${LIMA_CIDATA_TCP_DNS_LOCAL_PORT}"
32
fi
33
fi
34
35