This will guide you on packaging existing environments to use with Vagrant, replacing the shared disk with a new box file. These box files can make managing virtual machines and different versions of these virtual machines vastly simpler, especially, if you don’t want to build environments from base boxes every time.
In this example, I’ll use an existing Virtual Machine Environment build with Oracle Virtualbox where I’ll choose an existing environment based on the CentOS 7 operating system that has been created as a VirtualBox machine, the name of the Virtual Machine is openstack-controller shown in the screenshot below.
There is a user account present on the machine that we want to reuse. The
Username: vagrant Password: vagrant
NOTE: My Existing virtual machine has ssh installed and configured. Before packaging your Virtual Machine, make sure you have an installed ssh.
Now navigate to the folder containing Virtualbox’s Virtual Machines; mine is /home/josepy/VirtualBox VMs/
Check the presence of the Virtual Machine you want to package with ls command.
Now package and export the Virtual machine.
vagrant package --base=openstack-controller --output=openstack-asterisk.box
–output=” The name you want to give to packaged box”
After the box has been packaged successfully, we should create a folder to house vagrant environment.
mkdir -p ~/vagrant cp openstack-asterisk.box ~/vagrant cd ~/vagrant
Then import the box into your environment.
vagrant box add --name=openstack-asterisk openstack-updated-centos.box
This command will copy the box to your local Vagrant cache, so you are now ready to directly use the box.
Edit the box name in Vagrantfile to match one added above. Mine is openstack-asterisk.
vagrant init vi Vagrantfile
Then Edit config.vm.box on line 15.See screenshot below.
Start added box by running vagrant up
You should now be able to ssh into it.
If your username and password was not
config.ssh.username = "username"
config.ssh.password = " password"
In case you encounter problem trying to ssh like “retrying…”. Do
Then after a persisting retrying to login problem start, hit CTL+C to cancel. This will keep Vagrant Virtual Machine running on background. Now, generate ssh keys and note its storage location.
You’ll get output similar to one below.
My ssh key is located in “/home/josepy/vagrant/centos-asterisk/.vagrant/machines/default/virtualbox/private_key”
Add the location given above to the Vagrant file
Start with the line
config.ssh.private_key_path=”Path given by ssh-config command”
My full line is:
Replace the path with yours.Next thing we’ll do is update VirtualBox Guest additions on the guest.
On virtualbox menu go to File > Virtual Media Manager > Optical Disks. You should see VBoxGuestAdditions iso file. If it doesn’t exixt, download it first.
For my case, its location is /home/josepy/.config/VirtualBox/VBoxGuestAdditions_5.0.12.iso
I will copy above iso to the guest os. Follow steps below to achieve it.
1) Make sure the Guest OS is running and SSH server is installed and running as well.
2) If you’re not on local network, do port forwarding under networking option on VirtualBox Guest OS settings menu.
VirtualBox > centos-asterisk > Settings > Network > Adapter 1 > Port Forwarding.
Add rule if it doesn’t exist by clicking on + sign with following specifications.
Name: ssh Protocol: TCP Host IP: 127.0.0.1 Host port: 2222 Guest port: 22
Click on OK to save changes.
3) From your Host OS, copy VBoxGuestAdditions.iso file to Guest using scp
scp -P 2222 /home/josepy/.config/VirtualBox/VBoxGuestAdditions_version.iso [email protected]:~
Replace vagrant with your username of Guest OS.When asked
Are you sure you want to continue connecting (yes/no)? . Answer yes
4) On the Guest OS, mount the VBoxGuestAdditions_version.iso to /mnt
5) Logout and reload your VM by hitting CTL+D key twice, then
You should now have a working Vagrant environment. That’s all, thanks for reading.
Hope this article was of help to you. If you encounter any problem/error, let me know by dropping it on Comments section.
How to add Vagrant box using Existing VirtualBox Virtual Machine
Vagrant VirtualBox Virtual Machine creation
Using Existing Virtual Machines with Vagrant
Vagrant quick Virtual Machone environment creation.
There is an alternative Method here