Melpomene - Fotolia


Create a basic KVM cloud with a little help from Linux

In-house KVM cloud delivers a safe environment for quickly provisioning virtual machines that is similar to cloud environments at a fraction of the cost.

Public clouds are generally considered the most practical approach for quickly and efficiently provisioning a large number of virtual machines, but they certainly aren't the only option. For those seeking to deploy a smaller number of VMs, an in-house KVM cloud might be the better choice. In this article, we'll take a closer look at running a cloud-like environment without relying on a public cloud or expensive cloud software.

If you are looking for an easy method to deploy VMs, you should first consider creating a template for a ready-to-use disk image. This can be a disk file created in a kernel-based virtual machine (KVM) or any other virtualization software, or one created with a tool like virt-manager.

For the sake of this article, let's assume you already have such an image in place. In this demonstration, I'll be using a couple of VMs that were created with VMware Fusion, which will also allow us to learn more about the challenge of conversion.

Making a compatible VM file

When creating VM files in VMware, you must first make sure those files will be compatible with the rest of your infrastructure. This isn't a problem if you use only VMware's hypervisor, but many organizations also rely on KVM. One way of making sure your VMs are compatible is by exporting the VMs to the Open Virtualization Format (OVF). This format is like a zip file, with the advantage that it will be recognized by any current virtualization software or hypervisor.

In the VM you'll typically find an OVF file that describes the VM hardware, as well as at least one virtual machine disk file (VMDK) that is the virtual disk itself. You'll need to import the VMDK file before you can use it in your KVM cloud. To do this, use the following command from a Linux shell:

qemu-img convert -O qcow2 vmdisk.vmdk vmdisk.qcow2

Creating the VM

Once you've run the command shown above, you'll have a disk file compatible with KVM. The next step is to create the new VM based on that disk file. This can be accomplished with the virt-install command, followed by parameters to specify the exact hardware configuration you want to use. The resulting command should appear as such:

virt-install -n server1 --disk=/var/lib/libvirt/images/server1.qcow2 --ram 1024 vcpus 1 --connect qemu:///system --import --noautoconsole

This command will result in a new VM based on the qcow2 file that has been stored in the "/var/lib/libvirt/images" directory. At this point, you can start using the VM.

Attaching new hardware to the VM

After the VMs have been imported, you may need to add new hardware to them. To do so, use the virsh command to interact directly with the virsh management layer that talks to libvirtd. If, for instance, you needed to attach a network card to the VM, known as server1, you would use the following command:

virsh attach-interface --domain server1 --type network --source default --model virtio --mac 52:54:00:00:11:11 --config --live

This command provides a new network interface that uses the virtio driver for smart access to the network hardware. The virtio driver also writes to the configuration file and applies the new interface to the live configuration, which means the new hardware will be visible without rebooting.

You can also add new disk devices to a VM in a similar way. Before doing so, you'll need to create a storage device. To define the disk file, you can use the dd command to create a sparse file.

Use the following command to create a 1 GB sparse file:

dd if=/dev/zero of=/var/lib/libvirt/images/1Gfile.img bs=1M seek=1024 count=0

Next, attach it to the VM, using a command such as this:

virsh attach-diks myvm /var/lib/libvirt/images/1Gfile.img vdb --config --live

This command will add a virtual disk -- using the device name "/dev/vdb" -- to the VM.

Now that we've covered how to manage virtualization on a plain Linux KVM host, you can set up your own private KVM cloud using Linux commands and easily deploy and modify VMs based on template files.

Next Steps

Rules to simplify VM provisioning

Basic Linux commands for new users

Automation and virtualization tools aid VM provisioning

Dig Deeper on Cloud computing architecture