workstation/roles/home-cli/files/bhyve-vm

84 lines
2.8 KiB
Text
Raw Normal View History

2022-09-05 15:41:54 +00:00
#!/bin/sh
2022-09-06 14:49:07 +00:00
2022-09-05 15:41:54 +00:00
# Copyright 2022 Anthony Perkins
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2022-09-06 14:49:07 +00:00
# Copy this script to $vmpath. The filename should be the VM name. To ease
# snapshotting, each VM should be in its own directory, and should be a separate
# ZFS dataset. For example:
#
# /virt/vm/testvm <-- ZFS dataset.
# /virt/vm/testvm/testvm <-- This script.
# /virt/vm/testvm/testvm_disk0.img <-- Hard disk image.
# /virt/vm/testvm/cdrom.iso <-- Operating System install ISO.
#
# The VM will be named the same as this script filename, unless you change the
# vmname variable here.
2022-09-05 15:41:54 +00:00
vmname=$(basename $0)
vmpath=$(dirname $0)
2022-09-06 14:49:07 +00:00
# The VM will have one virtual CPU socket with this number of cores. Windows
# will only see up to two sockets, but up to 256 cores per socket. So cores are
# preferred.
2022-09-05 15:41:54 +00:00
cpucores=1
2022-09-06 14:49:07 +00:00
# RAM size in KiB (K), MiB (M), or GiB (G).
2022-09-05 15:41:54 +00:00
ram=1G
2022-09-06 14:49:07 +00:00
# VNC should be "localhost:5900", "127.0.0.1:5901", "[::1]:5902",
# "0.0.0.0:5903", "[::]:5904", or similar. Note that "0.0.0.0" and "[::]" will
# be accessible over the network. "localhost", "127.0.0.1", and "[::1]" will
# only be accessible from this host (or via SSH tunnels).
vnc="localhost:0"
2022-09-06 20:53:11 +00:00
# Network TAP device.
netif=tap0
2022-09-06 14:49:07 +00:00
# Change to /dev/nmdm0A for the first null-modem.
comoutput=stdio
# Comment the next line out for no CD-ROM.
2022-09-06 14:49:07 +00:00
cdrom="-s 3,ahci-cd,$vmpath/cdrom.iso"
2022-09-05 15:41:54 +00:00
################################################################################
# Check for firmware.
if ! [ -e /usr/local/share/uefi-firmware/BHYVE_UEFI.fd ]; then
echo "Firmware not found. Try \`pkg install bhyve-firmware\`." >&2
exit 1
fi
2022-09-06 15:29:43 +00:00
# Finally, run the bhyve command with all of these variables. Note that there
# are a few limitations with UEFI or Windows:
#
# - ahci-* devices must be in slots 3, 4, 5, or 6.
# - lpc must be in slot 31.
# - Some Windows versions require a CD-ROM device to be present. This can be
# an empty file, created with 'touch cdrom.iso'.
2022-09-05 15:41:54 +00:00
/usr/sbin/bhyve \
-c sockets=1,cores=$cpucores,threads=1 \
-m $ram \
-w \
-H \
-s 0,hostbridge \
2022-09-06 14:28:44 +00:00
-s 1,virtio-blk,$vmpath/${vmname}_disk0.img \
2022-09-06 20:53:11 +00:00
-s 2,virtio-net,$netif \
$cdrom \
2022-09-06 15:29:43 +00:00
-s 29,fbuf,tcp=${vnc},w=800,h=600 \
2022-09-05 15:41:54 +00:00
-s 30,xhci,tablet \
-s 31,lpc \
2022-09-05 15:41:54 +00:00
-l com1,$comoutput \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd \
$vmname