Understanding vmbuilder (a bit)

If you are using kvm (or qemu) on Ubuntu, then you can build a vm from scratch with the tool vmbuilder. Install it first by doing this:

aptitude install ubuntu-vm-builder

Creating a virtual machine is simple, but the most examples I’ve found left me back with an useless installation – at best.
What didn’t work for me? Well e.g. this one:

DON’T USE THIS!!!

sudo vmbuilder kvm ubuntu --suite lucid --flavour virtual --arch i386  -o --libvirt qemu:///system

This line works only (also at best) one time, a second time is will produce the error code listed below. Also you won’t be able to use it bridged, but NAT only. Err – who needs NAT? – Anyone?
The hostname bug

--hostname=<my_hostname>

I’ve figured out, that the switch “–hostname=” is needed, else you’ll get the error below. But be carefull, the next time you start it without “–hostname” it will work again, but will delete all of your already generated virtual machines in:

$HOME/ubunutu-kvm/

This is the default location where your virtual guest are stored, and this folder will only contain the new build virtual machine, all other are gone!

The bridge switch

--bridge=br0

In order to use your vm bridged, so you can access it from your own subnet, you’ll need to provide the “–bridge=br0” switch. But prepare you host for the br0 feature. Look for the second ocurance of “/etc/network/interfaces” here: http://blog.mymediasystem.net/uncategorized/vmware-kvm-migration-guide/
Some folks start modifying /etc/vmbuilder/libvirt/libvirtxml.tmpl and replaced the whole interface part with this here:

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

Well, it’s working like this also, but why should I do that? The bridge switch is working also. Maybe it didn’t before Lucid (10.04).

Providing user and password

--user=<my_username> --pass=<my_password> --name=<my_name_or_nick>

To make it round, I’d suggest to provide a new user and password. Just imagine you plan to set up a new exposed web-host, and you forget to change –user=ubuntu and –pass=ubuntu. You don’t need to be a hero to own this machine! You can also provide your real name or something “–name=<my_name_here>”

Architecture

--arch=amd64

or

--arch=i386

Select your target architecture. You can look up KVM’s site to see, if your OS is supported for a certain bitting.

Static IP

--ip=<my_ip>

If you’re setting up a server, why would you want to use DHCP?

Ubuntu Version

--suite=<your_desired_ubuntu_version>

For a server an LTS (long term support) is not such a bad idea, so I’m using Lucid (10.04) in this case.

The whole story
Ok, now you can put the pieces together and invoke this on your console (even on a fast connection it took for me up to half an hour):

USE THIS:

vmbuilder kvm ubuntu --suite=lucid --flavour=virtual --arch=amd64 -o --hostname=lucid64 --ip 192.168.1.100 --user john --name lill_joe --pass johnspw --libvirt=qemu:///system --bridge=br0

Starting

virt-manager

Your machine will have another name, but will automatically show up in the virt-manager. You can modify the preferences of your guest there, or perform a

virsh dumpxml <my_vmname> > <my_vmname>.xml

to create an xml file, which you can edit with the editor of your choice.
Let’s say your Ubuntu installation is called ubuntu_1, then you can modify your xml file, undefine your vm first and define it again with the new settings:

virsh -c qemu:///system undefine ubunut_1

Make your changes to the ubuntu_1.xml now, and do a

virsh -c qemu:///system define ubuntu_1.xml
virsh start ubuntu_1

–hostname-Bug

~# vmbuilder kvm ubuntu --suite=lucid --flavour=virtual --arch=amd64 -o --bridge=br0 --libvirt=qemu:///system
2011-05-14 19:18:32,396 INFO    : Calling hook: preflight_check
2011-05-14 19:18:32,444 INFO    : Calling hook: set_defaults
2011-05-14 19:18:32,445 INFO    : Calling hook: bootstrap
2011-05-14 19:31:04,343 INFO    : Calling hook: configure_os
2011-05-14 19:31:20,347 INFO    :
2011-05-14 19:31:20,347 INFO    : Current default time zone: 'Etc/UTC'
2011-05-14 19:31:20,351 INFO    : Local time is now:      Sat May 14 17:31:20 UTC 2011.
2011-05-14 19:31:20,351 INFO    : Universal Time is now:  Sat May 14 17:31:20 UTC 2011.
2011-05-14 19:31:20,351 INFO    :
Extracting templates from packages: 100%
2011-05-14 19:32:28,283 INFO    :
2011-05-14 19:32:28,283 INFO    : Current default time zone: 'Etc/UTC'
2011-05-14 19:32:28,287 INFO    : Local time is now:      Sat May 14 17:32:28 UTC 2011.
2011-05-14 19:32:28,287 INFO    : Universal Time is now:  Sat May 14 17:32:28 UTC 2011.
2011-05-14 19:32:28,287 INFO    : Run 'dpkg-reconfigure tzdata' if you wish to change it.
2011-05-14 19:32:28,287 INFO    :
2011-05-14 19:33:53,467 INFO    : start: Unknown parameter: JOB
2011-05-14 19:33:55,217 INFO    : Calling hook: post_install
2011-05-14 19:33:55,219 INFO    : Calling hook: preflight_check
Traceback (most recent call last):
  File "/usr/bin/vmbuilder", line 24, in
    cli.main()
  File "/usr/lib/python2.6/dist-packages/VMBuilder/contrib/cli.py", line 117, in main
    hypervisor.install_os()
  File "/usr/lib/python2.6/dist-packages/VMBuilder/hypervisor.py", line 60, in install_os
    self.call_hooks('preflight_check')
  File "/usr/lib/python2.6/dist-packages/VMBuilder/distro.py", line 66, in call_hooks
    call_hooks(self, *args, **kwargs)
  File "/usr/lib/python2.6/dist-packages/VMBuilder/util.py", line 158, in call_hooks
    getattr(plugin, func, log_no_such_method)(*args, **kwargs)
  File "/usr/lib/python2.6/dist-packages/VMBuilder/plugins/libvirt/__init__.py", line 54, in preflight_check
    if hostname in self.all_domains() and not self.vm.overwrite:
AttributeError: 'Libvirt' object has no attribute 'vm'

Notes
I’ve been using ubuntu-vm-builder version 0.12.4
My Ubuntu is a 10.04 LTS aka Lucid Lynx x86_64:

Linux server 2.6.32-31-server #61-Ubuntu SMP Fri Apr 8 19:44:42 UTC 2011 x86_64 GNU/Linux