2009-09-22 17:48:40 +00:00
|
|
|
/*
|
|
|
|
* qemu_monitor_text.c: interaction with QEMU monitor console
|
|
|
|
*
|
2014-03-18 08:15:21 +00:00
|
|
|
* Copyright (C) 2006-2014 Red Hat, Inc.
|
2009-09-22 17:48:40 +00:00
|
|
|
* Copyright (C) 2006 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2009-09-22 17:48:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "qemu_monitor_text.h"
|
2019-09-19 15:56:18 +00:00
|
|
|
#include "qemu_monitor_json.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2009-09-22 17:48:40 +00:00
|
|
|
|
2018-05-22 12:15:36 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_QEMU
|
2010-04-15 13:52:03 +00:00
|
|
|
|
2018-05-22 12:15:36 +00:00
|
|
|
VIR_LOG_INIT("qemu.qemu_monitor_text");
|
2009-12-14 09:50:01 +00:00
|
|
|
|
2010-01-26 15:34:46 +00:00
|
|
|
int qemuMonitorTextAddDrive(qemuMonitorPtr mon,
|
|
|
|
const char *drivestr)
|
|
|
|
{
|
|
|
|
char *cmd = NULL;
|
|
|
|
char *reply = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
/* 'dummy' here is just a placeholder since there is no PCI
|
|
|
|
* address required when attaching drives to a controller */
|
2019-10-22 13:26:14 +00:00
|
|
|
cmd = g_strdup_printf("drive_add dummy %s", drivestr);
|
2010-01-26 15:34:46 +00:00
|
|
|
|
2019-09-19 15:56:18 +00:00
|
|
|
if (qemuMonitorJSONHumanCommand(mon, cmd, &reply) < 0)
|
2010-01-26 15:34:46 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2010-11-01 17:09:31 +00:00
|
|
|
if (strstr(reply, "unknown command:")) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
|
|
|
|
_("drive hotplug is not supported"));
|
2010-11-01 17:09:31 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2011-02-17 03:32:16 +00:00
|
|
|
if (strstr(reply, "could not open disk image")) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
|
|
|
|
_("open disk image file failed"));
|
2011-02-17 03:32:16 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-01-17 16:14:11 +00:00
|
|
|
if (strstr(reply, "Could not open")) {
|
|
|
|
size_t len = strlen(reply);
|
|
|
|
if (reply[len - 1] == '\n')
|
|
|
|
reply[len - 1] = '\0';
|
|
|
|
|
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
|
|
|
|
reply);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-06-15 13:46:32 +00:00
|
|
|
if (strstr(reply, "Image is not in")) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
|
|
|
|
_("Incorrect disk format"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-01-26 15:34:46 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2010-01-26 15:34:46 +00:00
|
|
|
VIR_FREE(cmd);
|
|
|
|
VIR_FREE(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-02-11 14:28:16 +00:00
|
|
|
|
2016-03-11 15:33:03 +00:00
|
|
|
|
2010-12-08 21:30:12 +00:00
|
|
|
int qemuMonitorTextDriveDel(qemuMonitorPtr mon,
|
|
|
|
const char *drivestr)
|
2010-10-22 14:14:22 +00:00
|
|
|
{
|
|
|
|
char *cmd = NULL;
|
|
|
|
char *reply = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
cmd = g_strdup_printf("drive_del %s", drivestr);
|
2010-10-22 14:14:22 +00:00
|
|
|
|
2019-09-19 15:56:18 +00:00
|
|
|
if (qemuMonitorJSONHumanCommand(mon, cmd, &reply) < 0)
|
2010-10-22 14:14:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (strstr(reply, "unknown command:")) {
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_ERROR(_("deleting drive is not supported. "
|
2010-10-22 14:14:22 +00:00
|
|
|
"This may leak data if disk is reassigned"));
|
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
2010-12-08 21:30:12 +00:00
|
|
|
|
|
|
|
/* (qemu) drive_del wark
|
|
|
|
* Device 'wark' not found */
|
2015-08-10 15:37:30 +00:00
|
|
|
} else if (strstr(reply, "Device '") && strstr(reply, "not found")) {
|
2010-12-08 21:30:12 +00:00
|
|
|
/* NB: device not found errors mean the drive was auto-deleted and we
|
|
|
|
* ignore the error */
|
2010-10-22 14:14:22 +00:00
|
|
|
} else if (STRNEQ(reply, "")) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("deleting %s drive failed: %s"), drivestr, reply);
|
2010-10-22 14:14:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2010-10-22 14:14:22 +00:00
|
|
|
VIR_FREE(cmd);
|
|
|
|
VIR_FREE(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-02-11 14:28:16 +00:00
|
|
|
|
2016-01-15 11:53:30 +00:00
|
|
|
int
|
|
|
|
qemuMonitorTextCreateSnapshot(qemuMonitorPtr mon,
|
|
|
|
const char *name)
|
2010-04-02 14:10:37 +00:00
|
|
|
{
|
2011-03-09 20:47:06 +00:00
|
|
|
char *cmd = NULL;
|
2010-04-02 14:10:37 +00:00
|
|
|
char *reply = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
cmd = g_strdup_printf("savevm \"%s\"", name);
|
2010-04-02 14:10:37 +00:00
|
|
|
|
2019-09-19 15:56:18 +00:00
|
|
|
if (qemuMonitorJSONHumanCommand(mon, cmd, &reply))
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2016-01-15 11:53:30 +00:00
|
|
|
if (strstr(reply, "Error while creating snapshot") ||
|
|
|
|
strstr(reply, "Could not open VM state file") ||
|
2016-01-15 12:01:30 +00:00
|
|
|
strstr(reply, "State blocked by non-migratable device") ||
|
2019-04-10 17:56:54 +00:00
|
|
|
strstr(reply, "Error: ") ||
|
2016-01-15 11:53:30 +00:00
|
|
|
(strstr(reply, "Error") && strstr(reply, "while writing VM"))) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("Failed to take snapshot: %s"), reply);
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
2016-01-15 11:53:30 +00:00
|
|
|
} else if (strstr(reply, "No block device can accept snapshots")) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
|
|
|
_("this domain does not have a device to take snapshots"));
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2010-04-02 14:10:37 +00:00
|
|
|
VIR_FREE(cmd);
|
|
|
|
VIR_FREE(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemuMonitorTextLoadSnapshot(qemuMonitorPtr mon, const char *name)
|
|
|
|
{
|
2011-03-09 20:47:06 +00:00
|
|
|
char *cmd = NULL;
|
2010-04-02 14:10:37 +00:00
|
|
|
char *reply = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
cmd = g_strdup_printf("loadvm \"%s\"", name);
|
2010-04-02 14:10:37 +00:00
|
|
|
|
2019-09-19 15:56:18 +00:00
|
|
|
if (qemuMonitorJSONHumanCommand(mon, cmd, &reply))
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2019-04-10 17:52:29 +00:00
|
|
|
if (strstr(reply, "No block device supports snapshots")) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
|
|
|
_("this domain does not have a device to load snapshots"));
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
2019-04-10 17:52:29 +00:00
|
|
|
} else if (strstr(reply, "Could not find snapshot")) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("the snapshot '%s' does not exist, and was not loaded"),
|
|
|
|
name);
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
2019-04-10 17:52:29 +00:00
|
|
|
} else if (strstr(reply, "Snapshots not supported on device")) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("Failed to load snapshot: %s"), reply);
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
2019-04-10 17:52:29 +00:00
|
|
|
} else if (strstr(reply, "Could not open VM state file") ||
|
2019-04-10 17:56:54 +00:00
|
|
|
strstr(reply, "Error: ") ||
|
2019-04-10 17:52:29 +00:00
|
|
|
(strstr(reply, "Error") &&
|
|
|
|
(strstr(reply, "while loading VM state") ||
|
|
|
|
strstr(reply, "while activating snapshot on")))) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("Failed to load snapshot: %s"), reply);
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2010-04-02 14:10:37 +00:00
|
|
|
VIR_FREE(cmd);
|
|
|
|
VIR_FREE(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemuMonitorTextDeleteSnapshot(qemuMonitorPtr mon, const char *name)
|
|
|
|
{
|
2011-03-09 20:47:06 +00:00
|
|
|
char *cmd = NULL;
|
2010-04-02 14:10:37 +00:00
|
|
|
char *reply = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
cmd = g_strdup_printf("delvm \"%s\"", name);
|
2019-09-19 15:56:18 +00:00
|
|
|
if (qemuMonitorJSONHumanCommand(mon, cmd, &reply))
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2019-04-10 17:43:39 +00:00
|
|
|
if (strstr(reply, "No block device supports snapshots")) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
|
|
|
_("this domain does not have a device to delete snapshots"));
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
2019-04-10 17:43:39 +00:00
|
|
|
} else if (strstr(reply, "Snapshots not supported on device")) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s", reply);
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
2019-04-10 17:56:54 +00:00
|
|
|
} else if (strstr(reply, "Error: ") ||
|
|
|
|
(strstr(reply, "Error") &&
|
|
|
|
strstr(reply, "while deleting snapshot"))) {
|
2019-04-10 17:43:39 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("Failed to delete snapshot: %s"), reply);
|
2010-04-02 14:10:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2010-04-02 14:10:37 +00:00
|
|
|
VIR_FREE(cmd);
|
|
|
|
VIR_FREE(reply);
|
|
|
|
return ret;
|
|
|
|
}
|