Virsh虚拟化简介Qemu是一个广泛使用的开源计算机仿真器和虚拟机当作为仿真器时,可以在一种架构(如PC机)下运行另一种架构(如ARM)下的操作系统和程序。通过动态转化,可以获得很高的运行效率。当QEME作为虚拟机时,可以使用xen或kvm访问CPU的扩展功能(HVM),在主机CPU上直接执行虚拟客户端的代码,获得接近于真机的性能表现一.创建虚机步骤(1)生成硬盘镜像文件:(格式:raw或qcow2)1.#qemu-imgcreate-frawtest_disk.img10G2.#qemu-imginfotest_disk.img(2)编写xml配置文件,见详情(3)创建并运行虚拟机1.#sudoapt-getinstallqemu-kvm2.#virshdefinevm0.xml//导入虚拟机配置3.#virshstartvm0//开启vm04.#virshlist--all//显示所有虚拟机状态5.#virshdestroyvm0//销毁虚拟机6.#virshundefinevm1//删除虚拟机配置7.#virshdumpxmlvm0//显示虚拟机xml配置二.详情:test.xmldomaintype='kvm'//如果是Xen,则type=‘xen’namevm0/name//虚拟机名称,同一物理机唯一uuidfd3877db-2556-44e9-b067-3e4e48211353/uuid//同一物理机唯一,可用uuidgen生成memory524288/memory//最大内存,单位kcurrentMemory524288/currentMemory//可用内存,单位k,与memory值可设成一致vcpu2/vcpu//虚拟机的cpu数量,查看物理机CPU:cat/proc/cpuinfo|grepprocessor|wc-lostypearch='x86_64'machine='pc-i440fx-vivid'hvm/type//arch系统架构类型,machine则是机器类型,查看机器类型:qemu-system-x86_64-M?bootdev='hd'///启动介质,第一次需要装系统可以选择cdrom光盘启动bootdev='cdrom'///光盘启动bootmenuenable='yes'///表示启动按F12进入启动菜单/osfeaturesacpi///AdvancedConfigurationandPowerInterface,高级配置与电源接口apic///AdvancedProgrammableInterruptController,高级可编程中断控制器pae///PhysicalAddressExtension,物理地址扩展/featuresclockoffset='localtime'///虚拟机时钟设置,这里表示本地本机时间on_poweroffdestroy/on_poweroff//突发事件动作on_rebootrestart/on_rebooton_crashrestart/on_crashdevices//设备配置emulator/usr/bin/kvm/emulator//如果是Xen则是/usr/lib/xen/binqemu-dmdisktype='file'device='disk'//硬盘drivername='qemu'type='raw'/sourcefile='/opt/vm/vmdev/fdisk.img'///目的镜像路径targetdev='vda'bus='virtio'/addresstype='pci'domain='0x0000'bus='0x00'slot='0x06'function='0x0'///域、总线、槽、功能号,slot值同一虚拟机上唯一/diskdisktype='file'device='disk'drivername='qemu'type='raw'/sourcefile='/opt/vm/vmdev/fdisk2.img'/targetdev='vdb'bus='virtio'//diskdisktype='file'device='cdrom'//光盘drivername='qemu'type='raw'/sourcefile='/opt/vm/vmiso/ubuntu-15.10-server-amd64.iso'///光盘镜像路径targetdev='hdc'bus='ide'/readonly//disk/*利用Linux网桥连接网络*/interfacetype='bridge'macaddress='fa:92:01:33:d4:fa'///为虚拟机分配mac地址,务必唯一,否则dhcp获得同样ip,引起冲突sourcebridge='br100'///配置的网桥网卡名称targetdev='vnet0'///同一网桥下相同aliasname='net0'///别名,同一网桥下相同addresstype='pci'domain='0x0000'bus='0x00'slot='0x03'function='0x0'///注意slot值唯一/interface/*利用ovs网桥连接网络*/interfacetype='bridge'sourcebridge='br-ovs0'/virtualporttype='openvswitch'/targetdev='tap0'/modeltype='virtio'//interface/*配置成pci直通虚拟机连接网络,SR-IOV网卡的VF场景*/hostdevmode='subsystem'type='pci'managed='yes'sourceaddressdomain='0x0000'bus='0x03'slot='0x00'function='0x0'//source/hostdev/*利用vhostuser连接ovs端口*/interfacetype='vhostuser'macaddress='fa:92:01:33:d4:fa'///为虚拟机分配mac地址,务必唯一,否则dhcp获得同样ip,引起冲突sourcetype='unix'path='/var/run/vhost-user/tap0'mode='client'/modeltype='virtio'/drivervringbuf='2048'/addresstype='pci'domain='0x0000'bus='0x00'slot='0x03'function='0x0'//interfaceinterfacetype='network'//基于虚拟局域网的网络macaddress='52:54:4a:e1:1c:84'///可用命令生成,见下面的补充sourcenetwork='default'///默认targetdev='vnet1'///同一虚拟局域网的值相同aliasname='net1'/addresstype='pci'domain='0x0000'bus='0x00'slot='0x04'function='0x0'///注意slot值/interfacegraphicstype='vnc'port='5900'autoport='yes'listen='0.0.0.0'keymap='en-us'///配置vnc,windows下可以使用vncviewer登录,获取vnc端口号:virshvncdisplayvm0。vnc方式登录,端口号自动分配,自动加1,可以通过virshvncdisplay来查询已启动vnclistentype='address'address='0.0.0.0'//graphics/devices/domain三.出现问题:1.问题一:【问题现象】执行virshdefinetest.xml出现error:failedtoconnecttothehypervisorerror:Failedtoconnectsocketto'/var/run/libvirt/libvirt-sock':Nosuchfileordirectory【问题分析】libvirtd进程没有启动,找不到/var/run/libvirt/libvirt-sock【解决办法】执行systemctlstartlibvirtd2.问题二:【问题现象】执行virshdefinetest.xml出现error:Failedtodefinedomainfromtest.xmlerror:invalidargument:couldnotfindcapabilitiesforarch=x86_64【问题分析】系统找不到X86_64驱动【解决办法】执行yuminstall-yqemu-system-x86.x86_64安装驱动3.问题三:【问题现象】执行virshdefinetest.xml出现error:Failedtodefinedomainfromtest.xmlerror:CannotcheckQEMUbinary/usr/libexec/qemu-kvm:Nosuchfileordirectory【问题分析】系统找不到qemu-kvm执行文件【解决办法】执行yuminstall-yqemu-kvm4.问题四:【问题现象】执行virshstartlinux-1出现error:Failedtostartdomainlinux-1error:internalerror:qemuunexpectedlyclosedthemonitor:2017-10-26T09:21:16.678064Zqemu-kvm:-drivefile=/root/virsh_test/test.img,format=raw,if=none,id=drive-scsi0-0-0-0,readonly=on:couldnotopendiskimage/root/virsh_test/test.img:Couldnotopen'/root/virsh_test/test.img':Permissiondenied【问题分析】读取文件没有权限【解决办法】修改libvirt的配置文件,修改/etc/libvirt/qemu.conf为user=root,group=root,重启libvirtd(systemctlrestartlibvirtd)