qemu_driver.c 119.5 KB
Newer Older
D
Daniel P. Berrange 已提交
1 2 3
/*
 * driver.c: core driver methods for managing qemu guests
 *
4
 * Copyright (C) 2006, 2007, 2008 Red Hat, Inc.
D
Daniel P. Berrange 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

24
#include <config.h>
25

D
Daniel P. Berrange 已提交
26 27 28 29 30 31 32 33 34 35 36
#include <sys/types.h>
#include <sys/poll.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <strings.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
37
#include <sys/utsname.h>
38 39 40 41
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <paths.h>
42 43
#include <pwd.h>
#include <stdio.h>
44
#include <sys/wait.h>
45
#include <sys/ioctl.h>
D
Daniel P. Berrange 已提交
46

47
#if HAVE_NUMACTL
48
#define NUMA_VERSION1_COMPATIBILITY 1
49 50 51
#include <numa.h>
#endif

52 53 54 55
#if HAVE_SCHED_H
#include <sched.h>
#endif

56
#include "virterror_internal.h"
57
#include "logging.h"
58
#include "datatypes.h"
59 60
#include "qemu_driver.h"
#include "qemu_conf.h"
J
Jim Meyering 已提交
61
#include "c-ctype.h"
62
#include "event.h"
63
#include "buf.h"
64
#include "util.h"
65
#include "nodeinfo.h"
66
#include "stats_linux.h"
67
#include "capabilities.h"
68
#include "memory.h"
69
#include "uuid.h"
70
#include "domain_conf.h"
71

R
Richard W.M. Jones 已提交
72 73 74
/* For storing short-lived temporary files. */
#define TEMPDIR LOCAL_STATE_DIR "/cache/libvirt"

75 76
static int qemudShutdown(void);

77 78
#define qemudLog(level, msg...) fprintf(stderr, msg)

79 80 81 82 83 84 85 86 87
static int qemudSetCloseExec(int fd) {
    int flags;
    if ((flags = fcntl(fd, F_GETFD)) < 0)
        goto error;
    flags |= FD_CLOEXEC;
    if ((fcntl(fd, F_SETFD, flags)) < 0)
        goto error;
    return 0;
 error:
J
Jim Meyering 已提交
88
    qemudLog(QEMUD_ERR,
89
             "%s", _("Failed to set close-on-exec file descriptor flag\n"));
90 91 92 93 94 95 96 97 98 99 100 101 102
    return -1;
}


static int qemudSetNonBlock(int fd) {
    int flags;
    if ((flags = fcntl(fd, F_GETFL)) < 0)
        goto error;
    flags |= O_NONBLOCK;
    if ((fcntl(fd, F_SETFL, flags)) < 0)
        goto error;
    return 0;
 error:
J
Jim Meyering 已提交
103
    qemudLog(QEMUD_ERR,
104
             "%s", _("Failed to set non-blocking file descriptor flag\n"));
105 106 107 108
    return -1;
}


109 110
static void qemudDomainEventDispatch (struct qemud_driver *driver,
                                      virDomainObjPtr vm,
111 112
                                      int event,
                                      int detail);
113

114 115
static void qemudDispatchVMEvent(int watch,
                                 int fd,
116 117 118
                                 int events,
                                 void *opaque);

119 120
static int qemudStartVMDaemon(virConnectPtr conn,
                              struct qemud_driver *driver,
121 122
                              virDomainObjPtr vm,
                              const char *migrateFrom);
123

124 125
static void qemudShutdownVMDaemon(virConnectPtr conn,
                                  struct qemud_driver *driver,
126
                                  virDomainObjPtr vm);
127

128
static int qemudDomainGetMaxVcpus(virDomainPtr dom);
129

130
static int qemudMonitorCommand (const struct qemud_driver *driver,
131
                                const virDomainObjPtr vm,
132 133
                                const char *cmd,
                                char **reply);
134

J
Jim Meyering 已提交
135
static struct qemud_driver *qemu_driver = NULL;
136 137


138 139 140 141 142
static void
qemudAutostartConfigs(struct qemud_driver *driver) {
    unsigned int i;

    for (i = 0 ; i < driver->domains.count ; i++) {
143 144 145 146 147 148 149 150 151 152 153 154 155
        virDomainObjPtr vm = driver->domains.objs[i];
        if (vm->autostart &&
            !virDomainIsActive(vm)) {
            int ret = qemudStartVMDaemon(NULL, driver, vm, NULL);
            if (ret < 0) {
                virErrorPtr err = virGetLastError();
                qemudLog(QEMUD_ERR, _("Failed to autostart VM '%s': %s\n"),
                         vm->def->name,
                         err ? err->message : NULL);
            } else {
                qemudDomainEventDispatch(driver, vm, VIR_DOMAIN_EVENT_STARTED,
                                         VIR_DOMAIN_EVENT_STARTED_BOOTED);
            }
156 157
        }
    }
158 159
}

160 161 162 163 164 165 166
/**
 * qemudStartup:
 *
 * Initialization function for the QEmu daemon
 */
static int
qemudStartup(void) {
167 168 169
    uid_t uid = geteuid();
    struct passwd *pw;
    char *base = NULL;
D
Daniel P. Berrange 已提交
170
    char driverConf[PATH_MAX];
171

172
    if (VIR_ALLOC(qemu_driver) < 0)
173 174 175 176 177
        return -1;

    /* Don't have a dom0 so start from 1 */
    qemu_driver->nextvmid = 1;

178 179 180 181
    /* Init callback list */
    if(VIR_ALLOC(qemu_driver->domainEventCallbacks) < 0)
        return -1;

182
    if (!uid) {
183 184 185
        if (asprintf(&qemu_driver->logDir,
                     "%s/log/libvirt/qemu", LOCAL_STATE_DIR) == -1)
            goto out_of_memory;
186

D
Daniel P. Berrange 已提交
187
        if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
188 189 190
            goto out_of_memory;
    } else {
        if (!(pw = getpwuid(uid))) {
191
            qemudLog(QEMUD_ERR, _("Failed to find user record for uid '%d': %s\n"),
192
                     uid, strerror(errno));
193
            goto out_nouid;
194 195
        }

196 197 198
        if (asprintf(&qemu_driver->logDir,
                     "%s/.libvirt/qemu/log", pw->pw_dir) == -1)
            goto out_of_memory;
199

200
        if (asprintf (&base, "%s/.libvirt", pw->pw_dir) == -1)
201 202 203 204 205 206
            goto out_of_memory;
    }

    /* Configuration paths are either ~/.libvirt/qemu/... (session) or
     * /etc/libvirt/qemu/... (system).
     */
D
Daniel P. Berrange 已提交
207
    if (snprintf (driverConf, sizeof(driverConf), "%s/qemu.conf", base) == -1)
208
        goto out_of_memory;
D
Daniel P. Berrange 已提交
209
    driverConf[sizeof(driverConf)-1] = '\0';
210

D
Daniel P. Berrange 已提交
211
    if (asprintf (&qemu_driver->configDir, "%s/qemu", base) == -1)
212 213
        goto out_of_memory;

D
Daniel P. Berrange 已提交
214
    if (asprintf (&qemu_driver->autostartDir, "%s/qemu/autostart", base) == -1)
215 216
        goto out_of_memory;

217
    VIR_FREE(base);
218 219 220

    if ((qemu_driver->caps = qemudCapsInit()) == NULL)
        goto out_of_memory;
D
Daniel P. Berrange 已提交
221 222

    if (qemudLoadDriverConfig(qemu_driver, driverConf) < 0) {
223
        qemudShutdown();
D
Daniel P. Berrange 已提交
224 225 226
        return -1;
    }

227 228 229 230
    if (virDomainLoadAllConfigs(NULL,
                                qemu_driver->caps,
                                &qemu_driver->domains,
                                qemu_driver->configDir,
231 232
                                qemu_driver->autostartDir,
                                NULL, NULL) < 0) {
D
Daniel P. Berrange 已提交
233 234 235
        qemudShutdown();
        return -1;
    }
236 237 238 239 240
    qemudAutostartConfigs(qemu_driver);

    return 0;

 out_of_memory:
J
Jim Meyering 已提交
241
    qemudLog (QEMUD_ERR,
242
              "%s", _("qemudStartup: out of memory\n"));
243
 out_nouid:
244 245
    VIR_FREE(base);
    VIR_FREE(qemu_driver);
246 247 248
    return -1;
}

249 250 251 252 253 254 255 256 257 258
static void qemudNotifyLoadDomain(virDomainObjPtr vm, int newVM, void *opaque)
{
    struct qemud_driver *driver = opaque;

    if (newVM)
        qemudDomainEventDispatch(driver, vm,
                                 VIR_DOMAIN_EVENT_DEFINED,
                                 VIR_DOMAIN_EVENT_DEFINED_ADDED);
}

259 260 261 262 263 264 265 266
/**
 * qemudReload:
 *
 * Function to restart the QEmu daemon, it will recheck the configuration
 * files and update its state and the networking
 */
static int
qemudReload(void) {
267 268 269
    if (!qemu_driver)
        return 0;

270 271 272 273
    virDomainLoadAllConfigs(NULL,
                            qemu_driver->caps,
                            &qemu_driver->domains,
                            qemu_driver->configDir,
274 275
                            qemu_driver->autostartDir,
                            qemudNotifyLoadDomain, qemu_driver);
276

277
    qemudAutostartConfigs(qemu_driver);
278 279

    return 0;
280 281
}

282 283 284 285 286 287 288 289 290 291
/**
 * qemudActive:
 *
 * Checks if the QEmu daemon is active, i.e. has an active domain or
 * an active network
 *
 * Returns 1 if active, 0 otherwise
 */
static int
qemudActive(void) {
292
    unsigned int i;
293

294 295 296 297 298
    if (!qemu_driver)
        return 0;

    for (i = 0 ; i < qemu_driver->domains.count ; i++)
        if (virDomainIsActive(qemu_driver->domains.objs[i]))
299 300
            return 1;

301 302 303 304
    /* Otherwise we're happy to deal with a shutdown */
    return 0;
}

305 306 307 308 309 310 311
/**
 * qemudShutdown:
 *
 * Shutdown the QEmu daemon, it will stop all active domains and networks
 */
static int
qemudShutdown(void) {
312
    unsigned int i;
313

314
    if (!qemu_driver)
315
        return -1;
316

317 318
    virCapabilitiesFree(qemu_driver->caps);

319
    /* shutdown active VMs */
320 321 322 323 324
    for (i = 0 ; i < qemu_driver->domains.count ; i++) {
        virDomainObjPtr dom = qemu_driver->domains.objs[i];
        if (virDomainIsActive(dom))
            qemudShutdownVMDaemon(NULL, qemu_driver, dom);
        if (!dom->persistent)
325
            virDomainRemoveInactive(&qemu_driver->domains,
326
                                    dom);
327
    }
328

329
    virDomainObjListFree(&qemu_driver->domains);
330

331
    VIR_FREE(qemu_driver->logDir);
332 333 334
    VIR_FREE(qemu_driver->configDir);
    VIR_FREE(qemu_driver->autostartDir);
    VIR_FREE(qemu_driver->vncTLSx509certdir);
J
Jim Meyering 已提交
335
    VIR_FREE(qemu_driver->vncListen);
D
Daniel P. Berrange 已提交
336

337 338 339
    /* Free domain callback list */
    virDomainEventCallbackListFree(qemu_driver->domainEventCallbacks);

340 341 342
    if (qemu_driver->brctl)
        brShutdown(qemu_driver->brctl);

343
    VIR_FREE(qemu_driver);
344 345

    return 0;
346 347 348
}

/* Return -1 for error, 1 to continue reading and 0 for success */
349 350
typedef int qemudHandlerMonitorOutput(virConnectPtr conn,
                                      struct qemud_driver *driver,
351
                                      virDomainObjPtr vm,
352 353 354 355
                                      const char *output,
                                      int fd);

static int
356 357
qemudReadMonitorOutput(virConnectPtr conn,
                       struct qemud_driver *driver,
358
                       virDomainObjPtr vm,
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
                       int fd,
                       char *buf,
                       int buflen,
                       qemudHandlerMonitorOutput func,
                       const char *what)
{
#define MONITOR_TIMEOUT 3000
    int got = 0;
    buf[0] = '\0';

   /* Consume & discard the initial greeting */
    while (got < (buflen-1)) {
        int ret;

        ret = read(fd, buf+got, buflen-got-1);
        if (ret == 0) {
375
            qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
376
                             _("QEMU quit during %s startup\n%s"), what, buf);
377 378 379 380 381 382 383 384
            return -1;
        }
        if (ret < 0) {
            struct pollfd pfd = { .fd = fd, .events = POLLIN };
            if (errno == EINTR)
                continue;

            if (errno != EAGAIN) {
385
                qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
386
                                 _("Failure while reading %s startup output: %s"),
387 388 389 390 391 392
                                 what, strerror(errno));
                return -1;
            }

            ret = poll(&pfd, 1, MONITOR_TIMEOUT);
            if (ret == 0) {
393
                qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
394
                                 _("Timed out while reading %s startup output"), what);
395 396 397
                return -1;
            } else if (ret == -1) {
                if (errno != EINTR) {
398
                    qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
399
                                     _("Failure while reading %s startup output: %s"),
400 401 402 403 404 405 406 407 408
                                     what, strerror(errno));
                    return -1;
                }
            } else {
                /* Make sure we continue loop & read any further data
                   available before dealing with EOF */
                if (pfd.revents & (POLLIN | POLLHUP))
                    continue;

409
                qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
410
                                 _("Failure while reading %s startup output"), what);
411 412 413 414 415
                return -1;
            }
        } else {
            got += ret;
            buf[got] = '\0';
416
            if ((ret = func(conn, driver, vm, buf, fd)) != 1)
417 418 419 420
                return ret;
        }
    }

421
    qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
422
                     _("Out of space while reading %s startup output"), what);
423 424 425 426 427 428
    return -1;

#undef MONITOR_TIMEOUT
}

static int
429 430
qemudCheckMonitorPrompt(virConnectPtr conn ATTRIBUTE_UNUSED,
                        struct qemud_driver *driver ATTRIBUTE_UNUSED,
431
                        virDomainObjPtr vm,
432 433 434 435 436 437 438 439 440 441 442
                        const char *output,
                        int fd)
{
    if (strstr(output, "(qemu) ") == NULL)
        return 1; /* keep reading */

    vm->monitor = fd;

    return 0;
}

443 444
static int qemudOpenMonitor(virConnectPtr conn,
                            struct qemud_driver *driver,
445
                            virDomainObjPtr vm,
446
                            const char *monitor) {
447 448 449 450
    int monfd;
    char buf[1024];
    int ret = -1;

D
Daniel Veillard 已提交
451
    if ((monfd = open(monitor, O_RDWR)) < 0) {
452
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
453
                         _("Unable to open monitor path %s"), monitor);
454 455 456
        return -1;
    }
    if (qemudSetCloseExec(monfd) < 0) {
457
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
458
                         "%s", _("Unable to set monitor close-on-exec flag"));
459 460 461
        goto error;
    }
    if (qemudSetNonBlock(monfd) < 0) {
462
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
463
                         "%s", _("Unable to put monitor into non-blocking mode"));
464 465 466
        goto error;
    }

467 468
    ret = qemudReadMonitorOutput(conn,
                                 driver, vm, monfd,
469 470 471
                                 buf, sizeof(buf),
                                 qemudCheckMonitorPrompt,
                                 "monitor");
472 473 474 475 476

    /* Keep monitor open upon success */
    if (ret == 0)
        return ret;

477 478 479 480 481
 error:
    close(monfd);
    return ret;
}

482 483
static int qemudExtractMonitorPath(virConnectPtr conn,
                                   const char *haystack,
484
                                   size_t *offset,
485
                                   char **path) {
486
    static const char needle[] = "char device redirected to";
487
    char *tmp, *dev;
488

489
    VIR_FREE(*path);
490
    /* First look for our magic string */
491 492 493 494 495
    if (!(tmp = strstr(haystack + *offset, needle))) {
        return 1;
    }
    tmp += sizeof(needle);
    dev = tmp;
496

497 498 499 500 501
    /*
     * And look for first whitespace character and nul terminate
     * to mark end of the pty path
     */
    while (*tmp) {
502
        if (c_isspace(*tmp)) {
503 504 505 506 507 508 509
            if (VIR_ALLOC_N(*path, (tmp-dev)+1) < 0) {
                qemudReportError(conn, NULL, NULL,
                                 VIR_ERR_NO_MEMORY, NULL);
                return -1;
            }
            strncpy(*path, dev, (tmp-dev));
            (*path)[(tmp-dev)] = '\0';
510
            /* ... now further update offset till we get EOL */
511
            *offset = tmp - haystack;
512 513
            return 0;
        }
514
        tmp++;
515 516 517 518 519
    }

    /*
     * We found a path, but didn't find any whitespace,
     * so it must be still incomplete - we should at
520 521
     * least see a \n - indicate that we want to carry
     * on trying again
522
     */
523
    return 1;
524 525 526
}

static int
527 528
qemudFindCharDevicePTYs(virConnectPtr conn,
                        struct qemud_driver *driver,
529
                        virDomainObjPtr vm,
530 531
                        const char *output,
                        int fd ATTRIBUTE_UNUSED)
532
{
533
    char *monitor = NULL;
534
    size_t offset = 0;
535
    int ret, i;
536 537 538 539 540

    /* The order in which QEMU prints out the PTY paths is
       the order in which it procsses its monitor, serial
       and parallel device args. This code must match that
       ordering.... */
541

542
    /* So first comes the monitor device */
543 544
    if ((ret = qemudExtractMonitorPath(conn, output, &offset, &monitor)) != 0)
        goto cleanup;
545

546
    /* then the serial devices */
547 548
    for (i = 0 ; i < vm->def->nserials ; i++) {
        virDomainChrDefPtr chr = vm->def->serials[i];
549 550 551 552
        if (chr->type == VIR_DOMAIN_CHR_TYPE_PTY) {
            if ((ret = qemudExtractMonitorPath(conn, output, &offset,
                                               &chr->data.file.path)) != 0)
                goto cleanup;
553 554 555 556
        }
    }

    /* and finally the parallel devices */
557 558
    for (i = 0 ; i < vm->def->nparallels ; i++) {
        virDomainChrDefPtr chr = vm->def->parallels[i];
559 560 561 562
        if (chr->type == VIR_DOMAIN_CHR_TYPE_PTY) {
            if ((ret = qemudExtractMonitorPath(conn, output, &offset,
                                               &chr->data.file.path)) != 0)
                goto cleanup;
563 564 565 566
        }
    }

    /* Got them all, so now open the monitor console */
567 568 569 570 571
    ret = qemudOpenMonitor(conn, driver, vm, monitor);

cleanup:
    VIR_FREE(monitor);
    return ret;
572 573
}

574 575
static int qemudWaitForMonitor(virConnectPtr conn,
                               struct qemud_driver *driver,
576
                               virDomainObjPtr vm) {
577
    char buf[1024]; /* Plenty of space to get startup greeting */
578
    int ret = qemudReadMonitorOutput(conn,
579
                                     driver, vm, vm->stderr_fd,
580
                                     buf, sizeof(buf),
581
                                     qemudFindCharDevicePTYs,
582 583 584
                                     "console");

    buf[sizeof(buf)-1] = '\0';
585 586

    if (safewrite(vm->logfile, buf, strlen(buf)) < 0) {
587
        /* Log, but ignore failures to write logfile for VM */
588
        qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s\n"),
589 590 591 592 593
                 strerror(errno));
    }
    return ret;
}

594 595 596
static int
qemudDetectVcpuPIDs(virConnectPtr conn,
                    struct qemud_driver *driver,
597
                    virDomainObjPtr vm) {
598 599 600 601 602 603
    char *qemucpus = NULL;
    char *line;
    int lastVcpu = -1;

    /* Only KVM has seperate threads for CPUs,
       others just use main QEMU process for CPU */
604
    if (vm->def->virtType != VIR_DOMAIN_VIRT_KVM)
605 606 607 608 609 610 611 612 613 614
        vm->nvcpupids = 1;
    else
        vm->nvcpupids = vm->def->vcpus;

    if (VIR_ALLOC_N(vm->vcpupids, vm->nvcpupids) < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY,
                         "%s", _("allocate cpumap"));
        return -1;
    }

615
    if (vm->def->virtType != VIR_DOMAIN_VIRT_KVM) {
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
        vm->vcpupids[0] = vm->pid;
        return 0;
    }

    if (qemudMonitorCommand(driver, vm, "info cpus", &qemucpus) < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot run monitor command to fetch CPU thread info"));
        VIR_FREE(vm->vcpupids);
        vm->nvcpupids = 0;
        return -1;
    }

    /*
     * This is the gross format we're about to parse :-{
     *
     * (qemu) info cpus
     * * CPU #0: pc=0x00000000000f0c4a thread_id=30019
     *   CPU #1: pc=0x00000000fffffff0 thread_id=30020
     *   CPU #2: pc=0x00000000fffffff0 thread_id=30021
     *
     */
    line = qemucpus;
    do {
        char *offset = strchr(line, '#');
        char *end = NULL;
        int vcpu = 0, tid = 0;

        /* See if we're all done */
        if (offset == NULL)
            break;

        /* Extract VCPU number */
        if (virStrToLong_i(offset + 1, &end, 10, &vcpu) < 0)
            goto error;
        if (end == NULL || *end != ':')
            goto error;

        /* Extract host Thread ID */
        if ((offset = strstr(line, "thread_id=")) == NULL)
            goto error;
        if (virStrToLong_i(offset + strlen("thread_id="), &end, 10, &tid) < 0)
            goto error;
        if (end == NULL || !c_isspace(*end))
            goto error;

        /* Validate the VCPU is in expected range & order */
        if (vcpu > vm->nvcpupids ||
            vcpu != (lastVcpu + 1))
            goto error;

        lastVcpu = vcpu;
        vm->vcpupids[vcpu] = tid;

        /* Skip to next data line */
        line = strchr(offset, '\r');
        if (line == NULL)
            line = strchr(offset, '\n');
    } while (line != NULL);

    /* Validate we got data for all VCPUs we expected */
    if (lastVcpu != (vm->def->vcpus - 1))
        goto error;

679
    VIR_FREE(qemucpus);
680 681 682 683
    return 0;

error:
    VIR_FREE(vm->vcpupids);
684 685
    vm->nvcpupids = 0;
    VIR_FREE(qemucpus);
686 687 688 689 690 691 692 693

    /* Explicitly return success, not error. Older KVM does
       not have vCPU -> Thread mapping info and we don't
       want to break its use. This merely disables ability
       to pin vCPUS with libvirt */
    return 0;
}

694 695 696
static int
qemudInitCpus(virConnectPtr conn,
              struct qemud_driver *driver,
D
Daniel Veillard 已提交
697 698
              virDomainObjPtr vm,
              const char *migrateFrom) {
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
    char *info = NULL;
#if HAVE_SCHED_GETAFFINITY
    cpu_set_t mask;
    int i, maxcpu = QEMUD_CPUMASK_LEN;
    virNodeInfo nodeinfo;

    if (virNodeInfoPopulate(conn, &nodeinfo) < 0)
        return -1;

    /* setaffinity fails if you set bits for CPUs which
     * aren't present, so we have to limit ourselves */
    if (maxcpu > nodeinfo.cpus)
        maxcpu = nodeinfo.cpus;

    CPU_ZERO(&mask);
D
Daniel P. Berrange 已提交
714 715 716 717 718 719
    if (vm->def->cpumask) {
        for (i = 0 ; i < maxcpu ; i++)
            if (vm->def->cpumask[i])
                CPU_SET(i, &mask);
    } else {
        for (i = 0 ; i < maxcpu ; i++)
720
            CPU_SET(i, &mask);
D
Daniel P. Berrange 已提交
721
    }
722 723 724 725 726 727 728 729 730 731 732 733

    for (i = 0 ; i < vm->nvcpupids ; i++) {
        if (sched_setaffinity(vm->vcpupids[i],
                              sizeof(mask), &mask) < 0) {
            qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                             _("failed to set CPU affinity %s"),
                             strerror(errno));
            return -1;
        }
    }
#endif /* HAVE_SCHED_GETAFFINITY */

D
Daniel Veillard 已提交
734 735 736 737 738 739 740 741
    if (migrateFrom == NULL) {
        /* Allow the CPUS to start executing */
        if (qemudMonitorCommand(driver, vm, "cont", &info) < 0) {
            qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                             "%s", _("resume operation failed"));
            return -1;
        }
        VIR_FREE(info);
742 743 744 745 746 747
    }

    return 0;
}


748
static int qemudNextFreeVNCPort(struct qemud_driver *driver ATTRIBUTE_UNUSED) {
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
    int i;

    for (i = 5900 ; i < 6000 ; i++) {
        int fd;
        int reuse = 1;
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(i);
        addr.sin_addr.s_addr = htonl(INADDR_ANY);
        fd = socket(PF_INET, SOCK_STREAM, 0);
        if (fd < 0)
            return -1;

        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&reuse, sizeof(reuse)) < 0) {
            close(fd);
            break;
        }

        if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
            /* Not in use, lets grab it */
            close(fd);
            return i;
        }
        close(fd);

        if (errno == EADDRINUSE) {
            /* In use, try next */
            continue;
        }
        /* Some other bad failure, get out.. */
        break;
    }
    return -1;
}

784 785 786
static virDomainPtr qemudDomainLookupByName(virConnectPtr conn,
                                            const char *name);

787 788
static int qemudStartVMDaemon(virConnectPtr conn,
                              struct qemud_driver *driver,
789 790
                              virDomainObjPtr vm,
                              const char *migrateFrom) {
791
    const char **argv = NULL, **tmp;
792
    const char **progenv = NULL;
793
    int i, ret;
794
    char logfile[PATH_MAX];
795
    struct stat sb;
796 797
    int *tapfds = NULL;
    int ntapfds = 0;
798
    unsigned int qemuCmdFlags;
799
    fd_set keepfd;
800
    const char *emulator;
801 802

    FD_ZERO(&keepfd);
803

804
    if (virDomainIsActive(vm)) {
805
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
806
                         "%s", _("VM is already active"));
807 808 809
        return -1;
    }

810 811 812
    if (vm->def->graphics &&
        vm->def->graphics->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
        vm->def->graphics->data.vnc.autoport) {
813
        int port = qemudNextFreeVNCPort(driver);
814
        if (port < 0) {
815
            qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
816
                             "%s", _("Unable to find an unused VNC port"));
817 818
            return -1;
        }
819 820
        vm->def->graphics->data.vnc.port = port;
    }
821

822
    if ((strlen(driver->logDir) + /* path */
823 824 825 826
         1 + /* Separator */
         strlen(vm->def->name) + /* basename */
         4 + /* suffix .log */
         1 /* NULL */) > PATH_MAX) {
827
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
828
                         _("config file path too long: %s/%s.log"),
829
                         driver->logDir, vm->def->name);
830 831
        return -1;
    }
832
    strcpy(logfile, driver->logDir);
833 834 835 836
    strcat(logfile, "/");
    strcat(logfile, vm->def->name);
    strcat(logfile, ".log");

837
    if (virFileMakePath(driver->logDir) < 0) {
838
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
839
                         _("cannot create log directory %s: %s"),
840
                         driver->logDir, strerror(errno));
841 842 843 844 845
        return -1;
    }

    if ((vm->logfile = open(logfile, O_CREAT | O_TRUNC | O_WRONLY,
                            S_IRUSR | S_IWUSR)) < 0) {
846
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
847
                         _("failed to create logfile %s: %s"),
848 849 850
                         logfile, strerror(errno));
        return -1;
    }
851 852
    if (qemudSetCloseExec(vm->logfile) < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
853
                         _("Unable to set VM logfile close-on-exec flag %s"),
854 855 856 857 858
                         strerror(errno));
        close(vm->logfile);
        vm->logfile = -1;
        return -1;
    }
859

860 861 862 863 864 865
    emulator = vm->def->emulator;
    if (!emulator)
        emulator = virDomainDefDefaultEmulator(conn, vm->def, driver->caps);
    if (!emulator)
        return -1;

866 867 868 869
    /* Make sure the binary we are about to try exec'ing exists.
     * Technically we could catch the exec() failure, but that's
     * in a sub-process so its hard to feed back a useful error
     */
870
    if (stat(emulator, &sb) < 0) {
871 872
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                         _("Cannot find QEMU binary %s: %s"),
873
                         emulator,
874 875 876 877
                         strerror(errno));
        return -1;
    }

878
    if (qemudExtractVersionInfo(emulator,
879 880 881 882
                                NULL,
                                &qemuCmdFlags) < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                         _("Cannot determine QEMU argv syntax %s"),
883
                         emulator);
884 885
        return -1;
    }
886

887
    vm->def->id = driver->nextvmid++;
888
    if (qemudBuildCommandLine(conn, driver, vm,
889
                              qemuCmdFlags, &argv, &progenv,
890
                              &tapfds, &ntapfds, migrateFrom) < 0) {
891
        close(vm->logfile);
892
        vm->def->id = -1;
893 894 895 896
        vm->logfile = -1;
        return -1;
    }

897 898 899 900 901 902 903 904 905 906
    tmp = progenv;
    while (*tmp) {
        if (safewrite(vm->logfile, *tmp, strlen(*tmp)) < 0)
            qemudLog(QEMUD_WARN, _("Unable to write envv to logfile %d: %s\n"),
                     errno, strerror(errno));
        if (safewrite(vm->logfile, " ", 1) < 0)
            qemudLog(QEMUD_WARN, _("Unable to write envv to logfile %d: %s\n"),
                     errno, strerror(errno));
        tmp++;
    }
907 908
    tmp = argv;
    while (*tmp) {
909
        if (safewrite(vm->logfile, *tmp, strlen(*tmp)) < 0)
910
            qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s\n"),
911
                     errno, strerror(errno));
912
        if (safewrite(vm->logfile, " ", 1) < 0)
913
            qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s\n"),
914 915 916
                     errno, strerror(errno));
        tmp++;
    }
917
    if (safewrite(vm->logfile, "\n", 1) < 0)
918
        qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s\n"),
919 920
                 errno, strerror(errno));

921 922 923
    vm->stdout_fd = -1;
    vm->stderr_fd = -1;

924 925 926
    for (i = 0 ; i < ntapfds ; i++)
        FD_SET(tapfds[i], &keepfd);

927
    ret = virExec(conn, argv, progenv, &keepfd, &vm->pid,
928 929
                  vm->stdin_fd, &vm->stdout_fd, &vm->stderr_fd,
                  VIR_EXEC_NONBLOCK);
930
    if (ret == 0)
