Thursday 19 August 2010

Installing Linux Kernal-Based Virtual Machine (KVM) on CentOS 5.4 Server step by step

Step 1. Set SELINUX to “disabled”
# vim /etc/selinux/config
SELINUX=disabled
Step 2: Check that hardware support hardware virtualisation
# egrep '(vmx|svm)' --color=always /proc/cpuinfo
Your output should be something like this
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush
dts acpi mmx fxsr sse sse2 ss ht tm syscall nx pdpe1gb rdtscp lm constant_tsc nonstop_tsc pni monitor
ds_cpl vmx smx est tm2 cx16 xtpr popcnt lahf_lm
.......
If you do not get this output, then we can conclude that your hardware does not support virtualisation
Before you do a yum, make sure you have EPEL Repository enabled. For more information see Red Hat Enterprise Linux / CentOS Linux Enable EPEL (Extra Packages for Enterprise Linux) Repository
Step 3: Install the KVM and virtinst (tools to create virtual machines)
yum install kvm kmod-kvm qemu libvirt python-virtinst
Reboot the System
# shutdown -r now
Step 4: Verify that the kernel has is loaded
# lsmod | grep kvm
the output should be something like
kvm_amd 50452 0
kvm 109264 1 kvm_intel
Check that the KVM is installed
# virsh -c qemu:///system list
Id Name State
----------------------------------
Step 5: Setting up of a Network Bridge so that the VM can be accessed from other hosts on the same network
a. Install the Bridge
# yum install bridge-utils
b. Configure the Bridge. Create the “bridge configuration file”. Ensure the BOOTPROTO, BROADCAST, IPADDR, NETMASK and NETWORK are the same as /etc/sysconfig/network-scripts
# vim /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
BROADCAST=192.168.50.255
IPADDR=192.168.50.100
NETMASK=255.255.255.0
NETWORK=192.168.50.0
ONBOOT=yes
c. Modify the /etc/sysconfig/network-scripts/ifcfg-eth0. Amend as follows
# Chelsio Communications Inc T310 10GbE Single Port Adapter
DEVICE=eth0
#BOOTPROTO=static
HWADDR=00:xx:00:xx:00:xx
ONBOOT=yes
BRIDGE=br0
#IPADDR=192.168.50.100
#NETWORK=192.168.50.0
#NETMASK=255.255.255.0
d. Disable NetworkManager
# chkconfig NetworkManager off
# service NetworkManager stop
# chkconfig network on
e. Restart the Network
# service network start
f. Verify that the Network Bridge is working
# ifconfig
br0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx
inet addr:192.168.50.100 Bcast:192.168.50.255 Mask:255.255.255.0
inet6 addr: fe80::210:a7ff:fe05:afeb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:17 errors:0 dropped:0 overruns:0 frame:0
TX packets:53 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1160 (1.1 KiB) TX bytes:14875 (14.5 KiB)

eth0 Link encap:Ethernet HWaddr yy:yy:yy:yy:yy:yy
inet6 addr: fe80::210:a7ff:fe05:afeb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13662 errors:7 dropped:160 overruns:4 frame:0
TX packets:11646 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:15144608 (14.4 MiB) TX bytes:1379942 (1.3 MiB)
Interrupt:74 Base address:0xcc00

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:38 errors:0 dropped:0 overruns:0 frame:0
TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4308 (4.2 KiB) TX bytes:4308 (4.2 KiB)

