Creating qcow2 CentOS Image for OpenStack

This page has not been liked. Updated 4/12/13 4:34 PM by KarveTags: None


Creating a CentOS 6 image

Same instructions can be used for Redhat 6 and Fedora

These instructons were run on Ubuntu 11.10 Server x86_64. Run the following in a VNC Session on the hypervisor, you will need to install the vnc4server, vncviewer.

# Install the kvm-pxe to avoid the "pci_add_option_rom: failed to find romfile "pxe-rtl8139.bin"" message

sudo apt-get install kvm-pxe

# Download the CentOS DVD iso, alternatively check the mirrors at http://isoredirect.centos.org/centos/6/isos/x86_64/ for downloading relevant iso.

wget http://mirrors.usc.edu/pub/linux/distributions/centos/6.2/isos/x86_64/CentOS-6.2-x86_64-LiveDVD.iso

# Create a qcow2 image that represents the main HDD of the virtual machine and swap space. (Note: 5GB is not sufficient)

kvm-img create -f qcow2 centos.qcow2 10G

# Boot the Virtual Machine from DVD iso, select an unused vnc

sudo kvm -m 1024 -cdrom CentOS-6.2-x86_64-LiveDVD.iso -drive file=centos.qcow2,if=virtio,index=0 -boot d -net nic -net user -nographic -vnc :2

# This command will block

# Connect to VM using vncviewer from another window

vncviewer :2

# This will show you the desktop that was booted form the DVD iso file. Now we install the CentOS on the attached image file.

# Double Click on the "Install to Hard Drive" icon on the desktop and click Next

# Select the US English and click Next

# Select the default Storage and click Next, Click "Yes, Discard any data" on the popup

# Leave the default hostname and Click Next

# Default TimeZone and click Next

# Type the root password (at least 6 characters long) and confirm and click Next

# Select "Create custom layout", the "Review and modify partitioning layout" will automatically be checked and click Next

# Select the Free and Click on Create, Standard Partition, Create

# Select / as the Mount Point with ext4 as the File System Type and 9120 as the Size (MB), Fixed Size and click OK

# Select the Free and Click on Create, Standard Partition, Create

# Don't select the "Mount Point", instead Select swap for the "File System Type", the Mount Point will be grayed out, select the "Fill to maximum allowable size" and click OK

# Now you show see the vda1 with ext4 and vda2 as swap, Click Next, Format and Click on "Write Changes to Disk"

# Leave the defaults for the Boot loader and click Next

# It will copy the live image to hard drive, complete the post-installation. After completion, it will show the "Congratulations" message. Click Close.

# Select System and "Shut Down" the VM

# Now back to the bash prompt on the hypervisor, start the VM from the qcow2 image without the DVD iso

sudo kvm -m 1024 -drive file=centos.qcow2,if=virtio,index=0,boot=on -boot c -net nic -net user -nographic -vnc :2

# Reconnect to VM using vncviewer from the second window

vncviewer :2

# This will show you the Welcome page. Click Forward, Accept license, Forward

# Do not create any user (you may create if you want to, but not necessary if you only want root), click Forward



# Select Date time and click Forward# Select Date and click forward

# Ignore the "Insufficient memory to configure kdump!" message, click OK and Finish.

# Click on "Other", type root and hit Enter and your password and hit Enter and login to the VM.

# Close the root superuser warning message.

# Rightclick on the desktop and Open Terminal

# Install the latest updates

sudo yum -y update

# Enable ssh daemon to autostart

sudo chkconfig --level 345 sshd on

# Check that the levels for 345 are on

sudo chkconfig --list | grep sshd

# The firewall is set by default to allow ssh on port 22, you can check it by going to System, Administration, Firewall to verify

# Shutdown the VM (Careful - Don't shutdown the hypervisor, run this in VM's Terminal)

shutdown -h now

# Now back to the bash prompt on the hypervisor

The centos.qcow2 is ready to be added to glance

 

Adding the image to glance

# Run the following command to get the Access token Id. Note set the hostname to the glance hostname if it is different from current hostname

myhostname=`hostname`

accesstokenid=`curl -d '{"auth":{"passwordCredentials":{"username": "adminUser","password": "password"}}}' -H "Content-type: application/json" http://$myhostname:35357/v2.0/tokens | python -mjson.tool | grep -A3 access | grep id | sed "s/.*:.*\"\(.*\)\"/\\1/"`

