Vagrant on Fedora 22 - Part 1

This post is a quick guide for installing and using Vagrant on Fedora 22.

Update:
Part 2 : http://blog.raghavendratalur.in/2015/09/vagrant-on-fedora-22-part-2.html

Introduction

      $VAGRANT_HOME

  • Global config of vagrant exists in $VAGRANT_HOME.
  • Default points to ~/.vagrant.d.
  • This is where all boxes are stored
  • This is where all plugins are installed.
  • If you don't like huge amount of data being stored under dot folders, you can point it to a different location by exporting it in .bashrc. For example, add the following line to ~/.bashrc.
export VAGRANT_HOME="$HOME/vagrant/data"


      $VAGRANT_DEFAULT_PROVIDER

  • We will be mainly using libvirt as the provider, so let's make it the default provider. Add the following line to ~/.bashrc.
export VAGRANT_DEFAULT_PROVIDER=libvirt in your ~/.bashrc.


If you added any of the above lines, execute this command before proceeding.
$ source ~/.bashrc

For more info on environment variables that vagrant uses read this: https://docs.vagrantup.com/v2/other/environmental-variables.html

Installation


Some prerequisites, let's install them first.
# sudo dnf groupinstall "Development Tools"
# sudo dnf install ruby-devel gcc-c++ ruby-libvirt libvirt-devel

Now, install vagrant
# sudo dnf install vagrant.noarch vagrant-doc.noarch

Let us install few plugins that we will use
$ vagrant plugin install vagrant-libvirt
$ vagrant plugin install vagrant-lxc
$ vagrant plugin install vagrant-cachier
$ vagrant plugin install vagrant-mutate
$ vagrant plugin install vagrant-timezone

Adding a vagrant box


Download fedora 22 box from getfedora website.
$ vagrant box add https://download.fedoraproject.org/pub/fedora/linux/releases/22/Cloud/x86_64/Images/Fedora-Cloud-Base-Vagrant-22-20150521.x86_64.vagrant-libvirt.box --name fedora22

Libvirt administration and vagrant-libvirt


libvirt asks for password every time a domain(VM) is brought up. To authorize vagrant group to start and stop VMs, create a file "10-vagrant-libvirt.rules" under /usr/share/polkit-1/rules.d/ with the following content.
NOTE: Create rules.d if it does not exist.

polkit.addRule(function(action, subject) {
  if ((action.id == "org.libvirt.unix.manage"
    || action.id == "org.libvirt.unix.monitor")
    && subject.isInGroup("vagrant")) {
    return polkit.Result.YES;
  }
});

This requires that user is part of vagrant group.


Vagrant and sharing dirs/folders using NFS


When sharing directories between host and guest using NFS, a password prompt comes up.
To remove this prompt for password with nfs share add these lines to /etc/sudoers.d/vagrant

Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/tee -a /etc/exports
Cmnd_Alias VAGRANT_NFSD_CHECK = /usr/bin/systemctl status nfs-server.service
Cmnd_Alias VAGRANT_NFSD_START = /usr/bin/systemctl start nfs-server.service
Cmnd_Alias VAGRANT_NFSD_APPLY = /usr/sbin/exportfs -ar
Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /bin/sed -r -e * d -ibak /etc/exports
%vagrant ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD_CHECK, VAGRANT_NFSD_START, VAGRANT_NFSD_APPLY, VAGRANT_EXPORTS_REMOVE


Management network for vagrant-libvirt


vagrant-lbvirt creates a virtual network in libvirt for managing VMs. Its default name is vagrant-libvirt and by default uses IP in range 192.168.121.0/24. Read more here https://github.com/pradels/vagrant-libvirt#management-network .

Firewalld and virtual networks


If you have firewalld enabled then you might see some problem with nfs services etc. However, if we are going to use vagrant only for development setup then you can make your virtual networks trusted. Each command here is executed twice, with and without --permanent option.

Let us add all known services to the trusted zone in firewalld
# firewall-cmd --get-services | xargs -n 1 firewall-cmd --zone=trusted --add-service
# firewall-cmd --get-services | xargs -n 1 firewall-cmd --permanent --zone=trusted --add-service

Now, add your virtual network interfaces to trusted zone.
For my host machine, virbr0 and virbr1 are the interaces that libvirt uses. virbr0 is the default network and virbr1 is the vagrant-libvirt interface.

# firewall-cmd --zone=trusted --add-interface=virbr0
# firewall-cmd --permanent --zone=trusted --add-interface=virbr0
# firewall-cmd --zone=trusted --add-interface=virbr1
# firewall-cmd --permanent --zone=trusted --add-interface=virbr1

Create a Fedora 22 VM


We will create a simple Vagrantfile in a project directory and instruct vagrant to use the Fedora22 box that we just downloaded. There is no relation to the project directory and $VAGRANT_HOME env variable. Execute the following commands.

$ mkdir myproject
$ cd myproject
$ vim Vagrantfile

Paste the following into the file

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
        config.vm.define "myproject" do |myproject|
                myproject.vm.box = "fedora22"
                myproject.vm.hostname = "myproject"
        end
end

Save and exit.

          Start the VM
$ vagrant up

ssh into the machine and hack away!
$ vagrant ssh

Comments