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

    {
        .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");
}   

 

输出结果:



 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值