介绍:
很多 虚拟化开发者和系统架构师 需要在qemu 中添加新的功能,比如 post-copy migrate(后迁移模式)。处于兼容性和稳定性的考虑,添加新命令来封装这些功能比较合适。
本文介绍了在qemu代码里如何添加新命令 helloworld。
注意:
本文讲的是添加qemu交互界面(monitor)中的命令,并不是讲如何添加qemu参数,添加参数可参考c语言的argc,argv的做法
源文件版本: 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);
- <span lang="zh-CN" style="font-family:SimSun;"></span>
- <span lang="zh-CN" style="font-family:SimSun;">在</span><span lang="en-US" style="font-family:'Segoe UI';"> qemu-kvm-1.2.0/hmp.c </span><span lang="zh-CN" style="font-family:SimSun;">中添加</span><span lang="en-US" style="font-family:'Segoe UI';"> myhello </span><span lang="zh-CN" style="font-family:SimSun;">实现</span>
- 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.h由hxtool工具 利用 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
HMP和QMP:
如果希望和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");
- }
输出结果:

本文详细介绍了在qemu虚拟化环境中,如何为交互界面添加新的命令'helloworld'。通过修改qemu-kvm-1.2.0源代码,包括在hmp-commands.hx中定义命令,hmp.h中声明函数,hmp.c中实现函数,然后进行编译和安装。文章还简要提及了hmp和qmp命令的区别以及如何为qmp添加新命令。

被折叠的 条评论
为什么被折叠?