931
        vm->state = migrateFrom ? VIR_DOMAIN_PAUSED : VIR_DOMAIN_RUNNING;
932 933
    else
        vm->def->id = -1;
934

935
    for (i = 0 ; argv[i] ; i++)
936 937
        VIR_FREE(argv[i]);
    VIR_FREE(argv);
938

939 940 941 942
    for (i = 0 ; progenv[i] ; i++)
        VIR_FREE(progenv[i]);
    VIR_FREE(progenv);

943 944 945
    if (tapfds) {
        for (i = 0 ; i < ntapfds ; i++) {
            close(tapfds[i]);
946
        }
947
        VIR_FREE(tapfds);
948 949
    }

950
    if (ret == 0) {
951
        if (((vm->stdout_watch = virEventAddHandle(vm->stdout_fd,
952 953 954 955 956
                                                   VIR_EVENT_HANDLE_READABLE |
                                                   VIR_EVENT_HANDLE_ERROR |
                                                   VIR_EVENT_HANDLE_HANGUP,
                                                   qemudDispatchVMEvent,
                                                   driver, NULL)) < 0) ||
957 958 959 960 961
            ((vm->stderr_watch = virEventAddHandle(vm->stderr_fd,
                                                   VIR_EVENT_HANDLE_READABLE |
                                                   VIR_EVENT_HANDLE_ERROR |
                                                   VIR_EVENT_HANDLE_HANGUP,
                                                   qemudDispatchVMEvent,
962
                                                   driver, NULL)) < 0) ||
963 964
            (qemudWaitForMonitor(conn, driver, vm) < 0) ||
            (qemudDetectVcpuPIDs(conn, driver, vm) < 0) ||
D
Daniel Veillard 已提交
965
            (qemudInitCpus(conn, driver, vm, migrateFrom) < 0)) {
966 967 968
            qemudShutdownVMDaemon(conn, driver, vm);
            return -1;
        }
969 970
    }

971
    return ret;
972 973
}

974
static int qemudVMData(struct qemud_driver *driver ATTRIBUTE_UNUSED,
975
                       virDomainObjPtr vm, int fd) {
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
    char buf[4096];
    if (vm->pid < 0)
        return 0;

    for (;;) {
        int ret = read(fd, buf, sizeof(buf)-1);
        if (ret < 0) {
            if (errno == EAGAIN)
                return 0;
            return -1;
        }
        if (ret == 0) {
            return 0;
        }
        buf[ret] = '\0';

992
        if (safewrite(vm->logfile, buf, ret) < 0) {
993
            /* Log, but ignore failures to write logfile for VM */
994
            qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s\n"),
995 996 997 998 999 1000
                     strerror(errno));
        }
    }
}


1001
static void qemudShutdownVMDaemon(virConnectPtr conn ATTRIBUTE_UNUSED,
1002 1003
                                  struct qemud_driver *driver, virDomainObjPtr vm) {
    if (!virDomainIsActive(vm))
1004
        return;
1005

1006
    qemudLog(QEMUD_INFO, _("Shutting down VM '%s'\n"), vm->def->name);
1007 1008 1009

    kill(vm->pid, SIGTERM);

1010 1011
    qemudVMData(driver, vm, vm->stdout_fd);
    qemudVMData(driver, vm, vm->stderr_fd);
1012

1013 1014
    virEventRemoveHandle(vm->stdout_watch);
    virEventRemoveHandle(vm->stderr_watch);
1015 1016

    if (close(vm->logfile) < 0)
1017
        qemudLog(QEMUD_WARN, _("Unable to close logfile %d: %s\n"),
1018
                 errno, strerror(errno));
1019 1020
    close(vm->stdout_fd);
    close(vm->stderr_fd);
1021 1022 1023
    if (vm->monitor != -1)
        close(vm->monitor);
    vm->logfile = -1;
1024 1025
    vm->stdout_fd = -1;
    vm->stderr_fd = -1;
1026 1027 1028 1029 1030
    vm->monitor = -1;

    if (waitpid(vm->pid, NULL, WNOHANG) != vm->pid) {
        kill(vm->pid, SIGKILL);
        if (waitpid(vm->pid, NULL, 0) != vm->pid) {
J
Jim Meyering 已提交
1031
            qemudLog(QEMUD_WARN,
1032
                     "%s", _("Got unexpected pid, damn\n"));
1033 1034 1035 1036
        }
    }

    vm->pid = -1;
1037
    vm->def->id = -1;
1038
    vm->state = VIR_DOMAIN_SHUTOFF;
1039
    VIR_FREE(vm->vcpupids);
1040
    vm->nvcpupids = 0;
1041 1042

    if (vm->newDef) {
1043
        virDomainDefFree(vm->def);
1044
        vm->def = vm->newDef;
1045
        vm->def->id = -1;
1046 1047 1048 1049
        vm->newDef = NULL;
    }
}

1050
static int qemudDispatchVMLog(struct qemud_driver *driver, virDomainObjPtr vm, int fd) {
1051 1052
    if (qemudVMData(driver, vm, fd) < 0) {
        qemudShutdownVMDaemon(NULL, driver, vm);
1053 1054 1055
        qemudDomainEventDispatch(driver, vm,
                                 VIR_DOMAIN_EVENT_STOPPED,
                                 VIR_DOMAIN_EVENT_STOPPED_FAILED);
1056
        if (!vm->persistent)
1057 1058
            virDomainRemoveInactive(&driver->domains,
                                    vm);
1059
    }
1060 1061 1062
    return 0;
}

1063
static int qemudDispatchVMFailure(struct qemud_driver *driver, virDomainObjPtr vm,
1064
                                  int fd ATTRIBUTE_UNUSED) {
1065
    qemudShutdownVMDaemon(NULL, driver, vm);
1066 1067 1068
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_STOPPED,
                             VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1069
    if (!vm->persistent)
1070 1071
        virDomainRemoveInactive(&driver->domains,
                                vm);
1072 1073 1074 1075
    return 0;
}


1076
static void
1077
qemudDispatchVMEvent(int watch, int fd, int events, void *opaque) {
1078
    struct qemud_driver *driver = (struct qemud_driver *)opaque;
1079 1080 1081 1082 1083
    virDomainObjPtr vm = NULL;
    unsigned int i;

    for (i = 0 ; i < driver->domains.count ; i++) {
        if (virDomainIsActive(driver->domains.objs[i]) &&
1084 1085
            (driver->domains.objs[i]->stdout_watch == watch ||
             driver->domains.objs[i]->stderr_watch == watch)) {
1086
            vm = driver->domains.objs[i];
1087
            break;
1088
        }
1089 1090 1091 1092 1093
    }

    if (!vm)
        return;

1094 1095 1096 1097 1098 1099
    if (vm->stdout_fd != fd &&
        vm->stderr_fd != fd) {
        qemudDispatchVMFailure(driver, vm, fd);
        return;
    }

1100
    if (events == VIR_EVENT_HANDLE_READABLE)
1101
        qemudDispatchVMLog(driver, vm, fd);
1102
    else
1103
        qemudDispatchVMFailure(driver, vm, fd);
1104 1105
}

1106 1107
static int
qemudMonitorCommand (const struct qemud_driver *driver ATTRIBUTE_UNUSED,
1108
                     const virDomainObjPtr vm,
1109 1110
                     const char *cmd,
                     char **reply) {
D
Daniel P. Berrange 已提交
1111 1112
    int size = 0;
    char *buf = NULL;
1113
    size_t cmdlen = strlen(cmd);
D
Daniel P. Berrange 已提交
1114

1115 1116 1117
    if (safewrite(vm->monitor, cmd, cmdlen) != cmdlen)
        return -1;
    if (safewrite(vm->monitor, "\r", 1) != 1)
D
Daniel P. Berrange 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
        return -1;

    *reply = NULL;

    for (;;) {
        struct pollfd fd = { vm->monitor, POLLIN | POLLERR | POLLHUP, 0 };
        char *tmp;

        /* Read all the data QEMU has sent thus far */
        for (;;) {
            char data[1024];
            int got = read(vm->monitor, data, sizeof(data));
D
Daniel P. Berrange 已提交
1130

1131 1132
            if (got == 0)
                goto error;
D
Daniel P. Berrange 已提交
1133 1134 1135 1136 1137
            if (got < 0) {
                if (errno == EINTR)
                    continue;
                if (errno == EAGAIN)
                    break;
1138
                goto error;
1139
            }
1140
            if (VIR_REALLOC_N(buf, size+got+1) < 0)
1141 1142
                goto error;

D
Daniel P. Berrange 已提交
1143 1144 1145 1146
            memmove(buf+size, data, got);
            buf[size+got] = '\0';
            size += got;
        }
1147

D
Daniel P. Berrange 已提交
1148
        /* Look for QEMU prompt to indicate completion */
D
Daniel P. Berrange 已提交
1149
        if (buf && ((tmp = strstr(buf, "\n(qemu) ")) != NULL)) {
D
Daniel P. Berrange 已提交
1150 1151 1152 1153 1154 1155 1156 1157
            tmp[0] = '\0';
            break;
        }
    pollagain:
        /* Need to wait for more data */
        if (poll(&fd, 1, -1) < 0) {
            if (errno == EINTR)
                goto pollagain;
1158
            goto error;
D
Daniel P. Berrange 已提交
1159 1160 1161
        }
    }

1162 1163
    /* Log, but ignore failures to write logfile for VM */
    if (safewrite(vm->logfile, buf, strlen(buf)) < 0)
1164
        qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s\n"),
1165 1166
                 strerror(errno));

D
Daniel P. Berrange 已提交
1167 1168
    *reply = buf;
    return 0;
1169 1170 1171 1172 1173

 error:
    if (buf) {
        /* Log, but ignore failures to write logfile for VM */
        if (safewrite(vm->logfile, buf, strlen(buf)) < 0)
1174
            qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s\n"),
1175
                     strerror(errno));
1176
        VIR_FREE(buf);
1177 1178
    }
    return -1;
D
Daniel P. Berrange 已提交
1179 1180
}

1181 1182 1183 1184 1185 1186
/**
 * qemudProbe:
 *
 * Probe for the availability of the qemu driver, assume the
 * presence of QEmu emulation if the binaries are installed
 */
1187
static int qemudProbe(void)
1188 1189 1190
{
    if ((virFileExists("/usr/bin/qemu")) ||
        (virFileExists("/usr/bin/qemu-kvm")) ||
1191 1192 1193 1194
        (virFileExists("/usr/bin/xenner")))
        return 1;

    return 0;
1195
}
1196

1197
static virDrvOpenStatus qemudOpen(virConnectPtr conn,
1198
                                  virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1199
                                  int flags ATTRIBUTE_UNUSED) {
1200 1201 1202
    uid_t uid = getuid();

    if (qemu_driver == NULL)
1203
        goto decline;
1204

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
    if (!qemudProbe())
        goto decline;

    if (conn->uri == NULL) {
        conn->uri = xmlParseURI(uid ? "qemu:///session" : "qemu:///system");
        if (!conn->uri) {
            qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY,NULL);
            return VIR_DRV_OPEN_ERROR;
        }
    } else if (conn->uri->scheme == NULL ||
               conn->uri->path == NULL)
1216 1217
        goto decline;

1218
    if (STRNEQ (conn->uri->scheme, "qemu"))
1219 1220
        goto decline;

1221
    if (uid != 0) {
1222
        if (STRNEQ (conn->uri->path, "/session"))
1223
            goto decline;
1224
    } else { /* root */
1225 1226
        if (STRNEQ (conn->uri->path, "/system") &&
            STRNEQ (conn->uri->path, "/session"))
1227
            goto decline;
1228 1229 1230 1231 1232
    }

    conn->privateData = qemu_driver;

    return VIR_DRV_OPEN_SUCCESS;
1233 1234

 decline:
1235
    return VIR_DRV_OPEN_DECLINED;
1236 1237 1238
}

static int qemudClose(virConnectPtr conn) {
1239 1240 1241 1242
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;

    /* Get rid of callbacks registered for this conn */
    virDomainEventCallbackListRemoveConn(conn, driver->domainEventCallbacks);
1243 1244 1245 1246 1247 1248

    conn->privateData = NULL;

    return 0;
}

D
Daniel Veillard 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
/* Which features are supported by this driver? */
static int
qemudSupportsFeature (virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
    switch (feature) {
    case VIR_DRV_FEATURE_MIGRATION_V2: return 1;
    default: return 0;
    }
}

1259
static const char *qemudGetType(virConnectPtr conn ATTRIBUTE_UNUSED) {
1260
    return "QEMU";
1261 1262
}

1263 1264 1265 1266 1267

static int kvmGetMaxVCPUs(void) {
    int maxvcpus = 1;

    int r, fd;
1268

1269 1270
    fd = open(KVM_DEVICE, O_RDONLY);
    if (fd < 0) {
1271
        qemudLog(QEMUD_WARN, _("Unable to open %s: %s\n"), KVM_DEVICE, strerror(errno));
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
        return maxvcpus;
    }

    r = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS);
    if (r > 0)
        maxvcpus = r;

    close(fd);
    return maxvcpus;
}


1284
static int qemudGetMaxVCPUs(virConnectPtr conn, const char *type) {
1285 1286 1287
    if (!type)
        return 16;

1288
    if (STRCASEEQ(type, "qemu"))
1289 1290 1291 1292
        return 16;

    /* XXX future KVM will support SMP. Need to probe
       kernel to figure out KVM module version i guess */
1293
    if (STRCASEEQ(type, "kvm"))
1294
        return kvmGetMaxVCPUs();
1295

1296
    if (STRCASEEQ(type, "kqemu"))
1297
        return 1;
1298 1299 1300

    qemudReportError(conn, NULL, NULL, VIR_ERR_INVALID_ARG,
                     _("unknown type '%s'"), type);
1301 1302 1303
    return -1;
}

1304 1305 1306
static int qemudGetNodeInfo(virConnectPtr conn,
                            virNodeInfoPtr nodeinfo) {
    return virNodeInfoPopulate(conn, nodeinfo);
1307 1308 1309
}


1310 1311 1312
static char *qemudGetCapabilities(virConnectPtr conn) {
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
    char *xml;
1313

1314
    if ((xml = virCapabilitiesFormatXML(driver->caps)) == NULL) {
1315 1316
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY,
                 "%s", _("failed to allocate space for capabilities support"));
1317 1318 1319
        return NULL;
    }

1320
    return xml;
1321 1322 1323
}


1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
#if HAVE_NUMACTL
static int
qemudNodeGetCellsFreeMemory(virConnectPtr conn,
                            unsigned long long *freeMems,
                            int startCell,
                            int maxCells)
{
    int n, lastCell, numCells;

    if (numa_available() < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_SUPPORT,
                         "%s", _("NUMA not supported on this host"));
        return -1;
    }
    lastCell = startCell + maxCells - 1;
    if (lastCell > numa_max_node())
        lastCell = numa_max_node();

    for (numCells = 0, n = startCell ; n <= lastCell ; n++) {
        long long mem;
        if (numa_node_size64(n, &mem) < 0) {
            qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                             "%s", _("Failed to query NUMA free memory"));
            return -1;
        }
        freeMems[numCells++] = mem;
    }
    return numCells;
}

static unsigned long long
qemudNodeGetFreeMemory (virConnectPtr conn)
{
    unsigned long long freeMem = 0;
    int n;
    if (numa_available() < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_SUPPORT,
                         "%s", _("NUMA not supported on this host"));
        return -1;
    }

    for (n = 0 ; n <= numa_max_node() ; n++) {
        long long mem;
        if (numa_node_size64(n, &mem) < 0) {
            qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                             "%s", _("Failed to query NUMA free memory"));
            return -1;
        }
        freeMem += mem;
    }

    return freeMem;
}

#endif
1379

D
Daniel P. Berrange 已提交
1380 1381 1382
static int qemudGetProcessInfo(unsigned long long *cpuTime, int pid) {
    char proc[PATH_MAX];
    FILE *pidinfo;
1383
    unsigned long long usertime, systime;
D
Daniel P. Berrange 已提交
1384 1385 1386 1387 1388 1389

    if (snprintf(proc, sizeof(proc), "/proc/%d/stat", pid) >= (int)sizeof(proc)) {
        return -1;
    }

    if (!(pidinfo = fopen(proc, "r"))) {
1390
        /*printf("cannot read pid info");*/
D
Daniel P. Berrange 已提交
1391 1392 1393 1394 1395
        /* VM probably shut down, so fake 0 */
        *cpuTime = 0;
        return 0;
    }

1396
    if (fscanf(pidinfo, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %llu %llu", &usertime, &systime) != 2) {
1397
        qemudDebug("not enough arg");
D
Daniel P. Berrange 已提交
1398 1399 1400 1401 1402 1403 1404 1405
        return -1;
    }

    /* We got jiffies
     * We want nanoseconds
     * _SC_CLK_TCK is jiffies per second
     * So calulate thus....
     */
1406
    *cpuTime = 1000ull * 1000ull * 1000ull * (usertime + systime) / (unsigned long long)sysconf(_SC_CLK_TCK);
D
Daniel P. Berrange 已提交
1407

1408
    qemudDebug("Got %llu %llu %llu", usertime, systime, *cpuTime);
D
Daniel P. Berrange 已提交
1409 1410 1411 1412 1413 1414 1415

    fclose(pidinfo);

    return 0;
}


1416
static virDomainPtr qemudDomainLookupByID(virConnectPtr conn,
1417
                                          int id) {
1418
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
1419
    virDomainObjPtr vm = virDomainFindByID(&driver->domains, id);
1420 1421 1422
    virDomainPtr dom;

    if (!vm) {
1423
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
1424 1425 1426
        return NULL;
    }

1427
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
1428
    if (dom) dom->id = vm->def->id;
1429 1430
    return dom;
}
1431
static virDomainPtr qemudDomainLookupByUUID(virConnectPtr conn,
1432
                                            const unsigned char *uuid) {
1433
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
1434
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, uuid);
1435 1436 1437
    virDomainPtr dom;

    if (!vm) {
1438
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
1439 1440 1441
        return NULL;
    }

1442
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
1443
    if (dom) dom->id = vm->def->id;
1444 1445
    return dom;
}
1446
static virDomainPtr qemudDomainLookupByName(virConnectPtr conn,
1447
                                            const char *name) {
1448
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
1449
    virDomainObjPtr vm = virDomainFindByName(&driver->domains, name);
1450 1451 1452
    virDomainPtr dom;

    if (!vm) {
1453
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
1454 1455 1456
        return NULL;
    }

1457
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
1458
    if (dom) dom->id = vm->def->id;
1459 1460 1461
    return dom;
}

1462
static int qemudGetVersion(virConnectPtr conn, unsigned long *version) {
1463
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
1464
    if (qemudExtractVersion(conn, driver) < 0)
1465 1466
        return -1;

1467 1468
    *version = qemu_driver->qemuVersion;
    return 0;
D
Daniel P. Berrange 已提交
1469 1470
}

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
static char *
qemudGetHostname (virConnectPtr conn)
{
    int r;
    char hostname[HOST_NAME_MAX+1], *str;

    r = gethostname (hostname, HOST_NAME_MAX+1);
    if (r == -1) {
        qemudReportError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR,
                          "%s", strerror (errno));
        return NULL;
    }
    /* Caller frees this string. */
    str = strdup (hostname);
    if (str == NULL) {
        qemudReportError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR,
                         "%s", strerror (errno));
        return NULL;
    }
    return str;
}

1493
static int qemudListDomains(virConnectPtr conn, int *ids, int nids) {
1494
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
1495 1496 1497 1498 1499 1500
    int got = 0, i;

    for (i = 0 ; i < driver->domains.count && got < nids ; i++)
        if (virDomainIsActive(driver->domains.objs[i]))
            ids[got++] = driver->domains.objs[i]->def->id;

D
Daniel P. Berrange 已提交
1501 1502
    return got;
}
1503
static int qemudNumDomains(virConnectPtr conn) {
1504
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
1505 1506 1507 1508
    int n = 0, i;

    for (i = 0 ; i < driver->domains.count ; i++)
        if (virDomainIsActive(driver->domains.objs[i]))
1509
            n++;
1510

1511
    return n;
D
Daniel P. Berrange 已提交
1512
}
1513
static virDomainPtr qemudDomainCreate(virConnectPtr conn, const char *xml,
1514
                                      unsigned int flags ATTRIBUTE_UNUSED) {
1515 1516
    virDomainDefPtr def;
    virDomainObjPtr vm;
1517 1518
    virDomainPtr dom;
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
D
Daniel P. Berrange 已提交
1519

1520
    if (!(def = virDomainDefParseString(conn, driver->caps, xml)))
1521 1522
        return NULL;

1523
    vm = virDomainFindByName(&driver->domains, def->name);
1524
    if (vm) {
1525
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
1526
                         _("domain '%s' is already defined"),
1527 1528 1529 1530
                         def->name);
        virDomainDefFree(def);
        return NULL;
    }
1531
    vm = virDomainFindByUUID(&driver->domains, def->uuid);
1532 1533 1534 1535 1536
    if (vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(def->uuid, uuidstr);
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
1537
                         _("domain with uuid '%s' is already defined"),
1538 1539 1540 1541
                         uuidstr);
        virDomainDefFree(def);
        return NULL;
    }
1542

1543 1544 1545 1546
    if (!(vm = virDomainAssignDef(conn,
                                  &driver->domains,
                                  def))) {
        virDomainDefFree(def);
D
Daniel P. Berrange 已提交
1547 1548 1549
        return NULL;
    }

1550 1551 1552
    if (qemudStartVMDaemon(conn, driver, vm, NULL) < 0) {
        virDomainRemoveInactive(&driver->domains,
                                vm);
D
Daniel P. Berrange 已提交
1553 1554
        return NULL;
    }
1555 1556 1557
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_STARTED,
                             VIR_DOMAIN_EVENT_STARTED_BOOTED);
D
Daniel P. Berrange 已提交
1558

1559
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
1560
    if (dom) dom->id = vm->def->id;
1561
    return dom;
D
Daniel P. Berrange 已提交
1562 1563 1564
}


1565
static int qemudDomainSuspend(virDomainPtr dom) {
1566
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
D
Daniel P. Berrange 已提交
1567
    char *info;
1568
    virDomainObjPtr vm = virDomainFindByID(&driver->domains, dom->id);
D
Daniel P. Berrange 已提交
1569
    if (!vm) {
1570
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN, _("no domain with matching id %d"), dom->id);
D
Daniel P. Berrange 已提交
1571 1572
        return -1;
    }
1573
    if (!virDomainIsActive(vm)) {
1574 1575
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("domain is not running"));
D
Daniel P. Berrange 已提交
1576 1577
        return -1;
    }
1578
    if (vm->state == VIR_DOMAIN_PAUSED)
D
Daniel P. Berrange 已提交
1579
        return 0;
D
Daniel P. Berrange 已提交
1580

1581
    if (qemudMonitorCommand(driver, vm, "stop", &info) < 0) {
1582 1583
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("suspend operation failed"));
D
Daniel P. Berrange 已提交
1584 1585
        return -1;
    }
1586
    vm->state = VIR_DOMAIN_PAUSED;
1587
    qemudDebug("Reply %s", info);
1588 1589 1590
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_SUSPENDED,
                             VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1591
    VIR_FREE(info);
D
Daniel P. Berrange 已提交
1592 1593 1594 1595
    return 0;
}


1596
static int qemudDomainResume(virDomainPtr dom) {
1597
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
D
Daniel P. Berrange 已提交
1598
    char *info;
1599
    virDomainObjPtr vm = virDomainFindByID(&driver->domains, dom->id);
D
Daniel P. Berrange 已提交
1600
    if (!vm) {
1601 1602
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         _("no domain with matching id %d"), dom->id);
D
Daniel P. Berrange 已提交
1603 1604
        return -1;
    }
1605
    if (!virDomainIsActive(vm)) {
1606 1607
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("domain is not running"));
D
Daniel P. Berrange 已提交
1608 1609
        return -1;
    }
1610
    if (vm->state == VIR_DOMAIN_RUNNING)
D
Daniel P. Berrange 已提交
1611
        return 0;
1612
    if (qemudMonitorCommand(driver, vm, "cont", &info) < 0) {
1613 1614
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("resume operation failed"));
D
Daniel P. Berrange 已提交
1615 1616
        return -1;
    }
1617
    vm->state = VIR_DOMAIN_RUNNING;
1618
    qemudDebug("Reply %s", info);
1619 1620 1621
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_RESUMED,
                             VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1622
    VIR_FREE(info);
D
Daniel P. Berrange 已提交
1623
    return 0;
D
Daniel P. Berrange 已提交
1624 1625 1626
}


1627 1628
static int qemudDomainShutdown(virDomainPtr dom) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
1629
    virDomainObjPtr vm = virDomainFindByID(&driver->domains, dom->id);
1630 1631 1632 1633
    char* info;

    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1634
                         _("no domain with matching id %d"), dom->id);
1635 1636 1637 1638 1639
        return -1;
    }

    if (qemudMonitorCommand(driver, vm, "system_powerdown", &info) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1640
                         "%s", _("shutdown operation failed"));
1641 1642
        return -1;
    }
D
Daniel Veillard 已提交
1643
    VIR_FREE(info);
1644 1645 1646 1647 1648
    return 0;

}


1649
static int qemudDomainDestroy(virDomainPtr dom) {
1650
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
1651
    virDomainObjPtr vm = virDomainFindByID(&driver->domains, dom->id);
1652

D
Daniel P. Berrange 已提交
1653
    if (!vm) {
1654
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1655
                         _("no domain with matching id %d"), dom->id);
D
Daniel P. Berrange 已提交
1656 1657
        return -1;
    }
1658

1659
    qemudShutdownVMDaemon(dom->conn, driver, vm);
1660 1661 1662
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_STOPPED,
                             VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1663
    if (!vm->persistent)
1664 1665
        virDomainRemoveInactive(&driver->domains,
                                vm);
1666
    return 0;
D
Daniel P. Berrange 已提交
1667 1668 1669
}


1670 1671
static char *qemudDomainGetOSType(virDomainPtr dom) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
1672
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1673 1674 1675 1676
    char *type;

    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1677
                         "%s", _("no domain with matching uuid"));
1678 1679 1680 1681
        return NULL;
    }

    if (!(type = strdup(vm->def->os.type))) {
1682 1683
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_MEMORY,
                         "%s", _("failed to allocate space for ostype"));
1684 1685 1686 1687 1688
        return NULL;
    }
    return type;
}

1689 1690 1691
/* Returns max memory in kb, 0 if error */
static unsigned long qemudDomainGetMaxMemory(virDomainPtr dom) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
1692
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1693 1694

    if (!vm) {
1695 1696 1697
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1698
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1699
                         _("no domain with matching uuid '%s'"), uuidstr);
1700 1701 1702 1703 1704 1705 1706 1707
        return 0;
    }

    return vm->def->maxmem;
}

static int qemudDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
1708
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1709 1710

    if (!vm) {
1711 1712 1713
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1714
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1715
                         _("no domain with matching uuid '%s'"), uuidstr);
1716 1717 1718 1719 1720
        return -1;
    }

    if (newmax < vm->def->memory) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
1721
                         "%s", _("cannot set max memory lower than current memory"));
1722 1723 1724 1725 1726 1727 1728 1729 1730
        return -1;
    }

    vm->def->maxmem = newmax;
    return 0;
}

static int qemudDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
1731
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1732 1733

    if (!vm) {
1734 1735 1736
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1737
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1738
                         _("no domain with matching uuid '%s'"), uuidstr);
1739 1740 1741
        return -1;
    }

1742
    if (virDomainIsActive(vm)) {
1743
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
1744
                         "%s", _("cannot set memory of an active domain"));
1745 1746 1747 1748 1749
        return -1;
    }

    if (newmem > vm->def->maxmem) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
1750
                         "%s", _("cannot set memory higher than max memory"));
1751 1752 1753 1754 1755 1756 1757
        return -1;
    }

    vm->def->memory = newmem;
    return 0;
}

1758
static int qemudDomainGetInfo(virDomainPtr dom,
1759
                              virDomainInfoPtr info) {
1760
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
1761
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
D
Daniel P. Berrange 已提交
1762
    if (!vm) {
1763 1764
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
D
Daniel P. Berrange 已提交
1765 1766 1767
        return -1;
    }

1768
    info->state = vm->state;
D
Daniel P. Berrange 已提交
1769

1770
    if (!virDomainIsActive(vm)) {
1771
        info->cpuTime = 0;
D
Daniel P. Berrange 已提交
1772
    } else {
1773
        if (qemudGetProcessInfo(&(info->cpuTime), vm->pid) < 0) {
1774
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, ("cannot read cputime for domain"));
D
Daniel P. Berrange 已提交
1775 1776 1777 1778
            return -1;
        }
    }

1779 1780 1781
    info->maxMem = vm->def->maxmem;
    info->memory = vm->def->memory;
    info->nrVirtCpu = vm->def->vcpus;
D
Daniel P. Berrange 已提交
1782 1783 1784 1785
    return 0;
}


D
Daniel P. Berrange 已提交
1786
static char *qemudEscape(const char *in, int shell)
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
{
    int len = 0;
    int i, j;
    char *out;

    /* To pass through the QEMU monitor, we need to use escape
       sequences: \r, \n, \", \\

       To pass through both QEMU + the shell, we need to escape
       the single character ' as the five characters '\\''
    */

    for (i = 0; in[i] != '\0'; i++) {
        switch(in[i]) {
        case '\r':
        case '\n':
        case '"':
        case '\\':
            len += 2;
            break;
        case '\'':
D
Daniel P. Berrange 已提交
1808 1809 1810 1811
            if (shell)
                len += 5;
            else
                len += 1;
1812 1813 1814 1815 1816 1817 1818
            break;
        default:
            len += 1;
            break;
        }
    }

1819
    if (VIR_ALLOC_N(out, len + 1) < 0)
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
        return NULL;

    for (i = j = 0; in[i] != '\0'; i++) {
        switch(in[i]) {
        case '\r':
            out[j++] = '\\';
            out[j++] = 'r';
            break;
        case '\n':
            out[j++] = '\\';
            out[j++] = 'n';
            break;
        case '"':
        case '\\':
            out[j++] = '\\';
            out[j++] = in[i];
            break;
        case '\'':
D
Daniel P. Berrange 已提交
1838 1839 1840 1841 1842 1843 1844 1845 1846
            if (shell) {
                out[j++] = '\'';
                out[j++] = '\\';
                out[j++] = '\\';
                out[j++] = '\'';
                out[j++] = '\'';
            } else {
                out[j++] = in[i];
            }
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
            break;
        default:
            out[j++] = in[i];
            break;
        }
    }
    out[j] = '\0';

    return out;
}

