Stockton Web & Cloud Services Company Articles Get Started 801-360-8331
We are an 🇺🇸American🇺🇸 small business! Help us grow! Share with a friend. We have fast response times and very reasonable prices.
Vultr Cloud for your next cloud project!
Virtual Machines in FreeBSD

Virtual Machines in FreeBSD

Although our infrastructures runs off FreeBSD and OpenBSD, admittedly, sometimes I need to dabble into Linux for some programs. This is mainly because web developers typically aren't targeting their builds to run on a FreeBSD system.

So, when I need to run Linux on something, I've turned to FreeBSD's bhyve hypervisor. Here is a quick few steps to get setup, using vm-bhyve:

# pkg install vm-bhyve
# sysrc vm_enable="YES"
# zfs create -o mountpoint=/vm zroot/vm
# sysrc vm_dir="zfs:zroot/vm"
# vm init

Those steps should get you initialized to start testing things out. You might want to create a virtual switch as wll, using vm switch create. In my case, I already have a bridge configured that I wanted to use, so I used this:

# vm switch create -t manual -b bridge0 public

I also happed to already have an image file with a copy of Alpine Linux installed on it, so here is how I configured the vm:

# zfs create alpine
# zfs configure alpine

That takes you to the configuration file for the vm, and here is roughly what I set:

loader="uefi"
graphics="yes"
graphics_listen="0.0.0.0"
graphics_port="5900"
graphics_vga=io

cpu=2
memory=2048M

network0_type="virtio-net"
network0_switch="public"

disk0_type="virtio-blk"
disk0_name="/path/to/alpine.img"
disk0_dev="custom"

uuid="69e879d0-9c4f-4e12-83af-da07b29e9528"
network0_mac="58:9c:fc:09:c3:f3"

disk1_type="virtio-9p"
disk1_name="shared=/path/to/shared_dir/"
disk1_dev="custom"

So I disabled the bhyveload loader, and set the UEFI one, with a VNC listener.

You also might notice the 9p share in there. In the /etc/fstab file in Alpine, I have this line, to mount that shared directory into the vm:

shared     /mnt     9p     trans=virtio,version=9p2000.L   0       0

You use the 9p share name (in this case, "share") and the path where you want to mount it.

If you're looking to use virtual machines in FreeBSD, I hope this helps get you started on a good path.

It should be noted, you don't have to use this vm-bhyve package. I have used scripts to manually start/stop/install virtual machines just using the bhyve and bhyvectl utilities.

Be Strong, and of a Good Courage.

Share X.com Truth Social

Written by Jon

Author Profile Picture

Hi, I'm Jon. I live in Utah with my awesome wife and children, where we enjoy hockey, basketball, soccer, and raising chickens! I have a bachelors degree in Software Development, various computer & project management certifications, and I've worked for web hosting and other dev/online companies for over a decade.