Rename vm_id field and add list helpers

This commit is contained in:
Thales Maciel 2026-01-29 13:25:23 -03:00
parent eedc1fe1d8
commit 6aa191663a
No known key found for this signature in database
GPG key ID: 33112E6833C34679
4 changed files with 49 additions and 2 deletions

View file

@ -38,7 +38,7 @@ reboot
## VM Info File
Each VM writes a metadata file at `state/vm-*/info` with the following fields:
- `vm_id`: unique identifier for the VM instance.
- `id`: unique identifier for the VM instance.
- `pid`: Firecracker process ID.
- `created_at`: timestamp when the VM was launched.
- `guest_ip`: IP address assigned to the guest.

24
list.sh Executable file
View file

@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob
for info in state/vm-*/info; do
id="$(awk -F= '$1=="id"{print $2}' "$info")"
name="$(awk -F= '$1=="name"{print $2}' "$info")"
created_at="$(awk -F= '$1=="created_at"{print $2}' "$info")"
guest_ip="$(awk -F= '$1=="guest_ip"{print $2}' "$info")"
pid="$(awk -F= '$1=="pid"{print $2}' "$info")"
tap="$(awk -F= '$1=="tap"{print $2}' "$info")"
api_sock="$(awk -F= '$1=="api_sock"{print $2}' "$info")"
status="stale"
if [[ -n "$pid" && -n "$api_sock" ]]; then
if ps -p "$pid" -o comm=,args= 2>/dev/null | rg -q "firecracker.*--api-sock $api_sock"; then
status="running"
fi
fi
printf 'status=%s id=%s name=%s created_at=%s guest_ip=%s pid=%s tap=%s\n' \
"$status" "$id" "$name" "$created_at" "$guest_ip" "$pid" "$tap"
done

23
ps.sh Executable file
View file

@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob
for info in state/vm-*/info; do
id="$(awk -F= '$1=="id"{print $2}' "$info")"
name="$(awk -F= '$1=="name"{print $2}' "$info")"
created_at="$(awk -F= '$1=="created_at"{print $2}' "$info")"
guest_ip="$(awk -F= '$1=="guest_ip"{print $2}' "$info")"
pid="$(awk -F= '$1=="pid"{print $2}' "$info")"
tap="$(awk -F= '$1=="tap"{print $2}' "$info")"
api_sock="$(awk -F= '$1=="api_sock"{print $2}' "$info")"
if [[ -z "$pid" || -z "$api_sock" ]]; then
continue
fi
if ps -p "$pid" -o comm=,args= 2>/dev/null | rg -q "firecracker.*--api-sock $api_sock"; then
printf 'id=%s name=%s created_at=%s guest_ip=%s pid=%s tap=%s\n' \
"$id" "$name" "$created_at" "$guest_ip" "$pid" "$tap"
fi
done

2
run.sh
View file

@ -355,7 +355,7 @@ log "starting virtual machine"
VM_STARTED=1
cat > "$VM_DIR/info" <<EOF
vm_id=$VM_ID
id=$VM_ID
name=$VM_NAME
pid=$FC_PID
created_at=$(date -Iseconds)