Store VM metadata as JSON
This commit is contained in:
parent
bbd57d8dd2
commit
7af04b7535
11 changed files with 188 additions and 178 deletions
51
run.sh
51
run.sh
|
|
@ -62,9 +62,9 @@ shopt -s nullglob
|
|||
|
||||
name_taken() {
|
||||
local candidate="$1"
|
||||
local info existing_name
|
||||
for info in "$VM_ROOT"/*/info; do
|
||||
existing_name="$(awk -F= '$1=="name"{print $2}' "$info")"
|
||||
local vm_json existing_name
|
||||
for vm_json in "$VM_ROOT"/*/vm.json; do
|
||||
existing_name="$(jq -r '.meta.name // empty' "$vm_json" 2>/dev/null)"
|
||||
if [[ "$existing_name" == "$candidate" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
|
@ -311,6 +311,10 @@ if ! command -v e2cp >/dev/null 2>&1 || ! command -v e2rm >/dev/null 2>&1; then
|
|||
log "e2cp and e2rm are required to set hostname and resolv.conf"
|
||||
exit 1
|
||||
fi
|
||||
if ! command -v jq >/dev/null 2>&1; then
|
||||
log "jq is required to persist VM metadata"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
COW_BYTES="$(parse_disk_size "$COW_SIZE")"
|
||||
if [[ -z "$COW_BYTES" ]]; then
|
||||
|
|
@ -469,24 +473,29 @@ log "starting virtual machine"
|
|||
-H "Content-Type: application/json" \
|
||||
-d '{ "action_type": "InstanceStart" }' >/dev/null
|
||||
VM_STARTED=1
|
||||
|
||||
cat > "$VM_DIR/info" <<EOF
|
||||
id=$VM_ID
|
||||
name=$VM_NAME
|
||||
pid=$FC_PID
|
||||
created_at=$(date -Iseconds)
|
||||
rootfs=$ROOTFS
|
||||
kernel=$KERNEL
|
||||
guest_ip=$GUEST_IP
|
||||
tap=$TAP_DEV
|
||||
api_sock=$API_SOCK
|
||||
log=$LOG_FILE
|
||||
base_loop=$BASE_LOOP
|
||||
cow_file=$COW_FILE
|
||||
cow_loop=$COW_LOOP
|
||||
dm_name=$DM_NAME
|
||||
dm_dev=$DM_DEV
|
||||
EOF
|
||||
VM_CONFIG_JSON="$("${CURL_CMD[@]}" --unix-socket "$API_SOCK" -sS http://localhost/vm/config)"
|
||||
CREATED_AT="$(date -Iseconds)"
|
||||
jq -n \
|
||||
--arg id "$VM_ID" \
|
||||
--arg name "$VM_NAME" \
|
||||
--arg pid "$FC_PID" \
|
||||
--arg created_at "$CREATED_AT" \
|
||||
--arg guest_ip "$GUEST_IP" \
|
||||
--arg tap "$TAP_DEV" \
|
||||
--arg api_sock "$API_SOCK" \
|
||||
--arg log "$LOG_FILE" \
|
||||
--arg rootfs "$ROOTFS" \
|
||||
--arg kernel "$KERNEL" \
|
||||
--arg home_path "$HOME_PATH" \
|
||||
--arg var_path "$VAR_PATH" \
|
||||
--arg base_loop "$BASE_LOOP" \
|
||||
--arg cow_file "$COW_FILE" \
|
||||
--arg cow_loop "$COW_LOOP" \
|
||||
--arg dm_name "$DM_NAME" \
|
||||
--arg dm_dev "$DM_DEV" \
|
||||
--argjson config "$VM_CONFIG_JSON" \
|
||||
'{meta:{id:$id,name:$name,pid:$pid,created_at:$created_at,guest_ip:$guest_ip,tap:$tap,api_sock:$api_sock,log:$log,rootfs:$rootfs,kernel:$kernel,home_path:$home_path,var_path:$var_path,base_loop:$base_loop,cow_file:$cow_file,cow_loop:$cow_loop,dm_name:$dm_name,dm_dev:$dm_dev},config:$config}' \
|
||||
> "$VM_DIR/vm.json"
|
||||
|
||||
log "vm started successfully"
|
||||
log "guest ip: $GUEST_IP"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue