libvirt/src/qemu/qemu_extdevice.c
Stefan Berger 3f1a707042 qemu: Add swtpm to emulator cgroup
Add the external swtpm to the emulator cgroup so that upper limits of CPU
usage can be enforced on the emulated TPM.

To enable this we need to have the swtpm write its process id (pid) into a
file. We then read it from the file to configure the emulator cgroup.

The PID file is created in /var/run/libvirt/qemu/swtpm:

[root@localhost swtpm]# ls -lZ /var/run/libvirt/qemu/swtpm/
total 4
-rw-r--r--. 1 tss  tss  system_u:object_r:qemu_var_run_t:s0          5 Apr 10 12:26 1-testvm-swtpm.pid
srw-rw----. 1 qemu qemu system_u:object_r:svirt_image_t:s0:c597,c632 0 Apr 10 12:26 1-testvm-swtpm.sock

The swtpm command line now looks as follows:

root@localhost testvm]# ps auxZ | grep swtpm | grep socket | grep -v grep
system_u:system_r:virtd_t:s0:c597,c632 tss 18697 0.0  0.0 28172 3892 ?       Ss   16:46   0:00 /usr/bin/swtpm socket --daemon --ctrl type=unixio,path=/var/run/libvirt/qemu/swtpm/1-testvm-swtpm.sock,mode=0600 --tpmstate dir=/var/lib/libvirt/swtpm/485d0004-a48f-436a-8457-8a3b73e28568/tpm1.2/ --log file=/var/log/swtpm/libvirt/qemu/testvm-swtpm.log --pid file=/var/run/libvirt/qemu/swtpm/1-testvm-swtpm.pid

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2018-06-06 10:48:41 -04:00

181 lines
3.8 KiB
C

/*
* qemu_extdevice.c: QEMU external devices support
*
* Copyright (C) 2014, 2018 IBM Corporation
*
* 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/>.
*
* Author: Stefan Berger <stefanb@linux.vnet.ibm.com>
*/
#include <config.h>
#include "qemu_extdevice.h"
#include "qemu_domain.h"
#include "qemu_tpm.h"
#include "viralloc.h"
#include "virlog.h"
#include "virstring.h"
#include "virtime.h"
#include "virtpm.h"
#include "virpidfile.h"
#define VIR_FROM_THIS VIR_FROM_QEMU
VIR_LOG_INIT("qemu.qemu_extdevice")
int
qemuExtDeviceLogCommand(qemuDomainLogContextPtr logCtxt,
virCommandPtr cmd,
const char *info)
{
int ret = -1;
char *timestamp = NULL;
char *logline = NULL;
int logFD;
logFD = qemuDomainLogContextGetWriteFD(logCtxt);
if ((timestamp = virTimeStringNow()) == NULL)
goto cleanup;
if (virAsprintf(&logline, "%s: Starting external device: %s\n",
timestamp, info) < 0)
goto cleanup;
if (safewrite(logFD, logline, strlen(logline)) < 0)
goto cleanup;
virCommandWriteArgLog(cmd, logFD);
ret = 0;
cleanup:
VIR_FREE(timestamp);
VIR_FREE(logline);
return ret;
}
/*
* qemuExtDevicesInitPaths:
*
* @driver: QEMU driver
* @def: domain definition
*
* Initialize paths of external devices so that it is known where state is
* stored and we can remove directories and files in case of domain XML
* changes.
*/
static int
qemuExtDevicesInitPaths(virQEMUDriverPtr driver,
virDomainDefPtr def)
{
int ret = 0;
if (def->tpm)
ret = qemuExtTPMInitPaths(driver, def);
return ret;
}
/*
* qemuExtDevicesPrepareHost:
*
* @driver: QEMU driver
* @def: domain definition
*
* Prepare host storage paths for external devices.
*/
int
qemuExtDevicesPrepareHost(virQEMUDriverPtr driver,
virDomainDefPtr def)
{
int ret = 0;
if (def->tpm)
ret = qemuExtTPMPrepareHost(driver, def);
return ret;
}
void
qemuExtDevicesCleanupHost(virQEMUDriverPtr driver,
virDomainDefPtr def)
{
if (qemuExtDevicesInitPaths(driver, def) < 0)
return;
if (def->tpm)
qemuExtTPMCleanupHost(def);
}
int
qemuExtDevicesStart(virQEMUDriverPtr driver,
virDomainDefPtr def,
qemuDomainLogContextPtr logCtxt)
{
int ret = 0;
if (qemuExtDevicesInitPaths(driver, def) < 0)
return -1;
if (def->tpm)
ret = qemuExtTPMStart(driver, def, logCtxt);
return ret;
}
void
qemuExtDevicesStop(virQEMUDriverPtr driver,
virDomainDefPtr def)
{
if (qemuExtDevicesInitPaths(driver, def) < 0)
return;
if (def->tpm)
qemuExtTPMStop(driver, def);
}
bool
qemuExtDevicesHasDevice(virDomainDefPtr def)
{
if (def->tpm && def->tpm->type == VIR_DOMAIN_TPM_TYPE_EMULATOR)
return true;
return false;
}
int
qemuExtDevicesSetupCgroup(virQEMUDriverPtr driver,
virDomainDefPtr def,
virCgroupPtr cgroup)
{
int ret = 0;
if (def->tpm)
ret = qemuExtTPMSetupCgroup(driver, def, cgroup);
return ret;
}