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中開啓CPUVirtualizationTechnology

[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"

BOOTPROTO="none"

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

ONBOOT="yes"

TYPE="Bridge"

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

IPV6=no

USERCTL=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/images

安裝guest操作系統

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

-n win2003 \

-r 1024 \

--vcpus=2 \

--disk path=/home/kvm/images/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

##連接到虛擬機管理程序與libvirtURI,qemu:///system

-r MEMORY, --ram=MEMORY

##分配虛擬機實例(Guest)內存,單位爲M

-n NAME, --name=NAME

##虛擬機實例(Guest)的名稱

--vcpus=VCPUS

##配置實例(GuestCPU數目

--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)配置操作系統類型如linuxwindows

--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/images/win3test.qcow2 15G

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

 

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

image: /home/kvm/images/win3test.qcow2

file format: qcow2

virtual size: 15G (16106127360 bytes)

disk size: 264K

cluster_size: 65536

 

2)內存1G,2CPU ,15G硬盤安裝window 2003系統

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

--name win3test \

--ram 1024 \

--vcpus=2 \

--disk path=/home/kvm/images/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 \

--noautoconsole

 

 

Centos 6.4 KVM安裝和配置 - Robby - Robby工作室

 四、我們來創建實例(Guestlinuxtest

1)創建文件名linuxtest.qcow2的磁盤鏡像文件,文件格式爲qcow2,磁盤大小爲73G

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

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

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

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

image: /home/kvm/images/linuxtest.qcow2

file format: qcow2

virtual size: 73G (78383153152 bytes)

disk size: 136K

cluster_size: 65536

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

2)內存4G,4CPU ,73G硬盤安裝centos 6.4  64位系統

 

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

--name linuxtest \

--ram 4096 \

--vcpus=4 \

--disk path=/home/kvm/images/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 \

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

--hvm --os-variant=rhel6 \

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

--virt-type=kvm \

--noautoconsole

 

 

 

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

下面安裝Windows server 2003 Enterprise

wget http://alt.fedoraproject.org/pub/alt/virtio-win/latest/images/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,4CPU ,15G硬盤安裝windows server 2003系統

 

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

--name win3test \

--ram 4096 \

--vcpus=4 \

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

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

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

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

--hvm --os-variant=win2k3 \

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

--virt-type=kvm \

--noautoconsole

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

 

Centos 6.4 KVM安裝和配置 - Robby - Robby工作室

 

##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/images/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>

    <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'/>

    </disk>

    <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'/>

    </controller>

    <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/images/03template.qcow2

或者

[root@localhost ~]#virt-clone \

--original win3test \

--name 2003template \

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

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

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

 

 

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

##啓動(Guestwin3test

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

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

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

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

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

--cache none –persistent

##分離磁盤vdb

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

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

 

 

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

##關閉(guest)win3test

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

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

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

[root@localhost ~]# virsh attach-disk win3test /home/kvm/images/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/images/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/images/vm-add.qcow2,if=none,id=drive-virtio-disk1,format=qcow2: could not open disk image /home/kvm/images/vm-add.qcow2: Invalid argument

##分離vdb磁盤

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

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

 

 

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

##(guest)win3test開機狀態添加磁盤成功,但是磁盤無法讀寫。因爲磁盤文件爲qcow2

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

--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/images/Centostemplate.qcow2

 

##列表guest系統

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

 Id    名稱                         狀態

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

 -     linuxtest                      關閉

 -     win3test                       關閉

 

[root@localhost ~]#virt-clone \

> --original linuxtest \

> --name Centostemplate \

> --file /home/kvm/images/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

<!--

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 Centostemplate

or other application using the libvirt API.

-->

 

<domain type='kvm'>

  <name>Centostemplate</name>

  <uuid>8b5f02f2-ee18-376a-4388-3e8ffff64c06</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='utc'/>

  <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/images/Centostemplate.qcow2'/>

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

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

    </disk>

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

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

    </controller>

    <interface type='bridge'>

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

      <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='5904' autoport='no' listen='0.0.0.0'>

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

    </graphics>

    <video>

      <model type='cirrus' 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>

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

##啓動Centostemplate

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

 

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

 Id    名稱                         狀態

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

 22    Centostemplate                 running

 -     linuxtest                      關閉

 -     win3test                       關閉

 

##VNC連接

##登陸進系統刪除網卡的MAC70-persistent-net.rules然後重啓機器(這樣網卡不會衝突能啓動)

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

 

Centos 6.4 KVM安裝和配置 - Robby - Robby工作室

 

##關閉(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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章