1858 1859 1860 1861 1862
static char *qemudEscapeMonitorArg(const char *in)
{
    return qemudEscape(in, 0);
}

D
Daniel P. Berrange 已提交
1863 1864 1865 1866
static char *qemudEscapeShellArg(const char *in)
{
    return qemudEscape(in, 1);
}
1867

1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
#define QEMUD_SAVE_MAGIC "LibvirtQemudSave"
#define QEMUD_SAVE_VERSION 1

struct qemud_save_header {
    char magic[sizeof(QEMUD_SAVE_MAGIC)-1];
    int version;
    int xml_len;
    int was_running;
    int unused[16];
};

1879
static int qemudDomainSave(virDomainPtr dom,
1880
                           const char *path) {
1881
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
1882
    virDomainObjPtr vm = virDomainFindByID(&driver->domains, dom->id);
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
    char *command, *info;
    int fd;
    char *safe_path;
    char *xml;
    struct qemud_save_header header;

    memset(&header, 0, sizeof(header));
    memcpy(header.magic, QEMUD_SAVE_MAGIC, sizeof(header.magic));
    header.version = QEMUD_SAVE_VERSION;

D
Daniel P. Berrange 已提交
1893
    if (!vm) {
1894
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1895
                         _("no domain with matching id %d"), dom->id);
D
Daniel P. Berrange 已提交
1896 1897
        return -1;
    }
1898

1899
    if (!virDomainIsActive(vm)) {
1900
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1901
                         "%s", _("domain is not running"));
D
Daniel P. Berrange 已提交
1902 1903
        return -1;
    }
1904 1905 1906 1907 1908 1909

    /* Pause */
    if (vm->state == VIR_DOMAIN_RUNNING) {
        header.was_running = 1;
        if (qemudDomainSuspend(dom) != 0) {
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1910
                             "%s", _("failed to pause domain"));
1911 1912 1913 1914 1915
            return -1;
        }
    }

    /* Get XML for the domain */
1916
    xml = virDomainDefFormat(dom->conn, vm->def, VIR_DOMAIN_XML_SECURE);
1917 1918
    if (!xml) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1919
                         "%s", _("failed to get domain xml"));
1920 1921 1922 1923 1924 1925 1926
        return -1;
    }
    header.xml_len = strlen(xml) + 1;

    /* Write header to file, followed by XML */
    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1927
                         _("failed to create '%s'"), path);
1928
        VIR_FREE(xml);
1929 1930 1931 1932 1933
        return -1;
    }

    if (safewrite(fd, &header, sizeof(header)) != sizeof(header)) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1934
                         "%s", _("failed to write save header"));
1935
        close(fd);
1936
        VIR_FREE(xml);
1937 1938 1939 1940 1941
        return -1;
    }

    if (safewrite(fd, xml, header.xml_len) != header.xml_len) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1942
                         "%s", _("failed to write xml"));
1943
        close(fd);
1944
        VIR_FREE(xml);
1945 1946 1947 1948
        return -1;
    }

    close(fd);
1949
    VIR_FREE(xml);
1950 1951 1952 1953 1954

    /* Migrate to file */
    safe_path = qemudEscapeShellArg(path);
    if (!safe_path) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1955
                         "%s", _("out of memory"));
1956 1957 1958 1959
        return -1;
    }
    if (asprintf (&command, "migrate \"exec:"
                  "dd of='%s' oflag=append conv=notrunc 2>/dev/null"
1960
                  "\"", safe_path) == -1) {
1961
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1962
                         "%s", _("out of memory"));
1963
        VIR_FREE(safe_path);
1964 1965 1966 1967 1968 1969
        return -1;
    }
    free(safe_path);

    if (qemudMonitorCommand(driver, vm, command, &info) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1970
                         "%s", _("migrate operation failed"));
1971
        VIR_FREE(command);
1972 1973 1974
        return -1;
    }

1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
    DEBUG ("migrate reply: %s", info);

    /* If the command isn't supported then qemu prints:
     * unknown command: migrate" */
    if (strstr(info, "unknown command:")) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                          "%s",
                          _("'migrate' not supported by this qemu"));
        VIR_FREE(info);
        VIR_FREE(command);
        return -1;
    }

1988 1989
    VIR_FREE(info);
    VIR_FREE(command);
1990 1991 1992

    /* Shut it down */
    qemudShutdownVMDaemon(dom->conn, driver, vm);
1993 1994 1995
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_STOPPED,
                             VIR_DOMAIN_EVENT_STOPPED_SAVED);
1996
    if (!vm->persistent)
1997 1998
        virDomainRemoveInactive(&driver->domains,
                                vm);
1999
    return 0;
D
Daniel P. Berrange 已提交
2000 2001 2002
}


2003
static int qemudDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) {
2004 2005
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2006 2007 2008
    int max;

    if (!vm) {
2009 2010 2011
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
2012
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
2013
                         _("no domain with matching uuid '%s'"), uuidstr);
2014 2015 2016
        return -1;
    }

2017
    if (virDomainIsActive(vm)) {
2018
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT, "%s",
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
                         _("cannot change vcpu count of an active domain"));
        return -1;
    }

    if ((max = qemudDomainGetMaxVcpus(dom)) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                         _("could not determine max vcpus for the domain"));
        return -1;
    }

    if (nvcpus > max) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                         _("requested vcpus is greater than max allowable"
                           " vcpus for the domain: %d > %d"), nvcpus, max);
        return -1;
    }

    vm->def->vcpus = nvcpus;
    return 0;
}

2040 2041 2042 2043 2044 2045 2046 2047

#if HAVE_SCHED_GETAFFINITY
static int
qemudDomainPinVcpu(virDomainPtr dom,
                   unsigned int vcpu,
                   unsigned char *cpumap,
                   int maplen) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
2048
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2049 2050 2051 2052
    cpu_set_t mask;
    int i, maxcpu;
    virNodeInfo nodeinfo;

2053
    if (!virDomainIsActive(vm)) {
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                         "%s",_("cannot pin vcpus on an inactive domain"));
        return -1;
    }

    if (vcpu > (vm->nvcpupids-1)) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                         _("vcpu number out of range %d > %d"),
                         vcpu, vm->nvcpupids);
        return -1;
    }

    if (virNodeInfoPopulate(dom->conn, &nodeinfo) < 0)
        return -1;

    maxcpu = maplen * 8;
    if (maxcpu > nodeinfo.cpus)
        maxcpu = nodeinfo.cpus;

    CPU_ZERO(&mask);
    for (i = 0 ; i < maxcpu ; i++) {
        if ((cpumap[i/8] >> (i % 8)) & 1)
            CPU_SET(i, &mask);
    }

    if (vm->vcpupids != NULL) {
        if (sched_setaffinity(vm->vcpupids[vcpu], sizeof(mask), &mask) < 0) {
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                             _("cannot set affinity: %s"), strerror(errno));
            return -1;
        }
    } else {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                         "%s", _("cpu affinity is not supported"));
        return -1;
    }

    return 0;
}

static int
qemudDomainGetVcpus(virDomainPtr dom,
                    virVcpuInfoPtr info,
                    int maxinfo,
                    unsigned char *cpumaps,
                    int maplen) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
2101
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2102 2103 2104
    virNodeInfo nodeinfo;
    int i, v, maxcpu;

2105
    if (!virDomainIsActive(vm)) {
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                         "%s",_("cannot pin vcpus on an inactive domain"));
        return -1;
    }

    if (virNodeInfoPopulate(dom->conn, &nodeinfo) < 0)
        return -1;

    maxcpu = maplen * 8;
    if (maxcpu > nodeinfo.cpus)
        maxcpu = nodeinfo.cpus;

    /* Clamp to actual number of vcpus */
    if (maxinfo > vm->nvcpupids)
        maxinfo = vm->nvcpupids;

    if (maxinfo < 1)
        return 0;

    if (info != NULL) {
        memset(info, 0, sizeof(*info) * maxinfo);
        for (i = 0 ; i < maxinfo ; i++) {
            info[i].number = i;
            info[i].state = VIR_VCPU_RUNNING;
            /* XXX cpu time, current pCPU mapping */
        }
    }

    if (cpumaps != NULL) {
        memset(cpumaps, 0, maplen * maxinfo);
        if (vm->vcpupids != NULL) {
            for (v = 0 ; v < maxinfo ; v++) {
                cpu_set_t mask;
                unsigned char *cpumap = VIR_GET_CPUMAP(cpumaps, maplen, v);
                CPU_ZERO(&mask);

                if (sched_getaffinity(vm->vcpupids[v], sizeof(mask), &mask) < 0) {
                    qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                                     _("cannot get affinity: %s"), strerror(errno));
                    return -1;
                }

                for (i = 0 ; i < maxcpu ; i++)
                    if (CPU_ISSET(i, &mask))
                        VIR_USE_CPU(cpumap, i);
            }
        } else {
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                             "%s", _("cpu affinity is not available"));
            return -1;
        }
    }

    return maxinfo;
}
#endif /* HAVE_SCHED_GETAFFINITY */


2164 2165
static int qemudDomainGetMaxVcpus(virDomainPtr dom) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
2166
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2167 2168 2169 2170
    const char *type;
    int ret;

    if (!vm) {
2171 2172 2173
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
2174
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
2175
                         _("no domain with matching uuid '%s'"), uuidstr);
2176 2177 2178
        return -1;
    }

2179
    if (!(type = virDomainVirtTypeToString(vm->def->virtType))) {
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         _("unknown virt type in domain definition '%d'"),
                         vm->def->virtType);
        return -1;
    }

    if ((ret = qemudGetMaxVCPUs(dom->conn, type)) < 0) {
        return -1;
    }

    return ret;
}


2194
static int qemudDomainRestore(virConnectPtr conn,
2195 2196
                       const char *path) {
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
2197 2198
    virDomainDefPtr def;
    virDomainObjPtr vm;
2199
    int fd;
2200
    int ret;
2201 2202 2203 2204 2205 2206
    char *xml;
    struct qemud_save_header header;

    /* Verify the header and read the XML */
    if ((fd = open(path, O_RDONLY)) < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2207
                         "%s", _("cannot read domain image"));
2208 2209 2210 2211 2212
        return -1;
    }

    if (saferead(fd, &header, sizeof(header)) != sizeof(header)) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2213
                         "%s", _("failed to read qemu header"));
2214 2215 2216 2217 2218 2219
        close(fd);
        return -1;
    }

    if (memcmp(header.magic, QEMUD_SAVE_MAGIC, sizeof(header.magic)) != 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2220
                         "%s", _("image magic is incorrect"));
2221 2222 2223 2224 2225 2226
        close(fd);
        return -1;
    }

    if (header.version > QEMUD_SAVE_VERSION) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2227
                         _("image version is not supported (%d > %d)"),
2228 2229 2230 2231 2232
                         header.version, QEMUD_SAVE_VERSION);
        close(fd);
        return -1;
    }

2233
    if (VIR_ALLOC_N(xml, header.xml_len) < 0) {
2234
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2235
                         "%s", _("out of memory"));
2236 2237 2238 2239 2240 2241
        close(fd);
        return -1;
    }

    if (saferead(fd, xml, header.xml_len) != header.xml_len) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2242
                         "%s", _("failed to read XML"));
2243
        close(fd);
2244
        VIR_FREE(xml);
2245 2246 2247 2248
        return -1;
    }

    /* Create a domain from this XML */
2249
    if (!(def = virDomainDefParseString(conn, driver->caps, xml))) {
2250
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2251
                         "%s", _("failed to parse XML"));
2252
        close(fd);
2253
        VIR_FREE(xml);
2254 2255
        return -1;
    }
2256
    VIR_FREE(xml);
2257 2258

    /* Ensure the name and UUID don't already exist in an active VM */
2259
    vm = virDomainFindByUUID(&driver->domains, def->uuid);
2260
    if (!vm)
2261
        vm = virDomainFindByName(&driver->domains, def->name);
2262
    if (vm && virDomainIsActive(vm)) {
2263
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2264
                         _("domain is already active as '%s'"), vm->def->name);
2265 2266 2267 2268
        close(fd);
        return -1;
    }

2269 2270 2271
    if (!(vm = virDomainAssignDef(conn,
                                  &driver->domains,
                                  def))) {
2272
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2273
                         "%s", _("failed to assign new VM"));
2274
        virDomainDefFree(def);
2275 2276 2277 2278 2279
        close(fd);
        return -1;
    }

    /* Set the migration source and start it up. */
2280
    vm->stdin_fd = fd;
2281
    ret = qemudStartVMDaemon(conn, driver, vm, "stdio");
2282
    close(fd);
2283
    vm->stdin_fd = -1;
2284
    if (ret < 0) {
2285
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2286
                         "%s", _("failed to start VM"));
2287
        if (!vm->persistent)
2288 2289
            virDomainRemoveInactive(&driver->domains,
                                    vm);
2290 2291 2292
        return -1;
    }

2293 2294 2295 2296
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_STARTED,
                             VIR_DOMAIN_EVENT_STARTED_RESTORED);

2297 2298 2299
    /* If it was running before, resume it now. */
    if (header.was_running) {
        char *info;
2300
        if (qemudMonitorCommand(driver, vm, "cont", &info) < 0) {
2301
            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2302
                             "%s", _("failed to resume domain"));
2303 2304
            return -1;
        }
2305
        VIR_FREE(info);
2306 2307 2308 2309
        vm->state = VIR_DOMAIN_RUNNING;
    }

    return 0;
D
Daniel P. Berrange 已提交
2310 2311 2312
}


2313
static char *qemudDomainDumpXML(virDomainPtr dom,
2314
                                int flags ATTRIBUTE_UNUSED) {
2315
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
2316
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
D
Daniel P. Berrange 已提交
2317
    if (!vm) {
2318 2319
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
2320
        return NULL;
D
Daniel P. Berrange 已提交
2321 2322
    }

2323 2324 2325 2326
    return virDomainDefFormat(dom->conn,
                              (flags & VIR_DOMAIN_XML_INACTIVE) && vm->newDef ?
                              vm->newDef : vm->def,
                              flags);
D
Daniel P. Berrange 已提交
2327 2328 2329
}


2330
static int qemudListDefinedDomains(virConnectPtr conn,
2331 2332 2333
                            char **const names, int nnames) {
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
    int got = 0, i;
2334 2335 2336 2337

    for (i = 0 ; i < driver->domains.count && got < nnames ; i++) {
        if (!virDomainIsActive(driver->domains.objs[i])) {
            if (!(names[got++] = strdup(driver->domains.objs[i]->def->name))) {
2338
                qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY,
2339
                                 "%s", _("failed to allocate space for VM name string"));
2340 2341
                goto cleanup;
            }
2342
        }
D
Daniel P. Berrange 已提交
2343
    }
2344

D
Daniel P. Berrange 已提交
2345
    return got;
2346 2347 2348

 cleanup:
    for (i = 0 ; i < got ; i++)
2349
        VIR_FREE(names[i]);
2350
    return -1;
D
Daniel P. Berrange 已提交
2351 2352
}

2353
static int qemudNumDefinedDomains(virConnectPtr conn) {
2354
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
2355 2356 2357 2358
    int n = 0, i;

    for (i = 0 ; i < driver->domains.count ; i++)
        if (!virDomainIsActive(driver->domains.objs[i]))
2359
            n++;
2360

2361
    return n;
D
Daniel P. Berrange 已提交
2362 2363 2364
}


