Pages

Wednesday, October 29, 2014

Installing ubuntu cloudimages with uvtool

Introduction

With ubuntu 14.04 LTS, ubuntu released a new tool to simply install cloud images and create a virtual machine in minutes. The virtual machines it creates are also managable by libvirt. In this short tutorial I'm going to show how to install and use the uvtool and also briefly show how to use cloud-init with these images.

Install

To install uvtool in ubuntu 14.04 LTS you can simply use the package manager.
$ apt-get update
$ apt-get install uvtool
If you want to use uvtool for ubuntu 12,04 LTS you can use the ppa:
$ sudo add-apt-repository ppa:uvtool-dev/trunk
$ sudo apt-get update
$ sudo apt-get install uvtool

Cloudimages

Uvtool contains two main commands uvt-simplestreams-libvirt and uvt-kvm. The uvt-simplestreams-libvirt can be used to synchronize the ubuntu cloud images. To see which images are downloaded you can use:
$ uvt-simplestreams-libvirt query
release=trusty arch=amd64 label=release (20140927)
Uvtool creates a libvirt storage pool to store the cloud images.
$ virsh pool-list
 Name                 State      Autostart
-------------------------------------------
 default              active     yes
 uvtool               active     yes
To download all current cloudimages you can use uvt-simplestreams-libvirt sync. This will of-course take a long time and cause a lot of traffic. To selectively download the images you can use the filters release and/or arch. To download the latest trusty image for amd64 you would use:
$ uvt-simplestreams-libvirt sync release=trusty arch=amd64
or precise:
uvt-simplestreams-libvirt sync release=precise arch=amd64
This way you only download the images you need.
To see the volumes in the libvirt storage pool:
$ virsh vol-list uvtool
 Name                 Path
------------------------------------------------------------------------------
 x-uvt-b64-Y29tLnVidW50dS5jbG91ZDpzZXJ2ZXI6MTQuMDQ6YW1kNjQgMjAxNDA5Mjc= /var/lib/uvtool/libvirt/images/x-uvt-b64-Y29tLnVidW50dS5jbG91ZDpzZXJ2ZXI6MTQuMDQ6YW1kNjQgMjAxNDA5Mjc=

Create VM

Using the command uvt-kvm we can create a virtual machine based on the cloudimage we previously downloaded in the libvirt storage pool,
If you don't already created a pair of ssh-keys, create them with:
$ ssh-keygen
Create a trusty amd64 VM with the following command:
$ uvt-kvm create myvm release=trusty arch=amd64
This wil create and start a libvirt kvm virtual machine with default libvirt values.The command will return almost immediately. The creation of the VM though still continues in the background. If you want to know when the virtual machine is ready, use the uvt-kvm wait command.
$ uvt-kvm wait --insecure myvm
To find the ip of the just created machine (read the uvt-kvm manpage for the restrictions this command has):
$ uvt-kvm ip myvm
To stop en remove this virtual machine, issue the following command:
$ uvt-kvm destroy myvm
The destroy command will remove all persistent state associated with a VM, including VM-specific disk image files and the VM definition itself. It will not destroy the cloud images you downloaded with uvt-simplestreams-libvirt.
Although you can use uvt-kvm to create and remove virtual machine, this doesn't limit you to use the VM's with virsh.
$ virsh list
 Id    Name                           State
----------------------------------------------------
 8     myvm                         running
See also the ADVANCED USAGE section of the uvt-kvm's manpage.
Some more advanced create examples (see manpage for details):
# create a vm with 2 cpu cores, 1024 MiB memory and 32 Gib disk size.
uvt-kvm create --memory 1024 --disk 32 --cpu 2 myvm
# copies the script initscript.sh to the vm and executes once at startup, also installs the listed packages.
uvt-kvm create --run-script-once initscript.sh --packages heirloom-mailx,vim-nox myvm

Connect to the VM

You can connect to the running VM using the uvt-kvm ssh command or directly with ssh.

connect

$ uvt-kvm ssh myvm --insecure

search ip

uvt-kvm ip myvm
192.168.122.145

connect using ssh

ssh ubuntu@192.168.122.145

Cloud-init

It is possible to initialize your VM using cloud-init more directly. See http://cloudinit.readthedocs.org/en/latest/ for documentation
This overides default cloud-init from uvtool, that's why you also have to configure access like ssh keys or user/password combo.

Example:

create file init.cfg:

#cloud-config

ssh_authorized_keys:
  - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrhB9U7WKdqwx4hDoLshqKfhEZHVzZ57pS8REkYnfl3ZH+uyKDHbH3pgIRF+Pa44VETK6DusYB93+tVWi/v5YfYfGqHUbkEyPFo81DQccLVUZA2+c/OJFTjIhOHAvAmWz3IrSZldRkCHGQJ5t707Q4aJqAkXaqQRhaTbgKJwdjE6ebcpPqdGeacLrlFlbNvMB2snn9S9VdyayHoj9VGTfnVoW2ylwvArJ0KunDq2IQOos0T040T6u11bXc6OMHkavMi/EyOpJ0ITD3Jw4Vz2cc5qxx7TuJvlbX5gFssRjt+4XCgtfgjwBhdLZHtwl25ZR/dk7ZuyLD1iE5uPGbWGaV mykey@host

write_files:
  - content: |
      Test file to see if cloud-init works
    path: /tmp/test.txt

packages:
  - pwgen
  - vim-nox
  - puppet
  - vim-puppet

Then issue the following commands for testing:

uvt-kvm create --user-data init.cfg myvm
uvt-kvm wait --insecure myvm
uvt-kvm ssh --insecure myvm
uvt-kvm destroy myvm