Building an image

Now that we've looked at getting a disk image into Glance, let's investigate how a cloud image is built. A cloud image is just a sealed disk image with cloud-init included. A sealed disk image is a file that has an operating system installed in it and has had all the host-specific items removed from it. Cloud-init is a post-boot process that checks the metadata service of OpenStack and asks for post-boot commands that should be run on the launched instance. We'll see cloud-init's use cases in Chapter 5, Instance Management, and Chapter 9, Orchestration; for now, we'll just make sure it's included in the cloud image we build. To build the image, we'll use virt-install. There are quite a few other options. If you're familiar with a different disk-image-building tool, use that if you like. This is just one example of how to build one of these images. Go ahead and make sure virt-install is installed. The following command accomplishes this:

build-host# yum install -y virt-install httpd

Httpd was installed here too because we need a web server to serve the kickstart. Apache is not needed if you have an alternate web server to serve your kickstart. An automated Fedora installation is accomplished via the kickstart. A great place to get a baseline kickstart is from the collection of kickstarts at https://git.fedorahosted.org/cgit/cloud-kickstarts.git/tree/generic/ that Fedora uses to build cloud images.

These could even be adapted to build a different rpm-based distribution cloud image. Pull down one of those kickstart files and place it in /var/www/html/. Also, make sure that Apache is running. Issue the following command to accomplish this:

build-host# service httpd start

Now that we have something to build with and a kickstart to define what should be built, let's kick off a cloud image build, as follows:

build-host# qemu-img create -f qcow2 my_cloudimage.img 10G
build-host# sudo virt-install -n my_cloud_image -r 2048 --vcpus=2 
  --network=default --graphics=spice --noautoconsole 
  --noreboot -v --disk=path=my_cloudimage.img,format=qcow2 
  -l http://dl.fedoraproject.org/pub/linux/releases/20/Fedora/x86_64/os/ 
  -x "ks=http://192.168.122.1/my_kickstart_file.ks"

The first command creates an empty qcow2 formatted disk image. The second line spawns a virtual machine in libvirt named my_cloud_image with 2 GB of RAM and 2 vCPUs using the default libvirt network. The virtual machine boots using the kernel and the RAM disk in the install tree from the dl.fedoraproject.org URL. The ks= option is a kernel parameter. In this example, the kernel pulled from dl.fedoraproject.org knows how to pull down the kickstart being served from the local Apache instance on the libvirt network's gateway IP address. Once the installation is complete, the virtual machine can be torn down and the disk image that you created is now an installed cloud image. A final optional step is to sparsify the disk image. There is plenty of documentation on the Internet that can explain what it means to sparsify a disk image better than I can. Use your Internet-searching expertise to read more about what this command does and its benefits. To reiterate, this is optional and will not prevent the final image from being useful. Issue the following command to sparsify the disk image:

build-host# virt-sparsify --compress my_cloudimage.img sparsified.qcow2

If you sparsified, the resulting sparsified disk image is what is imported into Glance. If you didn't sparsify, then just import the resulting disk image from virt-installer. Note that the sparsify command used the .img extension and the .qcow2 extension. You can use these interchangeably. All the commands you run on these disk images don't really care what the file extension is as they inspect the contents of the disk image to complete their operations:

control# glance image-create --name Fedora --is-public true --disk-format qcow2 -- container-format bare --file sparsified.qcow2

Now, let's be frank here. All that really happened was that an operating system was installed into a standard qcow2 disk image with cloud-init included in the package list and the host's networking was set to DHCP. That means that if you want to do this manually instead of using virt-install, you could absolutely launch a virtual machine and do a manual installation. Then, make sure that cloud-init is installed and just before you shut down the machine, run commands to set the networking to DHCP and seal the image, somewhat like the following command:

cloud-image# cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
DEVICE="eth0"
ONBOOT="yes"
BOOTPROTO="dhcp"
TYPE="Ethernet"
EOF
cloud-image# rm -f /etc/ssh/ssh_host*
cloud-image# rm /etc/udev/rules.d/70-persistent-net.rules
cloud-image# halt –p

The udev rule may not actually exist, but it doesn't hurt to make sure it's not there. What these commands do is remove any host-specific identification. The MAC address and ID are removed from the networking device configuration and the SSH host keys are removed. They're regenerated on boot if they don't exist, and the udev network persistence configuration is removed, which is also regenerated on boot if it's needed. This list is not exclusive. In the unlikely event that you come across other host-specific things, you should make sure that they are removed to make the image generic. However, on a fresh basic Fedora installation, this list should work well to seal the image. Once you've run these commands and shut down the virtual machine, the disk image is ready to be imported into Glance. If you boot the virtual machine back up outside of OpenStack, you will have to partially reseal the image, as some of the things you just deleted will be regenerated when you boot up using the disk image again. This does not apply to instances you boot in OpenStack. This only applies to manually spawning a virtual machine using the disk image outside of OpenStack. Once the image has been imported into Glance, OpenStack will handle things properly and not taint the Glance image. The imported image will be stored in the Glance registry and copied out to the compute nodes using which the instances will run. The instances will run using copies of the original disk images stored in Glance.

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

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