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

 

輸出結果:



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章