Step-by-step guide on using existing virtual machines in Vagrant

This will guide you to package your existing environment for use with Vagrant and replace the shared disk with a new box file. These Box files make it easy to manage virtual machines and different versions of these virtual machines, especially if you don’t want to build your environment from the base box every time.

Last time I talked about how to use Vagrant to add, install and run CentOS 7 Vagrant box in Virtualbox and how to set the Default Vagrant Provider to Virtualbox on Linux. Fedora, Debian, Ubuntu

In this example, I will use the existing virtual machine environment of Oracle Virtualbox to build a version, where the existing environment will be selected based on the CentOS 7 operating system that has been created as a VirtualBox machine, and the name of the virtual machine is openstack-the controller is shown below In the screenshot.

A user account exists on the computer we want to reuse. of
The credentials are:

Username: vagrant
Password: vagrant

Note: My existing virtual machine has ssh installed and configured. Before packaging the virtual machine, make sure that ssh is installed.
Now, browse to the folder containing the Virtualbox virtual machine; mine is / home / josepy / VirtualBox VMs /

cd /home/josepy/VirtualBox VMs/

Use the ls command to check if the virtual machine to be packaged exists.

Step-by-step guide on using existing virtual machines in Vagrant

Package and export the virtual machine now.

 vagrant package --base=openstack-controller --output=openstack-asterisk.box

-basis= “Your existing virtual machine name”– Output= “The name you want to give the box”
After successfully packing the box, we should create a folder to hold the idle 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

Step-by-step guide on using existing virtual machines in Vagrant

This command copies the box into the local Vagrant cache, so you can now use the box directly.
Edit the box name in Vagrantfile to match the box name added above. Mine is an asterisk.

vagrant init
vi Vagrantfile

Then edit config.vm.box on line 15, see screenshot below.

Step-by-step guide on using existing virtual machines in Vagrant

Start adding boxes by running homeless

vagrant up

Step-by-step guide on using existing virtual machines in Vagrant

You should now be able to use it.

vagrant ssh

Step-by-step guide on using existing virtual machines in Vagrant

If your username and password are not
Username: vacancies
Password: Unemployed

vim  Vagrantfile

Set username and password:

config.ssh.username = "username" config.ssh.password = " password"

Step-by-step guide on using existing virtual machines in Vagrant

In case of problems, try switching like “Retry …”. do

vagrant up

Then, after continuously trying to retry the login issue, press CTL + C to cancel. This will keep the Vagrant virtual machine running in the background. Now, generate the ssh key and note where it is stored.

 vagrant  ssh-config

You will get output similar to the following.

Step-by-step guide on using existing virtual machines in Vagrant

My ssh key is in “/home/josepy/vagrant/centos-asterisk/.vagrant/machines/default/virtualbox/private_key”
Add the location given above to the Vagrant file

vim  Vagrantfile

Start from line
config.ssh.private_key_path = “The path given by the ssh-config command”
My green line is:

config.ssh.private_key_path="/home/josepy/vagrant/centos-asterisk/.vagrant/machines/default/virtua    lbox/private_key"

Step-by-step guide on using existing virtual machines in Vagrant

Replace the path with yours. Next, what we need to do is update the addition of the VirtualBox Guest on the guest.
On the virtualbox menu, go to File> Virtual Media Manager> Disc. You should see the VBoxGuestAdditions iso file. If it does not exist, download it first.
In my case, its location is /home/josepy/.config/VirtualBox/VBoxGuestAdditions_5.0.12.iso

Step-by-step guide on using existing virtual machines in Vagrant

I copied the above iso to the guest os. Please follow the steps below.
1) Make sure the Guest OS is running and the SSH server is also installed and running.
2) If you are not on the local network, perform port forwarding under the Network option on the VirtualBox Guest OS settings menu. VirtualBox> centos-asterisk> Settings> Network> Adapter 1> Port Forwarding.
If the rule does not exist, click the instructions below to add a + sign to add a rule.

Name: ssh
Protocol: TCP
Host IP: 127.0.0.1
Host port: 2222
Guest port: 22

Click OK to save your changes.

Step-by-step guide on using existing virtual machines in Vagrant

3) On the host operating system, copy the VBoxGuestAdditions.iso file to the guest using scp

scp -P 2222 /home/josepy/.config/VirtualBox/VBoxGuestAdditions_version.iso [email protected]:~

Replace the vagrant with your guest operating system username.
Are you sure you want to continue connecting (yes / no)? . The answer is

Step-by-step guide on using existing virtual machines in Vagrant

4) On Guest OS, mount VBoxGuestAdditions_version.iso to / mnt

sudo su -
cd /home/vagrant
mount -o loop VBoxGuestAdditions_5.0.12.iso /mnt/
cd /mnt/
sh VBoxLinuxAdditions.run 

Step-by-step guide on using existing virtual machines in Vagrant

Step-by-step guide on using existing virtual machines in Vagrant

5) Log out and reload the VM by pressing CTL + D twice, then

vagrant reload

You should now have a normal Vagrant environment. That’s it, thank you for reading.
I hope this article was helpful to you. If you encounter any issues / errors, please let me know in the “Comments” section.

label:
How to add a nomad box using an existing VirtualBox virtual machine
Vagrant VirtualBox virtual machine creation
Use an existing virtual machine with Vagrant
Tramps quickly create a virtual Machone environment.

Here is another way

The easiest way to create a Vagrant Box from an existing Virtualbox VM

Sidebar