From 0f060fe11bf553e7b3dad1d03ccc84c349d04df2 Mon Sep 17 00:00:00 2001 From: Albrecht Lohofener Date: Sun, 28 Jan 2024 15:49:45 +0100 Subject: [PATCH] Add fallback Ethernet interfaces --- Dockerfile | 15 ++++++++------- docker-compose.yml | 5 ++++- src/entry.sh | 40 +++++++++++++++++++++++++--------------- src/reset.sh | 14 ++------------ 4 files changed, 39 insertions(+), 35 deletions(-) diff --git a/Dockerfile b/Dockerfile index e33243e..173b5af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,21 +35,22 @@ RUN apt-get update \ && sed -i 's/^worker_processes.*/worker_processes 1;/' /etc/nginx/nginx.conf \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* +# Get OpenWrt images +RUN mkdir /var/vm \ + && wget "https://archive.openwrt.org/releases/23.05.1/targets/armsr/armv8/openwrt-23.05.1-armsr-armv8-generic-ext4-rootfs.img.gz" \ + -O /var/vm/rootfs.img.gz \ + && wget "https://archive.openwrt.org/releases/23.05.1/targets/armsr/armv8/openwrt-23.05.1-armsr-armv8-generic-kernel.bin" \ + -O /var/vm/kernel.bin + COPY ./src /run/ COPY ./web /var/www/ RUN chmod +x /run/*.sh RUN mv /var/www/nginx.conf /etc/nginx/sites-enabled/web.conf -# Get OpenWrt images -RUN mkdir /var/vm -RUN wget "https://archive.openwrt.org/releases/23.05.1/targets/armsr/armv8/openwrt-23.05.1-armsr-armv8-generic-ext4-rootfs.img.gz" \ - -O /var/vm/rootfs.img.gz -RUN wget "https://archive.openwrt.org/releases/23.05.1/targets/armsr/armv8/openwrt-23.05.1-armsr-armv8-generic-kernel.bin" \ - -O /var/vm/kernel.bin - VOLUME /storage EXPOSE 8006 +EXPOSE 8000 ARG VERSION_ARG "0.0" RUN echo "$VERSION_ARG" > /run/version diff --git a/docker-compose.yml b/docker-compose.yml index 3b9d506..391f2f5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,8 +5,10 @@ services: image: "openwrt-docker-arm64" environment: DEBUG: "y" + # If WAN interface is not specified the Docker network + # In this case a port forwarding from port OpenWrt 80 to port 8000 is available WAN_IF: "enx00e04c0407a6" - LAN_IF: "enx00e04d68033f" + #LAN_IF: "enx00e04d68033f" devices: - /dev/kvm device_cgroup_rules: @@ -15,6 +17,7 @@ services: - NET_ADMIN ports: - 8006:8006 + - 8000:8000 # only when Docker WAN network is used stop_grace_period: 2m restart: on-failure pid: "host" diff --git a/src/entry.sh b/src/entry.sh index 780893c..b37e919 100755 --- a/src/entry.sh +++ b/src/entry.sh @@ -58,14 +58,14 @@ rm -f "$PAGE" info "Checking for KVM ..." KVM_ERR="" -CPU="-cpu cortex-a53" +CPU_ARGS="-cpu cortex-a53" if [ ! -e /dev/kvm ]; then KVM_ERR="(device file missing)" else if ! sh -c 'echo -n > /dev/kvm' &> /dev/null; then KVM_ERR="(no write access)" else - CPU="--enable-kvm -cpu host" + CPU_ARGS="--enable-kvm -cpu host" info "KVM detected" fi fi @@ -75,21 +75,33 @@ if [ -n "$KVM_ERR" ]; then fi # Attach physical PHY to container -# HOST_WAN_IF="enx00e04c0407a6" # WAN -# HOST_LAN_IF="enx00e04d68033f" # LAN -HOST_WAN_IF=$WAN_IF -HOST_LAN_IF=$LAN_IF -attach_eth_if $HOST_WAN_IF $HOST_WAN_IF qwan0 -attach_eth_if $HOST_LAN_IF $HOST_LAN_IF qlan0 +LAN_ARGS="" +if [[ -z "${LAN_IF}" ]]; then + LAN_ARGS="-device virtio-net,netdev=qlan0 -netdev user,id=qlan0,net=192.168.1.0/24" +else + HOST_LAN_IF=$LAN_IF + attach_eth_if $HOST_LAN_IF $HOST_LAN_IF qlan0 + exec 30<>/dev/tap$(cat /sys/class/net/qlan0/ifindex) + LAN_ARGS="-device virtio-net-pci,netdev=hostnet0 -netdev tap,fd=30,id=hostnet0" +fi +WAN_ARGS="" +if [[ -z "${WAN_IF}" ]]; then + WAN_ARGS="-device virtio-net,netdev=qwan0 -netdev user,id=qwan0,hostfwd=tcp::8000-:80" +else + HOST_WAN_IF=$WAN_IF + attach_eth_if $HOST_WAN_IF $HOST_WAN_IF qwan0 + exec 31<>/dev/tap$(cat /sys/class/net/qwan0/ifindex) + WAN_ARGS="-device virtio-net-pci,netdev=hostnet1 -netdev tap,fd=31,id=hostnet1" +fi info "Booting image using $VERS..." [[ "$DEBUG" == [Yy1]* ]] && set -x exec qemu-system-aarch64 -M virt \ --m 512 \ +-m 128 \ -nodefaults \ -$CPU -smp 2 \ + $CPU_ARGS -smp 2 \ -bios /usr/share/qemu-efi-aarch64/QEMU_EFI.fd \ -display vnc=:0,websocket=5700 \ -vga none -device ramfb \ @@ -97,10 +109,8 @@ $CPU -smp 2 \ -blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=/storage/rootfs.img \ -device virtio-blk-pci,drive=hd0 \ -device qemu-xhci -device usb-kbd \ --device virtio-net-pci,netdev=hostnet0 \ --netdev tap,fd=30,id=hostnet0 30<>/dev/tap$(cat /sys/class/net/qlan0/ifindex) \ --device virtio-net-pci,netdev=hostnet1 \ --netdev tap,fd=31,id=hostnet1 31<>/dev/tap$(cat /sys/class/net/qwan0/ifindex) + $LAN_ARGS \ + $WAN_ARGS -#- device virtio-net,netdev=qlan1 -netdev user,id=qlan1,net=192.168.1.0/24,hostfwd=tcp::8000-192.168.1.1:80 \ +# -device virtio-net,netdev=qlan1 -netdev user,id=qlan1,net=192.168.1.0/24,hostfwd=tcp::8000-192.168.1.1:80 \ # -blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=/var/vm/openwrt-armsr-armv8-generic-ext4-combined.img \ diff --git a/src/reset.sh b/src/reset.sh index 9328fd4..f83f613 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -16,19 +16,9 @@ echo # Docker environment variables -: "${BOOT:=""}" # URL of the ISO file -: "${KERNELIMG:=""}" # URL of the ISO file +: "${WAN_IF:=""}" # Physical WAN interface name +: "${LAN_IF:=""}" # Physical LAN interface name : "${DEBUG:="N"}" # Disable debugging -: "${ALLOCATE:=""}" # Preallocate diskspace -: "${ARGUMENTS:=""}" # Extra QEMU parameters -: "${CPU_CORES:="1"}" # Amount of CPU cores -: "${RAM_SIZE:="1G"}" # Maximum RAM amount -: "${DISK_SIZE:="16G"}" # Initial data disk size -: "${BOOT_INDEX:="10"}" # Boot index of CD drive -: "${DISPLAY:="web"}" # Display type -: "${KVM:="Y"}" -: "${CPU_FLAGS:=""}" -: "${CPU_MODEL:="host"}" # Helper variables