uml_conf.c 14.6 KB
Newer Older
1 2 3
/*
 * uml_conf.c: UML driver configuration
 *
4
 * Copyright (C) 2006-2012 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
 * 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 <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"
44
#include "nodeinfo.h"
45
#include "logging.h"
46
#include "domain_nwfilter.h"
E
Eric Blake 已提交
47
#include "virfile.h"
D
Daniel P. Berrange 已提交
48
#include "command.h"
49
#include "virnetdevtap.h"
50 51
#include "virnodesuspend.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

static int umlDefaultConsoleType(const char *ostype ATTRIBUTE_UNUSED)
{
    return VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_UML;
}


65 66 67 68 69 70 71 72 73 74
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)
75
        goto error;
76

77 78 79 80 81 82
    /* 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);
83
        VIR_WARN("Failed to query host NUMA topology, disabling NUMA capabilities");
84
    }
85

86 87 88
    if (virNodeSuspendGetTargetMask(&caps->host.powerMgmt) < 0)
        VIR_WARN("Failed to get host power management capabilities");

89
    if (virGetHostUUID(caps->host.host_uuid)) {
90
        virReportError(VIR_ERR_INTERNAL_ERROR,
91 92 93 94
                       "%s", _("cannot get the host uuid"));
        goto error;
    }

95 96 97 98 99 100 101 102
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "uml",
                                         utsname.machine,
                                         STREQ(utsname.machine, "x86_64") ? 64 : 32,
                                         NULL,
                                         NULL,
                                         0,
                                         NULL)) == NULL)
103
        goto error;
104 105 106 107 108 109 110

    if (virCapabilitiesAddGuestDomain(guest,
                                      "uml",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
111
        goto error;
112

113
    caps->defaultConsoleTargetType = umlDefaultConsoleType;
114

115 116
    return caps;

117
 error:
118 119 120 121 122
    virCapabilitiesFree(caps);
    return NULL;
}


123
static int
124
umlConnectTapDevice(virConnectPtr conn,
125
                    virDomainDefPtr vm,
126
                    virDomainNetDefPtr net,
127 128
                    const char *bridge)
{
E
Eric Blake 已提交
129
    bool template_ifname = false;
130

131
    if (!net->ifname ||
E
Eric Blake 已提交
132
        STRPREFIX(net->ifname, VIR_NET_GENERATED_PREFIX) ||
133 134
        strchr(net->ifname, '%')) {
        VIR_FREE(net->ifname);
E
Eric Blake 已提交
135
        if (!(net->ifname = strdup(VIR_NET_GENERATED_PREFIX "%d")))
136
            goto no_memory;
137
        /* avoid exposing vnet%d in getXMLDesc or error outputs */
E
Eric Blake 已提交
138
        template_ifname = true;
139 140
    }

141
    if (virNetDevTapCreateInBridgePort(bridge, &net->ifname, &net->mac,
142
                                       vm->uuid, NULL,
143 144
                                       virDomainNetGetActualVirtPortProfile(net),
                                       VIR_NETDEV_TAP_CREATE_IFUP) < 0) {
145 146
        if (template_ifname)
            VIR_FREE(net->ifname);
147 148 149
        goto error;
    }

150
    if (net->filter) {
151
        if (virDomainConfNWFilterInstantiate(conn, vm->uuid, net) < 0) {
152 153 154 155 156 157
            if (template_ifname)
                VIR_FREE(net->ifname);
            goto error;
        }
    }

158 159 160
    return 0;

no_memory:
161
    virReportOOMError();
162 163 164 165 166 167
error:
    return -1;
}

static char *
umlBuildCommandLineNet(virConnectPtr conn,
168
                       virDomainDefPtr vm,
169 170 171 172 173 174 175
                       virDomainNetDefPtr def,
                       int idx)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

176
    virBufferAsprintf(&buf, "eth%d=", idx);
177 178 179 180 181 182 183 184 185

    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 */
186 187 188 189
        virBufferAddLit(&buf, "tuntap,");
        if (def->ifname) {
            virBufferAdd(&buf, def->ifname, -1);
        }
190
        if (def->data.ethernet.ipaddr) {
191
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
E
Eric Blake 已提交
192
                           _("IP address not supported for ethernet interface"));
193 194 195 196 197
            goto error;
        }
        break;

    case VIR_DOMAIN_NET_TYPE_SERVER:
