uml_conf.c 13.8 KB
Newer Older
1 2 3
/*
 * uml_conf.c: UML driver configuration
 *
4
 * Copyright (C) 2006-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * 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 "uml_conf.h"
38
#include "viruuid.h"
39
#include "virbuffer.h"
40
#include "virconf.h"
41
#include "viralloc.h"
42
#include "nodeinfo.h"
43
#include "virlog.h"
44
#include "domain_nwfilter.h"
E
Eric Blake 已提交
45
#include "virfile.h"
46
#include "vircommand.h"
47
#include "virnetdevtap.h"
48
#include "virnodesuspend.h"
49
#include "virstring.h"
50

51
#define VIR_FROM_THIS VIR_FROM_UML
52

53

54 55 56 57
virCapsPtr umlCapsInit(void) {
    virCapsPtr caps;
    virCapsGuestPtr guest;

58
    if ((caps = virCapabilitiesNew(virArchFromHost(),
59
                                   0, 0)) == NULL)
60
        goto error;
61

62 63 64 65 66 67
    /* 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);
68
        VIR_WARN("Failed to query host NUMA topology, disabling NUMA capabilities");
69
    }
70

71 72 73
    if (virNodeSuspendGetTargetMask(&caps->host.powerMgmt) < 0)
        VIR_WARN("Failed to get host power management capabilities");

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

80 81
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "uml",
82
                                         caps->host.arch,
83 84 85 86
                                         NULL,
                                         NULL,
                                         0,
                                         NULL)) == NULL)
87
        goto error;
88 89 90 91 92 93 94

    if (virCapabilitiesAddGuestDomain(guest,
                                      "uml",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
95
        goto error;
96 97 98

    return caps;

99
 error:
100
    virObjectUnref(caps);
101 102 103 104
    return NULL;
}


105
static int
106
umlConnectTapDevice(virConnectPtr conn,
107
                    virDomainDefPtr vm,
108
                    virDomainNetDefPtr net,
109 110
                    const char *bridge)
{
E
Eric Blake 已提交
111
    bool template_ifname = false;
112
    int tapfd = -1;
113

114
    if (!net->ifname ||
E
Eric Blake 已提交
115
        STRPREFIX(net->ifname, VIR_NET_GENERATED_PREFIX) ||
116 117
        strchr(net->ifname, '%')) {
        VIR_FREE(net->ifname);
118 119
        if (VIR_STRDUP(net->ifname, VIR_NET_GENERATED_PREFIX "%d") < 0)
            goto error;
120
        /* avoid exposing vnet%d in getXMLDesc or error outputs */
E
Eric Blake 已提交
121
        template_ifname = true;
122 123
    }

124
    if (virNetDevTapCreateInBridgePort(bridge, &net->ifname, &net->mac,
125
                                       vm->uuid, &tapfd, 1,
126
                                       virDomainNetGetActualVirtPortProfile(net),
127
                                       virDomainNetGetActualVlan(net),
128 129
                                       VIR_NETDEV_TAP_CREATE_IFUP |
                                       VIR_NETDEV_TAP_CREATE_PERSIST) < 0) {
130 131
        if (template_ifname)
            VIR_FREE(net->ifname);
132 133 134
        goto error;
    }

135
    if (net->filter) {
136
        if (virDomainConfNWFilterInstantiate(conn, vm->uuid, net) < 0) {
137 138 139 140 141 142
            if (template_ifname)
                VIR_FREE(net->ifname);
            goto error;
        }
    }

143
    VIR_FORCE_CLOSE(tapfd);
144 145 146
    return 0;

error:
147
    VIR_FORCE_CLOSE(tapfd);
148 149 150 151 152
    return -1;
}

static char *
umlBuildCommandLineNet(virConnectPtr conn,
153
                       virDomainDefPtr vm,
154 155 156 157
                       virDomainNetDefPtr def,
                       int idx)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
158
    char macaddr[VIR_MAC_STRING_BUFLEN];
159 160 161

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

162
    virBufferAsprintf(&buf, "eth%d=", idx);
163 164 165 166 167 168 169 170 171

    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 */
172 173 174 175
        virBufferAddLit(&buf, "tuntap,");
        if (def->ifname) {
            virBufferAdd(&buf, def->ifname, -1);
        }
176
        if (def->data.ethernet.ipaddr) {
177
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
E
Eric Blake 已提交
178
                           _("IP address not supported for ethernet interface"));
179 180 181 182 183
            goto error;
        }
        break;

    case VIR_DOMAIN_NET_TYPE_SERVER:
184
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
185 186 187 188
                       _("TCP server networking type not supported"));
        goto error;

    case VIR_DOMAIN_NET_TYPE_CLIENT:
