Ubuntu Developer Week: an introduction to vmbuilder

The following is the transcript of an Ubuntu Developer Week session that I ran today on irc about vmbuilder (#ubuntu-classroom on freenode).  It was a first for me and was in fact quite fun to do.  If you have a good subject, it's a great way to make it known, so feel free to contact Daniel Holbach to submit your subject for the next edition.

<JontheEchidna> Next up is g VMBuilder to create tests environments -- Søren Hansen and Nicolas Barcet will entertain you with virtual machines and the fantastic vmbuilder. Need a clean test environment for something? Don't want to run the latest development release on actual hardware yet? These two fine men have the answer for you.
<nijaba> Welcome everyone and thanks for attending this presentation about
                   __                   ___       __                  
                  /\ \              __ /\_ \     /\ \                 
__  __    ___ ___\ \ \____  __  __/\_\\//\ \    \_\ \     __   _ __  
\ \ \_/ |/\ \/\ \/\ \ \ \L\ \ \ \_\ \ \ \ \_\ \_/\ \L\ \/\  __/\ \ \/
 \ \___/ \ \_\ \_\ \_\ \_,__/\ \____/\ \_\/\____\ \___,_\ \____\\ \_\
  \/__/   \/_/\/_/\/_/\/___/  \/___/  \/_/\/____/\/__,_ /\/____/ \/_/
 
Originally Soren Hansen, the author and maintainer of vmbuilder was supposed to run this presentation with me, but he unfortunately got a bad flu and I'll therefore will have to do this alone.  
Well not completely alone, I bet mathiaz and zul are around to make sure I don't say anything stupid ;)

So, what is vmbuilder?
vmbuilder is a python tool that allows to create virtual machine images on the fly, without the need to start an installer in an hypervisor.
vmbuilder can create images for KVM, Xen, VMWare and for Amazon EC2.
On a reasonably powerfull machine with a local package cache (for example apt-proxy or apt-cacher), the creation of a minimal virtual server (aka JeOS) can take less than a minute.

As a python tool, it is first of all a library with a very powerfull plugin architecture.  
One of the plugins that is provided is a CLI (command line interface), and anyone could write another plugin to provide a GTK interface or use the vmbuilder library to create a web service.
Apart for the front end plugin mechanism, there are 2 other types of plugins that are used in vmbuilder: Hypervisors and Distributions.  
The distribution plugin would allow for someone to easily extend vmbuilder for other distributions, but at this time the only available one is Ubuntu.  
The same is true for the hypervisor plugin, and it seems that jmarsden|work is voluteering some work :)
Contributions are always welcome!

The first implementation of vmbuilder was on hardy as a shell script that used to be called ubuntu-vm-builder.
When Soren rewrote in python for intrepid, the source package was renamed python-vm-builder and contains a binary package called ubuntu-vm-builder which is just a wrapper to the old CLI to maintain upward compatibility.

Now that we have covered what vmbuilder is (in a nutshell), let's explore what you could use it for as a developer.

A couple scenarios come to mind, but I'm sure there are many others.
1/let's imagine that you want to test something (a program that you wrote, an upgrade, a bug) in a clean environement.
chroot is generally great for this, but is not really what we could call a complete environment.
Using vmbuilder, you can generate a virtual machine on the fly that contains whichever package and configuration that you want almost as quickly as you would generate a chroot environment.
You can then start the machine in your favorite hypervisor and start testing.
Some of the great options vmbuilder provides are the ability to chose wich series you want (dapper, gutsy, hardy, intrepid, ...) and, to the difference of pre-built images that you would duplicate, is building machines from the latest versions of the packages in the repositories.
Since vmbuilder does not require any user interaction to run, this can be scripted and made part of build or test process.

2/let's imagine that you want to deliver your package/packages/application/integration as a pre-built virtual machine for various hypervisor (providing so called "virtual-appliances", for example).
The traditional way would imply creating reference virtual images for each target environements and each time you updated your code, after building your package you would have to:
  a- start each vm
  b- update your package in each vm manually
  c- clean up the vm
  d- save it
  e- send it to testers/users
Since vmbuilder knows about your various Hypervisors, it is very easy to integrate this work in your build scripts so that your virtual images are almost ready to ship once your build process is completed.

To make things even easier, vmbuilder includes three "sub-scripts" features that can be specified:
  a- an "exec" script that is called during the machine build process just after finishing the the installation.  The script can chroot within the target machine and make additional changes.
  b- a "first-boot" script that is copied to the virtual machine and executed in batch mode the first time the vm will boot
  c- a "first-login" script that is copied as well to the vm and executed as the user the first time someone logs-in to the vm (and therefore allows to perform some final interactive setup).
 
ok, now that I have covered the main use cases, I'll try to answer the first few questions before we move on

QUESTION: What do I need to use KVM?
You need a processor that supports hardware virtualization
there is a detailed step by step procedure to test this on https://help.ubuntu.com/community/KVM/Installation

QUESTION: are there plans to also support qemu/kqemu?
That really should no be too hard to had, but it is not on any official roadmap, AFAIK
contributions, again, are very welcome

QUESTION: vmbuilder on gutsy?
unfortunately not.  ubuntu-vm-builder was first developped on hardy
we badly need kpartx for it, and it is not available before hardy

QUESTION: which one should I choose between KVM, Xen, VMWare and for Amazon EC2 (if I got a machine that supports hardware virtualization and I want to do ubuntu package testing for instance) ???
it all really depends on your use case
VMWARE (esx) is certainly the most complete virtualization solution available today
but it has some hard dependency, and is not really free software
Xen is nice if your processor does not have hardware virtualization extension
but then it will only provide paravirtualized os virtualization
and it is a BULKY thing to maintain in a distro
so that's mainly why, if your use case is server virtualization, we prefer KVM

