qemu-kvm 代码分析

虚拟机组成
实际上面所说计算机组成比较笼统,实际处理器,存储器,输入与设备种类繁多,
    -  
i440FX host PCI bridge and PIIX3 PCI to ISA bridge
  总线桥:  i440FX pci桥,PIIX3 ISA桥
    -  
Cirrus CLGD 5446 PCI VGA card or dummy VGA card with Bochs VESA
           extensions (hardware level,
including all non standard modes).
  显卡:
    -  
PS/2 mouse and keyboard
  鼠标和键盘
       -  
2 PCI IDE interfaces with hard disk and CD-ROM support
  IDE的硬盘和光驱
       -  
Floppy disk
  软盘
       -  
PCI and ISA network adapters
  PCI ISA 总线
       -  
Serial ports
  串口
       -  
Creative SoundBlaster 16 sound card
  声卡 SoundBlaster
1370 AC97等
       -  
ENSONIQ AudioPCI ES1370 sound card
 
       -  
Intel 82801AA AC97 Audio compatible sound card

-  
Intel HD Audio Controller and HDA codec

-  
Adlib(OPL2) - Yamaha YM3812 compatible chip

-  
Gravis Ultrasound GF1 sound card

-  
CS4231A compatible sound card
 UHCI USB总线控制器
       -  
PCI UHCI USB controller and a virtual USB hub.
 最大支持 255个cpu
       SMP is supported with up to 255
CPUs.

qemu-kvm参数说明:
 -S -M rhel6.3.0 -enable-kvm  //启用硬件辅助模拟,模拟的机器类型为rhel6.3.0,虚拟机默认不启动。(后面解释类型为rhel6.3.0大概配置)
//机器配置
-m 1111  //内存

-smp 1,sockets=1,cores=1,threads=1 //处理器
-rtc base=localtime,clock=vm,driftfix=slew //时钟
-device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 //usb控制器usb-uhci
-device usb-ehci,id=usb1,bus=pci.0,addr=0x5 //usb控制器usb-ehci
-device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 //pci
-drive
file=/mnt/local/4d4fdbb5-216a-44a4-afae-ea6e2a80b481,if=none,id=drive-ide0-0-0,format=qcow2,cache=writeback
//ide硬盘
-device ide-drive,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=2
//主ide
-drive
if=none,media=cdrom,id=drive-ide0-1-0,readonly=on,format=raw,cache=writeback
//ide光驱
-device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0,bootindex=1  //从ide
-netdev tap,fd=23,id=hostnet0 -device
e1000,netdev=hostnet0,id=net0,mac=02:00:77:a2:00:0d,bus=pci.0,addr=0x3  //网卡
-chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0
//伪终端
-chardev spicevmc,id=charchannel0,name=vdagent -device
virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0
//串口
-device usb-tablet,id=input0,bus=usb.0,port=1 
//触摸盘
-spice port=5902,addr=0.0.0.0,disable-ticketing //远程显示
-vga qxl -global qxl-vga.vram_size=67108864 -device
AC97,id=sound0,bus=pci.0,addr=0x4  //显卡
-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x7 -readconfig
/etc/qemu/ich9-ehci-uhci.cfg -chardev
spicevmc,name=usbredir,id=usbredirchardev1
-device usb-redir,chardev=usbredirchardev1,id=usbredirdev1,bus=ehci.0,debug=3
-chardev spicevmc,name=usbredir,id=usbredirchardev2 //usb设备重定向
-device usb-redir,chardev=usbredirchardev2,id=usbredirdev2,bus=ehci.0,debug=3
-chardev spicevmc,name=usbredir,id=usbredirchardev3
-device
usb-redir,chardev=usbredirchardev3,id=usbredirdev3,bus=ehci.0,debug=3

虚拟机流程

1.硬件虚拟设备初始化
main-->kvm_init-->kvm_create_context-->kvm_create
r = kvm_create_vm(kvm);
r = kvm_arch_create(kvm, phys_mem_bytes, vm_mem);
r = kvm_create_default_phys_mem(kvm, phys_mem_bytes, vm_mem);
kvm_create_irqchip(kvm);
由上面代码可见,硬件虚拟技术为处理器,内存,中断

