为什么要学习libvirt 中的XML
l Libvirt中配置虚拟机都是通过xml文件来配置的,如启动一个虚拟机需要的对虚拟机配置的描述就是通过XML来实现的。因此XML在libvirt管理虚拟机中有很重要的地位。
Domain XML format
- 1. 元素和属性概述
根元素为所有虚拟机需要的是命名域。它有两个属性,第一个属性是指定Hypervisor的类型,如”Xen”,“KVM”,“qemu”,“lxc”和“kqemu”。第二个属性是id,运行的guest Domain 的ID,已停用的guest Domain有没有ID值。
- 2. 通用元数据
每个guest Domain都有它的名称,uuid ,描述(描述可有可无)
<domain type='xen' id='3'> <name>xp</name> <uuid>4dea22b31d52d8f32516782e98ab3fa0</uuid> <description>Some human readable description</description> |
- 3. 启动操作系统
BIOS bootloader
... <os> <type>hvm</type> <loader>/usr/lib/xen/boot/hvmloader</loader> <boot dev='hd'/> <boot dev='cdrom'/> <bootmenu enable='yes'/> </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
Direct kernel boot
当安装新的一个客户机操作系统,直接从存储在主机操作系统上的内核和initrd启动是非常有效的引导,这样运行命令行参数直接传递给安装程序。全虚拟和半虚拟都可以使用这种引导方式
... <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> ... |
type
This element has the same semantics as described earlier in the BIOS boot section
loader
This element has the same semantics as described earlier in the BIOS boot section
kernel
The contents of this element specify the fully-qualified path to the kernel image in the host OS.
initrd
The contents of this element specify the fully-qualified path to the (optional) ramdisk image in the host OS.
cmdline
The contents of this element specify arguments to be passed to the kernel (or installer) at boottime. This is often used to specify an alternate primary console (eg serial port),or the installation media source / kickstart file
- 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 model and topology
<cpu match='exact'> <model>core2duo</model> <vendor>Intel</vendor> <topology sockets='1' cores='2' threads='1'/> <feature policy='disable' name='lahf_lm'/> </cpu> ... |
- 5. Lifecycle control
... <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash> ... |
- 6. Hypervisor features
... <features> <pae/> <acpi/> <apic/> </features> ... |
pae
Physical address extension mode allows 32-bit guests to address more than 4 GB of memory.
acpi
ACPI is useful for power management,for example,with KVM guests it is required for graceful shutdown to work.
- 7. Time keeping
Guest Domain 的时钟通常应初始化于主机时钟,然而大多数操作系统默认使用UTC时间,在windows中把这个叫做”localtime”
UTC协调世界时,又称世界统一时间,世界标准时间,国际协调时间。定义:以原子时为基准的一种时间计量系统,其时刻与世界时时刻差不超过±0.9s。
... <clock offset="localtime"> <timer name="rtc" tickpolicy="catchup" track="guest"> <catchup threshold=123 slew=120 limit=10000/> </timer> <timer name="pit" tickpolicy="none"/> </clock> ... |
Clock
Offset 的属性可能有四种值,用来让guest Domain 时钟采取什么方式同步到honst,但并不是所有的hypervisor都支持这四种方式
Offset的四个值:
utc
The guest clock will always be synchronized to UTC when booted
localtime
The guest clock will be synchronized to the host's configured timezone when booted,if any.
timezone
The guest clock will be synchronized to the requested timezone using the timezone attribute. Since 0.7.7
variable
The guest clock will have an arbitrary offset applied relative to UTC. The delta relative to UTC is specified in seconds,using the adjustment attribute. The guest is free to adjust the RTC over time an expect that it will be honoured at next reboot. This is in contrast to 'utc' mode,where the RTC adjustments are lost at each reboot.(根据libvirt0.7.7版,A clock
may have zero or more timer
sub-elements. Since 0.8.0 )
Timer
每一个timer元素都要求有一个name属性,指定不同的名字会有一些不同的属性,不同的hypervisor支持不同的属性组合
name
The name attribute selects which timer is being modified,and can be one of "platform","pit","rtc","hpet",or "tsc".
track
The track attribute specifies what the timer tracks,and can be "boot","guest",or "wall". Only valid for name="rtc" or name="platform".
tickpolicy
The tickpolicy attribute determines how missed ticks in the guest are handled,and can be "delay","catchup","merge",or "discard". If the policy is "catchup",there can be further details in the catchup sub-element.
catchup
The catchup element has three optional attributes,each a positive integer. The attributes are threshold,slew,and limit.
frequency
The frequency attribute is an unsigned integer specifying the frequency at which name="tsc" runs.
mode
The mode attribute controls how the name="tsc" timer is managed,and can be "auto","native","emulate","paravirt",or "smpsafe". Other timers are always emulated.
present
The present attribute can be "yes" or "no" to specify whether a particular timer is available to the guest
- 8. Devices
在最后一组xml元组中用来描述为guest Domain提供设备的,所有的设备都是在这里描述的
... <devices> <emulator>/usr/lib64/xen/bin/qemu-dm</emulator> </devices> ... |
Emulator 模拟器,给出二进制模拟器设备的完整路径
... <devices> <disk type='file'> <driver name="tap" type="aio" cache="default"/> <source file='/var/lib/xen/images/fv0'/> <target dev='hda' bus='ide'/> <encryption type='...'> ... </encryption> <shareable/> <serial> ... </serial> </disk> </devices> ... |
Encryption如果存在,指定如何给存储卷加密,查看存储加密页面更多信息
Serial 这个在全虚拟化中比较重要,如果用文件存储,不加下面一段代码,虚拟机无法运行,下面代码是指出硬盘为串口
<serial type='pty'>
<source path='/dev/pts/3'/>
<target port='0'/>
</serial>