Creating new VM with VMBuilder on Ubuntu 10.04

One inconvenient of VMBuilder is the command line length. There is no way you can remember all the parameters without making mistakes. So I created a small bash script on the host servers I manage to allow to create new VMs without searching all the parameters to submit.

Here is the simple script I use. Feel free to adapt it to your environment:

(You need the python-vm-builder package to use this script.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash

# Configure this before running the command

HOSTNAME=vm4
MEMORY=512
CPUS=1
DISK_SIZE=10000
IP=192.168.1.10

# -- End of configuration

time vmbuilder kvm ubuntu \
 -d /var/lib/libvirt/images/$HOSTNAME \
 -m $MEMORY \
 --cpus $CPUS \
 --arch amd64 \
 --hostname $HOSTNAME \
 --domain mydomain.com \
 --ip $IP \
 --mask 255.255.255.0 \
 --gw 192.168.1.1 \
 --dns 192.168.1.1 \
 --bridge br0 \
 --user myadmin \
 --pass mysecret \
 --suite lucid \
 --rootsize $DISK_SIZE \
 --swapsize $MEMORY \
 --flavour virtual \
 --libvirt qemu:///system \
 --timezone America/Montreal \
 --mirror http://ubuntu.mirror.iweb.ca/ \
 --security-mirror http://ubuntu.mirror.iweb.ca/ \
 --addpkg acpid

The DISK_SIZE is in megabytes so in this example, 10 000 = 10 gigabytes. When you have adapted your vmbuilder command line for your machine and network (netmask, DNS, gateway, etc.) you only have to edit the first 5 variables and you are good to go! It may take 10 to 20 minutes to build the VM depending on your network and server speed.

When the VM is working, you will then be able to login with the user “myadmin” and password “mysecret” with the virt-viewer client from your desktop:

virt-viewer -c qemu+ssh://kvmhost/system vm4

where “vm4” is the hostname of the VM you have just created.