From 7323ae399b2dbd85ede41f1b61b95c78a69031e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gytis=20Sto=C5=A1kevi=C4=8Dius?= Date: Mon, 31 Mar 2025 15:51:22 +0300 Subject: [PATCH] add static network interface configuration when network is bridge --- src/entry.sh | 47 +++++++++++++++++++++++-------------- src/network.sh | 63 ++++++++++++++++++++++++++++++++++++++++++++++---- src/power.sh | 15 ++++++------ 3 files changed, 95 insertions(+), 30 deletions(-) mode change 100644 => 100755 src/entry.sh mode change 100644 => 100755 src/network.sh diff --git a/src/entry.sh b/src/entry.sh old mode 100644 new mode 100755 index b037cdc..18d8e49 --- a/src/entry.sh +++ b/src/entry.sh @@ -8,32 +8,45 @@ SUPPORT="https://github.com/dockur/windows" cd /run -. reset.sh # Initialize system -. define.sh # Define versions -. mido.sh # Download code -. install.sh # Run installation -. disk.sh # Initialize disks -. display.sh # Initialize graphics -. network.sh # Initialize network -. samba.sh # Configure samba -. boot.sh # Configure boot -. proc.sh # Initialize processor -. power.sh # Configure shutdown -. config.sh # Configure arguments +. reset.sh # Initialize system +. define.sh # Define versions +. mido.sh # Download code +. install.sh # Run installation +. disk.sh # Initialize disks +. display.sh # Initialize graphics +. network.sh # Initialize network +. samba.sh # Configure samba +. boot.sh # Configure boot +. proc.sh # Initialize processor +. power.sh # Configure shutdown +. config.sh # Configure arguments trap - ERR version=$(qemu-system-x86_64 --version | head -n 1 | cut -d '(' -f 1 | awk '{ print $NF }') info "Booting ${APP}${BOOT_DESC} using QEMU v$version..." -{ qemu-system-x86_64 ${ARGS:+ $ARGS} >"$QEMU_OUT" 2>"$QEMU_LOG"; rc=$?; } || : -(( rc != 0 )) && error "$(<"$QEMU_LOG")" && exit 15 +{ + qemu-system-x86_64 ${ARGS:+ $ARGS} >"$QEMU_OUT" 2>"$QEMU_LOG" + rc=$? +} || : +((rc != 0)) && error "$(<"$QEMU_LOG")" && exit 15 terminal -( sleep 30; boot ) & +( + sleep 30 + boot + configure_guest_network_interface + info "Windows started succesfully, you can now connect using RDP" + if [[ "${NETWORK,,}" != "bridge"* ]]; then + info "or visit http://localhost:8006/ to view the screen..." + fi + touch "$STORAGE/ready" +) & tail -fn +0 "$QEMU_LOG" 2>/dev/null & -cat "$QEMU_TERM" 2> /dev/null | tee "$QEMU_PTY" & +cat "$QEMU_TERM" 2>/dev/null | tee "$QEMU_PTY" & wait $! || : -sleep 1 & wait $! +sleep 1 & +wait $! [ ! -f "$QEMU_END" ] && finish 0 diff --git a/src/network.sh b/src/network.sh old mode 100644 new mode 100755 index da15ef0..13835fb --- a/src/network.sh +++ b/src/network.sh @@ -25,6 +25,64 @@ ADD_ERR="Please add the following setting to your container:" # ###################################### # Functions # ###################################### +find_free_ip() { + local current_ip="$1" + local mask="$2" + + # Get network prefix + IFS='.' read -r i1 i2 i3 i4 <<<"$current_ip" + IFS='.' read -r m1 m2 m3 m4 <<<"$(ip -o -f inet addr show | awk '/scope global/ {print $4}' | cut -d'/' -f2)" + + network_ip=$((i1 & m1)).$((i2 & m2)).$((i3 & m3)).0 + base_ip="$i1.$i2.$i3" + + # Iterate over available IPs + for i in {2..254}; do + new_ip="$base_ip.$i" + if [[ "$new_ip" != "$current_ip" ]] && ! ping -c 1 -W 1 "$new_ip" &>/dev/null; then + echo "$new_ip" + return + fi + done + + echo "No free IP found" +} + +configure_guest_network_interface() { + if [[ "${NETWORK,,}" == "bridge"* ]]; then + + HOST_INTERFACE="dockerbridge" + CURRENT_IP=$(ip addr show $HOST_INTERFACE | grep -oP 'inet \K[\d.]+') + MASK="$(ip -4 addr show $HOST_INTERFACE | awk '/inet / {print $2}' | cut -d'/' -f2)" + + if [ -z "$CURRENT_IP" ]; then + echo "Error: Unable to retrieve the current IP address of $HOST_INTERFACE." + exit 1 + fi + + echo "Current Host IP: $CURRENT_IP" + + IFS='.' read -r -a ip_parts <<<"$CURRENT_IP" + NEW_HOST_IP=$(find_free_ip "$CURRENT_IP" "$MASK") + GW="${ip_parts[0]}.${ip_parts[1]}.${ip_parts[2]}.1" + + echo "New Host IP: $NEW_HOST_IP" + + ip addr del $CURRENT_IP/$MASK dev $HOST_INTERFACE + ip addr add $NEW_HOST_IP/$MASK dev $HOST_INTERFACE + + ip link set $HOST_INTERFACE down + ip link set $HOST_INTERFACE up + + route add default gw $GW + + echo -e '{"execute": "guest-exec", "arguments": {"path": "C:\\\\Windows\\\\System32\\\\netsh.exe", "arg": ["interface", "ipv4", "set", "address", "name=\\"Ethernet\\"", "static", "'"$CURRENT_IP"'", "255.255.255.0", "'"$GW"'"]}}' | nc -U /tmp/qga.sock -w 5 + echo -e '{"execute": "guest-exec", "arguments": {"path": "C:\\\\Windows\\\\System32\\\\netsh.exe", "arg": ["interface", "ipv4", "add", "dnsservers", "name=\\"Ethernet\\"", "address=\\"'$GW'\\"", "index=\\"1\\""]}}' | nc -U /tmp/qga.sock -w 5 + + fi + + return 0 +} configureDHCP() { @@ -409,11 +467,6 @@ configureBridge() { # error "Failed to set IP link!" && return 1 # fi - # add initial default route as well - if ! ip route add default dev dockerbridge via ${VM_NET_IP%.*}.1; then - error "Failed to setup default route" && return 10 - fi - NET_OPTS="-netdev tap,id=hostnet0,ifname=$VM_NET_TAP" if [ -c /dev/vhost-net ]; then diff --git a/src/power.sh b/src/power.sh index 3e36701..b619f5f 100644 --- a/src/power.sh +++ b/src/power.sh @@ -17,8 +17,9 @@ rm -f "$QEMU_DIR/qemu.*" touch "$QEMU_LOG" _trap() { - func="$1" ; shift - for sig ; do + func="$1" + shift + for sig; do trap "$func $sig" "$sig" done } @@ -35,8 +36,6 @@ boot() { grep -Fq "BOOTMGR is missing" "$QEMU_PTY" && fail="y" fi if [ -z "$fail" ]; then - info "Windows started succesfully, visit http://localhost:8006/ to view the screen..." - touch "$STORAGE/ready" return 0 fi fi @@ -129,7 +128,7 @@ terminal() { if [ -n "$msg" ]; then - if [[ "${msg,,}" != "char"* || "$msg" != *"serial0)" ]]; then + if [[ "${msg,,}" != "char"* || "$msg" != *"serial0)" ]]; then echo "$msg" fi @@ -193,13 +192,13 @@ _graceful_shutdown() { fi # Send ACPI shutdown signal - echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_PORT}" > /dev/null + echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_PORT}" >/dev/null local cnt=0 while [ "$cnt" -lt "$QEMU_TIMEOUT" ]; do sleep 1 - cnt=$((cnt+1)) + cnt=$((cnt + 1)) ! isAlive "$pid" && break # Workaround for zombie pid @@ -208,7 +207,7 @@ _graceful_shutdown() { info "Waiting for Windows to shutdown... ($cnt/$QEMU_TIMEOUT)" # Send ACPI shutdown signal - echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_PORT}" > /dev/null + echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_PORT}" >/dev/null done