天天看點

Centos 6.4 KVM安裝和配置

測試使用環境:

伺服器:Dell R710  CPU型号實體CPU顆數記憶體大小和數量

[root@localhost ~]# cat /proc/cpuinfo | grep name | cut -d: -f2 | uniq -c

16  Intel(R) Xeon(R) CPU           L5520  @ 2.27GHz

[root@localhost ~]# cat /proc/cpuinfo | grep physical | sort -n | uniq -c

     16 address sizes   : 40 bits physical, 48 bits virtual

      8 physical id     : 0

      8 physical id     : 1

##說明兩顆實體cpu

[root@localhost ~]# dmidecode|grep -A5 "Memory Device"|grep Size | cut -d: -f2 | sort -n | uniq -c

  10  No Module Installed

      8  4096 MB

##接了8根記憶體,每根記憶體大小4G

##還有相關指令

##dmidecode  | grep -A16 "Memory Device" | grep Speed | sort -n | uniq -c

[root@localhost ~]# dmidecode|grep  'Maximum Capacity'

        Maximum Capacity: 288 GB

作業系統Centos 6.4 64位系統

[root@localhost ~]# cat /etc/issue

CentOS release 6.4 (Final)

Kernel \r on an \m

本文介紹如何安裝KVM,在主控端上如何安裝作業系統,添加分離磁盤,軟驅,光驅和更改ISO鏡像檔案,如何克隆Guest系統主機。

一、檢查CPU是否支援虛拟化。(在主機闆BIOS中開啟CPU的VirtualizationTechnology)

[root@localhost ~]# uname -a

Linux localhost.localdomain 2.6.32-358.2.1.el6.x86_64 #1 SMP Wed Mar 13 00:26:49 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

[root@localhost ~]# grep -E -o 'vmx|svm' /proc/cpuinfo

vmx

二、安裝kvm

[root@localhost ~]#yum -y install qemu-kvm libvirt python-virtinst bridge-utils

或者

##yum -y groupinstall 'Virtualization' 'Virtualization Client' 'Virtualization Platform' 'Virtualization Tools'

##檢視子產品

[root@localhost ~]#  lsmod | grep kvm

kvm_intel              53484  6

kvm                   316506  1 kvm_intel

a.網卡橋接配置

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-em1

DEVICE="em1"

BOOTPROTO="none"

HWADDR="00:24:E8:60:49:66"

ONBOOT="yes"

TYPE="Ethernet"

IPADDR=192.168.18.138

NETMASK=255.255.255.192

GATEWAY=192.168.18.129

DNS1=202.102.134.68

DNS2=202.102.128.68

IPV6INIT=no

USERCTL=no

BRIDGE=br0

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE="br0"

TYPE="Bridge"

IPV6=no

##重新開機網卡

/etc/init.d/network restart

[root@localhost ~]# ip route list

192.168.18.128/26 dev br0  proto kernel  scope link  src 192.168.18.138

192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1

169.254.0.0/16 dev br0  scope link  metric 1006

default via 192.168.18.129 dev br0

[root@localhost ~]# brctl show

bridge name     bridge id               STP enabled     interfaces

br0             8000.0024e8604966       no              em1

virbr0          8000.52540098e2ed       yes             virbr0-nic

b.建立虛拟機(建立主控端的Guest系統)Guest系統可以是linux 或者windows等系統

我們使用管理工具virt-install

##建立存儲鏡像目錄

[root@localhost ~]# mkdir -p /home/kvm/p_w_picpaths

安裝guest作業系統

[root@localhost ~]# virt-install --connect qemu:///system \

-n win2003 \

-r 1024 \

--vcpus=2 \

--disk path=/home/kvm/p_w_picpaths/win2003.img,cache=writeback,size=10 \

-c /home/iso/WIN_2003.iso \

--vnc --vnclisten=0.0.0.0 --vncport=5901 --noautoconsole \

--os-type windows \

--accelerate --network bridge=br0 \

--hvm --os-variant=win2k3

##下面對virt-install指令做簡單介紹,詳細可以檢視virt-install  --help

--connect=URI

##連接配接到虛拟機管理程式與libvirt的URI,如qemu:///system

-r MEMORY, --ram=MEMORY

##配置設定虛拟機執行個體(Guest)記憶體,機關為M

-n NAME, --name=NAME

##虛拟機執行個體(Guest)的名稱

--vcpus=VCPUS

##配置執行個體(Guest)CPU數目

--disk path=DISKFILE

##指定執行個體(Guest)存儲路徑及其鏡像

-c CDROM, --cdrom=CDROM

##指定安裝鏡像所在位置和名稱, 它可以是一個ISO映像的路徑, 它也可以是一##個URL,從中提取/通路的最小引導ISO映像。

--vnc

