diff --git a/run/network.sh b/run/network.sh index 4aa6b31..465f79b 100644 --- a/run/network.sh +++ b/run/network.sh @@ -22,69 +22,43 @@ set -Eeuo pipefail configureDHCP() { - VM_NET_VLAN="${VM_NET_TAP}_vlan" - GATEWAY=$(ip r | grep default | awk '{print $3}') - 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}" == [Yy1]* ]] && set -x - - # Create a macvlan network to allow for communication between the host and the VM guest - { ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge ; rc=$?; } || : - - if (( rc != 0 )); then - echo "ERROR: Cannot create macvlan interface. Please make sure the network type is 'macvlan' and not 'ipvlan'," - echo "ERROR: and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 15 - fi - - ip address add "${IP}" dev "${VM_NET_VLAN}" - ip link set dev "${VM_NET_VLAN}" up - - ip route flush dev "${VM_NET_VLAN}" - ip route del "${NETWORK}" dev "${VM_NET_DEV}" - ip route add "${NETWORK}" dev "${VM_NET_VLAN}" metric 0 - # Create a macvtap network for the VM guest - { ip link add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge ; rc=$?; } || : + { ip link add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge ; rc=$?; } || : + if (( rc != 0 )); then - echo "ERROR: Capability NET_ADMIN has not been set most likely. Please add the " - echo "ERROR: following docker setting to your container: --cap-add NET_ADMIN" && exit 16 + error "Cannot create macvtap interface. Please make sure the network type is 'macvlan' and not 'ipvlan'," + error "and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 16 fi ip link set "${VM_NET_TAP}" up - { set +x; } 2>/dev/null - TAP_NR=$(>"$TAP_PATH"; rc=$?; } || : if (( rc != 0 )); then - echo "ERROR: Cannot create TAP interface ($rc). Please add the following docker settings to your " - echo "ERROR: container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21 + error "Cannot create TAP interface ($rc). Please add the following docker settings to your " + error "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21 fi { exec 40>>/dev/vhost-net; rc=$?; } || : if (( rc != 0 )); then - echo "ERROR: VHOST can not be found ($rc). Please add the following " - echo "ERROR: docker setting to your container: --device=/dev/vhost-net" && exit 22 + error "VHOST can not be found ($rc). Please add the following " + error "docker setting to your container: --device=/dev/vhost-net" && exit 22 fi NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30" @@ -92,15 +66,16 @@ configureDHCP() { configureNAT () { + # Create a bridge with a static IP for the VM guest + VM_NET_IP='20.20.20.21' [[ "${DEBUG}" == [Yy1]* ]] && set -x - # Create bridge with static IP for the VM guest { ip link add dev dockerbridge type bridge ; rc=$?; } || : if (( rc != 0 )); then - echo "ERROR: Capability NET_ADMIN has not been set most likely. Please add the " - echo "ERROR: following docker setting to your container: --cap-add NET_ADMIN" && exit 23 + error "Capability NET_ADMIN has not been set most likely. Please add the " + error "following docker setting to your container: --cap-add NET_ADMIN" && exit 23 fi ip address add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge @@ -128,7 +103,7 @@ configureNAT () { if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then { sysctl -w net.ipv4.ip_forward=1 ; rc=$?; } || : if (( rc != 0 )); then - echo "ERROR: Please add the following docker setting to your container: --sysctl net.ipv4.ip_forward=1" && exit 24 + error "Please add the following docker setting to your container: --sysctl net.ipv4.ip_forward=1" && exit 24 fi fi @@ -178,6 +153,8 @@ configureNAT () { $DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS} { set +x; } 2>/dev/null + + [[ "${DEBUG}" == [Yy1]* ]] && echo } # ###################################### @@ -191,7 +168,7 @@ if [ ! -c /dev/net/tun ]; then chmod 666 /dev/net/tun fi -[ ! -c /dev/net/tun ] && echo "ERROR: TUN network interface not available..." && exit 85 +[ ! -c /dev/net/tun ] && error "TUN network interface not available..." && exit 85 # Create the necessary file structure for /dev/vhost-net if [ ! -c /dev/vhost-net ]; then @@ -208,15 +185,15 @@ GATEWAY=$(ip r | grep default | awk '{print $3}') 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 + info "Container IP is ${IP} with gateway ${GATEWAY}" && echo fi if [[ "${DHCP}" == [Yy1]* ]]; then if [[ "$GATEWAY" == "172."* ]]; then - echo -n "ERROR: You cannot enable DHCP while the container is " - echo "in a bridge network, only on a macvlan network!" && exit 86 + error "You cannot enable DHCP while the container is " + error "in a bridge network, only on a macvlan network!" && exit 86 fi # Configuration for DHCP IP @@ -231,6 +208,4 @@ fi NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0" -[[ "${DEBUG}" == [Yy1]* ]] && echo && echo "Finished network setup.." && echo - return 0