qemu_extdevice.c 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * 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/>.
 */

#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"
31 32
#include "virtpm.h"
#include "virpidfile.h"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

#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,
130
                    virDomainObjPtr vm,
131 132 133 134
                    qemuDomainLogContextPtr logCtxt)
{
    int ret = 0;

135
    if (qemuExtDevicesInitPaths(driver, vm->def) < 0)
136 137
        return -1;

138 139
    if (vm->def->tpm)
        ret = qemuExtTPMStart(driver, vm, logCtxt);
140 141 142 143 144 145 146

    return ret;
}


void
qemuExtDevicesStop(virQEMUDriverPtr driver,
147
                   virDomainObjPtr vm)
148
{
149
    if (qemuExtDevicesInitPaths(driver, vm->def) < 0)
150 151
        return;

152 153
    if (vm->def->tpm)
        qemuExtTPMStop(driver, vm);
154
}
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178


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;
}