Skip to content

Instantly share code, notes, and snippets.

@bsdlme
Last active October 9, 2024 04:19
Show Gist options
  • Save bsdlme/14a46c568b2917206e9d84356e1a4303 to your computer and use it in GitHub Desktop.
Save bsdlme/14a46c568b2917206e9d84356e1a4303 to your computer and use it in GitHub Desktop.
Setting up vagrant-bhyve on FreeBSD

Using bhyve with vagrant

The following describes how to set up bhyve with Vagrant using the vagrant-bhyve plugin.

Prerequisites

You will need

  • FreeBSD >= 11.0
  • A bhyve [capable CPU] 1
  • For this tutorial a clean /etc/pf.conf (XXX check if that's really needed)
  • A working internet connection to download the vagrant boxes

Preparing the system

To use bhyve you first need to load the vmm.ko kernel module:

# kldload vmm

If you want to load this module at boot time, add it to the list of modules in /etc/rc.conf:

# sysrc kld_list+="vmm"

Install a couple of needed packages:

# pkg install sudo dnsmasq linuxfdisk qemu-utils vagrant rubygem-vagrant-mutate rubygem-vagrant-bhyve grub2-bhyve coreutils linuxfdisk

Install the vagrant plugins

I do not know if these steps are correct (we already installed the vagrant-bhyve and vagrant-mutate packages in the last step) but without installing the plugins the bhyve provider cannot be found.

$ vagrant plugin install vagrant-bhyve
$ vagrant plugin install vagrant-mutate

Download a (Virtualbox) vagrant box

Now it's time to download a vagrant box, for example a Ubuntu 14.04 (Trusty Tahr) box:

$ vagrant box add ubuntu/trusty64 --provider virtualbox

Convert the box from Virtualbox to bhyve

We only have a Virtualbox box, so we need to convert (mutate) the box into the bhyve format and remove the Virtualbox one:

$ vagrant mutate ubuntu/trusty64 bhyve
$ vagrant box remove ubuntu/trusty64 --provider virtualbox

Initialize and start your first vagrant-bhyve box

We now start the first vagrant-bhyve box!

$ mkdir -p ~/vagrant/trusty64
$ cd ~/vagrant/trusty64
$ vagrant init ubuntu/trusty64
$ vagrant up --provider bhyve

You should see some output that the box is being started. Depending on your sudo(1) configuration you are prompted for your user password, because some tasks (starting pf, dnsmasq and bhyve) need root credentials.

SSH'ing into the box

The last step is to ssh in to the freshly created and started box:

$ vagrant ssh

And voilà , you should be greeted by the Ubuntu VM.

@network-shark
Copy link

network-shark commented Sep 6, 2019

I'm using FreeBSD 12-p7 and I can't install the vagrant bhyve plugin. Do you know this problem ?

Which Versions are you using ?

DEBUG bundler: Removing activation request from install. Already installed. (winrm-elevated-1.1.0)
DEBUG bundler: Removing activation request from install. Already installed. (wdm-0.1.1)
DEBUG bundler: Removing activation request from install. Already installed. (vagrant-2.2.5)
DEBUG bundler: Removing activation request from install. Already installed. (vagrant-mutate-1.2.0)
DEBUG bundler: Installing required gems.
ERROR warden: Error occurred: undefined method name' for nil:NilClass INFO warden: Beginning recovery process... INFO warden: Calling recover: #<VagrantPlugins::CommandPlugin::Action::InstallGem:0x00000008022b9d28> INFO warden: Recovery complete. INFO warden: Beginning recovery process... INFO warden: Recovery complete. INFO environment: Running hook: environment_unload INFO runner: Preparing hooks for middleware sequence... INFO runner: 1 hooks defined. INFO runner: Running action: environment_unload #<Vagrant::Action::Builder:0x0000000812283448> Traceback (most recent call last): 18: from /usr/local/bin/vagrant:23:in

'
17: from /usr/local/bin/vagrant:23:in load' 16: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/bin/vagrant:187:in <top (required)>'
15: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/lib/vagrant/environment.rb:290:in cli' 14: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/lib/vagrant/cli.rb:66:in execute'
13: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/plugins/commands/plugin/command/root.rb:66:in execute' 12: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/plugins/commands/plugin/command/install.rb:69:in execute'
11: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/plugins/commands/plugin/command/install.rb:69:in each' 10: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/plugins/commands/plugin/command/install.rb:70:in block in execute'
9: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/plugins/commands/plugin/command/base.rb:14:in action' 8: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/lib/vagrant/action/runner.rb:102:in run'
7: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/lib/vagrant/util/busy.rb:19:in busy' 6: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/lib/vagrant/action/runner.rb:102:in block in run'
5: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/lib/vagrant/action/builder.rb:116:in call' 4: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in call'
3: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/before_trigger.rb:23:in call' 2: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in call'
1: from /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/plugins/commands/plugin/action/install_gem.rb:30:in call' /usr/local/lib/ruby/gems/2.5/gems/vagrant-2.2.5/lib/vagrant/plugin/manager.rb:156:in install_plugin': undefined method `name' for ni
l:NilClass (NoMethodError)

@bsdlme
Copy link
Author

bsdlme commented Sep 8, 2019

Sorry, I only played a bit with vagrant and bhyve a long time ago.
Maybe you can find help on one of FreeBSD's mailing lists.

@f-andrey
Copy link

This plugin seems to be very outdated and no longer works with ruby 2.x and actual vagrant :(

@mcontos
Copy link

mcontos commented Mar 28, 2024

To get it to work I had to use vagrant version 2.3.6 and ruby 3.2.3.

This will help
https://developer.hashicorp.com/vagrant/docs/installation/source

@bsdlme
Copy link
Author

bsdlme commented May 7, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment