bhyve Easy and best way to run FreeBSD with Xorg

Hi guys,

I am hitting my head with virtualization.

I followed the guide to use vmrun.sh to create a vm, but it looks like this method is for creating headless vm.

Now I am trying with Virtualbox, but it doesn't have any specific FreeBSD acceleration... Perhaps Libvirt?
I read that I need to built from ports to activate bhyve... What else? Qemu?

I need a vm to test out random software that might run on FreeBSD but I don't want to touch my real system, thanks... 🙏
 
Last edited:
Hi guys,

I am hitting my head with virtualization.

I followed the guide to use vmrun.sh to create a vm, but it looks like this method is for creating headless vm.

Now I am trying with Virtualbox, but it doesn't have any specific FreeBSD acceleration... Perhaps Libvirt?
I read that I need to built from ports to activate bhyve... What else? Qemu?

I need a vm to test out random software that might run on FreeBSD but I don't want to touch my real system, thanks... 🙏
bhyve is included with the base system, it has nothing to do with ports.
I would recommend sysutils/vm-bhyve to easily manage your bhyve VMs.
 
Generally I find a headless setup to work best for virtualization. Then if I want graphics:
  • If the UI I am using is decent, X11/ssh forward.
  • If the UI is bloated / crap, I use Xvnc (tigervnc).
If you want accelerated 3D / gpu passthrough then VirtualBox is easiest if using Win/Lin guests. Qemu doesn't have a Bhyve backend so is software CPU accel only. Bhyve I believe allows you to pass through the entire GPU if you have multiple on the host.

Perhaps just LLVMpipe is good enough for you for a 3D software renderer?
 
Perhaps Libvirt?
No that won't get you a GUI.
Problem is Bhyve is geared to command line.

This as a good place to start but I don't prefer that method. Jump ahead in the manual to 24.7.4 and checkout UEFI guest. That is probably the most practical example.
/usr/share/examples/bhyve/vmrun.sh -h

Libvirt has a GUI program for setting up VM's but not running one.

When I did GUI in VM I had separate video cards for each VM. Along with USB card for each VM.
And you need VT-d//IOMMU instructions on your CPU for PCI Passthrough.

So its pretty complex going hardware route. TigerVNC route might be better for you.
 
This is one little tweak if you are interested in a single VM GUI Bhyve experience.

If you run a NVidia/AMD Graphics card on your host machine you may have a spare Intel GPU that you are not using.

There is driver to send that through to a Bhyve VM.


 
Is the VNC server provided by bhyve or do I have to install a vnc server into the VM?

I don't need GPU passthrough, I only need to test some software in a safe way.
 
Bhyve does provide a VNC server but it is tied to their virtual GPU driver and scrapes the framebuffer from it. This is way more layers.

So, as long as you have CPU accel (and virtio network drivers), I believe tigervnc's Xvnc installed on the guest will still be faster. Its also much smaller than a full fat Xlibre/Xorg (though you likely want one of them too for the libs / utilities).
 
Bhyve does provide a VNC server but it is tied to their virtual GPU driver and scrapes the framebuffer from it. This is way more layers.

So, as long as you have CPU accel (and virtio network drivers), I believe tigervnc's Xvnc installed on the guest will still be faster. Its also much smaller than a full fat Xlibre/Xorg (though you likely want one of them too for the libs / utilities).

This is true... It is just convoluted, I haven't figured it out in my head yet...
 

The guide you gave made the process more straightforward, however I can't connect the VNC to the VM:

Code:
doas vm list
NAME     DATASTORE  LOADER     CPU  MEMORY  VNC  AUTO  STATE
freebsd  default    bhyveload  2    4G      -    No    Running (35875)

1767194096754.png


This is not the IP of my VM though, but I don't have a clue to solve this... 😓

Thanks... 🙏
 
If use "UEFI" ; I get errors and I am unable to connect:

Code:
doas vm list
NAME     DATASTORE  LOADER  CPU  MEMORY  VNC           AUTO  STATE
freebsd  default    uefi    2    4G      0.0.0.0:5900  No    Locked (gpc)
vm ) 
doas vm console freebsd
/usr/local/sbin/vm: ERROR: unable to locate console device for this virtual machine

The switch is working:

Code:
doas vm switch list
NAME    TYPE      IFACE      ADDRESS  PRIVATE  MTU  VLAN  PORTS
public  standard  vm-public  -        no       -    -     ue0

Perhaps is an error on the template?

Code:
loader="uefi"
graphics="yes"
graphics_res="1280x720"
xhci_mouse="yes"
cpu=2
memory=4G
network0_type="virtio-net"
network0_switch="public"
disk0_type="nvme"
disk0_name="disk0.img"

Can I run a FreeBSD graphical session?
 
