add static network interface configuration when network is bridge

This commit is contained in:
Gytis Stoškevičius 2025-03-31 15:51:22 +03:00
parent 3bd9ccb2b1
commit 7323ae399b
3 changed files with 95 additions and 30 deletions

47
src/entry.sh Normal file → Executable file
View file

@ -8,32 +8,45 @@ SUPPORT="https://github.com/dockur/windows"
cd /run cd /run
. reset.sh # Initialize system . reset.sh # Initialize system
. define.sh # Define versions . define.sh # Define versions
. mido.sh # Download code . mido.sh # Download code
. install.sh # Run installation . install.sh # Run installation
. disk.sh # Initialize disks . disk.sh # Initialize disks
. display.sh # Initialize graphics . display.sh # Initialize graphics
. network.sh # Initialize network . network.sh # Initialize network
. samba.sh # Configure samba . samba.sh # Configure samba
. boot.sh # Configure boot . boot.sh # Configure boot
. proc.sh # Initialize processor . proc.sh # Initialize processor
. power.sh # Configure shutdown . power.sh # Configure shutdown
. config.sh # Configure arguments . config.sh # Configure arguments
trap - ERR trap - ERR
version=$(qemu-system-x86_64 --version | head -n 1 | cut -d '(' -f 1 | awk '{ print $NF }') 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..." 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 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 & 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 $! || : wait $! || :
sleep 1 & wait $! sleep 1 &
wait $!
[ ! -f "$QEMU_END" ] && finish 0 [ ! -f "$QEMU_END" ] && finish 0

63
src/network.sh Normal file → Executable file
View file

@ -25,6 +25,64 @@ ADD_ERR="Please add the following setting to your container:"
# ###################################### # ######################################
# Functions # 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() { configureDHCP() {
@ -409,11 +467,6 @@ configureBridge() {
# error "Failed to set IP link!" && return 1 # error "Failed to set IP link!" && return 1
# fi # 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" NET_OPTS="-netdev tap,id=hostnet0,ifname=$VM_NET_TAP"
if [ -c /dev/vhost-net ]; then if [ -c /dev/vhost-net ]; then

View file

@ -17,8 +17,9 @@ rm -f "$QEMU_DIR/qemu.*"
touch "$QEMU_LOG" touch "$QEMU_LOG"
_trap() { _trap() {
func="$1" ; shift func="$1"
for sig ; do shift
for sig; do
trap "$func $sig" "$sig" trap "$func $sig" "$sig"
done done
} }
@ -35,8 +36,6 @@ boot() {
grep -Fq "BOOTMGR is missing" "$QEMU_PTY" && fail="y" grep -Fq "BOOTMGR is missing" "$QEMU_PTY" && fail="y"
fi fi
if [ -z "$fail" ]; then if [ -z "$fail" ]; then
info "Windows started succesfully, visit http://localhost:8006/ to view the screen..."
touch "$STORAGE/ready"
return 0 return 0
fi fi
fi fi
@ -129,7 +128,7 @@ terminal() {
if [ -n "$msg" ]; then if [ -n "$msg" ]; then
if [[ "${msg,,}" != "char"* || "$msg" != *"serial0)" ]]; then if [[ "${msg,,}" != "char"* || "$msg" != *"serial0)" ]]; then
echo "$msg" echo "$msg"
fi fi
@ -193,13 +192,13 @@ _graceful_shutdown() {
fi fi
# Send ACPI shutdown signal # 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 local cnt=0
while [ "$cnt" -lt "$QEMU_TIMEOUT" ]; do while [ "$cnt" -lt "$QEMU_TIMEOUT" ]; do
sleep 1 sleep 1
cnt=$((cnt+1)) cnt=$((cnt + 1))
! isAlive "$pid" && break ! isAlive "$pid" && break
# Workaround for zombie pid # Workaround for zombie pid
@ -208,7 +207,7 @@ _graceful_shutdown() {
info "Waiting for Windows to shutdown... ($cnt/$QEMU_TIMEOUT)" info "Waiting for Windows to shutdown... ($cnt/$QEMU_TIMEOUT)"
# Send ACPI shutdown signal # 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 done