echo $accesstokenid

# or

curl -d '{"auth":{"passwordCredentials":{"username": "adminUser","password": "password"}}}' -H "Content-type: application/json" http://`hostname`:35357/v2.0/tokens | python -mjson.tool

# Note the access token id above and set the accesstokenid manually



sudo glance -A $accesstokenid -v add name="centos-qcow2" is_public=True disk_format="qcow2" < centos6.qcow2

Convert disk format to raw

Instead of qcow2, you can convert the image to raw and add it to glance as follows. Note: set the accesstokenid as above before adding to glance.

qemu-img convert -O raw centos.qcow2 centos.raw

sudo glance -A $accesstokenid -v add name="centos-raw" is_public=True < centos.raw



List the images

nova --username=adminUser --apikey=password --projectid=adminTenant --url=http://`hostname`:5000/v2.0/ image-list



Notes

  1. The first ext4 partition with mount point "/" was created to allow injecting ssh key into the /root/.ssh/authorized_keys file. The code in /var/lib/python-support/python2.6/nova/virt/disk.pyc is hardcoded to mount at offset 0 on the image.
  2. Regardless of whether we add a qcow2 image or a raw image, the /var/log/nova/nova-compute.log will show that when the instance disk is created, a qcow2 image is created from the base image. For example:

    qemu-img create -f qcow2 -o cluster_size=2M,backing_file=/var/lib/nova/instances/_base/b3f0c7f6bb763af1be91d9e74eabfeb199dc1f1f_sm /var/lib/nova/instances/instance-0000002b/disk
  3. The backing_file=/var/lib/nova/instances/_base/$fileid_sm is always a sparse raw image. So if a qcow2 image was added, during first provisioning it is first extracted as _base/$fileid.part, converted to raw format _base/$fileid.converted and cached on the hypervisor in the _base directory after the .converted file is renamed to _base/$fileid (the _sm is appended for tiny flavor). Each instance is created as a qcow2 from this base image.

  4. Even after images are deleted from glance, the cached images on the hypervisors remain in /var/lib/nova/instances/_base unnecessarily utilizing disk space. You have to delete them manually if you want to recover disk space.
  5. You can check the files created when a instance is created as follows. Note the first disk is the qcow2 for the CentOS that was installd after all updates were installed. The second disk.local is the qcow2 for the additional disk and depends on flavor selected during provisioning.

    nova@mngdsrvc5:/var/lib/python-support$ ls /var/lib/nova/instances/instance-0000002e/ -lash

    total 1.7G

    4.0K drwxrwxr-x 2 nova nova 4.0K 2012-03-08 17:03 .

    4.0K drwxr-xr-x 4 nova nova 4.0K 2012-03-08 16:57 ..

    4.0K -rw-rw---- 1 libvirt-qemu kvm 1 2012-03-08 17:04 console.log

    1.7G -rw-r--r-- 1 libvirt-qemu kvm 1.7G 2012-03-08 17:22 disk

    4.1M -rw-r--r-- 1 libvirt-qemu kvm 6.1M 2012-03-08 17:03 disk.local

    4.0K -rw-rw-r-- 1 nova nova 2.4K 2012-03-08 16:57 libvirt.xml

  6. You can access the user-data within the VM using

    curl -m 10 -s http://169.254.169.254/latest/user-data

    or

    wget -O out.txt http://169.254.169.254/latest/user-data;cat out.txt
  7. The ssh key should already be injected into the /root/.ssh/authorized_keys. If your file system does not allow key injection, you can also access it directly access the key as follows from within the VM:

    curl -m 10 -s http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key

    or

    wget -O out.txt http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key;cat out.txt

    and create a simple service that will add that key into the ~/.ssh/authorized_keys during startup of the VM instance.

 

 

Creating an openSUSE image

Use the same instructions as above replacing centos with opensuse. The iso to use for installation can be downloaded from http://download.opensuse.org/distribution/11.4/iso/openSUSE-11.4-DVD-x86_64.iso. Instead of yum, you will need to use zypper, For example:

zypper install openssh curl

zypper update

Use yast2 to enable port 22 for ssh in Firewall