Unverified Commit 5a828006 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

macvtap

parent 402856d4
Loading
Loading
Loading
Loading
+34 −34
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ set -eu
# Docker environment variabeles

: ${VM_NET_TAP:='dsm'}
: ${VM_NET_DEV:='eth0'}
: ${VM_NET_HOST:='VirtualDSM'}
: ${VM_NET_MAC:='02:11:32:AA:BB:CC'}

@@ -19,41 +20,28 @@ set -eu

configureDHCP() {

  # Create /dev/vhost-net
  if [ ! -c /dev/vhost-net ]; then
    mknod /dev/vhost-net c 10 238
    chmod 660 /dev/vhost-net
  fi

  if [ ! -c /dev/vhost-net ]; then
    echo -n "Error: VHOST interface not available. Please add the following "
    echo "docker variable to your container: --device=/dev/vhost-net" && exit 85
  fi

  # Create macvlan to enable host <> guest communication

  VM_NET_VLAN="vlan"
  GATEWAY=$(ip r | grep default | awk '{print $3}')
  IP=$(ip address show dev eth0 | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
  NETWORK=$(ip -o route | grep eth0 | grep -v default | awk '{print $1}')
  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/)

  ip l add link eth0 macvlan0 type macvlan mode bridge
  ip l add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge

  ip address add "${IP}" dev macvlan0
  ip link set dev macvlan0 up
  ip address add "${IP}" dev "${VM_NET_VLAN}"
  ip link set dev "${VM_NET_VLAN}" up

  ip route flush dev eth0
  ip route flush dev macvlan0
  ip route flush dev "${VM_NET_DEV}"
  ip route flush dev "${VM_NET_VLAN}"

  ip route add $NETWORK dev macvlan0 metric 0
  ip route add $NETWORK dev "${VM_NET_VLAN}" metric 0
  ip route add default via "${GATEWAY}"

  echo "Info: Retrieving IP via DHCP using MAC ${VM_NET_MAC}..."

  # Create macvtap
  ip l add link eth0 name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge || true
  ip l add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge || true
  ip l set "${VM_NET_TAP}" up

  ip a flush eth0
  ip a flush "${VM_NET_DEV}"
  ip a flush "${VM_NET_TAP}"

  DHCP_IP=$( dhclient -v "${VM_NET_TAP}" 2>&1 | grep ^bound | cut -d' ' -f3 )
@@ -64,11 +52,19 @@ configureDHCP() {
    echo "ERROR: Cannot retrieve IP from DHCP using MAC ${VM_NET_MAC}" && exit 16
  fi

  # Store IP for Docker healthcheck
  echo "${DHCP_IP}" > "/var/dsm.ip"

  ip a flush "${VM_NET_TAP}"

  # Create /dev/vhost-net
  if [ ! -c /dev/vhost-net ]; then
    mknod /dev/vhost-net c 10 238
    chmod 660 /dev/vhost-net
  fi

  if [ ! -c /dev/vhost-net ]; then
    echo -n "Error: VHOST interface not available. Please add the following "
    echo "docker variable to your container: --device=/dev/vhost-net" && exit 85
  fi

  TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex)
  TAP_PATH="/dev/tap${TAP_NR}"

@@ -97,6 +93,9 @@ configureDHCP() {
    echo "variable to your container: --device=/dev/vhost-net" && exit 22
  fi

  # Store IP for Docker healthcheck
  echo "${DHCP_IP}" > "/var/dsm.ip"

  NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30"
}

@@ -104,22 +103,20 @@ configureNAT () {

  VM_NET_IP='20.20.20.21'

  # Store IP for Docker healthcheck
  echo "${VM_NET_IP}" > "/var/dsm.ip"

  #Create bridge with static IP for the VM guest
  brctl addbr dockerbridge
  ip addr add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
  ip link set dockerbridge up

  #QEMU Works with taps, set tap to the bridge created
  ip tuntap add dev "${VM_NET_TAP}" mode tap
  ip link set "${VM_NET_TAP}" up promisc on
  brctl addif dockerbridge "${VM_NET_TAP}"

  #Add internet connection to the VM
  iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  iptables -t nat -A PREROUTING -i eth0 -p tcp  -j DNAT --to $VM_NET_IP
  iptables -t nat -A PREROUTING -i eth0 -p udp  -j DNAT --to $VM_NET_IP
  iptables -t nat -A POSTROUTING -o "${VM_NET_DEV}" -j MASQUERADE
  iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p tcp  -j DNAT --to $VM_NET_IP
  iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p udp  -j DNAT --to $VM_NET_IP

  # Hack for guest VMs complaining about "bad udp checksums in 5 packets"
  iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill || true
@@ -134,6 +131,9 @@ configureNAT () {
  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

  # Store IP for Docker healthcheck
  echo "${VM_NET_IP}" > "/var/dsm.ip"

  NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0"

  # Build DNS options from container /etc/resolv.conf
@@ -182,7 +182,7 @@ GATEWAY=$(ip r | grep default | awk '{print $3}')

if [ "$DEBUG" = "Y" ]; then

  IP=$(ip address show dev eth0 | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
  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}"
  ifconfig
  ip route