From 313c159a3ec1801bd72c533590d255e2d75f7416 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 23 Aug 2025 23:31:35 -0400 Subject: [PATCH] Integrate code to launch the VMs. --- ansible/roles/bhyve/files/bhyverc.bash | 225 +++++++++++++++++++++++-- 1 file changed, 212 insertions(+), 13 deletions(-) diff --git a/ansible/roles/bhyve/files/bhyverc.bash b/ansible/roles/bhyve/files/bhyverc.bash index 2d160d3..190306a 100644 --- a/ansible/roles/bhyve/files/bhyverc.bash +++ b/ansible/roles/bhyve/files/bhyverc.bash @@ -37,6 +37,7 @@ fi : ${VNC_LISTEN:="127.0.0.1:5900"} : ${VNC_WIDTH:="1920"} : ${VNC_HEIGHT:="1080"} +: "${CD:=}" ############## Setup ######################### @@ -74,6 +75,8 @@ function main { elif [ "$cmd" = "_start_body" ]; then init start_body "${@}" + elif [ "$cmd" = "create-disk" ]; then + create_disk "${@}" else (>&2 echo "Unknown command: $cmd") exit 1 @@ -100,22 +103,10 @@ function start_one { # /usr/local/bin/tmux new-session -d -s "$tmux_name" "/usr/bin/env VNC_ENABLE=NO VNC_LISTEN=0.0.0.0:5900 /usr/local/bin/bash /home/talexander/launch_opnsense.bash" } -function start_body { - local name="$1" - local pidfile="/run/bhyverc/${name}/pid" - mkdir -p "/run/bhyverc/${name}" - trap "set +e; stop_one '${name}'" EXIT - local launch_cmd=() - launch_cmd+=(launch_pidfile "$pidfile" sleep 999) - ( - IFS=$' \n\t' - bash -c "${launch_cmd[*]}" - ) -} - function launch_pidfile { local pidfile="$1" shift 1 + mkdir -p "$(dirname "$pidfile")" cat > "${pidfile}" <<< "$$" set -x exec "${@}" @@ -222,5 +213,213 @@ function init { ############## Bhyve ########################### +function create_disk { + local zfs_path="$1" + local mount_path="$2" + local gigabytes="$3" + zfs create -o "mountpoint=$mount_path" "$zfs_path" + cp /usr/local/share/edk2-bhyve/BHYVE_UEFI_VARS.fd "${mount_path}/" + tee "${mount_path}/settings" <&2 echo "No available links on bridge $bridge_name") + exit 1 + fi + done +} + +function assert_bridge { + local host_interface_name="$1" + local bridge_name="$2" + local ip_range="$3" + + if ! ng_exists "${bridge_name}:"; then + ngctl -d -f - </dev/null 2>&1 +} + +function calculate_mac_address { + local name="$1" + local source + source=$(md5 -r -s "$name" | awk '{print $1}') + echo "06:${source:0:2}:${source:2:2}:${source:4:2}:${source:6:2}:${source:8:2}" +} + +function find_available_port { + local start_port="$1" + local port="$start_port" + while true; do + sockstat -P tcp -p 443 + port=$((port + 1)) + done +} + +function ngctlcat { + if [ "$VERBOSE" = "YES" ]; then + tee /dev/tty | ngctl -d -f - + else + ngctl -d -f - + fi +} main "${@}"