Monday, 25 August 2014

Getting started with KVM- Creating VMs

Getting Started with KVM
Part 2: Creating Virtual Machines

In this set of tutorial, I'll be showing some simple steps to creating Virtual Machines easily using KVM (Kernel-based Virtual Machine)

NOTE: before we begin, make sure that your host system has a functional Intel VT or AMD-V flag set. This is essential for creating Virtual Machines on your Host system. 

For the steps to install and configure KVM, have a look at my EARLIER POST 

There are basically two ways to go about creating VMs using KVM. I'll be demonstrating both the steps below:

1) Using Command Line
The first thing to do here is create a separate directory where we will store all our VMs. 
Create a new folder at /var/lib/libvirt directory for our VMs.

# cd /var/lib/libvirt

# mkdir VM

Next, download your favorite OSs ISO file. Here, I'm going to be using Debian as my Guest OS.

# wget

We will use the /var/lib/libvirt/images directory to store the ISOs that will be used to create our VMs.

# mv debian-7.6.0-amd64-netinst.iso /var/lib/libvirt/images

We are now ready to create our VM. We will be using virt-install for this. 

The virt-install utility must be run as root and accepts a wide range of command-line arguments that are used to provide configuration information related to the virtual machine being created. Some of these command-line options are mandatory (specifically name, ram and disk storage must be provided) while others are optional.

In the following example, I will be creating a Debian VM named "My-Debian-VM1" with "1" vCPU and "512MB" RAM with a Disk of "10GB". 

# virt-install \
My-Debian-VM1 \
--description "My First Debian VM using KVM" \
--ram=512 \
--vcpus=1 \
--disk path=/var/lib/libvirt/VM/My-Debian-VM1.img,size=10 \
-c /var/lib/libvirt/images/debian-7.6.0-amd64-netinst.iso \

--graphics vnc \
--network bridge:br0 \ 
--network bridge:br1 \
--os-type linux \
--os-variant=debiansqueeze \
--hvm \

What do these parameters mean??

-n My-Debian-VM1 : The name of the VM
--description "My First Debian VM using KVM" : The long description of the VM. This is an optional requirement
--ram=512 : Amount of memory allocated to the VM i.e. 512MB
--vcpus=1 : The number of virtual CPU(s) for the VM i.e. 1vCPU
--disk path=/var/lib/libvirt/VM/My-Debian-VM1.img,size=10 : This is is the image file for the VM, the size is specified in GBs
-c /var/lib/libvirt/images/debian-7.6.0-amd64-netinst.iso : Install debian-7.6.0 from an iso the location parameter
--graphics vnc : Use VNC to access installation using vnc server/viewer from your local Linux/OSX/Unix/Windows desktop
--network bridge:br0 : Create a network bridge using br0 
--network bridge:br1 : Create a network bridge using br1
--os-type linux : The VM OS type
--os-variant=debiansqueeze : The VM OS variant
--hvm : Request the use of full virtualization

Once your Vm is built, you can access it using the Virtual Machine Manager (VMM) as shown below:

Navigate to Applications > System Tools > Virtual Machine Manager

You should see the newly created VM running. Simply double click on it to view the console.

You can now install your Guest OS just like you would do on a normal server. Just remember that if you want to release the control of the mouse from the Guest OS, use the key combination LEFT CTRL + LEFT ALT

2) Using GUI
This is another method using which we can create VMs. Here I'll be using a tool called Virtual Machine Manager

Navigate to Applications > System Tools > Virtual Machine Manager

Select the new VM icon to get started

Enter the "Name" of your VM and also select how you would like to install the Guest OS. You can use local storage in the form of ISO images or CD/DVDs, HTTP or FTP servers, NFS area, or PXE. In this example, we will use the local install media itself for our Guest OS.

Next, choose either a physical device or an image. In our case, we will use an ISO. Then, choose the OS type and the version. This does not have to be precise, but an educated guess is good. It allows additional virtual machine optimization.

Browse for the ISO of your Guest OS (In this case, im using Debian v.7) 

The final outcome should look something like this:

Allocate the required "RAM" and "CPU

Next, provide a "Storage" for your VM. You can either create a new disk or use an existing one as well. You can set the size and pre-allocate all space, which is good for performance, plus it reduces fragmentation.

The final step provides a summary of your VMs settings, with some advanced options. You can change your network type, set a fixed Mac address, choose the virtualization type, and the target architecture here.

NOTE: I have purposely selected the check box "Customize configuration before install" as I have to add a secondary NIC card to my VM as well. I can do this step later as well after the VM is created with the Guest OS, but I prefer doing it at the beginning itself.

If you have selected the check box "Customize configuration before install" from the previous step, then this should pop up on your screen now. This are basically the "Settings" of your VM. To add a new NIC, we need to select the "Add hardware" option as shown below:

Next, select the "Network" tab and alongside, select the correct "Host Device" (in this case, its br1). Once done, click Finish.

Verify the new network card was successfully added. Next, click on "Begin Installation" to start the VMs boot up process.

You should see your new VM power ON in the Virtual Machine Manager

The next steps are pretty easy and trivial. Install your Guest OS as you would do it on a normal server. Once again, just remember that if you want to release the control of the mouse from the Guest OS, use the key combination LEFT CTRL + LEFT ALT

Once you power ON the Guest OS, the two NICs will be assigned to it, however, there will be no IP address available as we have not configured the Networking part (DHCP/ NAT/ Routing etc) of KVM yet.

In the next post, I'll show you How to configure the Networking and Storage for KVM.. so stay tuned..!!