virsh 是 Libvirt 自带 的 命令行 工具。 通过 virsh 命令, 可以 很 方便 地 对 虚拟 机 的 CPU、 内存、 网络 和 磁盘 等 各种 资源 进行 管理。 virsh 主要 是 通过 xml 文件 来 对这 些 资源 进行 描述 的。
Libvirt 的 xml 文件分为 几个 重要 部分。 首先 看 整体 结构:
在 Libvirt 官方 文档 里面, 将 虚拟 机 定义 为 domain,Xen 中 Domain 0 表示 宿主 机 系统。而在 KVM 中, domain 完全 指 虚拟 机 系统。
type 一项 指明 了 使 用的 是 哪种 虚拟 化 技术。 如果 使 用的 是 KVM, 那么 值 为 kvm。 如果 使 用的 是 Xen, 那么 值 为 xen。
下面就是一个虚拟机的完整配置文件,并加以说明:
<domain type='kvm'>
#虚拟 机 的 名字 必须 要 保证 是 唯一 的。 如果 存在 重名 的 情况, 添加 和 创建 虚拟 机时会 失败。
<name>centos_x86_6.4</name>
#UUID由字母和数字组成,不能包含空格 用uuidgen 命令可以生成UUID
<uuid>b9dcdd92-9b9b-14d6-3938-1982a9746a12</uuid>
#虚拟机内存信息,通常以KB为单位。一般而言,为了方便内存的管理与分配,都将 memory 和 currentMemory 的值设置为同一值。
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
#分配的虚拟 CPU 的个数。 单个虚拟机,最多可分配的虚拟 CPU个数,可以通过如下命令确定:# cat /proc/cpuinfo | grep processor | wc –l
#在宿主机上,所有虚拟机的虚拟机 CPU 总和可以大于虚拟 CPU 数目。此时会导致虚拟机运算性能下降。
<vcpu placement='static'>1</vcpu>
#主要是包括了两部分信息:类型;启动信息。
<os>
<type arch='x86_64' machine='pc-1.2'>hvm</type>
#arch:系统结构
#machine:机器类型
#type:表示全虚拟化还是半虚拟化,hvm表示全虚拟化
#可以通过qemu-system-x86_64 -M ? 查看支持的机器类型
<boot dev='hd'/>
#boot 怎么启动的,如"fd"表示从文件启动, "hd"从硬盘启动, "cdrom"从光驱启动 和 "network"从网络启动 #可以重复多行,指定不同的值,作为一个启动设备列表。 #The dev
attribute takes one of the values "fd", "hd", "cdrom" or "network"
<bootmenu enable='yes'>
#是否开启启动选项菜单
</os>
#处理器特性
<features>
<acpi/> #Advanced Configuration and Power Interface(高级配置与电源接口)
<apic/> #Advanced Programmable Interrupt Controller(高级可编程中断控制器)
<pae/> #Physical Address Extension物理地址扩展
</features>
<clock offset='localtime'> #时钟描述,使用本地时间
<timer name='pit' tickpolicy='delay'/>
<timer name='rtc' tickpolicy='catchup'/>
</clock>
<on_poweroff>destroy</on_poweroff> #当发生poweroff时,直接destroy虚拟机
<on_reboot>restart</on_reboot> #当发生reboot时,直接restart虚拟机
<on_crash>restart</on_crash> #同上
<devices>
#Guest需要的设备
<emulator>/bin/qemu-kvm</emulator> #KVM Hyper
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/> #目的镜像路径在这个例子中,在guest中显示为IDE设备。 镜像格式不同,所使用的xml文件格式也不相同。
<source file='/home/template_make/centos_x86_6.4.img'>
<seclabel model='selinux' relabel='no'/>
</source>
<target dev='hda' bus='ide'/>
<alias name='ide0-0-0'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/home/template_make/CentOS-6.4-x86_64-bin-DVD1.iso'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<alias name='ide0-1-0'/>
<address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>
<controller type='usb' index='0'>
<alias name='usb0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='ide' index='0'>
<alias name='ide0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<interface type='bridge'> #虚拟机网络连接方式
<mac address='52:54:00:78:f9:5a'/>
<source bridge='br0'/>
<target dev='vnet27'/> ## 使用virtio: 采用普通的驱动,即硬盘和网卡都采用默认配置情况下,硬盘是 ide 模式, 而网卡工作在 模拟的rtl 8139 网卡下,速度为
100M 全双工。 采用 virtio 驱动后,网卡工作在 1000M 的模式下,硬盘工作是SCSI模式下
<model type='virtio'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<input type='mouse' bus='ps2'/> #vnc方式登录,端口号自动分配 可以通过virsh vncdisplay来查询[vncdisplay domainId]
<graphics type='vnc' port='5915' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='9216' heads='1'/>
<alias name='video0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<alias name='balloon0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</memballoon>
</devices>
<seclabel type='dynamic' model='selinux' relabel='yes'>
<label>unconfined_u:system_r:svirt_t:s0:c362,c396</label>
<imagelabel>unconfined_u:object_r:svirt_image_t:s0:c362,c396</imagelabel>
</seclabel>
</domain>