Book Image

KVM Virtualization Cookbook

By : Konstantin Ivanov
Book Image

KVM Virtualization Cookbook

By: Konstantin Ivanov

Overview of this book

Virtualization technologies such as KVM allow for better control over the available server resources, by deploying multiple virtual instances on the same physical host, or clusters of compute resources. With KVM it is possible to run various workloads in isolation with the hypervisor layer providing better tenant isolation and higher degree of security. This book will provide a deep dive into deploying KVM virtual machines using qemu and libvirt and will demonstrate practical examples on how to run, scale, monitor, migrate and backup such instances. You will also discover real production ready recipes on deploying KVM instances with OpenStack and how to programatically manage the life cycle of KVM virtual machines using Python. You will learn numerous tips and techniques which will help you deploy & plan the KVM infrastructure. Next, you will be introduced to the working of libvirt libraries and the iPython development environment. Finally, you will be able to tune your Linux kernel for high throughput and better performance. By the end of this book, you will gain all the knowledge needed to be an expert in working with the KVM virtualization infrastructure.
Table of Contents (15 chapters)
Title Page
About the Author
About the Reviewer
Customer Feedback

Defining KVM instances with Python

In this recipe we are going to define a new KVM instance using the Python libvirt module we installed in the previous recipe. We are going to use a virtual environment and the iPython development tool for the following examples.

Getting ready

For this recipe we are going to need the following:

  • An Ubuntu host, with libvirt and QEMU installed and configured
  • The debian.img raw image file we built in the Installing custom OS on the image with debootstrap recipe from Chapter 1, Getting Started with QEMU and KVM
  • Python 2.7, the iPython tool, and the virtual environment we created in the Installing and using the Python libvirt library recipe in this chapter

How to do it...

To define a new KVM instance, using the Python libvirt module follow these instructions:

  1. In the iPython interpreter, import the libvirt module:
In [1]: import libvirt

In [2]:
  1. Create the instance definition string:
In [2]: xmlconfig = """
<domain type='kvm' id='1'>