This is a short tutorial on how to create a Vagrant box from existing Virtual Machine. Vagrant tool gives you the best environment to manage and control your Virtual Machines. This works on any Linux Distribution.

Installing Vagrant box from scratch may not be an 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.

Step 1: Create Vagrant box directory

Create a directory for vagrant box to be created.

mkdir -p ~/vagrant
cd ~/vagrant
mkdir centos7

Step 2: Create vagrant user on existing VM

You current VM which you need to convert to Vagrant box need to have vagrant user created. If missing, create a username called “vagrant” with password “vagrant“. If not you can add it

$ nano 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 the script executable and run it.

chmod +x adduser.sh
./adduser.sh

Step 3: Create Base vagrant box

Change to directory containing CentOS vagrant box you downloaded.

 cd ~/vagrant/centos7

Create a file called Vagrantfile and paste below data.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
end

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.

Step 4: Copy VM Virtual Disk

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 “centos7“.

Now rename your added VM on Virtualbox Gui. Before you can rename you have to turn it off.

cd ~/vagrant/centos7
vagrant halt

To rename go to Virtual Machine > General > Basic > Name

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/centos7/box-disk1.vmdk

Now start vagrant box

cd ~/vagrant/centos7
vagrant up
vagrant ssh

Step 4: Install VirtualBox Guest Additions

Install Virtualbox Guest Additions on the guest OS.

From Host computer, copy to VBoxGuestAdditions.iso ~/vagrant/centos7. Then on Guest OS run

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

Enjoy your Vagrant environment. Similar guides

How To Convert VirtualBox Disk Image (VDI) and img to Qcow2 format

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

Step by step guide on how to extend Vdi and VMDK Hard disks on VirtualBox