198
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
199 200 201 202
                       _("TCP server networking type not supported"));
        goto error;

    case VIR_DOMAIN_NET_TYPE_CLIENT:
203
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
204 205 206 207 208 209 210 211 212 213 214 215 216 217
                       _("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) {
218
            virReportError(VIR_ERR_INTERNAL_ERROR,
219 220 221 222 223 224 225 226 227 228
                           _("Network '%s' not found"),
                           def->data.network.name);
            goto error;
        }
        bridge = virNetworkGetBridgeName(network);
        virNetworkFree(network);
        if (bridge == NULL) {
            goto error;
        }

229
        if (umlConnectTapDevice(conn, vm, def, bridge) < 0) {
230 231 232 233 234
            VIR_FREE(bridge);
            goto error;
        }

        /* ethNNN=tuntap,tapname,macaddr,gateway */
235
        virBufferAsprintf(&buf, "tuntap,%s", def->ifname);
236 237 238 239
        break;
    }

    case VIR_DOMAIN_NET_TYPE_BRIDGE:
240 241
        if (umlConnectTapDevice(conn, vm, def,
                                def->data.bridge.brname) < 0)
242 243 244
            goto error;

        /* ethNNN=tuntap,tapname,macaddr,gateway */
245
        virBufferAsprintf(&buf, "tuntap,%s", def->ifname);
246 247 248
        break;

    case VIR_DOMAIN_NET_TYPE_INTERNAL:
249
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
250 251
                       _("internal networking type not supported"));
        goto error;
S
Stefan Berger 已提交
252 253

    case VIR_DOMAIN_NET_TYPE_DIRECT:
254
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
S
Stefan Berger 已提交
255 256 257
                       _("direct networking type not supported"));
        goto error;

258
    case VIR_DOMAIN_NET_TYPE_HOSTDEV:
259
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
260 261 262
                       _("hostdev networking type not supported"));
        goto error;

S
Stefan Berger 已提交
263 264
    case VIR_DOMAIN_NET_TYPE_LAST:
        break;
265 266
    }

267
    if (def->script) {
268
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
269 270 271 272
                       _("interface script execution not supported by this driver"));
        goto error;
    }

273
    virBufferAsprintf(&buf, ",%02x:%02x:%02x:%02x:%02x:%02x",
274 275
                      def->mac.addr[0], def->mac.addr[1], def->mac.addr[2],
                      def->mac.addr[3], def->mac.addr[4], def->mac.addr[5]);
276 277

    if (def->type == VIR_DOMAIN_NET_TYPE_MCAST) {
278
        virBufferAsprintf(&buf, ",%s,%d",
279 280 281 282 283
                          def->data.socket.address,
                          def->data.socket.port);
    }

    if (virBufferError(&buf)) {
284
        virReportOOMError();
285 286 287 288 289 290
        return NULL;
    }

    return virBufferContentAndReset(&buf);

error:
291
    virBufferFreeAndReset(&buf);
292 293 294
    return NULL;
}

295
static char *
296
umlBuildCommandLineChr(virDomainChrDefPtr def,
297
                       const char *dev,
D
Daniel P. Berrange 已提交
298
                       virCommandPtr cmd)
299
{
300
    char *ret = NULL;
301

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

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

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

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

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

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

    case VIR_DOMAIN_CHR_TYPE_FILE:
347 348 349
         {
            int fd_out;

350
            if ((fd_out = open(def->source.data.file.path,
351 352 353
                               O_WRONLY | O_APPEND | O_CREAT, 0660)) < 0) {
                virReportSystemError(errno,
                                     _("failed to open chardev file: %s"),
354
                                     def->source.data.file.path);
355 356 357 358
                return NULL;
            }
            if (virAsprintf(&ret, "%s%d=null,fd:%d", dev, def->target.port, fd_out) < 0) {
                virReportOOMError();
359
                VIR_FORCE_CLOSE(fd_out);
360 361
                return NULL;
            }
D
Daniel P. Berrange 已提交
362
            virCommandTransferFD(cmd, fd_out);
363 364 365 366 367
        }
        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. */
368 369 370 371 372

    case VIR_DOMAIN_CHR_TYPE_VC:
    case VIR_DOMAIN_CHR_TYPE_UDP:
    case VIR_DOMAIN_CHR_TYPE_UNIX:
    default:
373
        virReportError(VIR_ERR_INTERNAL_ERROR,
374
                       _("unsupported chr device type %d"), def->source.type);
375 376 377 378 379 380
        break;
    }

    return ret;
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
/*
 * 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;
}

406 407 408 409
/*
 * Constructs a argv suitable for launching uml with config defined
 * for a given virtual machine.
 */
