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

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

@ -26,14 +26,27 @@ 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=$?; } || :
{
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" &
wait $! || :
sleep 1 & wait $!
sleep 1 &
wait $!
[ ! -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
# ######################################
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

View file

@ -17,7 +17,8 @@ rm -f "$QEMU_DIR/qemu.*"
touch "$QEMU_LOG"
_trap() {
func="$1" ; shift
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