qemu中添加新命令helloworld

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

qemu中添加新命令helloworld

介绍:

 

很多 虚拟化开发者和系统架构师 需要在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

[javascript]  view plain copy
  1. {  
  2.     .name       = "hello",  
  3.     .args_type  = "",  
  4.     .params     = "",  
  5.     .help       = "",  
  6.     .mhandler.cmd = myhello,  
  7. },  

 

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

[cpp]  view plain copy
  1.    
[cpp]  view plain copy
  1. void myhello(Monitor *mon, const QDict *qdict);  
  2. <span lang="zh-CN" style="font-family:SimSun;"></span>  
[cpp]  view plain copy
  1. <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>  
[cpp]  view plain copy
  1. void myhello(Monitor *mon, const QDict *qdict){  
  2.     printf("hello, david wang!\n");  
  3. }  

 

编译:

 

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

make clean

make -j 16

make  install 

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

 

 

输出结果:


 

原理简单介绍:

 

monitor命令结构mon_cmd_t定义

[cpp]  view plain copy
  1. static const mon_cmd_t hmp_cmds[] = {  
  2. #include "hmp-commands.h"  
  3.     { /* NULL */ },  
  4.   
  5. };  

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

[ruby]  view plain copy
  1. 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

[html]  view plain copy
  1. {  
  2.     .name       = "helloworld",  
  3.     .args_type  = "",  
  4.     .mhandler.cmd_new = qmp_marshal_input_helloworld,  
  5. },  

 

qapi-schema.json

[html]  view plain copy
  1. { 'command': 'helloworld' }  


 

migration.c

[cpp]  view plain copy
  1. void qmp_helloworld(Error **errp)  
  2. {         
  3.     printf("oba gangnam style\n");  
  4. }     

 

输出结果:




参考来源: http://blog.csdn.net/w497629433/article/details/8271140
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值