Skip to content

Commit

Permalink
Add support for multiple krunvm VMs
Browse files Browse the repository at this point in the history
Add support for multiple krunvm-based VMs in the API. This uses a
temporary directory that will store the mappings between the name of the
VM and the PID of the underlying (top) process. The directory should be
removed through microvm_cleanup upon process exit.
  • Loading branch information
efrecon committed Apr 5, 2024
1 parent 970043c commit 6b8ecce
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/microvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# automatically selected.
: "${KRUNVM_RUNNER_RUNTIME:=""}"

# Directory used for VM->PID mapping when using krunvm
: "${KRUNVM_RUNNER_STORAGE:=""}"

# Run krunvm with the provided arguments, behind a buildah unshare.
run_krunvm() {
debug "Running krunvm $*"
Expand Down Expand Up @@ -50,6 +53,9 @@ microvm_runtime() {
krunvm)
check_command krunvm
check_command buildah
if [ -z "$KRUNVM_RUNNER_STORAGE" ]; then
KRUNVM_RUNNER_STORAGE="$(mktemp -d)"
fi
;;
*)
error "Unknown microVM runtime: $KRUNVM_RUNNER_RUNTIME"
Expand Down Expand Up @@ -168,6 +174,7 @@ EOF
run_krunvm start "$KRUNVM_RUNNER_NAME" "$RUNNER_ENTRYPOINT" -- "$@" </dev/null &
KRUNVM_RUNNER_PID=$!
eval "$optstate"; # Restore options
printf %d\\n "$KRUNVM_RUNNER_PID" > "${KRUNVM_RUNNER_STORAGE}/${KRUNVM_RUNNER_NAME}.pid"
verbose "Started microVM '$KRUNVM_RUNNER_NAME' with PID $KRUNVM_RUNNER_PID"
wait "$KRUNVM_RUNNER_PID"
KRUNVM_RUNNER_PID=
Expand All @@ -190,6 +197,7 @@ microvm_wait() {
podman*)
podman wait "$1";;
krunvm)
KRUNVM_RUNNER_PID=$(cat "${KRUNVM_RUNNER_STORAGE}/$1.pid")
if [ -n "$KRUNVM_RUNNER_PID" ]; then
# shellcheck disable=SC2046 # We want to wait for all children
waitpid $(ps_tree "$KRUNVM_RUNNER_PID"|tac)
Expand All @@ -216,10 +224,12 @@ microvm_stop() {
# TODO: Specify howlog to wait between TERM and KILL?
podman stop "$1";;
krunvm)
KRUNVM_RUNNER_PID=$(cat "${KRUNVM_RUNNER_STORAGE}/$1.pid")
if [ -n "$KRUNVM_RUNNER_PID" ]; then
kill_tree "$KRUNVM_RUNNER_PID"
# shellcheck disable=SC2046 # We want to wait for all children
microvm_wait "$1"
rm -f "${KRUNVM_RUNNER_STORAGE}/$1.pid" || true
fi
;;
*)
Expand All @@ -241,7 +251,8 @@ microvm_delete() {
podman rm -f "$1";;
krunvm)
verbose "Removing microVM '$1'"
run_krunvm delete "$1";;
run_krunvm delete "$1"
;;
*)
error "Unknown microVM runtime: $KRUNVM_RUNNER_RUNTIME"
;;
Expand All @@ -265,5 +276,8 @@ microvm_pull() {
error "Unknown microVM runtime: $KRUNVM_RUNNER_RUNTIME"
;;
esac
}

}
microvm_cleanup() {
[ -n "$KRUNVM_RUNNER_STORAGE" ] && rm -rf "$KRUNVM_RUNNER_STORAGE"
}
2 changes: 2 additions & 0 deletions orchestrator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ EOF
verbose "Removing isolation environment $ORCHESTRATOR_ENVIRONMENT"
rm -rf "$ORCHESTRATOR_ENVIRONMENT"
fi

microvm_cleanup
}

# Pass the runtime to the microvm script
Expand Down
2 changes: 2 additions & 0 deletions runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ cleanup() {
vm_terminate "$RUNNER_ID"
vm_delete "$RUNNER_ID"
fi

microvm_cleanup
}

trap cleanup EXIT
Expand Down

0 comments on commit 6b8ecce

Please sign in to comment.