#!/bin/bash # Tear down the lab network simulation. # # By default this destroys VMs and networks but KEEPS the downloaded base # image, so the next bring-up is fast. Pass --purge to remove that too. # # Usage: ./labsim-down.sh [--purge] [vlan-id ...] set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "$SCRIPT_DIR/lib.sh" source "$SCRIPT_DIR/ovs.sh" PURGE=false ARGS=() for a in "$@"; do case "$a" in --purge) PURGE=true ;; *) ARGS+=("$a") ;; esac done selected_vlans "${ARGS[@]+"${ARGS[@]}"}" for entry in "${SELECTED[@]}"; do IFS=: read -r vid name prefix _r <<<"$entry" vm="$(vm_name "$vid" "$name")" if virsh_q dominfo "$vm" >/dev/null 2>&1; then log "destroying VM $vm" virsh_q destroy "$vm" >/dev/null 2>&1 || true virsh_q undefine "$vm" --nvram >/dev/null 2>&1 || virsh_q undefine "$vm" >/dev/null 2>&1 || true fi sudo rm -f "$IMG_DIR/${vm}.qcow2" "$IMG_DIR/${vm}-seed.iso" done # Legacy per-VLAN Linux-bridge networks from before the OVS migration. If # these survive they keep a duplicate .2/24 on a dead bridge, and the # kernel may prefer that route over the OVS host leg — which looks exactly # like "the VM is unreachable" while ping -I hostvN works fine. for entry in "${SELECTED[@]}"; do IFS=: read -r vid _n _p _r <<<"$entry" legacy="labsim-vlan${vid}" if virsh_q net-info "$legacy" >/dev/null 2>&1; then log "removing legacy network $legacy" virsh_q net-destroy "$legacy" >/dev/null 2>&1 || true virsh_q net-undefine "$legacy" >/dev/null 2>&1 || true fi done log "removing OVS fabric" ovs_down if [ "$PURGE" = true ]; then log "purging base image $BASE_IMAGE" sudo rm -f "$BASE_IMAGE" sudo rmdir "$IMG_DIR" 2>/dev/null || true fi log "environment is DOWN"