189
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
190 191 192 193 194 195 196 197 198 199 200 201 202 203
                       _("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) {
204
            virReportError(VIR_ERR_INTERNAL_ERROR,
205 206 207 208 209 210 211 212 213 214
                           _("Network '%s' not found"),
                           def->data.network.name);
            goto error;
        }
        bridge = virNetworkGetBridgeName(network);
        virNetworkFree(network);
        if (bridge == NULL) {
            goto error;
        }

215
        if (umlConnectTapDevice(conn, vm, def, bridge) < 0) {
216 217 218 219 220
            VIR_FREE(bridge);
            goto error;
        }

        /* ethNNN=tuntap,tapname,macaddr,gateway */
221
        virBufferAsprintf(&buf, "tuntap,%s", def->ifname);
222 223 224 225
        break;
    }

    case VIR_DOMAIN_NET_TYPE_BRIDGE:
226 227
        if (umlConnectTapDevice(conn, vm, def,
                                def->data.bridge.brname) < 0)
228 229 230
            goto error;

        /* ethNNN=tuntap,tapname,macaddr,gateway */
231
        virBufferAsprintf(&buf, "tuntap,%s", def->ifname);
232 233 234
        break;

    case VIR_DOMAIN_NET_TYPE_INTERNAL:
235
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
236 237
                       _("internal networking type not supported"));
        goto error;
S
Stefan Berger 已提交
238 239

    case VIR_DOMAIN_NET_TYPE_DIRECT:
240
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
S
Stefan Berger 已提交
241 242 243
                       _("direct networking type not supported"));
        goto error;

244
    case VIR_DOMAIN_NET_TYPE_HOSTDEV:
245
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
246 247 248
                       _("hostdev networking type not supported"));
        goto error;

S
Stefan Berger 已提交
249 250
    case VIR_DOMAIN_NET_TYPE_LAST:
        break;
251 252
    }

253
    if (def->script) {
254
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
255 256 257 258
                       _("interface script execution not supported by this driver"));
        goto error;
    }

259
    virBufferAsprintf(&buf, ",%s", virMacAddrFormat(&def->mac, macaddr));
260 261

    if (def->type == VIR_DOMAIN_NET_TYPE_MCAST) {
262
        virBufferAsprintf(&buf, ",%s,%d",
263 264 265 266 267
                          def->data.socket.address,
                          def->data.socket.port);
    }

    if (virBufferError(&buf)) {
268
        virReportOOMError();
269 270 271 272 273 274
        return NULL;
    }

    return virBufferContentAndReset(&buf);

error:
275
    virBufferFreeAndReset(&buf);
276 277 278
    return NULL;
}

279
static char *
280
umlBuildCommandLineChr(virDomainChrDefPtr def,
281
                       const char *dev,
D
Daniel P. Berrange 已提交
282
                       virCommandPtr cmd)
283
{
284
    char *ret = NULL;
285

286
    switch (def->source.type) {
287
    case VIR_DOMAIN_CHR_TYPE_NULL:
288
        if (virAsprintf(&ret, "%s%d=null", dev, def->target.port) < 0)
289 290 291 292
            return NULL;
        break;

    case VIR_DOMAIN_CHR_TYPE_PTY:
293
        if (virAsprintf(&ret, "%s%d=pts", dev, def->target.port) < 0)
294 295 296 297
            return NULL;
        break;

    case VIR_DOMAIN_CHR_TYPE_DEV:
298
        if (virAsprintf(&ret, "%s%d=tty:%s", dev, def->target.port,
299
                        def->source.data.file.path) < 0)
300 301 302 303
            return NULL;
        break;

    case VIR_DOMAIN_CHR_TYPE_STDIO:
304
        if (virAsprintf(&ret, "%s%d=fd:0,fd:1", dev, def->target.port) < 0)
305 306 307 308
            return NULL;
        break;

    case VIR_DOMAIN_CHR_TYPE_TCP:
309
        if (def->source.data.tcp.listen != 1) {
310
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
311
                           _("only TCP listen is supported for chr device"));
312 313 314
            return NULL;
        }

315
        if (virAsprintf(&ret, "%s%d=port:%s", dev, def->target.port,
316
                        def->source.data.tcp.service) < 0)
317 318 319 320
            return NULL;
        break;

    case VIR_DOMAIN_CHR_TYPE_FILE:
321 322 323
         {
            int fd_out;

324
            if ((fd_out = open(def->source.data.file.path,
325 326 327
                               O_WRONLY | O_APPEND | O_CREAT, 0660)) < 0) {
                virReportSystemError(errno,
                                     _("failed to open chardev file: %s"),
328
                                     def->source.data.file.path);
329 330 331
                return NULL;
            }
            if (virAsprintf(&ret, "%s%d=null,fd:%d", dev, def->target.port, fd_out) < 0) {
332
                VIR_FORCE_CLOSE(fd_out);
333 334
                return NULL;
            }
D
Daniel P. Berrange 已提交
335
            virCommandTransferFD(cmd, fd_out);
336 337 338 339 340
        }
        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. */
341 342 343 344 345

    case VIR_DOMAIN_CHR_TYPE_VC:
    case VIR_DOMAIN_CHR_TYPE_UDP:
    case VIR_DOMAIN_CHR_TYPE_UNIX:
    default:
346
        virReportError(VIR_ERR_INTERNAL_ERROR,
347
                       _("unsupported chr device type %d"), def->source.type);
348 349 350 351 352 353
        break;
    }

    return ret;
}

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
/*
 * 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;
}

379 380 381 382
/*
 * Constructs a argv suitable for launching uml with config defined
 * for a given virtual machine.
 */
