Unverified Commit 0b764ff8 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

feat: Add IPv6 support (#697)

parent d269f559
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ services:
    image: qemux/qemu
    container_name: qemu
    environment:
      BOOT: "mint"
      BOOT: "alpine"
    devices:
      - /dev/kvm
      - /dev/net/tun
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ spec:
        image: qemux/qemu
        env:
        - name: BOOT
          value: "mint"
          value: "alpine"
        - name: DISK_SIZE
          value: "16G"
        ports:
+3 −3
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ services:
    image: qemux/qemu
    container_name: qemu
    environment:
      BOOT: "mint"
      BOOT: "alpine"
    devices:
      - /dev/kvm
      - /dev/net/tun
@@ -48,7 +48,7 @@ services:
Via Docker CLI:

```bash
docker run -it --rm --name qemu -e "BOOT=mint" -p 8006:8006 --device=/dev/kvm --device=/dev/net/tun --cap-add NET_ADMIN -v ${PWD:-.}/qemu:/storage --stop-timeout 120 qemux/qemu
docker run -it --rm --name qemu -e "BOOT=alpine" -p 8006:8006 --device=/dev/kvm --device=/dev/net/tun --cap-add NET_ADMIN -v ${PWD:-.}/qemu:/storage --stop-timeout 120 qemux/qemu
```

Via Kubernetes:
@@ -87,7 +87,7 @@ kubectl apply -f https://raw.githubusercontent.com/qemus/qemu/refs/heads/master/

  ```yaml
  environment:
    BOOT: "mint"
    BOOT: "alpine"
  ```

  Select from the values below:
+29 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ set -Eeuo pipefail
: "${VM_NET_MAC:="$MAC"}"
: "${VM_NET_HOST:="QEMU"}"
: "${VM_NET_IP:="20.20.20.21"}"
: "${VM_NET_IP6:="fdb4:ccc4:abd8::12"}"

: "${DNSMASQ_OPTS:=""}"
: "${DNSMASQ:="/usr/sbin/dnsmasq"}"
@@ -217,6 +218,12 @@ configureNAT() {
    error "Failed to add IP address!" && return 1
  fi

  if [ -n "$IP6" ]; then
    if ! ip -6 address add dev dockerbridge scope global "$VM_NET_IP6"; then
      error "Failed to add IPv6 address"
    fi
  fi

  while ! ip link set dockerbridge up; do
    info "Waiting for IP address to become available..."
    sleep 2
@@ -272,6 +279,19 @@ configureNAT() {
    iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill > /dev/null 2>&1 || true
  fi

  if [ -n "$IP6" ] && ip6tables -t nat -A POSTROUTING -o "$VM_NET_DEV" -j MASQUERADE; then

    # shellcheck disable=SC2086
    if ! ip6tables -t nat -A PREROUTING -i "$VM_NET_DEV" -d "$IP6" -p tcp${exclude} -j DNAT --to "$VM_NET_IP6"; then
      error "Failed to configure IPv6 tables!"
    fi

    if ! ip6tables -t nat -A PREROUTING -i "$VM_NET_DEV" -d "$IP6" -p udp  -j DNAT --to "$VM_NET_IP6"; then
      error "Failed to configure IPv6 tables!"
    fi

  fi

  NET_OPTS="-netdev tap,id=hostnet0,ifname=$VM_NET_TAP"

  if [ -c /dev/vhost-net ]; then
@@ -408,8 +428,14 @@ getInfo() {
    error "Invalid MAC address: '$VM_NET_MAC', should be 12 or 17 digits long!" && exit 28
  fi

  GATEWAY=$(ip route list dev "$VM_NET_DEV" | awk ' /^default/ {print $3}')
  IP=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
  GATEWAY=$(ip route list dev "$VM_NET_DEV" | awk ' /^default/ {print $3}' | head -n 1)
  IP=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/ | head -n 1)

  IP6=$(ifconfig -a | grep inet6)
  if [ -f /proc/net/if_inet6 ] && [ -n "$IP6" ]; then
    IP6=$(ip -6 addr show dev "$VM_NET_DEV" scope global up)
    [ -n "$IP6" ] && IP6=$(echo "$IP6" | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d' | head -n 1)
  fi

  return 0
}
+15 −0
Original line number Diff line number Diff line
@@ -306,6 +306,21 @@ fi
# Set password
echo "$user:{PLAIN}${PASS:-}" > /etc/nginx/.htpasswd

# Check if IPv6 is supported
ipv6=$(ifconfig -a | grep inet6)

if [ -f /proc/net/if_inet6 ] && [ -n "$ipv6" ]; then

  sed -i "s/listen 80;/listen [::]:80 ipv6only=off;/g" /etc/nginx/sites-enabled/web.conf
  sed -i "s/listen 8006 default_server;/listen [::]:8006 default_server ipv6only=off;/g" /etc/nginx/sites-enabled/web.conf

else

  sed -i "s/listen [::]:80 ipv6only=off;/listen 80;/g" /etc/nginx/sites-enabled/web.conf
  sed -i "s/listen [::]:8006 default_server ipv6only=off;/listen 8006 default_server;/g" /etc/nginx/sites-enabled/web.conf

fi

# Start webserver
cp -r /var/www/* /run/shm
html "Starting $APP for Docker..."