2365
static int qemudDomainStart(virDomainPtr dom) {
2366
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
2367
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2368
    int ret;
2369 2370

    if (!vm) {
2371
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
2372
                         "%s", _("no domain with matching uuid"));
2373
        return -1;
2374 2375
    }

2376 2377 2378 2379 2380 2381 2382
    ret = qemudStartVMDaemon(dom->conn, driver, vm, NULL);
    if (ret < 0)
        return ret;
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_STARTED,
                             VIR_DOMAIN_EVENT_STARTED_BOOTED);
    return 0;
D
Daniel P. Berrange 已提交
2383 2384 2385
}


2386
static virDomainPtr qemudDomainDefine(virConnectPtr conn, const char *xml) {
2387
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
2388 2389
    virDomainDefPtr def;
    virDomainObjPtr vm;
2390
    virDomainPtr dom;
2391
    int newVM = 1;
2392

2393
    if (!(def = virDomainDefParseString(conn, driver->caps, xml)))
2394 2395
        return NULL;

2396 2397 2398 2399
    vm = virDomainFindByName(&driver->domains, def->name);
    if (vm)
        newVM = 0;

2400 2401 2402 2403
    if (!(vm = virDomainAssignDef(conn,
                                  &driver->domains,
                                  def))) {
        virDomainDefFree(def);
2404 2405
        return NULL;
    }
2406
    vm->persistent = 1;
2407

2408 2409
    if (virDomainSaveConfig(conn,
                            driver->configDir,
2410
                            vm->newDef ? vm->newDef : vm->def) < 0) {
2411 2412
        virDomainRemoveInactive(&driver->domains,
                                vm);
2413 2414 2415
        return NULL;
    }

2416 2417 2418 2419 2420 2421
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_DEFINED,
                             newVM ?
                             VIR_DOMAIN_EVENT_DEFINED_ADDED :
                             VIR_DOMAIN_EVENT_DEFINED_UPDATED);

2422
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
2423
    if (dom) dom->id = vm->def->id;
2424
    return dom;
D
Daniel P. Berrange 已提交
2425 2426
}

2427
static int qemudDomainUndefine(virDomainPtr dom) {
2428
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
2429
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
D
Daniel P. Berrange 已提交
2430 2431

    if (!vm) {
2432 2433
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
D
Daniel P. Berrange 已提交
2434 2435 2436
        return -1;
    }

2437
    if (virDomainIsActive(vm)) {
2438 2439
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot delete active domain"));
D
Daniel P. Berrange 已提交
2440 2441 2442
        return -1;
    }

2443 2444 2445 2446 2447 2448 2449
    if (!vm->persistent) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot undefine transient domain"));
        return -1;
    }

    if (virDomainDeleteConfig(dom->conn, driver->configDir, driver->autostartDir, vm) < 0)
D
Daniel P. Berrange 已提交
2450 2451
        return -1;

2452 2453 2454 2455
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_UNDEFINED,
                             VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);

2456 2457
    virDomainRemoveInactive(&driver->domains,
                            vm);
D
Daniel P. Berrange 已提交
2458 2459 2460 2461

    return 0;
}

2462
/* Return the disks name for use in monitor commands */
2463 2464
static char *qemudDiskDeviceName(const virDomainPtr dom,
                                 const virDomainDiskDefPtr disk) {
2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478

    int busid, devid;
    int ret;
    char *devname;

    if (virDiskNameToBusDeviceIndex(disk, &busid, &devid) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         _("cannot convert disk '%s' to bus/device index"),
                         disk->dst);
        return NULL;
    }

    switch (disk->bus) {
        case VIR_DOMAIN_DISK_BUS_IDE:
2479 2480 2481 2482
            if (disk->device== VIR_DOMAIN_DISK_DEVICE_DISK)
                ret = asprintf(&devname, "ide%d-hd%d", busid, devid);
            else
                ret = asprintf(&devname, "ide%d-cd%d", busid, devid);
2483 2484
            break;
        case VIR_DOMAIN_DISK_BUS_SCSI:
2485 2486 2487 2488
            if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
                ret = asprintf(&devname, "scsi%d-hd%d", busid, devid);
            else
                ret = asprintf(&devname, "scsi%d-cd%d", busid, devid);
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
            break;
        case VIR_DOMAIN_DISK_BUS_FDC:
            ret = asprintf(&devname, "floppy%d", devid);
            break;
        case VIR_DOMAIN_DISK_BUS_VIRTIO:
            ret = asprintf(&devname, "virtio%d", devid);
            break;
        default:
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                             _("Unsupported disk name mapping for bus '%s'"),
                             virDomainDiskBusTypeToString(disk->bus));
            return NULL;
    }

    if (ret == -1) {
        qemudReportError(dom->conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return NULL;
    }

    return devname;
}

static int qemudDomainChangeEjectableMedia(virDomainPtr dom,
                                           virDomainDeviceDefPtr dev)
{
2514
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
2515
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2516
    virDomainDiskDefPtr origdisk = NULL, newdisk;
2517
    char *cmd, *reply, *safe_path;
2518
    char *devname = NULL;
2519
    unsigned int qemuCmdFlags;
2520
    int i;
2521 2522 2523 2524 2525 2526 2527

    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
        return -1;
    }

2528
    origdisk = NULL;
2529
    newdisk = dev->data.disk;
2530 2531 2532 2533
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (vm->def->disks[i]->bus == newdisk->bus &&
            STREQ(vm->def->disks[i]->dst, newdisk->dst)) {
            origdisk = vm->def->disks[i];
2534
            break;
2535
        }
2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
    }

    if (!origdisk) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         _("No device with bus '%s' and target '%s'"),
                         virDomainDiskBusTypeToString(newdisk->bus),
                         newdisk->dst);
        return -1;
    }

    if (qemudExtractVersionInfo(vm->def->emulator,
                                NULL,
                                &qemuCmdFlags) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         _("Cannot determine QEMU argv syntax %s"),
                         vm->def->emulator);
        return -1;
    }

    if (qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE) {
        if (!(devname = qemudDiskDeviceName(dom, newdisk)))
            return -1;
    } else {
        /* Back compat for no -drive option */
        if (newdisk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
            devname = strdup(newdisk->dst);
        else if (newdisk->device == VIR_DOMAIN_DISK_DEVICE_CDROM &&
                 STREQ(newdisk->dst, "hdc"))
            devname = strdup("cdrom");
        else {
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                             _("Emulator version does not support removable "
                               "media for device '%s' and target '%s'"),
                               virDomainDiskDeviceTypeToString(newdisk->device),
                               newdisk->dst);
            return -1;
        }

        if (!devname) {
            qemudReportError(dom->conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
            return -1;
        }
    }
2579

2580
    if (newdisk->src) {
2581 2582
        safe_path = qemudEscapeMonitorArg(newdisk->src);
        if (!safe_path) {
2583 2584
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_MEMORY, NULL);
            VIR_FREE(devname);
2585 2586
            return -1;
        }
2587 2588
        if (asprintf (&cmd, "change %s \"%s\"", devname, safe_path) == -1) {
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_MEMORY, NULL);
2589
            VIR_FREE(safe_path);
2590
            VIR_FREE(devname);
2591 2592
            return -1;
        }
2593
        VIR_FREE(safe_path);
2594

2595 2596 2597
    } else if (asprintf(&cmd, "eject %s", devname) == -1) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_MEMORY, NULL);
        VIR_FREE(devname);
2598 2599
        return -1;
    }
2600
    VIR_FREE(devname);
2601 2602

    if (qemudMonitorCommand(driver, vm, cmd, &reply) < 0) {
2603 2604
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("cannot change cdrom media"));
2605
        VIR_FREE(cmd);
2606 2607
        return -1;
    }
2608 2609 2610 2611

    /* If the command failed qemu prints:
     * device not found, device is locked ...
     * No message is printed on success it seems */
2612
    DEBUG ("ejectable media change reply: %s", reply);
2613 2614 2615 2616 2617 2618 2619
    if (strstr(reply, "\ndevice ")) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("changing cdrom media failed"));
        VIR_FREE(reply);
        VIR_FREE(cmd);
        return -1;
    }
2620 2621
    VIR_FREE(reply);
    VIR_FREE(cmd);
2622

2623 2624
    VIR_FREE(origdisk->src);
    origdisk->src = newdisk->src;
2625
    newdisk->src = NULL;
2626
    origdisk->type = newdisk->type;
2627 2628 2629
    return 0;
}

2630
static int qemudDomainAttachPciDiskDevice(virDomainPtr dom, virDomainDeviceDefPtr dev)
2631 2632 2633 2634
{
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    int ret, i;
2635
    char *cmd, *reply, *s;
2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682
    char *safe_path;
    const char* type = virDomainDiskBusTypeToString(dev->data.disk->bus);

    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
        return -1;
    }

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(vm->def->disks[i]->dst, dev->data.disk->dst)) {
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                           _("target %s already exists"), dev->data.disk->dst);
            return -1;
        }
    }

    if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
        qemudReportError(dom->conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return -1;
    }

    safe_path = qemudEscapeMonitorArg(dev->data.disk->src);
    if (!safe_path) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("out of memory"));
        return -1;
    }

    ret = asprintf(&cmd, "pci_add 0 storage file=%s,if=%s",
                         safe_path, type);
    VIR_FREE(safe_path);
    if (ret == -1) {
        qemudReportError(dom->conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return ret;
    }

    if (qemudMonitorCommand(driver, vm, cmd, &reply) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         _("cannot attach %s disk"), type);
        VIR_FREE(cmd);
        return -1;
    }

    DEBUG ("pci_add reply: %s", reply);
    /* If the command succeeds qemu prints:
     * OK bus 0... */
2683 2684 2685 2686 2687 2688
#define PCI_ATTACH_OK_MSG "OK bus 0, slot "
    if ((s=strstr(reply, PCI_ATTACH_OK_MSG))) {
        char* dummy = s;
        s += strlen(PCI_ATTACH_OK_MSG);

        if (virStrToLong_i ((const char*)s, &dummy, 10, &dev->data.disk->slotnum) == -1)
J
Jim Meyering 已提交
2689
            qemudLog(QEMUD_WARN, "%s", _("Unable to parse slot number\n"));
2690
    } else {
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          _("adding %s disk failed"), type);
        VIR_FREE(reply);
        VIR_FREE(cmd);
        return -1;
    }

    vm->def->disks[vm->def->ndisks++] = dev->data.disk;
    qsort(vm->def->disks, vm->def->ndisks, sizeof(*vm->def->disks),
          virDomainDiskQSort);

    VIR_FREE(reply);
    VIR_FREE(cmd);
    return 0;
}
2706

2707 2708 2709
static int qemudDomainAttachUsbMassstorageDevice(virDomainPtr dom, virDomainDeviceDefPtr dev)
{
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
2710
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2711 2712
    int ret, i;
    char *safe_path;
2713 2714 2715 2716 2717 2718 2719 2720
    char *cmd, *reply;

    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
        return -1;
    }

2721 2722 2723 2724 2725 2726 2727 2728
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(vm->def->disks[i]->dst, dev->data.disk->dst)) {
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                           _("target %s already exists"), dev->data.disk->dst);
            return -1;
        }
    }

2729 2730 2731
    if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
        qemudReportError(dom->conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return -1;
2732 2733
    }

2734 2735 2736 2737 2738 2739 2740 2741 2742
    safe_path = qemudEscapeMonitorArg(dev->data.disk->src);
    if (!safe_path) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("out of memory"));
        return -1;
    }

    ret = asprintf(&cmd, "usb_add disk:%s", safe_path);
    VIR_FREE(safe_path);
2743 2744 2745 2746 2747 2748 2749
    if (ret == -1) {
        qemudReportError(dom->conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return ret;
    }

    if (qemudMonitorCommand(driver, vm, cmd, &reply) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2750
                         "%s", _("cannot attach usb disk"));
2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
        VIR_FREE(cmd);
        return -1;
    }

    DEBUG ("attach_usb reply: %s", reply);
    /* If the command failed qemu prints:
     * Could not add ... */
    if (strstr(reply, "Could not add ")) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s",
2761
                          _("adding usb disk failed"));
2762 2763 2764 2765
        VIR_FREE(reply);
        VIR_FREE(cmd);
        return -1;
    }
2766

2767 2768 2769
    vm->def->disks[vm->def->ndisks++] = dev->data.disk;
    qsort(vm->def->disks, vm->def->ndisks, sizeof(*vm->def->disks),
          virDomainDiskQSort);
2770

2771 2772 2773 2774 2775
    VIR_FREE(reply);
    VIR_FREE(cmd);
    return 0;
}

2776 2777 2778
static int qemudDomainAttachHostDevice(virDomainPtr dom, virDomainDeviceDefPtr dev)
{
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
2779
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2780 2781 2782 2783 2784 2785 2786 2787
    int ret;
    char *cmd, *reply;

    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
        return -1;
    }
2788 2789 2790 2791
    if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0) {
        qemudReportError(dom->conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return -1;
    }
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824

    if (dev->data.hostdev->source.subsys.usb.vendor) {
        ret = asprintf(&cmd, "usb_add host:%.4x:%.4x",
                       dev->data.hostdev->source.subsys.usb.vendor,
                       dev->data.hostdev->source.subsys.usb.product);
    } else {
        ret = asprintf(&cmd, "usb_add host:%.3d.%.3d",
                       dev->data.hostdev->source.subsys.usb.bus,
                       dev->data.hostdev->source.subsys.usb.device);
    }
    if (ret == -1) {
        qemudReportError(dom->conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return -1;
    }

    if (qemudMonitorCommand(driver, vm, cmd, &reply) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("cannot attach usb device"));
        VIR_FREE(cmd);
        return -1;
    }

    DEBUG ("attach_usb reply: %s", reply);
    /* If the command failed qemu prints:
     * Could not add ... */
    if (strstr(reply, "Could not add ")) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s",
                          _("adding usb device failed"));
        VIR_FREE(reply);
        VIR_FREE(cmd);
        return -1;
    }
2825

2826
    vm->def->hostdevs[vm->def->nhostdevs++] = dev->data.hostdev;
2827

2828 2829
    VIR_FREE(reply);
    VIR_FREE(cmd);
2830 2831 2832
    return 0;
}

2833 2834 2835
static int qemudDomainAttachDevice(virDomainPtr dom,
                                   const char *xml) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
2836
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2837
    virDomainDeviceDefPtr dev;
2838
    int ret = 0, supported = 0;
2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851

    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
        return -1;
    }

    if (!virDomainIsActive(vm)) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot attach device on inactive domain"));
        return -1;
    }

2852 2853 2854
    dev = virDomainDeviceDefParse(dom->conn,
                                  driver->caps,
                                  vm->def, xml);
2855 2856 2857 2858
    if (dev == NULL) {
        return -1;
    }

2859 2860 2861 2862 2863
    if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
        switch (dev->data.disk->device) {
            case VIR_DOMAIN_DISK_DEVICE_CDROM:
            case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
                supported = 1;
2864
                ret = qemudDomainChangeEjectableMedia(dom, dev);
2865 2866 2867 2868 2869 2870 2871 2872
                break;
            case VIR_DOMAIN_DISK_DEVICE_DISK:
                if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_USB) {
                    supported = 1;
                    ret = qemudDomainAttachUsbMassstorageDevice(dom, dev);
                } else if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_SCSI ||
                           dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_VIRTIO) {
                    supported = 1;
2873
                    ret = qemudDomainAttachPciDiskDevice(dom, dev);
2874 2875 2876
                }
                break;
        }