To start using vmbuilder, you need to first install its package:
$sudo apt-get install python-vm-builder
note that if you want to create EC2 images, you will need to install the python-vm-builder-ec2, which, for licence reasons on the AMI tools, had to be made a separate package and placed into multiverse.

Once the install is complete, the first thing to do is to have a look a the help.
As I said before, python-vm-builder uses a plugin mechanism.  
This implies that you will have different sets of options depending on the hypervisor and distribution that you pick.
This is the reason why the man page or a basic 'sudo vmbuilder --help' will only list options that are common to all hypervisors or distributions.
To get all options displayed, you need to specify your target hypervisor and distribution.  

For example try doing:
$sudo vmbuilder kvm ubuntu --help
or
$sudo vmbuilder xen ubuntu --help 
and notice the additional options provided in each case.

So now, assuming that you all are going to use vmbuilder intensively, let's start setting up a package caching system.
Soren likes apt-cacher while I've preferred apt-proxy.  
Since the later is what I know and is quite simple to enable, I'll invite you to:
$sudo apt-get install apt-proxy
once this is done, apt-proxy will immediately start listenning on all your interfaces on port 9999.  
There are multiple options you can set on /etc/apt-proxy/apt-proxy.conf, but the default will work very nicely for our purpose.
In order for apt-proxy to be used by vmbuilder automatically, we could very well add the option "--install-mirror http://127.0.0.1:9999/ubuntu" each time we would invoke vmbuilder, but since I am quite lazy, I have defined this as a permanent option in my ~/.vmbuilder.cfg file by putting in the following two lines:
[ubuntu]
install_mirror = http://127.0.0.1:9999/ubuntu
in fact, any command line option that can be passed to vmbuilder can be added to this config file, and you can even specify an additional config file that uses the same format using the --config option.
Note that the format of the config file changed between hardy and interpid... so I assume your are all on intrepid or jaunty here

Another option that speeds up the creation of vm is the --tmpfs one, which will use a ramdisk to store temporary file.
I have added in my ~/.vmbuilder.cfg file the following 2 lines:
[DEFAULT]
tmpfs = -

Ok, so we are now ready to start building our first image.  
For this example we'll build and equivalent of an Intrepid JeOS using the following command:

$sudo vmbuilder kvm ubuntu --suite intrepid --flavour virtual --arch i386 --verbose

I do not think the above options need much comments, do they?

The first time you will build a machine for a new suite, it will obviously take a bit longer as apt-proxy is not loaded.

While your vm is building, let's open the floor to questions.  


QUESTION: sry to dwell on this but why could KVM only be used for server testing? cant I run X.org inside a KVM?
well, sure you can run X.org inside a KVM
but the interface has not really been tuned to provide you with the best experience possible for full desktop virtualization
but, I do use kvm for testing gnome once in a while
and apart from a few quirk, it is quite useable
I would not recommend it for any real usage on a daily basis though

QUESTIONwhat I have done in the past is boot from an .iso and then use rtegular tools to make mods. This is a CLI approach based on, listing all the pkgs you want to install in a text file, for example?
yes, this is the general approach, yes

QUESTION: when running vmbuilder, it printed a bunch of these: "Can not write log, openpty() failed (/dev/pts not mounted?)"   is that a problem?
no, actually the reason why you saw those is because we enabled the --verbose option
but, unless vmbuilder stops before sayng it is done, these error are just transient

QUESTION: what about custom downloads.. eg not debian/ubuntu packages.. not covered by this approach?
this is the reason why we have multiple scripts options
as I described earlier
you could have a --exec script
that downloads something with wget
and untars it wherever you want

Another very inresting part of vmbuilder are templates
Have a look at what is inside /etc/vmbuilder/
Each of these directories contain base templates for file that we'll use for configuration during the build process

For example, to enable bridging in our vm, we'll need to modify the libvirt template:

$mkdir -p mytemplates/libvirt
$cp /etc/vmbuilder/libvirt/* mytemplates/libvirt/

we can then edit mytemplates/libvirt/libvirtxml.tmpl
and change the interface section of it to:

    <interface type='bridge'>
      <source bridge='br0'/>
    </interface>

and then specify which template set to use by adding the following to our command line:

--templates mytemplates


QUESTION: how can I start the kvm I just built?
this brings me to the --libvirt option
assuming that libvirt is installed and running
(if it is not, have a look at https://help.ubuntu.com/community/KVM/Installation )
then you can pass "--libvirt qemu:///system" to the command line
which will automatically add the generated vm as a livirt domain
which can be started immediately
https://help.ubuntu.com/community/KVM/Managing#From%20the%20shell gives an overview of how to manage running vm
I you do not want to use libvirt, see https://help.ubuntu.com/community/KVM/Directly

QUESTION: kvm is a standard'ish ubuntu VM I take it?
yes, it is the official virtualization technology since hardy
have a look at http://www.ubuntu.com/products/whatisubuntu/serveredition/technologies/virtualization

For additional reference, apart for the --help, I would like to point you to a tutorial I have writen on using VMBuilder which is available at https://help.ubuntu.com/community/JeOSVMBuilder

QUESTION: hey, I started run.sh and I get to the prompt... but what is the password to my new VM ??
by default user and password are ubuntu
of course there is a command line option to change that too :)

Hope this was a useful session for most of you and feel free to drop by #ubuntu-virt (on freenode) at any time if you have additional questions or just want to say hi :)
Thanks for attending and asking very good questions everyone :)

Share this

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Very interesting article.

Very interesting article. Thanks

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.