From 9fcc955d13537a930849fdf4739dbef59c89aea9 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 26 Apr 2023 01:19:01 -0400 Subject: [PATCH] Starting to write my own script to manage bhyve VMs. vm-bhyve isn't going to allow me enough control to detect open netgraph hooks on bridges so I will manage the virtual machines myself. --- ansible/roles/bhyve/files/arch.conf | 2 +- .../bhyve/files/bhyve_netgraph_bridge.bash | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 ansible/roles/bhyve/files/bhyve_netgraph_bridge.bash diff --git a/ansible/roles/bhyve/files/arch.conf b/ansible/roles/bhyve/files/arch.conf index 447049e..4127080 100644 --- a/ansible/roles/bhyve/files/arch.conf +++ b/ansible/roles/bhyve/files/arch.conf @@ -19,7 +19,7 @@ disk0_dev="sparse-zvol" virt_random="yes" # virtio-rnd # Creates a link to host_bridge1's link3 hook to the vmlink hook on a type socket -bhyve_options="-s 2:0,virtio-net,netgraph,path=host_bridge1:,peerhook=link3" +bhyve_options="-s 2:0,virtio-net,netgraph,path=bridge_jail_nat:,peerhook=link90" # Share a host directory to the guest via 9pfs. # diff --git a/ansible/roles/bhyve/files/bhyve_netgraph_bridge.bash b/ansible/roles/bhyve/files/bhyve_netgraph_bridge.bash new file mode 100644 index 0000000..a5e0ff8 --- /dev/null +++ b/ansible/roles/bhyve/files/bhyve_netgraph_bridge.bash @@ -0,0 +1,47 @@ +#!/usr/local/bin/bash +# +set -euo pipefail +IFS=$'\n\t' +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +function main { + if [ "$1" = "create-disk" ]; then + shift 1 + create_disk "${@}" + elif [ "$1" = "start" ]; then + shift 1 + start_vm "${@}" + else + >&2 echo "Unrecognized command" + exit 1 + fi +} + +function create_disk { + zfs_path="$1" + gigabytes="$2" + zfs create "-V${gigabytes}G" -o volmode=dev "$zfs_path" +} + +function start_vm { + name="$1" + zfs_path="$2" + CPU_CORES=1 + MEMORY=1G + # -H release the CPU when guest issues HLT instruction. Otherwise 100% of core will be consumed. + bhyve \ + -c $CPU_CORES \ + -m $MEMORY \ + -H \ + -s 0,hostbridge \ + -s 3,ahci-cd,/vm/.iso/archlinux-2023.04.01-x86_64.iso \ + -s 4,virtio-blk,/dev/zvol/${zfs_path} \ + -s 2:0,virtio-net,netgraph,path=bridge_jail_nat:,peerhook=link90 \ + -s 29,fbuf,tcp=0.0.0.0:5900,w=1920,h=1080,wait \ + -s 30,xhci,tablet \ + -s 31,lpc -l com1,stdio \ + -l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd \ + "$name" +} + +main "${@}"