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¶
sudo dnf install @vagrant
https://developer.fedoraproject.org/tools/vagrant/vagrant-libvirt.html
Also check the libvert installation page.
Warning
You must run vagrant commands with sudo
access.
CPU Virtualisation¶
Redacted
Other Optional Tweaks¶
To make life easier with Vagrant I also suggest doing the following:
#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:
mkdir -p /opt/Vagrant/my-machine
cd /opt/Vagrant/my-machine
Then in that directory, make a file called Vagrantfile
with the contents:
#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
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
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
sudo bash
Destroy¶
To destroy your machine, you simply
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
:
#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
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 tohyperv_feature
- Help link
Accessing Front End of Windows¶
Once the machine has built, you can access the front end via virt-manager
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:
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
List All Machines¶
sudo virsh list --all
Which gives you output like:
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:
#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.
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.
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.
Using Vagrant with LXC¶
As an alternative to libvert
we can use lxc
to create the vagrant machines.
Install Plugin¶
vagrant plugin install vagrant-lxc
You may need to remove the vagrant-libvert
plugin, if this was installed as a system dependency use dnf
to uninstall.
Example Vagrant File¶
#Set the machine name
$machineName="example"
#https://app.vagrantup.com/
$vagrantBox="magneticone/centos-7"
#Provide BASH commands to do basic provisioning
$script = <<BASH
echo "run any commands you want with this script"
BASH
Vagrant.require_version ">= 1.7.0"
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'lxc'
Vagrant.configure("2") do |config|
config.vm.box = $vagrantBox
config.vm.hostname=$machineName
config.vm.define $machineName do |t|
t.vm.host_name=$machineName
config.vm.provider :lxc do |lxc|
lxc.container_name = :machine
end
end
config.vm.provision "shell",
inline: $script
end