Full analysis of QEMU source code 1 - QEMU parameter analysis (1)

References for the content of this article:

"Interesting Talk about Linux Operating System" —— Liu Chao, Geek Time

"QEMU/KVM" source code analysis and application - Li Qiang, Machinery Industry Press

Thank you very much!

1. QEMU parameter analysis

To analyze the QEMU source code, we must first start from the QEMU command line. The author gave some QEMU commands in the previous series of QEMU-related articles, such as:

$ qemu-system-x86_64 -enable-kvm -m 8G -smp 4 -boot once=d -drive file=./Ubuntu22.img -cdrom ../iso_images/ubuntu-22.10-desktop-amd64.iso

This section briefly introduces the parsing of QEMU command line parameters to help readers connect QEMU command line parameters with their implementation in code. The command analysis of QEMU is the following long list of contents, in the void qemu_init(int argc, char **argv) in softmmu/vl.c:

    qemu_add_opts(&qemu_drive_opts);
    qemu_add_drive_opts(&qemu_legacy_drive_opts);
    qemu_add_drive_opts(&qemu_common_drive_opts);
    qemu_add_drive_opts(&qemu_drive_opts);
    qemu_add_drive_opts(&bdrv_runtime_opts);
    qemu_add_opts(&qemu_chardev_opts);
    qemu_add_opts(&qemu_device_opts);
    qemu_add_opts(&qemu_netdev_opts);
    qemu_add_opts(&qemu_nic_opts);
    qemu_add_opts(&qemu_net_opts);
    qemu_add_opts(&qemu_rtc_opts);
    qemu_add_opts(&qemu_global_opts);
    qemu_add_opts(&qemu_mon_opts);
    qemu_add_opts(&qemu_trace_opts);
    qemu_plugin_add_opts();
    qemu_add_opts(&qemu_option_rom_opts);
    qemu_add_opts(&qemu_accel_opts);
    qemu_add_opts(&qemu_mem_opts);
    qemu_add_opts(&qemu_smp_opts);
    qemu_add_opts(&qemu_boot_opts);
    qemu_add_opts(&qemu_add_fd_opts);
    qemu_add_opts(&qemu_object_opts);
    qemu_add_opts(&qemu_tpmdev_opts);
    qemu_add_opts(&qemu_overcommit_opts);
    qemu_add_opts(&qemu_msg_opts);
    qemu_add_opts(&qemu_name_opts);
    qemu_add_opts(&qemu_numa_opts);
    qemu_add_opts(&qemu_icount_opts);
    qemu_add_opts(&qemu_semihosting_config_opts);
    qemu_add_opts(&qemu_fw_cfg_opts);
    qemu_add_opts(&qemu_action_opts);

Of course, there are also many qemu_add_opts scattered in some other files, so I won’t list them one by one here, but only grasp the main line.

Some people will say, there are too many opts, right? Indeed. The example command parameters given above are not too few, but compared with the command line parameters below, they are "insignificant".

$ qemu-system-x86_64 -enable-kvm 
                                           -name instance-00000024 
                                           -machine pc-i440fx-trusty,accel=kvm,usb=off 
                                           -cpu SandyBridge,+erms,+smep,+fsgsbase,+pdpe1gb,+rdrand,+f16c,+osxsave,+dca,+pcid,+pdcm,+xtpr,+tm2+est,+smx,+vmx,+ds_cpl,+monitor,+dtes64,+pbe,+tm,+ht,+ss,+acpi,+ds,+vme 
                                           -m 2048 
                                           -smp 1,sockets=1,cores=1,threads=1 
                                           ……
                                           -rtc base=utc,driftfix=slew 
                                           -drive file=/var/lib/nova/instances/1f8e6f7e-5a70-4780-89c1-464dc0e7f308/disk,if=none,id=drive-virtio-disk0,format=qcow2,cache=none 
                                           -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x4,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 
                                           -netdev tap,fd=32,id=hostnet0,vhost=on,vhostfd=37 
                                           -device virtio-net-pci,netdev=hostnet0,id=net0,mac=fa:16:3e:d1:2d:99,bus=pci1.0,addr=0x3 
                                           -chrdev file,id=charserial0,path=/var/lib/nova/instances/1f8e6f7e-5a70-4780-89c1-464dc0e7f308/console.log 
                                           -vnc 0.0.0.0:12 
                                           -device cirrus-vga,id-video0,bus=pci.0,addr=0x2

Now you can understand why there are so many qemu_add_opts? It is really not enough to parse these parameters.

So what do the above parameters mean? The following are detailed introductions one by one.

  • enable-kvm

Indicates that hardware-assisted virtualization is enabled.

  • -name instance-00000024

Indicates the name of the virtual machine.

  • -machine pci-i440fx-trusty,accel=kvm,usb=off

machine stands for computer architecture. qemu will simulate a variety of architectures, commonly used are ordinary PCs, that is, the 32-bit or 64-bit architecture of x86; the PowerPC architecture of Apple Mac computers; the SPARC architecture of Sun; the architecture of MIPS, etc. If you use KVM hardware-assisted virtualization and use pure simulation, there are parameters accel=tcg, -no-kvm.

  • -cpu SandyBridge,+erms,+smep,+fsgsbase,+pdpe1gb,+rdrand,+f16c,+osxsave,+dca,+pcid,+pdcm,+xtpr,+tm2+est,+smx,+vmx,+ds_cpl,+monitor,+dtes64,+pbe,+tm,+ht,+ss,+acpi,+ds,+vme

It means to set the CPU, SandyBridge is an Intel processor, and the guys behind are added CPU parameters, which will be displayed in /proc/cpuinfo.

  • -m 2048

Indicates the size of the used memory.

  • -smp 1,sockets=1,cores=1,threads=1

SMP is a symmetric multiprocessor, or UMA, which corresponds to NUMA. qemu emulates a processor with 1 vcpu, 1 socket, 1 core, and 1 threads.

What are the concepts of socket, core, and threads? The socket is the number of slots on the motherboard where the CPU is inserted, also known as the "road". core is the "core" that is often said, such as dual-core, quad-core, etc. thread refers to the number of hardware threads per core, that is, hyperthreading.

  • -rtc base=utc,driftfix=slew

Indicates that the system time is specified by the parameter -rtc.

  • -device cirrus-vga,id=video0,bus=pci.0,addr=0x2

Indicates that the display is set with the parameter -vga, and the default is cirrus, which simulates the CL-GD5446PCI VGA card.

  • -netdev tap,fd=32,id=hostnet0,vhost=on,vhostfd=37 

NIC-related settings from the HOST perspective.

  • -device virtio-net-pci,netdev=hostnet0,id=net0,mac=fa:16:3e:d1:2d:99,bus=pci1.0,addr=0x3 

NIC-related settings from the GUEST perspective.

Note: The network card device is set using the -net parameter and the -device parameter.

  • -drive file=/var/lib/nova/instances/1f8e6f7e-5a70-4780-89c1-464dc0e7f308/disk,if=none,id=drive-virtio-disk0,format=qcow2,cache=none

Hard disk related settings from the HOST perspective.

  • -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x4,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1

Hard disk related settings from the GUEST perspective.

Note: Hard disk devices are set using -hda, -hdb, or -drive and -device parameters.

  • -vnc 0.0.0.0:12

Set up VNC.

The parameters in the above command line are basically parsed. Of course, this is only part of it. Don't be discouraged, you don't need to understand everything at the moment, you only need to understand it roughly.

So how are so many parameters parsed by QEMU? Let's see the breakdown next time.

Guess you like

Origin blog.csdn.net/phmatthaus/article/details/131505960