uml_conf.c 18.2 KB
Newer Older
1 2 3
/*
 * uml_conf.c: UML driver configuration
 *
4
 * Copyright (C) 2006-2010 Red Hat, Inc.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <dirent.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <sys/utsname.h>

#include "uml_conf.h"
#include "uuid.h"
#include "buf.h"
#include "conf.h"
#include "util.h"
#include "memory.h"
45
#include "nodeinfo.h"
46
#include "verify.h"
47
#include "bridge.h"
48
#include "logging.h"
49

50
#define VIR_FROM_THIS VIR_FROM_UML
51

52 53
#define umlLog(level, msg, ...)                                     \
        virLogMessage(__FILE__, level, 0, msg, __VA_ARGS__)
54 55 56 57 58 59 60 61 62 63 64

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

    /* Really, this never fails - look at the man-page. */
    uname (&utsname);

    if ((caps = virCapabilitiesNew(utsname.machine,
                                   0, 0)) == NULL)
65
        goto error;
66

67 68 69 70 71 72 73 74
    /* Some machines have problematic NUMA toplogy causing
     * unexpected failures. We don't want to break the QEMU
     * driver in this scenario, so log errors & carry on
     */
    if (nodeCapsInitNUMA(caps) < 0) {
        virCapabilitiesFreeNUMAInfo(caps);
        VIR_WARN0("Failed to query host NUMA topology, disabling NUMA capabilities");
    }
75

76 77 78 79 80 81
    if (virGetHostUUID(caps->host.host_uuid)) {
        umlReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot get the host uuid"));
        goto error;
    }

82 83 84 85 86 87 88 89
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "uml",
                                         utsname.machine,
                                         STREQ(utsname.machine, "x86_64") ? 64 : 32,
                                         NULL,
                                         NULL,
                                         0,
                                         NULL)) == NULL)
90
        goto error;
91 92 93 94 95 96 97

    if (virCapabilitiesAddGuestDomain(guest,
                                      "uml",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
98
        goto error;
99

100 101
    caps->defaultConsoleTargetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_UML;

102 103
    return caps;

104
 error:
105 106 107 108 109
    virCapabilitiesFree(caps);
    return NULL;
}


110
static int
111
umlConnectTapDevice(virDomainNetDefPtr net,
112 113
                    const char *bridge)
{
114
    brControl *brctl = NULL;
115
    int tapfd = -1;
116
    int template_ifname = 0;
117
    int err;
118
    unsigned char tapmac[VIR_MAC_BUFLEN];
119 120

    if ((err = brInit(&brctl))) {
121
        virReportSystemError(err, "%s",
122
                             _("cannot initialize bridge support"));
123 124 125
        goto error;
    }

126 127 128 129 130 131 132 133 134 135
    if (!net->ifname ||
        STRPREFIX(net->ifname, "vnet") ||
        strchr(net->ifname, '%')) {
        VIR_FREE(net->ifname);
        if (!(net->ifname = strdup("vnet%d")))
            goto no_memory;
        /* avoid exposing vnet%d in dumpxml or error outputs */
        template_ifname = 1;
    }

136 137 138 139 140 141 142 143
    memcpy(tapmac, net->mac, VIR_MAC_BUFLEN);
    tapmac[0] = 0xFE; /* Discourage bridge from using TAP dev MAC */
    if ((err = brAddTap(brctl,
                        bridge,
                        &net->ifname,
                        tapmac,
                        0,
                        &tapfd))) {
D
Doug Goldstein 已提交
144
        if (err == ENOTSUP) {
145
            /* In this particular case, give a better diagnostic. */
146
            umlReportError(VIR_ERR_INTERNAL_ERROR,
147 148
                           _("Failed to add tap interface to bridge. "
                             "%s is not a bridge device"), bridge);
149 150 151 152 153
        } else if (err == ENOENT) {
            virReportSystemError(err, "%s",
                    _("Failed to add tap interface to bridge. Your kernel "
                      "is missing the 'tun' module or CONFIG_TUN, or you need "
                      "to add the /dev/net/tun device node."));
154
        } else if (template_ifname) {
155
            virReportSystemError(err,
156 157
                                 _("Failed to add tap interface to bridge '%s'"),
                                 bridge);
158
        } else {
159
            virReportSystemError(err,
160 161
                                 _("Failed to add tap interface '%s' to bridge '%s'"),
                                 net->ifname, bridge);
162
        }
163 164
        if (template_ifname)
            VIR_FREE(net->ifname);
165 166 167 168 169 170 171 172 173
        goto error;
    }
    close(tapfd);

    brShutdown(brctl);

    return 0;

no_memory:
174
    virReportOOMError();
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
error:
    brShutdown(brctl);
    return -1;
}

static char *
umlBuildCommandLineNet(virConnectPtr conn,
                       virDomainNetDefPtr def,
                       int idx)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    /* General format:  ethNN=type,options */

    virBufferVSprintf(&buf, "eth%d=", idx);

    switch (def->type) {
    case VIR_DOMAIN_NET_TYPE_USER:
        /* ethNNN=slirp,macaddr */
        virBufferAddLit(&buf, "slirp");
        break;

    case VIR_DOMAIN_NET_TYPE_ETHERNET:
        /* ethNNN=tuntap,tapname,macaddr,gateway */
        virBufferAddLit(&buf, "tuntap");
        if (def->data.ethernet.ipaddr) {
201
            umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
202 203 204 205
                           _("IP address not supported for ethernet inteface"));
            goto error;
        }
        if (def->data.ethernet.script) {
206
            umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
207 208 209 210 211 212
                           _("script execution not supported for ethernet inteface"));
            goto error;
        }
        break;

    case VIR_DOMAIN_NET_TYPE_SERVER:
