qemu: hot-plug of watchdog

https://bugzilla.redhat.com/show_bug.cgi?id=1447169

Since domain can have at most one watchdog it simplifies things a
bit. However, since we must be able to set the watchdog action as
well, new monitor command needs to be used.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
Michal Privoznik 2017-09-01 13:39:15 +02:00
parent 8a54cc1d08
commit 361c8dc179
14 changed files with 212 additions and 5 deletions

View File

@ -405,6 +405,17 @@ qemuAssignDeviceShmemAlias(virDomainDefPtr def,
}
int
qemuAssignDeviceWatchdogAlias(virDomainWatchdogDefPtr watchdog)
{
/* Currently, there's just one watchdog per domain */
if (VIR_STRDUP(watchdog->info.alias, "watchdog0") < 0)
return -1;
return 0;
}
int
qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr qemuCaps)
{
@ -482,7 +493,7 @@ qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr qemuCaps)
return -1;
}
if (def->watchdog) {
if (virAsprintf(&def->watchdog->info.alias, "watchdog%d", 0) < 0)
if (qemuAssignDeviceWatchdogAlias(def->watchdog) < 0)
return -1;
}
if (def->memballoon) {

View File

@ -65,6 +65,8 @@ int qemuAssignDeviceShmemAlias(virDomainDefPtr def,
virDomainShmemDefPtr shmem,
int idx);
int qemuAssignDeviceWatchdogAlias(virDomainWatchdogDefPtr watchdog);
int qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr qemuCaps);
int qemuDomainDeviceAliasIndex(const virDomainDeviceInfo *info,

View File

@ -4035,7 +4035,7 @@ qemuBuildHostNetStr(virDomainNetDefPtr net,
}
static char *
char *
qemuBuildWatchdogDevStr(const virDomainDef *def,
virDomainWatchdogDefPtr dev,
virQEMUCapsPtr qemuCaps)

View File

@ -205,6 +205,8 @@ char *qemuBuildShmemDevStr(virDomainDefPtr def,
virQEMUCapsPtr qemuCaps)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
char *qemuBuildWatchdogDevStr(const virDomainDef *def,
virDomainWatchdogDefPtr dev,
virQEMUCapsPtr qemuCaps);
#endif /* __QEMU_COMMAND_H__*/

View File

@ -7648,12 +7648,20 @@ qemuDomainAttachDeviceLive(virDomainObjPtr vm,
}
break;
case VIR_DOMAIN_DEVICE_WATCHDOG:
ret = qemuDomainAttachWatchdog(driver, vm,
dev->data.watchdog);
if (!ret) {
alias = dev->data.watchdog->info.alias;
dev->data.watchdog = NULL;
}
break;
case VIR_DOMAIN_DEVICE_NONE:
case VIR_DOMAIN_DEVICE_FS:
case VIR_DOMAIN_DEVICE_INPUT:
case VIR_DOMAIN_DEVICE_SOUND:
case VIR_DOMAIN_DEVICE_VIDEO:
case VIR_DOMAIN_DEVICE_WATCHDOG:
case VIR_DOMAIN_DEVICE_GRAPHICS:
case VIR_DOMAIN_DEVICE_HUB:
case VIR_DOMAIN_DEVICE_SMARTCARD:

View File

@ -2907,6 +2907,78 @@ qemuDomainAttachShmemDevice(virQEMUDriverPtr driver,
}
int
qemuDomainAttachWatchdog(virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainWatchdogDefPtr watchdog)
{
int ret = -1;
qemuDomainObjPrivatePtr priv = vm->privateData;
virDomainDeviceDef dev = { VIR_DOMAIN_DEVICE_WATCHDOG, { .watchdog = watchdog } };
virDomainWatchdogAction actualAction = watchdog->action;
const char *actionStr = NULL;
char *watchdogstr = NULL;
bool releaseAddress = false;
int rv;
if (vm->def->watchdog) {
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
_("domain already has a watchdog"));
return -1;
}
if (qemuAssignDeviceWatchdogAlias(watchdog) < 0)
return -1;
if (!(watchdogstr = qemuBuildWatchdogDevStr(vm->def, watchdog, priv->qemuCaps)))
return -1;
if (watchdog->model == VIR_DOMAIN_WATCHDOG_MODEL_I6300ESB) {
if (qemuDomainEnsurePCIAddress(vm, &dev, driver) < 0)
goto cleanup;
releaseAddress = true;
} else {
virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
_("hotplug of watchdog of model %s is not supported"),
virDomainWatchdogModelTypeToString(watchdog->model));
goto cleanup;
}
/* QEMU doesn't have a 'dump' action; we tell qemu to 'pause', then
libvirt listens for the watchdog event, and we perform the dump
ourselves. so convert 'dump' to 'pause' for the qemu cli */
if (actualAction == VIR_DOMAIN_WATCHDOG_ACTION_DUMP)
actualAction = VIR_DOMAIN_WATCHDOG_ACTION_PAUSE;
actionStr = virDomainWatchdogActionTypeToString(actualAction);
qemuDomainObjEnterMonitor(driver, vm);
rv = qemuMonitorSetWatchdogAction(priv->mon, actionStr);
if (rv >= 0)
rv = qemuMonitorAddDevice(priv->mon, watchdogstr);
if (qemuDomainObjExitMonitor(driver, vm) < 0) {
releaseAddress = false;
goto cleanup;
}
if (rv < 0)
goto cleanup;
releaseAddress = false;
vm->def->watchdog = watchdog;
ret = 0;
cleanup:
if (releaseAddress)
qemuDomainReleaseDeviceAddress(vm, &watchdog->info, NULL);
VIR_FREE(watchdogstr);
return ret;
}
static int
qemuDomainChangeNetBridge(virDomainObjPtr vm,
virDomainNetDefPtr olddev,

View File

@ -80,6 +80,9 @@ int qemuDomainAttachHostDevice(virConnectPtr conn,
int qemuDomainAttachShmemDevice(virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainShmemDefPtr shmem);
int qemuDomainAttachWatchdog(virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainWatchdogDefPtr watchdog);
int qemuDomainFindGraphicsIndex(virDomainDefPtr def,
virDomainGraphicsDefPtr dev);
int qemuDomainAttachMemory(virQEMUDriverPtr driver,

View File

@ -4339,3 +4339,15 @@ qemuMonitorEventPanicInfoFree(qemuMonitorEventPanicInfoPtr info)
VIR_FREE(info);
}
int
qemuMonitorSetWatchdogAction(qemuMonitorPtr mon,
const char *action)
{
VIR_DEBUG("watchdogAction=%s", action);
QEMU_CHECK_MONITOR_JSON(mon);
return qemuMonitorJSONSetWatchdogAction(mon, action);
}

View File

@ -1129,4 +1129,6 @@ int qemuMonitorSetBlockThreshold(qemuMonitorPtr mon,
virJSONValuePtr qemuMonitorQueryNamedBlockNodes(qemuMonitorPtr mon);
int qemuMonitorSetWatchdogAction(qemuMonitorPtr mon,
const char *action);
#endif /* QEMU_MONITOR_H */

View File

@ -7695,3 +7695,31 @@ qemuMonitorJSONQueryNamedBlockNodes(qemuMonitorPtr mon)
return ret;
}
int
qemuMonitorJSONSetWatchdogAction(qemuMonitorPtr mon,
const char *action)
{
virJSONValuePtr cmd;
virJSONValuePtr reply = NULL;
int ret = -1;
if (!(cmd = qemuMonitorJSONMakeCommand("watchdog-set-action",
"s:action", action,
NULL)))
return -1;
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
goto cleanup;
if (qemuMonitorJSONCheckError(cmd, reply) < 0)
goto cleanup;
ret = 0;
cleanup:
virJSONValueFree(cmd);
virJSONValueFree(reply);
return ret;
}

View File

@ -521,4 +521,7 @@ int qemuMonitorJSONSetBlockThreshold(qemuMonitorPtr mon,
virJSONValuePtr qemuMonitorJSONQueryNamedBlockNodes(qemuMonitorPtr mon)
ATTRIBUTE_NONNULL(1);
int qemuMonitorJSONSetWatchdogAction(qemuMonitorPtr mon,
const char *action)
ATTRIBUTE_NONNULL(1);
#endif /* QEMU_MONITOR_JSON_H */

View File

@ -127,6 +127,9 @@ testQemuHotplugAttach(virDomainObjPtr vm,
case VIR_DOMAIN_DEVICE_SHMEM:
ret = qemuDomainAttachShmemDevice(&driver, vm, dev->data.shmem);
break;
case VIR_DOMAIN_DEVICE_WATCHDOG:
ret = qemuDomainAttachWatchdog(&driver, vm, dev->data.watchdog);
break;
default:
VIR_TEST_VERBOSE("device type '%s' cannot be attached\n",
virDomainDeviceTypeToString(dev->type));
@ -811,10 +814,14 @@ mymain(void)
"device_del", QMP_OK,
"object-del", QMP_OK);
DO_TEST_ATTACH("base-live+disk-scsi-wwn",
"disk-scsi-duplicate-wwn", false, true,
"disk-scsi-duplicate-wwn", false, false,
"human-monitor-command", HMP("OK\\r\\n"),
"device_add", QMP_OK);
DO_TEST_ATTACH("base-live", "watchdog", false, false,
"watchdog-set-action", QMP_OK,
"device_add", QMP_OK);
#define DO_TEST_CPU_GROUP(prefix, vcpus, modernhp, expectfail) \
do { \
cpudata.test = prefix; \

View File

@ -0,0 +1 @@
<watchdog model='i6300esb' action='poweroff'/>

View File

@ -0,0 +1,56 @@
<domain type='kvm' id='7'>
<name>hotplug</name>
<uuid>d091ea82-29e6-2e34-3005-f02617b36e87</uuid>
<memory unit='KiB'>4194304</memory>
<currentMemory unit='KiB'>4194304</currentMemory>
<vcpu placement='static'>4</vcpu>
<os>
<type arch='x86_64' machine='pc'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<controller type='usb' index='0'>
<alias name='usb'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='ide' index='0'>
<alias name='ide'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='scsi' index='0' model='virtio-scsi'>
<alias name='scsi0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</controller>
<controller type='pci' index='0' model='pci-root'>
<alias name='pci'/>
</controller>
<controller type='virtio-serial' index='0'>
<alias name='virtio-serial0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</controller>
<input type='mouse' bus='ps2'>
<alias name='input0'/>
</input>
<input type='keyboard' bus='ps2'>
<alias name='input1'/>
</input>
<watchdog model='i6300esb' action='poweroff'>
<alias name='watchdog0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</watchdog>
<memballoon model='none'>
<alias name='balloon0'/>
</memballoon>
</devices>
<seclabel type='none' model='none'/>
</domain>