2011/09/02

Installing KVM Virtual Machines

Many of my coworkers share the same basic virtual machine images with me, we call them golden images. These images should be very minimal installations of the operating system plus our default account and the configuration management client. In this post I will show how we build such disk-images.

To install a new virtual machine you will need an ISO CD/DVD image containing the Linux distribution of your choice. We do follow the rule to save these golden images to a dedicated directory /srv/images. These images should be never instantiated once they are installed. The folders holding particular virtual machine images are named according to the distribution name, version, and bitness. Furthermore We append a string describing the general purpose of the image, like being with graphical user interface, or providing a specific service. Examples are:

  • debian64-6.0.0-server
  • ubuntu64-10.04-desktop
  • debian64-6.0.2.1-chef-server-0.10.4

The following libvirt configuration (called libvirt_install.xml in this example) is used to start a virtual machine with an ISO image attached, which will be used to boot.

<domain type='kvm'>
  <name>debian-6.0.0-server</name>
  <memory>524288</memory>
  <vcpu>1</vcpu>
  <os>
    <type arch="x86_64">hvm</type>
    <boot dev='cdrom'/>
  </os>
  <clock sync="localtime"/>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='disk'>
      <source file='/srv/images/debian64-6.0.0-server/disk.img'/>
      <target dev='hda'/>
      <driver name='qemu' type='qcow2'/>
    </disk>
    <interface type='bridge'>
      <source bridge='nbr0'/>
    </interface>
    <disk type='file' device='cdrom'>
      <source file='/srv/isos/debian-6.0.0-amd64-netinst.iso'/>
      <target dev='hdc'/>
      <readonly/>
    </disk>
    <graphics type='vnc' port='5905'/>
  </devices>
  <features>
    <acpi/>
  </features>
</domain>

You will need to adjust the source file locations of the virtual machine disk image and the ISO image. Before you can install the operating system you need to prepare a virtual machine disk image, which is in the case of Linux KVM created and initialized with the kvm-img command. (The parameter "40G" indicates the maximum size in GB the image can grow to, while being used.)

$ kvm-img create -f qcow2 disk.img 40G
$ virsh create libvirt_install.xml

Once the instance has started you need to connect a VNC client to the port 5905 as it was defined above with the graphics tag. While you follow the installation menu we propose to always create a minimal system configuration, which is the same across all golden images your create.

We do set the following configuration during installation:

  • Keymap: English
  • Host name is the distribution nick-name (e.g squeeze or lucid)
  • Domain name 'devops.org'
  • On big disk partition, no SWAP!
  • Username is 'devops'
  • Only standard system, no desktop environment (unless really needed), no services, no development environment, no editor, nothing, except a bootable Linux.

After the installation is finished, we elevate the "devops" user to be able to run commands as root via Sudo and we install the Chef configuration management system.

For Debian flavored Linux distributions this could look like:

$ echo "deb http://apt.opscode.com/ squeeze main" > /etc/apt/sources.list.d/opscode.list
$ wget -qO - http://apt.opscode.com/packages@opscode.com.gpg.key | sudo apt-key add -
$ apt-get update
$ apt-get install openssh-server sudo rsync chef
$ apt-get clean
$ groupadd admin
$ usermod -a -G admin devops

We added the following line to /etc/sudoers:

%admin ALL=NOPASSWD: ALL

When installation and final configuration is finished, shutdown the instance and don't touch it anymore, but clone new virtual machines from there.

You can compress the disk image:

$ kvm-img convert -c -f qcow2 -O qcow2 -o cluster_size=2M disk.img compressed.img
$ mv compressed.img disk.img

As a last step we will add a libvirt configuration used to start a virtual machine instance of this image. The golden image directory will contain the following files at the end:

  • The file containing the golden image disk.img.
  • The configuration libvirt_install.xml used to install the operating system, for later reference.
  • The configuration libvirt_instance.xml used to start a virtual machine. This file needs to be adjusted after the golden image was cloned.

The libvirt_instance.xml template looks like:

<domain type="kvm">
  <name>ADD FQDN HERE</name>
  <memory>524288</memory>
  <vcpu>1</vcpu>
  <os>
    <type arch="x86_64">hvm</type>
  </os>
  <clock sync="localtime"/>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type="file" device="disk">
      <source file="ADD PATH TO DISK IMAGE HERE"/>
      <target dev="hda"/>
      <driver name="qemu" type="qcow2"/>
    </disk>
    <interface type="bridge">
      <source bridge="nbr0"/>
      <mac address="ADD MAC ADDRESS HERE"/>
    </interface>
  </devices>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <features>
    <acpi/>
  </features>
</domain>

In a future post I will describe how to add an SSH key for password-less login to enable easy access to such images.

1 comment: