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 )"
|
|
|
|
|
2023-04-27 21:08:57 +00:00
|
|
|
# Share a host directory to the guest via 9pfs.
|
|
|
|
#
|
|
|
|
# Inside the VM run:
|
|
|
|
# mount -t virtfs -o trans=virtio sharename /some/vm/path
|
|
|
|
# mount -t 9p -o cache=mmap -o msize=512000 sharename /mnt/9p
|
|
|
|
# mount -t 9p -o trans=virtio,cache=mmap,msize=512000 sharename /path/to/mountpoint
|
|
|
|
# bhyve_options="-s 28,virtio-9p,sharename=/"
|
|
|
|
|
|
|
|
# Enable Sound
|
|
|
|
# bhyve_options="-s 16,hda,play=/dev/dsp,rec=/dev/dsp"
|
|
|
|
|
2023-05-29 03:09:36 +00:00
|
|
|
# Example usage:
|
|
|
|
#
|
|
|
|
# doas bhyve_netgraph_bridge create-disk zdata/vm/poudriere /vm/poudriere 10
|
2023-05-31 01:18:47 +00:00
|
|
|
# doas bhyve_netgraph_bridge start poudriere zdata/vm/poudriere /vm/poudriere /vm/iso/FreeBSD-13.2-RELEASE-amd64-bootonly.iso
|
|
|
|
# doas bhyve_netgraph_bridge start poudriere zdata/vm/poudriere /vm/poudriere
|
2023-05-29 03:09:36 +00:00
|
|
|
|
2023-06-13 18:13:55 +00:00
|
|
|
: ${VERBOSE:="NO"} # or YES
|
2023-05-29 22:35:30 +00:00
|
|
|
: ${CPU_CORES:="1"}
|
|
|
|
: ${MEMORY:="1G"}
|
2023-06-13 18:13:55 +00:00
|
|
|
: ${NETWORK:="NAT"} # or RAW or BOTH
|
2023-05-29 22:35:30 +00:00
|
|
|
: ${IP_RANGE:="10.215.1.1/24"} # Ignored for RAW networks
|
|
|
|
: ${INTERFACE_NAME:="jail_nat"} # or the external interface like lagg0 for RAW networks
|
|
|
|
: ${BRIDGE_NAME:="bridge_$INTERFACE_NAME"} # or bridge_raw for RAW networks
|
2023-05-31 01:18:47 +00:00
|
|
|
: ${VNC_ENABLE:="NO"}
|
|
|
|
: ${VNC_LISTEN:="127.0.0.1:5900"}
|
2023-05-29 22:35:30 +00:00
|
|
|
|
2023-06-13 18:13:55 +00:00
|
|
|
if [ "$VERBOSE" = "YES" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
2023-07-14 15:18:38 +00:00
|
|
|
############## Setup #########################
|
|
|
|
|
|
|
|
function cleanup {
|
|
|
|
for vm in "${vms[@]}"; do
|
|
|
|
log "Destroying bhyve vm $f"
|
|
|
|
bhyvectl "--vm=$vm" --destroy
|
|
|
|
log "Destroyed bhyve vm $f"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
vms=()
|
|
|
|
for sig in EXIT INT QUIT HUP TERM; do
|
|
|
|
trap "set +e; cleanup" "$sig"
|
|
|
|
done
|
|
|
|
|
|
|
|
function die {
|
|
|
|
local status_code="$1"
|
|
|
|
shift
|
|
|
|
(>&2 echo "${@}")
|
|
|
|
exit "$status_code"
|
|
|
|
}
|
|
|
|
|
|
|
|
function log {
|
|
|
|
(>&2 echo "${@}")
|
|
|
|
}
|
|
|
|
|
|
|
|
############## Program #########################
|
|
|
|
|
2023-04-26 05:19:01 +00:00
|
|
|
function main {
|
2023-06-13 18:13:55 +00:00
|
|
|
local cmd="$1"
|
2023-05-29 22:35:30 +00:00
|
|
|
shift 1
|
|
|
|
if [ "$cmd" = "create-disk" ]; then
|
2023-04-26 05:19:01 +00:00
|
|
|
create_disk "${@}"
|
2023-05-29 22:35:30 +00:00
|
|
|
elif [ "$cmd" = "start" ]; then
|
2023-04-26 05:19:01 +00:00
|
|
|
start_vm "${@}"
|
|
|
|
else
|
2023-05-29 22:35:30 +00:00
|
|
|
die 1 "Unrecognized command $cmd"
|
2023-04-26 05:19:01 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:41:02 +00:00
|
|
|
function die {
|
|
|
|
local status_code="$1"
|
|
|
|
shift
|
|
|
|
(>&2 echo "${@}")
|
|
|
|
exit "$status_code"
|
|
|
|
}
|
|
|
|
|
2023-04-26 05:19:01 +00:00
|
|
|
function create_disk {
|
2023-06-13 18:13:55 +00:00
|
|
|
local zfs_path="$1"
|
|
|
|
local mount_path="$2"
|
|
|
|
local gigabytes="$3"
|
2023-04-26 21:58:46 +00:00
|
|
|
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
|
2023-05-29 22:35:30 +00:00
|
|
|
CPU_CORES="$CPU_CORES"
|
|
|
|
MEMORY="$MEMORY"
|
|
|
|
NETWORK="$NETWORK"
|
|
|
|
IP_RANGE="$IP_RANGE"
|
|
|
|
BRIDGE_NAME="$BRIDGE_NAME"
|
|
|
|
INTERFACE_NAME="$INTERFACE_NAME"
|
2023-04-26 21:58:46 +00:00
|
|
|
EOF
|
2023-06-22 17:28:56 +00:00
|
|
|
zfs create -s "-V${gigabytes}G" -o volmode=dev -o primarycache=metadata -o secondarycache=none "$zfs_path/disk0"
|
2023-04-26 05:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function start_vm {
|
2023-06-13 18:13:55 +00:00
|
|
|
local name="$1"
|
|
|
|
local zfs_path="$2"
|
|
|
|
local mount_path="$3"
|
|
|
|
local mount_cd="${4:-}"
|
2023-04-26 23:06:28 +00:00
|
|
|
|
2023-04-26 21:58:46 +00:00
|
|
|
if [ -e "${mount_path}/settings" ]; then
|
|
|
|
source "${mount_path}/settings"
|
|
|
|
fi
|
2023-05-29 21:41:02 +00:00
|
|
|
|
2023-06-13 18:13:55 +00:00
|
|
|
local host_interface_name="$INTERFACE_NAME" # for raw, external interface
|
|
|
|
local bridge_name="$BRIDGE_NAME"
|
|
|
|
local ip_range="$IP_RANGE" # for raw this value does not matter
|
2023-05-29 22:35:30 +00:00
|
|
|
|
2023-06-13 18:13:55 +00:00
|
|
|
local mac_address=$(calculate_mac_address "$name")
|
2023-05-29 22:35:30 +00:00
|
|
|
|
2023-06-13 18:13:55 +00:00
|
|
|
local additional_args=()
|
2023-05-29 22:35:30 +00:00
|
|
|
|
2023-05-29 21:41:02 +00:00
|
|
|
if [ "$NETWORK" = "NAT" ]; then
|
|
|
|
assert_bridge "$host_interface_name" "$bridge_name" "$ip_range"
|
2023-06-13 18:13:55 +00:00
|
|
|
local bridge_link_name=$(detect_available_link "${bridge_name}")
|
|
|
|
additional_args+=("-s" "2:0,virtio-net,netgraph,path=${bridge_name}:,peerhook=${bridge_link_name},mac=${mac_address}")
|
2023-05-29 21:41:02 +00:00
|
|
|
elif [ "$NETWORK" = "RAW" ]; then
|
|
|
|
assert_raw "$host_interface_name" "$bridge_name"
|
2023-06-13 18:13:55 +00:00
|
|
|
local bridge_link_name=$(detect_available_link "${bridge_name}")
|
|
|
|
additional_args+=("-s" "2:0,virtio-net,netgraph,path=${bridge_name}:,peerhook=${bridge_link_name},mac=${mac_address}")
|
|
|
|
elif [ "$NETWORK" = "BOTH" ]; then
|
|
|
|
assert_bridge "jail_nat" "$bridge_name" "$ip_range"
|
|
|
|
assert_raw "$host_interface_name" "bridge_raw"
|
|
|
|
local bridge_link_name=$(detect_available_link "${bridge_name}")
|
|
|
|
local raw_bridge_link_name=$(detect_available_link "bridge_raw")
|
|
|
|
local raw_mac_address=$(calculate_mac_address "${name}_raw")
|
|
|
|
additional_args+=("-s" "2:0,virtio-net,netgraph,path=${bridge_name}:,peerhook=${bridge_link_name},mac=${mac_address}")
|
|
|
|
additional_args+=("-s" "3:0,virtio-net,netgraph,path=bridge_raw:,peerhook=${raw_bridge_link_name},mac=${raw_mac_address}")
|
2023-05-29 21:41:02 +00:00
|
|
|
else
|
|
|
|
die 1 "Unrecognized NETWORK type $NETWORK"
|
|
|
|
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 \
|
2023-04-27 22:44:32 +00:00
|
|
|
# -s 29,fbuf,tcp=0.0.0.0:5900,w=1920,h=1080 \
|
|
|
|
|
|
|
|
# TODO: Look into using nmdm instead of stdio for serial console
|
2023-05-04 20:15:38 +00:00
|
|
|
if [ -n "$mount_cd" ]; then
|
2023-08-02 20:48:09 +00:00
|
|
|
additional_args+=("-s" "4,ahci-cd,$mount_cd")
|
2023-05-04 20:15:38 +00:00
|
|
|
fi
|
2023-05-31 01:18:47 +00:00
|
|
|
if [ "$VNC_ENABLE" = "YES" ]; then
|
|
|
|
additional_args+=("-s" "29,fbuf,tcp=$VNC_LISTEN,w=1920,h=1080")
|
|
|
|
fi
|
2023-07-14 15:18:38 +00:00
|
|
|
vms+=("$name")
|
2023-04-26 21:58:46 +00:00
|
|
|
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" \
|
|
|
|
-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" \
|
2023-05-04 20:15:38 +00:00
|
|
|
"${additional_args[@]}" \
|
2023-04-26 21:58:46 +00:00
|
|
|
"$name"
|
2023-06-13 18:13:55 +00:00
|
|
|
local 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 05:19:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 21:58:46 +00:00
|
|
|
function detect_available_link {
|
2023-06-13 18:13:55 +00:00
|
|
|
local bridge_name="$1"
|
|
|
|
local linknum=1
|
2023-04-26 21:58:46 +00:00
|
|
|
while true; do
|
2023-06-13 18:13:55 +00:00
|
|
|
local link_name="link${linknum}"
|
2023-04-26 21:58:46 +00:00
|
|
|
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 {
|
2023-06-13 18:13:55 +00:00
|
|
|
local host_interface_name="$1"
|
|
|
|
local bridge_name="$2"
|
|
|
|
local ip_range="$3"
|
2023-04-26 21:58:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:41:02 +00:00
|
|
|
function assert_raw {
|
2023-06-13 18:13:55 +00:00
|
|
|
local extif="$1"
|
|
|
|
local bridge_name="$2"
|
2023-05-29 21:41:02 +00:00
|
|
|
|
|
|
|
kldload -n ng_bridge ng_eiface ng_ether
|
|
|
|
|
|
|
|
if ! ng_exists "${bridge_name}:"; then
|
2023-06-13 18:13:55 +00:00
|
|
|
ngctlcat <<EOF
|
2023-05-29 21:41:02 +00:00
|
|
|
# Create a bridge.
|
|
|
|
mkpeer $extif: bridge lower link0
|
|
|
|
# Assign a name to the bridge.
|
|
|
|
name $extif:lower ${bridge_name}
|
|
|
|
# Since the host is also using $extif, we need to connect the upper hook also. Otherwise we will lose connectivity.
|
|
|
|
connect $extif: ${bridge_name}: upper link1
|
|
|
|
|
|
|
|
# Enable promiscuous mode so the host ethernet adapter accepts packets for all addresses
|
|
|
|
msg $extif: setpromisc 1
|
|
|
|
|
|
|
|
# Do not overwrite source address on packets
|
|
|
|
msg $extif: setautosrc 0
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-04-26 21:58:46 +00:00
|
|
|
function ng_exists {
|
|
|
|
ngctl status "${1}" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:06:28 +00:00
|
|
|
function calculate_mac_address {
|
2023-06-13 18:13:55 +00:00
|
|
|
local name="$1"
|
|
|
|
local source=$(md5 -r -s "$name" | awk '{print $1}')
|
2023-04-27 22:44:32 +00:00
|
|
|
echo "06:${source:0:2}:${source:2:2}:${source:4:2}:${source:6:2}:${source:8:2}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function find_available_port {
|
2023-06-13 18:13:55 +00:00
|
|
|
local start_port="$1"
|
|
|
|
local port="$start_port"
|
2023-04-27 22:44:32 +00:00
|
|
|
while true; do
|
|
|
|
sockstat -P tcp -p 443
|
|
|
|
port=$((port + 1))
|
|
|
|
done
|
2023-04-26 23:06:28 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 18:13:55 +00:00
|
|
|
function ngctlcat {
|
|
|
|
if [ "$VERBOSE" = "YES" ]; then
|
|
|
|
tee /dev/tty | ngctl -d -f -
|
|
|
|
else
|
|
|
|
ngctl -d -f -
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-04-26 21:58:46 +00:00
|
|
|
|
2023-04-26 05:19:01 +00:00
|
|
|
main "${@}"
|