`

libvirt虚拟机配置文件virtual.xml详解

    博客分类:
  • KVM
 
阅读更多

一般元数据介绍:
            <domain type='kvm'>
            domain 是一个所有虚拟机都需要的根元素,它有两个属性,type定义使用哪个虚拟机管理程序,值可以是:xen、kvm、qemu、lxc、kqemu,第二个参数是id,它唯一的标示一个运行的虚拟机,不活跃的客户端没有id。

            <name>kvm_test3</name>
            name参数为虚拟机定义了一个简短的名字,必须唯一。

            <uuid>f7333079-650e-8bea-4c36-184480afa0ba</uuid>
            uuid为虚拟机定义了一个全球唯一的标示符,uuid的格式必须遵循RFC 4122指定的格式,当创建虚拟机没有指定uuid时会随机的生成一个uuid。

           <title>This is my first test kvm</title>
           title参数提供一个对虚拟机简短的说明,它不能包含换行符。

操作系统启动介绍:

            有多重不同的方法引导虚拟机:
            BIOS bootloader  #通过BIOS启动支持全虚拟化                 

           <os>
                <type arch='x86_64'>hvm< pe>
                type参数指定了虚拟机操作系统的类型,内容:hvm表明该OS被设计为直接运行在裸金属上面,需要全虚拟化,type同样有两个可选参数:arch指定虚拟机的CPU构架,machine指定机器的类型。XEN中pv是半虚拟化,hvm是全虚拟化,pv只能用于linux内核的系统,效率更高,hvm可以虚拟所有常见操作系统(可以使用windows),理论效率比pv略低,另外,hvm需要cpu虚拟化指令支持(CPU

必须开启Intel VT or AMD -V),pv无此要求。

                <boot dev='hd'/>
                dev属性的值可以是:fd(软盘)、hd(硬盘)、cdrom(只读光盘)、network,它经常被用来指定下一次启动。boot的元素可以被设置多个用来建立一个启动优先规则。

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>

CPU分配:
          <vcpu placement='static' cpuset="1-4,^3,6" current="1">2</vcpu>
           vcpu的内容是为虚拟机最多分配几个cpu,值处于1~maxcpu之间,可选参数:cpuset参数指定虚拟cpu可以映射到那些物理cpu上,物理cpu用逗号分开,单个数字的标示单个cpu,也可以用range符号标示多个cpu,数字前面的脱字符标示排除这个cpu,current参数指定虚拟机最少,placement参数指定一个domain的cpu的分配模式,值可以是static、auto。

内存分配:

         <memory unit='KiB'>524288</memory>
         memory 定义客户端启动时可以分配到的最大内存,内存单位由unit定义,单位可以是:K、KiB、M、MiB、G、GiB、T、TiB。默认是KiB。

         <currentMemory>1024000</currentMemory>
         currentMemory 定义实际分给给客户端的内存她小于memory的定义,如果没有定义,值和memory一致。

控制周期:
         <on_poweroff>destroy</on_poweroff>
         <on_reboot>restart</on_reboot>
         <on_crash>restart</on_crash>

         当一个客户端的OS触发lifecycle时,它将采取新动作覆盖默认操作,具体状态参数如下:
         on_poweroff:当客户端请求poweroff时执行特定的动作
         on_reboot:当客户端请求reboot时执行特定的动作
         on_crash:当客户端崩溃时执行的动作
         每种状态下可以允许指定如下四种行为:
         destory:domain将会被完全终止,domain的所有资源会被释放
         restart:domain会被终止,然后以相同的配置重新启动
         preserver:domain会被终止,它的资源会被保留用来分析
         rename-restart:domain会被终止,然后以一个新名字被重新启动

     Hypervisor的特性:
         <features>
              <pae/>
              <acpi/>
              <apic/>
              <hap/>
              <privnet/>
         </features>
         Hypervisors允许特定的CPU/机器特性打开或关闭,所有的特性都在fearures元素中,以下介绍一些在全虚拟化中常用的标记:
         pae:扩展物理地址模式,使32位的客户端支持大于4GB的内存
         acpi:用于电源管理
         hap:Enable use of Hardware Assisted Paging if available in the hardware.

      时间设置:

         APIC 是装置的扩充组合用来驱动 Interrupt 控制器。

         

           <clock offset="localtime" />
           客户端的时间初始化来自宿主机的时间,大多数操作系统期望硬件时钟保持UTC格式,UTC也是默认格式,然而Windows机器却期望它是'localtime'
          clock的offset属性支持四种格式的时间:UTC localtime timezone variable
          UTC:当引导时客户端时钟同步到UTC时钟
          localtime:当引导时客户端时钟同步到主机时钟所在的时区
          timezone:The guest clock will be synchronized to the requested timezone using the timezone attribute.

设备设置:

           <devices>
           所有的设备都是一个名为devices元素的子设备(All devices occur as children of the main devices element.),以下是一个简单的配置:
          <emulator>/usr/bin/kvm</emulator>
          emulator元素指定模拟设备二进制文件的全路径 

          <disk type='block' device='disk'>
                <driver name='qemu' cache='none'/>
                <source dev='/dev/cciss/c0d0p6'/>
                <target dev='vda' bus='virtio'/>
         </disk>
         <disk type='block' device='cdrom'>
                <target dev='hdc' bus='ide'/>
                <readonly/>
         </disk>
         所有的设备看起来就像一个disk、floppy、cdrom或者一个 paravirtualized driver,他们通过一个disk元素指定。
disk是一个描述disks的主要容器,type特性包括:file,block,dir,network。device描述disk如何受到客户端OS的,特性包括:floppy、disk、cdrom、lun,默认是disk。snapshot属性表明默认行为在磁盘做snapshot的时候,snapshot的参数有:internal ,在snapshot的时候可以存储改变的数据。external,在snapshot时分开活动的数据。no,disk不参加snapshot,只读磁盘默认是no。

          source元素:在disk的type是file时,file属性指定一个合格的全路径文件映像作为客户端的磁盘,在disk的type是block时,dev属性指定一个主机设备的路径作为disk,在disk的type是dir时,dir属性指定一个全路径的目录作为disk,在disk的type是network时,protocol属性指定协议用来访问镜像,镜像的值可以是:nbd,rbd,sheepdog。当protocal的属性值是rbd或者sheepdog时,必须用一个额外的name属性指定使用那个镜像,当type的值是network时,source可以有0个或者多个host字属性指定连接哪些主机。

          target元素:控制总线设备在某个磁盘被选为客户端的OS时,dev属性表明本地磁盘在客户端上的实际名称,因为实际设备的名称指定并不能保证映射到客户端OS上的设备。bus属性指定了哪种类型的磁盘被模拟,值主要有:ide、scsi、virtio、xen、usb、sata。如果省略,总线类型从设备名来推断,例如设备名是sda,则使用scsi类型的总线。tray属性指定可移动磁盘的状态,例如cdrom或者floppy,它的值是open或closed,默认是closed。

          driver允许更进一步的指定hypervisor driver的相关细节。如果hypervisor支持多个后端驱动程序,name属性选择一个主要的后端驱动的名称,可选type参数可以指定一个子类型,例如:xen支持的名称包括tap、tap2、phy、file,qemu只支持qemu名称,但是多类型的包括raw、bochs、qcow2、qed等。cache属性控制cache机制,值可以是:default、none、writethtough、writeback、directsync、unsafe。error_policy属性指定当hypervisor在读写磁盘出现错误时的行为,值可以是:stop、report、ignored、enospace,默认值是report。io属性控制IO策略,qemu客户端支持threads、native。

         readonly元素:指定客户端不能修改设备。当一个disk含有type=cdrom,readonly则是默认值。

         host元素:有两个属性name和port,分别指定了hostname和port。

 网络接口:
      有好几种网络接口访问客户端:Virtual network、Bridge to LAN、Userspace SLIRP stack、Generic ethernet connection、Direct attachment to physical interface。
      Virtual network:这种推荐配置一般是对使用动态/无线网络环境访问客户端的情况。
      Bridge to LAN:这种推荐配置一般是使用静态有限网络连接客户端的情况。
      <interface type='bridge'>
          <source bridge='br0'/>
          <mac address='52:54:00:ad:82:97'/>
          <model type='virtio'/>
      </interface>

使用默认的虚拟网络代替网桥,即guest为NAT模式。也可以省略mac地址元素,这样将自动生成mac地址
<interface type='network'>         
    <source network='default'/>
     <mac address="3B:6E:01:69:3A:11"/>
</interface>
使用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
  输入设备:
      输入设备允许使用图形化界面和虚拟机交互,当有图形化framebuffer的时候,输入设备会被自动提供的。
      <input type='mouse' bus='ps2'/>
      input元素:input元素含有一个强制的属性,type属性的值可以是mouse活tablet,前者使用想对运动,后者使用绝对运动。bus属性指定一个明确的设备类型,值可以是:xen、ps2、usb。

  图形设备:
      图形设备允许有个图形接口和客户端进行交互,客户端有图形接口和text console模式允许admin进行交互。
      <graphics type='vnc' port='-1' keymap='en-us'/>
      <graphics type='vnc' port='5904'>
           <listen type='address' address='1.2.3.4'/>
      </graphics>
       graphics元素:graphics含有一个强制的属性type,type的值可以是:sdl、vnc、rdp、desktop。vnc则启动vnc服务,port属性指定tcp端口,如果是-1,则表示自动分配,vnc的端口自动分配的话是从5900向上递增。listen属性提供一个IP地址给服务器监听,可以单独在listen元素中设置。passwd属性提供一个vnc的密码。keymap属性提供一个keymap使用。
Rather than putting the address information used to set up the listening socket for graphics types vnc and spice in the <graphics> listen attribute, a separate subelement of <graphics>, called <listen> can be specified (see the examples above)since 0.9.4. <listen> accepts the following attributes:
        listen元素:listen元素专门针对vnc和spice设置监听端口等。它包含以下属性:type、address、network。type的值可以是address或network。如果设置了type=address,那么address属性设置一个ip地址或者主机名来监听。如果type=network,则network属性设置一个网络名称在libvirt‘s的网络配置文件中。

     字符设备提供同虚拟机进行交互的接口,Paravirtualized consoles, serial ports, parallel ports and channels 都是字符设备,它们使用相同的语法。

串行端口:
     <serial type='pty'>
         <target port='0'/>
     </serial>
    <console type='pty'>
         <target type='serial' port='0'/>
     </console>

     Pseudo TTY 分配使用/dev/ptmx,A suitable client such as 'virsh console' can connect to interact with the serial port locally.
     <parallel type='pty'>
         <source path='/dev/pts/2'/>
         <target port='0'/>
    </parallel>

     在每组指令中,最顶层的指令(parallel, serial, console, channel)描述设备怎样出现在客户端中,客户端接口通过target配置。
     The interface presented to the host is given in the type attribute of the top-level element. The host interface is configured by the source element

     主机接口通过source元素配置。

声音设备:
    <video>
        <model type='cirrus'/>
    </video>
     video元素:是描述声音设备的容器,为了向后完全兼容,如果没有设置video但是有graphics在xml配置文件中,这时libvirt会按照客户端类型增加一个默认的video,。model元素有一个强制的type属性,它的值可以是:vga、cirrus、vmvga、xen、vbox、qxl。例如一个客户端类型为kvm,那么默认的type值是cirrus。

在技术上KVM-QMEU架构采用了一种叫virtio-serial的技术,这种技术处理主机用户空间和客户机用户空间的数据传输。它主要包含两个部分:1. Qemu中模拟一个叫virtio-pci的设备,这个设备提供给客户机使用;2.客户机上安装一个字符设备驱动访问virtio-pci设备。

在实现上,大致需要以下一些工作:

     在用libvirt定义虚拟机的时候需要包含添加virtio-serial的控制器设备:

<controller type='virtio-serial' index='0'>

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

</controller>

 

 

 

该设备挂载在PCI总线0的第5个槽位上。这个设备就是QEMU中模拟的,提供给客户及访问的virtio-pci设备;

 

2.       在用libvirt定义虚拟机的时候需要包含一个叫“com.redhat.spice.0”的客户机接口,它是”channel”类型,其他的客户机接口类型还有parallel port, serial port, console等,它们从本质上来说都是一种提供给客户机访问的字符设备。

<channel type='spicevmc'>

<target type='virtio' name='com.redhat.spice.0'/>

<address type='virtio-serial' controller='0' bus='0' port='1'/>

</channel>

 

 

 

 

 

Channel还可以分为”spicevmc””unix”, “pty”等类型,具体可以参考libvirt的说明文档:http://www.libvirt.org/formatdomain.html

注意上面的”address”,它指定了这个字符设备是挂载到了我们在前面定义的virtio-serial控制器上,这样这两者就关联起来了。

 

 

 

 

 

<domain type="kvm">
  <name>6F61D5</name>
  <uuid>b3c40c40-d5c6-11e3-b22d-008cfa0c6caa</uuid>
  <memory unit="KiB">2097152</memory>
  <currentMemory unit="KiB">2097152</currentMemory>
  <vcpu current="2" placement="static">2</vcpu>
  <os>
    <type arch="x86_64" machine="">hvm</type>
    <boot dev="hd" />
  </os>
  <features>
    <acpi />
    <apic />
    <pae />
  </features>
  <cpu>
    <topology cores="8" sockets="2" threads="2" />
  </cpu>
  <clock offset="localtime" />
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/local/bin/qemu-system-x86_64</emulator>
    <disk device="disk" type="file">
      <driver name="qemu" type="qcow2" />
      <source file="/home/cloud/vebula/mounts/vms/6F61D5" />
      <target bus="virtio" dev="vda" />
    </disk>
    <disk device="cdrom" type="file">
      <target bus="ide" dev="hdc" />
    </disk>
    <interface type="bridge">
      <mac address="0a:ee:ee:6f:61:d5" />
      <source bridge="br5dd77f8620" />
      <target dev="in0c83a71e" />
      <virtualport type="openvswitch" />
      <model type="virtio" />
    </interface><serial type="pty">
      <target port="0" />
    </serial>
    <console type="pty">
      <target port="0" type="serial" />
    </console>
    <input bus="usb" type="tablet" />
    <input bus="ps2" type="mouse" />
    <sound model="ich6" />
    <video>
      <model heads="1" type="qxl" vram="65536" />
    </video>
  <channel type="spicevmc">
      <target name="com.redhat.spice.0" type="virtio" />
      <address bus="0" controller="0" port="1" type="virtio-serial" />
    </channel>
    <controller index="0" model="ich9-ehci1" type="usb">  
      <address bus="0x00" domain="0x0000" function="0x7" slot="0x0c" type="pci" />
    </controller>
    <controller index="0" model="ich9-uhci1" type="usb">
      <master startport="0" />
      <address bus="0x00" domain="0x0000" function="0x0" multifunction="on" slot="0x0c" type="pci" />
    </controller>
    <controller index="0" model="ich9-uhci2" type="usb"> 
     <master startport="2" />
      <address bus="0x00" domain="0x0000" function="0x1" slot="0x0c" type="pci" />
    </controller>
    <controller index="0" model="ich9-uhci3" type="usb">  
      <master startport="4" />
      <address bus="0x00" domain="0x0000" function="0x2" slot="0x0c" type="pci" />
    </controller> 
    <controller index="0" type="virtio-serial">
    </controller>
    <graphics autoport="no" listen="0.0.0.0" passwd="BrwShI4SfKYAgD5p" port="20002" type="spice">
      <listen address="0.0.0.0" type="address" />
      <image compression="auto_glz" />
    </graphics>
    <redirdev bus="usb" type="spicevmc">
    </redirdev>
    <redirdev bus="usb" type="spicevmc">
    </redirdev>
    <memballoon model="virtio">
    </memballoon>
    </devices>
</domain>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics