uml_conf.c 14.3 KB
Newer Older
1 2 3
/*
 * uml_conf.c: UML driver configuration
 *
4
 * Copyright (C) 2006-2014, 2016 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 "virlog.h"
43
#include "domain_nwfilter.h"
E
Eric Blake 已提交
44
#include "virfile.h"
45
#include "vircommand.h"
46
#include "virnetdevtap.h"
47
#include "virnodesuspend.h"
48
#include "virstring.h"
49

50
#define VIR_FROM_THIS VIR_FROM_UML
51

52
VIR_LOG_INIT("uml.uml_conf");
53

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

59
    if ((caps = virCapabilitiesNew(virArchFromHost(),
60
                                   false, false)) == NULL)
61
        goto error;
62

63 64 65 66
    /* 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
     */
M
Martin Kletzander 已提交
67
    if (virCapabilitiesInitNUMA(caps) < 0) {
68
        virCapabilitiesFreeNUMAInfo(caps);
69
        VIR_WARN("Failed to query host NUMA topology, disabling NUMA capabilities");
70
    }
71

72 73 74
    if (virCapabilitiesInitCaches(caps) < 0)
        VIR_WARN("Failed to get host CPU cache info");

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

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

84
    if ((guest = virCapabilitiesAddGuest(caps,
85
                                         VIR_DOMAIN_OSTYPE_UML,
86
                                         caps->host.arch,
87 88 89 90
                                         NULL,
                                         NULL,
                                         0,
                                         NULL)) == NULL)
91
        goto error;
92 93

    if (virCapabilitiesAddGuestDomain(guest,
94
                                      VIR_DOMAIN_VIRT_UML,
95 96 97 98
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
99
        goto error;
100 101 102

    return caps;

103
 error:
104
    virObjectUnref(caps);
105 106 107 108
    return NULL;
}


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

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

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

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

147
    VIR_FORCE_CLOSE(tapfd);
148 149
    return 0;

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

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

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

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

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

M
Michele Paolino 已提交
186 187 188 189 190
    case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("vhostuser networking type not supported"));
        goto error;

191
    case VIR_DOMAIN_NET_TYPE_SERVER:
192
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
193 194 195 196
                       _("TCP server networking type not supported"));
        goto error;

    case VIR_DOMAIN_NET_TYPE_CLIENT:
197
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
198 199 200
                       _("TCP client networking type not supported"));
        goto error;

201 202 203 204 205
    case VIR_DOMAIN_NET_TYPE_UDP:
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("UDP networking type not supported"));
        goto error;

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

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

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

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

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

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

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

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

S
Stefan Berger 已提交
261 262
    case VIR_DOMAIN_NET_TYPE_LAST:
        break;
263 264
    }

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

271
    virBufferAsprintf(&buf, ",%s", virMacAddrFormat(&def->mac, macaddr));
272 273

    if (def->type == VIR_DOMAIN_NET_TYPE_MCAST) {
274
        virBufferAsprintf(&buf, ",%s,%d",
275 276 277 278
                          def->data.socket.address,
                          def->data.socket.port);
    }

279
    if (virBufferCheckError(&buf) < 0)
280 281 282 283
        return NULL;

    return virBufferContentAndReset(&buf);

284
 error:
285
    virBufferFreeAndReset(&buf);
286 287 288
    return NULL;
}

289
static char *
290
umlBuildCommandLineChr(virDomainChrDefPtr def,
291
                       const char *dev,
D
Daniel P. Berrange 已提交
292
                       virCommandPtr cmd)
