5

Step by step guide on using existing virtual machines with Vagrant

 3 years ago
source link: https://computingforgeeks.com/step-by-step-guide-on-using-existing-virtual-machines-with-vagrant/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Step by step guide on using existing virtual machines with Vagrant
Search

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.

Last time I talked about How to add, install and run CentOS 7 Vagrant box to Virtualbox using Vagrant and How to set Default Vagrant Provider to Virtualbox on Linux; Fedora,Debian,Ubuntu

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
credentials are:

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/

cd /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

–base=”Your_existing_virtual_machine_name”
–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

vagrant up

You should now be able to ssh into it.

vagrant ssh

If your username and password was not
username: vagrant
password: vagrant

vim  Vagrantfile
Set username and password:
config.ssh.username = "username" config.ssh.password = " password"

In case you encounter problem trying to ssh like “retrying…”. Do

vagrant up

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.

 vagrant  ssh-config

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

vim  Vagrantfile

Start with the line
config.ssh.private_key_path=”Path given by ssh-config command”
My full line is:

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

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

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

5) Logout and reload your VM by hitting CTL+D key twice, then

vagrant reload

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.

Tags:
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

Easiest way to create Vagrant Box from existing Virtualbox VM 


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK