This is the seventh post in the series “HOWTO: Add libvirt support for a qemu command”
Phew, we are getting near the end! In contrast to the remote support, this part will seem easy. We have only one file to change. In that file, we define the command and some metadata. Then we provide an API wrapper function that parses virsh command line arguments, makes the API call, and outputs the result to the user. Find the patch here.
diff --git a/tools/virsh.c b/tools/virsh.c index 50ca50f..254a2bd 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -3987,6 +3987,59 @@ done: } /* + * "hello" command + */ +static const vshCmdInfo info_domhello[] = { + {"help", N_("say hello to a domain")}, + {"desc", N_("Qemu demonstration command -- does nothing useful .")}, + {NULL, NULL} +}; + +static const vshCmdOptDef opts_domhello[] = { + {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("domain name, id or uuid")}, + {"exclaim", VSH_OT_BOOL, 0, N_("Get a more excited greeting") }, + {"lang", VSH_OT_DATA, VSH_OFLAG_NONE, N_("Select desired language (english or spanish)")}, + { NULL, 0, 0, NULL }, +}; +
First create two structures that define the virsh command. info_domhello
defines some help text that will get displayed by the help
command. opts_domhello
defines the virsh options for our command. Each entry sets: the option name, the type, is whether the parameter is optional, and the help text.
+static int +cmdDomHello(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom; + const char *name; + struct _virHelloParams params; + int exclaim = 0; + const char *lang; + char *response; + + if (!vshConnectionUsability(ctl, ctl->conn)) + return FALSE; + + if (!(dom = vshCommandOptDomain(ctl, cmd, &name))) + return FALSE; + + if (vshCommandOptBool(cmd, "exclaim")) + exclaim = 1; + if (vshCommandOptString(cmd, "lang", &lang) < 0) + lang = NULL; + + params.lang = (char *) lang; + params.exclaim = exclaim; + response = virDomainHello(dom, ¶ms); + + if (response) { + vshPrint(ctl, "%s", response); + VIR_FREE(response); + } else { + virDomainFree(dom); + return FALSE; + } + + virDomainFree(dom); + return TRUE; +} + +/* * "net-autostart" command */ static const vshCmdInfo info_network_autostart[] = { @@ -10504,6 +10557,7 @@ static const vshCmdDef domMonitoringCmds[] = { {"dommemstat", cmdDomMemStat, opts_dommemstat, info_dommemstat}, {"domstate", cmdDomstate, opts_domstate, info_domstate}, {"list", cmdList, opts_list, info_list}, + {"domhello", cmdDomHello, opts_domhello, info_domhello}, {NULL, NULL, NULL, NULL} };
By now, for structure of this function should be looking very familiar. It starts by testing the connection and domain for validity. Next, the exclaim
and lang
arguments are extracted if present and the params
structure is built. Next, the API call is made. Either the response or error message is displayed and cleanup is done.
Up next… Enable python bindings for hello — The last one!