one_conf.c 9.6 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
                                         "hvm",
                                         "x86_64",
                                         64,
                                         NULL,
                                         NULL,
                                         0,
                                         NULL)) == NULL)
    {
        goto no_memory;
    }

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    if (virCapabilitiesAddGuestDomain(guest,
                                      "one",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
    {
        goto no_memory;
    }
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "xen",
                                         "i686",
                                         32,
                                         NULL,
                                         NULL,
                                         0,
                                         NULL)) == NULL)
    {
        goto no_memory;
    }
D
Daniel Veillard 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    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
 */
137

D
Daniel Veillard 已提交
138 139 140 141 142 143 144
int oneSubmitVM(virConnectPtr    conn,
                one_driver_t*    driver ATTRIBUTE_UNUSED,
                virDomainObjPtr  vm)
{
    char* templ;
    int   oneid;

145 146 147 148
    if ((templ = xmlOneTemplate(conn,vm->def)) == NULL)
        return -1;

    if ((oneid = c_oneAllocateTemplate(templ)) < 0) {
D
Daniel Veillard 已提交
149
        oneError(conn, NULL, VIR_ERR_OPERATION_FAILED,
150 151 152
                 _("Error submitting virtual machine to OpenNebula"));
        VIR_FREE(templ);
        return -1;
D
Daniel Veillard 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    }

    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)
170 171 172 173 174 175 176 177
{
    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 已提交
178
    /*Optional Booting OpenNebula Information:*/
179
    if (def->os.kernel) {
D
Daniel Veillard 已提交
180
        virBufferVSprintf(&buf,"OS=[ kernel = \"%s\"",def->os.kernel);
181 182 183 184 185 186 187 188
        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 已提交
189 190
    }
    /* set Disks & NICS */
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"
195
                              "\tsource = \"%s\",\n",
196 197 198 199
                              def->disks[i]->src);
        }
        else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
            virBufferAddLit(&buf,  "DISK=[ type = cdrom,\n");
200
            if (def->disks[i]->src) virBufferVSprintf(&buf, "\tsource = \"%s\",\n",def->disks[i]->src);
201 202 203
        }
        else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
            virBufferAddLit(&buf,  "DISK=[ type = floppy,\n");
204
            if (def->disks[i]->src) virBufferVSprintf(&buf, "\tsource = \"%s\",\n",def->disks[i]->src);
205 206
        }

207 208
        virBufferVSprintf(&buf, "\ttarget = \"%s\",\n"
                          "\treadonly =",
209 210 211 212 213 214
                          def->disks[i]->dst);

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

    for (i=0 ; i< def->nnets ; i++)
D
Daniel Veillard 已提交
218
    {
219
        if (!def->nets[i]) {
D
Daniel Veillard 已提交
220 221 222 223 224
            continue;
        }

        switch(def->nets[i]->type)
        {
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        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;
        }
    }

248 249 250 251 252
    for(i=0;i<def->ngraphics;i++) {
        if (def->graphics[i] == NULL)
            continue;

        if (def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
253 254
            virBufferAddLit(&buf,"GRAPHICS = [\n  type = \"vnc\"");

255 256 257
            if (def->graphics[i]->data.vnc.listenAddr != NULL)
                virBufferVSprintf(&buf,",\n  listen = \"%s\"",
                    def->graphics[i]->data.vnc.listenAddr);
258

259 260 261
            if (def->graphics[i]->data.vnc.autoport == 0)
                virBufferVSprintf(&buf,",\n  port = \"%d\"",
                    def->graphics[i]->data.vnc.port);
262

263 264 265
            if (def->graphics[i]->data.vnc.passwd != NULL)
                virBufferVSprintf(&buf,",\n  passwd = \"%s\"",
                    def->graphics[i]->data.vnc.passwd);
266 267 268 269 270 271 272

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

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

D
Daniel Veillard 已提交
273 274 275 276 277 278
    }
    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

279
no_memory:
D
Daniel Veillard 已提交
280 281 282 283
    virReportOOMError(conn);
    char* tmp = virBufferContentAndReset(&buf);
    VIR_FREE(tmp);
    return NULL;
284
};