Create Ubuntu Server Virtual Guest

For anyone looking to setup a virtual environment, this post should save you a bit of headache. There are quite a few paths you can choose when looking to setup some type of virtual management system: Virtualbox, VMware, and KVM just to name a few. Personally, I feel as if KVM is the easiest to use and manage plus it’s free on top of being extremely easy to use via command line.

Through out this tutorial you’ll see Resource links; when I was attempting to do this myself I used several sites to accomplish the subject at hand — these resources are what I used as a guide and may be able to assist you further than I can. Instead of spending hours upon hours googling, I figured it would be nice to include them here :)

I named my server salamander; so that it's easily recognizable throughout this tutorial (feel free to name yours as you please).

Getting Started

Okay so first we’ll need to install the vm builder package

sudo apt-get install ubuntu-vm-builder

Now if we wanted to, we could create the (very basic, with all options set to default) guest. but let’s not do that

sudo ubuntu-vm-builder kvm hardy --libvirt qemu:///system

The above command simply tells the ubuntu-vm-builder that we want to create a virtual guest from the ubuntu hardy build and that we want to use virsh to manage it. Now there are several other arguments you can pass which will help define your guest more so than the default. Below is a link that can help you in setting up your virtual guest along with how I’ve setup mine. Also keep in mind, you will need to append −−libvirt qemu:///system to the end of the code you receive from the link provided below as it won’t be included.

Resource: https://help.ubuntu.com/community/KVM/CreateGuests

sudo ubuntu-vm-builder kvm hardy --arch 'amd64' --mem '128' --rootsize '4096' --swapsize '1024' --kernel-flavour 'generic' --hostname 'salamander' --mirror 'http://archive.ubuntu.com/ubuntu' --components 'main' --name 'sean' --user 'sean' --pass 'zyos' --ip '192.168.1.101' --mask '255.255.255.0' --net '192.168.1.0' --bcast '192.168.1.255' --gw '192.168.1.1' --dns '192.168.1.1' --addpkg 'openssh-server' --libvirt qemu:///system

This process could take a few minutes depending on your internet connection. The tutorial on Ubuntu’s help site claims you can use a local mirror to speed up the process, although I’ve yet to get it to work properly. Most of the arguments are self explanatory. −−user would be the username you want use along with −−pass for the password. −−hostname would be the name for the guest. −−addpkg is for any additional packages you’d like to install; as you can see we have openssh-server (for some reason it's not installed by default). You can add as many packages as you’d like, just separate them by space. 
 ex: −−addpkg 'openssh-server nano bind9'

For all intents and purposes you could be done here. You could simply launch virsh and start the guest but, there’s a few more things you’ll probably want to accomplish before getting to that step: such as getting your guest to access the LAN. Otherwise half the arguments you passed to the vm-builder would have been for nothing. So what we’re going to do is create a bridged interface for your guest to access.

Creating a Bridged Interface

Resource: http://wiki.libvirt.org/page/Networking#Debian.2FUbuntu_Bridging

You’ll need to edit your network interfaces

sudo nano /etc/network/interfaces

Now find your main interface, for this example we are using eth0, and remove the allow-hotplug eth0 line replacing it with auto br0. Then change the next line from iface eth0 to iface br0. Afterward, define the interface as a bridge and specify its ports.

Your configuration should now look something like this:

auto br0 iface br0 inet static address 192.168.1.100 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.255 gateway 192.168.1.1 bridge_ports eth0 bridge_stp off bridge_maxwait 5

Once that’s done, save and exit then bring up the new interface using:

ifup br0

Finally append the following lines to /etc/sysctl.conf

net.bridge.bridge-nf-call-ip6tables = 0 net.bridge.bridge-nf-call-iptables = 0 net.bridge.bridge-nf-call-arptables = 0

Now we load the new settings:

sysctl -p /etc/sysctl.conf

You should now have a “shared physical device”, to which guests can be attached and have full LAN access. Now we just need to edit the guest and inform it to use the bridge we just created. Edit /etc/libvirt/qemu/salamander.xml and look for a line that begins with: <interface type='network'>.

Your going to need to change network to bridge then look for <source network='default'/> and change network to bridge and default to br0. Afterward, append <model type='virtio'/> and you don’t need to edit the mac address.

Resource: https://help.ubuntu.com/community/KVM/Networking#Configuring ubuntu-vm-builder to create bridged guests

So that bracket should now look like this — your mac address will be different

<interface type='bridge'> <mac address='52:54:00:f3:60:2a'/> <source bridge='br0'/> <model type='virtio'/> </interface>

Start ‘er up!

You’re pretty much done, simply open virsh and start your new guest.

sudo virsh

Resource: http://docs.sun.com/source/820-3838-10/chapter3.html

Now that we have virsh open you need to define your new guest

define /etc/libvirt/qemu/salamander.xml

Afterward, turn it on

start salamander

Once you’ve gotten to this point, simply wait a moment or two for the guest to startup (like you would any machine), then you should simply be able to SSH into it using the IP you specified within the initial ubuntu-vm-build statement.

Additional Resources:


Originally published at seanwragg.com on March 31, 2010.

Like what you read? Give Sean Wragg a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.