共计 7544 个字符,预计需要花费 19 分钟才能阅读完成。
title: 可信计算_Qemu04 采用virsh创建Qemu/KVM虚拟机 date: 2022-07-11 18:38:10.702 updated: 2022-07-12 10:56:22.719 url: /archives/可信计算qemu04采用virsh创建qemukvm虚拟机 categories:
- 可信计算 tags:
- 可信计算
- KVM
1. libvirt、virsh、virt-manager
- 其中,libvirt是提供了一个方便的方式来管理虚拟机和其他虚拟化功能的软件的集合,如存储和网络接口管理。这些软件包括一个API库,一个守护进程(Libvirtd),和一个命令行实用程序(virsh)。
- libvirt的首要目标是能够管理多个不同的虚拟化供应商/虚拟机管理程序提供一个单一的方式。例如,命令“virsh列表等等都可以用于任何支持现有的虚拟机管理程序列表(KVM、Xen、VMware ESX,等)不需要学习管理程序特定的工具!
- 而virsh是命令行工具,virt-manager是GUI,带有图形界面的工具。但他们都是通过libvirt提供的接口,来进行虚拟机管理的一些操作。
virt-manager是一个面向桌面的虚拟机管理工具,通过libvirt来进行虚拟机的管理。主要用于管理KVM,也可以管理Xen和LXC(linux containers)。 Virt-manager提供了一个窗口界面用于查看运行的虚拟机(zai libvirt配置文件中,称为domain),虚拟机的性能和资源使用情况。提供了向导用来创建新虚拟机,配置、修改虚拟机的资源分配和虚拟硬件设备。一个内置的VNC 和 SPICE 客户端提供连到虚拟机的图形接口。
virt-manager还提供了命令行的支持性工具:参照官网:https://virt-manager.org/
- virt-install 命令行工具,用于部署虚拟机。(安装了virt-install之后virt-clone和virt-xml就已经安装了。)
- virt-viewer 轻量的UI接口,VNC和SPICE协议都支持,使用libvirt来查看图形链接信息。
- virt-clone命令行工具,用于克隆已经存在的(关机状态)的主机。拷贝当前磁盘镜像,定义配置文件、名字、UUID、MAC地址到新磁盘镜像。
- virt-xml命令行工具,用于编辑domain XML。(libvirt使用XML文件定义虚拟机domain)
- virt-convert命令行工具,用户转换OVF和VMX 虚拟机配置以支持使用libvirt运行。
- virt-bootstrap 命令行工具,用于为基于libvirt的container设置root file system。
以上工具需要单独安装 Centos 安装virt-install:
yum install virt-install -y
之前我们已经使用过了virt-manger,所以我们就不采用virt-manager了,这次我们使用virsh来进行创建虚拟机,virsh是需要定义xml文件来进行虚拟机相关操作的,我们可以利用virt-install来进行安装虚拟机。
3 virsh常用命令
virsh常用命令:
- virsh start x 启动名字为x的非活动虚拟机
- virsh list –all 列出虚拟机
- virsh create x.xml 创建虚拟机(创建后,虚拟机立即执行,成为活动主机)
- virsh suspend x 暂停虚拟机
- virsh resume x 启动暂停的虚拟机
- virsh shutdown x 正常关闭虚拟机
- virsh destroy x 强制关闭虚拟机
- virsh undefine x 删除虚拟机
- virsh dominfo x 显示虚拟机的基本信息
- virsh domname 2 显示id号为2的虚拟机名
- virsh domid x 显示虚拟机id号
- virsh domuuid x 显示虚拟机的uuid
- virsh domstate x 显示虚拟机的当前状态
- virsh dumpxml x 显示虚拟机的当前配置文件(可能和定义虚拟机时的配置不同,因为当虚拟机启动时,需要给虚拟机分配id号、uuid、vnc端口号等等)
- virsh setmem x 512000 给不活动虚拟机设置内存大小
- virsh edit x 编辑配置文件(一般是在刚定义完虚拟机之后)
2 安装虚拟机
同样的创建磁盘: qemu-img create -f qcow2 centos7-02.qcow2 10G
使用virt-install安装虚拟机
virt-install -n centos7-02 -r 2048 --vcpu=2 --disk path=centos7-02.qcow2 --cdrom CentOS-7-x86_64-DVD-2009.iso
- -n –name 指定虚拟机名称
- -r –ram 指定虚拟机内存
- –vcpu 指定虚拟CPU个数
- –disk 指定磁盘文件
- –cdrom 指定通过iso镜像文件启动
报错:
[root@centos7 devsdb]# virt-install -n centos7-02 -r 2048 --vcpu=2 --disk path=centos7-02.qcow2 --cdrom CentOS-7-x86_64-DVD-2009.iso
WARNING No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.
Starting install...
ERROR 不支持的配置:vmport is not available with this QEMU binary
Domain installation does not appear to have been successful.
If it was, you can restart your domain by running:
virsh --connect qemu:///system start centos7-02
otherwise, please restart your installation.
应该是Qemu版本与libvirt版本不兼容导致的,我的qemu版本是7.0.0 ,我们通过命令查看libvirt的版本:virsh version
[root@centos7 devsdb]# virsh version
Compiled against library: libvirt 4.5.0
Using library: libvirt 4.5.0
Using API: QEMU 4.5.0
Running hypervisor: QEMU 7.0.0
可以看到它使用的Qemu 4.5.0的API,而我们使用的是Qemu7.0.0版本的,所以出现以上问题。
需要降低Qemu版本,因为我们尝试升级libvirt失败:yum update libvirtd
显示:
[root@centos7 devsdb]# yum update libvirtd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.njupt.edu.cn
* extras: mirrors.cqu.edu.cn
* updates: mirrors.cqu.edu.cn
qemu-firmware-jenkins | 3.0 kB 00:00:00
No Match for argument: libvirtd
No package libvirtd available.
No packages marked for update
在安装的过程中,忘记记录了,他报了一些错误,我们换了一台机器,安装命令如下:
- 先配置网卡连接:
virsh iface-bridge eth0 br0 #eth0根据自己的机器修改
- 安装命令:
virt-install --connect qemu:///system --virt-type kvm --name rhel6 --ram=1024 --disk path=/root/centos7-02.qcow2 --os-variant=centos7.0 --accelerate -c CentOS-7-x86_64-DVD-2009.iso --network bridge=br0,model=virtio --vnc --vncport=5991 --vnclisten=0.0.0.0 --force
需要安装vncserver,这个我们之前也介绍过,还需要修改libvirtd中qemu的权限,/etc/libvirt/qemu.conf
,之前也介绍过。
可以看到,已经安装好机器,只是弹出来的virt-view是乱码,解决一下。 解决方法:https://www.landery.cn/archives/bao-cuo-ji-lu-virt-view-he-vncviewer-xian-shi-fang-kuai-luan-ma-deng-wen-ti
[root@centos7 ~]# virsh list --all
Id Name State
----------------------------------------------------
- rhel6 shut off
[root@centos7 ~]# virsh start rhel6
Domain rhel6 started
[root@centos7 ~]# virsh list --all
Id Name State
----------------------------------------------------
5 rhel6 running
[root@centos7 ~]# virt-viewer rhel6
结果如下:启动成功。
导出xml文件
virsh dumpxml rhel6 > rhel6.xml
查看生成的文件rhel6.xml:
<domain type='kvm'>
<name>rhel6</name>
<uuid>968cdfe5-eee2-44db-a3ce-9a9782dc3075</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Broadwell-noTSX-IBRS</model>
<feature policy='require' name='md-clear'/>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/root/centos7-02.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hda' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</controller>
<interface type='bridge'>
<mac address='52:54:00:91:eb:fb'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='5991' autoport='no' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>
<rng model='virtio'>
<backend model='random'>/dev/urandom</backend>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</rng>
</devices>
</domain>
virsh可以通过这种xml文件定义、创建虚拟机,命令virsh define x.xml
和 virsh create x.xml
。