How to install KVM and setup Virtual Machine?

Host Setup/ Installing KVM:

Install all the packages you might need.

yum -y install @virt* dejavu-lgc-* xorg-x11-xauth tigervnc \
libguestfs-tools policycoreutils-python bridge-utils

If you have use any directories other than /var/lib/libvirt for kvm files, set the selinux context. In this example I use /vm to store my disk image files.

semanage fcontext -a -t virt_image_t "/vm(/.*)?"; restorecon -R /vm

Allow packet forwarding between interfaces.

sed -i 's/^\(net.ipv4.ip_forward =\).*/\1 1/' /etc/sysctl.conf; sysctl -p

Configure libvirtd service to start automatically and reboot.

chkconfig libvirtd on; shutdown -r now

Optionally you can set up bridging which will allow guests to have a network adaptor on the same physical lan as the host. In this example eth0 is the device to support the bridge and br0 will be the new device.

chkconfig network on
service network restart
yum -y erase NetworkManager
cp -p /etc/sysconfig/network-scripts/ifcfg-{eth0,br0}
sed -i -e'/HWADDR/d' -e'/UUID/d' -e's/eth0/br0/' -e's/Ethernet/Bridge/' \
echo DELAY=0 >> /etc/sysconfig/network-scripts/ifcfg-br0
echo 'BOOTPROTO="none"' >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo BRIDGE=br0 >> /etc/sysconfig/network-scripts/ifcfg-eth0
service network restart
brctl show

The host is now ready to start creating kvm guests.

Guest Setup/ Creating Virtual Machine:

Use following command to create the guest:

virt-install -n vm1 --description "vm1" --os-type=Linux --os-variant=rhel6 --ram=2048 --vcpus=1 --disk path=/vm/kvm/vm1.img,bus=virtio,size=25 --graphics vnc,port=5901,listen=,password=phil102 --cdrom=/vm/iso/CentOS-6.8-x86_64-netinstall.iso --network bridge=br0

Note: It could take a considerable amount of time to complete, especially if you have chosen a large, non-sparse disk file on a slow hard drive.

Was this answer helpful? 26 Users Found This Useful (26 Votes)