Setting up KVM Virtual Machine using CLI

KVM is Virtualization Solution for Linux. Using KVM, one can run multiple virtual machines running unmodified Linux or Windows images. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc.

KVM uses the Linux kernel as a bare metal hypervisor. A host running KVM is actually running a Linux kernel and the KVM kernel module, which was merged into Linux 2.6.20 and has since been maintained as part of the kernel.

KVM basic Architecture:

 

 

 

Installing and Configuring KVM:

grep ‘(vmx|svm)’ /proc/cpuinfo
–> This command will check whether hardware is KVM supported or not.
You must be able to see something like:
root@illusion:~# grep ‘(vmx|svm)’ /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni pclmulqdq monitor ssse3 cx16 sse4_1 sse4_2 popcnt aes xsave avx lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 nodeid_msr topoext perfctr_core arat cpb npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold

yum install qemu-kvm python-virtinst virt-manager virt-top virt-viewer
libvirt libvirt-client
–> This command will install KVM required components on CentOS machine.

lsmod | grep kvm
 –> It will check whether KVM Kernel Module is loaded in RAM or Not.
If above command do not give any output that means module is not loaded.
Then please load the module using below command:

modprobe kvm

Please keep one seperate partition for Storing Virtual Machines if possible.
I have created /vm partition and will be storing all VMs on it.
So need to create proper folder structure under /vm

mkdir -p /vm/qemu/images
chown -R qemu.qemu /vm/qemu
chcon –reference /var/lib/libvirt/images /vm/qemu/images
rmdir /var/lib/libvirt/images
ln -s /vm/qemu/images /var/lib/libvirt/images
ls -l /var/lib/libvirt/ –> To see if symlink is created or not.
service NetworkManager stop

You will need bridge interface for Network of Virtual Machine. Decide the interface on which you want to create Bridge. move /etc/sysconfig/network-scripts/ifcfg-eth0 and other.. Create new file /etc/sysconfig/network-scripts/ifcfg-eth0
in that please enter below details:

     DEVICE=eth0
ONBOOT=yes
BRIDGE=br0

Create new file /etc/sysconfig/network-scripts/ifcfg-br0
Please enter below details in it:
     DEVICE=eth0
     Type=Bridge
     ONBOOT=yes
     BOOTPROTO=dhcp
     STP=yes
    DELAY=0

Please enter below commands to make bridge and network active:
service network restart
ip addr show eth0
ip addr show br0
brctl show

You should be able to see Bridge Device br0 with STP enabled.

If IPtables firewall on then please allow traffice on host machine.
iptables -I FORWARD -m physdev –physdev-is-bridged -j ACCEPT
service iptables save
service iptables restart
iptables -L

Please add below entries to /etc/sysctl.conf, if already not there.
net.bridge.bridge-nf-call-iptables=0
net.bridge.bridge-nf-call-ip6tables=0
net.bridge.bridge-nf-call-arptables=0

Please run below command to update the kernel parameters.
sysctl -p /etc/sysctl.conf

Restart libvirtd daemon using below CLI
service libvirtd restart

Create & Start VM by giving below command. In our example we are going to install Linux. And HDD Size is 12 GB :

virt-install –name=vm1
–disk path=/var/lib/libvirt/images/vm1.img,size=12 –ram=512
–os-type=linux –os-variant=rhel6 –network bridge:br0
–nographics –cdrom <Path of OS installer ISO>

Then it will start booting CentOS installer. When it will show “Welcome to CentOS..” Wait DON’T HIT ENTER. Enter Tab and after initrd.img type: “console=ttyS0,115200
Then hit enter. You may need to click “Reinitialize All” if you get any driver error.

Now go ahead and install linux on it. You need to configure Network, Hostname and other details after installation.

Neelesh Gurjar has written 122 articles

Leave a Reply