首頁 > 後端開發 > XML/RSS教程 > 在KVM虛擬機器中的配置xml的程式碼詳解

在KVM虛擬機器中的配置xml的程式碼詳解

Y2J
發布: 2017-04-24 09:17:34
原創
3431 人瀏覽過

在RHEL6中,用於從磁碟啟動的XML檔案

#  這裡以dcs01.xml為例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

 <domain type=&#39;kvm&#39;>

<name>dcs01</name>

<uuid>e5fff551-bbe1-e748-c8e4-8ecb3bffb902</uuid>

<memory>1048576</memory>

<currentMemory>1048576</currentMemory>

<vcpu>1</vcpu>

<os>

<type arch=&#39;x86_64&#39; machine=&#39;rhel6.0.0&#39;>hvm</type>

<boot dev=&#39;hd&#39;/>

</os>

<features>

<acpi/>

<apic/>

<pae/>

</features>

<clock offset=&#39;localtime&#39;/>

<on_poweroff>destroy</on_poweroff>

<on_reboot>restart</on_reboot>

<on_crash>restart</on_crash>

<devices>

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

<disk type=&#39;file&#39; device=&#39;disk&#39;>

<driver name=&#39;qemu&#39; type=&#39;raw&#39; cache=&#39;none&#39;/>

<source file=&#39;/home/kvm/images/dcs01.img&#39;/>

<target dev=&#39;hda&#39; bus=&#39;ide&#39;/>

<address type=&#39;drive&#39; controller=&#39;0&#39; bus=&#39;0&#39; unit=&#39;0&#39;/>

</disk>

<disk type=&#39;file&#39; device=&#39;cdrom&#39;>

<driver name=&#39;qemu&#39; type=&#39;raw&#39;/>

<target dev=&#39;hdc&#39; bus=&#39;ide&#39;/>

<readonly/>

<address type=&#39;drive&#39; controller=&#39;0&#39; bus=&#39;1&#39; unit=&#39;0&#39;/>

</disk>

<controller type=&#39;ide&#39; index=&#39;0&#39;>

<address type=&#39;pci&#39; domain=&#39;0x0000&#39; bus=&#39;0x00&#39; slot=&#39;0x01&#39; function=&#39;0x1&#39;/>

</controller>

<interface type=&#39;bridge&#39;>

<mac address=&#39;52:54:00:ad:75:98&#39;/>

<source bridge=&#39;br0&#39;/>

<address type=&#39;pci&#39; domain=&#39;0x0000&#39; bus=&#39;0x00&#39; slot=&#39;0x03&#39; function=&#39;0x0&#39;/>

</interface>

<input type=&#39;tablet&#39; bus=&#39;usb&#39;/>

<input type=&#39;mouse&#39; bus=&#39;ps2&#39;/>

<graphics type=&#39;vnc&#39; port=&#39;-1&#39; autoport=&#39;yes&#39;/>

<video>

<model type=&#39;vga&#39; vram=&#39;9216&#39; heads=&#39;1&#39;/>

<address type=&#39;pci&#39; domain=&#39;0x0000&#39; bus=&#39;0x00&#39; slot=&#39;0x02&#39; function=&#39;0x0&#39;/>

</video>

<memballoon model=&#39;virtio&#39;>

<address type=&#39;pci&#39; domain=&#39;0x0000&#39; bus=&#39;0x00&#39; slot=&#39;0x05&#39; function=&#39;0x0&#39;/>

</memballoon>

</devices>

</domain>

登入後複製

  註解如下:

  1、kvm guest 定義開始

1

<domain type=&#39;kvm&#39;>

登入後複製

  2、guest的short name。由字母和數字組成,不能包含空格

1

<name>dcs01</name>

登入後複製

  3、uuid,由命令列工具 uuidgen產生。

1

<uuid>e5fff551-bbe1-e748-c8e4-8ecb3bffb902</uuid>

登入後複製

  4、在不reboot guest的情況下,guset可以使用的最大內存,以KB為單位

1

<memory>1048576</memory>

登入後複製

  5、guest啟動時內存,可以通過virsh setmem來調整內存,但不能大於最大可使用內存。

1

<currentMemory>1048576</currentMemory>

登入後複製

  6、分配的虛擬cpu

1

<vcpu>1</vcpu>

登入後複製

  7、有關OS
架構:i686、x86_64
machine:宿主機的作業系統
boot:指定啟動設備,可以重複多行,指定不同的值,作為一個啟動設備清單。

1

2

3

4

<os>

<type arch=&#39;x86_64&#39; machine=&#39;rhel6.0.0&#39;>hvm</type>

<boot dev=&#39;hd&#39;/>

</os>

登入後複製

  8、處理器特性

1

2

3

4

5

<features>

<acpi/>

<apic/>

<pae/>

</features>

登入後複製

  9、時鐘。使用本地時間:localtime

1

<clock offset=&#39;localtime&#39;/>

登入後複製

  10、定義了在kvm環境中power off,reboot,或crash時的預設的動作分別為destroy和restart。其他允許的動作包括: preserve,rename-restart.。
destroy:停止該虛擬機器。相當於關閉電源。
restart重啟虛擬機器。

1

2

3

<on_poweroff>destroy</on_poweroff>

<on_reboot>restart</on_reboot>

<on_crash>restart</on_crash>

登入後複製

  11、裝置定義開始

1

<devices>

登入後複製

  12、模擬元素,此處寫法用於kvm的guest

1

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

登入後複製

  13、用於kvm存儲的檔案。在這個例子中,在guest中顯示為IDE設備。
使用qemu-img指令建立該文件,kvm image的預設目錄為:/var/lib/libvirt/images/

1

2

3

4

5

6

<disk type=&#39;file&#39; device=&#39;disk&#39;>

<driver name=&#39;qemu&#39; type=&#39;raw&#39; cache=&#39;none&#39;/>

<source file=&#39;/home/kvm/images/dcs01.img&#39;/>

<target dev=&#39;hda&#39; bus=&#39;ide&#39;/>

<address type=&#39;drive&#39; controller=&#39;0&#39; bus=&#39;0&#39; unit=&#39;0&#39;/>

</disk>

登入後複製

  補充:可以定義多個磁碟。
使用virtio:
採用普通的驅動,即硬碟和網卡都採用預設配置情況下,網卡工作在 模擬的rtl 8139 網卡下,速度為100M 全雙工。採用 virtio 驅動後,網路卡工作在 1000M 的模式下。

  採用普通的驅動,即硬碟和網路卡都採用預設配置情況下,硬碟是 ide 模式。採用 virtio 驅動程式後,硬碟工作是SCSI模式下。

1

2

3

4

5

<disk type=&#39;file&#39; device=&#39;disk&#39;>

<driver name=&#39;qemu&#39; type=&#39;raw&#39;/>

<source file=&#39;/usr/local/kvm/vmsample/disk.os&#39;/>

<target dev=&#39;vda&#39; bus=&#39;virtio&#39;/>

</disk>

登入後複製

  CD-ROM device:

1

2

3

4

5

6

<disk type=&#39;file&#39; device=&#39;cdrom&#39;>

<driver name=&#39;qemu&#39; type=&#39;raw&#39;/>

<target dev=&#39;hdc&#39; bus=&#39;ide&#39;/>

<readonly/>

<address type=&#39;drive&#39; controller=&#39;0&#39; bus=&#39;1&#39; unit=&#39;0&#39;/>

</disk>

登入後複製

  14、使用橋接型別。確保每個kvm guest的mac位址唯一。將建立tun設備,名稱為vnetx(x為0,1,2...)

1

2

3

4

5

<interface type=&#39;bridge&#39;>

<mac address=&#39;52:54:00:ad:75:98&#39;/>

<source bridge=&#39;br0&#39;/>

<address type=&#39;pci&#39; domain=&#39;0x0000&#39; bus=&#39;0x00&#39; slot=&#39;0x03&#39; function=&#39;0x0&#39;/>

</interface>

登入後複製

  補充:
使用預設的虛擬網路取代網橋,即guest為NAT模式。也可以省略mac位址元素,這樣將自動產生mac位址。

1

2

3

4

<interface type=&#39;network&#39;>

<source network=&#39;default&#39;/>

<mac address="3B:6E:01:69:3A:11"/>

</interface>

登入後複製

預設分配192.168.122.x/24的位址,也可以手動指定。網關為192.168.122.1

  使用virtio:
採用普通的驅動,即硬碟和網卡都採用預設配置情況下,網卡工作在模擬的rtl 8139 網卡下,速度為100M 全雙工。採用 virtio 驅動後,網路卡工作在 1000M 的模式下。

1

2

3

4

<interface type=&#39;bridge&#39;>

<source bridge=&#39;br1&#39;/>

<model type=&#39;virtio&#39; />

</interface>

登入後複製

  15、輸入裝置

1

2

<input type=&#39;tablet&#39; bus=&#39;usb&#39;/>

<input type=&#39;mouse&#39; bus=&#39;ps2&#39;/>

登入後複製

  16、定義與guset互動的圖形裝置。在這個例子中,使用vnc協定。 listen的位址為host的位址。 prot為-1,表示自動分配埠號,透過以下的指令找出埠號:
virsh vncdisplay

  這裡未設定

1

<graphics type=&#39;vnc&#39; port=&#39;-1&#39; autoport=&#39;yes&#39;/>

登入後複製

  17 、裝置定義結束

1

</devices>

登入後複製

  18、KVM定義結束

1

</domain>

登入後複製

  centos_x86_6.4
  b9dcdd92-9b9b-14d6-3938##  b9dcdd92-9b9b-14d6-3938-1982a1774> #  2097152
  1
    hvm
  destroy
  restart
  restart
  /bin/1 在guest中顯示為IDE設備。       

1

2

<disk type=&#39;file&#39; device=&#39;disk&#39;>

    <driver name=&#39;qemu&#39; type=&#39;qcow2&#39;/>

登入後複製

   #虛擬機器網路連線方式 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<source file=&#39;/home/template_make/centos_x86_6.4.img&#39;>

        <seclabel model=&#39;selinux&#39; relabel=&#39;no&#39;/>

      </source>

      <target dev=&#39;hda&#39; bus=&#39;ide&#39;/>

      <alias name=&#39;ide0-0-0&#39;/>

      <address type=&#39;drive&#39; controller=&#39;0&#39; bus=&#39;0&#39; target=&#39;0&#39; unit=&#39;0&#39;/>

    </disk>

    <disk type=&#39;file&#39; device=&#39;cdrom&#39;>

      <driver name=&#39;qemu&#39; type=&#39;raw&#39;/>

      <source file=&#39;/home/template_make/CentOS-6.4-x86_64-bin-DVD1.iso&#39;/>

      <target dev=&#39;hdc&#39; bus=&#39;ide&#39;/>

      <readonly/>

      <alias name=&#39;ide0-1-0&#39;/>

      <address type=&#39;drive&#39; controller=&#39;0&#39; bus=&#39;1&#39; target=&#39;0&#39; unit=&#39;0&#39;/>

    </disk>

    <controller type=&#39;usb&#39; index=&#39;0&#39;>

      <alias name=&#39;usb0&#39;/>

      <address type=&#39;pci&#39; domain=&#39;0x0000&#39; bus=&#39;0x00&#39; slot=&#39;0x01&#39; function=&#39;0x2&#39;/>

    </controller>

    <controller type=&#39;ide&#39; index=&#39;0&#39;>

      <alias name=&#39;ide0&#39;/>

      <address type=&#39;pci&#39; domain=&#39;0x0000&#39; bus=&#39;0x00&#39; slot=&#39;0x01&#39; function=&#39;0x1&#39;/>

    </controller>

    <interface type=&#39;bridge&#39;>

登入後複製

     ## 使用virtio: 採用普通的驅動,即硬碟卡都採用預設配置情況下,硬碟是ide 模式, 而網卡工作在模擬的rtl 8139 網卡下,速度為100M 全雙工。 採用 virtio 驅動程式後,網路卡工作在 1000M 的模式下,硬碟工作是SCSI模式下      

1

2

3

<mac address=&#39;52:54:00:78:f9:5a&#39;/>

     <source bridge=&#39;br0&#39;/>

     <target dev=&#39;vnet27&#39;/>

登入後複製

   #vnc方式登錄,連接埠號碼自動分配 可透過virsh vncdisplay查詢[vncdisplay domainId]     

1

2

3

4

5

<model type=&#39;virtio&#39;/>

      <alias name=&#39;net0&#39;/>

      <address type=&#39;pci&#39; domain=&#39;0x0000&#39; bus=&#39;0x00&#39; slot=&#39;0x03&#39; function=&#39;0x0&#39;/>

    </interface>

    <input type=&#39;mouse&#39; bus=&#39;ps2&#39;/>

登入後複製

以上是在KVM虛擬機器中的配置xml的程式碼詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板