2.启动虚拟机
1>初始化所有设备,
module_call_init(MODULE_INIT_DEVICE);
块驱动,外围设备,机器初始化...
module.h:30:#define block_init(function) module_init(function,
MODULE_INIT_BLOCK)
module.h:31:#define device_init(function) module_init(function,
MODULE_INIT_DEVICE)
module.h:32:#define machine_init(function) module_init(function,
MODULE_INIT_MACHINE)

hw/sb16.c:1418:device_init (sb16_register)
module.h:31:#define device_init(function) module_init(function,
MODULE_INIT_DEVICE)

block/qcow2.c:1525:block_init(bdrv_qcow2_init);
module.h:30:#define block_init(function) module_init(function,
MODULE_INIT_BLOCK)

hw/pc.c:1787:machine_init(rhel_machine_init);
module.h:32:#define machine_init(function) module_init(function,
MODULE_INIT_MACHINE)

2>根据-M rhel6.3.0类型启动虚拟机。
main--> machine->init(ram_size, boot_devices,kernel_filename,
kernel_cmdline, initrd_filename, cpu_model);

1)创建vcpu
kvm_vm_ioctl(kvm_state, KVM_CREATE_VCPU, id);
初始化vcpu
kvm_arch_init_vcpu
2) 创建内存
    mmap_size = kvm_ioctl(kvm_state,
KVM_GET_VCPU_MMAP_SIZE, 0);
   env->kvm_run = mmap(NULL,
mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, env->kvm_fd,
3) 启动虚拟机,运行系统
kvm_run(env);

qemu-kvm虚拟机线程
1. qemu-kvm线程工作过程:
1)启动一个子线程,创建初始化vcpu,主线程等待
2)子线程创建初始化vcpu完毕,子线程等待,并等候通知主线程运行
3)主线程继续初始化虚拟机工作,初始化完成,通知子线程继续运行
4)子线程继续启动虚拟机kvm_run,主线程执行select交互处理

[[email protected] ~]$ ps aux |grep qemu
qemu     27033  1.3 12.9 3153132 502284 ?      Sl  
14:58   4:19
/usr/bin/qemu-system-x86_64 -machine accel=kvm -name RHEL6.5-20131111.0-Server
-S -machine pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp
1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7
-no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait
-mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew
-global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device
ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5
-device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1
-device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2
-device lsi,id=scsi0,bus=pci.0,addr=0x9 -device
virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive
file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2
-device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1
-drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device
ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive
file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough
-device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1
-netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device
virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3
-chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0
-chardev spicevmc,id=charchannel0,name=vdagent -device
virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0
-device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2
-device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device
hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device
virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8

http://blog.chinaunix.net/uid-26000137-id-3747844.html

2.一个虚拟机进程包含着几个线程
1)启动虚拟机
2)ps -eLf | grep qemu-kvm

[[email protected] ~]$ sudo ps -eLf | grep qemu
qemu     27033     1 27033 
1    5 14:58 ?        00:03:14 /usr/bin/qemu-system-x86_64
-machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine
pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp
1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7
-no-user-config -nodefaults -chardev
socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait
-mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew
-global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device
ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5
-device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1
-device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2
-device lsi,id=scsi0,bus=pci.0,addr=0x9 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6
-drive
file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2
-device
virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1
-drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device
ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive
file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough
-device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1
-netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device
virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3
-chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0
-chardev spicevmc,id=charchannel0,name=vdagent -device
virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0
-device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device
qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2
-device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device
hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device
virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8
qemu     27033     1 27090 
0    5 14:58 ?        00:00:34 /usr/bin/qemu-system-x86_64
-machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine
pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp
1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7
-no-user-config -nodefaults -chardev
socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait
-mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew
-global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7
-device
ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5
-device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1
-device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2
-device lsi,id=scsi0,bus=pci.0,addr=0x9 -device
virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive
file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2
-device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1
-drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device
ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive
file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough
-device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1
-netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device
virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3
-chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0
-chardev spicevmc,id=charchannel0,name=vdagent -device
virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0
-device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device
qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2
-device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device
hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device
virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8
qemu     27033     1 27092 
0    5 14:58 ?        00:00:27 /usr/bin/qemu-system-x86_64
-machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine
pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp
1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7
-no-user-config -nodefaults -chardev
socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait
-mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew
-global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device
ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device
ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5
-device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1
-device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2
-device lsi,id=scsi0,bus=pci.0,addr=0x9 -device
virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive
file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2
-device
virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1
-drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device
ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive
file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough
-device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1
-netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3
-chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0
-chardev spicevmc,id=charchannel0,name=vdagent -device
virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0
-device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device
qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2
-device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device
hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device
virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8
qemu     27033     1 27093 
0    5 14:58 ?        00:00:00 /usr/bin/qemu-system-x86_64
-machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine
pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp
1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7
-no-user-config -nodefaults -chardev
socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait
-mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew
-global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device
ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device
ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5
-device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1
-device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2
-device lsi,id=scsi0,bus=pci.0,addr=0x9 -device
virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive
file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2
-device
virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1
-drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device
ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive
file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough
-device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1
-netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device
virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3
-chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0
-chardev spicevmc,id=charchannel0,name=vdagent -device
virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0
-device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device
qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2
-device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device
hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device
virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8
qemu     27033     1 29367 
0    5 20:14 ?        00:00:00 /usr/bin/qemu-system-x86_64
-machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine
pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp
1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7
-no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait
-mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew
-global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device
ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device
ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5
-device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1
-device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2
-device lsi,id=scsi0,bus=pci.0,addr=0x9 -device
virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive
file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2
-device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1
-drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device
ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive
file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough
-device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1
-netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device
virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3
-chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0
-chardev spicevmc,id=charchannel0,name=vdagent -device
virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0
-device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2
-device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device
hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device
virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8
simon    29431 29188 29431  0    1 20:15
pts/2    00:00:00 grep --color=auto
qemu

[[email protected] ~]$ sudo gdb -p 27033

(gdb) info thread
  Id  
Target Id         Frame
  5   
Thread 0x7fb6929e4700 (LWP 27090) "qemu-system-x86"
0x00007fb6a0246067 in ioctl () from /lib64/libc.so.6
  4   
Thread 0x7fb68b422700 (LWP 27092) "qemu-system-x86"
0x00007fb6a02449dd in poll () from /lib64/libc.so.6
  3   
Thread 0x7fb68abff700 (LWP 27093) "qemu-system-x86"
0x00007fb6a81bad20 in [email protected]@GLIBC_2.3.2 () from
/lib64/libpthread.so.0
  2 
  Thread 0x7fb607fff700 (LWP
29444) "qemu-system-x86" 0x00007fb6a81bcec0 in sem_timedwait () from
/lib64/libpthread.so.0
* 1    Thread 0x7fb6a9f0fa80 (LWP 27033)
"qemu-system-x86" 0x00007fb6a02449dd in poll () from
/lib64/libc.so.6

整好5个

(gdb) bt
#0  0x00007fb6a02449dd in poll () from
/lib64/libc.so.6
#1  0x00007fb6aa155046 in main_loop_wait
()
#2  0x00007fb6aa0079a0 in main ()
这个线程是主线程

(gdb) thread 2
[Switching to thread 2 (Thread 0x7fb607fff700 (LWP 29444))]
#0  0x00007fb6a81bcec0 in sem_timedwait
() from /lib64/libpthread.so.0
(gdb) bt
#0  0x00007fb6a81bcec0 in sem_timedwait
() from /lib64/libpthread.so.0
#1  0x00007fb6aa2f6487 in
qemu_sem_timedwait ()
#2  0x00007fb6aa1a848c in worker_thread
()
#3  0x00007fb6a81b6f33 in start_thread ()
from /lib64/libpthread.so.0
#4  0x00007fb6a024eded in clone () from
/lib64/libc.so.6
(gdb) thread 3
[Switching to thread 3 (Thread 0x7fb68abff700 (LWP 27093))]
#0  0x00007fb6a81bad20 in
[email protected]@GLIBC_2.3.2 () from /lib64/libpthread.so.0
(gdb) bt
#0  0x00007fb6a81bad20 in
[email protected]@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x00007fb6aa2f62b9 in qemu_cond_wait
()
#2  0x00007fb6aa1c86a3 in
vnc_worker_thread_loop ()
#3  0x00007fb6aa1c8b30 in
vnc_worker_thread ()
#4  0x00007fb6a81b6f33 in start_thread ()
from /lib64/libpthread.so.0
#5  0x00007fb6a024eded in clone () from
/lib64/libc.so.6
异步进行i/o操作,主要针对磁盘映像操作(block
drive)
(gdb) thread 4
[Switching to thread 4 (Thread 0x7fb68b422700 (LWP 27092))]
#0  0x00007fb6a02449dd in poll () from
/lib64/libc.so.6
(gdb) bt
#0  0x00007fb6a02449dd in poll () from
/lib64/libc.so.6
#1  0x00007fb6a0f2e8f7 in red_worker_main
() from /lib64/libspice-server.so.1
#2  0x00007fb6a81b6f33 in start_thread ()
from /lib64/libpthread.so.0
#3  0x00007fb6a024eded in clone () from
/lib64/libc.so.6
(gdb) thread 5
[Switching to thread 5 (Thread 0x7fb6929e4700 (LWP 27090))]
#0  0x00007fb6a0246067 in ioctl () from
/lib64/libc.so.6
(gdb) bt
#0  0x00007fb6a0246067 in ioctl () from
/lib64/libc.so.6
#1  0x00007fb6aa230475 in kvm_vcpu_ioctl
()
#2  0x00007fb6aa230554 in kvm_cpu_exec
()
#3  0x00007fb6aa1d94b2 in
qemu_kvm_cpu_thread_fn ()
#4  0x00007fb6a81b6f33 in start_thread ()
from /lib64/libpthread.so.0
#5  0x00007fb6a024eded in clone () from
/lib64/libc.so.6
这个子线程,kvm_run启动和运行虚拟机

qemu-kvm虚拟机进程的字符设备处理(char drive)
1.查看采用哪些字符设备
[[email protected] ~]$ ps aux |grep qemu |grep "\-chardev"
-chardev
socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait
-mon chardev=charmonitor,id=monitor,mode=control
-chardev pty,id=charserial0 -device
isa-serial,chardev=charserial0,id=serial0
-chardev spicevmc,id=charchannel0,name=vdagent -device
virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0

时间: 2024-11-03 22:27:41

qemu-kvm 代码分析的相关文章

几篇QEMU/KVM代码分析文章

QEMU/KVM结合起来分析的几篇文章,代码跟最新的版本有些差异,但大体逻辑一样,写得通俗易懂.我把链接放这里主要是为自己需要查看时调转过去方便,感谢作者的付出! QEMU Source Code Study - 1 QEMU Source Code Study (2) - KVM_init QEMU Source Code Study (3) - KVM_CREATE_VCPU QEMU Source Code Study - KVM_RUN

KVM 介绍(7):使用 libvirt 做 QEMU/KVM 快照和 Nova 快照 (Nova Instances Snapshot Libvirt)

学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分配和 SR-IOV (5)libvirt 介绍 (6)Nova 通过 libvirt 管理 QEMU/KVM 虚机 (7)快照 本文将梳理 QEMU/KVM 快照相关的知识,以及在 OpenStack Nova 中使用 libvirt 来对 QEMU/KVM 虚机做快照的过程. 1. QEMU/KV

KVM(七)使用 libvirt 做 QEMU/KVM 快照和 Nova 实例的快照

本文将梳理 QEMU/KVM 快照相关的知识,以及在 OpenStack Nova 中使用 libvirt 来对 QEMU/KVM 虚机做快照的过程. 1. QEMU/KVM 快照 1.1 概念 QEMU/KVM 快照的定义: 磁盘快照:磁盘的内容(可能是虚机的全部磁盘或者部分磁盘)在某个时间点上被保存,然后可以被恢复. 磁盘数据的保存状态: 在一个运行着的系统上,一个磁盘快照很可能只是崩溃一致的(crash-consistent) 而不是完整一致(clean)的,也是说它所保存的磁盘状态可能相

