libxl supports sysrq. Add .domainSendKey function to support
sending sysrq key.
Signed-off-by: Chunyan Liu <cyliu(a)suse.com>
---
src/libxl/libxl_driver.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 89 insertions(+)
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 53c87ce..6cc5fe6 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -56,6 +56,7 @@
#include "viratomic.h"
#include "virhostdev.h"
#include "network/bridge_driver.h"
+#include "virkeycode.h"
#define VIR_FROM_THIS VIR_FROM_LIBXL
@@ -4729,6 +4730,93 @@ libxlDomainMigrateConfirm3Params(virDomainPtr domain,
return libxlDomainMigrationConfirm(driver, vm, flags, cancelled);
}
+typedef struct keyname_to_letter {
+ const char *keyname;
+ const char *letter;
+} keyname_to_letter;
+
+static const keyname_to_letter sysrq_keymap[] = {
+ {"KEY_0", "0"}, {"KEY_1", "1"},
{"KEY_2", "2"}, {"KEY_3", "3"},
+ {"KEY_4", "4"}, {"KEY_5", "5"},
{"KEY_6", "6"}, {"KEY_7", "7"},
+ {"KEY_8", "8"}, {"KEY_9", "9"},
{"KEY_A", "a"}, {"KEY_B", "b"},
+ {"KEY_C", "c"}, {"KEY_D", "d"},
{"KEY_E", "e"}, {"KEY_F", "f"},
+ {"KEY_G", "g"}, {"KEY_H", "h"},
{"KEY_I", "i"}, {"KEY_J", "j"},
+ {"KEY_K", "k"}, {"KEY_L", "l"},
{"KEY_M", "m"}, {"KEY_N", "n"},
+ {"KEY_O", "o"}, {"KEY_P", "p"},
{"KEY_Q", "q"}, {"KEY_R", "r"},
+ {"KEY_S", "s"}, {"KEY_T", "t"},
{"KEY_U", "u"}, {"KEY_V", "v"},
+ {"KEY_W", "w"}, {"KEY_X", "x"},
{"KEY_Y", "y"}, {"KEY_Z", "z"},
+ {NULL, NULL}
+};
+
+static int
+libxlDomainSendKey(virDomainPtr dom,
+ unsigned int codeset,
+ unsigned int holdtime ATTRIBUTE_UNUSED,
+ unsigned int *keycodes,
+ int nkeycodes,
+ unsigned int flags)
+{
+ virDomainObjPtr vm;
+ libxlDomainObjPrivatePtr priv;
+ const char *keyname0, *keyname1;
+ int ret = -1;
+
+ virCheckFlags(0, -1);
+
+ if (!(vm = libxlDomObjFromDomain(dom)))
+ goto cleanup;
+
+ priv = vm->privateData;
+
+ if (virDomainSendKeyEnsureACL(dom->conn, vm->def) < 0)
+ goto cleanup;
+
+ /* check key, only support sending magic sysrq. Like:
+ * #virsh send-key guest1 KEY_LEFTALT KEY_SYSRQ KEY_H
+ */
+ if (nkeycodes < 3)
+ goto non_sysrq;
+
+ keyname0 = virKeynameFromKeycode(codeset, keycodes[0]);
+ keyname1 = virKeynameFromKeycode(codeset, keycodes[1]);
+ if (!keyname0 || !keyname1)
+ goto non_sysrq;
+
+ if ((STREQ(keyname0, "KEY_LEFTALT") && STREQ(keyname1,
"KEY_SYSRQ")) ||
+ (STREQ(keyname1, "KEY_LEFTALT") && STREQ(keyname0,
"KEY_SYSRQ"))) {
+ const keyname_to_letter *item = sysrq_keymap;
+ char *key = NULL;
+ const char *keyname = virKeynameFromKeycode(codeset, keycodes[2]);
+
+ while (item && item->keyname) {
+ if (keyname && STREQ(item->keyname, keyname)) {
+ if (VIR_STRDUP(key, item->letter) < 0)
+ goto cleanup;
+ break;
+ }
+ item++;
+ }
+
+ if (!key)
+ goto non_sysrq;
+
+ ret = libxl_send_sysrq(priv->ctx, vm->def->id, key[0]);
+ VIR_FREE(key);
+ goto cleanup;
+
+ } else {
+ goto non_sysrq;
+ }
+
+ non_sysrq:
+ virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
+ "%s", _("Sending non-sysrq keys is not
supported"));
+
+ cleanup:
+ if (vm)
+ virObjectUnlock(vm);
+ return ret;
+}
static virHypervisorDriver libxlDriver = {
.no = VIR_DRV_LIBXL,
@@ -4824,6 +4912,7 @@ static virHypervisorDriver libxlDriver = {
.domainMigratePerform3Params = libxlDomainMigratePerform3Params, /* 1.2.6 */
.domainMigrateFinish3Params = libxlDomainMigrateFinish3Params, /* 1.2.6 */
.domainMigrateConfirm3Params = libxlDomainMigrateConfirm3Params, /* 1.2.6 */
+ .domainSendKey = libxlDomainSendKey, /* 1.2.11 */
};
static virStateDriver libxlStateDriver = {
--
1.8.4.5