Virtualization is a very old technology but still finds high use, especially in cloud computing. It involves creating software-based or virtual versions of computer resources such as storage, networks, applications, and servers. This makes it possible to partition a single server into multiple virtual instances that run and interact independently.
There are mainly two types of virtualization. These are:
- Type 1/Bare Metal virtualization where the hypervisor is installed directly on top of the physical machine. For example Microsoft Hyper-V, open-source Kernel-based VMs (KVMs), VMware ESXi
- Type 2/Hosted virtualization where the hypervisor is installed on an existing operating system. For example VMware Workstation and Oracle VirtualBox
In this guide, we will discuss how to install and use Vagrant With VirtualBox / KVM on Rocky Linux 9.
Vagrant is an open-source tool that can be used to build and manage virtual machine environments in a single workflow. It provides the simplest and easiest command line way to manage virtual environments. This reduces the development environment setup time while increasing the production parity.
Vagrant is preferred due to the following features:
- It is built on top of industry-standard technology and is controlled by a single consistent workflow.
- It offers simplicity when setting up an environment.
- It standardizes environments across platforms
- Virtual machines can easily be provisioned on top of VirtualBox, VMware, AWS, or any other provider.
- It is a cross-platform tool
There are terminologies involved when dealing with Vagrant. These are:
- Vagrant Box: This is the Vagrant setup unit. just like docker, Vagrant is a self-contained image. This is a packaged Vagrant environment.
- Vagrant File: This is the configuration file for Vagrant. It contains the configurations used to create a VM.
- Provider: is the location in which the virtual environment runs, the default option is Virtualbox but can be set to any other tool such as KVM, docker e.t.c
Now let’s plunge in and enjoy the awesomeness of this tool!
Before you Begin
Before we begin, you need a hypervisor installed on your Rocky Linux 9 machine. For this case, we will use KVM or Virtualbox, which can be installed with the aid below;
1. Install Vagrant on Rocky Linux 9
Once a preferred hypervisor has been installed, proceed and install Vagrant on Rocky Linux 9.
To achieve this, begin by adding the Vagrant repository to the system.
sudo yum install yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
Install Vagrant on Rocky Linux 9 with the command:
sudo yum install vagrant
Dependency Tree:
Dependencies resolved.
=======================================
Package
Arch Version Repo Size
=======================================
Installing:
vagrant
x86_64 2.3.0-1 hashicorp 108 M
Transaction Summary
=======================================
Install 1 Package
Total download size: 108 M
Installed size: 257 M
Is this ok [y/N]: y
2. Using Vagrant with VirtualBox / KVM on Rocky Linux 9
Once installed, Vagrant can be used to create/provision Virtual Machine on your hypervisor.
First, create a working directory for Vagrant and navigate into it:
mkdir Vagrant && cd Vagrant
Install the Vagrant Libvirt Provider(KVM Only)
For KVM, you need to download the Vagrant plugin. First, enable the PowerTools CRB repo:
sudo dnf install epel-release
sudo dnf config-manager --set-enabled crb
Install the required build tools:
sudo dnf install gcc make perl kernel-devel kernel-headers bzip2 dkms elfutils-libelf-devel
sudo yum groupinstall "Development Tools
sudo dnf install libvirt-devel
Install the Vagrant Libvirt Provider
vagrant plugin install vagrant-libvirt
##OR
CONFIGURE_ARGS="with-libvirt-include=/usr/include/libvirt with-libvirt-lib=/usr/lib64" vagrant plugin install vagrant-libvirt
Verify the installation with the command:
$ vagrant plugin list
vagrant-libvirt (0.12.1, global)
Configure and Initialize the VM
Now download a Vagrant box file for the desired VM and set the provider. For example Ubuntu 20.04
##For VirtualBox
vagrant box add generic/ubuntu2204 --provider=virtualbox
##For KVM
vagrant box add generic/ubuntu2204 --provider=libvirt
Create a Vagrant file for the VM using the init command:
$ vagrant init generic/ubuntu2004
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.
You can view the Vagrantfile:
cat Vagrantfile
Start the VM with the command:
vagrant up
Sample Output:
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'generic/ubuntu2004'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'generic/ubuntu2004' version '4.1.6' is up to date...
==> default: Setting the name of the VM: Vagrant_default_1660834980416_30148
.....
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: Checking for guest additions in VM...
After this, you will have the VM created and running on your hypervisor.
For VirtualBox
For KVM
Creating a Custom Vagrantfile
It is also possible to create your own Vagrantfiles to run the VMs. This can be done as shown:
vim Vagrantfile
Add the lines below replacing them appropriately:
# -*- mode: ruby -*-
# vi: set ft=ruby :
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
Vagrant.configure("2") do |config|
##### DEFINE VM #####
config.vm.define "centos01" do |config|
config.vm.hostname = "centos7.example.com"
config.vm.box = "centos/7"
config.vm.box_check_update = true
end
end
Now start the VM with the command:
vagrant up
SSH into the machine
You can ssh to the VM with the Vagrant command below:
$ vagrant ssh
vagrant@ubuntu2004:~$
You are now set to interact with your VM as preferred. To terminate the session. use CTRL+D or the logout command:
$ logout
Connection to 192.168.121.125 closed.
File shares
Synced/shared folders are configured within your Vagrantfile using the config.vm.synced_folder
parameter. For example, we can choose to share the local vagrant folder by adding the lines below to the Vagrantfile.
Vagrant.configure("2") do |config|
# other config here
config.vm.synced_folder "/home/rocky9/Vagrant/", "/vagrant"
......
end
The first parameter represents the path on the local machine /home/rocky9/Vagrant/ for this case and the second is the path to be created on the guest machine
Reload the VM to apply the changes
vagrant reload
Vagrant will automatically mount /vagrant directory which is synced with the directory containing the Vagrantfile on your host system. This can be verified once you SSH into the VM as shown:
$ vagrant ssh
$ ls /vagrant/
Vagrantfile
To test if you can sync files between the host and guest systems, add a new file in the VM’s vagrant directory:
vagrant@vagrant:~$ touch /vagrant/share_test
End the session:
$ exit
Verify if the file exists in the local vagrant directory:
$ ls
share_test Vagrantfile
Ansible Scripts
It is also possible to provision VMS using Ansible playbooks. The Vagrant Ansible provisioner is the tool that allows you to perform all this.
You need to have:
- Ansible installed on your Vagrant host.
- a recent version of OpenSSH that supports ControlPersist on your Vagrant host.
Now you can have the simplest Vagrantfile configuration as shown:
Vagrant.configure("2") do |config|
#
# Run Ansible from the Vagrant Host
#
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end
More on how to use the Vagrant Ansible provisioner has been covered in the guide below:
3. Teardown an Environment
We have explored how to quickly set up a development environment. It is time we know how to stop, shut down and delete the environment.
List the available vagrant boxes with the command:
$ vagrant box list
centos/7 (libvirt, 2004.01)
centos/8 (libvirt, 1905.1)
generic/ubuntu2004 (libvirt, 3.0.20)
Suspend a VM
Suspending a VM stop and save the current running state. The command below can be used to suspend a VM:
$ vagrant suspend
==> default: Saving VM state and suspending execution...
To resume, you need to issue the command:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'generic/ubuntu2004' version '4.1.6' is up to date...
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
The VM resumes with the state where you left off.
Halt a VM
Halting gracefully shuts down the guest operating system and powers off the VM. The command for this is:
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
When you power on the VM with the vagrant up
command, it takes time to start from a cold boot.
Destroy a VM
To remove all traces of the VM from your system you use the destroy
command. This command will stop/power off the VM, and reclaim the disk space and RAM used.
The command used is as shown below:
$ vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] y
You will be prompted to confirm for the VM to be destroyed.
Conclusion
That marks the end of this amazing guide on how to use Vagrant With VirtualBox / KVM on Rocky Linux 9. At this point, you should be able to provision and manage a VM on VirtualBox / KVM using Vagrant. I hope this was important to you.
See more:
How To Manage Podman Containers With Vagrant