D
Daniel P. Berrange 已提交
410 411 412
virCommandPtr umlBuildCommandLine(virConnectPtr conn,
                                  struct uml_driver *driver,
                                  virDomainObjPtr vm)
413
{
414 415
    int i, j;
    struct utsname ut;
D
Daniel P. Berrange 已提交
416
    virCommandPtr cmd;
417 418 419

    uname(&ut);

D
Daniel P. Berrange 已提交
420 421 422 423 424
    cmd = virCommandNew(vm->def->os.kernel);

    virCommandAddEnvPassCommon(cmd);

    //virCommandAddArgPair(cmd, "con0", "fd:0,fd:1");
425
    virCommandAddArgFormat(cmd, "mem=%lluK", vm->def->mem.cur_balloon);
D
Daniel P. Berrange 已提交
426 427
    virCommandAddArgPair(cmd, "umid", vm->def->name);
    virCommandAddArgPair(cmd, "uml_dir", driver->monitorDir);
428 429

    if (vm->def->os.root)
D
Daniel P. Berrange 已提交
430
        virCommandAddArgPair(cmd, "root", vm->def->os.root);
431 432 433 434 435

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

        if (!STRPREFIX(disk->dst, "ubd")) {
436
            virReportError(VIR_ERR_INTERNAL_ERROR,
437 438 439 440
                           _("unsupported disk type '%s'"), disk->dst);
            goto error;
        }

D
Daniel P. Berrange 已提交
441
        virCommandAddArgPair(cmd, disk->dst, disk->src);
442 443
    }

444
    for (i = 0 ; i < vm->def->nnets ; i++) {
445
        char *ret = umlBuildCommandLineNet(conn, vm->def, vm->def->nets[i], i);
446 447
        if (!ret)
            goto error;
D
Daniel P. Berrange 已提交
448 449
        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
450 451
    }

452
    for (i = 0 ; i < UML_MAX_CHAR_DEVICE ; i++) {
453
        virDomainChrDefPtr chr = NULL;
454
        char *ret = NULL;
455 456 457 458 459
        for (j = 0 ; j < vm->def->nconsoles ; j++)
            if (vm->def->consoles[j]->target.port == i)
                chr = vm->def->consoles[j];
        if (chr)
            ret = umlBuildCommandLineChr(chr, "con", cmd);
460
        if (!ret)
461
            if (virAsprintf(&ret, "con%d=none", i) < 0)
462
                goto no_memory;
D
Daniel P. Berrange 已提交
463 464
        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
465 466 467 468
    }

    for (i = 0 ; i < UML_MAX_CHAR_DEVICE ; i++) {
        virDomainChrDefPtr chr = NULL;
469
        char *ret = NULL;
470
        for (j = 0 ; j < vm->def->nserials ; j++)
471
            if (vm->def->serials[j]->target.port == i)
472 473
                chr = vm->def->serials[j];
        if (chr)
D
Daniel P. Berrange 已提交
474
            ret = umlBuildCommandLineChr(chr, "ssl", cmd);
475
        if (!ret)
476
            if (virAsprintf(&ret, "ssl%d=none", i) < 0)
477
                goto no_memory;
D
Daniel P. Berrange 已提交
478 479 480

        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
481 482
    }

483 484
    if (vm->def->os.cmdline) {
        char *args, *next_arg;
D
Daniel P. Berrange 已提交
485
        char *cmdline;
486 487 488 489 490 491 492 493 494
        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 已提交
495
            virCommandAddArg(cmd, args);
496 497
            args = next_arg;
        }
D
Daniel P. Berrange 已提交
498
        VIR_FREE(cmdline);
499 500
    }

D
Daniel P. Berrange 已提交
501
    return cmd;
502 503

 no_memory:
504
    virReportOOMError();
505
 error:
506

D
Daniel P. Berrange 已提交
507 508
    virCommandFree(cmd);
    return NULL;
509
}