(: November 24, 2018)
This is a short tutorial on how to create a Vagrant machine from existing Virtualbox 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.
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 software installed.
You can download centos 7.1 vagrant box from the Link Vagrant Centos 7.1.box
or use wget to download it.
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
Paste the code below and save the file.
Make the 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.
2. Generate Vagrantfile
3.Edit the name of the box inside Vagrantfile to be “centos-server-7.1”
Your line 15, should be similar to one below
config.vm.box = "centos-server-7.1"
4. Add the vagrant box to your Environment with the name of a 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
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