How to setup Virtualbox VM using vagrant

How to setup Virtualbox VM using vagrant


Purpose – To setup a Virtual machine on VirtualBox using vagrant inside a RHEL7 linux machine

Pre-requisites

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

[root@desktop1 software]# ls -ltr

total 77276

-rw-r–r–. 1 root root 79129920 Apr 28 14:08 VirtualBox-5.1-5.1.22_115126_el7-1.x86_64.rpm

[root@desktop1 software]# chmod 755 VirtualBox-5.1-5.1.22_115126_el7-1.x86_64.rpm

[root@desktop1 software]# yum -y install VirtualBox-5.1-5.1.22_115126_el7-1.x86_64.rpm

Loaded plugins: langpacks, product-id

Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast

Examining VirtualBox-5.1-5.1.22_115126_el7-1.x86_64.rpm: VirtualBox-5.1-5.1.22_115126_el7-1.x86_64

Marking VirtualBox-5.1-5.1.22_115126_el7-1.x86_64.rpm to be installed

Resolving Dependencies

–> Running transaction check

—> Package VirtualBox-5.1.x86_64 0:5.1.22_115126_el7-1 will be installed

–> Finished Dependency Resolution

base | 4.1 kB 00:00:00

Dependencies Resolved

=====================================================================================================================================================

Package Arch Version Repository Size

=====================================================================================================================================================

Installing:

VirtualBox-5.1 x86_64 5.1.22_115126_el7-1 /VirtualBox-5.1-5.1.22_115126_el7-1.x86_64 169 M

Transaction Summary

=====================================================================================================================================================

Install 1 Package

Total size: 169 M

Installed size: 169 M

Downloading packages:

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

Installing : VirtualBox-5.1-5.1.22_115126_el7-1.x86_64 1/1

Installed:

VirtualBox-5.1.x86_64 0:5.1.22_115126_el7-1

Complete!

Step3: Verify the VirtualBox installation

[root@desktop1 software]# rpm -qa | grep VirtualBox

VirtualBox-5.1-5.1.22_115126_el7-1.x86_64

[root@desktop1 software]# which VirtualBox

/bin/VirtualBox

[root@desktop1 software]# VBoxManage –version

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

[root@desktop1 software]# yum -y install vagrant_1.9.6_x86_64.rpm

Loaded plugins: langpacks, product-id

Examining vagrant_1.9.6_x86_64.rpm: 1:vagrant-1.9.6-1.x86_64

Marking vagrant_1.9.6_x86_64.rpm to be installed

Resolving Dependencies

–> Running transaction check

—> Package vagrant.x86_64 1:1.9.6-1 will be installed

–> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================

Package Arch Version Repository Size

=====================================================================================================================================================

Installing:

vagrant x86_64 1:1.9.6-1 /vagrant_1.9.6_x86_64 190 M

Transaction Summary

=====================================================================================================================================================

Install 1 Package

Total size: 190 M

Installed size: 190 M

Downloading packages:

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

Installing : 1:vagrant-1.9.6-1.x86_64 1/1

Verifying : 1:vagrant-1.9.6-1.x86_64 1/1

Installed:

vagrant.x86_64 1:1.9.6-1

Complete!

Step6: Verify the Vagrant installation

[root@desktop1 software]# rpm -qa | grep vagrant

vagrant-1.9.6-1.x86_64

[root@desktop1 software]# which vagrant

/bin/vagrant

[root@desktop1 software]# vagrant –version

Vagrant 1.9.6

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

[root@desktop1 software]# vagrant box add bento/centos-7.2 –provider=virtualbox

==> 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’!

[root@desktop1 software]# ls -ltr ~/.vagrant.d/boxes/

total 0

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

[root@desktop1 vagrantfiles]# vagrant init bento/centos-7.2

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.

[root@desktop1 vagrantfiles]# ls -ltr

total 4

-rw-r–r–. 1 root root 3023 Jul 6 00:51 Vagrantfile

Step9: Start up the initialed VirtualBox

[root@desktop1 vagrantfiles]# vagrant up

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

[root@desktop1 vagrantfiles]# export VBOX_HWVIRTEX_IGNORE_SVM_IN_USE=true

[root@desktop1 vagrantfiles]# vagrant up

Step10: Verify the State of the VM

[root@desktop1 vagrantfiles]# vagrant status

Current machine states:

default running (virtualbox)

Hope you enjoyed reading this article. Thank you.