213
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
214 215 216 217
                       _("TCP server networking type not supported"));
        goto error;

    case VIR_DOMAIN_NET_TYPE_CLIENT:
218
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
219 220 221 222 223 224 225 226 227 228 229 230 231 232
                       _("TCP client networking type not supported"));
        goto error;

    case VIR_DOMAIN_NET_TYPE_MCAST:
        /* ethNNN=tuntap,macaddr,ipaddr,port */
        virBufferAddLit(&buf, "mcast");
        break;

    case VIR_DOMAIN_NET_TYPE_NETWORK:
    {
        char *bridge;
        virNetworkPtr network = virNetworkLookupByName(conn,
                                                       def->data.network.name);
        if (!network) {
233
            umlReportError(VIR_ERR_INTERNAL_ERROR,
234 235 236 237 238 239 240 241 242 243
                           _("Network '%s' not found"),
                           def->data.network.name);
            goto error;
        }
        bridge = virNetworkGetBridgeName(network);
        virNetworkFree(network);
        if (bridge == NULL) {
            goto error;
        }

244
        if (umlConnectTapDevice(def, bridge) < 0) {
245 246 247 248 249 250 251 252 253 254
            VIR_FREE(bridge);
            goto error;
        }

        /* ethNNN=tuntap,tapname,macaddr,gateway */
        virBufferVSprintf(&buf, "tuntap,%s", def->ifname);
        break;
    }

    case VIR_DOMAIN_NET_TYPE_BRIDGE:
255
        if (umlConnectTapDevice(def, def->data.bridge.brname) < 0)
256 257 258 259 260 261 262
            goto error;

        /* ethNNN=tuntap,tapname,macaddr,gateway */
        virBufferVSprintf(&buf, "tuntap,%s", def->ifname);
        break;

    case VIR_DOMAIN_NET_TYPE_INTERNAL:
263
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
264 265
                       _("internal networking type not supported"));
        goto error;
S
Stefan Berger 已提交
266 267

    case VIR_DOMAIN_NET_TYPE_DIRECT:
268
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
S
Stefan Berger 已提交
269 270 271 272 273
                       _("direct networking type not supported"));
        goto error;

    case VIR_DOMAIN_NET_TYPE_LAST:
        break;
274 275 276 277 278 279 280 281 282 283 284 285 286
    }

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

    if (def->type == VIR_DOMAIN_NET_TYPE_MCAST) {
        virBufferVSprintf(&buf, ",%s,%d",
                          def->data.socket.address,
                          def->data.socket.port);
    }

    if (virBufferError(&buf)) {
287
        virReportOOMError();
288 289 290 291 292 293
        return NULL;
    }

    return virBufferContentAndReset(&buf);

error:
294
    virBufferFreeAndReset(&buf);
295 296 297
    return NULL;
}