virbr0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:35 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:9987 (9.7 KiB)
Step 6: Installing Virt-Manager on CentOS on a remote or local server
# yum install virt-manager
================================================================================
Package Arch Version Repository
Size
================================================================================
Installing:
virt-manager x86_64 0.6.1-12.el5 base 1.5 M
Installing for dependencies:
e4fsprogs-libs x86_64 1.41.9-3.el5 base 104 k
gnome-python2-gnomekeyring
x86_64 2.16.0-3.el5 base 17 k
gtk-vnc x86_64 0.3.8-3.el5 base 81 k
gtk-vnc-python x86_64 0.3.8-3.el5 base 12 k
libvirt x86_64 0.6.3-33.el5_5.3 updates 2.0 M
libvirt-python x86_64 0.6.3-33.el5_5.3 updates 137 k
python-virtinst noarch 0.400.3-9.el5_5.1 updates 380 k
xen-libs x86_64 3.0.3-105.el5_5.5 updates 156 k
xz-libs x86_64 4.999.9-0.3.beta.20091007git.el5 base 95 k

Transaction Summary
================================================================================
Install 10 Package(s)
Upgrade 0 Package(s)

Total download size: 4.5 M
Is this ok [y/N]:
Step 7: Install Virtual Machines using virt-install
a. Do put an ISO of the Operating System into Server so that you can build the Virtual Machine From. If you only have the disk but not the ISO, you may want to look at “Making Disc Images using mkisofs” from Linux Toolkit
b. Do also take a look at the Guest Support Status from KVM to know what is supported for the version of KVM.
Step 8: Prepare the Virtual Machine
Prepare a Installation Script for easier management.
a. For CentOS Virtual Machine
# vim kvm_centos5.4.sh
virt-install \
--connect qemu:///system \
--name centos5.4_n01 \
--vcpus=2 \
--ram 1024 \
--disk path=/nfs_shared/vms/centos5.4_n01.img,size=40, \
--cdrom=/nfs_shared/ISO/CentOS-5.4-x86_64-bin-DVD.iso \
--network=bridge:br0 \
--accelerate \
--vnc \
--noautoconsole \
--os-type=linux \
--os-variant=rhel5.4 \
--hvm
b. For Windows XP Machine
# vim kvm_winXp.sh
virt-install \
--connect qemu:///system \
--name winxp_n01 \
--vcpus=1 \
--ram 1024 \
--disk path=/nfs_shared/vms/winxp_n01.img,size=25, \
--cdrom=/nfs_shared/ISO/Windows_XP2.iso \
--network=bridge:br0 \
--accelerate \
--vnc \
--noautoconsole \
--os-type=windows \
--os-variant=winxp \
--hvm
Some notes:
Do note that the disk path should be on a shared drive if you are planning to use “migration” from physical nodes to another physical node.
noautoconsole -> No connection to it is started by default although you can make a connection to it via virt-manager.
accelerate -> The VM will runin using kernel acceleration if available.
os-type and os-variant -> Please check the man virt-install for more information on the exact paramters.
Step 9: Run the script
You should see something like this
Starting install...
Creating storage file... | 40 GB 00:00
Creating domain... | 0 B 00:01
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
Step 10: Continue the installation through Virt-Manager Console.
If you are on the graphical console, just type
# virt-manager
you should be able to see the node name. double-clicked the node icon, you should be able to continue the rest of the installation

Disable the Ctrl-Alt-Delete shutdown keys in Linux

On a production system it is recommended that you disable the [Ctrl]-[Alt]-[Delete] shutdown. It is configured using /etc/inittab (used by sysv-compatible init process) file. The inittab file describes which processes are started at bootup and during normal operation. You need to open this file and remove (or comment it) ctrlaltdel entry.
Ctrlaltdel specifies the process that will be executed when init receives the SIGINT signal. SIGINT is the symbolic name for the signal thrown by computer programs when a user wishes to interrupt the process, for example reboot/shutdown system using [Ctrl]-[Alt]-[Del].). This means that someone on the system console has pressed the CTRL-ALT-DEL key combination. Typically one wants to execute some sort of shutdown either to get into single-user level or to reboot the machine.

Disable CTRL+ALT+Del keys
Open /etc/inittab file, enter:
# vi /etc/inittab
Search for line that read as follows:
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now
And remove the line or comment out the above line by putting a hash mark (#) in front of it:
# ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now
Save the file and exit to shell promot. Reboot system to take effect or type command:
# init q