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
2
3
4
5
vagrantVersion=$(curl -s https://releases.hashicorp.com/vagrant/ | grep vagrant | head -n 1 | cut -d '_' -f 2 | cut -d '<' -f 1)
sudo dnf install -y libvirt-daemon-kvm qemu-kvm libvirt-devel
sudo dnf install -y https://releases.hashicorp.com/vagrant/${vagrantVersion}/vagrant_${vagrantVersion}_x86_64.rpm
sudo vagrant plugin install vagrant-libvirt
sudo vagrant plugin install vagrant-sshfs

CPU Virtualisation

In order to run certain OS's you'll need to enable your CPU's virtualisation support in the BIOS.

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.

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

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