Create a KVM Based CentOS 6 OpenStack Cloud Image

Wednesday, September 24, 2014

For some reason CentOS does not keep their historical CentOS releases available online in OpenStack cloud image format. They only have the very latest CentOS OpenStack cloud image available. If I’m wrong and they do keep them somewhere, please let me know. However, the current and historical CentOS repositories are of course available online and you can create your own custom OpenStack cloud image from them.

This post will be a step-by-step manual process of creating a CentOS 6.5 OpenStack cloud image. Many of the steps were derived from the OpenStack CentOS Image docs.

The following steps require you to have access to a Linux distribution that has KVM/QEMU and libvirt installed.

Install KVM/QEMU and libvirt

If you happen to be using a Linux distribution that does not have any virtualization packages installed, the following packages will be needed depending on which Linux distribution you are running.


Install the necessary repository packages:

yum groupinstall "Virtualization" "Virtualization Platform"

Enable and start the libvirtd service on CentOS 6 or RHEL 6:

chkconfig libvirtd on

service libvirtd start

Enable and start the libvirtd service on Fedora 17 and newer:

systemctl enable libvirtd

systemctl start libvirtd


Install the necessary repository packages:

sudo apt-get install qemu-kvm libvirt-bin virt-manager

Start the libvirt-bin service:

service libvirt-bin start

Create the Virtual Machine

You can create the CentOS 6.5 virtual machine using the following command. It will pull everything it needs from the CentOS repository seen in the location parameter. Depending on the speed of your internet connection, the install may take anywhere from 5 - 20 minutes:

sudo virt-install --virt-type kvm --name centos-6.5 --ram 1024 --location= --disk path=/tmp/centos-6.5-vm.img,size=5 --network network=default --graphics vnc,listen= --noautoconsole --os-type=linux --os-variant=rhel6 --extra-args="noverifyssl ks="

Once the install finishes, the virtual machine will automatically shutdown. Start it back up and proceed to the next section.

Post-install Steps

With the base install of CentOS 6.5 in place, it is time for you to configure the operating system to work with OpenStack.

Using the KVM console or SSH, login as the root user (password is centos).

Add the EPEL 6 repository so you can install the cloud-init packages:

yum install -y

Install all of the necessary cloud-init packages along with some other packages:

yum install -y cloud-init cloud-utils cloud-utils-growpart

Replace the default /etc/cloud/cloud.cfg with the following contents:

 - default

disable_root: 1
ssh_pwauth:   0

locale_configfile: /etc/sysconfig/i18n
mount_default_fields: [~, ~, 'auto', 'defaults,nofail', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys:   0
ssh_genkeytypes:  ~
syslog_fix_perms: ~

 - bootcmd
 - write-files
 - resizefs
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - rsyslog
 - users-groups
 - ssh

 - mounts
 - locale
 - set-passwords
 - timezone
 - puppet
 - chef
 - salt-minion
 - mcollective
 - disable-ec2-metadata
 - runcmd

 - rightscale_userdata
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message

  distro: rhel
    name: centos
    lock_passwd: True
    shell: /bin/bash
    sudo: ["ALL=(ALL) NOPASSWD: ALL"]
    cloud_dir: /var/lib/cloud
    templates_dir: /etc/cloud/templates
  ssh_svcname: sshd

# vim:syntax=yaml

In order for the OpenStack Instance to communicate with the metadata service turn off network zero configuration:

echo "NOZEROCONF=yes" >> /etc/sysconfig/network

In order to see the OpenStack Instance console within the Horizon Dashboard and when using the nova console-log command, you will need to add and remove some kernel boot parameters.

Open /boot/grub/menu.lst and append the following parameters to the kernel line:

console=tty0 console=ttyS0,115200n8

In the same file on the same line remove any references to the following parameters:

rhgb quiet

There are some hardcoded UUID and MAC addresses in the network configuration files and udev. Remove these entries with the following commands:

rm -f /etc/udev/rules.d/70-persistent-net.rules

PRIMARY_INTERFACE=$(ip route list match | awk 'NR==1 {print $5}')
sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-$PRIMARY_INTERFACE
sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-$PRIMARY_INTERFACE

Clean up yum:

yum clean all

Remove everything in the /tmp directory:

rm -rf /tmp/*

Clean up the last logged in users logs:

rm -f /var/log/wtmp /var/log/btmp

Clean up the command history:

history -c

Shutdown the virtual machine:

shutdown -h now

Compress the Image

The virt-install command above created the virtual machine’s backing disk file at /tmp/centos-6.5-vm.img and the final file size will be about 5 GB. Typically, OpenStack images are not this large, and there is a lot of useless space within the current image. So, use the qemu-img command to compress the image to about 500 MB:

qemu-img convert -c /tmp/centos-6.5-vm.img -O qcow2 /tmp/centos-6.5.img

Upload the Image into the Glance Repository

With the final image created, it is time to upload it to the Glance Repository in your OpenStack environment.

You can upload the image into the Glance Repository from a local file path using the following command:

glance image-create --name centos-6.5 --disk-format=qcow2 --container-format=bare --file /tmp/centos-6.5.img

Or, you can upload the image into the Glance Repository from a URL using the following command:

glance image-create --name centos-6.5 --disk-format=qcow2 --container-format=bare --copy-from

With the image now uploaded into your Glance Repository, you can begin creating OpenStack Instances from it.

Your SSH public key will be injected into the centos user’s directory. So, when you login via SSH, login as that user. If you want to change this user modify the name parameter in the default_user section of the cloud-init config above.

comments powered by Disqus