uml_conf.c 14.2 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 (virNodeSuspendGetTargetMask(&caps->host.powerMgmt) < 0)
        VIR_WARN("Failed to get host power management capabilities");

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

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

    if (virCapabilitiesAddGuestDomain(guest,
91
                                      VIR_DOMAIN_VIRT_UML,
92 93 94 95
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
96
        goto error;
97 98 99

    return caps;

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


106
static int
107
umlConnectTapDevice(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, net->backend.tap, &tapfd, 1,
126
                                       virDomainNetGetActualVirtPortProfile(net),
127
                                       virDomainNetGetActualVlan(net),
128
                                       0, NULL,
129 130
                                       VIR_NETDEV_TAP_CREATE_IFUP |
                                       VIR_NETDEV_TAP_CREATE_PERSIST) < 0) {
131 132
        if (template_ifname)
            VIR_FREE(net->ifname);
133 134 135
        goto error;
    }

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

144
    VIR_FORCE_CLOSE(tapfd);
145 146
    return 0;

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

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

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

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

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

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

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

    case VIR_DOMAIN_NET_TYPE_CLIENT:
194
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
195 196 197
                       _("TCP client networking type not supported"));
        goto error;

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

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

224
        if (umlConnectTapDevice(vm, def, bridge) < 0) {
225 226 227 228 229
            VIR_FREE(bridge);
            goto error;
        }

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

    case VIR_DOMAIN_NET_TYPE_BRIDGE:
235
        if (umlConnectTapDevice(vm, def,
236
                                def->data.bridge.brname) < 0)
237 238 239
            goto error;

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

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

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

253
    case VIR_DOMAIN_NET_TYPE_HOSTDEV:
254
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
255 256 257
                       _("hostdev networking type not supported"));
        goto error;

S
Stefan Berger 已提交
258 259
    case VIR_DOMAIN_NET_TYPE_LAST:
        break;
260 261
    }

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

268
    virBufferAsprintf(&buf, ",%s", virMacAddrFormat(&def->mac, macaddr));
269 270

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

276
    if (virBufferCheckError(&buf) < 0)
277 278 279 280
        return NULL;

    return virBufferContentAndReset(&buf);

281
 error:
282
    virBufferFreeAndReset(&buf);
283 284 285
    return NULL;
}

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

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

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

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

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

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

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

    case VIR_DOMAIN_CHR_TYPE_FILE:
328 329 330
         {
            int fd_out;

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

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

    return ret;
}

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

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

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

    virCommandAddEnvPassCommon(cmd);

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

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

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

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

419
        virCommandAddArgPair(cmd, disk->dst, virDomainDiskGetSource(disk));
420 421
    }

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

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

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

        virCommandAddArg(cmd, ret);
        VIR_FREE(ret);
459 460
    }

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

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

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

D
Daniel P. Berrange 已提交
479
    return cmd;
480 481

 error:
D
Daniel P. Berrange 已提交
482 483
    virCommandFree(cmd);
    return NULL;
484
}