uml_conf.c 13.9 KB
Newer Older
1 2 3
/*
 * uml_conf.c: UML driver configuration
 *
4
 * Copyright (C) 2006-2014 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
VIR_LOG_INIT("uml.uml_conf");
54

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

60
    if ((caps = virCapabilitiesNew(virArchFromHost(),
61
                                   0, 0)) == NULL)
62
        goto error;
63

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

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

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

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

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

    return caps;

101
 error:
102
    virObjectUnref(caps);
103 104 105 106
    return NULL;
}


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

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

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

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

145
    VIR_FORCE_CLOSE(tapfd);
146 147 148
    return 0;

error:
149
    VIR_FORCE_CLOSE(tapfd);
150 151 152 153 154
    return -1;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Stefan Berger 已提交
251 252
    case VIR_DOMAIN_NET_TYPE_LAST:
        break;
253 254
    }

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

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

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

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

    return virBufferContentAndReset(&buf);

error:
277
    virBufferFreeAndReset(&buf);
278 279 280
    return NULL;
}

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

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

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

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

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

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

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

    case VIR_DOMAIN_CHR_TYPE_FILE:
323 324 325
         {
            int fd_out;

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

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

    return ret;
}

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

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

D
Daniel P. Berrange 已提交
393 394 395 396 397
    cmd = virCommandNew(vm->def->os.kernel);

    virCommandAddEnvPassCommon(cmd);

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

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

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

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

414
        virCommandAddArgPair(cmd, disk->dst, virDomainDiskGetSource(disk));
415 416
    }

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

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

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

        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
454 455
    }

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

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

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

D
Daniel P. Berrange 已提交
474
    return cmd;
475 476

 error:
D
Daniel P. Berrange 已提交
477 478
    virCommandFree(cmd);
    return NULL;
479
}