uml_conf.c 14.0 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
                                   false, false)) == 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(virDomainDefPtr vm,
109
                    virDomainNetDefPtr net,
110 111
                    const char *bridge)
{
E
Eric Blake 已提交
112
    bool template_ifname = false;
113
    int tapfd = -1;
114

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

125
    if (virNetDevTapCreateInBridgePort(bridge, &net->ifname, &net->mac,
126
                                       vm->uuid, net->backend.tap, &tapfd, 1,
127
                                       virDomainNetGetActualVirtPortProfile(net),
128
                                       virDomainNetGetActualVlan(net),
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->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 198 199 200 201 202 203 204 205 206 207 208
                       _("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) {
209
            virReportError(VIR_ERR_INTERNAL_ERROR,
210 211 212 213 214
                           _("Network '%s' not found"),
                           def->data.network.name);
            goto error;
        }
        bridge = virNetworkGetBridgeName(network);
215
        virObjectUnref(network);
216
        if (bridge == NULL)
217 218
            goto error;

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

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

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

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

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

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

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

S
Stefan Berger 已提交
253 254
    case VIR_DOMAIN_NET_TYPE_LAST:
        break;
255 256
    }

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

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

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

271
    if (virBufferCheckError(&buf) < 0)
272 273 274 275
        return NULL;

    return virBufferContentAndReset(&buf);

276
 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
}