qemu,kvm,qemu-kvm,xen,libvir 区别

KVM:(Kernel-based Virtual Machine)基于内核的虚拟机 KVM是集成到Linux内核的Hypervisor,是X86架构且硬件支持虚拟化技术(Intel VT或AMD-V)的Linux的全虚拟化解决方案.它是Linux的一个很小的模块,利用Linux做大量的事,如任务调度.内存管理与硬件设备交互等. 从存在形式看, KVM是两个内核模块kvm.ko和kvm_intel.ko(对AMD处理器来说,就是kvm_amd.ko),这两个模块用来实现CPU的虚拟化. 如果要让

qemu/kvm/qemu-kvm/virsh的区别

转自:http://www.2cto.com/os/201305/209596.html qemu是一套虚拟机管理系统,kqemu是qemu的加速器,可以认为是qemu的一个插件:qemu可以虚拟出不同架构的虚拟机,如在x86平台上可以虚拟出power机器: kvm是另外的一套虚拟机管理系统,包括内核虚拟构架和处理器相关模块,其借用了 quem 其它一些组件,kvm的非内核部分是由qemu实现的:加载了模块后,才能进一步通过其他工具创建虚拟机.但仅有 KVM 模块是远远不够的,因为用户无法直接控

qemu,kvm,qemu-kvm,xen,libvirt的区别

KVM:(Kernel-based Virtual Machine)基于内核的虚拟机 KVM是集成到Linux内核的Hypervisor,是X86架构且硬件支持虚拟化技术(Intel VT或AMD-V)的Linux的全虚拟化解决方案.它是Linux的一个很小的模块,利用Linux做大量的事,如任务调度.内存管理与硬件设备交互等. 从存在形式看,KVM是两个内核模块kvm.ko和kvm_intel.ko(对AMD处理器来说,就是kvm_amd.ko),这两个模块用来实现CPU的虚拟化. 如果要让用

理解 QEMU/KVM 和 Ceph(3):存储卷挂接和设备名称

本系列文章会总结 QEMU/KVM 和 Ceph 之间的整合: (1)QEMU-KVM 和 Ceph RBD 的 缓存机制总结 (2)QEMU 的 RBD 块驱动(block driver) (3)存储卷挂接和设备名称 这篇文章分析一下一个 Ceph RBD 卷是如何被映射到一个 QEMU/KVM 客户机的,以及客户机中设备的命名问题. 1. 遇到的设备命名问题 1.1 通过 Nova 和 Cinder 做 Ceph RDB 卷挂接和卸载步骤 挂接一个卷: #运行nova-attach 命令no

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

接着KVM虚拟机IO处理过程中Guest Vm IO处理过程(http://blog.csdn.net/dashulu/article/details/16820281),本篇文章主要描述IO从guest vm跳转到kvm和qemu后的处理过程. 首先回顾一下kvm的启动过程(http://blog.csdn.net/dashulu/article/details/17074675).qemu通过调用kvm提供的一系列接口来启动kvm. qemu的入口为vl.c中的main函数,main函数通过

KVM 介绍(8):使用 libvirt 迁移 QEMU/KVM 虚机和 Nova 虚机 [Nova Libvirt QEMU/KVM Live Migration]

学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分配和 SR-IOV (5)libvirt 介绍 (6)Nova 通过 libvirt 管理 QEMU/KVM 虚机 (7)快照 (snapshot) (8)迁移 (migration) 1. QEMU/KVM 迁移的概念 迁移(migration)包括系统整体的迁移和某个工作负载的迁移.系统整理迁移

KVM 介绍(6):Nova 通过 libvirt 管理 QEMU/KVM 虚机 [Nova Libvirt QEMU/KVM Domain]

学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分配和 SR-IOV (5)libvirt 介绍 (6)Nova 通过 libvirt 管理 QEMU/KVM 虚机 1. Libvirt 在 OpenStack 架构中的位置 在 Nova Compute 节点上运行的 nova-compute 服务调用 Hypervisor API 去管理运行在该