文章目录

  • 参考
  • 缩写
  • 潜意识
  • 代码阅读的问题
    • qemu
  • virsh
    • 重新创建vm
    • virsh dump
  • qemu-kvm
    • --device
      • virtio-net-pci
    • -machine
    • -netdev / -net
    • -smp
    • 创建vm,基于vmlinz
    • 错误总结
      • Failed to create chardev
      • ramdisk 的大小超过80M,boot failure
      • 指定串口速率
  • qemu-img
    • info
      • file format
      • virtual size
      • disk size
  • 代码阅读
    • qemu 代码阅读
  • 参考

参考

1、帮助手册:https://www.mankier.com/1/qemu
2、source code: qemu-options.hx
3、qemu object 模型:https://github.com/Gyumeijie/qemu-object-model
4、支持的架构:https://qemu.readthedocs.io/en/latest/system/targets.html
5、vhost-user说明:https://blog.csdn.net/sinat_20184565/article/details/83070894

缩写

qemu: quick emulator

潜意识

QEMU不只是支持x86架构的虚拟机,还支持很多其他类型的如:ARM,MIPS,等;但是对于每一种类型,就支持那么几种通用性强,性能好的硬件架构。很固定的模式。
需要看参考4;

代码阅读的问题

qemu

需要首先了解,object这一套结构的机制。

virsh

重新创建vm

修改虚拟机的xml文件
virsh shutdown 原来的虚拟机
virsh undefine 原来的虚拟机
virsh define 新的 xml文件,创建虚拟机
virsh start 虚拟机

virsh dump

如果是openstack的环境,virsh dump --file 指定文件路径后,生成的文件,不是在 指定路径而是在:
/var/lib/docker/overlay2/****/diff/root

可以用来dump vm的 memory,是不是可以用gdb 来做dump;

下面这个链接有关于如何生成guest vm的的core、crash文件。
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/virtualization_deployment_and_administration_guide/sect-domain_commands-creating_a_dump_file_of_a_domains_core

qemu-kvm

qemu manual : https://www.mankier.com/1/qemu

–device

-device driver[,prop[=value][,...]]

添加一个设备驱动。指定属性:prop=value。
依据设备驱动各自的属性,来判断属性值;想获得可能的驱动帮助和属性列表可以使用:“-device help” and “-device driver,help”.

virtio-net-pci

[root@10 ~]# /usr/libexec/qemu-kvm -device virtio-net-pci,help
virtio-net-pci options:
acpi-index= - (default: 0)
addr= - Slot and optional function number, example: 06.0 or 06 (default: -1)
aer= - on/off (default: false)
any_layout= - on/off (default: true)
ats= - on/off (default: false)
bootindex=
csum= - on/off (default: true)
ctrl_guest_offloads= - on/off (default: true)
ctrl_mac_addr= - on/off (default: true)
ctrl_rx= - on/off (default: true)
ctrl_rx_extra= - on/off (default: true)
ctrl_vlan= - on/off (default: true)
ctrl_vq= - on/off (default: true)
disable-legacy= - on/off/auto (default: “auto”)
disable-modern= - (default: false)
duplex=
event_idx= - on/off (default: true)
failover= - (default: false)
failover_pair_id=
gso= - on/off (default: true)
guest_announce= - on/off (default: true)
guest_csum= - on/off (default: true)
guest_ecn= - on/off (default: true)
guest_rsc_ext= - on/off (default: false)
guest_tso4= - on/off (default: true)
guest_tso6= - on/off (default: true)
guest_ufo= - on/off (default: true)
hash= - on/off (default: false)
host_ecn= - on/off (default: true)
host_mtu= - (default: 0)
host_tso4= - on/off (default: true)
host_tso6= - on/off (default: true)
host_ufo= - on/off (default: true)
indirect_desc= - on/off (default: true)
ioeventfd= - on/off (default: true)
iommu_platform= - on/off (default: false)
mac= - Ethernet 6-byte MAC Address, example: 52:54:00:12:34:56
migrate-extra= - on/off (default: true)
modern-pio-notify= - on/off (default: false)
mq= - on/off (default: false)
mrg_rxbuf= - on/off (default: true)
multifunction= - on/off (default: false)
netdev= - ID of a netdev to use as a backend
notify_on_empty= - on/off (default: true)
packed= - on/off (default: false)
page-per-vq= - on/off (default: false)
rombar= - (default: 1)
romfile=
romsize= - (default: 4294967295)
rsc_interval= - (default: 300000)
rss= - on/off (default: false)
rx_queue_size= - (default: 256)
speed= - (default: -1)
status= - on/off (default: true)
tx=
tx_queue_size= - (default: 256)
use-disabled-flag= - (default: true)
use-started= - (default: true)
vectors= - (default: 4294967295)
virtio-backend=<child>
virtio-pci-bus-master-bug-migration= - on/off (default: false)
x-ats-page-aligned= - on/off (default: true)
x-disable-legacy-check= - (default: false)
x-disable-pcie= - on/off (default: false)
x-ignore-backend-features= - (default: false)
x-mtu-bypass-backend= - (default: true)
x-pcie-deverr-init= - on/off (default: true)
x-pcie-extcap-init= - on/off (default: true)
x-pcie-flr-init= - on/off (default: true)
x-pcie-lnkctl-init= - on/off (default: true)
x-pcie-lnksta-dllla= - on/off (default: true)
x-pcie-pm-init= - on/off (default: true)
x-txburst= - (default: 256)
x-txtimer= - (default: 150000)

-machine

-machine pc-i440fx-rhel7.0.0,accel=kvm,usb=off,dump-guest-core=off
[root@10 ~]# /usr/libexec/qemu-kvm -machine help
Supported machines are:
pc RHEL 7.6.0 PC (i440FX + PIIX, 1996) (alias of pc-i440fx-rhel7.6.0)
pc-i440fx-rhel7.6.0 RHEL 7.6.0 PC (i440FX + PIIX, 1996) (default)
pc-i440fx-rhel7.5.0 RHEL 7.5.0 PC (i440FX + PIIX, 1996)
pc-i440fx-rhel7.4.0 RHEL 7.4.0 PC (i440FX + PIIX, 1996)
pc-i440fx-rhel7.3.0 RHEL 7.3.0 PC (i440FX + PIIX, 1996)
pc-i440fx-rhel7.2.0 RHEL 7.2.0 PC (i440FX + PIIX, 1996)
pc-i440fx-rhel7.1.0 RHEL 7.1.0 PC (i440FX + PIIX, 1996)
pc-i440fx-rhel7.0.0 RHEL 7.0.0 PC (i440FX + PIIX, 1996)
q35 RHEL-8.6.0 PC (Q35 + ICH9, 2009) (alias of pc-q35-rhel8.6.0)
pc-q35-rhel8.6.0 RHEL-8.6.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel8.5.0 RHEL-8.5.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel8.4.0 RHEL-8.4.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel8.3.0 RHEL-8.3.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel8.2.0 RHEL-8.2.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel8.1.0 RHEL-8.1.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel8.0.0 RHEL-8.0.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel7.6.0 RHEL-7.6.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel7.5.0 RHEL-7.5.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel7.4.0 RHEL-7.4.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel7.3.0 RHEL-7.3.0 PC (Q35 + ICH9, 2009)
none empty machine

[root@10 ~]# /usr/libexec/qemu-kvm -machine pc-i440fx-rhel7.0.0,help
pc-i440fx-rhel7.0.0-machine options:
acpi= - Enable ACPI
append= - Linux kernel command line
bus-lock-ratelimit=<uint64_t> - Set the ratelimit for the bus locks acquired in VMs
confidential-guest-support=<link> - Set confidential guest scheme to support
default-bus-bypass-iommu=
dt-compatible= - Overrides the “compatible” property of the dt root node
dtb= - Linux kernel device tree file
dump-guest-core= - Include guest memory in a core dump;是否包含guest的内存,在core dump文件里;
这个使用时特别小心,如果guest的内存很大,和容易将host的磁盘吃光。需要权衡利弊。
https://libvirt.org/kbase/qemu-core-dump.html
dumpdtb= - Dump current dtb to a file and quit
firmware= - Firmware image
graphics= - Set on/off to enable/disable graphics emulation
hpet=
initrd= - Linux initial ramdisk file
kernel= - Linux kernel image file
max-fw-size= - Maximum combined firmware size
max-ram-below-4g= - Maximum ram below the 4G boundary (32bit boundary)
mem-merge= - Enable/disable memory merge support
memory-backend= - Set RAM backendValid value is ID of hostmem based backend
memory-encryption= - Set memory encryption object to use
phandle-start= - The first phandle ID we may generate dynamically
pit=
sata=
sgx-epc= - SGX EPC device
smbus=
smm= - Enable SMM
smp= - CPU topology
suppress-vmdesc= - Set on to disable self-describing migration
usb= - Set on/off to enable/disable usb
vmport= - Enable vmport (pc & q35)
x-oem-id= - Override the default value of field OEMID in ACPI table header.The string may be up to 6 bytes in size
x-oem-table-id= - Override the default value of field OEM Table ID in ACPI table header.The string may be up to 8 bytes in size

-netdev / -net

这个参数可以有多个格式,分别对应建立的网络设备;基本格式:

   -netdev tap,id=id[,fd=h][,ifname=name][,script=file][,downscript=dfile][,helper=helper]-net tap[,vlan=n][,name=name][,fd=h][,ifname=name][,script=file][,downscript=dfile][,helper=helper]下面这个是增强模式,支持vlan:Connect the host TAP network interface name to VLAN n.

这个参数可以使用脚本来配置,网络设备:
script=file是配置脚本;
downscript=dfile是消除配置脚本;如果没有提供ifname参数,系统会默认提供一个;默认的配置脚本在:/etc/qemu-ifup,消除配置脚本是在/etc/qemu-ifdown。如果不想使用脚本可以设置尾no:script=no 和downscript=no;
如果当前qemu执行的用户非root用户,可以是i有helper参数来帮助对TAP设备进行配置,默认的帮助脚本是:/path/to/qemu-bridge-helper。
fd=h,可以用来指定已经打开的主机TAP设备的处理程序,handler,文件描述符。

举例: #launch a QEMU instance with the default network script
qemu-system-i386 linux.img -net nic -net tap
#launch a QEMU instance with two NICs, each one connected to a TAP device
qemu-system-i386 linux.img
-net nic,vlan=0 -net tap,vlan=0,ifname=tap0
-net nic,vlan=1 -net tap,vlan=1,ifname=tap1
#launch a QEMU instance with the default network helper to connect a TAP device to bridge br0
qemu-system-i386 linux.img
-net nic -net tap,“helper=/path/to/qemu-bridge-helper”

这个说明有点老;无法解释下面的参数设置;
-netdev tap,fd=32,id=hostnet0,vhost=on,vhostfd=34
" use vhost=on to enable experimental in kernel accelerator\n" ;; 使用内核加速器;
" use ‘vhostfd=h’ to connect to an already opened vhost net device\n";;链接已经打开的vhost网络设备

-smp

-smp [[cpus=]n][,maxcpus=maxcpus][,sockets=sockets][,dies=dies][,clusters=clusters][,cores=cores][,threads=threads]
模拟SMP的系统,虚拟器初始之后,模拟出n个CPU。如果模拟的板子支持CPU热拔插,maxcpus就是在系统运行时,最多可以添加到多少个CPU。如果cpus和maxcpus两个参数没有设置,会根据相关的其他参数来做计算,计算的值就是两个参数的值。When only one of them is given then the omitted one will be set to its counterpart’s value. Both parameters may be specified, but the maximum number of CPUs must be equal to or greater than the initial CPU count. Product of the CPU topology hierarchy must be equal to the maximum number of CPUs. Both parameters are subject to an upper limit that is determined by the specific machine type chosen.

To control reporting of CPU topology information, values of the topology parameters can be specified. Machines may only support a subset of the parameters and different machines may have different subsets supported which vary depending on capacity of the corresponding CPU targets. So for a particular machine type board, an expected topology hierarchy can be defined through the supported sub-option. Unsupported parameters can also be provided in addition to the sub-option, but their values must be set as 1 in the purpose of correct parsing.Either the initial CPU count, or at least one of the topology parameters must be specified. The specified parameters must be greater than zero, explicit configuration like "cpus=0" is not allowed. Values for any omitted parameters will be computed from those which are given.For example, the following sub-option defines a CPU topology hierarchy (2 sockets totally on the machine, 2 cores per socket, 2 threads per core) for a machine that only supports sockets/cores/threads. Some members of the option can be omitted but their values will be automatically computed:-smp 8,sockets=2,cores=2,threads=2,maxcpus=8

接下来的参数,就是CPU拓扑结构(2个sockets,每个socket两个dies,每个die有两个core,每个core有2个threads)
The following sub-option defines a CPU topology hierarchy (2 sockets totally on the machine, 2 dies per socket, 2 cores per die, 2 threads per core) for PC machines which support sockets/dies/cores/threads. Some members of the option can be omitted but their values will be automatically computed:

-smp 16,sockets=2,dies=2,cores=2,threads=2,maxcpus=16The following sub-option defines a CPU topology hierarchy (2 sockets totally on the machine, 2 clusters per socket, 2 cores per cluster, 2 threads per core) for ARM virt machines which support sockets/clusters /cores/threads. Some members of the option can be omitted but their values will be automatically computed:-smp 16,sockets=2,clusters=2,cores=2,threads=2,maxcpus=16Historically preference was given to the coarsest topology parameters when computing missing values (ie sockets preferred over cores, which were preferred over threads), however, this behaviour is considered liable to change. Prior to 6.2 the preference was sockets over cores over threads. Since 6.2 the preference is cores over sockets over threads.For example, the following option defines a machine board with 2 sockets of 1 core before 6.2 and 1 socket of 2 cores after 6.2:-smp 2

创建vm,基于vmlinz

-kernel bzImage Use bzImage as kernel image. The kernel can be either a Linux kernel or in multiboot format.
-append cmdline Use cmdline as kernel command line
-initrd file Use file as initial ram disk.
-name name Sets the name of the guest. This name will be displayed in the SDL window caption. The name will also be used for the VNC server. Also optionally set the top visible process name in Linux.

-serial stdio 在command 参数里指定 console=ttyS0,就可以打印出来了。debug kernel/系统启动的问题时,就用这个参数就可用。不需要其他的

-m 指定内存大小

/usr/libexec/qemu-kvm -kernel vmlinuz-4.18.0-240.15.1.el8_3.x86_64 -initrd js.lcprhel.image -append “root=/dev/ram rw root_size=300M lcpbrdtype=kvm console=ttyS0” -serial stdio -m 512

错误总结

Failed to create chardev

char device redirected to /dev/pts/12 (label charserial0)
qemu-kvm: -chardev pipe,id=charchannel0,path=/tmp/FROM-abc: Failed to create chardev
这个是什么原因:
chardev/char-pty.c
char_pty_open
error_report(“char device redirected to %s (label %s)”, pty_name, chr->label);
为什么会打印这个错误:https://gitlab.com/qemu-project/qemu/-/commit/6ade45f2ac936114d82a18ed10df1fb717f4a584
Why is it even printed? No other ChardevClass::open() prints anything on success. It’s because you need to know PTY_NAME to actually use this char device, e.g. like e.g. “socat STDIO,cfmakeraw FILE:PTY_NAME” to use the monitor’s readline interface. You can get PTY_NAME with “info chardev” (a.k.a. query-chardev for QMP), but only if you already have a monitor.

之后再怎么处理? 有log文件,可用查看log文件,在哪里?

ramdisk 的大小超过80M,boot failure

/usr/libexec/qemu-kvm -kernel vmlinuz-4.18.0-240.15.1.el8_3.x86_64 -initrd js.lcprhel.image -append “root=/dev/ram rw root_size=300M lcpbrdtype=kvm console=ttyS0” -serial stdio -m 512
如果ramdsik的大小大于80M,需要指定的内存大一点。-m 默认是128M
https://stackoverflow.com/questions/42872992/qemu-fails-to-load-when-my-initrd-cpio-is-large-80-mb
这个有什么可用供参考的log吗?

指定串口速率

/usr/libexec/qemu-kvm -kernel vmlinuz-4.18.0-240.15.1.el8_3.x86_64 -initrd js.lcprhel.image -append “root=/dev/ram rw root_size=300M lcpbrdtype=kvm console=ttyS0,115200n81” -serial stdio -m 512
指定串口的速率,这个也比较重要;不如串口不能输入命令

qemu-img

qemu-img可以离线新建,转换,修改images。It can handle all image formats supported by QEMU.
警告,绝不要在image被虚拟机或者其他进程使用三修改它。这样会导致image损坏。同时在这种情况下也不要查询image,可能导致image的状态不一致。

info

info [–object objectdef] [–image-opts] [-f fmt] [–output=ofmt] [–backing-chain] [-U] filename
查询磁盘image文件的信息。主要是查看占有的磁盘空间,原因是所占用的磁盘空间可能和显示的大小不一样。
Use it in particular to know the size reserved on disk which can be different from the displayed size. If VM snapshots are stored in the disk image, they are displayed too.
If a disk image has a backing file chain, information about each disk image in the chain can be recursively enumerated by using the option “–backing-chain”.
For instance, if you have an image chain like:
base.qcow2 <- snap1.qcow2 <- snap2.qcow2
To enumerate information about each disk image in the above chain, starting from top to base, do:
qemu-img info --backing-chain snap2.qcow2
The command can output in the format ofmt which is either “human” or “json”. The JSON output is an object
of QAPI type “ImageInfo”; with “–backing-chain”, it is an array of “ImageInfo” objects.

       "--output=human" reports the following information (for every image in the chain):imageThe image file name

file format

The image format: 这些都是什么format,格式,奇奇怪怪的格式。
Supported formats: blkdebug blklogwrites blkreplay blkverify copy-on-read file ftp ftps gluster host_cdrom host_device http https iscsi iser luks nbd null-aio null-co nvme qcow2 quorum raw rbd ssh throttle vhdx vmdk vpc

virtual size

The size of the guest disk

disk size

How much space the image file occupies on the host file system (may be shown as 0 if this information is unavailable, e.g. because there is no file system)

代码阅读

qemu 代码阅读

这一段代码的含义是,在5.1 这个版本上设置:no_kvm_steal_time = true;

static void virt_machine_5_1_options(MachineClass *mc)
{VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));virt_machine_5_2_options(mc);compat_props_add(mc->compat_props, hw_compat_5_1, hw_compat_5_1_len);vmc->no_kvm_steal_time = true;
}

参考

https://abelsu7.top/2019/03/28/kvm-review/

QEMU: virsh/qemu-kvm 总结相关推荐

  1. centos7 qemu -- 03 使用KVM虚拟机遇到的问题

    1.qemu-kvm.kvm.qemu 和 qemu-system-x86_64四种命令的区别 qemu 和 qemu-system-x86_64:启动qemu虚拟机的命令,x86_64为64位系统时 ...

  2. QEMU:QEMU与KVM基础

    文章目录 参考 介绍 QEMU与KVM 关系 架构 QEMU的任务(图中左上) 虚拟机的运行(图中右上) KVM驱动(图中下部) QEMU架构 参考 qemu的详细资料大全(入门必看!!!) unde ...

  3. virsh 关机_使用virsh管理KVM虚拟机

    使用QEMU/KVM虚拟化平台启动虚拟机,通常需要手动输入一行很长的命令,里面可能有几十个参数,要想启动多个虚拟机,以及监视多个虚拟机运行状态会非常困难,这时候就需要virsh这样一个虚拟机的管理工具 ...

  4. Qemu,KVM,Virsh傻傻的分不清

    转载自:https://blog.csdn.net/popsuper1982/article/details/79472789 当你安装了一台Linux,想启动一个KVM虚拟机的时候,你会发现需要安装 ...

  5. 计算机io工作方式,QEMU/KVM和VirtIO工作模式

    https://blog.csdn.net/shengxia1999/article/details/52244119 KVM:Kernel-Based Virtual Machine 基于内核的虚拟 ...

  6. KVM之初体验——QEMU安装虚拟机

    QEMU简介 QEMU是一款开源的模拟器及虚拟机监管器(Virtual Machine Monitor,  VMM).QEMU主要提供两种功能给用户使用.一是作为用户态模拟器,利用动态代码翻译机制来执 ...

  7. KVM,QEMU,libvirt入门学习笔记【转】

    转自:http://blog.csdn.net/julykobe/article/details/27571387 注:本文内容均来自网络,我只是在此做了一些摘抄和整理的工作,来源均有注明. 0.虚拟 ...

  8. KVM虚拟机IO处理过程(二) ----QEMU/KVM I/O 处理过程

    接着KVM虚拟机IO处理过程中Guest Vm IO处理过程(http://blog.csdn.net/dashulu/article/details/16820281),本篇文章主要描述IO从gue ...

  9. QEMU/KVM原理概述

    1. QEMU与KVM 架构 qemu 和 kvm 架构整体上分为 3 部分,对应着上图的三个部分 (左上.右上和下),3 部分分别是 VMX root 的应用层,VMX no-root 和 VMX ...

  10. Qemu(纯软实现)架构+KVM(基于kernel模拟硬件)原理(一)

    一.QEMU简介及与KVM等虚拟化的关系 QEMU是"Quick Emulator"的缩写,是一个用C语言编写的开源虚拟化软件.本文的目的是描述本人所理解的QEMU技术架构的见解, ...

最新文章

  1. 中国科学家研发新的全基因组组装算法
  2. latex 常用小结
  3. iOS开发网络篇—NSURLConnection基本使用(一)
  4. python argsort排序结果_numpy.argsort()可以对元组或列表进行排序
  5. Windows Pe 第三章 PE头文件(中)
  6. sql 实现两行数据作差
  7. 【NLP傻瓜式教程】手把手带你CNN文本分类(附代码)
  8. VTK:定向圆柱体用法实战
  9. node+express学习日记
  10. C++中extern关键字
  11. jquery 停止动画_jQuery停止动画
  12. 彩灯控制器课程设计vhdl_基于VHDL的彩灯控制器的设计
  13. 蓝牙技术|蓝牙音频LE Audio的技术特点
  14. bzoj4199品酒大会
  15. matlab批量修改文件名
  16. linux 图片压缩命令,Linux:优化和压缩JPEG和PNG图片的命令行工具
  17. 应用于高速计数器的编码器基础知识介绍
  18. python爬微信公众号视频_python爬虫公众号所有信息,并批量下载公众号视频
  19. 2019年内大892数据结构部分参考答案
  20. Lumia 1020 诞生:诺基亚拍照技术的一次狂欢

热门文章

  1. 时间控件-Jedate
  2. matlab时变函数,MATLAB在《复变函数》教学中的应用(图文)
  3. 微型计算机如何开声音,MP3声控录音机
  4. 中级工程师基础SQL试题
  5. 【数学建模】人口增长Leslie模型
  6. 怎么把文本文档txt改成html,如何将Word文档转换成txt格式文本
  7. OJ在线编程常见输入输出JavaScript练习
  8. openssl、gmssl的简单介绍
  9. 固态硬盘故障检测_diskgenius检测固态硬盘(ssd固态硬盘坏道修复)
  10. python篇---在windows下利用pyinstaller打包成exe