My script
%> cat /usr/local/bin/vmvnc
#!/bin/sh
# vmvnc — ouvre un bureau Openbox via TigerVNC dans une VM FreeBSD (bhyve) via SSH (tunnel)
# - Pas d’auth VNC (SecurityTypes None) : on se repose sur le tunnel SSH
# - Le serveur VNC dans la VM écoute uniquement en localhost (-localhost)
# - Force /bin/sh côté VM (pas de csh), évite les redirections csh ambiguës

VM_HOST="xxx.xxx.xx.x" # IP/nom de la VM
VM_USER="OP" # utilisateur dans la VM
GEOM="1920x1080" # résolution
LPORT="5905" # port local (hôte)
RPORT="5901" # port VM (display :1)
VIEWER="vncviewer" # tigervnc-viewer
SSH_OPTS='-o BatchMode=yes -o PreferredAuthentications=publickey -o PubkeyAuthentication=yes -o ConnectTimeout=5'

set -eu

say() { printf '%s\n' "$*"; }
die() { printf 'ERREUR: %s\n' "$*" >&2; exit 1; }

wait_ssh() {
i=0
while ! ssh $SSH_OPTS -o ConnectTimeout=3 "${VM_USER}@${VM_HOST}" true 2>/dev/null; do
i=$((i+1)); [ $i -ge 20 ] && return 1
sleep 1
done
}

say "[0/4] Attente SSH sur ${VM_HOST}…"
wait_ssh || die "SSH injoignable sur ${VM_HOST} (réseau/sshd ?)"

say "[1/4] Vérification/démarrage VNC côté VM…"
# IMPORTANT : on exécute /bin/sh directement côté VM via -s (stdin), pour ne JAMAIS passer par csh.
# On injecte ${GEOM} depuis l’hôte (expansion locale dans le heredoc non-quoté).
ssh $SSH_OPTS "${VM_USER}@${VM_HOST}" /bin/sh -s <<EOF || die "Impossible de démarrer vncserver dans la VM."
PATH=/usr/local/bin:/usr/bin:/bin

# Nettoyage d’un ancien :1 (verrous + pid)
vncserver -kill :1 >/dev/null 2>&1 || true
rm -f /tmp/.X1-lock "\$HOME"/.vnc/*:1.pid /tmp/.X11-unix/X1 >/dev/null 2>&1 || true

# xstartup minimal (Openbox) si absent
if [ ! -x "\$HOME/.vnc/xstartup" ]; then
mkdir -p "\$HOME/.vnc"
printf "%s\n" "#!/bin/sh" "exec openbox-session" > "\$HOME/.vnc/xstartup"
chmod +x "\$HOME/.vnc/xstartup"
fi

# Si un serveur tourne déjà, ne pas échouer
if vncserver -list 2>/dev/null | grep -qE '^[[:space:]]*:1[[:space:]]'; then
exit 0
fi

# Démarrage TigerVNC (sans mot de passe VNC; on se repose sur le tunnel SSH)
vncserver :1 -geometry ${GEOM} -localhost -SecurityTypes None
EOF

say "[2/4] Ouverture du tunnel SSH localhost:${LPORT} → ${VM_HOST}:localhost:${RPORT}…"
if sockstat -4 -l | awk '{print $6}' | grep -q "^127\.0\.0\.1:${LPORT}\$"; then
say " (déjà ouvert)"
TUN_PID=""
else
ssh -f -N -L "${LPORT}:localhost:${RPORT}" $SSH_OPTS "${VM_USER}@${VM_HOST}" || die "Tunnel SSH échoué."
sleep 0.3
TUN_PID="$(pgrep -f "ssh -f -N -L ${LPORT}:localhost:${RPORT} ${VM_USER}@${VM_HOST}" || true)"
fi

say "[3/4] Lancement du viewer → localhost:${LPORT}"
command -v "$VIEWER" >/dev/null 2>&1 || die "Viewer VNC introuvable: pkg install -y tigervnc-viewer"
"$VIEWER" "localhost:${LPORT}"

say "[4/4] Nettoyage côté VM et fermeture tunnel…"
# On nettoie :1 proprement (sans passer par csh) ;
# ne pas échouer si déjà stoppé
ssh $SSH_OPTS "${VM_USER}@${VM_HOST}" /bin/sh -s <<'CLEAN_SH' || true
PATH=/usr/local/bin:/usr/bin:/bin
vncserver -kill :1 >/dev/null 2>&1 || pkill -f "Xvnc :1" >/dev/null 2>&1 || true
CLEAN_SH

# Fermer le tunnel si créé par ce script
if [ -n "${TUN_PID:-}" ]; then
kill "$TUN_PID" >/dev/null 2>&1 || true
fi

say "Terminé."
Sorry, comments in French.
 
I confirm the issue is with the UEFI loader.

It looks vm-bhyve is not meant to run FreeBSD graphical session...


☹️
 
I can't even perform the installation, unless I install the OS in bhyveload, and when I finish the installation I switch to UEFI[/code].

Pretty weird...
Really weird... For me I can install directly into UEFI just fine.
What vm command do you exactly use for the installation?
 
Back
Top