298
static char *
299
umlBuildCommandLineChr(virDomainChrDefPtr def,
300 301
                       const char *dev)
{
302
    char *ret = NULL;
303 304 305

    switch (def->type) {
    case VIR_DOMAIN_CHR_TYPE_NULL:
306
        if (virAsprintf(&ret, "%s%d=null", dev, def->target.port) < 0) {
307
            virReportOOMError();
308 309 310 311 312
            return NULL;
        }
        break;

    case VIR_DOMAIN_CHR_TYPE_PTY:
313
        if (virAsprintf(&ret, "%s%d=pts", dev, def->target.port) < 0) {
314
            virReportOOMError();
315 316 317 318 319
            return NULL;
        }
        break;

    case VIR_DOMAIN_CHR_TYPE_DEV:
320
        if (virAsprintf(&ret, "%s%d=tty:%s", dev, def->target.port,
321
                        def->data.file.path) < 0) {
322
            virReportOOMError();
323 324 325 326 327
            return NULL;
        }
        break;

    case VIR_DOMAIN_CHR_TYPE_STDIO:
328
        if (virAsprintf(&ret, "%s%d=fd:0,fd:1", dev, def->target.port) < 0) {
329
            virReportOOMError();
330 331 332 333 334 335
            return NULL;
        }
        break;

    case VIR_DOMAIN_CHR_TYPE_TCP:
        if (def->data.tcp.listen != 1) {
336 337
            umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("only TCP listen is supported for chr device"));
338 339 340
            return NULL;
        }

341
        if (virAsprintf(&ret, "%s%d=port:%s", dev, def->target.port,
342
                        def->data.tcp.service) < 0) {
343
            virReportOOMError();
344 345 346 347 348 349 350 351 352 353 354 355
            return NULL;
        }
        break;

    case VIR_DOMAIN_CHR_TYPE_FILE:
    case VIR_DOMAIN_CHR_TYPE_PIPE:
        /* XXX could open the file/pipe & just pass the FDs */

    case VIR_DOMAIN_CHR_TYPE_VC:
    case VIR_DOMAIN_CHR_TYPE_UDP:
    case VIR_DOMAIN_CHR_TYPE_UNIX:
    default:
356
        umlReportError(VIR_ERR_INTERNAL_ERROR,
357 358 359 360 361 362 363
                       _("unsupported chr device type %d"), def->type);
        break;
    }

    return ret;
}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
/*
 * Null-terminate the current argument and return a pointer to the next.
 * This should follow the same rules as the Linux kernel: arguments are
 * separated by spaces; arguments can be quoted with double quotes; double
 * quotes can't be escaped.
 */
static char *umlNextArg(char *args)
{
    int in_quote = 0;

    for (; *args; args++) {
        if (*args == ' ' && !in_quote) {
            *args++ = '\0';
            break;
        }
        if (*args == '"')
            in_quote = !in_quote;
    }

    while (*args == ' ')
        args++;

    return args;
}

389 390 391 392 393 394 395 396
/*
 * Constructs a argv suitable for launching uml with config defined
 * for a given virtual machine.
 */
int umlBuildCommandLine(virConnectPtr conn,
                        struct uml_driver *driver ATTRIBUTE_UNUSED,
                        virDomainObjPtr vm,
                        const char ***retargv,
397 398
                        const char ***retenv)
{
399 400 401 402 403 404 405
    int i, j;
    char memory[50];
    struct utsname ut;
    int qargc = 0, qarga = 0;
    const char **qargv = NULL;
    int qenvc = 0, qenva = 0;
    const char **qenv = NULL;
406
    char *cmdline = NULL;
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

    uname(&ut);

#define ADD_ARG_SPACE                                                   \
    do {                                                                \
        if (qargc == qarga) {                                           \
            qarga += 10;                                                \
            if (VIR_REALLOC_N(qargv, qarga) < 0)                        \
                goto no_memory;                                         \
        }                                                               \
    } while (0)

#define ADD_ARG(thisarg)                                                \
    do {                                                                \
        ADD_ARG_SPACE;                                                  \
        qargv[qargc++] = thisarg;                                       \
    } while (0)

#define ADD_ARG_LIT(thisarg)                                            \
    do {                                                                \
        ADD_ARG_SPACE;                                                  \
        if ((qargv[qargc++] = strdup(thisarg)) == NULL)                 \
            goto no_memory;                                             \
    } while (0)

#define ADD_ARG_PAIR(key,val)                                           \
    do {                                                                \
        char *arg;                                                      \
        ADD_ARG_SPACE;                                                  \
436
        if (virAsprintf(&arg, "%s=%s", key, val) < 0)                   \
437
            goto no_memory;                                             \
438
        qargv[qargc++] = arg;                                           \
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    } while (0)


#define ADD_ENV_SPACE                                                   \
    do {                                                                \
        if (qenvc == qenva) {                                           \
            qenva += 10;                                                \
            if (VIR_REALLOC_N(qenv, qenva) < 0)                         \
                goto no_memory;                                         \
        }                                                               \
    } while (0)

#define ADD_ENV(thisarg)                                                \
    do {                                                                \
        ADD_ENV_SPACE;                                                  \
        qenv[qenvc++] = thisarg;                                        \
    } while (0)

#define ADD_ENV_LIT(thisarg)                                            \
    do {                                                                \
        ADD_ENV_SPACE;                                                  \
        if ((qenv[qenvc++] = strdup(thisarg)) == NULL)                  \
            goto no_memory;                                             \
    } while (0)

#define ADD_ENV_COPY(envname)                                           \
    do {                                                                \
        char *val = getenv(envname);                                    \
        char *envval;                                                   \
        ADD_ENV_SPACE;                                                  \
        if (val != NULL) {                                              \
470
            if (virAsprintf(&envval, "%s=%s", envname, val) < 0)        \
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
                goto no_memory;                                         \
            qenv[qenvc++] = envval;                                     \
        }                                                               \
    } while (0)

    snprintf(memory, sizeof(memory), "%luK", vm->def->memory);

    ADD_ENV_LIT("LC_ALL=C");

    ADD_ENV_COPY("LD_PRELOAD");
    ADD_ENV_COPY("LD_LIBRARY_PATH");
    ADD_ENV_COPY("PATH");
    ADD_ENV_COPY("HOME");
    ADD_ENV_COPY("USER");
    ADD_ENV_COPY("LOGNAME");
    ADD_ENV_COPY("TMPDIR");

    ADD_ARG_LIT(vm->def->os.kernel);
    //ADD_ARG_PAIR("con0", "fd:0,fd:1");
    ADD_ARG_PAIR("mem", memory);
    ADD_ARG_PAIR("umid", vm->def->name);

    if (vm->def->os.root)
        ADD_ARG_PAIR("root", vm->def->os.root);

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        virDomainDiskDefPtr disk = vm->def->disks[i];

        if (!STRPREFIX(disk->dst, "ubd")) {
500
            umlReportError(VIR_ERR_INTERNAL_ERROR,
501 502 503 504 505 506 507
                           _("unsupported disk type '%s'"), disk->dst);
            goto error;
        }

        ADD_ARG_PAIR(disk->dst, disk->src);
    }

508 509 510 511 512 513 514
    for (i = 0 ; i < vm->def->nnets ; i++) {
        char *ret = umlBuildCommandLineNet(conn, vm->def->nets[i], i);
        if (!ret)
            goto error;
        ADD_ARG(ret);
    }

515 516 517
    for (i = 0 ; i < UML_MAX_CHAR_DEVICE ; i++) {
        char *ret;
        if (i == 0 && vm->def->console)
518
            ret = umlBuildCommandLineChr(vm->def->console, "con");
519
        else
520
            if (virAsprintf(&ret, "con%d=none", i) < 0)
521 522 523 524 525 526 527 528
                goto no_memory;
        ADD_ARG(ret);
    }

    for (i = 0 ; i < UML_MAX_CHAR_DEVICE ; i++) {
        virDomainChrDefPtr chr = NULL;
        char *ret;
        for (j = 0 ; j < vm->def->nserials ; j++)
529
            if (vm->def->serials[j]->target.port == i)
530 531
                chr = vm->def->serials[j];
        if (chr)
532
            ret = umlBuildCommandLineChr(chr, "ssl");
533
        else
534
            if (virAsprintf(&ret, "ssl%d=none", i) < 0)
535 536 537 538
                goto no_memory;
        ADD_ARG(ret);
    }

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
    if (vm->def->os.cmdline) {
        char *args, *next_arg;
        if ((cmdline = strdup(vm->def->os.cmdline)) == NULL)
            goto no_memory;

        args = cmdline;
        while (*args == ' ')
            args++;

        while (*args) {
            next_arg = umlNextArg(args);
            ADD_ARG_LIT(args);
            args = next_arg;
        }
    }

555 556 557 558 559 560 561 562
    ADD_ARG(NULL);
    ADD_ENV(NULL);

    *retargv = qargv;
    *retenv = qenv;
    return 0;

 no_memory:
563
    virReportOOMError();
564
 error:
565

566 567 568 569 570 571 572 573 574 575
    if (qargv) {
        for (i = 0 ; i < qargc ; i++)
            VIR_FREE((qargv)[i]);
        VIR_FREE(qargv);
    }
    if (qenv) {
        for (i = 0 ; i < qenvc ; i++)
            VIR_FREE((qenv)[i]);
        VIR_FREE(qenv);
    }
576
    VIR_FREE(cmdline);
577 578 579 580 581 582 583 584 585 586 587
    return -1;

#undef ADD_ARG
#undef ADD_ARG_LIT
#undef ADD_ARG_SPACE
#undef ADD_USBDISK
#undef ADD_ENV
#undef ADD_ENV_COPY
#undef ADD_ENV_LIT
#undef ADD_ENV_SPACE
}