Thursday, January 21, 2016

My Vagrant How-To (with libvirt)

I am super late the the Vagrant party. "All you have to do is vagrant up!" is all I hear from developers everywhere. But when I tried it, that was not my experience.

A lot of my life over the past several years has revolved around Linux and libvirt. I even did some backend work for a virtualization management platform called Kimchi. But since I was coming from the land of, "Here's your image, and here's your VM," I over-complicated it. "Where is the image I just downloaded?" "Doesn't my Vagrantfile need to be in the same place as that image?" The answers: "Don't worry about it," and "No."

So here's what I think is a pretty good explanation for people who are used to something like virt-manager but want to use Vagrant.

Intro: Projects, Vagrantfiles, & Boxes


Set up a directory to house your projects. You could source-control it, too. Then make subdirectories for each project:

$ mkdir vagrantfiles && cd vagrantfiles
$ mkdir someproject && cd someproject

This is before you've added any boxes. The directories will hold your `Vagrantfile`s – not your boxes. The base boxes (or .img files) live in ~/.vagrant.d/boxes, and are base images. When you set up a project, you'll be building on top of that base image. This allows sharing of one base image across multiple projects. So, pick a base you'd like to use a lot, and you'll save space on your hardrive.

Decide which box you want. You can look at available boxes from hashicorp here: https://atlas.hashicorp.com/boxes/search. Clicking on a box will show you more useful info, like the init command to use to get that box.

I want to use Fedora and libvirt (see Vagrant with Libvirt (on Fedora)), so I'm looking at https://atlas.hashicorp.com/fedora/boxes/23-cloud-base.  Let's init our project:

$ vagrant init fedora/23-cloud-base

If this is the first time you've used this box, it will download the base image. When it completes, you'll see a Vagrantfile in your directory.  If you want to do additional config, change your Vagrantfile first. The default one will get you a working VM with host networking, so that's good enough for me.

Now, up your box:
$ vagrant up --provider libvirt               # watch the config & start
$ sudo virsh list –all                               # see that your box is now running
$ vagrant ssh                                         # access your machine

For more, definitely read the Vagrant docs: https://www.vagrantup.com/docs/getting-started

Vagrant with Libvirt (on Fedora)

Installing the Plugin

There is a project to use libvirt instead of the default (Virtualbox) with Vagrant: https://github.com/pradels/vagrant-libvirt. The project instructions say to use the vagrant command to install the libvirt plugin. However, for Fedora at least, you should use dnf to install the plugin:

$ dnf install vagrant-libvirt

Box Configuration

As opposed to the Virtualbox provider, if you want to update some VM properties (e.g. VCPUs or memory), you'll need to use virsh (read: do it outside of Vagrant).

The default memory is 512M, which isn't going to cut it for my purposes. so I've gone back and updated my VM. In the future, I'll remember to add the following to my Vagrantfile *before* I 'vagrant up.'

config.vm.provider :libvirt do |libvirt|   
      libvirt.memory = 2048
end