Using Ubuntu cloud images in KVM

By : Thejaswi Puthraya

Quite a few of our clients are powered by Amazon EC2 or Rackspace and we use Ubuntu LTS releases for our servers. Canonical provides EC2 AMIs and Openstack images for all their releases. By using these JeOS images on the server as well as on the development platform reduces the dev/prod parity which we discussed in a previous post.

In this post, we'll see how to setup the Ubuntu cloud images in the local KVM hypervisor. Unlike the previous post, we'll use only the virt-manager to do the initial provisioning but you can be assured that there is a command line way to do all of the same.

Head over to Ubuntu cloud images and select the appropriate release (or daily build). At the time of writing the article, 12.04 is the latest LTS release and can be downloaded from here. I have downloaded the 64-bit (or amd64) images (ubuntu-12.04-server-cloudimg-amd64-root.tar.gz) since I use a 64-bit machine for my development and the servers are all 64-bit file. This compressed file contains a virtual hard disk, virtual floppy disk and a kernel.

Let us now use this virtual hard disk as a base for our images and create new guest VM based on this disk.:

$ qemu-img create -b ubuntu-12.04-server-cloudimg-amd64.img -f qcow2 new_vm.img

The qemu-img command creates a new_vm.img image with qcow2 format using the ubuntu-12.04-server-cloudimg-amd64.img as the backing image.

Right click on the connection in virt-manager and select New and provide a name for the new virtual machine and select the Import existing disk image option. In the next step, provide the path of the image just created (ie new_vm.img) and optionally select the OS type and version. In the third step, select the RAM allocated to the VM and the number of cores and in the final step check the Customize configuration before install before filling other options.

In the Customization screen, click on the section that says Disk 1 and set the storage format to qcow2 and apply the changes.

Configuring the disk

Now all that is left is to set the boot order and there are two methods:

  • Using the floppy disk image
  • Using cloud-init on the hard disk image

Using the floppy disk for booting

If you don't see a floppy disk in the list of devices in the customization screen, click on Add Hardware and add a new Storage device of type Floppy disk and check the Select managed or other existing storage and provide the path to the floppy disk image from the download and set the Storage format to raw.

Adding Floppy Disk

Select the Boot Options section in the customization screen after creating the floppy disk and select the Floppy in the Boot device order. Start the installation and after that run the VM and you are done!

After booting the new VM, you should see the GRUB screen in a few seconds as below.

Grub Screen

Choose the option that you prefer and you will be redirected to the login prompt after the bootup.

This method is useful if you have a provisioning or a metadata service like Orchestra or Cobbler though not a compulsion.

Using cloud-init on the hard disk

CloudInit is an init script that performs some basic configuration and house keeping tasks on guest VMs like setting the hostname, generating SSH keys etc

Go to the Boot Options section and select the Hard disk from the Boot device order and change the Direct kernel boot sub section.

Provide the kernel file (the file ending with vmlinuz-virtual) from the download in the Kernel path and the following values in the Kernel arguments:

ro init=/usr/lib/cloud-init/uncloud-init root=/dev/vda ds=nocloud ubuntu-pass=initialpassword
Configuring the hard disk

Start the installation and in a few moments you have a brand new guest VM.

You now have exactly the same packages as an official and freshAmazon AMI or Rackspace image and so you don't have to break your head worrying about the dependency hell and enjoy more time developing.


Related Posts


Can we help you build amazing apps? Contact us today.

Comments

Deepak Shetty 19th Nov., 2013

Nice article, helped a lot
One quick Q
--- Is there a way to know the root creds for the above case
If i select ubuntu:ubuntu grub menu option.. I can only logn as ubuntu user... later if i need to change to root (su -).. whats the root passwd ?

thanx,
deepak

commmenttor
Patrick Viet 19th Jan., 2014

Deepak, if these images are anything like the images used on Amazon (I'd say 99% chance that yes they are the same)

Then
- there isn't a root password
- the ubuntu user can sudo su without a password

Which means if you want to set a root password (not recommended but whatever), you can simply type "sudo passwd root" as ubuntu user to set that root passwd.

My advice: learn about sudo rather than su.

commmenttor
Deepak Shetty 10th June, 2014

Hi, I got few more Qs in general on the cloud enabled images
1) Whats the difference between

trusty-server-cloudimg-amd64-disk1.img
and
trusty-server-cloudimg-amd64-root.tar.gz

From what i know, former is a complete OS image, but latter is just the rootfs and hence needs a kernel supplied for it to be used to boot, is that right ?

2) How is it different to use xx-disk1.img Vs xxx-root.tar,gz contents to create a VM using virt-manager ? I assume the disk.img has cloud-init running so i won't be able to connect to the guest if i create a VM using that (since virt-manager won't have the setup needed for cloud-init to work?)

3) The Boot options
ro init=/usr/lib/cloud-init/uncloud-init root=/dev/vda ds=nocloud ubuntu-pass=initialpassword

Are they specified officially somewhere by the ubuntu folks ? Asking bcoz i don't see this specified anywhere in http://cloud-images.ubuntu.com/trusty/current/ from where i picked the image.

commmenttor
Deepak Shetty 10th June, 2014

Thejaswi,
I think in the article above , ubuntu-12.04-server-cloudimg-amd64-root.tar.gz is incorrect, it should have been ubuntu-12.04-server-cloudimg-amd64.tar.gz

commmenttor
© Agiliq, 2009-2012