Installing KVM on CentOS 6.4

Install CentOS 6.4

CentOS installation is the same as RedHat installation. See my previous blog on installing CentOS using  a USB stick here http://bit.ly/11dMehl.

Install KVM

Installing KVM is fairly easy, on CentOS you can use the yum groupinstall command to get all the goodies, such as below:

sudo yum groupinstall virtualization

Start libvirt with :

sudo service libvirtd start

Create network bridge if you want to use external IP and not NAT

By default KVM creates a bridge called virbr0, for NAT access from VM’s and add’s the appropriate IPtables rules in both the filter and the NAT tables. I created another bridge for public IP guests. Below are the steps you can follow to create the bridge which I named br0. Keep in mind that you can use bridge ctl utilities, however I did this manually by editing the interface config files.


$ cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
BOOTPROTO=static
IPADDR=<Your-em1-or-eth1-IP-Address>
NETMASK=255.255.255.0
BROADCAST=Your-eth1-or-em1-netmask

$ cat /etc/sysconfig/network-scripts/ifcfg-em1
DEVICE=em1
HWADDR=<Leave-as-is>
BRIDGE=br0
UUID=<Leave-as-is>
ONBOOT=yes
NM_CONTROLLED=yes

The above will give br0 the IP of em1. Em1 is basically the BIOS name of eth0.

Enable IP forwarding

$ cat /proc/sys/net/ipv4/ip_forward
1
$ grep -A1 -i forwarding /etc/sysctl.conf
# Controls IP packet forwarding
net.ipv4.ip_forward = 1

Download ISO image for Guest VM

wget http://mirror.stanford.edu/yum/pub/centos/6.4/isos/x86_64/CentOS-6.4-x86_64-bin-DVD1.iso

Use virt-install or Virtual Machine Manager to create guest VM

sudo virt-install --connect qemu:///system --name vm.example.com \
--ram 1024 --vcpus 1 --disk path=/vm1/vm.example.com.qcow2  \
--network=bridge:virbr0 --os-type=linux --os-variant=rhel6 \
--cdrom /vm1/iso/CentOS-6.4-x86_64-bin-DVD1.iso \
--graphics spice,password=mypassword --autostart

You can use virt-manager gui or virt-install which is command line based to install a VM. In terms of the network, I am using the default bridge virbr0 since the VM will have a private RFC 1918 IP, which in KVM defaults to 192.168.122.X/24 network. Specifying os-type and also os-variant allows KVM to optmize for that particular OS. Using the cdrom method I am able to specify the ISO image to install from. For connecting to the VM console, I am using spice, with the password specified on the command line. (Not secure, but there is a bug with KVM that does not allow spice connections if you specify a default spice password in /etc/libvirt/qemu.conf. The autostart option causes KVM to restart the domain when the host (hypervisor) restarts.

Use Spice or VNC to connect to console of VM and complete install

virt-viewer --connect qemu:///system  vm.example.com

External Links

  1. http://red.ht/17IX7qh
  2. http://libvirt.org/remote.html

4 thoughts on “Installing KVM on CentOS 6.4

  1. Pingback: Managing KVM with Libvirt In Java: Step by Step Tutorial | DoubleCloud.org

  2. autocopiante2013

    hello, is a great pleasure to have met with your post, I have a question for you when you create (create) the BR0 interface, and you assign the IP and broadcast eth1, does the ip of eth1 is fixed or published?, thanks for your response

    Reply
    1. syedaali Post author

      If eth1 has a static IP, then br0 will get the static IP of eth1 and eth1 will not have an IP.
      If you want to avoid editing the network configuration files, you can use this simple command to take care of it:
      # virsh iface-bridge eth1 br0
      This will save you the trouble of figuring out the IP addresses. 🙂

      Reply
    2. syedaali Post author

      Not sure what you mean by fixed or published IP? If you mean DHCP, then the answer is that you should avoid using DHCP if this is a server and use a fixed IP.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s