2023-04-26 05:19:01 +00:00
|
|
|
#!/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"
|
2023-04-26 21:58:46 +00:00
|
|
|
mount_path="$2"
|
|
|
|
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" <<EOF
|
|
|
|
CPU_CORES=1
|
|
|
|
MEMORY=1G
|
|
|
|
EOF
|
|
|
|
zfs create -s "-V${gigabytes}G" -o volmode=dev "$zfs_path/disk0"
|
2023-04-26 05:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function start_vm {
|
|
|
|
name="$1"
|
|
|
|
zfs_path="$2"
|
2023-04-26 21:58:46 +00:00
|
|
|
mount_path="$3"
|
|
|
|
host_interface_name="$4"
|
|
|
|
bridge_name="bridge_${host_interface_name}"
|
|
|
|
ip_range="$5"
|
|
|
|
|
2023-04-26 23:06:28 +00:00
|
|
|
mac_address=$(calculate_mac_address "$name")
|
|
|
|
|
2023-04-26 21:58:46 +00:00
|
|
|
assert_bridge "$host_interface_name" "$bridge_name" "$ip_range"
|
|
|
|
bridge_link_name=$(detect_available_link "${bridge_name}")
|
|
|
|
|
|
|
|
|
2023-04-26 05:19:01 +00:00
|
|
|
CPU_CORES=1
|
|
|
|
MEMORY=1G
|
2023-04-26 21:58:46 +00:00
|
|
|
if [ -e "${mount_path}/settings" ]; then
|
|
|
|
source "${mount_path}/settings"
|
|
|
|
fi
|
2023-04-26 05:19:01 +00:00
|
|
|
# -H release the CPU when guest issues HLT instruction. Otherwise 100% of core will be consumed.
|
2023-04-26 21:58:46 +00:00
|
|
|
# -s 3,ahci-cd,/vm/.iso/archlinux-2023.04.01-x86_64.iso \
|
|
|
|
# -s 29,fbuf,tcp=0.0.0.0:5900,w=1920,h=1080,wait \
|
|
|
|
while true; do
|
|
|
|
set -x
|
2023-04-26 22:17:35 +00:00
|
|
|
set +e
|
2023-04-26 21:58:46 +00:00
|
|
|
bhyve \
|
2023-04-26 22:17:35 +00:00
|
|
|
-D \
|
2023-04-26 21:58:46 +00:00
|
|
|
-c $CPU_CORES \
|
|
|
|
-m $MEMORY \
|
|
|
|
-H \
|
|
|
|
-s 0,hostbridge \
|
|
|
|
-s "4,nvme,/dev/zvol/${zfs_path}/disk0" \
|
2023-04-26 23:06:28 +00:00
|
|
|
-s "2:0,virtio-net,netgraph,path=${bridge_name}:,peerhook=${bridge_link_name},mac=${mac_address}" \
|
|
|
|
-s 3,ahci-cd,/vm/.iso/archlinux-2023.04.01-x86_64.iso \
|
2023-04-26 21:58:46 +00:00
|
|
|
-s 29,fbuf,tcp=0.0.0.0:5900,w=1920,h=1080 \
|
|
|
|
-s 30,xhci,tablet \
|
|
|
|
-s 31,lpc -l com1,stdio \
|
|
|
|
-l "bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd,${mount_path}/BHYVE_UEFI_VARS.fd" \
|
|
|
|
"$name"
|
|
|
|
exit_code=$?
|
2023-04-26 22:17:35 +00:00
|
|
|
set -e
|
2023-04-26 21:58:46 +00:00
|
|
|
set +x
|
|
|
|
if [ $exit_code -eq 0 ]; then
|
|
|
|
echo "Rebooting."
|
|
|
|
elif [ $exit_code -eq 1 ]; then
|
|
|
|
echo "Powered off."
|
|
|
|
break
|
|
|
|
elif [ $exit_code -eq 2 ]; then
|
|
|
|
echo "Halted."
|
|
|
|
break
|
|
|
|
elif [ $exit_code -eq 3 ]; then
|
|
|
|
echo "Triple fault."
|
|
|
|
break
|
|
|
|
elif [ $exit_code -eq 4 ]; then
|
|
|
|
echo "Exited due to an error."
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2023-04-26 22:17:35 +00:00
|
|
|
|
|
|
|
bhyvectl "--vm=$name" --destroy
|
|
|
|
echo "Destroyed bhyve vm."
|
2023-04-26 05:19:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 21:58:46 +00:00
|
|
|
function detect_available_link {
|
|
|
|
bridge_name="$1"
|
|
|
|
linknum=1
|
|
|
|
while true; do
|
|
|
|
link_name="link${linknum}"
|
|
|
|
if ! ng_exists "${bridge_name}:${link_name}"; then
|
|
|
|
echo "$link_name"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
linknum=$((linknum + 1))
|
|
|
|
if [ "$linknum" -gt 90 ]; then
|
|
|
|
(>&2 echo "No available links on bridge $bridge_name")
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function assert_bridge {
|
|
|
|
host_interface_name="$1"
|
|
|
|
bridge_name="$2"
|
|
|
|
ip_range="$3"
|
|
|
|
|
|
|
|
if ! ng_exists "${bridge_name}:"; then
|
|
|
|
ngctl -d -f - <<EOF
|
|
|
|
mkpeer . eiface hook ether
|
|
|
|
name .:hook $host_interface_name
|
|
|
|
EOF
|
|
|
|
ngctl -d -f - <<EOF
|
|
|
|
mkpeer ${host_interface_name}: bridge ether link0
|
|
|
|
name ${host_interface_name}:ether $bridge_name
|
|
|
|
EOF
|
|
|
|
ifconfig $(ngctl msg "${host_interface_name}:" getifname | grep Args | cut -d '"' -f 2) name "${host_interface_name}" "$ip_range" up
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function ng_exists {
|
|
|
|
ngctl status "${1}" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:06:28 +00:00
|
|
|
function calculate_mac_address {
|
|
|
|
name="$1"
|
|
|
|
source=$(md5 -r -s "$name" | awk '{print $1}')
|
|
|
|
echo "${source:0:2}:${source:2:2}:${source:4:2}:${source:6:2}:${source:8:2}:${source:10:2}"
|
|
|
|
}
|
|
|
|
|
2023-04-26 21:58:46 +00:00
|
|
|
|
2023-04-26 05:19:01 +00:00
|
|
|
main "${@}"
|