Bhyve started from tmux

I have been messing around with different ways to start VM's with Bhyve.
My latest works good. I use the stdio console and sysutils/tmux.
/etc/rc.local
Code:
#!/bin/sh
bhyveload -m 8G -S -d /vm/freebsd/freebsd1.img freebsd1
sleep 2
bhyveload -m 4G -S -d /vm/freebsd/freebsd2.img freebsd2
sleep 2
bhyveload -m 4G -S -d /vm/freebsd/freebsd3.img freebsd3
sleep 2
bhyveload -m 8G -S -d /vm/freebsd/freebsd4.img freebsd4
sleep 8
/usr/local/bin/tmux new-session -d -s freebsd1 'bhyve -S -m 8G -c 8 -AHP -s 0,hostbridge -s 1,lpc -s 2:0,ahci-hd,/vm/freebsd/freebsd1.img -s 5:0,passthru,132/0/0 -s30,xhci,tablet -l com1,stdio freebsd1'
sleep 2
/usr/local/bin/tmux new-session -d -s freebsd2 'bhyve -S -m 4G -c 4 -AHP -s 0,hostbridge -s 1,lpc -s 2:0,ahci-hd,/vm/freebsd/freebsd2.img -s 5:0,passthru,132/0/1 -s30,xhci,tablet -l com1,stdio freebsd2'
sleep 2
/usr/local/bin/tmux new-session -d -s freebsd3 'bhyve -S -m 4G -c 4 -AHP -s 0,hostbridge -s 1,lpc -s 2:0,ahci-hd,/vm/freebsd/freebsd3.img -s 5:0,passthru,134/0/0 -s30,xhci,tablet -l com1,stdio freebsd3'
sleep 2
/usr/local/bin/tmux new-session -d -s freebsd4 'bhyve -S -m 8G -c 8 -AHP -s 0,hostbridge -s 1,lpc -s 2:0,ahci-hd,/vm/freebsd/freebsd4.img -s 5:0,passthru,134/0/1 -s30,xhci,tablet -l com1,stdio freebsd4'

To make sure my VM's shutdown correctly I add this to the hypervisor:
/etc/rc.shutdown.local
Code:
#!/bin/sh
pkill bhyve
sleep 10
bhyvectl --destroy --vm=freebsd1
bhyvectl --destroy --vm=freebsd2
bhyvectl --destroy --vm=freebsd3
bhyvectl --destroy --vm=freebsd4
tmux kill-server

So I am willing to take any criticisms. Please critique my methods.
Next I want to try and quad screen all 4 tmux sessions into one big main screen.
 
The shutdown script could probably be done a bit better. If one (or more) of your VMs take longer than 10 seconds to shutdown they're going to get forcefully powered off. So if you have a MySQL server that takes 20 seconds to shutdown you're going to power it off halfway through its shutdown and you likely end up with a corrupted filesystem and/or database.
 
After pkill bhyve instead of sleep 10 you can use a while loop to give VMs a longer time to shutdown and killing any stragglers after that. This way you only wait for long in case a vm doesn't shutdown normally. Something like
Code:
sleep 60 &
while ps | grep '[b]hyve:' >/dev/null; do
  if ps | grep '[s]leep' >/dev/null; then
    sleep 1
  else
    for vm in $(ps | grep '[b]hyve: ' | awk '{print $6}'); do
      bhyvectl --destroy --vm=$vm
    done
    break
  fi
done
 
Back
Top