2018-08-17 08:19:59 +00:00
|
|
|
/*
|
|
|
|
* vircgroupv1.c: methods for cgroups v1 backend
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2015,2018 Red Hat, Inc.
|
|
|
|
* Copyright IBM Corp. 2008
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <config.h>
|
|
|
|
|
2018-09-13 14:27:56 +00:00
|
|
|
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
|
|
|
|
# include <mntent.h>
|
|
|
|
#endif
|
|
|
|
|
2018-08-17 08:19:59 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
#define __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
|
|
|
|
#include "vircgrouppriv.h"
|
|
|
|
#undef __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
|
|
|
|
|
|
|
|
#include "vircgroup.h"
|
|
|
|
#include "vircgroupbackend.h"
|
|
|
|
#include "vircgroupv1.h"
|
2018-09-13 14:27:56 +00:00
|
|
|
#include "virfile.h"
|
2018-08-17 08:19:59 +00:00
|
|
|
#include "virlog.h"
|
2018-09-14 11:21:19 +00:00
|
|
|
#include "virstring.h"
|
|
|
|
#include "virsystemd.h"
|
2018-08-17 08:19:59 +00:00
|
|
|
|
|
|
|
VIR_LOG_INIT("util.cgroup");
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_CGROUP
|
|
|
|
|
|
|
|
|
|
|
|
VIR_ENUM_DECL(virCgroupV1Controller);
|
|
|
|
VIR_ENUM_IMPL(virCgroupV1Controller, VIR_CGROUP_CONTROLLER_LAST,
|
|
|
|
"cpu", "cpuacct", "cpuset", "memory", "devices",
|
|
|
|
"freezer", "blkio", "net_cls", "perf_event",
|
|
|
|
"name=systemd");
|
|
|
|
|
|
|
|
|
2018-09-13 14:27:56 +00:00
|
|
|
/* We're looking for at least one 'cgroup' fs mount,
|
|
|
|
* which is *not* a named mount. */
|
|
|
|
static bool
|
|
|
|
virCgroupV1Available(void)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
FILE *mounts = NULL;
|
|
|
|
struct mntent entry;
|
|
|
|
char buf[CGROUP_MAX_VAL];
|
|
|
|
|
|
|
|
if (!virFileExists("/proc/cgroups"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!(mounts = fopen("/proc/mounts", "r")))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
|
|
|
|
if (STREQ(entry.mnt_type, "cgroup") && !strstr(entry.mnt_opts, "name=")) {
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FORCE_FCLOSE(mounts);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-14 11:21:19 +00:00
|
|
|
static bool
|
|
|
|
virCgroupV1ValidateMachineGroup(virCgroupPtr group,
|
|
|
|
const char *name,
|
|
|
|
const char *drivername,
|
|
|
|
const char *machinename)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
VIR_AUTOFREE(char *) partname = NULL;
|
|
|
|
VIR_AUTOFREE(char *) scopename_old = NULL;
|
|
|
|
VIR_AUTOFREE(char *) scopename_new = NULL;
|
|
|
|
VIR_AUTOFREE(char *) partmachinename = NULL;
|
|
|
|
|
|
|
|
if (virAsprintf(&partname, "%s.libvirt-%s",
|
|
|
|
name, drivername) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (virCgroupPartitionEscape(&partname) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (virAsprintf(&partmachinename, "%s.libvirt-%s",
|
|
|
|
machinename, drivername) < 0 ||
|
|
|
|
virCgroupPartitionEscape(&partmachinename) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!(scopename_old = virSystemdMakeScopeName(name, drivername, true)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!(scopename_new = virSystemdMakeScopeName(machinename,
|
|
|
|
drivername, false)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (virCgroupPartitionEscape(&scopename_old) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (virCgroupPartitionEscape(&scopename_new) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if (i == VIR_CGROUP_CONTROLLER_SYSTEMD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!group->controllers[i].placement)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tmp = strrchr(group->controllers[i].placement, '/');
|
|
|
|
if (!tmp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (i == VIR_CGROUP_CONTROLLER_CPU ||
|
|
|
|
i == VIR_CGROUP_CONTROLLER_CPUACCT ||
|
|
|
|
i == VIR_CGROUP_CONTROLLER_CPUSET) {
|
|
|
|
if (STREQ(tmp, "/emulator"))
|
|
|
|
*tmp = '\0';
|
|
|
|
tmp = strrchr(group->controllers[i].placement, '/');
|
|
|
|
if (!tmp)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp++;
|
|
|
|
|
|
|
|
if (STRNEQ(tmp, name) &&
|
|
|
|
STRNEQ(tmp, machinename) &&
|
|
|
|
STRNEQ(tmp, partname) &&
|
|
|
|
STRNEQ(tmp, partmachinename) &&
|
|
|
|
STRNEQ(tmp, scopename_old) &&
|
|
|
|
STRNEQ(tmp, scopename_new)) {
|
|
|
|
VIR_DEBUG("Name '%s' for controller '%s' does not match "
|
|
|
|
"'%s', '%s', '%s', '%s' or '%s'",
|
|
|
|
tmp, virCgroupV1ControllerTypeToString(i),
|
|
|
|
name, machinename, partname,
|
|
|
|
scopename_old, scopename_new);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 07:46:10 +00:00
|
|
|
static int
|
|
|
|
virCgroupV1CopyMounts(virCgroupPtr group,
|
|
|
|
virCgroupPtr parent)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
|
|
|
|
if (!parent->controllers[i].mountPoint)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (VIR_STRDUP(group->controllers[i].mountPoint,
|
|
|
|
parent->controllers[i].mountPoint) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (VIR_STRDUP(group->controllers[i].linkPoint,
|
|
|
|
parent->controllers[i].linkPoint) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 08:19:59 +00:00
|
|
|
virCgroupBackend virCgroupV1Backend = {
|
|
|
|
.type = VIR_CGROUP_BACKEND_TYPE_V1,
|
2018-09-13 14:27:56 +00:00
|
|
|
|
|
|
|
.available = virCgroupV1Available,
|
2018-09-14 11:21:19 +00:00
|
|
|
.validateMachineGroup = virCgroupV1ValidateMachineGroup,
|
2018-08-17 07:46:10 +00:00
|
|
|
.copyMounts = virCgroupV1CopyMounts,
|
2018-08-17 08:19:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virCgroupV1Register(void)
|
|
|
|
{
|
|
|
|
virCgroupBackendRegister(&virCgroupV1Backend);
|
|
|
|
}
|