Installing KVM in Centos, Redhat or Fedora

KVM is available in RHEL 5.4. I finally managed to find the time to play around it over the weekend. I don’t think it is as matured as xen but having said that, I do agree with the approach – Kernel virtualisation. Redhat is promoting kvm very hard and you can see it from their website and mailing list. If you are using redhat systems, better upgrade to kvm as xen might be phased out soon.

Unlike xen, I found installing kvm abit of pain…. I am willing to share what I know and this is what this blog is for.

Check your cpu before you start because kvm only supports full virtualisation, ie your /proc/cpuinfo must have either the vmx (intel) or the svm (amd) tag.

Next, I suggest you do a minimal clean centos/rhel/fedora installation. After rebooting, log in as root and add packages as needed. We will start with the kvm package.

yum groupinstall kvm

I didnt install the virtualisation package because it will bring in the xen hypervisor and stuffs – don’t want it for now.

The pain with kvm is that it doesnt come with a network bridge (xen wins!). we will need to add one if you want your virtual machine be in the same network as your physical machine. Assuming my network card is eth1:

cd /etc/sysconfig/network-scripts/
vim ifcfg-eth1

my eth1 config looks like this

# D-Link System Inc RTL8139 Ethernet
DEVICE=eth1
ONBOOT=yes
HWADDR=00:13:46:3a:14:55
BRIDGE=kvmbr0

now we need to create the kvm bridge

vim ifcfg-kvmbr0

my ifcfg-kvmbr0 looks like this:

DEVICE=kvmbr0
TYPE=Bridge
BOOTPROTO=static
GATEWAY=10.1.1.254
IPADDR=10.1.1.201
NETMASK=255.255.255.0
ONBOOT=yes

Its now time to restart the network

service network restart

my routing information looks like this

[root@home network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.1.1.0        0.0.0.0         255.255.255.0   U     0      0        0 kvmbr0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 kvmbr0
0.0.0.0         10.1.1.254      0.0.0.0         UG    0      0        0 kvmbr0

If you have firewall turned on, turn them off for the bridge. In /etc/sysctl.conf, add

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

reload sysctl and libvirtd and we are ready to go

sysctl -p /etc/sysctl.conf
chkconfig libvirtd on
service libvirtd restart

start the virt-manager gui

virt-manager &

I am lazy to post screen shots. Redhat has done a good job already. Just follow the steps in the
redhat kvm guide. Start from Step 3.

If you have selinux running, remember to read the security guide

If you are using lvm as disk, you need to fix the fcontext. My volume group is named as “vm”. So my selinux command is:

semanage fcontext -a -t virt_image_t "/dev/mapper/vm(.*)?"

That’s it for now. Merry Christmas!!

Xen: Booting into run level 1

Booting into run level 1 from grub is useful in times of troubleshooting. I am using centos and my problem is when I am using xen, everything is automated by the pygrub bootloader.

I tried to google for a solution for a long time and couldn’t find anything. Someone suggested changing the inittab. Yes, I know but I want to be able to boot into different run levels from grub! I eventually experimented and found a quick trick myself.

We start off by looking at the xen boot parameters.

Doing a ‘xm list -l’ gives something like:

(domid 1)
(uuid 735cd9c5-9387-dbf9-2421-037e28e5df5e)
(vcpus 1)
(cpu_weight 1.0)
(memory 128)
(shadow_memory 0)
(maxmem 128)
(bootloader /usr/bin/pygrub)
(features )
(name hera)
(on_poweroff destroy)
(on_reboot restart)
(on_crash restart)
(image
(linux
(ramdisk /var/lib/xen/boot_ramdisk.LWoAIP)
(kernel /var/lib/xen/boot_kernel.CXG2Ju)
(args ‘ro root=LABEL=/’)
)
)

To get the bootloader to boot up in run level 1, I need to somehow append a ‘1’ to the end of the ‘ro root=LABEL=/’

The pygrub creates the vm for us with the right parameters. So having an args tag and overriding the args isn’t an option. The trick is to add a root parameter like so:

root = “LABEL=/ ro 1”

Lets run “xm list -l” again.

(image
(linux
(ramdisk /var/lib/xen/boot_ramdisk.7zQG0Q)
(kernel /var/lib/xen/boot_kernel.8h2uu9)
(args ‘ro root=LABEL=/’)
(root ‘ro LABEL=/ 1’)
)
)

the ‘root’ tag now correctly appends after the args which should overide the args. This should enable my virtual machine to boot into run level 1.

and it did. YAY!

* note: need to create xen instance from command line, ie “xm create xen-config” in order for it to work. does not work if run from virt-manager