D
Daniel P. Berrange 已提交
383 384 385
virCommandPtr umlBuildCommandLine(virConnectPtr conn,
                                  struct uml_driver *driver,
                                  virDomainObjPtr vm)
386
{
387
    int i, j;
D
Daniel P. Berrange 已提交
388
    virCommandPtr cmd;
389

D
Daniel P. Berrange 已提交
390 391 392 393 394
    cmd = virCommandNew(vm->def->os.kernel);

    virCommandAddEnvPassCommon(cmd);

    //virCommandAddArgPair(cmd, "con0", "fd:0,fd:1");
395
    virCommandAddArgFormat(cmd, "mem=%lluK", vm->def->mem.cur_balloon);
D
Daniel P. Berrange 已提交
396 397
    virCommandAddArgPair(cmd, "umid", vm->def->name);
    virCommandAddArgPair(cmd, "uml_dir", driver->monitorDir);
398 399

    if (vm->def->os.root)
D
Daniel P. Berrange 已提交
400
        virCommandAddArgPair(cmd, "root", vm->def->os.root);
401

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

        if (!STRPREFIX(disk->dst, "ubd")) {
406
            virReportError(VIR_ERR_INTERNAL_ERROR,
407 408 409 410
                           _("unsupported disk type '%s'"), disk->dst);
            goto error;
        }

D
Daniel P. Berrange 已提交
411
        virCommandAddArgPair(cmd, disk->dst, disk->src);
412 413
    }

414
    for (i = 0; i < vm->def->nnets; i++) {
415
        char *ret = umlBuildCommandLineNet(conn, vm->def, vm->def->nets[i], i);
416 417
        if (!ret)
            goto error;
D
Daniel P. Berrange 已提交
418 419
        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
420 421
    }

422
    for (i = 0; i < UML_MAX_CHAR_DEVICE; i++) {
423
        virDomainChrDefPtr chr = NULL;
424
        char *ret = NULL;
425
        for (j = 0; j < vm->def->nconsoles; j++)
426 427 428 429
            if (vm->def->consoles[j]->target.port == i)
                chr = vm->def->consoles[j];
        if (chr)
            ret = umlBuildCommandLineChr(chr, "con", cmd);
430
        if (!ret)
431
            if (virAsprintf(&ret, "con%d=none", i) < 0)
432
                goto error;
D
Daniel P. Berrange 已提交
433 434
        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
435 436
    }

437
    for (i = 0; i < UML_MAX_CHAR_DEVICE; i++) {
438
        virDomainChrDefPtr chr = NULL;
439
        char *ret = NULL;
440
        for (j = 0; j < vm->def->nserials; j++)
441
            if (vm->def->serials[j]->target.port == i)
442 443
                chr = vm->def->serials[j];
        if (chr)
D
Daniel P. Berrange 已提交
444
            ret = umlBuildCommandLineChr(chr, "ssl", cmd);
445
        if (!ret)
446
            if (virAsprintf(&ret, "ssl%d=none", i) < 0)
447
                goto error;
D
Daniel P. Berrange 已提交
448 449 450

        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
451 452
    }

453 454
    if (vm->def->os.cmdline) {
        char *args, *next_arg;
D
Daniel P. Berrange 已提交
455
        char *cmdline;
456 457
        if (VIR_STRDUP(cmdline, vm->def->os.cmdline) < 0)
            goto error;
458 459 460 461 462 463 464

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

        while (*args) {
            next_arg = umlNextArg(args);
D
Daniel P. Berrange 已提交
465
            virCommandAddArg(cmd, args);
466 467
            args = next_arg;
        }
D
Daniel P. Berrange 已提交
468
        VIR_FREE(cmdline);
469 470
    }

D
Daniel P. Berrange 已提交
471
    return cmd;
472 473

 error:
D
Daniel P. Berrange 已提交
474 475
    virCommandFree(cmd);
    return NULL;
476
}