参考源地址:http://libvirt.org/formatdomain.html
http://blog.csdn.net/qq250941970/article/details/6022094
libvirt是管理KVM的工具,通过libvirt可以管理配置虚拟机。而虚拟机的配置主要都是通过libvirt的xml文件来实现的,因此xml是十分重要的。
Domain XML format
从官网文档中,我们可以发现xml文件具有一定的格式。
1、Element and attribute overview(元素和属性概述)
根元素是所有虚拟机的域。它有两个属性,第一个属性是指定Hypervisor的类型,如”Xen”,“KVM”,“qemu”,“lxc”和“kqemu”。第二个属性是id,运行的guest Domain 的ID,已停用的guest Domain有没有ID值。
<domain type=\'xen\' id=\'3\'> <name>xp</name> <uuid>4dea22b31d52d8f32516782e98ab3fa0</uuid> <description>Some human readable description</description>
2、Operating system booting(操作系统的启动)
BIOS bootloader
<os>
<type>hvm</type>
<loader readonly=\'yes\' type=\'rom\'>/usr/lib/xen/boot/hvmloader</loader>
<nvram template=\'/usr/share/OVMF/OVMF_VARS.fd\'>/var/lib/libvirt/nvram/guest_VARS.fd</nvram>
<boot dev=\'hd\'/>
<boot dev=\'cdrom\'/>
<bootmenu enable=\'yes\' timeout=\'3000\'/>
<smbios mode=\'sysinfo\'/>
<bios useserial=\'yes\' rebootTimeout=\'0\'/>
</os>
type 表示全虚拟化还是半虚拟化,hvm表示全虚拟化
loader 全虚拟化才需要的,表示全虚拟化的守护进程所在位置
boot 启动设备列表,如"fd"表示从文件启动, "hd"从硬盘启动, "cdrom"从光驱启动 和 "network"从网络启动,同时启动次序也可以由此顺序决定。
bootmenu 当虚拟机启动时是否启用引导菜单,默认时表示使用引导菜单
Host bootloader
半虚拟化的虚拟机启动一般使用这个,在主机使用一个伪引导来为guest Domain提供一个界面选择启动的内核。下面是个xen下半虚拟化的启动范例
<bootloader>/usr/bin/pygrub</bootloader> <bootloader_args>--append single</bootloader_args>
Bootloader给出主机操作系统引导程序的完整路径,运行这个引导程序,选择启动的内核,这个会因使用的Hypervisor不同而不同。
bootloader_args该元素的可选bootloader_args允许命令行参数传递到bootloader
<os>
<type>hvm</type>
<loader>/usr/lib/xen/boot/hvmloader</loader>
<kernel>/root/f8-i386-vmlinuz</kernel>
<initrd>/root/f8-i386-initrd</initrd>
<cmdline>console=ttyS0 ks=http://example.com/f8-i386/os/</cmdline>
</os>
<domain> ... <vcpu placement=\'static\' cpuset="1-4,^3,6" current="1">2</vcpu> ... </domain>
所谓cpuset,就是在用户空间中操作cgroup文件系统来执行进程与cpu和进程与内存结点之间的绑定,在cpu调优中一般会用到(稍后再写)
4、基本资源
<memory>524288</memory> <currentMemory>524288</currentMemory> <memoryBacking> <hugepages/> </memoryBacking> <memtune> <hard_limit>1048576</hard_limit> <soft_limit>131072</soft_limit> <swap_hard_limit>2097152</swap_hard_limit> <min_guarantee>65536</min_guarantee> </memtune> <vcpu cpuset="1-4,^3,6" current="1">2</vcpu>
<cpu match=\'exact\'> <model>core2duo</model> <vendor>Intel</vendor> <topology sockets=\'1\' cores=\'2\' threads=\'1\'/> <feature policy=\'disable\' name=\'lahf_lm\'/> </cpu>
这里可以看到各种资源的配置。内存大小、页面大小、memtune内存设定。cpu的model等等
5、 Lifecycle control(操作控制)
<on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash>
用户请求不同动作时,对domain的执行也不同。
<devices>
<disk type=\'file\' snapshot=\'external\'>
<driver name="tap" type="aio" cache="default"/>
<source file=\'/var/lib/xen/images/fv0\' startupPolicy=\'optional\'>
<seclabel relabel=\'no\'/>
</source>
<target dev=\'hda\' bus=\'ide\'/>
<iotune>
<total_bytes_sec>10000000</total_bytes_sec>
<read_iops_sec>400000</read_iops_sec>
<write_iops_sec>100000</write_iops_sec>
</iotune>
<boot order=\'2\'/>
<encryption type=\'...\'>
...
</encryption>
<shareable/>
<serial>
...
</serial>
</disk>
...
<disk type=\'network\'>
<driver name="qemu" type="raw" io="threads" ioeventfd="on" event_idx="off"/>
<source protocol="sheepdog" name="image_name">
<host name="hostname" port="7000"/>
</source>
<target dev="hdb" bus="ide"/>
<boot order=\'1\'/>
<transient/>
<address type=\'drive\' controller=\'0\' bus=\'1\' unit=\'0\'/>
</disk>
<disk type=\'network\'>
<driver name="qemu" type="raw"/>
<source protocol="rbd" name="image_name2">
<host name="hostname" port="7000"/>
<snapshot name="snapname"/>
<config file="/path/to/file"/>
</source>
<target dev="hdc" bus="ide"/>
<auth username=\'myuser\'>
<secret type=\'ceph\' usage=\'mypassid\'/>
</auth>
</disk>
<disk type=\'block\' device=\'cdrom\'>
<driver name=\'qemu\' type=\'raw\'/>
<target dev=\'hdd\' bus=\'ide\' tray=\'open\'/>
<readonly/>
</disk>
<disk type=\'network\' device=\'cdrom\'>
<driver name=\'qemu\' type=\'raw\'/>
<source protocol="http" name="url_path">
<host name="hostname" port="80"/>
</source>
<target dev=\'hde\' bus=\'ide\' tray=\'open\'/>
<readonly/>
</disk>
<disk type=\'network\' device=\'cdrom\'>
<driver name=\'qemu\' type=\'raw\'/>
<source protocol="https" name="url_path">
<host name="hostname" port="443"/>
</source>
<target dev=\'hdf\' bus=\'ide\' tray=\'open\'/>
<readonly/>
</disk>
<disk type=\'network\' device=\'cdrom\'>
<driver name=\'qemu\' type=\'raw\'/>
<source protocol="ftp" name="url_path">
<host name="hostname" port="21"/>
</source>
<target dev=\'hdg\' bus=\'ide\' tray=\'open\'/>
<readonly/>
</disk>
<disk type=\'network\' device=\'cdrom\'>
<driver name=\'qemu\' type=\'raw\'/>
<source protocol="ftps" name="url_path">
<host name="hostname" port="990"/>
</source>
<target dev=\'hdh\' bus=\'ide\' tray=\'open\'/>
<readonly/>
</disk>
<disk type=\'network\' device=\'cdrom\'>
<driver name=\'qemu\' type=\'raw\'/>
<source protocol="tftp" name="url_path">
<host name="hostname" port="69"/>
</source>
<target dev=\'hdi\' bus=\'ide\' tray=\'open\'/>
<readonly/>
</disk>
<disk type=\'block\' device=\'lun\'>
<driver name=\'qemu\' type=\'raw\'/>
<source dev=\'/dev/sda\'/>
<target dev=\'sda\' bus=\'scsi\'/>
<address type=\'drive\' controller=\'0\' bus=\'0\' target=\'3\' unit=\'0\'/>
</disk>
<disk type=\'block\' device=\'disk\'>
<driver name=\'qemu\' type=\'raw\'/>
<source dev=\'/dev/sda\'/>
<geometry cyls=\'16383\' heads=\'16\' secs=\'63\' trans=\'lba\'/>
<blockio logical_block_size=\'512\' physical_block_size=\'4096\'/>
<target dev=\'hdj\' bus=\'ide\'/>
</disk>
<disk type=\'volume\' device=\'disk\'>
<driver name=\'qemu\' type=\'raw\'/>
<source pool=\'blk-pool0\' volume=\'blk-pool0-vol0\'/>
<target dev=\'hdk\' bus=\'ide\'/>
</disk>
<disk type=\'network\' device=\'disk\'>
<driver name=\'qemu\' type=\'raw\'/>
<source protocol=\'iscsi\' name=\'iqn.2013-07.com.example:iscsi-nopool/2\'>
<host name=\'example.com\' port=\'3260\'/>
</source>
<auth username=\'myuser\'>
<secret type=\'iscsi\' usage=\'libvirtiscsi\'/>
</auth>
<target dev=\'vda\' bus=\'virtio\'/>
</disk>
<disk type=\'network\' device=\'lun\'>
<driver name=\'qemu\' type=\'raw\'/>
<source protocol=\'iscsi\' name=\'iqn.2013-07.com.example:iscsi-nopool/1\'>
<host name=\'example.com\' port=\'3260\'/>
</source>
<auth username=\'myuser\'>
<secret type=\'iscsi\' usage=\'libvirtiscsi\'/>
</auth>
<target dev=\'sdb\' bus=\'scsi\'/>
</disk>
<disk type=\'volume\' device=\'disk\'>
<driver name=\'qemu\' type=\'raw\'/>
<source pool=\'iscsi-pool\' volume=\'unit:0:0:1\' mode=\'host\'/>
<auth username=\'myuser\'>
<secret type=\'iscsi\' usage=\'libvirtiscsi\'/>
</auth>
<target dev=\'vdb\' bus=\'virtio\'/>
</disk>
<disk type=\'volume\' device=\'disk\'>
<driver name=\'qemu\' type=\'raw\'/>
<source pool=\'iscsi-pool\' volume=\'unit:0:0:2\' mode=\'direct\'/>
<auth username=\'myuser\'>
<secret type=\'iscsi\' usage=\'libvirtiscsi\'/>
</auth>
<target dev=\'vdc\' bus=\'virtio\'/>
</disk>
<disk type=\'file\' device=\'disk\'>
<driver name=\'qemu\' type=\'qcow2\'/>
<source file=\'/var/lib/libvirt/images/domain.qcow\'/>
<backingStore type=\'file\'>
<format type=\'qcow2\'/>
<source file=\'/var/lib/libvirt/images/snapshot.qcow\'/>
<backingStore type=\'block\'>
<format type=\'raw\'/>
<source dev=\'/dev/mapper/base\'/>
<backingStore/>
</backingStore>
</backingStore>
<target dev=\'vdd\' bus=\'virtio\'/>
</disk>
</devices>
Encryption如果存在,指定如何给存储卷加密,查看存储加密页面更多信息
Serial 这个在全虚拟化中比较重要,如果用文件存储,不加下面一段代码,虚拟机无法运行,下面代码是指出硬盘为串口
<serial type=\'pty\'>
<source path=\'/dev/pts/3\'/>
<target port=\'0\'/>
</serial>
<devices>
<controller type=\'usb\' index=\'0\' model=\'ich9-ehci1\'>
<address type=\'pci\' domain=\'0\' bus=\'0\' slot=\'4\' function=\'7\'/>
</controller>
<controller type=\'usb\' index=\'0\' model=\'ich9-uhci1\'>
<master startport=\'0\'/>
<address type=\'pci\' domain=\'0\' bus=\'0\' slot=\'4\' function=\'0\' multifunction=\'on\'/>
</controller>
...
</devices>
USB设备的xml描述格式如上所示。
官网关于USB重定向的描述:
Redirected devices
USB device redirection through a character device is supported since after 0.9.5 (KVM only):
<devices>
<redirdev bus=\'usb\' type=\'tcp\'>
<source mode=\'connect\' host=\'localhost\' service=\'4000\'/>
<boot order=\'1\'/>
</redirdev>
<redirfilter>
<usbdev class=\'0x08\' vendor=\'0x1234\' product=\'0xbeef\' version=\'2.56\' allow=\'yes\'/> //0x08指大容量存储设备,文件系统。此处指只有大容量存储设备才能访问虚拟机
<usbdev allow=\'no\'/> //其他USB设备不行
</redirfilter>
</devices>
USB常用设备类别和ID:
http://wenku.baidu.com/link?url=66ymV2VeFOTlFCAEVG2iU0yZ3TM5zzWOzs2ICDUNdfK_ZIbg0CKNxgVCS1j-UMfoWSzQ954WsPPx8tfl9vbJz-Tm8nbizgdLNvaEKoteItS
除此之外,还有网络设备,视频声卡