Store VM metadata as JSON

This commit is contained in:
Thales Maciel 2026-01-31 23:17:12 -03:00
parent bbd57d8dd2
commit 7af04b7535
No known key found for this signature in database
GPG key ID: 33112E6833C34679
11 changed files with 188 additions and 178 deletions

View file

@ -6,18 +6,19 @@ log() {
}
cleanup() {
if [[ -z "${VM_INFO:-}" || ! -f "$VM_INFO" ]]; then
if [[ -z "${VM_JSON:-}" || ! -f "$VM_JSON" ]]; then
return
fi
# shellcheck disable=SC1090
source "$VM_INFO"
if [[ -n "${pid:-}" ]]; then
pid="$(jq -r '.meta.pid // empty' "$VM_JSON")"
tap="$(jq -r '.meta.tap // empty' "$VM_JSON")"
vm_dir="$(dirname "$VM_JSON")"
if [[ -n "$pid" ]]; then
sudo kill "$pid" 2>/dev/null || true
fi
if [[ -n "${tap:-}" ]]; then
if [[ -n "$tap" ]]; then
sudo ip link del "$tap" 2>/dev/null || true
fi
if [[ -n "${vm_dir:-}" ]]; then
if [[ -n "$vm_dir" ]]; then
rm -rf "$vm_dir"
fi
}
@ -35,18 +36,21 @@ if [[ -z "$VM_DIR" ]]; then
log "no VM state directory found"
exit 1
fi
VM_INFO="$VM_DIR/info"
if [[ ! -f "$VM_INFO" ]]; then
log "info file not found: $VM_INFO"
VM_JSON="$VM_DIR/vm.json"
if [[ ! -f "$VM_JSON" ]]; then
log "vm.json not found: $VM_JSON"
exit 1
fi
# shellcheck disable=SC1090
source "$VM_INFO"
name="$(jq -r '.meta.name // empty' "$VM_JSON")"
created_at="$(jq -r '.meta.created_at // empty' "$VM_JSON")"
guest_ip="$(jq -r '.meta.guest_ip // empty' "$VM_JSON")"
tap="$(jq -r '.meta.tap // empty' "$VM_JSON")"
pid="$(jq -r '.meta.pid // empty' "$VM_JSON")"
vm_dir="$VM_DIR"
if [[ -z "${name:-}" || -z "${created_at:-}" || -z "${guest_ip:-}" ]]; then
log "missing name or created_at in info file"
if [[ -z "$name" || -z "$created_at" || -z "$guest_ip" ]]; then
log "missing name or created_at in vm.json"
exit 1
fi