Vagrant

Vagrant is a great tool for managing and building local development VMs

It can manage different virtualisation technologies however the two of note are Virtualbox and libvirt.

As we are on Fedora and have libvirt for free (without any messing about) we will use libvirt

Installing Vagrant and Libvirt

1
sudo dnf install @vagrant

https://developer.fedoraproject.org/tools/vagrant/vagrant-libvirt.html

CPU Virtualisation

Redacted

Other Optional Tweaks

To make life easier with Vagrant I also suggest doing the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#set up an alias for vagrant to always run as sudo
echo "alias vagrant='sudo vagrant'" >> ~/.bashrc

#resource your bashrc to bring in that alias
source ~/.bashrc

#update sudo so that no password required when running vagrant
echo "$(whoami) ALL=(ALL) NOPASSWD:/$(which \vagrant)" | sudo tee /etc/sudoers.d/vagrant

#syntax highlight Vagrantfile in vim
echo '

" ============ Vagrantfile syntax highlighting =============

au BufRead,BufNewFile Vagrantfile set filetype=ruby

' >> ~/.vimrc

Buidling a Vagrant Machine

Here is how to use Vagrant to build a local VM

Vagrantfile

To build a vagrant machine, we need to create a Vagrantfile

In a directory of your choosing, make the file - for example:

1
2
mkdir -p /opt/Vagrant/my-machine
cd /opt/Vagrant/my-machine

Then in that directory, make a file called Vagrantfile with the contents:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#Set the machine name
$machineName="lxchost"

#Provide BASH commands to do basic provisioning
$script = <<BASH
echo "Running BASH provisioning..."
yum -y install ansible
BASH

Vagrant.require_version ">= 1.7.0"
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname=$machineName
  config.vm.network "private_network", type: "dhcp"
  config.vm.define $machineName do |t|
    t.vm.host_name=$machineName
    config.vm.provider :libvirt do |v|
      v.memory = 8192
      v.cpus = 6
    end
  end
  config.vm.provision "shell",
    inline: $script
end

This file has configured Vagrant to pull centos/7 from https://app.vagrantup.com/centos/boxes/7

It has configured the hostname and also the VM name on your desktop machine

Tip

Don't use dashes or hyphens in the machine name, for an easy life

It has configured the machine to have an automatic IP address allocated

It has configured it with 8GB of RAM and 6 cores

It has then run some BASH commands, in this instance to install Ansible.

Build

To build the machine, you need to do the following

1
2
cd /path/to/Vagrantfile_Directory
sudo vagrant up

This will then give you lots of output and you should see that your machine has been built

Access via SSH

To access the machine that has been built, you simply run

1
2
cd /path/to/Vagrantfile_Directory
sudo vagrant ssh

This connects you to the machine as the vagrant user

This user has passwordless access to sudo bash, so to become root simply

1
sudo bash

Destroy

To destroy your machine, you simply

1
2
cd /path/to/Vagrantfile_Directory
sudo vagrant destroy

Building a Windows 10 Machine

Thanks to the wonderfulness of Vagrant and Libvirt, you can easily set up a local Windows 10 VM

The process is the same as described above. We will use one of these boxes.

For example, this Vagrantefile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#Set the machine name
$machineName="windows10"

Vagrant.require_version ">= 1.7.0"
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'
Vagrant.configure("2") do |config|
  config.vm.box = "peru/windows-10-enterprise-x64-eval"
  config.vm.hostname=$machineName
  config.vm.network "private_network", type: "dhcp"
  config.vm.define $machineName do |t|
    t.vm.host_name=$machineName
    config.vm.provider :libvirt do |v|
      v.memory = 8192
      v.cpus = 6
    end
  end
end

Note

You need to have installed the bleeding edge Vagrant as described above.

Note

You might need to install these plugins if you get errors

1
2
3
vagrant plugin install winrm
vagrant plugin install winrm-fsfound
vagrant plugin install winrm-elevated

  • If you get this error The following settings shouldn't exist: hyperv_feature
  • Then edit the main Vagrantfile which can be found here /root/.vagrant.d/boxes/BOX_NAME/VERSION/libvirt and remove the lines related to hyperv_feature
  • Help link

Accessing Front End of Windows

Once the machine has built, you can access the front end via virt-manager

1
dnf -y install virt-manager

Your password for each user is vagrant

Managing Libvirt Machines

Use virt-manager

By far the easiest way to manage these machines outside of Vagrant is to use virt-manager in the directory

Simply:

1
sudo dnf -y install virt-manager

Tip

For accessing the frontend of a machine - eg a Windows machine - use virt-manager

Alternatively use virsh

If for any reason you need to manage a machine that has been created without using Vagrant, you can use virsh

https://www.centos.org/docs/5/html/5.2/Virtualization/chap-Virtualization-Managing_guests_with_virsh.html

List All Machines

1
sudo virsh list --all

Which gives you output like:

1
2
3
4
5
joseph@joseph-thinkpad sudo virsh list --all
[sudo] password for joseph:
 Id    Name                           State
----------------------------------------------------
 7     ansible-scratch_lxchost        running

The Id number can then be used to manage specific machines, for example to destroy:

1
2
3
4
5
6
#The id of your machine
id=7

#Stop and remveo the machine
sudo virsh destroy $id
sudo virsh undefine $id

Virtualbox

Guest additions and synced folders

In order for synced folders to work when using Virtualbox, you will need to ensure that the guest additions are installed. If you do not have the guest additions installed correctly you may receive an error about the vboxfs filesystem being missing.

The simplest way to ensure that the guest additions are installed is to install the guest additions Vagrant plugin.

1
vagrant plugin install vagrant-vbguest

It will also be useful to add the following snippet to raise a warning if the guest additions plugin is not installed.

1
2
3
unless Vagrant.has_plugin?("vagrant-vbguest")
  raise 'vagrant-vbguest is not installed!, please run "vagrant plugin install vagrant-vbguest" '
end

Vagrant Commands

Vagrant Rsync

If you find that you need to resync the root /vagrant directory you can use vagrant rsync or vagrant rsync-auto. When used in conjunction with ansible_local provisioning you can keep your ansible project in sync and more easily run plays individually.