Instalando e configurando o KVM no CentOS 5
Autor: André Cadmo | Publicado: 2011-03-29T17:28:00-07:00

Configure o SELinux para Permissive (virt-install não irá funcionar se você configurar o SELinux para Disabled ).

&nbsp;&nbsp; # system-config-securitylevel

Em seguida, verifique se o seu processador suporta virtualização de hardware .

&nbsp;&nbsp; # egrep '(vmx|svm)' --color=always /proc/cpuinfo

Deve mostrar alguma coisa,como este&nbsp;exemplo :
[root@svkvm ~]# egrep '(vmx|svm)' --color=always /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 rdtscp lm 3dnowext 3dnow pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy misalignsse
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 rdtscp lm 3dnowext 3dnow pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy misalignsse

Se nada for exibido, então o seu processador não suporta virtualização de hardware, e você deve parar por aqui.

Agora vamos importar as chaves GPG para os pacotes de software:

&nbsp;&nbsp; # rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*

Para instalar o KVM e virtinst ( ferramenta para criar máquinas virtuais), execute:

&nbsp;&nbsp; # yum install kvm kmod-kvm qemu libvirt python-virtinst

Então reinicie o sistema:

&nbsp;&nbsp; # reboot

Após a reinicialização, o módulo do kernel KVM deve ser carregado:

&nbsp;&nbsp; # lsmod | grep kvm

[root@svkvm  ~]# lsmod | grep kvm
kvm &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 109264  1 kvm_amd

(Esta saída é a para um sistema com um processador AMD-V. Se seu sistema usa um processador Intel VT, ele deve exibir algo como kvm_intel ).

Para verificar se o KVM foi instalado com sucesso, execute

&nbsp;&nbsp; #virsh -c qemu:///system list

Ele deve exibir algo como isto:
[root@svkvm  ~]# virsh -c qemu:///system list
Id Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; State
----------------------------------

Se ele exibe um erro em vez disso, então alguma coisa deu errado.
Em seguida é preciso criar uma bridge no nosso servidor para que as nossas máquinas virtuais posam acessar a partir de outros hosts, como se estivessem  na rede.
Para fazer isso, vamos instalar o pacote bridge-utils ...

&nbsp;&nbsp; &nbsp;# yum install bridge-utils

Agora vamos configurar a bridge. Copie o arquivo o arquivo ifcfg-eth0 e faça as alterações necessárias.

&nbsp;&nbsp; # cp /etc/sysconfig/network-scripts/ifcfg-eth0  /etc/sysconfig/network-scripts/ifcfg-br0

&nbsp;&nbsp; # vi /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
BROADCAST=192.168.20.255
IPADDR=192.168.20.254
NETMASK=255.255.255.0
NETWORK=192.168.20.0
ONBOOT=yes

Modifique o arquivo  /etc/sysconfig/network-scripts/ifcfg-eth0 da seguinte forma (comentar BOOTPROTO, BROADCAST, IPADDR, NETMASK e NETWORK e adicione BRIDGE=br0 ):

&nbsp;&nbsp; # vi /etc/sysconfig/network-scripts/ifcfg-eth0

# Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+
DEVICE=eth0
#BOOTPROTO=static
#BROADCAST=192.168.20.255
HWADDR=01:1F:B7:95:AA:BB
#IPADDR=192.168.20.254
#NETMASK=255.255.255.0
#NETWORK=192.168.20.0
ONBOOT=yes
BRIDGE=br0

Reinicie a rede ...
&nbsp;&nbsp; &nbsp;# /etc/init.d/network restart

Ele deve agora mostrar a bridge ( br0 ):
&nbsp;&nbsp; &nbsp;# ifconfig

root@svkvm  ~]# ifconfig
br0 &nbsp; &nbsp; &nbsp; &nbsp; Link encap:Ethernet  HWaddr 01:1F:B7:95:AA:BB
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inet addr:192.168.20.254  Bcast:192.168.20.255  Mask:255.255.255.0
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inet6 addr: fe80::210:a7ff:fe05:afeb/64 Scope:Link
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RX packets:17 errors:0 dropped:0 overruns:0 frame:0
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TX packets:53 errors:0 dropped:0 overruns:0 carrier:0
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; collisions:0 txqueuelen:0
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RX bytes:1160 (1.1 KiB)  TX bytes:14875 (14.5 KiB)

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

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

virbr0 &nbsp; &nbsp; &nbsp;Link encap:Ethernet  HWaddr 00:00:00:00:00:00
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RX packets:0 errors:0 dropped:0 overruns:0 frame:0
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TX packets:35 errors:0 dropped:0 overruns:0 carrier:0
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; collisions:0 txqueuelen:0
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RX bytes:0 (0.0 b)  TX bytes:9987 (9.7 KiB)


Agora vamos criar as LVM:

Criando volume físico
&nbsp;&nbsp; #  pvcreate /dev/sdx   
Aonde “x” representa o numero da  partição LVM

Criando grupo 
&nbsp;&nbsp; #  vgcreate 'nome do grupo' /dev/sdx

Ativando o grupo
&nbsp;&nbsp; #  vgchange -ay

Criando volume lógico
&nbsp;&nbsp; #  lvcreate -L 'tamanho da partição GB' 'nome do grupo' -n 'nome da partição'

Aprenda a usar o virt-install
&nbsp;&nbsp; # man virt-install

Criação de um Guest do  Debian Lenny

Para criar um Guest  do Debian Lenny (em modo bridging ) com o nome sv-firewall , 512MB de RAM, dois processadores virtuais, e a imagem de disco , insira o CD do Debian Lenny  no CDROM e execute.

&nbsp;&nbsp; # virt-install --connect qemu:///system -n sv-firewall -r 512 --vcpus=2 -f ~/dev/”nome da grupo”/”nome do volume lógico”  -c /dev/cdrom --vnc --noautoconsole --os-type linux --os-variant debianlenny --accelerate --network=bridge:br0 –hvm

(O man virt-install mostra os valores que são válidos para -os-type e  -os-variant).

Você também pode criar uma imagem ISO do Debian Lenny ou qualquer outro sistema operacional pelo CD.

&nbsp;&nbsp; # dd if=/dev/cdrom of=~/debian_lenny.iso

Usando uma imagem ISO no comando virt-install:

&nbsp;&nbsp; # virt-install --connect qemu:///system -n sv-firewall -r 512 --vcpus=2 -f ~/ dev/”nome da grupo”/”nome do volume lógico” -c ~/”local aonde esta a ISO”/debian_lenny.iso --vnc --noautoconsole --os-type linux --os-variant debianlenny --accelerate --network=bridge:br0 –hvm