完整的xml文件php
<domain type='kvm'>
linux
<name>centoskvm3</name>
windows
<uuid>bdbb89fb-57d1-4d01-b3b7-ff33a9346ae6</uuid>
centos
<memory>2048000</memory>
api
<currentMemory>1024000</currentMemory>
網絡
<vcpu>2</vcpu>
架構
<os>
app
<type arch='x86_64' machine='pc'>hvm</type>
dom
<boot dev='hd'/>
ide
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='localtime'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk' cache='none'>
<source file='/usr/local/kvm/centvm3.img'/>
<target dev='vda' bus='virtio'/>
</disk>
<interface type='bridge'>
<mac address='3b:6e:01:69:3a:11'/>
<source bridge='br0'/>
<model type='virtio'/>
</interface>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' listen='221.196.131.121' keymap='en-us' passwd='123456'/>
</devices>
</domain>
一、kvm guest 定義開始
<domain type='kvm'>
二、guest的short name。由字母和數字組成,不能包含空格
<name>centosvm1</name>
三、uuid,由命令行工具 uuidgen生成。
<uuid>bdbb89fb-57d1-4d01-b3b7-ff33a9346ae6</uuid>
四、在不reboot guest的狀況下,guset可使用的最大內存,以KB爲單位
<memory>2048000</memory>
五、guest啓動時內存,能夠經過virsh setmem來調整內存,但不能大於最大可以使用內存。
<currentMemory>1024000</currentMemory>
六、分配的虛擬cpu
<vcpu>2</vcpu>
七、有關OS
x86架構:i686
hvm:全虛擬化
kernel:指定guest使用的內核,若是使用ISO(安裝時)或guset系統中(系統已經安裝完成)的內核,不須要指定該項
initrd:指定guest使用的ram disk,若是使用ISO(安裝時)或guest系統中(系統已經安裝完成)的ram disk,不須要指定該項
注:kernel 和initrd文件位於RHEL系統光盤的images/pxeboot目錄,拷貝這兩個文件到本地磁盤,並指定路徑。
注:這兩個元素,若是是爲了安裝guset而指定,在安裝完成之後便可以刪除。
如 果host開啓了SELINUX,you also need to change the type of security context for the files to virt_image_t to allow libvirtd to access them for booting:
# chcon -t virt_image_t /tmp/vmlinuz-rhel54
# chcon -t virt_image_t /tmp/initrd-rhel54.img
After you change the security context, verify that the correct security context is assigned to them as shown in the following example:
# ls -Z /tmp|grep virt
-r--r--r-- root root root:object_r:virt_image_t initrd-rhel54.img
-r--r--r-- root root root:object_r:virt_image_t vmlinuz -rhel54
boot:指定啓動設備,能夠重複多行,指定不一樣的值,做爲一個啓動設備列表。
能夠在cmdline元素中添加啓動參數,例如,使用kickstart文件:
<cmdline>method=http://10.1.1.212/install/rhel5.4/x86_64
ks=http://10.1.1.212/install/autoinst/c20m2n05v3</cmdline>
<os>
<type arch='x86_64' machine='pc'>hvm</type>
<kernel>/tmp/vmlinuz-rhel54</kernel>
<initrd>/tmp/initrd-rhel54.img</initrd>
<boot dev='hd'/>
</os>
八、處理器特性
<features>
<acpi/>
<apic/>
<pae/>
</features>
關於處理器特性,查看:
http://blog.chinaunix.net/space.php?uid=1838361&do=blog&id=1753201
九、時鐘。使用本地時間:localtime
<clock offset='utc'/>
仍然須要再計劃任務中添加時間同步:
*/15 * * * * (/usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1;/sbin/hwclock -w)
十、定義了在kvm環境中power off,reboot,或crash時的默認的動做爲destroy。其餘容許的動做包括:restart,preserve,rename-restart.
在 這個例子中,destory之因此也用在了on_reboot元素中,是由於一旦安裝完成,指望的guest動做是stop,以便從the installed guest operating system而不是從the installation kernel or ISO編輯guest的定義。
destroy:中止該虛擬機。至關於關閉電源
<on_poweroff>destroy</on_poweroff>
<on_reboot>destroy</on_reboot>
<on_crash>destroy</on_crash>
十一、設備定義開始
<devices>
十二、模擬元素,此處寫法用於kvm的guest(xen怎麼寫?)
<emulator>/usr/libexec/qemu-kvm</emulator>
1三、用於kvm存儲的文件。在這個例子中,在guest中顯示爲IDE設備。
使用qemu-img命令建立該文件,kvm image的默認目錄爲:/var/lib/libvirt/images/
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source file='/var/lib/libvirt/images/kvm3.img'/>
<target dev='hda' bus='ide'/>
</disk>
這裏須要注意:使用libvirt 0.6.3寫的xml,沒有<driver name='qemu' type='qcow2'/>參數,當將該xml放到libvirt 0.8版本時,該參數會默認爲raw,因此會形成在0.6中能夠啓動的guest沒法啓動,更改參數便可
使用virtio:
採用普通的驅動,即硬盤和網卡都採用默認配置狀況下,硬盤是 ide 模式,而網卡工做在 模擬的rtl 8139 網卡下,速度爲100M 全雙工。採用 virtio 驅動後,網卡工做在 1000M 的模式下,硬盤工做是SCSI模式下。
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source file='/usr/local/kvm/vmsample/disk.os'/>
<target dev='vda' bus='virtio'/>
</disk>
硬盤採用 virtio 後,安裝windows 系統,將不能正常的識別硬盤,解決的方法是:
從kvm 的官網下載virtio的驅動iso。
1. 先採用ide模式安裝系統。
2. 安裝完成後,添加一個virtio模式的硬盤。
3. 啓動vm後,系統會自動搜索 SCSI的驅動,找到下載的virtio 驅動後,安裝便可。
4. 修改vm 配置文件,刪除掉添加的 vitro 硬盤後,修改ide硬盤爲 virtio模式便可。
A Note About Virtio Drivers
Virtio is paravirtualized drivers for kvm/Linux. With this you can can run multiple virtual machines running unmodified Linux or Windows VMs. Each virtual machine has private virtualized hardware a network card, disk, graphics adapter, etc. According to Redhat:
Para-virtualized drivers enhance the performance of fully virtualized guests. With the para-virtualized drivers guest I/O latency decreases and throughput increases to near bare-metal levels. It is recommended to use the para-virtualized drivers for fully virtualized guests running I/O heavy tasks and applications.
關於cache=none
若是不加該參數,默認cache policy爲write through。
kvm官方及IBM都推薦使用raw,設置cache爲none,以關閉kvm磁盤的cache策略,讓數據能夠直接落在實體硬盤上(實體機上其實也有磁盤cache的,並不須要kvm爲cache操心)
For the best storage performance on guest operating systems that use raw disk volumes or partitions,completely avoid the page cache on the host.
下面的帖子中有一段關於cache的內容:
http://www.linuxeden.com/forum/viewthread.php?tid=205581&extra=&page=3
補充:能夠定義多個磁盤,其餘可行的磁盤定義:
Logical volume device:
<disk type='block' device='disk'>
<source 'dev=/dev/mapper/VolGroup01-LVM1'/>
<target dev='hdb'/>
</disk>
Disk partition:
<disk type='block' device='disk'>
<source dev='/dev/sda4'/>
<target dev='hdb' bus='ide'/>
</disk>
CD-ROM device:
<disk type='block' device='cdrom'>
<source 'dev=/dev/sde'>
<target dev='hdb'/>
<readonly/>
</disk>
1四、使用網橋類型。確保每一個kvm guest的mac地址惟一。將建立tun設備,名稱爲vnetx(x爲0,1,2...)
<interface type='bridge'>
<source bridge='br0'/>
<mac address="3B:6E:01:69:3A:11"/>
</interface>
補充:使用默認的虛擬網絡代替網橋,即guest爲NAT模式。也能夠省略mac地址元素,這樣將自動生成mac地址。
<interface type='network'>
<source network='default'/>
<mac address="3B:6E:01:69:3A:11"/>
</interface>
默認分配192.168.122.x/24的地址,也能夠手動指定。網關爲192.168.122.1
使用virtio:
採用普通的驅動,即硬盤和網卡都採用默認配置狀況下,硬盤是 ide 模式,而網卡工做在 模擬的rtl 8139 網卡下,速度爲100M 全雙工。採用 virtio 驅動後,網卡工做在 1000M 的模式下,硬盤工做是SCSI模式下。
rhel5.6 guest with virtio interface can not boot successfully if the system installation would use the ide interface
https://bugzilla.redhat.com/show_bug.cgi?id=647387
<interface type='bridge'>
<source bridge='br1'/>
<model type='virtio' />
</interface>
性能:經過物理機上拷貝
普通驅動:26.1M
virtio驅動:44.7M
由於交換機爲100M,沒法測試從其餘物理機上拷貝。
1五、輸入設備
<input type='mouse' bus='ps2'/>
1六、定義與guset交互的圖形設備。在這個例子中,使用vnc協議。listen的地址爲host的地址。prot爲-1,表示自動分配端口號,經過如下的命令查找端口號:
virsh vncdisplay <KVM Guest Name>
注意:是passwd 而不是password
VNC is configured to listen on 127.0.0.1 by default. To make it listen on all public interfaces, edit /etc/libvirt/qemu.conf file.
<graphics type='vnc' listen='221.191.134.123' passwd='123456' port='-1' autoport='yes' keymap='en-us'/>
1七、設備定義結束
</devices>
1八、KVM定義結束
</domain>