Vagrant a high-level open-source tool for building and maintaining virtual environments. With the use of plugins, Vagrant can manage many underlying VM technology, like VirtualBox, KVM, etc. In this article we will configure Vagrant with KVM/QEMU.
Install vagrant
using either pamac
…
$ pamac install vagrant
…or pacman
:
$ sudo pacman -Sy vagrant
You can verify vagrant is installed by checking the version number.
[andy@home-pc ~]$ vagrant version Installed Version: 2.2.19 Latest Version: 2.2.19 You're running an up-to-date version of Vagrant!
You can also take a look at the help page using the -h or –help options for an idea of how to use this command.
$ vagrant -h
You can also use the help option on sub commands, like plugin for example.
[andy@home-pc ~]$ vagrant plugin -h Usage: vagrant plugin <command> [<args>] Available subcommands: expunge install license list repair uninstall update
Above we can see that we can list what plugins are installed. On my system, we can see that I currently have no plugins installed.
[andy@home-pc ~]$ vagrant plugin list No plugins installed.
Install the vagrant-libvirt
plugin.
[andy@home-pc ~]$ vagrant plugin install vagrant-libvirt
Create a directory in your home where you will keep your vagrant files.
[andy@home-pc ~]$ mkdir -v ~/Vagrant mkdir: created directory '/home/andy/Vagrant' [andy@home-pc ~]$ cd ~/Vagrant [andy@home-pc Vagrant]$
Here I’m looking to install Alma Linux 8. Navigate to the below URL and search for your preferred distro.
https://app.vagrantup.com/almalinux/boxes/8
Use the vagrant init
command to generate the Vagrant file.
[andy@home-pc Vagrant]$ vagrant init almalinux/8 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
Here we can see its generated the Vagrantfile
.
[andy@home-pc Vagrant]$ ls -l total 4 -rw-r--r-- 1 andy andy 3017 Jul 12 00:09 Vagrantfile
This file contains a lot of useful information that you can read using less.
[andy@home-pc Vagrant]$ less Vagrantfile
Ultimately though, it creates file containing the following. Note – the below command uses egrep -v
to strip out the comment:
$ egrep -v "^[[:space:]]*$|#" Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "almalinux/8" end
With that file now in place, you should be able to create and start the VM using vagrant up
.
[andy@home-pc Vagrant]$ vagrant up
This should start up the machine and provide output in relation to what its doing:
[andy@home-pc Vagrant]$ vagrant up
Bringing machine 'default' up with 'libvirt' provider...
==> default: Box 'almalinux/8' could not be found. Attempting to find and install...
default: Box Provider: libvirt
default: Box Version: >= 0
==> default: Loading metadata for box 'almalinux/8'
default: URL: https://vagrantcloud.com/almalinux/8
==> default: Adding box 'almalinux/8' (v8.6.20220513) for provider: libvirt
default: Downloading: https://vagrantcloud.com/almalinux/boxes/8/versions/8.6.20220513/providers/libvirt.box
default: Calculating and comparing box checksum...
==> default: Successfully added box 'almalinux/8' (v8.6.20220513) for 'libvirt'!
==> default: Uploading base box image as volume into Libvirt storage...
==> default: Creating image (snapshot of base box volume).
==> default: Creating domain with the following settings...
==> default: -- Name: Vagrant_default
==> default: -- Description: Source: /home/andy/Vagrant/Vagrantfile
==> default: -- Domain type: kvm
==> default: -- Cpus: 1
==> default: -- Feature: acpi
==> default: -- Feature: apic
==> default: -- Feature: pae
==> default: -- Clock offset: utc
==> default: -- Memory: 512M
==> default: -- Management MAC:
==> default: -- Loader:
==> default: -- Nvram:
==> default: -- Base box: almalinux/8
==> default: -- Storage pool: default
==> default: -- Image(vda): /var/lib/libvirt/images/Vagrant_default.img, virtio, 20G
==> default: -- Disk driver opts: cache='default'
==> default: -- Kernel:
==> default: -- Initrd:
==> default: -- Graphics Type: vnc
==> default: -- Graphics Port: -1
==> default: -- Graphics IP: 127.0.0.1
==> default: -- Graphics Password: Not defined
==> default: -- Video Type: cirrus
==> default: -- Video VRAM: 16384
==> default: -- Video 3D accel: false
==> default: -- Sound Type:
==> default: -- Keymap: en-us
==> default: -- TPM Backend: passthrough
==> default: -- TPM Path:
==> default: -- INPUT: type=mouse, bus=ps2
==> default: Creating shared folders metadata...
==> default: Starting domain.
==> default: Waiting for domain to get an IP address...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 192.168.121.19:22
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Rsyncing folder: /home/andy/Vagrant/ => /vagrant
You can check the ssh configuration with the following:
[andy@home-pc Vagrant]$ vagrant ssh-config
Host default
HostName 192.168.121.19
User vagrant
Port 22
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /home/andy/Vagrant/.vagrant/machines/default/libvirt/private_key
IdentitiesOnly yes
LogLevel FATAL
Which means you can login with:
$ ssh -i .vagrant/machines/default/libvirt/private_key [email protected]
…or simply with:
$ vagrant ssh [vagrant@localhost ~]$
You can check the status using vagrant from within the ~/Vagrant
directory we created earlier like so.
[andy@home-pc Vagrant]$ vagrant status
Current machine states:
default running (libvirt)
From any directory – you can check the status with the global-status
option.
[andy@home-pc Vagrant]$ cd / && vagrant global-status
id name provider state directory
----------------------------------------------------------------------
7a4d4c7 default libvirt running /home/andy/Vagrant
You can also globally (from any directory) control your VM with vagrant – like shut it down using the halt
command. However, here we do have to provide the ID.
[andy@home-pc /]$ vagrant halt 7a4d4c7
==> default: Attempting graceful shutdown of VM...
To completely delete a VM – you can use the destroy option. Again, this can be done globally using the ID as above, or from within the directory we created earlier.
[andy@home-pc Vagrant]$ vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Removing domain...
==> default: Deleting the machine folder
Be the first to comment