[OpenStack Beginner's Guide for Ubuntu 11.04] – Image Management
There are several pre-built images for OpenStack available from various sources. You can download such images and use them to get familiar with OpenStack. You can refer to http://docs.openstack.org/cactus/openstack-compute/admin/content/starting-images.html for details on using such images.
For any production deployment, you may like to have the ability to bundle custom images, with a custom set of applications or configuration. This chapter will guide you through the process of creating Linux images of Debian and Redhat based distributions from scratch. We have also covered an approach to bundling Windows images.
There are some minor differences in the way you would bundle a Linux image, based on the distribution. Ubuntu makes it very easy by providing cloud-init package, which can be used to take care of the instance configuration at the time of launch. cloud-init handles importing ssh keys for password-less login, setting hostname etc. The instance acquires the instance specific configuration from Nova-compute by connecting to a meta data interface running on 169.254.169.254.
While creating the image of a distro that does not have cloud-init or an equivalent package, you may need to take care of importing the keys etc. by running a set of commands at boot time from rc.local.
The process used for Ubuntu and Fedora is largely the same with a few minor differences, which are explained below.
In both cases, the documentation below assumes that you have a working KVM installation to use for creating the images. We are using the machine called ‘client1′ as explained in the chapter on “Installation and Configuration” for this purpose.
The approach explained below will give you disk images that represent a disk without any partitions. Nova-compute can resize such disks ( including resizing the file system) based on the instance type chosen at the time of launching the instance. These images cannot have ‘bootable’ flag and hence it is mandatory to have associated kernel and ramdisk images. These kernel and ramdisk images need to be used by nova-compute at the time of launching the instance.
However, we have also added a small section towards the end of the chapter about creating bootable images with multiple partitions that can be be used by nova to launch an instance without the need for kernel and ramdisk images. The caveat is that while nova-compute can re-size such disks at the time of launching the instance, the file system size is not altered and hence, for all practical purposes, such disks are not re-sizable.
Creating a Linux Image – Ubuntu & Fedora
The first step would be to create a raw image on Client1. This will represent the main HDD of the virtual machine, so make sure to give it as much space as you will need.
kvm-img create -f raw server.img 5G
Download the iso file of the Linux distribution you want installed in the image. The instructions below are tested on Ubuntu 11.04 Natty Narwhal 64-bit server and Fedora 14 64-bit. Most of the instructions refer to Ubuntu. The points of difference between Ubuntu and Fedora are mentioned wherever required.
Boot a KVM Instance with the OS installer ISO in the virtual CD-ROM. This will start the installation process. The command below also sets up a VNC display at port 0
sudo kvm -m 256 -cdrom ubuntu-11.04-server-amd64.iso -drive file=server.img,if=scsi,index=0 -boot d -net nic -net user -nographic -vnc :0
Connect to the VM through VNC (use display number :0) and finish the installation.
For Example, where 10.10.10.4 is the IP address of client1:
vncviewer 10.10.10.4 :0
During the installation of Ubuntu, create a single ext4 partition mounted on ‘/’. Do not create a swap partition.
In the case of Fedora 14, the installation will not progress unless you create a swap partition. Please go ahead and create a swap partition.
After finishing the installation, relaunch the VM by executing the following command.
sudo kvm -m 256 -drive file=server.img,if=scsi,index=0,boot=on -boot c -net nic -net user -nographic -vnc :0
At this point, you can add all the packages you want to have installed, update the installation, add users and make any configuration changes you want in your image.
At the minimum, for Ubuntu you may run the following commands
sudo apt-get update sudo apt-get upgrade sudo apt-get install openssh-server cloud-init
For Fedora run the following commands as root
yum update yum install openssh-server chkconfig sshd on
Also remove the network persistence rules from /etc/udev/rules.d as their presence will result in the network interface in the instance coming up as an interface other than eth0.
sudo rm -rf /etc/udev/rules.d/70-persistent-net.rules
Shutdown the Virtual machine and proceed with the next steps.
Extracting the EXT4 partition
The image that needs to be uploaded to OpenStack needs to be an ext4 filesystem image. Here are the steps to create a ext4 filesystem image from the raw image i.e server.img
sudo losetup -f server.img sudo losetup -a
You should see an output like this:
/dev/loop0: :16908388 ($filepath)
Observe the name of the loop device ( /dev/loop0 in our setup) when $filepath is the path to the mounted .raw file.
Now we need to find out the starting sector of the partition. Run:
sudo fdisk -cul /dev/loop0
You should see an output like this:
Disk /dev/loop0: 5368 MB, 5368709120 bytes 149 heads, 8 sectors/track, 8796 cylinders, total 10485760 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00072bd4 Device Boot Start End Blocks Id System /dev/loop0p1 * 2048 10483711 5240832 83 Linux
Make a note of the starting sector of the /dev/loop0p1 partition i.e the partition whose ID is 83. This number should be multiplied by 512 to obtain the correct value. In this case: 2048 x 512 = 1048576
Unmount the loop0 device:
sudo losetup -d /dev/loop0
Now mount only the partition(/dev/loop0p1) of server.img which we had previously noted down, by adding the -o parameter with value previously calculated value
sudo losetup -f -o 1048576 server.img sudo losetup -a
You’ll see a message like this:
/dev/loop0: :16908388 ($filepath) offset 1048576
Make a note of the mount point of our device(/dev/loop0 in our setup) when $filepath is the path to the mounted .raw file.
Copy the entire partition to a new .raw file
sudo dd if=/dev/loop0 of=serverfinal.img
Now we have our ext4 filesystem image i.e serverfinal.img
Unmount the loop0 device
sudo losetup -d /dev/loop0
You will need to tweak /etc/fstab to make it suitable for a cloud instance. Nova-compute may resize the disk at the time of launch of instances based on the instance type chosen. This can make the UUID of the disk invalid. Hence we have to use File system label as the identifier for the partition instead of the UUID.
Loop mount the serverfinal.img, by running
sudo mount -o loop serverfinal.img /mnt
Edit /mnt/etc/fstab and modify the line for mounting root partition(which may look like the following)
UUID=e7f5af8d-5d96-45cc-a0fc-d0d1bde8f31c / ext4 errors=remount-ro 0 1
LABEL=uec-rootfs / ext4 defaults 0 0
Fetching Metadata in Fedora
Since, Fedora does not ship with cloud-init or an equivalent, you will need to take a few steps to have the instance fetch the meta data like ssh keys etc.
Edit the /etc/rc.local file and add the following lines before the line “touch /var/lock/subsys/local”
depmod -a modprobe acpiphp # simple attempt to get the user ssh key using the meta-data service mkdir -p /root/.ssh echo >> /root/.ssh/authorized_keys curl -m 10 -s http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key | grep 'ssh-rsa' >> /root/.ssh/authorized_keys echo "AUTHORIZED_KEYS:" echo "************************" cat /root/.ssh/authorized_keys echo "************************"
Kernel and Initrd for OpenStack
Copy the kernel and the initrd image from /mnt/boot to user home directory. These will be used later for creating and uploading a complete virtual image to OpenStack.
sudo cp /mnt/boot/vmlinuz-2.6.38-7-server /home/localadmin sudo cp /mnt/boot/initrd.img-2.6.38-7-server /home/localadmin
Unmount the Loop partition
sudo umount /mnt
Change the filesystem label of serverfinal.img to ‘uec-rootfs’
sudo tune2fs -L uec-rootfs serverfinal.img
Now, we have all the components of the image ready to be uploaded to OpenStack imaging server.
Registering with OpenStack
The last step would be to upload the images to Openstack Imaging Server glance. The files that need to be uploaded for the above sample setup of Ubuntu are: vmlinuz-2.6.38-7-server, initrd.img-2.6.38-7-server, serverfinal.img
Run the following command
uec-publish-image -t image --kernel-file vmlinuz-2.6.38-7-server --ramdisk-file initrd.img-2.6.38-7-server amd64 serverfinal.img bucket1
For Fedora, the process will be similar. Make sure that you use the right kernel and initrd files extracted above.
uec-publish-image, like several other commands from euca2ools, returns the prompt back immediately. However, the upload process takes some time and the images will be usable only after the process is complete. You can keep checking the status using the command ‘euca-describe-images’ as mentioned below.
You can register bootable disk images without associating kernel and ramdisk images. When you do not want the flexibility of using the same disk image with different kernel/ramdisk images, you can go for bootable disk images. This greatly simplifies the process of bundling and registering the images. However, the caveats mentioned in the introduction to this chapter apply. Please note that the instructions below use server.img and you can skip all the cumbersome steps related to extracting the single ext4 partition.
euca-bundle-image -i server.img euca-upload-bundle -b mybucket -m /tmp/server.img.manifest.xml euca-register mybucket/server.img.manifest.xml
The status of the images that have been uploaded can be viewed by using euca-describe-images command. The output should like this:
localadmin@client1:~$ euca-describe-images IMAGE ari-7bfac859 bucket1/initrd.img-2.6.38-7-server.manifest.xml css available private x86_64 ramdisk IMAGE ami-5e17eb9d bucket1/serverfinal.img.manifest.xml css available private x86_64 machine aki-3d0aeb08 ari-7bfac859 IMAGE aki-3d0aeb08 bucket1/vmlinuz-2.6.38-7-server.manifest.xml css available private x86_64 kernel localadmin@client1:~$
Creating a Windows Image
The first step would be to create a raw image on Client1, this will represent the main HDD of the virtual machine, so make sure to give it as much space as you will need.
kvm-img create -f raw windowsserver.img 20G
OpenStack presents the disk using aVIRTIO interface while launching the instance. Hence the OS needs to have drivers for VIRTIO. By default, the Windows Server 2008 ISO does not have the drivers for VIRTIO. Sso download a virtual floppy drive containing VIRTIO drivers from the following location
and attach it during the installation
Start the installation by running
sudo kvm -m 1024 -cdrom win2k8_dvd.iso -drive file=windowsserver.img,if=virtio,boot=on -fda virtio-win-1.1.16.vfd -boot d -nographic -vnc :0
When the installation prompts you to choose a hard disk device you won’t see any devices available. Click on “Load drivers” at the bottom left and load the drivers from A:\i386\Win2008
After the Installation is over, boot into it once and install any additional applications you need to install and make any configuration changes you need to make. Also ensure that RDP is enabled as that would be the only way you can connect to a running instance of Windows. Windows firewall needs to be configured to allow incoming ICMP and RDP connections.
For OpenStack to allow incoming RDP Connections, use euca-authorize command to open up port 3389 as described in the chapter on “Security”.
Shut-down the VM and upload the image to OpenStack
euca-bundle-image -i windowsserver.img euca-upload-bundle -b mybucket -m /tmp/windowsserver.img.manifest.xml euca-register mybucket/windowsserver.img.manifest.xml