Creating VM templates

Up until now, we have been using the cirros template that we downloaded from the Ubuntu website to testing our cloud. We now need to create our own virtual machine templates that we can use. These templates need to be created as per the standards of our organization and this is how we like them best.

The following tools may be used to create the images. There are others as well, just choose the one that works best for you depending on the images that you are trying to build. As an example VMBuilder that ships with Ubuntu can only create Ubuntu images:

  • Oz
  • Packer
  • Disk image builder
  • VM builder

In this chapter, we will use a tool called Oz. This nifty little tool can install the operating system and represents it as disk images. It actually installs the images using libvirt, so those packages are prerequisites to use the tool.

Installing Oz and its dependencies

On the machine where we intend to create the template, we can install Oz and its dependencies.

It is advised that these be separate from the OpenStack nodes.

RHEL/CentOS

Installing Oz on the RHEL or CentOS is easily accomplished using the following command:

yum -y install kvm libvirt oz qemu-kvm

The preceding command installs Oz and its dependencies.

Ubuntu

On Ubuntu, we need a few more packages. So we execute the following commands:

apt-get install build-essential kvm libguestfs-tools python-all genisoimage mtools openssh-client

Once the pre-requisites are installed, we will use git to pull the packages, compile them, and install them:

mkdir ~/oz
cd ~/oz
git clone https://github.com/clalancette/oz.git oz-git
cd ~/oz/oz-git
dpkg-buildpackage -us –uc
cd ~/oz
dpkg -i oz_*_all.deb

This will install Oz on the system.

Oz templates

Oz uses template files in order to feed information to Oz as to what kind of machine needs to be built. This template is a kind of XML file, is readable, and easily understandable.

Let's take a look at a sample template file, which will use an ISO file in order to create a CentOS installation, which in effect will use a kickstart file to help with the installation:

<template>
  <name>centos64</name>
  <os>
    <name>CentOS-6</name>
    <version>4</version>
    <arch>x86_64</arch>
    <install type='iso'>
    <iso>http://mirror.rackspace.com/CentOS/6/isos/x86_64/CentOS-6.4-x86_64-bin-DVD1.iso</iso>
    </install>
  </os>
  <description>CentOS 6.4 x86_64</description>
</template>

The preceding template is the Oz template that is created. You can download sample templates for Oz from the following URL:

https://github.com/rcbops/oz-image-build/tree/master/templates

You can find Oz templates for several of the OSs that we normally build using Oz. However, for our purposes, we will use the previous template and save it as centos64.tdl, and we will now create a kickstart file to go along with this template.

You can choose to put all the customizations in the kickstart file itself.

Let's type the following in to a file and call it myCentOS.ks:

install
text
key --skip
keyboard us
lang en_US.UTF-8
skipx
network --device eth0 --bootproto dhcp
rootpw myRootPwd!
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
timezone --utc America/Chicago
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200"
zerombr yes
clearpart --all
part /boot --fstype ext4 --size=200
part swap --size=512
part / --fstype ext4 --size=1024 --grow
repo --name=epel --baseurl=http://ftp.nluug.nl/pub/os/Linux/distr/fedora-epel/6/x86_64/
reboot

%packages
@core
@base

%post
rpm -Uvh http://ftp.nluug.nl/pub/os/Linux/distr/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
rm -f /etc/udev/rules.d/70-persistent-net.rules
sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i 's,UUID=[^[:blank:]]* / ,/dev/vda3               / ,' /etc/fstab
sed -i 's,UUID=[^[:blank:]]* /boot,/dev/vda1               /boot,' /etc/fstab
sed -i 's,UUID=[^[:blank:]]* swap,/dev/vda2               swap,' /etc/fstab
rm -f /root/anaconda-ks.cfg
rm -f /root/install.log
rm -f /root/install.log.syslog
find /var/log -type f -delete

This will serve as my base file. You can change the root password and the time zone, and also add additional post-customization tasks.

Once we have created these files, we are now in a position to create the template.

Creating VM templates using Oz

We will now build a VM using the Oz tool and convert its disk by running the following command:

oz-install -p -u -d1 -a myCentOS.ks centos64.tdl

The system uses the kickstart file in order to complete the installation. We can use a similar method for Ubuntu, using a pre-seed file, and achieve the same thing.

The machine disk will be created in the /var/lib/libvirt/images/ folder, with the file name centos64.dsk. (Please note the <name> tag in the tdl file.)

We will now need to convert this to the QCOW2 format. We do this using the qemu-img tool:

qemu-img convert /var/lib/libvirt/images/centos64.dsk -O qcow2 /root/centos64_x86_64.qcow2

Now we have an image that we can upload to our Glance repository, either using the command line tool or using Horizon.

Uploading the image

Let's upload the image using the Horizon portal. Log in to the portal as admin and navigate to the Admin dashboard and the Images panel.

Click on the Create Image button.

Uploading the image

Once you fill in the details and click the Upload button, the image will be uploaded in the Glance repository.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset