This is a short tutorial on how to create Vagrant machine from existing Virtualbox Virtual Machine.Vagrant tool give you best environment to manage and control your Virtual Machines. This works on any Linux Ditribution
Installing Vagrant box from scratch may not be efficient way if you already have a running Virtualbox Vm from same Base os as the vagrant box. Additionally you’ll save bandwidth if you have updated pre-existing Vm and maybe installed additional packages that you’ll like to have on new Vagrant controlled Virtual Machine.
Create a directory for vagrant boxes

mkdir -p ~/vagrant
cd ~/vagrant
mkdir centos-server-7.1

In our example we’ll use an existing Virtualbox CentOS 7.1 server.This Virtual Machine is updated and have Asterisk Pbx installed.
To use this trick we’ll also need the following softwares installed.

  • Vagrant
  • Virtualbox

You can download centos 7.1 vagrant box from the Link Vagrant Centos 7.1.box
or use wget to download it.

wget https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.1/vagrant-centos-7.1.box

Once the download is finished, move it to ~./Vagrant/centos-server-7.1

mv vagrant-centos-7.1.box ~./vagrant/centos-server-7.1

If you don’t have Virtualbox installed, you can use our articles:
For Fedora: Read How to Install Virtualbox on Fedora 23
For Debian and Ubuntu:  How to install Virtualbox Latest on Ubuntu and Kali Linux

If you Love what we do, support us by downloading this tutorial as pdf from the link below:

NOTE: Make sure your Virtualbox VM i.e in my case Centos-asterisk server have username called “vagrant” with password “vagrant”. If not you can add it
using script below.

vim adduser.sh

Paste the code below and save the file.

# Vagrant specific
date > /etc/vagrant_box_build_time

# Add vagrant user /usr/sbin/groupadd vagrant
/usr/sbin/useradd vagrant -g vagrant -G wheel
echo "vagrant"|passwd --stdin vagrant
echo "vagrant        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers.d/vagrant
chmod 0440 /etc/sudoers.d/vagrant

# Installing vagrant keys
mkdir -pm 700 /home/vagrant/.ssh
wget --no-check-certificate 'https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub' -O /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh

# Customize the message of the day
echo 'Welcome to your Vagrant-built virtual machine.' > /etc/motd 

Make script executable and run it.

 chmod +x adduser.sh
 ./adduser.sh

Add user account before copying .vmdk file.
Then do the following:
1. Change to directory containing CentOS vagrant box you downloaded.

  cd ~/vagrant/centos-server-7.1

2. Then generate Vagrantfile

vagrant init

3.Edit the name of box inside Vagrantfile to be “centos-server-7.1”

vim Vagrantfile

Your line 15, should be similar to one below

 config.vm.box = "centos-server-7.1"

4. Add vagrant box to your Environment with the name of box we specified on Vagrantfile

vagrant box add centos-7.1-x86_64.box --name centos-server-7.1

5. Bring the added vm up

vagrant up
If successful in spinning up, you should be logged in by default. If you get error message  “default: Warning: Connection timeout. Retrying...”, try press CTL+C and use vagrant ssh to login.
Change directory to VirtualBox VMs directory and find the name of Virtualbox VM that we want to make newly added box use.
cd  ~/VirtualBox VMs/
ls

Mine is under centos-asterisk folder.With the name of vmdk as centos-asterisk-disk1.vmdk.We need to copy this file to the directory containing vagrant vm we added “centos-server-7.1”.
6. Now rename your added VM on Virtualbox Gui. Before you can rename you have to turn it off.

cd ~/vagrant/centos-server-7.1
vagrant halt

To rename go to Virtual Machine > General > Basic > Name.See screenshots  below.

 

 

7. Next thing to do is copy centos-asterisk-disk1.vmdk to box-disk1.vmdk. Make sure the Virtual Machine is off, if not turn it off.

cp ~/VirtualBox VMs/centos-asterisk/centos-asterisk-disk1.vmdk ~/VirtualBox VMs/centos-server-7.1/box-disk1.vmdk

8. Now start vagrant box

  cd ~/vagrant/centos-server-7.1
  vagrant up
  vagrant ssh

9. Install Virtualbox Guest Addtions on the guest OS.
From Host computer, copy to VBoxGuestAdditions.iso ~/vagrant/centos-server-7.1. Then on Guest OS run

mount -o loop /vagrant/VBoxGuestAdditions.iso /mnt
sh /mnt/VBoxLinuxAdditions.run
umount /mnt

If you Love what we do, support us by downloading this tutorial as pdf from the link below:

Enjoy your Vagrant environment.

How to add,install and run CentOS 7 Vagrant box to Virtualbox using Vagrant