How to setup Virtualbox VM using vagrant

How to setup Virtualbox VM using vagrant

vagrant_virtualbox_setup

Here in this article we will try to setup a Virtual machine on VirtualBox using vagrant on top of a RHEL7 linux machine.

Test Environment

RHEL7 machine

Procedure

Step1: Download VirtualBox RPM for RHEL7

URL - http://download.virtualbox.org/virtualbox/5.1.22/VirtualBox-5.1-5.1.22_115126_el7-1.x86_64.rpm

Step2: Install VirtualBox RPM

sudo chmod 755 VirtualBox-5.1-5.1.22_115126_el7-1.x86_64.rpm

sudo yum -y install VirtualBox-5.1-5.1.22_115126_el7-1.x86_64.rpm

Step3: Verify the VirtualBox installation

Verify the rpm package that is installed.

sudo rpm -qa | grep VirtualBox

Output:

VirtualBox-5.1-5.1.22_115126_el7-1.x86_64

Check the VirtualBox binary installation location.

sudo which VirtualBox

Output:

/bin/VirtualBox

Check the version of VBoxManage.

sudo VBoxManage --version

Output:

5.1.22r115126

Step4: Download the vagrant rpm for RHEL 7

URL - https://releases.hashicorp.com/vagrant/1.9.6/vagrant_1.9.6_x86_64.rpm

Step5: Install Vagrant

sudo yum -y install vagrant_1.9.6_x86_64.rpm

Step6: Verify the Vagrant installation

Verify the rpm package installed.

sudo rpm -qa | grep vagrant

Output:

vagrant-1.9.6-1.x86_64

Verify the binary location of vagrant.

sudo which vagrant

Output:

/bin/vagrant

Verify the version of vagrant installed.

vagrant --version

Output:

Vagrant 1.9.6

Step7: Download a virtualbox for centos 7 to be run in VirtualBox environment

sudo vagrant box add bento/centos-7.2 --provider=virtualbox

Output:

==> box: Loading metadata for box 'bento/centos-7.2'

box: URL: https://vagrantcloud.com/bento/centos-7.2

==> box: Adding box 'bento/centos-7.2' (v2.3.1) for provider: virtualbox

box: Downloading: https://app.vagrantup.com/bento/boxes/centos-7.2/versions/2.3.1/providers/virtualbox.box

==> box: Successfully added box 'bento/centos-7.2' (v2.3.1) for 'virtualbox'!

Verify the location where the box is downloaded.

sudo ls -ltr ~/.vagrant.d/boxes/

Output:

drwxr-xr-x. 3 root root 37 Jul 6 00:44 bento-VAGRANTSLASH-centos-7.2

Step8: Initialize the VirtualBox to prepare the Vagrant file

sudo vagrant init bento/centos-7.2

Output:

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.

Step9: Start up the initialized VirtualBox

sudo vagrant up

Output:

Bringing machine 'default' up with 'virtualbox' provider...

==> default: Importing base box 'bento/centos-7.2'...

==> default: Matching MAC address for NAT networking...

==> default: Checking if box 'bento/centos-7.2' is up to date...

==> default: Setting the name of the VM: vagrantfiles_default_1499316779827_97949

==> default: Clearing any previously set network interfaces...

==> default: Preparing network interfaces based on configuration...

default: Adapter 1: nat

==> default: Forwarding ports...

default: 22 (guest) => 2222 (host) (adapter 1)

==> default: Booting VM...

…

There was an error while executing `VBoxManage`, a CLI used by Vagrant

for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "10d54dca-baf5-4f65-a8b7-901ad3088c97", "--type", "headless"]

Stderr: VBoxManage: error: AMD-V is being used by another hypervisor (VERR_SVM_IN_USE).
VBoxManage: error: VirtualBox can't enable the AMD-V extension. Please disable the KVM kernel extension, recompile your kernel and reboot (VERR_SVM_IN_USE)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole

Received above error while starting up the VM using vagrant. After searching through google could found that it might be due a clash between the modules that are installed for VirtualBox and KVM that is already installed on Linux machine. Was able to resolve the above error using the below export variable before running the vagrant up command

sudo export VBOX_HWVIRTEX_IGNORE_SVM_IN_USE=true
sudo vagrant up

Step10: Verify the State of the VM

sudo vagrant status

Output:

Current machine states:

default running (virtualbox)

Hope you enjoyed reading this article. Thank you..