one_conf.c 8.8 KB
Newer Older
D
Daniel Veillard 已提交
1
/*----------------------------------------------------------------------------------*/
2 3 4
/* Copyright 2002-2009, Distributed Systems Architecture Group, Universidad
 * Complutense de Madrid (dsa-research.org)
 *
D
Daniel Veillard 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
18
 */
D
Daniel Veillard 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 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
/*-----------------------------------------------------------------------------------*/

#include <config.h>
#include <sys/utsname.h>

#include "virterror_internal.h"
#include "one_conf.h"
#include "buf.h"
#include "memory.h"
#include "util.h"

#define VIR_FROM_THIS VIR_FROM_ONE
/* --------------------------------------------------------------------------------- */

/**
 * oneCapsInit initialize the driver capabilities
 * @return a pointer to the driver capabilities NULL in case of error
 */

virCapsPtr oneCapsInit(void)
{
    struct utsname  utsname;
    virCapsPtr      caps;
    virCapsGuestPtr guest;

    uname(&utsname);

    if ((caps = virCapabilitiesNew(utsname.machine,0,0)) == NULL)
    {
        goto no_memory;
    }

    virCapabilitiesSetMacPrefix(caps,(unsigned char[]){ 0x52, 0x54, 0x00 });

    if ((guest = virCapabilitiesAddGuest(caps,
                                         "hvm",
                                         "i686",
                                         32,
                                         NULL,
                                         NULL,
                                         0,
                                         NULL)) == NULL)
    {
        goto no_memory;
    }

    if (virCapabilitiesAddGuestDomain(guest,
                                      "one",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
    {
        goto no_memory;
    }


76
    if ((guest = virCapabilitiesAddGuest(caps,
D
Daniel Veillard 已提交
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
                                         "hvm",
                                         "x86_64",
                                         64,
                                         NULL,
                                         NULL,
                                         0,
                                         NULL)) == NULL)
    {
        goto no_memory;
    }

    if (virCapabilitiesAddGuestDomain(guest,
                                      "one",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
    {
        goto no_memory;
    }

    return caps;

no_memory:

    virCapabilitiesFree(caps);
    return NULL;
}

/* --------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------- */


/**
 * oneSubmitVM generates an OpenNebula description file and submits the new VM
 * @param driver the OpenNebula driver
 * @param vm the virtual machine pointer
 * @return the OpenNebula ID for the new VM or -1 in case of error
 */
117

D
Daniel Veillard 已提交
118 119 120 121 122 123 124
int oneSubmitVM(virConnectPtr    conn,
                one_driver_t*    driver ATTRIBUTE_UNUSED,
                virDomainObjPtr  vm)
{
    char* templ;
    int   oneid;

125 126 127 128
    if ((templ = xmlOneTemplate(conn,vm->def)) == NULL)
        return -1;

    if ((oneid = c_oneAllocateTemplate(templ)) < 0) {
D
Daniel Veillard 已提交
129
        oneError(conn, NULL, VIR_ERR_OPERATION_FAILED,
130 131 132
                 _("Error submitting virtual machine to OpenNebula"));
        VIR_FREE(templ);
        return -1;
D
Daniel Veillard 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    }

    VIR_FREE(templ);
    return oneid;
}
/* --------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------- */

/**
 * xmlOneTemplate Generate an OpenNebula template to deploy a VM from libvirt
 * internal Domain definition.
 * @param def  Internal libvirt Domain definition
 * @return OpenNebula VM template.
 */

char* xmlOneTemplate(virConnectPtr conn,virDomainDefPtr def)
150 151 152 153 154 155 156 157
{
    int i;
    virBuffer buf= VIR_BUFFER_INITIALIZER;
    virBufferVSprintf(&buf,"#OpenNebula Template automatically generated by libvirt\nNAME = %s\nCPU = %ld\nMEMORY = %ld\n",
                      def->name,
                      def->vcpus,
                      (def->maxmem)/1024);

D
Daniel Veillard 已提交
158
    /*Optional Booting OpenNebula Information:*/
159
    if (def->os.kernel) {
D
Daniel Veillard 已提交
160
        virBufferVSprintf(&buf,"OS=[ kernel = \"%s\"",def->os.kernel);
161 162 163 164 165 166 167 168
        if (def->os.initrd)
            virBufferVSprintf(&buf,",\n    initrd = \"%s\"",def->os.initrd);
        if (def->os.cmdline)
            virBufferVSprintf(&buf,",\n    kernel_cmd = \"%s\"",def->os.cmdline);
        if (def->os.root)
            virBufferVSprintf(&buf,",\n    root  = \"%s\"",def->os.root);

        virBufferAddLit(&buf," ]\n");
D
Daniel Veillard 已提交
169 170
    }
    /* set Disks & NICS */
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    for (i=0 ; i < def->ndisks ; i++) {
        // missing source is only allowed at cdrom and floppy
        if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
            virBufferVSprintf(&buf, "DISK=[ type = disk,\n"
                              "	source = \"%s\",\n",
                              def->disks[i]->src);
        }
        else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
            virBufferAddLit(&buf,  "DISK=[ type = cdrom,\n");
            if (def->disks[i]->src) virBufferVSprintf(&buf, "	source = \"%s\",\n",def->disks[i]->src);
        }
        else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
            virBufferAddLit(&buf,  "DISK=[ type = floppy,\n");
            if (def->disks[i]->src) virBufferVSprintf(&buf, "	source = \"%s\",\n",def->disks[i]->src);
        }

        virBufferVSprintf(&buf, "	target = \"%s\",\n"
                          "	readonly =",
                          def->disks[i]->dst);

        if (def->disks[i]->readonly)
            virBufferAddLit(&buf,"\"yes\"]\n");
        else
            virBufferAddLit(&buf,"\"no\"]\n");
D
Daniel Veillard 已提交
195
    }
196 197

    for (i=0 ; i< def->nnets ; i++)
D
Daniel Veillard 已提交
198
    {
199
        if (!def->nets[i]) {
D
Daniel Veillard 已提交
200 201 202 203 204
            continue;
        }

        switch(def->nets[i]->type)
        {
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        case VIR_DOMAIN_NET_TYPE_BRIDGE:
            virBufferVSprintf(&buf,"NIC=[ bridge =\"%s\",\n",def->nets[i]->data.bridge.brname);

            if (def->nets[i]->ifname)
                virBufferVSprintf(&buf,"      target =\"%s\",\n",def->nets[i]->ifname);

            virBufferVSprintf(&buf,"      mac =\"%02x:%02x:%02x:%02x:%02x:%02x\" ]\n",
                              def->nets[i]->mac[0],def->nets[i]->mac[1],
                              def->nets[i]->mac[2],def->nets[i]->mac[3],
                              def->nets[i]->mac[4],def->nets[i]->mac[5]);
            break;

        case VIR_DOMAIN_NET_TYPE_NETWORK:
            virBufferVSprintf(&buf,"NIC=[ network=\"%s\"",def->nets[i]->data.network.name);
            if (def->nets[i]->ifname)
                virBufferVSprintf(&buf,",\n      target =\"%s\"",def->nets[i]->ifname);
            virBufferAddLit(&buf," ]\n");
            break;

        default: break;
        }
    }

    if (def->graphics != NULL) {
        if (def->graphics->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
            virBufferAddLit(&buf,"GRAPHICS = [\n  type = \"vnc\"");

            if (def->graphics->data.vnc.listenAddr != NULL)
                virBufferVSprintf(&buf,",\n  listen = \"%s\"",def->graphics->data.vnc.listenAddr);

            if (def->graphics->data.vnc.autoport == 0)
                virBufferVSprintf(&buf,",\n  port = \"%d\"",def->graphics->data.vnc.port);

            if (def->graphics->data.vnc.passwd != NULL)
                virBufferVSprintf(&buf,",\n  passwd = \"%s\"",def->graphics->data.vnc.passwd);

            virBufferAddLit(&buf," ]\n");

        }
        else //graphics.type==VIR_DOMAIN_GRAPHICS_TYPE_SDL
            virBufferAddLit(&buf,"GRAPHICS = [\n  type = \"sdl\" ]\n");

D
Daniel Veillard 已提交
247 248 249 250 251 252
    }
    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

253
no_memory:
D
Daniel Veillard 已提交
254 255 256 257
    virReportOOMError(conn);
    char* tmp = virBufferContentAndReset(&buf);
    VIR_FREE(tmp);
    return NULL;
258
};
D
Daniel Veillard 已提交
259