2877 2878 2879
    } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV &&
        dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
        dev->data.hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
2880
                supported = 1;
2881
                ret = qemudDomainAttachHostDevice(dom, dev);
2882 2883 2884
    }

    if (!supported) {
2885
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
2886
                         "%s", _("this device type cannot be attached"));
2887 2888 2889 2890 2891 2892 2893
        ret = -1;
    }

    VIR_FREE(dev);
    return ret;
}

2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922
static int qemudDomainDetachPciDiskDevice(virDomainPtr dom, virDomainDeviceDefPtr dev)
{
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    int i, ret = -1;
    char *cmd, *reply;
    virDomainDiskDefPtr detach = NULL;

    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
        return -1;
    }

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(vm->def->disks[i]->dst, dev->data.disk->dst)) {
            detach = vm->def->disks[i];
            break;
        }
    }

    if (!detach) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         _("disk %s not found"), dev->data.disk->dst);
        return -1;
    }

    if (detach->slotnum < 1) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2923
                         _("disk %s cannot be detached - invalid slot number %d"),
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945
                           detach->dst, detach->slotnum);
        return -1;
    }

    ret = asprintf(&cmd, "pci_del 0 %d", detach->slotnum);
    if (ret == -1) {
        qemudReportError(dom->conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return ret;
    }

    if (qemudMonitorCommand(driver, vm, cmd, &reply) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          _("failed to execute detach disk %s command"), detach->dst);
        VIR_FREE(cmd);
        return -1;
    }

    DEBUG ("pci_del reply: %s", reply);
    /* If the command fails due to a wrong slot qemu prints: invalid slot,
     * nothing is printed on success */
    if (strstr(reply, "invalid slot")) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2946
                          _("failed to detach disk %s: invalid slot %d"),
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010
                            detach->dst, detach->slotnum);
        ret = -1;
        goto out;
    }

    if (vm->def->ndisks > 1) {
        vm->def->disks[i] = vm->def->disks[--vm->def->ndisks];
        if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks) < 0) {
            qemudReportError(dom->conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
            ret = -1;
            goto out;
        }
        qsort(vm->def->disks, vm->def->ndisks, sizeof(*vm->def->disks),
              virDomainDiskQSort);
    } else {
        VIR_FREE(vm->def->disks[0]);
        vm->def->ndisks = 0;
    }
    ret = 0;
out:
    VIR_FREE(reply);
    VIR_FREE(cmd);
    return ret;
}

static int qemudDomainDetachDevice(virDomainPtr dom,
                                   const char *xml) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    virDomainDeviceDefPtr dev;
    int ret = 0;

    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
        return -1;
    }

    if (!virDomainIsActive(vm)) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot attach device on inactive domain"));
        return -1;
    }

    dev = virDomainDeviceDefParse(dom->conn, driver->caps, vm->def, xml);
    if (dev == NULL) {
        return -1;
    }

    if (dev->type == VIR_DOMAIN_DEVICE_DISK &&
        dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
        (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_SCSI ||
         dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_VIRTIO))
                    ret = qemudDomainDetachPciDiskDevice(dom, dev);
    else {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                         "%s", _("only SCSI or virtio disk device can be detached dynamically"));
        ret = -1;
    }

    VIR_FREE(dev);
    return ret;
}

3011
static int qemudDomainGetAutostart(virDomainPtr dom,
3012 3013
                            int *autostart) {
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
3014
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3015 3016

    if (!vm) {
3017 3018
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
3019 3020 3021 3022 3023 3024 3025 3026
        return -1;
    }

    *autostart = vm->autostart;

    return 0;
}

3027
static int qemudDomainSetAutostart(virDomainPtr dom,
3028
                                   int autostart) {
3029
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
3030
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3031 3032
    char *configFile = NULL, *autostartLink = NULL;
    int ret = -1;
3033 3034

    if (!vm) {
3035 3036
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
3037 3038 3039
        return -1;
    }

3040 3041 3042 3043 3044 3045
    if (!vm->persistent) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot set autostart for transient domain"));
        return -1;
    }

3046 3047 3048 3049 3050
    autostart = (autostart != 0);

    if (vm->autostart == autostart)
        return 0;

3051 3052 3053 3054 3055
    if ((configFile = virDomainConfigFile(dom->conn, driver->configDir, vm->def->name)) == NULL)
        goto cleanup;
    if ((autostartLink = virDomainConfigFile(dom->conn, driver->autostartDir, vm->def->name)) == NULL)
        goto cleanup;

3056 3057 3058
    if (autostart) {
        int err;

3059
        if ((err = virFileMakePath(driver->autostartDir))) {
3060
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
3061
                             _("cannot create autostart directory %s: %s"),
3062
                             driver->autostartDir, strerror(err));
3063
            goto cleanup;
3064 3065
        }

3066
        if (symlink(configFile, autostartLink) < 0) {
3067
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
3068 3069 3070
                             _("Failed to create symlink '%s to '%s': %s"),
                             autostartLink, configFile, strerror(errno));
            goto cleanup;
3071 3072
        }
    } else {
3073
        if (unlink(autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
3074
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
3075
                             _("Failed to delete symlink '%s': %s"),
3076 3077
                             autostartLink, strerror(errno));
            goto cleanup;
3078 3079 3080
        }
    }

3081
    vm->autostart = autostart;
3082
    ret = 0;
3083

3084 3085 3086 3087 3088
cleanup:
    VIR_FREE(configFile);
    VIR_FREE(autostartLink);

    return ret;
3089 3090
}

3091 3092 3093 3094 3095 3096 3097 3098 3099
/* This uses the 'info blockstats' monitor command which was
 * integrated into both qemu & kvm in late 2007.  If the command is
 * not supported we detect this and return the appropriate error.
 */
static int
qemudDomainBlockStats (virDomainPtr dom,
                       const char *path,
                       struct _virDomainBlockStats *stats)
{
3100
    struct qemud_driver *driver =
3101
        (struct qemud_driver *)dom->conn->privateData;
3102
    char *dummy, *info = NULL;
3103
    const char *p, *eol;
3104
    const char *qemu_dev_name = NULL;
3105
    size_t len;
3106
    int i, ret = -1;
3107
    const virDomainObjPtr vm = virDomainFindByID(&driver->domains, dom->id);
3108
    virDomainDiskDefPtr disk = NULL;
3109 3110 3111 3112 3113 3114

    if (!vm) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                          _("no domain with matching id %d"), dom->id);
        return -1;
    }
3115
    if (!virDomainIsActive (vm)) {
3116 3117 3118 3119 3120
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("domain is not running"));
        return -1;
    }

3121 3122 3123 3124 3125 3126 3127 3128
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(path, vm->def->disks[i]->dst)) {
            disk = vm->def->disks[i];
            break;
        }
    }

    if (!disk) {
3129 3130 3131 3132 3133
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                          _("invalid path: %s"), path);
        return -1;
    }

3134 3135 3136
    qemu_dev_name = qemudDiskDeviceName(dom, disk);
    if (!qemu_dev_name)
        return -1;
3137 3138 3139 3140 3141
    len = strlen (qemu_dev_name);

    if (qemudMonitorCommand (driver, vm, "info blockstats", &info) < 0) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("'info blockstats' command failed"));
3142
        goto out;
3143 3144 3145 3146 3147 3148 3149 3150
    }
    DEBUG ("info blockstats reply: %s", info);

    /* If the command isn't supported then qemu prints the supported
     * info commands, so the output starts "info ".  Since this is
     * unlikely to be the name of a block device, we can use this
     * to detect if qemu supports the command.
     */
3151
    if (STRPREFIX (info, "info ")) {
3152 3153 3154
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                          "%s",
                          _("'info blockstats' not supported by this qemu"));
3155
        goto out;
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181
    }

    stats->rd_req = -1;
    stats->rd_bytes = -1;
    stats->wr_req = -1;
    stats->wr_bytes = -1;
    stats->errs = -1;

    /* The output format for both qemu & KVM is:
     *   blockdevice: rd_bytes=% wr_bytes=% rd_operations=% wr_operations=%
     *   (repeated for each block device)
     * where '%' is a 64 bit number.
     */
    p = info;

    while (*p) {
        if (STREQLEN (p, qemu_dev_name, len)
            && p[len] == ':' && p[len+1] == ' ') {

            eol = strchr (p, '\n');
            if (!eol)
                eol = p + strlen (p);

            p += len+2;         /* Skip to first label. */

            while (*p) {
3182
                if (STRPREFIX (p, "rd_bytes=")) {
3183 3184 3185
                    p += 9;
                    if (virStrToLong_ll (p, &dummy, 10, &stats->rd_bytes) == -1)
                        DEBUG ("error reading rd_bytes: %s", p);
3186
                } else if (STRPREFIX (p, "wr_bytes=")) {
3187 3188 3189
                    p += 9;
                    if (virStrToLong_ll (p, &dummy, 10, &stats->wr_bytes) == -1)
                        DEBUG ("error reading wr_bytes: %s", p);
3190
                } else if (STRPREFIX (p, "rd_operations=")) {
3191 3192 3193
                    p += 14;
                    if (virStrToLong_ll (p, &dummy, 10, &stats->rd_req) == -1)
                        DEBUG ("error reading rd_req: %s", p);
3194
                } else if (STRPREFIX (p, "wr_operations=")) {
3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205
                    p += 14;
                    if (virStrToLong_ll (p, &dummy, 10, &stats->wr_req) == -1)
                        DEBUG ("error reading wr_req: %s", p);
                } else
                    DEBUG ("unknown block stat near %s", p);

                /* Skip to next label. */
                p = strchr (p, ' ');
                if (!p || p >= eol) break;
                p++;
            }
3206 3207
            ret = 0;
            goto out;
3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218
        }

        /* Skip to next line. */
        p = strchr (p, '\n');
        if (!p) break;
        p++;
    }

    /* If we reach here then the device was not found. */
    qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                      _("device not found: %s (%s)"), path, qemu_dev_name);
3219 3220 3221 3222
 out:
    VIR_FREE(qemu_dev_name);
    VIR_FREE(info);
    return ret;
3223 3224
}

3225 3226 3227 3228 3229 3230 3231
static int
qemudDomainInterfaceStats (virDomainPtr dom,
                           const char *path,
                           struct _virDomainInterfaceStats *stats)
{
#ifdef __linux__
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
3232
    virDomainObjPtr vm = virDomainFindByID(&driver->domains, dom->id);
3233
    int i;
3234 3235 3236

    if (!vm) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
3237
                          _("no domain with matching id %d"), dom->id);
3238 3239 3240
        return -1;
    }

3241
    if (!virDomainIsActive(vm)) {
3242
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3243
                         "%s", _("domain is not running"));
3244 3245 3246 3247 3248
        return -1;
    }

    if (!path || path[0] == '\0') {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
3249
                         "%s", _("NULL or empty path"));
3250 3251 3252 3253
        return -1;
    }

    /* Check the path is one of the domain's network interfaces. */
3254 3255 3256
    for (i = 0 ; i < vm->def->nnets ; i++) {
        if (vm->def->nets[i]->ifname &&
            STREQ (vm->def->nets[i]->ifname, path))
3257
            goto ok;
3258 3259 3260
    }

    qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
3261
                      _("invalid path, '%s' is not a known interface"), path);
3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272
    return -1;
 ok:

    return linuxDomainInterfaceStats (dom->conn, path, stats);
#else
    qemudReportError (dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                      "%s", __FUNCTION__);
    return -1;
#endif
}

3273 3274 3275 3276 3277 3278 3279 3280
static int
qemudDomainBlockPeek (virDomainPtr dom,
                      const char *path,
                      unsigned long long offset, size_t size,
                      void *buffer,
                      unsigned int flags ATTRIBUTE_UNUSED)
{
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
3281
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3282
    int fd, ret = -1, i;
3283 3284 3285

    if (!vm) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
J
Jim Meyering 已提交
3286
                          "%s", _("no domain with matching uuid"));
3287 3288 3289 3290 3291
        return -1;
    }

    if (!path || path[0] == '\0') {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
3292
                         "%s", _("NULL or empty path"));
3293 3294 3295 3296
        return -1;
    }

    /* Check the path belongs to this domain. */
3297 3298 3299 3300
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (vm->def->disks[i]->src != NULL &&
            STREQ (vm->def->disks[i]->src, path))
            goto found;
3301 3302
    }
    qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
3303
                      "%s", _("invalid path"));
3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331
    return -1;

found:
    /* The path is correct, now try to open it and get its size. */
    fd = open (path, O_RDONLY);
    if (fd == -1) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
                          "%s", strerror (errno));
        goto done;
    }

    /* Seek and read. */
    /* NB. Because we configure with AC_SYS_LARGEFILE, off_t should
     * be 64 bits on all platforms.
     */
    if (lseek (fd, offset, SEEK_SET) == (off_t) -1 ||
        saferead (fd, buffer, size) == (ssize_t) -1) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
                          "%s", strerror (errno));
        goto done;
    }

    ret = 0;
 done:
    if (fd >= 0) close (fd);
    return ret;
}

R
Richard W.M. Jones 已提交
3332 3333 3334 3335 3336 3337 3338
static int
qemudDomainMemoryPeek (virDomainPtr dom,
                       unsigned long long offset, size_t size,
                       void *buffer,
                       unsigned int flags)
{
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
3339
    virDomainObjPtr vm = virDomainFindByID(&driver->domains, dom->id);
R
Richard W.M. Jones 已提交
3340 3341 3342 3343 3344 3345
    char cmd[256], *info;
    char tmp[] = TEMPDIR "/qemu.mem.XXXXXX";
    int fd = -1, ret = -1;

    if (flags != VIR_MEMORY_VIRTUAL) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
3346
                          "%s", _("QEMU driver only supports virtual memory addrs"));
R
Richard W.M. Jones 已提交
3347 3348 3349 3350 3351 3352 3353 3354 3355
        return -1;
    }

    if (!vm) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                          _("no domain with matching id %d"), dom->id);
        return -1;
    }

3356
    if (!virDomainIsActive(vm)) {
R
Richard W.M. Jones 已提交
3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("domain is not running"));
        return -1;
    }

    /* Create a temporary filename. */
    if ((fd = mkstemp (tmp)) == -1) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
                          "%s", strerror (errno));
        return -1;
    }

    /* Issue the memsave command. */
    snprintf (cmd, sizeof cmd, "memsave %llu %zi \"%s\"", offset, size, tmp);
    if (qemudMonitorCommand (driver, vm, cmd, &info) < 0) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3373
                          "%s", _("'memsave' command failed"));
R
Richard W.M. Jones 已提交
3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393
        goto done;
    }

    DEBUG ("memsave reply: %s", info);
    free (info);

    /* Read the memory file into buffer. */
    if (saferead (fd, buffer, size) == (ssize_t) -1) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
                          "%s", strerror (errno));
        goto done;
    }

    ret = 0;
done:
    if (fd >= 0) close (fd);
    unlink (tmp);
    return ret;
}

3394

3395 3396 3397
static int
qemudDomainEventRegister (virConnectPtr conn,
                          void *callback,
3398 3399
                          void *opaque,
                          virFreeCallback freecb)
3400 3401 3402 3403
{
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;

    return virDomainEventCallbackListAdd(conn, driver->domainEventCallbacks,
3404
                                         callback, opaque, freecb);
3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418
}

static int
qemudDomainEventDeregister (virConnectPtr conn,
                            void *callback)
{
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;

    return virDomainEventCallbackListRemove(conn, driver->domainEventCallbacks,
                                            callback);
}

