Friday, November 13, 2009

KVM on Centos 5

I've been a XEN man for sometime now, but yesterday my employer asked me to set up KVM on one of our Centos 5 servers. It was a snap.

Installation

yum install kvm qemu virt-manager libvirt


The next step is to modprobe the kvm module for your architecture. Use the module that is right for you:


modprobe kvm-intel
...or...
modprobe kvm-amd


If all goes well, you should have the kvm module loaded on your system by now. You can check this by running:


/sbin/lsmod | grep kvm


Setup the Bridge

If you want to access the virtual machine from the LAN, you'll need to set up a bridge on one or more of your network interfaces. Once this step is complete, virt-manager will take care of the remaining details when setting up a virtual machine using the GUI.

Check that a default bridge is configured. Run brctl show and you should see something like this.


[root@server ~]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.000000000000 yes


What you should see is a line that includes vnet0. Don't screw the pooch like I did and use virbr0 if vnet0 is not listed. virbr0 is used by NAT and you will hose the system if you go this route. IF vnet0 is not listed, restart networking. If still not present, use Google to resolve before continuing. Your goal is to see this:


bridge name bridge id STP enabled interfaces
virbr0 8000.000000000000 yes
vnet0 8000.000000000000 no


Create a file in network-scripts called ifcfg-vnet0...


vim /etc/sysconfig/network-scripts/ifcfg-vnet0

...and add the following if you want to configure the bridge for eth0:


DEVICE=vnet0
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes

Now add the bridge:


brctl addif vnet0 eth0

Now you'll see:


[root@server ~]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.000000000000 yes
vnet0 8000.003048625732 no eth0


To make this sticky (available after reboot), add BRIDGE=vnet0 to /etc/sysconfig/network-scripts/ifcfg-eth0.

Create the Virtual Machine

It's possible to create a virtual machine from the command line like this:


qemu-kvm -hda windisk.img -cdrom winimg.iso -m 1024 -boot d

However, it's probably easier to manage things using virt-manager. Launch from the UI or from the command line.


virt-manager

Click on the first row to highlight it, right-click and select new to open the wizard. Follow the steps including choice of ISO and where to write the image. When you get to the network screen, choose Shared Physical Device and you will see your bridged eth0 interface. If you skipped bridging, you will not have this option so take a different one.

Once complete, start up the VM. Done.

No comments: