71 lines
2.2 KiB
Bash
71 lines
2.2 KiB
Bash
|
|
#!/bin/bash
|
||
|
|
# Bring up the lab network simulation: one isolated libvirt network per VLAN,
|
||
|
|
# each with a single tiny Alpine VM offering SSH + a hello-world HTTP page.
|
||
|
|
#
|
||
|
|
# Idempotent: re-running only creates what is missing. Safe to run repeatedly.
|
||
|
|
#
|
||
|
|
# Usage: ./labsim-up.sh [vlan-id ...] (default: every VLAN in vlans.conf)
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||
|
|
source "$SCRIPT_DIR/lib.sh"
|
||
|
|
source "$SCRIPT_DIR/ovs.sh"
|
||
|
|
|
||
|
|
require_tools
|
||
|
|
[ -f "$BASE_IMAGE" ] || die "base image missing: $BASE_IMAGE (see README)"
|
||
|
|
|
||
|
|
SSH_PUB="$(find_ssh_pubkey)"
|
||
|
|
log "Using SSH key: ${SSH_PUB%% *} ...${SSH_PUB##* }"
|
||
|
|
|
||
|
|
selected_vlans "$@"
|
||
|
|
|
||
|
|
# --- switch fabric ------------------------------------------------------
|
||
|
|
log "bringing up OVS fabric ($OVS_BR) with host legs per VLAN"
|
||
|
|
ovs_up
|
||
|
|
|
||
|
|
# --- VMs ------------------------------------------------------------------
|
||
|
|
for entry in "${SELECTED[@]}"; do
|
||
|
|
IFS=: read -r vid name prefix real <<<"$entry"
|
||
|
|
vm="$(vm_name "$vid" "$name")"
|
||
|
|
ip="${prefix}.10"
|
||
|
|
|
||
|
|
if virsh_q dominfo "$vm" >/dev/null 2>&1; then
|
||
|
|
state="$(virsh_q domstate "$vm" 2>/dev/null | head -1 | tr -d '\n')"
|
||
|
|
if [ "$state" = "running" ]; then
|
||
|
|
log "VM $vm already running ($ip)"
|
||
|
|
continue
|
||
|
|
fi
|
||
|
|
log "VM $vm exists but is $state — starting"
|
||
|
|
virsh_q start "$vm" >/dev/null
|
||
|
|
continue
|
||
|
|
fi
|
||
|
|
|
||
|
|
log "creating VM $vm ($ip on vlan $vid/$name)"
|
||
|
|
|
||
|
|
disk="$IMG_DIR/${vm}.qcow2"
|
||
|
|
seed="$IMG_DIR/${vm}-seed.iso"
|
||
|
|
|
||
|
|
# Copy-on-write overlay: each VM costs a few MB, not 176.
|
||
|
|
sudo qemu-img create -q -f qcow2 -F qcow2 -b "$BASE_IMAGE" "$disk" "$VM_DISK" >/dev/null
|
||
|
|
|
||
|
|
build_seed "$seed" "$vm" "$vid" "$name" "$prefix" "$ip" "$real" "$SSH_PUB"
|
||
|
|
|
||
|
|
sudo virt-install \
|
||
|
|
--connect "$LIBVIRT_URI" \
|
||
|
|
--name "$vm" \
|
||
|
|
--memory "$VM_MEM" --vcpus "$VM_CPUS" \
|
||
|
|
--disk "path=$disk,format=qcow2,bus=virtio" \
|
||
|
|
--disk "path=$seed,device=cdrom,readonly=on" \
|
||
|
|
--network "network=$OVS_NET,portgroup=vlan${vid},model=virtio" \
|
||
|
|
--os-variant alpinelinux3.18 \
|
||
|
|
--graphics none --noautoconsole --import >/dev/null
|
||
|
|
done
|
||
|
|
|
||
|
|
echo
|
||
|
|
log "waiting for VMs to answer on SSH + HTTP..."
|
||
|
|
wait_ready
|
||
|
|
echo
|
||
|
|
status_table
|
||
|
|
echo
|
||
|
|
log "environment is UP. Tear down with: $SCRIPT_DIR/labsim-down.sh"
|