測試使用環境:python
服務器:Dell R710 CPU型號物理CPU顆數內存大小和數量linux
[root@localhost ~]# cat /proc/cpuinfo | grep name | cut -d: -f2 | uniq -cwindows
16 Intel(R) Xeon(R) CPU L5520 @ 2.27GHzcentos
[root@localhost ~]# cat /proc/cpuinfo | grep physical | sort -n | uniq -capi
16 address sizes : 40 bits physical, 48 bits virtual服務器
8 physical id : 0網絡
8 physical id : 1app
##說明兩顆物理cpudom
[root@localhost ~]# dmidecode|grep -A5 "Memory Device"|grep Size | cut -d: -f2 | sort -n | uniq -cide
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系統主機。
1、檢查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
2、安裝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/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 能夠了解命令
3、咱們來建立實例(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 \
--vcpus=2 \
--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 \
--noautoconsole
4、咱們來建立實例(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
file format: qcow2
virtual size: 73G (78383153152 bytes)
disk size: 136K
cluster_size: 65536
##=========================================================
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 \
--network bridge=br0,model=virtio,model=e1000 \
--hvm --os-variant=rhel6 \
--graphics vnc,listen=0.0.0.0,port=5903 \
--virt-type=kvm \
--noautoconsole
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系統
[root@localhost ~]# virt-install --connect qemu:///system \
--name win3test \
--ram 4096 \
--vcpus=4 \
--disk path=/home/kvm/p_w_picpaths/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
##===================================
##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>
<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/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
[root@localhost ~]# virsh -c qemu:///system destroy 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磁盤
[root@localhost iso]# virsh detach-disk win3test vdb --persistent
##======================================
##======================================
##(guest)win3test開機狀態添加磁盤成功,可是磁盤沒法讀寫。由於磁盤文件爲qcow2
[root@localhost ~]#virsh attach-disk win3test /home/kvm/p_w_picpaths/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/p_w_picpaths/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/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
<!--
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/p_w_picpaths/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鏈接
##登錄進系統刪除網卡的MAC和70-persistent-net.rules而後重啓機器(這樣網卡不會衝突能啓動)
[root@localhost ~]# rm /etc/udev/rules.d/70-persistent-net.rules
##關閉(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