range_common.sh is a sourced library: range_up.sh, range_down.sh and range_status.sh consume its constants, which shellcheck cannot see when it analyses the file alone, so it reported every one as unused. Exporting them states that interface, and the two $(cd ...) assignments are split from their export so a failing cd is not masked.
139 lines
7.1 KiB
Bash
Executable file
139 lines
7.1 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Shared settings for the real-sensor range (sourced). Runtime state lives on disk under RANGE_DIR
|
|
# (not /tmp: it must survive host reboots and travel).
|
|
set -euo pipefail
|
|
# shellcheck disable=SC2034
|
|
# ^ sourced library: range_up.sh / range_down.sh / range_status.sh consume these
|
|
# constants, which shellcheck cannot see when it analyses this file alone.
|
|
RANGE_HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # separate: export would mask the cd status
|
|
REPO=$(cd "$RANGE_HERE/../../.." && pwd)
|
|
export RANGE_HERE REPO
|
|
DEFAULT_RANGE_DIR=/var/tmp/sb-range # range_attack.py falls back to the same path
|
|
export RANGE_DIR=${SB_RANGE_DIR:-$DEFAULT_RANGE_DIR}
|
|
export VM_SRC=${SB_RANGE_VM_SRC:-$REPO/deploy/validation/vm} # arch-cloud.qcow2 + id_vm from the 2026-11-08 de-risk
|
|
export BASE_IMG=$VM_SRC/arch-cloud.qcow2
|
|
export SSHK=$VM_SRC/id_vm
|
|
export SENSORS=${SB_RANGE_SENSORS:-3}
|
|
# Guest RAM per sensor. 2048 fits a full desktop host; lower it (e.g. 1536) when the
|
|
# host cannot keep ~8 GiB free with three VMs up, rather than letting the
|
|
# monitor pause the range every few hours. The daemon itself needs ~300 MiB.
|
|
export VM_MEM_MB=${SB_RANGE_VM_MEM:-2048}
|
|
export SSH_PORT_BASE=2240 # sensor-i ssh on 127.0.0.1:(2240+i)
|
|
export CALDERA_NAME=sbrange-caldera
|
|
export CALDERA_KEY=ADMIN123 # Caldera -E default red key (local range only)
|
|
export CALDERA_URL=http://127.0.0.1:8888
|
|
export GUEST_CALDERA=http://10.0.2.2:8888 # slirp gateway back to the host
|
|
# Host safety (learned from the 2026-11-17 global OOM): the range is three 2-GiB VMs plus a
|
|
# 3-GiB-capped Caldera, so refuse to start below these floors and pause below RANGE_MIN_AVAIL_MB.
|
|
export RANGE_MIN_AVAIL_KB=$(( ${RANGE_MIN_AVAIL_MB:-10240} * 1024 ))
|
|
export RANGE_MIN_DISK_KB=$(( ${RANGE_MIN_DISK_GB:-20} * 1024 * 1024 ))
|
|
# Caldera keeps a registration for every paw that ever checked in, so /api/v2/agents counts agent
|
|
# restarts too. An agent counts as live only when its last beacon is at most this old (sandcat's
|
|
# default beacon interval is under a minute).
|
|
export AGENT_STALE_S=${SB_RANGE_AGENT_STALE_S:-300}
|
|
export SCHED_SCRIPT=$RANGE_HERE/range_attack.py
|
|
export SCHED_PID_FILE=$RANGE_DIR/state/scheduler.pid # written by range_up.sh
|
|
mkdir -p "$RANGE_DIR"/{logs,vms,payload}
|
|
|
|
port_of() { echo $((SSH_PORT_BASE + $1)); }
|
|
ssh_vm() { local i=$1; shift; ssh -i "$SSHK" -p "$(port_of "$i")" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
|
|
-o ConnectTimeout=5 -o BatchMode=yes -o LogLevel=ERROR arch@127.0.0.1 "$@"; }
|
|
scp_vm() { local i=$1 src=$2 dst=$3; scp -q -i "$SSHK" -P "$(port_of "$i")" -o StrictHostKeyChecking=no \
|
|
-o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -r "$src" "arch@127.0.0.1:$dst"; }
|
|
# Only report a PID that is still this range's QEMU for that sensor: a persisted PID can be
|
|
# recycled by an unrelated process, which must never be signalled or counted as a live VM.
|
|
vm_pid() {
|
|
local f="$RANGE_DIR/vms/sensor-$1.pid" pid
|
|
[ -s "$f" ] || return 0
|
|
pid=$(cat "$f")
|
|
kill -0 "$pid" 2>/dev/null || return 0
|
|
tr '\0' ' ' < "/proc/$pid/cmdline" 2>/dev/null | grep -q -- "-name sensor-$1 " || return 0
|
|
tr '\0' ' ' < "/proc/$pid/cmdline" 2>/dev/null | grep -q -- "$RANGE_DIR/vms/sensor-$1.qcow2" || return 0
|
|
echo "$pid"
|
|
}
|
|
# Every sensor index this range has state for: the union of the vms/ entries range_up.sh creates
|
|
# (sensor-N.pid, sensor-N.qcow2, sensor-N-seed.iso). Teardown must cover these, not the current
|
|
# SB_RANGE_SENSORS: a lowered count or a stale pid file would otherwise leave a VM behind.
|
|
range_sensor_ids() {
|
|
local f id
|
|
for f in "$RANGE_DIR"/vms/sensor-*.pid "$RANGE_DIR"/vms/sensor-*.qcow2 "$RANGE_DIR"/vms/sensor-*-seed.iso; do
|
|
[ -e "$f" ] || continue
|
|
id=$(basename "$f" | sed -n 's/^sensor-\([0-9][0-9]*\)\.pid$/\1/p; s/^sensor-\([0-9][0-9]*\)\.qcow2$/\1/p; s/^sensor-\([0-9][0-9]*\)-seed\.iso$/\1/p')
|
|
[ -n "$id" ] && echo "$id"
|
|
done | sort -n -u
|
|
return 0
|
|
}
|
|
# PIDs of live QEMUs whose command line names one of this range's sensor disks. This finds a VM even
|
|
# when its pid file is missing or holds a recycled PID, which vm_pid() (correctly) refuses.
|
|
range_qemu_pids() {
|
|
local d pid
|
|
for d in /proc/[0-9]*; do
|
|
pid=${d#/proc/}
|
|
[ -r "$d/cmdline" ] || continue
|
|
tr '\0' ' ' <"$d/cmdline" 2>/dev/null | grep -q -- "qemu-system" || continue
|
|
tr '\0' ' ' <"$d/cmdline" 2>/dev/null | grep -q -- "$RANGE_DIR/vms/sensor-" || continue
|
|
echo "$pid"
|
|
done
|
|
return 0
|
|
}
|
|
# The SB_RANGE_DIR a process actually runs with: range_attack.py reads it from the environment and
|
|
# falls back to DEFAULT_RANGE_DIR, so this is the range identity of a scheduler candidate.
|
|
proc_range_dir() {
|
|
local dir
|
|
dir=$(tr '\0' '\n' < "/proc/$1/environ" 2>/dev/null | sed -n 's/^SB_RANGE_DIR=//p' | tail -1)
|
|
echo "${dir:-$DEFAULT_RANGE_DIR}"
|
|
}
|
|
# A scheduler PID counts only while its command line still runs this repo's range_attack.py with
|
|
# SB_RANGE_DIR resolving to this RANGE_DIR: another range's (or an unrelated) scheduler is never
|
|
# signalled, which a host-wide `pkill -f range_attack.py` cannot promise.
|
|
sched_pid_alive() {
|
|
local pid=$1
|
|
[ -n "$pid" ] || return 1
|
|
kill -0 "$pid" 2>/dev/null || return 1
|
|
tr '\0' ' ' < "/proc/$pid/cmdline" 2>/dev/null | grep -q -F -- "$SCHED_SCRIPT" || return 1
|
|
[ "$(proc_range_dir "$pid")" = "$RANGE_DIR" ] || return 1
|
|
return 0
|
|
}
|
|
# This range's schedulers: the verified PID from the pid file, plus any process found by scanning
|
|
# /proc, so a missing or stale pid file (crash, an older range_up.sh) is still cleaned up.
|
|
sched_pids() {
|
|
local f=$SCHED_PID_FILE pid seen=" " d
|
|
if [ -s "$f" ]; then
|
|
pid=$(cat "$f")
|
|
if sched_pid_alive "$pid"; then
|
|
echo "$pid"
|
|
seen=" $pid "
|
|
fi
|
|
fi
|
|
for d in /proc/[0-9]*; do
|
|
pid=${d#/proc/}
|
|
case "$seen" in *" $pid "*) continue;; esac
|
|
[ -r "$d/cmdline" ] || continue
|
|
tr '\0' ' ' <"$d/cmdline" 2>/dev/null | grep -q -F -- "$SCHED_SCRIPT" || continue
|
|
[ "$(proc_range_dir "$pid")" = "$RANGE_DIR" ] || continue
|
|
echo "$pid"
|
|
done
|
|
return 0
|
|
}
|
|
# Stop only this range's scheduler(s): SIGTERM, a bounded wait, then SIGKILL. Returns non-zero when
|
|
# there was nothing to stop; the pid file is removed only once no verified scheduler is left.
|
|
sched_stop() {
|
|
local pids pid alive i
|
|
pids=$(sched_pids)
|
|
[ -n "$pids" ] || { rm -f "$SCHED_PID_FILE"; return 1; }
|
|
for pid in $pids; do kill -TERM "$pid" 2>/dev/null || true; done
|
|
for i in $(seq 1 15); do
|
|
alive=""
|
|
for pid in $pids; do kill -0 "$pid" 2>/dev/null && alive="$alive $pid"; done
|
|
[ -n "$alive" ] || break
|
|
sleep 1
|
|
done
|
|
for pid in $pids; do kill -KILL "$pid" 2>/dev/null || true; done
|
|
sleep 1
|
|
[ -n "$(sched_pids)" ] && return 0
|
|
rm -f "$SCHED_PID_FILE"
|
|
return 0
|
|
}
|
|
mem_available_kb() { awk '/^MemAvailable:/{print $2}' /proc/meminfo; }
|
|
disk_available_kb() { df -Pk "$RANGE_DIR" | awk 'NR==2{print $4}'; }
|
|
caldera() { curl -sf -H "KEY: $CALDERA_KEY" "$@"; }
|