mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-23 13:05:27 +00:00
Add API for using 'usb_add' for disk devices
* src/qemu/qemu_monitor.c, src/qemu/qemu_monitor.h: Add new qemuMonitorAddUSBDisk() API * src/qemu/qemu_driver.c: Switch USB disk hotplug to the new src/qemu/qemu_driver.c API.
This commit is contained in:
parent
ed2a10a1c5
commit
f566c5924a
@ -4698,59 +4698,32 @@ static int qemudDomainAttachUsbMassstorageDevice(virConnectPtr conn,
|
||||
virDomainObjPtr vm,
|
||||
virDomainDeviceDefPtr dev)
|
||||
{
|
||||
int ret, i;
|
||||
char *safe_path;
|
||||
char *cmd, *reply;
|
||||
int i;
|
||||
|
||||
for (i = 0 ; i < vm->def->ndisks ; i++) {
|
||||
if (STREQ(vm->def->disks[i]->dst, dev->data.disk->dst)) {
|
||||
qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
|
||||
qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
|
||||
_("target %s already exists"), dev->data.disk->dst);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!dev->data.disk->src) {
|
||||
qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
|
||||
"%s", _("disk source path is missing"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
|
||||
virReportOOMError(conn);
|
||||
return -1;
|
||||
}
|
||||
|
||||
safe_path = qemudEscapeMonitorArg(dev->data.disk->src);
|
||||
if (!safe_path) {
|
||||
virReportOOMError(conn);
|
||||
if (qemuMonitorAddUSBDisk(vm, dev->data.disk->src) < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = virAsprintf(&cmd, "usb_add disk:%s", safe_path);
|
||||
VIR_FREE(safe_path);
|
||||
if (ret == -1) {
|
||||
virReportOOMError(conn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
|
||||
qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
|
||||
"%s", _("cannot attach usb disk"));
|
||||
VIR_FREE(cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
DEBUG ("%s: attach_usb reply: %s",vm->def->name, reply);
|
||||
/* If the command failed qemu prints:
|
||||
* Could not add ... */
|
||||
if (strstr(reply, "Could not add ")) {
|
||||
qemudReportError (conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
|
||||
"%s",
|
||||
_("adding usb disk failed"));
|
||||
VIR_FREE(reply);
|
||||
VIR_FREE(cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
virDomainDiskInsertPreAlloced(vm->def, dev->data.disk);
|
||||
|
||||
VIR_FREE(reply);
|
||||
VIR_FREE(cmd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1199,3 +1199,46 @@ cleanup:
|
||||
VIR_FREE(dest);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int qemuMonitorAddUSBDisk(const virDomainObjPtr vm,
|
||||
const char *path)
|
||||
{
|
||||
char *cmd = NULL;
|
||||
char *safepath;
|
||||
int ret = -1;
|
||||
char *info = NULL;
|
||||
|
||||
safepath = qemudEscapeMonitorArg(path);
|
||||
if (!safepath) {
|
||||
virReportOOMError(NULL);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (virAsprintf(&cmd, "usb_add disk:%s", safepath) < 0) {
|
||||
virReportOOMError(NULL);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (qemudMonitorCommand(vm, cmd, &info) < 0) {
|
||||
qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
|
||||
"%s", _("cannot run monitor command to add usb disk"));
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
DEBUG ("%s: usb_add reply: %s", vm->def->name, info);
|
||||
/* If the command failed qemu prints:
|
||||
* Could not add ... */
|
||||
if (strstr(info, "Could not add ")) {
|
||||
qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED,
|
||||
_("unable to add USB disk %s: %s"), path, info);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
cleanup:
|
||||
VIR_FREE(cmd);
|
||||
VIR_FREE(safepath);
|
||||
return ret;
|
||||
}
|
||||
|
@ -136,4 +136,8 @@ int qemuMonitorMigrateToCommand(const virDomainObjPtr vm,
|
||||
const char * const *argv,
|
||||
const char *target);
|
||||
|
||||
|
||||
int qemuMonitorAddUSBDisk(const virDomainObjPtr vm,
|
||||
const char *path);
|
||||
|
||||
#endif /* QEMU_MONITOR_TEXT_H */
|
||||
|
Loading…
x
Reference in New Issue
Block a user