293
{
294
    char *ret = NULL;
295

296
    switch (def->source->type) {
297
    case VIR_DOMAIN_CHR_TYPE_NULL:
298
        if (virAsprintf(&ret, "%s%d=null", dev, def->target.port) < 0)
299 300 301 302
            return NULL;
        break;

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

    case VIR_DOMAIN_CHR_TYPE_DEV:
308
        if (virAsprintf(&ret, "%s%d=tty:%s", dev, def->target.port,
309
                        def->source->data.file.path) < 0)
310 311 312 313
            return NULL;
        break;

    case VIR_DOMAIN_CHR_TYPE_STDIO:
314
        if (virAsprintf(&ret, "%s%d=fd:0,fd:1", dev, def->target.port) < 0)
315 316 317 318
            return NULL;
        break;

    case VIR_DOMAIN_CHR_TYPE_TCP:
319
        if (def->source->data.tcp.listen != 1) {
320
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
321
                           _("only TCP listen is supported for chr device"));
322 323 324
            return NULL;
        }

325
        if (virAsprintf(&ret, "%s%d=port:%s", dev, def->target.port,
326
                        def->source->data.tcp.service) < 0)
327 328 329 330
            return NULL;
        break;

    case VIR_DOMAIN_CHR_TYPE_FILE:
331 332 333
         {
            int fd_out;

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

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

    return ret;
}

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
/*
 * 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;
}

390 391 392 393
/*
 * Constructs a argv suitable for launching uml with config defined
 * for a given virtual machine.
 */
D
Daniel P. Berrange 已提交
394 395 396
virCommandPtr umlBuildCommandLine(virConnectPtr conn,
                                  struct uml_driver *driver,
                                  virDomainObjPtr vm)
397
{
398
    size_t i, j;
D
Daniel P. Berrange 已提交
399
    virCommandPtr cmd;
400

D
Daniel P. Berrange 已提交
401 402 403 404
    cmd = virCommandNew(vm->def->os.kernel);

    virCommandAddEnvPassCommon(cmd);

M
Michal Privoznik 已提交
405
    /* virCommandAddArgPair(cmd, "con0", "fd:0,fd:1"); */
406
    virCommandAddArgFormat(cmd, "mem=%lluK", vm->def->mem.cur_balloon);
D
Daniel P. Berrange 已提交
407 408
    virCommandAddArgPair(cmd, "umid", vm->def->name);
    virCommandAddArgPair(cmd, "uml_dir", driver->monitorDir);
409 410

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

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

        if (!STRPREFIX(disk->dst, "ubd")) {
417
            virReportError(VIR_ERR_INTERNAL_ERROR,
418 419 420 421
                           _("unsupported disk type '%s'"), disk->dst);
            goto error;
        }

422
        virCommandAddArgPair(cmd, disk->dst, virDomainDiskGetSource(disk));
423 424
    }

425
    for (i = 0; i < vm->def->nnets; i++) {
426
        char *ret = umlBuildCommandLineNet(conn, vm->def, vm->def->nets[i], i);
427 428
        if (!ret)
            goto error;
D
Daniel P. Berrange 已提交
429 430
        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
431 432
    }

433
    for (i = 0; i < UML_MAX_CHAR_DEVICE; i++) {
434
        virDomainChrDefPtr chr = NULL;
435
        char *ret = NULL;
436
        for (j = 0; j < vm->def->nconsoles; j++)
437 438 439 440
            if (vm->def->consoles[j]->target.port == i)
                chr = vm->def->consoles[j];
        if (chr)
            ret = umlBuildCommandLineChr(chr, "con", cmd);
441
        if (!ret)
442
            if (virAsprintf(&ret, "con%zu=none", i) < 0)
443
                goto error;
D
Daniel P. Berrange 已提交
444 445
        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
446 447
    }

448
    for (i = 0; i < UML_MAX_CHAR_DEVICE; i++) {
449
        virDomainChrDefPtr chr = NULL;
450
        char *ret = NULL;
451
        for (j = 0; j < vm->def->nserials; j++)
452
            if (vm->def->serials[j]->target.port == i)
453 454
                chr = vm->def->serials[j];
        if (chr)
D
Daniel P. Berrange 已提交
455
            ret = umlBuildCommandLineChr(chr, "ssl", cmd);
456
        if (!ret)
457
            if (virAsprintf(&ret, "ssl%zu=none", i) < 0)
458
                goto error;
D
Daniel P. Berrange 已提交
459 460 461

        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
462 463
    }

464 465
    if (vm->def->os.cmdline) {
        char *args, *next_arg;
D
Daniel P. Berrange 已提交
466
        char *cmdline;
467 468
        if (VIR_STRDUP(cmdline, vm->def->os.cmdline) < 0)
            goto error;
469 470 471 472 473 474 475

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

        while (*args) {
            next_arg = umlNextArg(args);
D
Daniel P. Berrange 已提交
476
            virCommandAddArg(cmd, args);
477 478
            args = next_arg;
        }
D
Daniel P. Berrange 已提交
479
        VIR_FREE(cmdline);
480 481
    }

D
Daniel P. Berrange 已提交
482
    return cmd;
483 484

 error:
D
Daniel P. Berrange 已提交
485 486
    virCommandFree(cmd);
    return NULL;
487
}