lxc_domain.c 3.6 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 33
VIR_LOG_INIT("lxc.lxc_domain");

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

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

    return priv;
}

44
static void virLXCDomainObjPrivateFree(void *data)
45
{
46
    virLXCDomainObjPrivatePtr priv = data;
47

48 49
    virCgroupFree(&priv->cgroup);

50 51 52 53
    VIR_FREE(priv);
}


54 55 56
static int
virLXCDomainObjPrivateXMLFormat(virBufferPtr buf,
                                virDomainObjPtr vm)
57
{
58
    virLXCDomainObjPrivatePtr priv = vm->privateData;
59

60
    virBufferAsprintf(buf, "<init pid='%llu'/>\n",
61 62 63 64 65
                      (unsigned long long)priv->initpid);

    return 0;
}

66 67 68
static int
virLXCDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt,
                               virDomainObjPtr vm)
69
{
70
    virLXCDomainObjPrivatePtr priv = vm->privateData;
71 72 73 74 75 76 77 78 79 80 81 82 83
    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;
}

84 85 86 87 88 89
virDomainXMLPrivateDataCallbacks virLXCDriverPrivateDataCallbacks = {
    .alloc = virLXCDomainObjPrivateAlloc,
    .free = virLXCDomainObjPrivateFree,
    .format = virLXCDomainObjPrivateXMLFormat,
    .parse  = virLXCDomainObjPrivateXMLParse,
};
90 91 92 93 94 95 96 97 98 99 100

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;

101 102 103 104
    /* memory hotplug tunables are not supported by this driver */
    if (virDomainDefCheckUnsupportedMemoryHotplug(def) < 0)
        return -1;

105 106 107
    return 0;
}

108 109 110

static int
virLXCDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
111
                               const virDomainDef *def ATTRIBUTE_UNUSED,
112 113 114 115 116 117 118 119
                               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;

120 121 122 123

    if (virDomainDeviceDefCheckUnsupportedMemoryDevice(dev) < 0)
        return -1;

124 125 126 127
    return 0;
}


128 129
virDomainDefParserConfig virLXCDriverDomainDefParserConfig = {
    .domainPostParseCallback = virLXCDomainDefPostParse,
130
    .devicesPostParseCallback = virLXCDomainDeviceDefPostParse,
131
};