##啟用VNC遠端管理,該選項不被贊同,可以用"--graphics vnc,..."代替

--vnclisten=VNCLISTEN

##可以用"--graphics vnc,listen=LISTEN,..." 代替

--vncport=VNCPORT

##可以用"--graphics vnc,port=PORT,..."代替

--noautoconsole

##不要自動嘗試連接配接到guest虛拟機控制台。預設行為是啟動一個VNC用戶端顯##示的圖形控制台

--os-type=OS_TYPE

##優化執行個體(Guest)配置作業系統類型如linux或windows

--os-variant=OS_VARIANT

##優化執行個體(Guest)配置一個特定的作業系統(如##win7,winxp,win2k3,win2k8,rhel6)

--hvm

##要求使用完全虛拟化

--accelerate   

##加速該選項已經過時可以參考--virt-type qemu

-w NETWORK, --network=NETWORK,opt1=val1,opt2=val2

## 執行個體(Guest)連接配接到主機的網絡,網橋--network bridge=br0,用nat路由出站##--network network=default

##==========================================

##清單(Guest) win2003

virsh -c qemu:///system list --all

##啟動Guest) win2003

virsh -c qemu:///system start win2003

##關閉(Guest) win2003

virsh -c qemu:///system destroy win2003

##編輯(Guest) win2003配置檔案xml

virsh -c qemu:///system edit win2003

##删除(Guest) win2003

virsh -c qemu:///system undefine win2003

##man virsh 可以了解指令

三、我們來建立執行個體(Guest)win3test

1)首先建立磁盤鏡像檔案,

檔案格式qcow2格式是kvm支援的标準格式,raw格式為虛拟磁盤檔案通用格式。

建立檔案名win3test.qcow2的磁盤鏡像檔案名,檔案格式為qcow2,磁盤大小為10G

[root@localhost ~]# qemu-img create -f qcow2 /home/kvm/p_w_picpaths/win3test.qcow2 15G

Formatting '/home/kvm/p_w_picpaths/win3test.qcow2', fmt=qcow2 size=16106127360 encryption=off cluster_size=65536

[[root@localhost ~]# qemu-img info /home/kvm/p_w_picpaths/win3test.qcow2

p_w_picpath: /home/kvm/p_w_picpaths/win3test.qcow2

file format: qcow2

virtual size: 15G (16106127360 bytes)

disk size: 264K

cluster_size: 65536

2)記憶體1G,2核CPU ,15G硬碟安裝window 2003系統

virt-install --connect qemu:///system \

--name win3test \

--ram 1024 \

--disk path=/home/kvm/p_w_picpaths/win3test.qcow2,device=disk,format=qcow2,bus=ide,cache=writeback,size=15 \

--cdrom /home/iso/WIN_2003.iso --os-type=windows \

--network bridge=br0,model=virtio,model=e1000 \

--hvm --os-variant=win2k3 \

--graphics vnc,listen=0.0.0.0,port=5902 \

--virt-type=kvm \

Centos 6.4 KVM安裝和配置

 四、我們來建立執行個體(Guest)linuxtest。

1)建立檔案名linuxtest.qcow2的磁盤鏡像檔案,檔案格式為qcow2,磁盤大小為73G

[root@localhost ~]# qemu-img create -f qcow2 /home/kvm/p_w_picpaths/linuxtest.qcow2 73G

Formatting '/home/kvm/p_w_picpaths/linuxtest.qcow2', fmt=qcow2 size=78383153152 encryption=off cluster_size=65536

##=======================================================

[root@localhost ~]# qemu-img info /home/kvm/p_w_picpaths/linuxtest.qcow2

p_w_picpath: /home/kvm/p_w_picpaths/linuxtest.qcow2

virtual size: 73G (78383153152 bytes)

disk size: 136K

##=========================================================

2)記憶體4G,4核CPU ,73G硬碟安裝centos 6.4  64位系統

[root@localhost ~]#virt-install --connect qemu:///system \

--name linuxtest \

--ram 4096 \

--vcpus=4 \

--disk path=/home/kvm/p_w_picpaths/linuxtest.qcow2,device=disk,format=qcow2,bus=virtio,cache=none,size=73 \

--location='http://mirrors.163.com/centos/6.4/os/x86_64/' \

--os-type=linux \

--hvm --os-variant=rhel6 \

--graphics vnc,listen=0.0.0.0,port=5903 \

4)virtio半虛拟化驅動來提高性能(微軟作業系統的IO性能)

下面安裝Windows server 2003 Enterprise

wget http://alt.fedoraproject.org/pub/alt/virtio-win/latest/p_w_picpaths/bin/virtio-win-0.1-52.iso

wget http://www.linuxwind.org/download/virtio-win-0.1-52.tar.gz

##解壓

tar zxvf virtio-win-0.1-52.tar.gz

安裝系統的時候記得按F6從軟驅加載驅動

記憶體4G,4核CPU ,15G硬碟安裝windows server 2003系統

--disk path=/home/kvm/p_w_picpaths/win3test.qcow2,device=disk,format=qcow2,bus=virtio,cache=none,size=15 \

--disk path=/home/iso/virtio-WIN2003-x86.vfd,device=floppy,perms=rw \

##===================================

Centos 6.4 KVM安裝和配置

##win3test.xml如下

[root@localhost ~]# cat /etc/libvirt/qemu/win3test.xml

<!--

WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE

OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:

  virsh edit win3test

or other application using the libvirt API.

-->

<domain type='kvm'>

  <name>win3test</name>

  <uuid>9fd1785e-430a-2aa7-050a-ed76247c7e64</uuid>

  <memory unit='KiB'>4194304</memory>

  <currentMemory unit='KiB'>4194304</currentMemory>

  <vcpu placement='static'>4</vcpu>

  <os>

    <type arch='x86_64' machine='rhel6.4.0'>hvm</type>

    <boot dev='hd'/>

  </os>

  <features>

    <acpi/>

    <apic/>

    <pae/>

  </features>

  <clock offset='localtime'>

    <timer name='rtc' tickpolicy='catchup'/>

  </clock>

  <on_poweroff>destroy</on_poweroff>

  <on_reboot>restart</on_reboot>

  <on_crash>restart</on_crash>

  <devices>

    <emulator>/usr/libexec/qemu-kvm</emulator>

    <disk type='file' device='disk'>

      <driver name='qemu' type='qcow2' cache='none'/>

      <source file='/home/kvm/p_w_picpaths/win3test.qcow2'/>

      <target dev='vda' bus='virtio'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>

    </disk>

    <disk type='file' device='floppy'>

      <driver name='qemu' type='raw' cache='none'/>

      <source file='/home/iso/virtio-WIN2003-x86.vfd'/>

      <target dev='fda' bus='fdc'/>

      <address type='drive' controller='0' bus='0' target='0' unit='0'/>

    <disk type='file' device='cdrom'>

      <driver name='qemu' type='raw'/>

      <source file='/home/iso/WIN_2003.iso'/>

      <target dev='hdc' bus='ide'/>

      <readonly/>

      <address type='drive' controller='0' bus='1' target='0' unit='0'/>

    <controller type='usb' index='0'>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>

    </controller>

    <controller type='fdc' index='0'/>

    <controller type='ide' index='0'>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>

    <interface type='bridge'>

      <mac address='52:54:00:cc:a1:bd'/>

      <source bridge='br0'/>

      <model type='e1000'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>

    </interface>

    <serial type='pty'>

      <target port='0'/>

    </serial>

    <console type='pty'>

      <target type='serial' port='0'/>

    </console>

    <input type='tablet' bus='usb'/>

    <input type='mouse' bus='ps2'/>

    <graphics type='vnc' port='5902' autoport='no' listen='0.0.0.0'>

      <listen type='address' address='0.0.0.0'/>

    </graphics>

    <video>

      <model type='vga' vram='9216' heads='1'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>

    </video>

    <memballoon model='virtio'>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>

    </memballoon>

  </devices>

</domain>

##=============================

##紅色部分為軟驅,稍後要解除安裝(做模版的時候)

##先關機分離軟驅

[root@localhost ~]# virsh -c qemu:///system destroy win3test

[root@localhost ~]# virsh detach-disk win3test fda --persistent

成功分離磁盤

4)克隆備份虛拟機

a)克隆windows server 2003模闆

[root@localhost ~]#virt-clone --original win3test --name 03template --file /home/kvm/p_w_picpaths/03template.qcow2

[root@localhost ~]#virt-clone \

--original win3test \

--name 2003template \

--file /home/kvm/p_w_picpaths/2003template.qcow2

##編輯配置03template.xml修改vnc端口修改為5905

[root@localhost ~]# vi /etc/libvirt/qemu/03template.xml

##1)添加分離磁盤===========================

##啟動(Guest)win3test

[root@localhost ~]# virsh -c qemu:///system start win3test

##建立磁盤格式raw,添加磁盤 vdb

[root@localhost ~]#qemu-img create -f raw /home/kvm/p_w_picpaths/vm-add.img 1G

##成功熱加載vdb硬碟,硬碟在系統上能正常使用

[root@localhost ~]#virsh attach-disk win3test /home/kvm/p_w_picpaths/vm-add.img vdb \

--cache none –persistent

##分離磁盤vdb

[root@localhost iso]# virsh detach-disk win3test vdb --persistent

##======================================

##關閉(guest)win3test

##添加IDE硬碟,檔案格式qcow2(系統開啟來後無法使用新增磁盤,應該是BUG)

[root@localhost ~]# qemu-img create -f qcow2 /home/kvm/p_w_picpaths/vm-add.qcow2 1G

[root@localhost ~]# virsh attach-disk win3test /home/kvm/p_w_picpaths/vm-add.qcow2 hde \

--cache none --persistent --subdriver qcow2

##分離IDE磁盤關,閉系統後操作

[root@localhost iso]# virsh detach-disk win3test hde --persistent

##(guest)win3test關機狀态的時候能成功添加,但是啟動報錯,應該是BUG

[root@localhost ~]#virsh attach-disk win3test /home/kvm/p_w_picpaths/vm-add.qcow2 vdb \

--address pci:0000.00.06.0 --persistent \

--driver qemu --subdriver qcow2 --sourcetype file

virsh # start win3test

錯誤:開始域 win3test 失敗

錯誤:internal error Process exited while reading console log output: char device redirected to /dev/pts/0

qemu-kvm: -drive file=/home/kvm/p_w_picpaths/vm-add.qcow2,if=none,id=drive-virtio-disk1,format=qcow2: could not open disk p_w_picpath /home/kvm/p_w_picpaths/vm-add.qcow2: Invalid argument

##分離vdb磁盤

##(guest)win3test開機狀态添加磁盤成功,但是磁盤無法讀寫。因為磁盤檔案為qcow2

--persistent

##分離磁盤

[root@localhost ~]# virsh detach-disk win3test vdb --persistent

##2)添加分離軟驅=============================

##添加fda軟碟(guest)win3test關機後操作

[root@localhost ~]#virsh attach-disk win3test /home/iso/virtio-WIN2003-x86.vfd fda \

--type floppy –persistent

##分離fda軟驅

[root@localhost ~]#virsh detach-disk win3test fda --persistent

##3)添加分離光驅=============================

##添加分離光驅(guest)win3test關機後操作

[root@localhost~]# virsh detach-disk win3test hdc --persistent

##加載光驅

[root@localhost ~]#virsh attach-disk win3test /home/iso/WIN_2003.iso hdc \

--type cdrom –persistent

##換iso鏡像檔案

[root@localhost ~]#virsh change-media win3test hdc /home/iso/virtio-win-0.1-52.iso

succeeded to complete action update on media

##彈出鏡像檔案

[root@localhost ~]#virsh change-media win3test hdc --eject

b)克隆Centos 6.2系統模闆

virt-clone \

--original linuxtest \

--name Centostemplate \

--file /home/kvm/p_w_picpaths/Centostemplate.qcow2

##清單guest系統

[root@localhost ~]#virsh -c qemu:///system list --all

 Id    名稱                         狀态

----------------------------------------------------

 -     linuxtest                      關閉

 -     win3test                       關閉

> --original linuxtest \

> --name Centostemplate \

> --file /home/kvm/p_w_picpaths/Centostemplate.qcow2

正在克隆 linuxtest.qcow2                             | 2.0 GB     00:05    

Clone 'Centostemplate' created successfully.

##編輯xml修改vnc端口修改為5904

[root@localhost ~]#virsh -c qemu:///system edit Centostemplate

[root@localhost ~]#cat /etc/libvirt/qemu/Centostemplate.xml

  virsh edit Centostemplate

  <name>Centostemplate</name>

  <uuid>8b5f02f2-ee18-376a-4388-3e8ffff64c06</uuid>

  <clock offset='utc'/>

      <source file='/home/kvm/p_w_picpaths/Centostemplate.qcow2'/>

      <mac address='52:54:00:47:13:79'/>

    <graphics type='vnc' port='5904' autoport='no' listen='0.0.0.0'>

      <model type='cirrus' vram='9216' heads='1'/>

##啟動Centostemplate

[root@localhost ~]# virsh -c qemu:///system start Centostemplate

[root@localhost ~]# virsh -c qemu:///system list --all

 22    Centostemplate                 running

##用VNC連接配接

##登陸進系統删除網卡的MAC和70-persistent-net.rules然後重新開機機器(這樣網卡不會沖突能啟動)

[root@localhost ~]# rm /etc/udev/rules.d/70-persistent-net.rules

Centos 6.4 KVM安裝和配置

##關閉(Guest) Centostemplate

[root@localhost ~]#virsh -c qemu:///system destroy Centostemplate

##删除(Guest) Centostemplate

[root@localhost ~]#virsh -c qemu:///system undefined Centostemplate

##關于網橋,檢視mac位址

[root@localhost ~]#brctl showmacs br0

##網橋設定mac生存期

[root@localhost ~]#brctl setageing br0 30

##xml虛拟機配置檔案目錄

/etc/libvirt/qemu

歡迎交流

Robby

QQ:6822089

http://yerunian.blog.163.com/

http://hi.baidu.com/gerryx