KVM: Installation and operation

From Define Wiki
Revision as of 15:19, 2 September 2014 by David (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Check CPU supports KVM

egrep '(vmx|svm)' --color=always /proc/cpuinfo

Install KVM

# on RHEL 5
yum install virt-viewer virt-manager
yum install kvm kvm-qemu-img
OR
yum groupinstall KVM

# on RHEL 6
yum groupinstall  Virtualisation 'Virtualization Client' 'Virtualization Platform' 'Virtualization Tools'

Install the modules

modprobe kvm-intel

Start libvirtd

/etc/init.d/libvirtd restart

Load up virtual manager

virt-manager

Setup Bridged Networking

By Default KVM uses NAT (hosts can see outside world but world cannot see the hosts). To setup bridged networking:

  • Disable NetworkManager
 # chkconfig NetworkManager off
 # chkconfig network on
 # service NetworkManager stop
 # service network start
  • Edit the file: /etc/sysconfig/network-scripts/ifcfg-eth0
 DEVICE="eth0"
 ONBOOT="yes"
 BRIDGE=br0


  • Create the bridge script/file: /etc/sysconfig/network-scripts/ifcfg-br0
 DEVICE=br0
 TYPE=Bridge
 IPADDR=172.28.10.58
 NETMASK=255.255.0.0
 NETWORK=172.28.0.0
 DNS1=172.28.1.3
 ONBOOT=yes
 DELAY=0
  • Configure iptables to allow traffic be forwarded across the bridge
 # iptables -I FORWARD -m physdev --physdev-is-bridged -j ACCEPT
 # service iptables save
 # service iptables restart
  • Reload libvirtd
 # service libvirtd reload
  • Verify it's working fine:
 # brctl show 
 bridge name	bridge id		STP enabled	interfaces
 br0		8000.0025902efb56	no		eth0
                                                       vnet2
 virbr0	8000.525400098976	yes		virbr0-nic
                                                       vnet0

Create the image file

In RHEL6, use virt-install: (dont use any files in /root)

[root@kvm kvms]$ virt-install \
                 --name=guest1-sl6-64 \
                 --disk path=/vm/guest1-rhel6-64,size=10,sparse=false \
                 --vnc \
                 --vcpus=2 \
                 --ram=2048 \
                 --cdrom=/vm/SL-61-x86_64-2011-07-27-Everything-DVD1.iso \
                 --network bridge=br0 \
                 --os-type=linux \
                 --os-variant=rhel6

Starting install...
Creating storage file guest1-rhel6-64    8% [==-                           ]  17 MB/s | 873 MB     09:13 ETA

Notes from RHEL5 on creating a qcow filesystem (virt-install runs this under the covers)

dd if=/dev/zero of=/home/viglen/vm_winxp.img bs=1024k count=12000
OR
qemu-img create -f qcow windows.img 12G

Another one for installing off the cobbler system (in an VM on a physical system which is on the cobbler network)

[root@x8-0 ~]# qemu-img create -f qcow2 -o preallocation=metadata /vm/images/centos6vm.qcow2 20G 
Formatting '/vm/images/centos6vm.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 preallocation='metadata' 
[root@x8-0 ~]# virt-install --connect qemu:///system --name centos6vm --ram 1024 --vcpus 1 --disk \
path=/vm/images/centos6vm.qcow2,format=qcow2,bus=virtio,cache=none --network=bridge:br0,model=virtio \ 
--vnc --os-type=linux --os-variant=rhel6 --accelerate --noautoconsole --keymap=en-gb --pxe

Creating files in /root resulted in the following error:

Starting install...
ERROR    internal error process exited while connecting to monitor: char device redirected to /dev/pts/1
qemu: could not open disk image /root/scratch/SL-61-x86_64-2011-07-27-Everything-DVD1.iso: Permission denied
  • Change CDs using virt-manger -> Open Guest -> Show Virtual Hardware -> CDROM

Start qemu

/usr/libexec/qemu-kvm -hda vig_xp.img  -cdrom vig_xp.iso -m 512 -boot d

Clone a VM

This can only be done when the image is shutdown or suspended

virt-clone --connect=qemu:///system -o centos6vm -n openstack1 --auto-clone
Cloning centos6vm.qcow2                                                               24% [==================                                                         ] 202 MB/s | 4.8 GB     01:17 ETA