mirror of
https://github.com/vdsm/virtual-dsm.git
synced 2025-02-24 13:30:02 +08:00
configureDNS
This commit is contained in:
parent
9b13bc46d8
commit
eb0f91ad3b
@ -66,6 +66,52 @@ configureDHCP() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configureDNS () {
|
||||||
|
|
||||||
|
# dnsmasq configuration:
|
||||||
|
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-range=$VM_NET_IP,$VM_NET_IP --dhcp-host=$VM_NET_MAC,,$VM_NET_IP,$VM_NET_HOST,infinite --dhcp-option=option:netmask,255.255.255.0"
|
||||||
|
|
||||||
|
# Create lease file for faster resolve
|
||||||
|
echo "0 $VM_NET_MAC $VM_NET_IP $VM_NET_HOST 01:${VM_NET_MAC}" > /var/lib/misc/dnsmasq.leases
|
||||||
|
chmod 644 /var/lib/misc/dnsmasq.leases
|
||||||
|
|
||||||
|
# Build DNS options from container /etc/resolv.conf
|
||||||
|
|
||||||
|
if [[ "${DEBUG}" == [Yy1]* ]]; then
|
||||||
|
echo "/etc/resolv.conf:" && echo && cat /etc/resolv.conf && echo
|
||||||
|
fi
|
||||||
|
|
||||||
|
mapfile -t nameservers < <( { grep '^nameserver' /etc/resolv.conf || true; } | sed 's/\t/ /g' | sed 's/nameserver //' | sed 's/ //g')
|
||||||
|
searchdomains=$( { grep '^search' /etc/resolv.conf || true; } | sed 's/\t/ /g' | sed 's/search //' | sed 's/#.*//' | sed 's/\s*$//g' | sed 's/ /,/g')
|
||||||
|
domainname=$(echo "$searchdomains" | awk -F"," '{print $1}')
|
||||||
|
|
||||||
|
for nameserver in "${nameservers[@]}"; do
|
||||||
|
nameserver=$(echo "$nameserver" | sed 's/#.*//' )
|
||||||
|
if ! [[ "$nameserver" =~ .*:.* ]]; then
|
||||||
|
[[ -z "$DNS_SERVERS" ]] && DNS_SERVERS="$nameserver" || DNS_SERVERS="$DNS_SERVERS,$nameserver"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
[[ -z "$DNS_SERVERS" ]] && DNS_SERVERS="1.1.1.1"
|
||||||
|
|
||||||
|
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:dns-server,$DNS_SERVERS --dhcp-option=option:router,${VM_NET_IP%.*}.1"
|
||||||
|
|
||||||
|
if [ -n "$searchdomains" ] && [ "$searchdomains" != "." ]; then
|
||||||
|
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-search,$searchdomains --dhcp-option=option:domain-name,$domainname"
|
||||||
|
else
|
||||||
|
[[ -z $(hostname -d) ]] || DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$(hostname -d)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//')
|
||||||
|
|
||||||
|
[[ "${DEBUG}" == [Yy1]* ]] && set -x
|
||||||
|
$DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
|
||||||
|
{ set +x; } 2>/dev/null
|
||||||
|
[[ "${DEBUG}" == [Yy1]* ]] && echo
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
configureNAT () {
|
configureNAT () {
|
||||||
|
|
||||||
# Create a bridge with a static IP for the VM guest
|
# Create a bridge with a static IP for the VM guest
|
||||||
@ -119,55 +165,6 @@ configureNAT () {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
configureDNS () {
|
|
||||||
|
|
||||||
# dnsmasq configuration:
|
|
||||||
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-range=$VM_NET_IP,$VM_NET_IP --dhcp-host=$VM_NET_MAC,,$VM_NET_IP,$VM_NET_HOST,infinite --dhcp-option=option:netmask,255.255.255.0"
|
|
||||||
|
|
||||||
# Create lease file for faster resolve
|
|
||||||
echo "0 $VM_NET_MAC $VM_NET_IP $VM_NET_HOST 01:${VM_NET_MAC}" > /var/lib/misc/dnsmasq.leases
|
|
||||||
chmod 644 /var/lib/misc/dnsmasq.leases
|
|
||||||
|
|
||||||
# Build DNS options from container /etc/resolv.conf
|
|
||||||
|
|
||||||
if [[ "${DEBUG}" == [Yy1]* ]]; then
|
|
||||||
echo "/etc/resolv.conf:" && echo && cat /etc/resolv.conf && echo
|
|
||||||
fi
|
|
||||||
|
|
||||||
mapfile -t nameservers < <( { grep '^nameserver' /etc/resolv.conf || true; } | sed 's/\t/ /g' | sed 's/nameserver //' | sed 's/ //g')
|
|
||||||
searchdomains=$( { grep '^search' /etc/resolv.conf || true; } | sed 's/\t/ /g' | sed 's/search //' | sed 's/#.*//' | sed 's/\s*$//g' | sed 's/ /,/g')
|
|
||||||
domainname=$(echo "$searchdomains" | awk -F"," '{print $1}')
|
|
||||||
|
|
||||||
for nameserver in "${nameservers[@]}"; do
|
|
||||||
nameserver=$(echo "$nameserver" | sed 's/#.*//' )
|
|
||||||
if ! [[ "$nameserver" =~ .*:.* ]]; then
|
|
||||||
[[ -z "$DNS_SERVERS" ]] && DNS_SERVERS="$nameserver" || DNS_SERVERS="$DNS_SERVERS,$nameserver"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
[[ -z "$DNS_SERVERS" ]] && DNS_SERVERS="1.1.1.1"
|
|
||||||
|
|
||||||
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:dns-server,$DNS_SERVERS --dhcp-option=option:router,${VM_NET_IP%.*}.1"
|
|
||||||
|
|
||||||
if [ -n "$searchdomains" ] && [ "$searchdomains" != "." ]; then
|
|
||||||
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-search,$searchdomains --dhcp-option=option:domain-name,$domainname"
|
|
||||||
else
|
|
||||||
[[ -z $(hostname -d) ]] || DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$(hostname -d)"
|
|
||||||
fi
|
|
||||||
|
|
||||||
DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//')
|
|
||||||
|
|
||||||
[[ "${DEBUG}" == [Yy1]* ]] && set -x
|
|
||||||
|
|
||||||
$DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
|
|
||||||
|
|
||||||
{ set +x; } 2>/dev/null
|
|
||||||
|
|
||||||
[[ "${DEBUG}" == [Yy1]* ]] && echo
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
# ######################################
|
# ######################################
|
||||||
# Configure Network
|
# Configure Network
|
||||||
# ######################################
|
# ######################################
|
||||||
|
Loading…
x
Reference in New Issue
Block a user