lxc_domain.c 3.2 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2014 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright IBM Corp. 2008
 *
 * lxc_domain.h: LXC domain helpers
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25
 */

#include <config.h>

#include "lxc_domain.h"

26
#include "viralloc.h"
27
#include "virlog.h"
28
#include "virerror.h"
29 30

#define VIR_FROM_THIS VIR_FROM_LXC
31

32
static void *virLXCDomainObjPrivateAlloc(void)
33
{
34
    virLXCDomainObjPrivatePtr priv;
35 36 37 38 39 40 41

    if (VIR_ALLOC(priv) < 0)
        return NULL;

    return priv;
}

42
static void virLXCDomainObjPrivateFree(void *data)
43
{
44
    virLXCDomainObjPrivatePtr priv = data;
45

46 47
    virCgroupFree(&priv->cgroup);

48 49 50 51
    VIR_FREE(priv);
}


52 53 54 55
static int virLXCDomainObjPrivateXMLFormat(virBufferPtr buf, void *data)
{
    virLXCDomainObjPrivatePtr priv = data;

56
    virBufferAsprintf(buf, "<init pid='%llu'/>\n",
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
                      (unsigned long long)priv->initpid);

    return 0;
}

static int virLXCDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt, void *data)
{
    virLXCDomainObjPrivatePtr priv = data;
    unsigned long long thepid;

    if (virXPathULongLong("string(./init[1]/@pid)", ctxt, &thepid) < 0) {
        virErrorPtr err = virGetLastError();
        VIR_WARN("Failed to load init pid from state %s", err ? err->message : "null");
        priv->initpid = 0;
    } else {
        priv->initpid = thepid;
    }

    return 0;
}

78 79 80 81 82 83
virDomainXMLPrivateDataCallbacks virLXCDriverPrivateDataCallbacks = {
    .alloc = virLXCDomainObjPrivateAlloc,
    .free = virLXCDomainObjPrivateFree,
    .format = virLXCDomainObjPrivateXMLFormat,
    .parse  = virLXCDomainObjPrivateXMLParse,
};
84 85 86 87 88 89 90 91 92 93 94 95 96 97

static int
virLXCDomainDefPostParse(virDomainDefPtr def,
                         virCapsPtr caps,
                         void *opaque ATTRIBUTE_UNUSED)
{
    /* check for emulator and create a default one if needed */
    if (!def->emulator &&
        !(def->emulator = virDomainDefGetDefaultEmulator(def, caps)))
        return -1;

    return 0;
}

98 99 100

static int
virLXCDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
101
                               const virDomainDef *def ATTRIBUTE_UNUSED,
102 103 104 105 106 107 108 109 110 111 112 113
                               virCapsPtr caps ATTRIBUTE_UNUSED,
                               void *opaque ATTRIBUTE_UNUSED)
{
    if (dev->type == VIR_DOMAIN_DEVICE_CHR &&
        dev->data.chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE &&
        dev->data.chr->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_NONE)
        dev->data.chr->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_LXC;

    return 0;
}


114 115
virDomainDefParserConfig virLXCDriverDomainDefParserConfig = {
    .domainPostParseCallback = virLXCDomainDefPostParse,
116
    .devicesPostParseCallback = virLXCDomainDeviceDefPostParse,
117
};