Wednesday, 27 August 2014

Getting started with KVM- Networking and Storage

Getting Started with KVM
Part 3: Networking and Storage

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. 

In my PREVIOUS POST, we looked at the two ways in which we can create VMs for KVM. This is a continuation of that post where we look at some of the advanced Networking and Storage options provided for KVM.

1) Virtual Networks
The first thing we are going to do is create Virtual Networks for our VM to use. In the PREVIOUS POST, we created a VM called "My-Debian-VM2", I'll be demonstrating these steps on this VM itself. So without further adieu, lets get started!!

The first thing to do is select the "localhost (QEMU)" option from our VMM console. 

This brings up the following "Connection Details" Page. Here you can create Virtual Networks, assign Storage Pools, Add new Network Interfaces etc. You can even monitor the performance of your KVM using the basic performance graphs that are generated here.

For our first exercise, we need to select the "Virtual Networks" Tab. There is already a Virtual Network created here by default, and as you can see, its got few Network configurations done to it, such as DHCP start and end IP addresses, Forwarding enabled and set to NAT etc. What we are going to do is we will "Add" a fresh Virtual Network here. To do that, click on the "+" icon as shown:

This will present a dialog that will help you setup your Virtual Network with ease. To begin, click "Forward"

Provide a suitable name for the Virtual Network. In my case, I'm calling it a "VM_br0_NET

Next, provide it a suitable "IPv4 Address Space". You need to provide any valid IP range here, provided they belong to the, or IP segments. I have provided for a "" network.  

The wizard will automatically provide you your Virtual Network details such as Broadcast and Gateway IPs.

In the next section, you can set the DHCP range for your Virtual Network. This is optional, but helps to set it out never the less. You need to enter the "start" and the "end" IP addresses from your Virtual Network IP range as shown. 

Next, select how the Virtual Network should connect to a Physical Network on your KVM Host. You can either opt to set the Virtual Network as "Isolated" or have the Virtual Network "Forwarded" to a Physical NIC as we are about to do. 

In my case, I have forwarded the Virtual Network on my eth0 device and Ill be using NAT as my forwarding mode.

Review the changes and click "Finish" to create the Virtual Network.

Your new Virtual Network will be created and displayed as below:

Perform the same steps for the second NIC (eth1) as well. Here, I created a similar Virtual Network called "VM_br1_NET" and provided it an IP range of ""

Once the Virtual Networks are created, "Close" the KVM Connections Manager window.

We now have to add the Virtual Network to our VM. To do so, sect the VM (My-Debian-VM2) as shown below:

This pops up a "Virtual Machine Settings" page. Here, you can configure your VMs Virtual Hardware settings, add new Virtual Hardware, etc.. 

In our case, select the "Network Card" as shown. Next, change the "Source Device" from "Bridge (br0)" to the Virtual Network "VM_br0_NET" that we created earlier.

Do not change the "Device Model" settings.

Perform the same change for the second NIC as well. Change "Bridge (br1)" to "VM_br1_NET" Virtual Network. Once done, hit "Apply" and exit the VM Manager Console.

2) Storage Pools

The second thing that we will configure for our VMs is a Storage Pool. Storage Pools are logical storage groups that can contain one or more volumes or virtual disks in various formats.

Open the Virtual Machine Manager (VMM). Click Edit > Host Details > Storage tab.

This will open the Storage section. In the left column, you will see all of your listed storage pools. By default, there will be only one, default, located under /var (This is a default location). In the screenshot below, 45% of the storage pool is currently in use. As you can see, this storage pool contains the ISO image for our Guest OS, along with the actual guest OS disks (virtual disks) as well.

First, lets add some Storage Pools for our tutorial. Click on the "+" icon as shown.

A dialog box appears which will help you setup your Storage Pools. First thing is to choose a "Name" and "Type". You can use directories (mount points), including NFS, SCSI and iSCSI storage, LVM, and even physical disks.

In my case, I've provided a name "StoragePool1" and am providing a "Directory" as my pool choice. 

NOTE: Before starting this activity, I have created and mounted a separate HDD to my KVM Host. This new HDD (/dev/sdb1) is mounted locally to a directory called "StoragePool1" which contains a folder within it called "MyNewVMDirectory". 

In the next section, browse to the required folder that you want to use as your Storage Pool destination folder (../StoragePool1/MyNewVMDirectory). Once done, click "Finish"

Your new Storage Pools is now ready for use. The next step is to create a "New Volume" and attach that volume to our VM. to do so, select the "New Volume" option as shown below:

This pops up a dialog that will help you create your Virtual Disks (aka volumes). The disks will be suffixed with ".img", but they can be formatted any which way you choose to. You may want to use qcow2, raw or vmdk, for instance. The format will determine if you can use the virtual machines with other solutions such as VMware Player/ Workstation etc.

Provide a suitable name for your new Volume (Debian-Disk-2). Select "Max Capacity" and "Allocation" units as well.

"Max Capacity" is the max size of the volume. "Allocation" is how much you want to allocate now. Basically, if you choose zero size, you are creating a sparse file with 0 size. You can allocate any value between zero and max. capacity.

Once done, click "Finish" to create your Volume.

Your new Volume is created and ready to be used by your VM.

To add a new Volume to your VM, go to Virtual Machine Manager (VMM) > {VM_NAME}Virtual Machine Settings page. As you can see from the screenshot below, there is only one Disk allocated to the VM (My-Debian-VM2). Click on "Add Hardware" to begin.

Next, select the "Storage" tab. You can either create a new Disk using this wizard or attach an already existing disk as well. "Browse" for the new Volume that we created in our earlier steps.

As you can see, I have selected my Storage Pool "StoragePool1" and within that, I have my new Volume aka "Debian-Disk-2.img"

Once added, you can choose to change the "Disk Type" however I prefer to keep these values to their defaults. Click "Finish" when done.

You should see your newly added Disk attached to your VM as shown below. You are now ready to Power ON your VM and install the guest OS.

Just a few screenshots I took during the Install process. AS you can see here, I am asked to select the Primary Network Interface for my Debian guest OS. On selecting either of the cards, the network automatically assigns the VM an IP address form the DHCP pool that we created in our earlier steps.

NOTE: Here, eth0 will provide my Debian VM with internet access, while eth1 is routed on my LAN network, so remains isolated from the web.

Secondly, here's a screen grab of the two HDDs that we used for this VM. I created partitions manually on both the disks and formatted both to ext4 file systems.

Thahts all there is to it folks!! Hope this tutorial proves helpful to you in setting up your very own KVM Hosts... 

No comments :

Post a Comment

Note: only a member of this blog may post a comment.