static void qemudDomainEventDispatch (struct qemud_driver *driver,
                                      virDomainObjPtr vm,
3419 3420
                                      int event,
                                      int detail)
3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
{
    int i;
    virDomainEventCallbackListPtr cbList;

    cbList = driver->domainEventCallbacks;

    for(i=0 ; i < cbList->count ; i++) {
        if(cbList->callbacks[i] && cbList->callbacks[i]->cb) {
            virConnectPtr conn = cbList->callbacks[i]->conn;
            virDomainPtr dom = virGetDomain(conn, vm->def->name,
                                            vm->def->uuid);
            if (dom) {
                dom->id = virDomainIsActive(vm) ? vm->def->id : -1;
3434 3435 3436
                DEBUG("Dispatching callback %p %p event %d detail %d",
                      cbList->callbacks[i],
                      cbList->callbacks[i]->cb, event, detail);
3437
                cbList->callbacks[i]->cb(cbList->callbacks[i]->conn,
3438
                                         dom, event, detail,
3439 3440 3441 3442 3443 3444 3445 3446
                                         cbList->callbacks[i]->opaque);
                virDomainFree(dom);
            }
        }
    }

}

D
Daniel Veillard 已提交
3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586
/* Migration support. */

/* Prepare is the first step, and it runs on the destination host.
 *
 * This starts an empty VM listening on a TCP port.
 */
static int
qemudDomainMigratePrepare2 (virConnectPtr dconn,
                            char **cookie ATTRIBUTE_UNUSED,
                            int *cookielen ATTRIBUTE_UNUSED,
                            const char *uri_in,
                            char **uri_out,
                            unsigned long flags ATTRIBUTE_UNUSED,
                            const char *dname,
                            unsigned long resource ATTRIBUTE_UNUSED,
                            const char *dom_xml)
{
    static int port = 0;
    struct qemud_driver *driver = (struct qemud_driver *)dconn->privateData;
    virDomainDefPtr def;
    virDomainObjPtr vm = NULL;
    int this_port;
    char hostname [HOST_NAME_MAX+1];
    char migrateFrom [64];
    const char *p;

    if (!dom_xml) {
        qemudReportError (dconn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                          "%s", _("no domain XML passed"));
        return -1;
    }

    /* The URI passed in may be NULL or a string "tcp://somehostname:port".
     *
     * If the URI passed in is NULL then we allocate a port number
     * from our pool of port numbers and return a URI of
     * "tcp://ourhostname:port".
     *
     * If the URI passed in is not NULL then we try to parse out the
     * port number and use that (note that the hostname is assumed
     * to be a correct hostname which refers to the target machine).
     */
    if (uri_in == NULL) {
        this_port = QEMUD_MIGRATION_FIRST_PORT + port++;
        if (port == QEMUD_MIGRATION_NUM_PORTS) port = 0;

        /* Get hostname */
        if (gethostname (hostname, HOST_NAME_MAX+1) == -1) {
            qemudReportError (dconn, NULL, NULL, VIR_ERR_SYSTEM_ERROR,
                              "%s", strerror (errno));
            return -1;
        }

        /* Caller frees */
        if (asprintf(uri_out, "tcp:%s:%d", hostname, this_port) < 0) {
            qemudReportError (dconn, NULL, NULL, VIR_ERR_NO_MEMORY,
                              "%s", strerror (errno));
            return -1;
        }
    } else {
        /* Check the URI starts with "tcp:".  We will escape the
         * URI when passing it to the qemu monitor, so bad
         * characters in hostname part don't matter.
         */
        if (!STREQLEN (uri_in, "tcp:", 6)) {
            qemudReportError (dconn, NULL, NULL, VIR_ERR_INVALID_ARG,
                  "%s", _("only tcp URIs are supported for KVM migrations"));
            return -1;
        }

        /* Get the port number. */
        p = strrchr (uri_in, ':');
        p++; /* definitely has a ':' in it, see above */
        this_port = virParseNumber (&p);
        if (this_port == -1 || p-uri_in != strlen (uri_in)) {
            qemudReportError (dconn, NULL, NULL, VIR_ERR_INVALID_ARG,
                              "%s", _("URI did not have ':port' at the end"));
            return -1;
        }
    }

    /* Parse the domain XML. */
    if (!(def = virDomainDefParseString(dconn, driver->caps, dom_xml))) {
        qemudReportError (dconn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("failed to parse XML"));
        return -1;
    }

    /* Target domain name, maybe renamed. */
    dname = dname ? dname : def->name;

#if 1
    /* Ensure the name and UUID don't already exist in an active VM */
    vm = virDomainFindByUUID(&driver->domains, def->uuid);
#else
    /* For TESTING ONLY you can change #if 1 -> #if 0 above and use
     * this code which lets you do localhost migrations.  You must still
     * supply a fresh 'dname' but this code assigns a random UUID.
     */
    if (virUUIDGenerate (def->uuid) == -1) {
        qemudReportError (dconn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
            _("could not generate random UUID"));
    }
#endif

    if (!vm) vm = virDomainFindByName(&driver->domains, dname);
    if (vm) {
        if (virDomainIsActive(vm)) {
            qemudReportError (dconn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
                              _("domain with the same name or UUID already exists as '%s'"),
                              vm->def->name);
            virDomainDefFree(def);
            return -1;
        }
    }

    if (!(vm = virDomainAssignDef(dconn,
                                  &driver->domains,
                                  def))) {
        qemudReportError (dconn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("failed to assign new VM"));
        virDomainDefFree(def);
        return -1;
    }

    /* Domain starts inactive, even if the domain XML had an id field. */
    vm->def->id = -1;

    /* Start the QEMU daemon, with the same command-line arguments plus
     * -incoming tcp:0.0.0.0:port
     */
    snprintf (migrateFrom, sizeof (migrateFrom), "tcp:0.0.0.0:%d", this_port);
    if (qemudStartVMDaemon (dconn, driver, vm, migrateFrom) < 0) {
        qemudReportError (dconn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("failed to start listening VM"));
        if (!vm->persistent)
            virDomainRemoveInactive(&driver->domains, vm);

        return -1;
    }
3587 3588 3589
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_STARTED,
                             VIR_DOMAIN_EVENT_STARTED_MIGRATED);
D
Daniel Veillard 已提交
3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621

    return 0;
}

/* Perform is the second step, and it runs on the source host. */
static int
qemudDomainMigratePerform (virDomainPtr dom,
                           const char *cookie ATTRIBUTE_UNUSED,
                           int cookielen ATTRIBUTE_UNUSED,
                           const char *uri,
                           unsigned long flags ATTRIBUTE_UNUSED,
                           const char *dname ATTRIBUTE_UNUSED,
                           unsigned long resource)
{
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
    virDomainObjPtr vm = virDomainFindByID(&driver->domains, dom->id);
    char *safe_uri;
    char cmd[HOST_NAME_MAX+50];
    char *info;

    if (!vm) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                          _("no domain with matching id %d"), dom->id);
        return -1;
    }

    if (!virDomainIsActive(vm)) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("domain is not running"));
        return -1;
    }

3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
    if (!(flags & VIR_MIGRATE_LIVE)) {
        /* Pause domain for non-live migration */
        snprintf(cmd, sizeof cmd, "%s", "stop");
        qemudMonitorCommand (driver, vm, cmd, &info);
        DEBUG ("stop reply: %s", info);
        VIR_FREE(info);

        qemudDomainEventDispatch(driver, vm,
                                 VIR_DOMAIN_EVENT_SUSPENDED,
                                 VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED);
    }

D
Daniel Veillard 已提交
3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672
    if (resource > 0) {
        /* Issue migrate_set_speed command.  Don't worry if it fails. */
        snprintf (cmd, sizeof cmd, "migrate_set_speed %lum", resource);
        qemudMonitorCommand (driver, vm, cmd, &info);

        DEBUG ("migrate_set_speed reply: %s", info);
        VIR_FREE (info);
    }

    /* Issue the migrate command. */
    safe_uri = qemudEscapeMonitorArg (uri);
    if (!safe_uri) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
                          "%s", strerror (errno));
        return -1;
    }
    snprintf (cmd, sizeof cmd, "migrate \"%s\"", safe_uri);
    VIR_FREE (safe_uri);

    if (qemudMonitorCommand (driver, vm, cmd, &info) < 0) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("migrate operation failed"));
        return -1;
    }

    DEBUG ("migrate reply: %s", info);

    /* Now check for "fail" in the output string */
    if (strstr(info, "fail") != NULL) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          _("migrate failed: %s"), info);
        VIR_FREE(info);
        return -1;
    }

    VIR_FREE (info);

    /* Clean up the source domain. */
    qemudShutdownVMDaemon (dom->conn, driver, vm);
3673 3674 3675
    qemudDomainEventDispatch(driver, vm,
                             VIR_DOMAIN_EVENT_STOPPED,
                             VIR_DOMAIN_EVENT_STOPPED_MIGRATED);
D
Daniel Veillard 已提交
3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709
    if (!vm->persistent)
        virDomainRemoveInactive(&driver->domains, vm);

    return 0;
}

/* Finish is the third and final step, and it runs on the destination host. */
static virDomainPtr
qemudDomainMigrateFinish2 (virConnectPtr dconn,
                           const char *dname,
                           const char *cookie ATTRIBUTE_UNUSED,
                           int cookielen ATTRIBUTE_UNUSED,
                           const char *uri ATTRIBUTE_UNUSED,
                           unsigned long flags ATTRIBUTE_UNUSED,
                           int retcode)
{
    struct qemud_driver *driver = (struct qemud_driver *)dconn->privateData;
    virDomainObjPtr vm = virDomainFindByName(&driver->domains, dname);
    virDomainPtr dom;
    char *info = NULL;

    if (!vm) {
        qemudReportError (dconn, NULL, NULL, VIR_ERR_INVALID_DOMAIN,
                          _("no domain with matching name %s"), dname);
        return NULL;
    }

    /* Did the migration go as planned?  If yes, return the domain
     * object, but if no, clean up the empty qemu process.
     */
    if (retcode == 0) {
        dom = virGetDomain (dconn, vm->def->name, vm->def->uuid);
        VIR_FREE(info);
        vm->state = VIR_DOMAIN_RUNNING;
3710 3711 3712
        qemudDomainEventDispatch(driver, vm,
                                 VIR_DOMAIN_EVENT_RESUMED,
                                 VIR_DOMAIN_EVENT_RESUMED_MIGRATED);
D
Daniel Veillard 已提交
3713 3714 3715
        return dom;
    } else {
        qemudShutdownVMDaemon (dconn, driver, vm);
3716 3717 3718
        qemudDomainEventDispatch(driver, vm,
                                 VIR_DOMAIN_EVENT_STOPPED,
                                 VIR_DOMAIN_EVENT_STOPPED_FAILED);
D
Daniel Veillard 已提交
3719 3720 3721 3722 3723 3724
        if (!vm->persistent)
            virDomainRemoveInactive(&driver->domains, vm);
        return NULL;
    }
}

3725 3726 3727 3728 3729
static virDriver qemuDriver = {
    VIR_DRV_QEMU,
    "QEMU",
    qemudOpen, /* open */
    qemudClose, /* close */
D
Daniel Veillard 已提交
3730
    qemudSupportsFeature, /* supports_feature */
3731 3732
    qemudGetType, /* type */
    qemudGetVersion, /* version */
3733
    qemudGetHostname, /* hostname */
3734
    NULL, /* URI  */
3735 3736 3737 3738 3739
    qemudGetMaxVCPUs, /* getMaxVcpus */
    qemudGetNodeInfo, /* nodeGetInfo */
    qemudGetCapabilities, /* getCapabilities */
    qemudListDomains, /* listDomains */
    qemudNumDomains, /* numOfDomains */
3740
    qemudDomainCreate, /* domainCreateXML */
3741 3742 3743 3744 3745
    qemudDomainLookupByID, /* domainLookupByID */
    qemudDomainLookupByUUID, /* domainLookupByUUID */
    qemudDomainLookupByName, /* domainLookupByName */
    qemudDomainSuspend, /* domainSuspend */
    qemudDomainResume, /* domainResume */
3746
    qemudDomainShutdown, /* domainShutdown */
3747 3748 3749
    NULL, /* domainReboot */
    qemudDomainDestroy, /* domainDestroy */
    qemudDomainGetOSType, /* domainGetOSType */
3750 3751 3752
    qemudDomainGetMaxMemory, /* domainGetMaxMemory */
    qemudDomainSetMaxMemory, /* domainSetMaxMemory */
    qemudDomainSetMemory, /* domainSetMemory */
3753 3754 3755 3756
    qemudDomainGetInfo, /* domainGetInfo */
    qemudDomainSave, /* domainSave */
    qemudDomainRestore, /* domainRestore */
    NULL, /* domainCoreDump */
3757
    qemudDomainSetVcpus, /* domainSetVcpus */
3758 3759 3760 3761
#if HAVE_SCHED_GETAFFINITY
    qemudDomainPinVcpu, /* domainPinVcpu */
    qemudDomainGetVcpus, /* domainGetVcpus */
#else
3762 3763
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
3764
#endif
3765
    qemudDomainGetMaxVcpus, /* domainGetMaxVcpus */
3766 3767 3768 3769 3770 3771
    qemudDomainDumpXML, /* domainDumpXML */
    qemudListDefinedDomains, /* listDomains */
    qemudNumDefinedDomains, /* numOfDomains */
    qemudDomainStart, /* domainCreate */
    qemudDomainDefine, /* domainDefineXML */
    qemudDomainUndefine, /* domainUndefine */
3772
    qemudDomainAttachDevice, /* domainAttachDevice */
3773
    qemudDomainDetachDevice, /* domainDetachDevice */
3774 3775 3776 3777 3778
    qemudDomainGetAutostart, /* domainGetAutostart */
    qemudDomainSetAutostart, /* domainSetAutostart */
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
D
Daniel Veillard 已提交
3779 3780
    NULL, /* domainMigratePrepare (v1) */
    qemudDomainMigratePerform, /* domainMigratePerform */
3781
    NULL, /* domainMigrateFinish */
3782
    qemudDomainBlockStats, /* domainBlockStats */
3783
    qemudDomainInterfaceStats, /* domainInterfaceStats */
3784
    qemudDomainBlockPeek, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
3785
    qemudDomainMemoryPeek, /* domainMemoryPeek */
3786 3787 3788 3789
#if HAVE_NUMACTL
    qemudNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    qemudNodeGetFreeMemory,  /* getFreeMemory */
#else
3790
    NULL, /* nodeGetCellsFreeMemory */
3791
    NULL, /* getFreeMemory */
3792
#endif
3793 3794
    qemudDomainEventRegister, /* domainEventRegister */
    qemudDomainEventDeregister, /* domainEventDeregister */
D
Daniel Veillard 已提交
3795 3796
    qemudDomainMigratePrepare2, /* domainMigratePrepare2 */
    qemudDomainMigrateFinish2, /* domainMigrateFinish2 */
3797 3798 3799
};


3800
static virStateDriver qemuStateDriver = {
3801 3802 3803 3804
    .initialize = qemudStartup,
    .cleanup = qemudShutdown,
    .reload = qemudReload,
    .active = qemudActive,
3805
};
3806

3807
int qemuRegister(void) {
3808 3809 3810 3811 3812
    virRegisterDriver(&qemuDriver);
    virRegisterStateDriver(&qemuStateDriver);
    return 0;
}