uml_conf.c 15.1 KB
Newer Older
1 2 3
/*
 * uml_conf.c: UML driver configuration
 *
4
 * Copyright (C) 2006-2011 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
#include "domain_nwfilter.h"
50
#include "files.h"
D
Daniel P. Berrange 已提交
51
#include "command.h"
52

53
#define VIR_FROM_THIS VIR_FROM_UML
54

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

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)
68
        goto error;
69

70 71 72 73 74 75 76 77
    /* 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");
    }
78

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

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

    if (virCapabilitiesAddGuestDomain(guest,
                                      "uml",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
101
        goto error;
102

103 104
    caps->defaultConsoleTargetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_UML;

105 106
    return caps;

107
 error:
108 109 110 111 112
    virCapabilitiesFree(caps);
    return NULL;
}


113
static int
114 115
umlConnectTapDevice(virConnectPtr conn,
                    virDomainNetDefPtr net,
116 117
                    const char *bridge)
{
118 119
    brControl *brctl = NULL;
    int template_ifname = 0;
120
    int err;
121
    unsigned char tapmac[VIR_MAC_BUFLEN];
122 123

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

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

139 140 141 142 143 144 145
    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,
146
                        true,
147
                        NULL))) {
D
Doug Goldstein 已提交
148
        if (err == ENOTSUP) {
149
            /* In this particular case, give a better diagnostic. */
150
            umlReportError(VIR_ERR_INTERNAL_ERROR,
151 152
                           _("Failed to add tap interface to bridge. "
                             "%s is not a bridge device"), bridge);
153 154 155 156 157
        } 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."));
158
        } else if (template_ifname) {
159
            virReportSystemError(err,
160 161
                                 _("Failed to add tap interface to bridge '%s'"),
                                 bridge);
162
        } else {
163
            virReportSystemError(err,
164 165
                                 _("Failed to add tap interface '%s' to bridge '%s'"),
                                 net->ifname, bridge);
166
        }
167 168
        if (template_ifname)
            VIR_FREE(net->ifname);
169 170 171
        goto error;
    }

172 173 174 175 176 177 178 179
    if (net->filter) {
        if (virDomainConfNWFilterInstantiate(conn, net)) {
            if (template_ifname)
                VIR_FREE(net->ifname);
            goto error;
        }
    }

180 181 182 183 184
    brShutdown(brctl);

    return 0;

no_memory:
185
    virReportOOMError();
186 187 188 189 190 191 192 193 194 195 196 197 198 199
error:
    brShutdown(brctl);
    return -1;
}

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

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

200
    virBufferAsprintf(&buf, "eth%d=", idx);
201 202 203 204 205 206 207 208 209 210 211

    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) {
212
            umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
213 214 215 216
                           _("IP address not supported for ethernet inteface"));
            goto error;
        }
        if (def->data.ethernet.script) {
217
            umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
218 219 220 221 222 223
                           _("script execution not supported for ethernet inteface"));
            goto error;
        }
        break;

    case VIR_DOMAIN_NET_TYPE_SERVER:
224
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
225 226 227 228
                       _("TCP server networking type not supported"));
        goto error;

    case VIR_DOMAIN_NET_TYPE_CLIENT:
229
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
230 231 232 233 234 235 236 237 238 239 240 241 242 243
                       _("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) {
244
            umlReportError(VIR_ERR_INTERNAL_ERROR,
245 246 247 248 249 250 251 252 253 254
                           _("Network '%s' not found"),
                           def->data.network.name);
            goto error;
        }
        bridge = virNetworkGetBridgeName(network);
        virNetworkFree(network);
        if (bridge == NULL) {
            goto error;
        }

255
        if (umlConnectTapDevice(conn, def, bridge) < 0) {
256 257 258 259 260
            VIR_FREE(bridge);
            goto error;
        }

        /* ethNNN=tuntap,tapname,macaddr,gateway */
261
        virBufferAsprintf(&buf, "tuntap,%s", def->ifname);
262 263 264 265
        break;
    }

    case VIR_DOMAIN_NET_TYPE_BRIDGE:
266
        if (umlConnectTapDevice(conn, def, def->data.bridge.brname) < 0)
267 268 269
            goto error;

        /* ethNNN=tuntap,tapname,macaddr,gateway */
270
        virBufferAsprintf(&buf, "tuntap,%s", def->ifname);
271 272 273
        break;

    case VIR_DOMAIN_NET_TYPE_INTERNAL:
274
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
275 276
                       _("internal networking type not supported"));
        goto error;
S
Stefan Berger 已提交
277 278

    case VIR_DOMAIN_NET_TYPE_DIRECT:
279
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
S
Stefan Berger 已提交
280 281 282 283 284
                       _("direct networking type not supported"));
        goto error;

    case VIR_DOMAIN_NET_TYPE_LAST:
        break;
285 286
    }

287
    virBufferAsprintf(&buf, ",%02x:%02x:%02x:%02x:%02x:%02x",
288 289 290 291
                      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) {
292
        virBufferAsprintf(&buf, ",%s,%d",
293 294 295 296 297
                          def->data.socket.address,
                          def->data.socket.port);
    }

    if (virBufferError(&buf)) {
298
        virReportOOMError();
299 300 301 302 303 304
        return NULL;
    }

    return virBufferContentAndReset(&buf);

error:
305
    virBufferFreeAndReset(&buf);
306 307 308
    return NULL;
}

309
static char *
310
umlBuildCommandLineChr(virDomainChrDefPtr def,
311
                       const char *dev,
D
Daniel P. Berrange 已提交
312
                       virCommandPtr cmd)
313
{
314
    char *ret = NULL;
315

316
    switch (def->source.type) {
317
    case VIR_DOMAIN_CHR_TYPE_NULL:
318
        if (virAsprintf(&ret, "%s%d=null", dev, def->target.port) < 0) {
319
            virReportOOMError();
320 321 322 323 324
            return NULL;
        }
        break;

    case VIR_DOMAIN_CHR_TYPE_PTY:
325
        if (virAsprintf(&ret, "%s%d=pts", dev, def->target.port) < 0) {
326
            virReportOOMError();
327 328 329 330 331
            return NULL;
        }
        break;

    case VIR_DOMAIN_CHR_TYPE_DEV:
332
        if (virAsprintf(&ret, "%s%d=tty:%s", dev, def->target.port,
333
                        def->source.data.file.path) < 0) {
334
            virReportOOMError();
335 336 337 338 339
            return NULL;
        }
        break;

    case VIR_DOMAIN_CHR_TYPE_STDIO:
340
        if (virAsprintf(&ret, "%s%d=fd:0,fd:1", dev, def->target.port) < 0) {
341
            virReportOOMError();
342 343 344 345 346
            return NULL;
        }
        break;

    case VIR_DOMAIN_CHR_TYPE_TCP:
347
        if (def->source.data.tcp.listen != 1) {
348 349
            umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("only TCP listen is supported for chr device"));
350 351 352
            return NULL;
        }

353
        if (virAsprintf(&ret, "%s%d=port:%s", dev, def->target.port,
354
                        def->source.data.tcp.service) < 0) {
355
            virReportOOMError();
356 357 358 359 360
            return NULL;
        }
        break;

    case VIR_DOMAIN_CHR_TYPE_FILE:
361 362 363
         {
            int fd_out;

364
            if ((fd_out = open(def->source.data.file.path,
365 366 367
                               O_WRONLY | O_APPEND | O_CREAT, 0660)) < 0) {
                virReportSystemError(errno,
                                     _("failed to open chardev file: %s"),
368
                                     def->source.data.file.path);
369 370 371 372
                return NULL;
            }
            if (virAsprintf(&ret, "%s%d=null,fd:%d", dev, def->target.port, fd_out) < 0) {
                virReportOOMError();
373
                VIR_FORCE_CLOSE(fd_out);
374 375
                return NULL;
            }
D
Daniel P. Berrange 已提交
376
            virCommandTransferFD(cmd, fd_out);
377 378 379 380 381
        }
        break;
   case VIR_DOMAIN_CHR_TYPE_PIPE:
        /* XXX could open the pipe & just pass the FDs. Be wary of
         * the effects of blocking I/O, though. */
382 383 384 385 386

    case VIR_DOMAIN_CHR_TYPE_VC:
    case VIR_DOMAIN_CHR_TYPE_UDP:
    case VIR_DOMAIN_CHR_TYPE_UNIX:
    default:
387
        umlReportError(VIR_ERR_INTERNAL_ERROR,
388
                       _("unsupported chr device type %d"), def->source.type);
389 390 391 392 393 394
        break;
    }

    return ret;
}

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
/*
 * 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;
}

420 421 422 423
/*
 * Constructs a argv suitable for launching uml with config defined
 * for a given virtual machine.
 */
D
Daniel P. Berrange 已提交
424 425 426
virCommandPtr umlBuildCommandLine(virConnectPtr conn,
                                  struct uml_driver *driver,
                                  virDomainObjPtr vm)
427
{
428 429
    int i, j;
    struct utsname ut;
D
Daniel P. Berrange 已提交
430
    virCommandPtr cmd;
431 432 433

    uname(&ut);

D
Daniel P. Berrange 已提交
434 435 436 437 438 439 440 441
    cmd = virCommandNew(vm->def->os.kernel);

    virCommandAddEnvPassCommon(cmd);

    //virCommandAddArgPair(cmd, "con0", "fd:0,fd:1");
    virCommandAddArgFormat(cmd, "mem=%luK", vm->def->mem.cur_balloon);
    virCommandAddArgPair(cmd, "umid", vm->def->name);
    virCommandAddArgPair(cmd, "uml_dir", driver->monitorDir);
442 443

    if (vm->def->os.root)
D
Daniel P. Berrange 已提交
444
        virCommandAddArgPair(cmd, "root", vm->def->os.root);
445 446 447 448 449

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

        if (!STRPREFIX(disk->dst, "ubd")) {
450
            umlReportError(VIR_ERR_INTERNAL_ERROR,
451 452 453 454
                           _("unsupported disk type '%s'"), disk->dst);
            goto error;
        }

D
Daniel P. Berrange 已提交
455
        virCommandAddArgPair(cmd, disk->dst, disk->src);
456 457
    }

458 459 460 461
    for (i = 0 ; i < vm->def->nnets ; i++) {
        char *ret = umlBuildCommandLineNet(conn, vm->def->nets[i], i);
        if (!ret)
            goto error;
D
Daniel P. Berrange 已提交
462 463
        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
464 465
    }

466
    for (i = 0 ; i < UML_MAX_CHAR_DEVICE ; i++) {
467
        char *ret = NULL;
468
        if (i == 0 && vm->def->console)
D
Daniel P. Berrange 已提交
469
            ret = umlBuildCommandLineChr(vm->def->console, "con", cmd);
470
        if (!ret)
471
            if (virAsprintf(&ret, "con%d=none", i) < 0)
472
                goto no_memory;
D
Daniel P. Berrange 已提交
473 474
        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
475 476 477 478
    }

    for (i = 0 ; i < UML_MAX_CHAR_DEVICE ; i++) {
        virDomainChrDefPtr chr = NULL;
479
        char *ret = NULL;
480
        for (j = 0 ; j < vm->def->nserials ; j++)
481
            if (vm->def->serials[j]->target.port == i)
482 483
                chr = vm->def->serials[j];
        if (chr)
D
Daniel P. Berrange 已提交
484
            ret = umlBuildCommandLineChr(chr, "ssl", cmd);
485
        if (!ret)
486
            if (virAsprintf(&ret, "ssl%d=none", i) < 0)
487
                goto no_memory;
D
Daniel P. Berrange 已提交
488 489 490

        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
491 492
    }

493 494
    if (vm->def->os.cmdline) {
        char *args, *next_arg;
D
Daniel P. Berrange 已提交
495
        char *cmdline;
496 497 498 499 500 501 502 503 504
        if ((cmdline = strdup(vm->def->os.cmdline)) == NULL)
            goto no_memory;

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

        while (*args) {
            next_arg = umlNextArg(args);
D
Daniel P. Berrange 已提交
505
            virCommandAddArg(cmd, args);
506 507
            args = next_arg;
        }
D
Daniel P. Berrange 已提交
508
        VIR_FREE(cmdline);
509 510
    }

D
Daniel P. Berrange 已提交
511
    return cmd;
512 513

 no_memory:
514
    virReportOOMError();
515
 error:
516

D
Daniel P. Berrange 已提交
517 518
    virCommandFree(cmd);
    return NULL;
519
}