Skip to content

Commit

Permalink
Delete network on shutdown
Browse files Browse the repository at this point in the history
Delete network on shutdown
  • Loading branch information
kroese committed Jul 4, 2023
2 parents ceb144e + 76e1772 commit 83714a0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion run/disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if [ -f "${DATA}" ]; then

REQ=$((DATA_SIZE-OLD_SIZE))

# Check free diskspace
# Check free diskspace
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)

if (( REQ > SPACE )); then
Expand Down
36 changes: 33 additions & 3 deletions run/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ configureDHCP() {
error "and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 16
fi

ip link set "${VM_NET_TAP}" up
while ! ip link set "${VM_NET_TAP}" up; do
info "Waiting for address to become available..."
sleep 2
done

TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex)
TAP_PATH="/dev/tap${TAP_NR}"
Expand Down Expand Up @@ -127,11 +130,20 @@ configureNAT () {
fi

ip address add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
ip link set dockerbridge up

while ! ip link set dockerbridge up; do
info "Waiting for address to become available..."
sleep 2
done

# QEMU Works with taps, set tap to the bridge created
ip tuntap add dev "${VM_NET_TAP}" mode tap
ip link set "${VM_NET_TAP}" up promisc on

while ! ip link set "${VM_NET_TAP}" up promisc on; do
info "Waiting for tap to become available..."
sleep 2
done

ip link set dev "${VM_NET_TAP}" master dockerbridge

# Add internet connection to the VM
Expand Down Expand Up @@ -165,6 +177,24 @@ configureNAT () {
return 0
}

closeNetwork () {

if [[ "${DHCP}" == [Yy1]* ]]; then

ip link set "${VM_NET_TAP}" down || true
ip link delete "${VM_NET_TAP}" || true

else

ip link set "${VM_NET_TAP}" down promisc off || true
ip link delete "${VM_NET_TAP}" || true

ip link set dockerbridge down || true
ip link delete dockerbridge || true

fi
}

# ######################################
# Configure Network
# ######################################
Expand Down
2 changes: 2 additions & 0 deletions run/power.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ _graceful_shutdown() {
echo && echo "❯ Quitting..."
echo 'quit' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" >/dev/null 2>&1 || true

closeNetwork

return
}

Expand Down
2 changes: 1 addition & 1 deletion run/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ set -m
)
set +m

if (( KERNEL > 5 )) || ( (( KERNEL == 5 )) && (( MINOR > 2 )) ); then
if (( KERNEL > 5 )) || ( (( KERNEL == 5 )) && (( MINOR > 10 )) ); then
pidwait -F "${_QEMU_PID}" & wait $!
else
tail --pid "$(cat "${_QEMU_PID}")" --follow /dev/null & wait $!
Expand Down

0 comments on commit 83714a0

Please sign in to comment.