2010-03-26 14:49:45 +00:00
|
|
|
/*
|
2012-12-12 17:00:34 +00:00
|
|
|
* virhook.c: implementation of the synchronous hooks support
|
2010-03-26 14:49:45 +00:00
|
|
|
*
|
2014-03-18 08:14:35 +00:00
|
|
|
* Copyright (C) 2010-2014 Red Hat, Inc.
|
2010-03-26 14:49:45 +00:00
|
|
|
* Copyright (C) 2010 Daniel Veillard
|
|
|
|
*
|
|
|
|
* 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/>.
|
2010-03-26 14:49:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2010-04-29 03:31:16 +00:00
|
|
|
#include <sys/wait.h>
|
2010-03-26 14:49:45 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 17:00:34 +00:00
|
|
|
#include "virhook.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2010-11-16 14:54:17 +00:00
|
|
|
#include "configmake.h"
|
2012-12-12 16:27:01 +00:00
|
|
|
#include "vircommand.h"
|
2010-03-26 14:49:45 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_HOOK
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.hook");
|
|
|
|
|
2010-11-16 14:54:17 +00:00
|
|
|
#define LIBVIRT_HOOK_DIR SYSCONFDIR "/libvirt/hooks"
|
2010-03-26 14:49:45 +00:00
|
|
|
|
2019-01-20 16:04:56 +00:00
|
|
|
VIR_ENUM_DECL(virHookDriver);
|
|
|
|
VIR_ENUM_DECL(virHookDaemonOp);
|
|
|
|
VIR_ENUM_DECL(virHookSubop);
|
|
|
|
VIR_ENUM_DECL(virHookQemuOp);
|
|
|
|
VIR_ENUM_DECL(virHookLxcOp);
|
|
|
|
VIR_ENUM_DECL(virHookNetworkOp);
|
|
|
|
VIR_ENUM_DECL(virHookLibxlOp);
|
2010-03-26 14:49:45 +00:00
|
|
|
|
|
|
|
VIR_ENUM_IMPL(virHookDriver,
|
|
|
|
VIR_HOOK_DRIVER_LAST,
|
|
|
|
"daemon",
|
|
|
|
"qemu",
|
2014-01-31 15:48:06 +00:00
|
|
|
"lxc",
|
2016-07-11 11:55:53 +00:00
|
|
|
"network",
|
2019-01-20 16:30:15 +00:00
|
|
|
"libxl",
|
|
|
|
);
|
2010-03-26 14:49:45 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virHookDaemonOp,
|
|
|
|
VIR_HOOK_DAEMON_OP_LAST,
|
2010-03-26 14:49:45 +00:00
|
|
|
"start",
|
|
|
|
"shutdown",
|
2019-01-20 16:30:15 +00:00
|
|
|
"reload",
|
|
|
|
);
|
2010-03-26 14:49:45 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virHookSubop,
|
|
|
|
VIR_HOOK_SUBOP_LAST,
|
2010-03-26 14:49:45 +00:00
|
|
|
"-",
|
|
|
|
"begin",
|
2019-01-20 16:30:15 +00:00
|
|
|
"end",
|
|
|
|
);
|
2010-03-26 14:49:45 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virHookQemuOp,
|
|
|
|
VIR_HOOK_QEMU_OP_LAST,
|
2010-03-26 14:49:45 +00:00
|
|
|
"start",
|
2011-03-22 13:12:36 +00:00
|
|
|
"stopped",
|
|
|
|
"prepare",
|
2012-02-28 12:42:42 +00:00
|
|
|
"release",
|
Add some missing hook functions
A core use case of the hook scripts is to be able to do things
to a guest's network configuration. It is possible to hook into
the 'start' operation for a QEMU guest which runs just before
the guest is started. The TAP devices will exist at this point,
but the QEMU process will not. It can be desirable to have a
'started' hook too, which runs once QEMU has started.
If libvirtd is restarted it will re-populate firewall rules,
but there is no QEMU hook to trigger for existing domains.
This is solved with a 'reconnect' hook.
Finally, if attaching to an external QEMU process there needs
to be an 'attach' hook script.
This all also applies to the LXC driver
* docs/hooks.html.in: Document new operations
* src/util/hooks.c, src/util/hooks.c: Add 'started', 'reconnect'
and 'attach' operations for QEMU. Add 'prepare', 'started',
'release' and 'reconnect' operations for LXC
* src/lxc/lxc_driver.c: Add hooks for 'prepare', 'started',
'release' and 'reconnect' operations
* src/qemu/qemu_process.c: Add hooks for 'started', 'reconnect'
and 'reconnect' operations
2012-05-28 14:04:31 +00:00
|
|
|
"migrate",
|
|
|
|
"started",
|
|
|
|
"reconnect",
|
2014-09-17 09:38:39 +00:00
|
|
|
"attach",
|
2019-01-20 16:30:15 +00:00
|
|
|
"restore",
|
|
|
|
);
|
2010-03-26 14:49:45 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virHookLxcOp,
|
|
|
|
VIR_HOOK_LXC_OP_LAST,
|
2010-03-26 14:49:45 +00:00
|
|
|
"start",
|
Add some missing hook functions
A core use case of the hook scripts is to be able to do things
to a guest's network configuration. It is possible to hook into
the 'start' operation for a QEMU guest which runs just before
the guest is started. The TAP devices will exist at this point,
but the QEMU process will not. It can be desirable to have a
'started' hook too, which runs once QEMU has started.
If libvirtd is restarted it will re-populate firewall rules,
but there is no QEMU hook to trigger for existing domains.
This is solved with a 'reconnect' hook.
Finally, if attaching to an external QEMU process there needs
to be an 'attach' hook script.
This all also applies to the LXC driver
* docs/hooks.html.in: Document new operations
* src/util/hooks.c, src/util/hooks.c: Add 'started', 'reconnect'
and 'attach' operations for QEMU. Add 'prepare', 'started',
'release' and 'reconnect' operations for LXC
* src/lxc/lxc_driver.c: Add hooks for 'prepare', 'started',
'release' and 'reconnect' operations
* src/qemu/qemu_process.c: Add hooks for 'started', 'reconnect'
and 'reconnect' operations
2012-05-28 14:04:31 +00:00
|
|
|
"stopped",
|
|
|
|
"prepare",
|
|
|
|
"release",
|
|
|
|
"started",
|
2019-01-20 16:30:15 +00:00
|
|
|
"reconnect",
|
|
|
|
);
|
2010-03-26 14:49:45 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virHookNetworkOp,
|
|
|
|
VIR_HOOK_NETWORK_OP_LAST,
|
2014-01-31 15:48:06 +00:00
|
|
|
"start",
|
|
|
|
"started",
|
|
|
|
"stopped",
|
2018-12-19 15:36:04 +00:00
|
|
|
"port-created",
|
|
|
|
"port-deleted",
|
2019-01-20 16:30:15 +00:00
|
|
|
"updated",
|
|
|
|
);
|
2014-01-31 15:48:06 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virHookLibxlOp,
|
|
|
|
VIR_HOOK_LIBXL_OP_LAST,
|
2016-07-11 11:55:53 +00:00
|
|
|
"start",
|
|
|
|
"stopped",
|
|
|
|
"prepare",
|
|
|
|
"release",
|
|
|
|
"migrate",
|
|
|
|
"started",
|
2019-01-20 16:30:15 +00:00
|
|
|
"reconnect",
|
|
|
|
);
|
2016-07-11 11:55:53 +00:00
|
|
|
|
2010-03-26 14:49:45 +00:00
|
|
|
static int virHooksFound = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virHookCheck:
|
|
|
|
* @driver: the driver name "daemon", "qemu", "lxc"...
|
|
|
|
*
|
|
|
|
* Check is there is an installed hook for the given driver, if this
|
|
|
|
* is the case register it. Then subsequent calls to virHookCall
|
|
|
|
* will call the hook if found.
|
|
|
|
*
|
|
|
|
* Returns 1 if found, 0 if not found, and -1 in case of error
|
|
|
|
*/
|
|
|
|
static int
|
2014-03-18 08:14:35 +00:00
|
|
|
virHookCheck(int no, const char *driver)
|
|
|
|
{
|
2018-07-24 15:52:18 +00:00
|
|
|
VIR_AUTOFREE(char *) path = NULL;
|
2010-03-26 14:49:45 +00:00
|
|
|
|
|
|
|
if (driver == NULL) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid hook name for #%d"), no);
|
2011-03-18 20:41:13 +00:00
|
|
|
return -1;
|
2010-03-26 14:49:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 15:30:38 +00:00
|
|
|
if (virBuildPath(&path, LIBVIRT_HOOK_DIR, driver) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to build path for %s hook"),
|
|
|
|
driver);
|
2011-03-18 20:41:13 +00:00
|
|
|
return -1;
|
2010-03-26 14:49:45 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 21:14:27 +00:00
|
|
|
if (!virFileExists(path)) {
|
|
|
|
VIR_DEBUG("No hook script %s", path);
|
2018-07-24 15:52:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virFileIsExecutable(path)) {
|
2011-03-25 21:14:27 +00:00
|
|
|
VIR_WARN("Non-executable hook script %s", path);
|
2018-07-24 15:52:18 +00:00
|
|
|
return 0;
|
2010-03-26 14:49:45 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:18 +00:00
|
|
|
VIR_DEBUG("Found hook script %s", path);
|
|
|
|
return 1;
|
2010-03-26 14:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* virHookInitialize:
|
|
|
|
*
|
|
|
|
* Initialize synchronous hooks support.
|
|
|
|
* Check is there is an installed hook for all the drivers
|
|
|
|
*
|
|
|
|
* Returns the number of hooks found or -1 in case of failure
|
|
|
|
*/
|
|
|
|
int
|
2014-03-18 08:14:35 +00:00
|
|
|
virHookInitialize(void)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
|
|
|
int res, ret = 0;
|
2010-03-26 14:49:45 +00:00
|
|
|
|
|
|
|
virHooksFound = 0;
|
2013-05-24 16:58:25 +00:00
|
|
|
for (i = 0; i < VIR_HOOK_DRIVER_LAST; i++) {
|
2010-03-26 14:49:45 +00:00
|
|
|
res = virHookCheck(i, virHookDriverTypeToString(i));
|
|
|
|
if (res < 0)
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2010-03-26 14:49:45 +00:00
|
|
|
|
|
|
|
if (res == 1) {
|
|
|
|
virHooksFound |= (1 << i);
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 11:33:35 +00:00
|
|
|
return ret;
|
2010-03-26 14:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virHookPresent:
|
|
|
|
* @driver: the driver number (from virHookDriver enum)
|
|
|
|
*
|
|
|
|
* Check if a hook exists for the given driver, this is needed
|
|
|
|
* to avoid unnecessary work if the hook is not present
|
|
|
|
*
|
|
|
|
* Returns 1 if present, 0 otherwise
|
|
|
|
*/
|
|
|
|
int
|
2014-03-18 08:14:35 +00:00
|
|
|
virHookPresent(int driver)
|
|
|
|
{
|
2010-03-26 14:49:45 +00:00
|
|
|
if ((driver < VIR_HOOK_DRIVER_DAEMON) ||
|
|
|
|
(driver >= VIR_HOOK_DRIVER_LAST))
|
2012-03-22 11:33:35 +00:00
|
|
|
return 0;
|
2010-03-26 14:49:45 +00:00
|
|
|
if (virHooksFound == -1)
|
2012-03-22 11:33:35 +00:00
|
|
|
return 0;
|
2010-03-26 14:49:45 +00:00
|
|
|
|
|
|
|
if ((virHooksFound & (1 << driver)) == 0)
|
2012-03-22 11:33:35 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
2010-03-26 14:49:45 +00:00
|
|
|
}
|
|
|
|
|
2012-02-27 16:06:22 +00:00
|
|
|
/**
|
2010-03-26 14:49:45 +00:00
|
|
|
* virHookCall:
|
|
|
|
* @driver: the driver number (from virHookDriver enum)
|
|
|
|
* @id: an id for the object '-' if non available for example on daemon hooks
|
|
|
|
* @op: the operation on the id e.g. VIR_HOOK_QEMU_OP_START
|
|
|
|
* @sub_op: a sub_operation, currently unused
|
|
|
|
* @extra: optional string information
|
|
|
|
* @input: extra input given to the script on stdin
|
2012-02-27 16:06:22 +00:00
|
|
|
* @output: optional address of variable to store malloced result buffer
|
2010-03-26 14:49:45 +00:00
|
|
|
*
|
|
|
|
* Implement a hook call, where the external script for the driver is
|
|
|
|
* called with the given information. This is a synchronous call, we wait for
|
2012-02-27 16:06:22 +00:00
|
|
|
* execution completion. If @output is non-NULL, *output is guaranteed to be
|
|
|
|
* allocated after successful virHookCall, and is best-effort allocated after
|
|
|
|
* failed virHookCall; the caller is responsible for freeing *output.
|
2010-03-26 14:49:45 +00:00
|
|
|
*
|
|
|
|
* Returns: 0 if the execution succeeded, 1 if the script was not found or
|
|
|
|
* invalid parameters, and -1 if script returned an error
|
|
|
|
*/
|
|
|
|
int
|
2012-02-27 16:06:22 +00:00
|
|
|
virHookCall(int driver,
|
|
|
|
const char *id,
|
|
|
|
int op,
|
|
|
|
int sub_op,
|
|
|
|
const char *extra,
|
|
|
|
const char *input,
|
|
|
|
char **output)
|
|
|
|
{
|
2010-05-25 11:18:53 +00:00
|
|
|
int ret;
|
2018-07-24 15:52:18 +00:00
|
|
|
VIR_AUTOFREE(char *) path = NULL;
|
2018-07-24 15:52:19 +00:00
|
|
|
VIR_AUTOPTR(virCommand) cmd = NULL;
|
2010-03-26 14:49:45 +00:00
|
|
|
const char *drvstr;
|
|
|
|
const char *opstr;
|
|
|
|
const char *subopstr;
|
|
|
|
|
2012-02-27 16:06:22 +00:00
|
|
|
if (output)
|
|
|
|
*output = NULL;
|
|
|
|
|
2010-03-26 14:49:45 +00:00
|
|
|
if ((driver < VIR_HOOK_DRIVER_DAEMON) ||
|
|
|
|
(driver >= VIR_HOOK_DRIVER_LAST))
|
2012-03-22 11:33:35 +00:00
|
|
|
return 1;
|
2010-03-26 14:49:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We cache the availability of the script to minimize impact at
|
|
|
|
* runtime if no script is defined, this is being reset on SIGHUP
|
|
|
|
*/
|
|
|
|
if ((virHooksFound == -1) ||
|
|
|
|
((driver == VIR_HOOK_DRIVER_DAEMON) &&
|
2011-04-28 06:48:26 +00:00
|
|
|
(op == VIR_HOOK_DAEMON_OP_RELOAD ||
|
2018-09-19 08:38:14 +00:00
|
|
|
op == VIR_HOOK_DAEMON_OP_SHUTDOWN)))
|
2010-03-26 14:49:45 +00:00
|
|
|
virHookInitialize();
|
|
|
|
|
|
|
|
if ((virHooksFound & (1 << driver)) == 0)
|
2012-03-22 11:33:35 +00:00
|
|
|
return 1;
|
2010-03-26 14:49:45 +00:00
|
|
|
|
|
|
|
drvstr = virHookDriverTypeToString(driver);
|
|
|
|
|
|
|
|
opstr = NULL;
|
|
|
|
switch (driver) {
|
|
|
|
case VIR_HOOK_DRIVER_DAEMON:
|
|
|
|
opstr = virHookDaemonOpTypeToString(op);
|
|
|
|
break;
|
|
|
|
case VIR_HOOK_DRIVER_QEMU:
|
|
|
|
opstr = virHookQemuOpTypeToString(op);
|
|
|
|
break;
|
|
|
|
case VIR_HOOK_DRIVER_LXC:
|
|
|
|
opstr = virHookLxcOpTypeToString(op);
|
|
|
|
break;
|
2016-07-11 11:55:53 +00:00
|
|
|
case VIR_HOOK_DRIVER_LIBXL:
|
|
|
|
opstr = virHookLibxlOpTypeToString(op);
|
|
|
|
break;
|
2014-01-31 15:48:06 +00:00
|
|
|
case VIR_HOOK_DRIVER_NETWORK:
|
|
|
|
opstr = virHookNetworkOpTypeToString(op);
|
2010-03-26 14:49:45 +00:00
|
|
|
}
|
|
|
|
if (opstr == NULL) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Hook for %s, failed to find operation #%d"),
|
|
|
|
drvstr, op);
|
2012-03-22 11:33:35 +00:00
|
|
|
return 1;
|
2010-03-26 14:49:45 +00:00
|
|
|
}
|
|
|
|
subopstr = virHookSubopTypeToString(sub_op);
|
|
|
|
if (subopstr == NULL)
|
|
|
|
subopstr = "-";
|
|
|
|
if (extra == NULL)
|
|
|
|
extra = "-";
|
|
|
|
|
2016-02-10 15:30:38 +00:00
|
|
|
if (virBuildPath(&path, LIBVIRT_HOOK_DIR, drvstr) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to build path for %s hook"),
|
|
|
|
drvstr);
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2010-03-26 14:49:45 +00:00
|
|
|
}
|
|
|
|
|
2012-06-15 12:48:34 +00:00
|
|
|
VIR_DEBUG("Calling hook opstr=%s subopstr=%s extra=%s",
|
|
|
|
opstr, subopstr, extra);
|
|
|
|
|
2010-05-25 11:18:53 +00:00
|
|
|
cmd = virCommandNewArgList(path, id, opstr, subopstr, extra, NULL);
|
2010-03-26 14:49:45 +00:00
|
|
|
|
2010-05-25 11:18:53 +00:00
|
|
|
virCommandAddEnvPassCommon(cmd);
|
2010-03-26 14:49:45 +00:00
|
|
|
|
2010-05-25 11:18:53 +00:00
|
|
|
if (input)
|
|
|
|
virCommandSetInputBuffer(cmd, input);
|
2012-02-27 16:06:22 +00:00
|
|
|
if (output)
|
|
|
|
virCommandSetOutputBuffer(cmd, output);
|
2010-03-26 14:49:45 +00:00
|
|
|
|
2012-10-09 03:44:36 +00:00
|
|
|
ret = virCommandRun(cmd, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
/* Convert INTERNAL_ERROR into known error. */
|
2014-09-30 11:01:49 +00:00
|
|
|
virReportError(VIR_ERR_HOOK_SCRIPT_FAILED, "%s",
|
2016-05-19 19:10:19 +00:00
|
|
|
virGetLastErrorMessage());
|
2011-07-29 14:40:47 +00:00
|
|
|
}
|
2010-03-26 14:49:45 +00:00
|
|
|
|
2010-05-25 11:18:53 +00:00
|
|
|
return ret;
|
2010-03-26 14:49:45 +00:00
|
|
|
}
|