Add vm control scripts
This commit is contained in:
parent
6aa191663a
commit
75f03aeb04
6 changed files with 270 additions and 16 deletions
87
kill.sh
Executable file
87
kill.sh
Executable file
|
|
@ -0,0 +1,87 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
log() {
|
||||
printf '[kill] %s\n' "$*"
|
||||
}
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: ./kill.sh <id-or-name-prefix> [--signal SIGTERM|SIGKILL|...]
|
||||
|
||||
Sends a signal to the Firecracker process.
|
||||
EOF
|
||||
}
|
||||
|
||||
get_prop() {
|
||||
local info="$1"
|
||||
local key="$2"
|
||||
awk -F= -v k="$key" '$1==k {print $2}' "$info"
|
||||
}
|
||||
|
||||
find_vm_info() {
|
||||
local query="$1"
|
||||
local info match_count=0 match=""
|
||||
|
||||
for info in state/vm-*/info; do
|
||||
[[ -f "$info" ]] || continue
|
||||
local id name
|
||||
id="$(get_prop "$info" "id")"
|
||||
name="$(get_prop "$info" "name")"
|
||||
if [[ "$id" == "$query"* || "$name" == "$query"* ]]; then
|
||||
match="$info"
|
||||
match_count=$((match_count + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
if (( match_count == 0 )); then
|
||||
log "no VM found for prefix: $query"
|
||||
exit 1
|
||||
fi
|
||||
if (( match_count > 1 )); then
|
||||
log "multiple VMs found for prefix: $query"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf '%s' "$match"
|
||||
}
|
||||
|
||||
SIGNAL="TERM"
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--signal)
|
||||
SIGNAL="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
if [[ -z "${QUERY:-}" ]]; then
|
||||
QUERY="$1"
|
||||
shift
|
||||
continue
|
||||
fi
|
||||
log "unknown option: $1"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -z "$QUERY" || "$QUERY" == "-h" || "$QUERY" == "--help" ]]; then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
INFO_FILE="$(find_vm_info "$QUERY")"
|
||||
PID="$(get_prop "$INFO_FILE" "pid")"
|
||||
if [[ -z "$PID" ]]; then
|
||||
log "pid not found in $INFO_FILE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log "sending SIG$SIGNAL to pid $PID"
|
||||
sudo kill "-$SIGNAL" "$PID"
|
||||
log "signal sent"
|
||||
Loading…
Add table
Add a link
Reference in a new issue