qemu中添加 新命令 helloworld

来源:互联网 发布:www.web pro seo.com 编辑:程序博客网 时间:2024/05/02 02:45

介绍:

 

很多虚拟化开发者和系统架构师需要在qemu中添加新的功能,比如 post-copy migrate(后迁移模式)。处于兼容性和稳定性的考虑,添加新命令来封装这些功能比较合适。

本文介绍了在qemu代码里如何添加新命令 helloworld

 

注意:

 

本文讲的是添加qemu交互界面(monitor)中的命令,并不是讲如何添加qemu参数,添加参数可参考c语言的argcargv的做法

源文件版本:qemu-kvm-1.2.0

 

添加命令:

 

在  qemu-kvm-1.2.0/hmp-commands.hx 中添加命令hello,指向函数myhello

    {        .name       = "hello",        .args_type  = "",        .params     = "",        .help       = "",        .mhandler.cmd = myhello,    },

 

在qemu-kvm-1.2.0/hmp.h  中 添加myhello 声明

 
void myhello(Monitor *mon, const QDict *qdict);
 qemu-kvm-1.2.0/hmp.c 中添加 myhello 实现
void myhello(Monitor *mon, const QDict *qdict){    printf("hello, david wang!\n");}

 

编译:

 

./configure(第一次编译前先执行,以后可以不用执行了)

make clean

make -j 16

make  install 

编译后生成的可执行程序在 x86_64-softmmu下,make  install后放在/usr/local/bin/qemu-system-x86_64

 

 

输出结果:


 

原理简单介绍:

 

monitor命令结构mon_cmd_t定义

static const mon_cmd_t hmp_cmds[] = {#include "hmp-commands.h"    { /* NULL */ },};

所需要的hpm-commands.hhxtool工具利用 hmp-commands.hx生成。

sh /root/qemu-kvm-1.2.0/scripts/hxtool -h < /root/qemu-kvm-1.2.0/hmp-commands.hx > hmp-commands.h 

 

HMPQMP

如果希望和qemu中其他命令  migrate等一样同时走hmp和qmp方式,则还需要修改 qmp-commands.hx,qapi-schema.json  以及一处函数实现的文件位置,比如 migration.c.

qmp-commands.hx

    {        .name       = "helloworld",        .args_type  = "",        .mhandler.cmd_new = qmp_marshal_input_helloworld,    },

 

qapi-schema.json

{ 'command': 'helloworld' }


 

migration.c

void qmp_helloworld(Error **errp){           printf("oba gangnam style\n");}   

 

输出结果: