Showing posts with label virtualization. Show all posts
Showing posts with label virtualization. Show all posts

Tuesday, December 20, 2016

Vagrant for Automating Your Virtual Box

Vagrant creates and configures virtual development environments. It can be seen as a higher-level wrapper around virtualization software such as VirtualBox, VMware, KVM and Linux Containers (LXC), and around configuration management software such as Ansible, Chef, Salt and Puppet.

Vagrant is not just about creating VMs, it is about automating the work of setting up development environments for our projects. Also, we can check Vagrantfiles into source control with each project, so the environment is essentially stored with the code, without having to find a way to store a VM itself.



Install VirtualBox
sudo apt-get install virtualbox

Install Vagrant
sudo apt-get install vagrant

OR Install Vagrant from here
https://www.vagrantup.com/downloads.html

mkdir vagrant_test
cd vagrant_test
vagrant init

This will create a Vagrantfile. As said Vagrantfile is config file for the virtual machine.

Next download an image.
vagrant box add ubuntu/trusty64
you can browse for available boxes here : https://atlas.hashicorp.com/boxes/search

This stores the box under a specific name so that multiple Vagrant environments can re-use it. Just like VM templates

[Note : The syntax for the vagrant box add subcommand is changed with the version 1.5, due to the Vagrant Cloud introduction.
vagrant box add {title} {url}
http://www.vagrantbox.es/]

Next change your Vagrantfile contens as below.

Vagrant.configure("2") do |config|
   config.vm.box = "ubuntu/trusty64"
   config.vm.network "private_network", ip: "xxx.xxx.xx.xx"
   config.vm.provider "virtualbox" do |vb|
     vb.cpus = 2
     vb.memory = "4096"
   end
end
Instead of using default box , we point config.vm.box to "ubuntu/trusty64" that we downloaded earlier.
config.vm.network : This allows to access any servers running on the box to be available to the network. You can configure public ip if you need.
Next , we set number of cpus 2 , also memory to 4GB.

Now bring up the box using following command
vagrant up

We can connect to the machine using
vagrant ssh

To destroy VM, we can use following command
vagrant destroy

Also note that vagrant halt will shut down the machine gracefully. And if you make any changes to Vagrantfile, to update the box we can use vagrant reload command.


Sunday, August 21, 2016

Quick Emulator ( qemu ) for Hardware Virtualization

Have you ever met a requirement where you had to install a windows on top of linux ? Here is a cool solution. QEMU is a free and open-source hosted hypervisor that performs hardware virtualization. Lets see how to install a windows server on a Ubuntu server.

This tutorial assumes that your hardware supports virtualization.
You can check by running following command on your linux terminal.
egrep -c '(vmx|svm)' /proc/cpuinfo

If output is 0 , then not supported , any value above 0 means it is supported.

1. Install qemu
apt-get install qemu-kvm

Once QEMU has been installed, it should be ready to run a guest OS from a disk image.

Now we will test with a tiny example.Download a small image of linux from here.
This is a Small Linux disk image containing a 2.6.20 Linux kernel, X11 and various utilities to test QEMU.

Now unzip archive
bzip2 -d linux-0.2.img.bz2
Note, that this command will not preserve original archive file.
To preserve the original archive, add the -k option:
bzip2 -dk linux-0.2.img.bz2
Now the image is ready.

you can issue the following command.
sudo qemu-system-x86_64 -display vnc=0.0.0.0:1 -smp cpus=2 -m 250M -machine pc-1.0,accel=kvm -net user,hostfwd=tcp::80-:80,hostfwd=tcp::3389-:3389 -net nic linux-0.2.img

This command creates a virtualization with 2 cpu , 250MB memory with VNC server running on it.
You can connect through any vnc client ( realvnc or tightvnc ) 0.0.0.0:1



Now to install a windows OS , we first need to create an empty image

sudo qemu-img create -f raw win.img 40G

This creates an image with 40GB hardisk and raw format.
Now we can install windows os by using the iso image like below.

sudo qemu-system-x86_64 -display vnc=0.0.0.0:1 -cdrom /media/windows_server_2012_r2_with_update_x64_dvd_6052708.iso -smp cpus=2 -m 16G -machine pc-1.0,accel=kvm /var/spool/win.img

Once you run the above command , a vnc server is started and you can connect from a vnc client on display 1 or 5901, depending on client and install the windows.



Enable IIS.
Enable RDP.
Shutdown VM.
Run Windows VM with IIS via:

sudo qemu-system-x86_64 -display vnc=0.0.0.0:1 -smp cpus=2 -m 16G -machine pc-1.0,accel=kvm -net user,hostfwd=tcp::80-:80,hostfwd=tcp::3389-:3389 -net nic /var/spool/win.img

This will launch the machine with 16GB ram , 40G Hardisk . You can connect from RDP and work on it.