Rumah  >  Artikel  >  pembangunan bahagian belakang  >  在KVM虚拟机中的配置xml的代码详解

在KVM虚拟机中的配置xml的代码详解

Y2J
Y2Jasal
2017-04-24 09:17:343198semak imbas

在RHEL6中,用于从磁盘启动的XML文件

  这里以dcs01.xml为例:

 <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 定义开始

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

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

<name>dcs01</name>

  3、uuid,由命令行工具 uuidgen生成。

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

  4、在不reboot guest的情况下,guset可以使用的最大内存,以KB为单位

<memory>1048576</memory>

  5、guest启动时内存,可以通过virsh setmem来调整内存,但不能大于最大可使用内存。

<currentMemory>1048576</currentMemory>

  6、分配的虚拟cpu

<vcpu>1</vcpu>

  7、有关OS
架构:i686、x86_64
machine:宿主机的操作系统
boot:指定启动设备,可以重复多行,指定不同的值,作为一个启动设备列表。

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

  8、处理器特性

<features>
<acpi/>
<apic/>
<pae/>
</features>

  9、时钟。使用本地时间:localtime

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

  10、定义了在kvm环境中power off,reboot,或crash时的默认的动作分别为destroy和restart。其他允许的动作包括: preserve,rename-restart.。
destroy:停止该虚拟机。相当于关闭电源。
restart重启虚拟机。

<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>

  11、设备定义开始

<devices>

  12、模拟元素,此处写法用于kvm的guest

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

  13、用于kvm存储的文件。在这个例子中,在guest中显示为IDE设备。
使用qemu-img命令创建该文件,kvm image的默认目录为:/var/lib/libvirt/images/

<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模式下。

<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:

<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...)

<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地址。

<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 的模式下。

<interface type=&#39;bridge&#39;>
<source bridge=&#39;br1&#39;/>
<model type=&#39;virtio&#39; />
</interface>

  15、输入设备

<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 33e1e9ce7395ccd8e6281087cf15dba5

  这里未设置

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

  17、设备定义结束

</devices>

  18、KVM定义结束

</domain>

  centos_x86_6.4
  b9dcdd92-9b9b-14d6-3938-1982a9746a12
  2097152
  2097152
  1
    hvm
  destroy
  restart
  restart
  /bin/qemu-kvm
 

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

     #目的镜像路径 在这个例子中,在guest中显示为IDE设备。       

<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;>

   #虚拟机网络连接方式     

 <mac address=&#39;52:54:00:78:f9:5a&#39;/>
      <source bridge=&#39;br0&#39;/>
      <target dev=&#39;vnet27&#39;/>

     ## 使用virtio: 采用普通的驱动,即硬盘和网卡都采用默认配置情况下,硬盘是 ide 模式, 而网卡工作在 模拟的rtl 8139 网卡下,速度为100M 全双工。 采用 virtio 驱动后,网卡工作在 1000M 的模式下,硬盘工作是SCSI模式下      

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

   #vnc方式登录,端口号自动分配 可以通过virsh vncdisplay来查询[vncdisplay domainId]     


      
    
    


Atas ialah kandungan terperinci 在KVM虚拟机中的配置xml的代码详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn