Creating a CentOS VM image for OpenStack

When you install OpenStack, you will get a very simple Linux VM image (Cirros) to test your installation. This works fine for testing purposes, but you will probably want to add some VM images for CentOS or Ubuntu. Although there are predefined VM images available, they may not suit your needs. This guide will create a CentOS VM image with a tool called oz-install.

The oz-install tool automatically installs operating systems into files that represent disk images. The input is an XML file representing the operating system and packages to be installed. The one we will use for CentOS 6.5 is as follows (centos65.tdl):

<template>
   <name>centos_65_x86_64</name>
   <description>CentOS 6.5 x86_64 template</description>
   <os>
      <name>CentOS-6</name>
      <version>5</version>
      <arch>x86_64</arch>
      <install type='url'>
         <url>http://ftp.nluug.nl/pub/os/Linux/distr/CentOS/6.5/os/x86_64/</url>
      </install>
   </os>
</template>

We also use a kickstart file to describe the installation process. It contains settings about the timezone, keyboard, users, etc. The one we will use is as follows (centos65.ks):

install
text
key --skip
keyboard us
lang en_US.UTF-8
skipx
network --device eth0 --bootproto dhcp
rootpw changeme
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
timezone --utc Europe/Amsterdam
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

Install CentOS 6.5 (a lower version may also work) and use the following command to install oz-install and its dependencies:

yum -y install kvm libvirt oz qemu-kvm

Now we can tell oz-install to use both files and create the actual VM image:

oz-install -p -u -d1 -a centos65.ks centos65.tdl

When this step is finished, we can convert the resulting image file to the QCOW2 format:

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

The last step is actually uploading the VM image to OpenStack (can be done through the command line or the GUI) and start using it.

openstack-centos-vm-image

5 thoughts on “Creating a CentOS VM image for OpenStack

  1. Permalink  ⋅ Reply

    Nivas

    February 7, 2014 at 12:52pm

    Thats perfect! Short and needed information

  2. Permalink  ⋅ Reply

    Dennis

    April 2, 2014 at 11:26am

    Bedankt werkt perfect!
    Wat ik me nog wel af vroeg… Ik wil een vm image maken van een CENTOS minimal install.
    Nu loop ik als ik een iso gebruik steeds vast in het laatste stukje. Er lijk een scherm of popup te zijn tijdens de installatie waar hij niet door heen komt..

    Nu heb ik geprobeerd de iso te mounten, maar het lukt me niet om deze goed aan te roepen in de *tdl file.

    Ideeen?

  3. Permalink  ⋅ Reply

    Pian

    September 5, 2014 at 5:59am

    Hi,

    Today i was creating image for centos but getting failed like this :

    “Fetching the original media
    Attempting to get the lock for /var/lib/oz/isos/CentOS-65x86_64-url.iso
    Got the lock, doing the download
    Fetching the original install media from http://ftp.nluug.nl/pub/os/Linux/distr/CentOS/6.5/os/x86_64//images/boot.iso
    Cleaning up after install
    Traceback (most recent call last):
    File “/usr/bin/oz-install”, line 159, in
    customize or generate_icicle)
    File “/usr/lib/python2.6/site-packages/oz/RedHat.py”, line 659, in generate_install_media
    customize_or_icicle)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 1772, in _iso_generate_install_media
    self._get_original_iso(url, force_download)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 1404, in _get_original_iso
    self._get_original_media(isourl, self.orig_iso, force_download)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 911, in _get_original_media
    oz.ozutil.http_download_file(url, fd, True, self.log)
    File “/usr/lib/python2.6/site-packages/oz/ozutil.py”, line 831, in http_download_file
    c.perform()
    pycurl.error: (6, ‘name lookup timed out’) ”

    The command that i use is :

    # oz-install -p -u -d1 -a centos65.ks centos65.tdl -d3

    Is there any solution for this ?

    Thanks.

  4. Permalink  ⋅ Reply

    Jeff Silverman

    October 1, 2014 at 12:55am

    The command to import the qcow file into an image is

    glance image-create –name Centos_6.5 –is-public=true –container-format=bare –disk-format=qcow2 –file /root/centos65_x86_64.qcow2

Leave a Reply

Your email will not be published. Name and Email fields are required.