Add vhost support in NAT mode

This commit is contained in:
Kroese 2023-05-06 16:30:30 +02:00 committed by GitHub
parent 98cb797574
commit f1a1b90460

View file

@ -25,7 +25,7 @@ configureDHCP() {
NETWORK=$(ip -o route | grep "${VM_NET_DEV}" | grep -v default | awk '{print $1}')
IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
[ "$DEBUG" = "Y" ] && set -x
[[ "${DEBUG}" == [Yy1]* ]] && set -x
{ ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge 2> /dev/null ; rc=$?; } || :
if (( rc != 0 )); then
@ -80,12 +80,6 @@ configureDHCP() {
echo "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21
fi
# Create /dev/vhost-net
if [ ! -c /dev/vhost-net ]; then
mknod /dev/vhost-net c 10 238
chmod 660 /dev/vhost-net
fi
{ exec 40>>/dev/vhost-net; rc=$?; } || :
if (( rc != 0 )); then
@ -99,7 +93,7 @@ configureDHCP() {
configureNAT () {
VM_NET_IP='20.20.20.21'
[ "$DEBUG" = "Y" ] && set -x
[[ "${DEBUG}" == [Yy1]* ]] && set -x
#Create bridge with static IP for the VM guest
@ -129,7 +123,7 @@ configureNAT () {
fi
{ set +x; } 2>/dev/null
[ "$DEBUG" = "Y" ] && echo
[[ "${DEBUG}" == [Yy1]* ]] && echo
#Check port forwarding flag
if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
@ -149,9 +143,12 @@ configureNAT () {
NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0"
{ exec 40>>/dev/vhost-net; rc=$?; } || :
(( rc == 0 )) && NET_OPTS="$NET_OPTS,vhost=on,vhostfd=40"
# Build DNS options from container /etc/resolv.conf
if [ "$DEBUG" = "Y" ]; then
if [[ "${DEBUG}" == [Yy1]* ]]; then
echo "/etc/resolv.conf:" && echo && cat /etc/resolv.conf && echo
fi
@ -178,7 +175,7 @@ configureNAT () {
DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//')
[ "$DEBUG" = "Y" ] && set -x
[[ "${DEBUG}" == [Yy1]* ]] && set -x
$DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
@ -198,13 +195,19 @@ fi
[ ! -c /dev/net/tun ] && echo "ERROR: TUN network interface not available..." && exit 85
# Create the necessary file structure for /dev/vhost-net
if [ ! -c /dev/vhost-net ]; then
mknod /dev/vhost-net c 10 238
chmod 660 /dev/vhost-net
fi
update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null
VM_NET_MAC="${VM_NET_MAC//-/:}"
GATEWAY=$(ip r | grep default | awk '{print $3}')
if [ "$DEBUG" = "Y" ]; then
if [[ "${DEBUG}" == [Yy1]* ]]; then
IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
echo "INFO: Container IP is ${IP} with gateway ${GATEWAY}" && echo
@ -213,12 +216,7 @@ if [ "$DEBUG" = "Y" ]; then
fi
if [ "$DHCP" != "Y" ]; then
# Configuration for static IP
configureNAT
else
if [[ "${DHCP}" == [Yy1]* ]]; then
if [[ "$GATEWAY" == "172."* ]]; then
echo -n "ERROR: You cannot enable DHCP while the container is "
@ -228,10 +226,15 @@ else
# Configuration for DHCP IP
configureDHCP
else
# Configuration for static IP
configureNAT
fi
NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"
[ "$DEBUG" = "Y" ] && echo && echo "Finished network setup.." && echo
[[ "${DEBUG}" == [Yy1]* ]] && echo && echo "Finished network setup.." && echo
return 0