building an ubuntu 10.04 virtual server

having floundered through this a couple of times now, i’ve decided to write the process down so that i don’t have to spend half a day doing this each time. note that this is based on a basic ubuntu 10.04 server system with normal updates as of 2010.6.7.

the criteria i’m working with will involved working remotely with the server itself. this means that i’ll be assuming public IP addresses will be needed/used and that at no point will the server’s console be touched after the initial setup. i’m also installing multiple 10.04 virtual machines on top of a single 10.04 host machine. each one will have its own IP, but each will be cloned from an initial template vm.

i’m using ‘.img’ as an identifier for each image file. while i don’t show it in this doc, i’d recommend having a path somewhere on the host server that holds all of the image files. this way you have a single backup/restore/management location.

GETTING THE SERVER READY
install the tools you’ll need

sudo apt-get install python-virtinst bridge-utils

now change your network card’s config to be bridged. if you’re currently using eth0, you’ll want to comment that out and use br0 instead. your config should look like :

auto lo
iface lo inet loopback

# The primary network interface
#auto eth0
#iface eth0 inet dhcp
#iface eth0 inet static
# address SOME_IP_ADDRESS
# netmask SOME_NETMASK
# gateway SOME_GATEWAY

auto br0
iface br0 inet static
address SOME_IP_ADDRESS
netmask SOME_NETMASK
gateway SOME_GATEWAY
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off

you should reboot now so that this config is in place. in theory you could do a simple network restart, but i’ve had some weirdness happen when doing that. better to make sure this comes up as expected from a boot.

MAKING YOUR TEMPLATE
now download the ubuntu server iso. note that this is assuming that you want to install an ubuntu vm. if not, then obtain the iso you need for whatever you’re doing.


wget -c http://ubuntu.positive-internet.com/releases/lucid/ubuntu-10.04-server-i386.iso

now setup this vm. this will start the process, and you’ll need to use vnc to access the public IP of the host machine to setup the template as if it were a new system.


sudo virt-install -n template -r 256 -f template.img -s 4 -c ubuntu-10.04-server-i386.iso --connect=qemu:///system --vnc --vnclisten=0.0.0.0 -v

change values as needed, but that should be a fairly robust virtual server.

now vnc to the IP of the host machine and you’ll basically be looking at the install boot for 10.04 (or whatever). if installing another 10.04 system, which from now on i’m assuming you will do, hit F4 and choose the virtual machine option. now install as normal.

this will take a VERY long time compared to a normal install. make sure to select ‘basic’ and ‘openssh’ at least from the tasksel.

once installation is completed, you’ll want to boot it as normal. this means you need to start it from the host machine.


sudo virsh start template

in a few seconds you should be able to vnc into the host IP again and login to the new vm’s command line. do so. you’ll want to go ahead and modify the template to have all the defaults you’ll want for future virtuals. if you want to be really fancy, you should go ahead and write in a script here that will do some of the post-cloning work for you (see later).

now halt the template vm. you shouldn’t need to login into it again.

you’ll now want to edit your template.xml file

sudo vi /etc/libvirt/qemu/template.xml

delete the following line :

<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' keymap='en-us'/>

replace the section that looks like this :

<interface type='network'>
<mac address='52:54:00:7c:04:dd'/>
<source network='default'/>
</interface>

with this :

<interface type='bridge'>
<mac address='52:54:00:7c:04:dd'/>
<source bridge='br0'/>
</interface>

but keep that line handy as you’ll add/remove it each time you setup a new virtual machine.

redefine your vm.

sudo virsh define /etc/libvirt/qemu/template.xml

MAKING YOUR CLONES
now that you have a decent template, you can start cloning it to make your workhorse virtual machines. this is pretty quick and easy now that you’ve got a template.

replace NEW_HOSTNAME with the hostname you’ll be using for the new virtual.


sudo virt-clone -o template -n NEW_HOSTNAME -f NEW_HOSTNAME.img --connect=qemu:///system

now edit the xml file for this new virtual.

sudo vi /etc/libvirt/qemu/NEW_HOSTNAME.xml

add the following line into the config. out of habit, i usually do this after the mouse config line.

<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' keymap='en-us'/>

and define the new virtual machine.

sudo virsh define /etc/libvirt/qemu/NEW_HOSTNAME.xml

and start it.

sudo virsh start NEW_HOSTNAME

now you can vnc into this new host. you’ll need to do this so you can setup the IP info and change the hostname on it. change all instances of ‘template’ in the following files to match your new hostname.


sudo vi /etc/hostname
sudo vi /etc/hosts

edit the interface config to use the IP you want.


sudo vi /etc/network/interfaces

remove the net rules. this is key.


sudo rm /etc/udev/rules.d/70-persistent-net.rules

and update resolve.conf if your template used different nameservers.

halt the virtual machine.


sudo halt

on the host, run


watch 'virsh list'

once you see the new virtual disappear, you’ll want to clean it up.

now edit the xml file for this new virtual.

sudo vi /etc/libvirt/qemu/NEW_HOSTNAME.xml

remove the vnc line.

<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' keymap='en-us'/>

and define the new virtual machine. again.

sudo virsh define /etc/libvirt/qemu/NEW_HOSTNAME.xml

and start it.

sudo virsh start NEW_HOSTNAME

and you might as well autostart it if you want to use it a lot.

sudo virsh autostart NEW_HOSTNAME

if you want to remove something from the autostart list, you’ll have to delete the link located at : “/etc/libvirt/qemu/autostart/VIRTUAL_SERVER_NAME”.

that’s it.

building an ubuntu 10.04 virtual server

Leave a Reply