qemu_driver.c 128.4 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 void qemuDriverLock(struct qemud_driver *driver)
{
    pthread_mutex_lock(&driver->lock);
}
static void qemuDriverUnlock(struct qemud_driver *driver)
{
    pthread_mutex_unlock(&driver->lock);
}

88 89 90 91 92 93 94 95 96
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 已提交
97
    qemudLog(QEMUD_ERR,
98
             "%s", _("Failed to set close-on-exec file descriptor flag\n"));
99 100 101 102 103 104 105 106 107 108 109 110 111
    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 已提交
112
    qemudLog(QEMUD_ERR,
113
             "%s", _("Failed to set non-blocking file descriptor flag\n"));
114 115 116 117
    return -1;
}


118 119 120 121

static void qemuDomainEventFlush(int timer, void *opaque);
static void qemuDomainEventQueue(struct qemud_driver *driver,
                                 virDomainEventPtr event);
122

123 124
static void qemudDispatchVMEvent(int watch,
                                 int fd,
125 126 127
                                 int events,
                                 void *opaque);

128 129
static int qemudStartVMDaemon(virConnectPtr conn,
                              struct qemud_driver *driver,
130 131
                              virDomainObjPtr vm,
                              const char *migrateFrom);
132

133 134
static void qemudShutdownVMDaemon(virConnectPtr conn,
                                  struct qemud_driver *driver,
135
                                  virDomainObjPtr vm);
136

137
static int qemudDomainGetMaxVcpus(virDomainPtr dom);
138

139
static int qemudMonitorCommand (const virDomainObjPtr vm,
140 141
                                const char *cmd,
                                char **reply);
142

J
Jim Meyering 已提交
143
static struct qemud_driver *qemu_driver = NULL;
144 145


146 147 148
static void
qemudAutostartConfigs(struct qemud_driver *driver) {
    unsigned int i;
149 150 151 152 153 154 155 156 157
    /* XXX: Figure out a better way todo this. The domain
     * startup code needs a connection handle in order
     * to lookup the bridge associated with a virtual
     * network
     */
    virConnectPtr conn = virConnectOpen(getuid() ?
                                        "qemu:///session" :
                                        "qemu:///system");
    /* Ignoring NULL conn which is mostly harmless here */
158 159

    for (i = 0 ; i < driver->domains.count ; i++) {
160
        virDomainObjPtr vm = driver->domains.objs[i];
161
        virDomainObjLock(vm);
162 163
        if (vm->autostart &&
            !virDomainIsActive(vm)) {
164
            int ret = qemudStartVMDaemon(conn, driver, vm, NULL);
165 166 167 168 169 170
            if (ret < 0) {
                virErrorPtr err = virGetLastError();
                qemudLog(QEMUD_ERR, _("Failed to autostart VM '%s': %s\n"),
                         vm->def->name,
                         err ? err->message : NULL);
            } else {
171 172 173 174 175 176
                virDomainEventPtr event =
                    virDomainEventNewFromObj(vm,
                                             VIR_DOMAIN_EVENT_STARTED,
                                             VIR_DOMAIN_EVENT_STARTED_BOOTED);
                if (event)
                    qemuDomainEventQueue(driver, event);
177
            }
178
        }
179
        virDomainObjUnlock(vm);
180
    }
181 182

    virConnectClose(conn);
183 184
}

185 186 187 188 189 190 191
/**
 * qemudStartup:
 *
 * Initialization function for the QEmu daemon
 */
static int
qemudStartup(void) {
192 193 194
    uid_t uid = geteuid();
    struct passwd *pw;
    char *base = NULL;
D
Daniel P. Berrange 已提交
195
    char driverConf[PATH_MAX];
196

197
    if (VIR_ALLOC(qemu_driver) < 0)
198 199
        return -1;

200 201 202
    pthread_mutex_init(&qemu_driver->lock, NULL);
    qemuDriverLock(qemu_driver);

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

206 207
    /* Init callback list */
    if(VIR_ALLOC(qemu_driver->domainEventCallbacks) < 0)
208
        goto out_of_memory;
209 210 211 212 213 214
    if (!(qemu_driver->domainEventQueue = virDomainEventQueueNew()))
        goto out_of_memory;

    if ((qemu_driver->domainEventTimer =
         virEventAddTimeout(-1, qemuDomainEventFlush, qemu_driver, NULL)) < 0)
        goto error;
215

216
    if (!uid) {
217 218 219
        if (asprintf(&qemu_driver->logDir,
                     "%s/log/libvirt/qemu", LOCAL_STATE_DIR) == -1)
            goto out_of_memory;
220

D
Daniel P. Berrange 已提交
221
        if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
222 223 224
            goto out_of_memory;
    } else {
        if (!(pw = getpwuid(uid))) {
225
            qemudLog(QEMUD_ERR, _("Failed to find user record for uid '%d': %s\n"),
226
                     uid, strerror(errno));
227
            goto error;
228 229
        }

230 231 232
        if (asprintf(&qemu_driver->logDir,
                     "%s/.libvirt/qemu/log", pw->pw_dir) == -1)
            goto out_of_memory;
233

234
        if (asprintf (&base, "%s/.libvirt", pw->pw_dir) == -1)
235 236 237 238 239 240
            goto out_of_memory;
    }

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

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

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

251
    VIR_FREE(base);
252 253 254

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

    if (qemudLoadDriverConfig(qemu_driver, driverConf) < 0) {
257
        goto error;
D
Daniel P. Berrange 已提交
258 259
    }

260 261 262 263
    if (virDomainLoadAllConfigs(NULL,
                                qemu_driver->caps,
                                &qemu_driver->domains,
                                qemu_driver->configDir,
264
                                qemu_driver->autostartDir,
265 266
                                NULL, NULL) < 0)
        goto error;
267 268
    qemudAutostartConfigs(qemu_driver);

269 270
    qemuDriverUnlock(qemu_driver);

271 272
    return 0;

273
out_of_memory:
J
Jim Meyering 已提交
274
    qemudLog (QEMUD_ERR,
275
              "%s", _("qemudStartup: out of memory\n"));
276 277 278
error:
    if (qemu_driver)
        qemuDriverUnlock(qemu_driver);
279
    VIR_FREE(base);
280
    qemudShutdown();
281 282 283
    return -1;
}

284 285 286 287
static void qemudNotifyLoadDomain(virDomainObjPtr vm, int newVM, void *opaque)
{
    struct qemud_driver *driver = opaque;

288 289 290 291 292 293 294 295
    if (newVM) {
        virDomainEventPtr event =
            virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_DEFINED,
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED);
        if (event)
            qemuDomainEventQueue(driver, event);
    }
296 297
}

298 299 300 301 302 303 304 305
/**
 * qemudReload:
 *
 * Function to restart the QEmu daemon, it will recheck the configuration
 * files and update its state and the networking
 */
static int
qemudReload(void) {
306 307 308
    if (!qemu_driver)
        return 0;

309
    qemuDriverLock(qemu_driver);
310 311 312 313
    virDomainLoadAllConfigs(NULL,
                            qemu_driver->caps,
                            &qemu_driver->domains,
                            qemu_driver->configDir,
314 315
                            qemu_driver->autostartDir,
                            qemudNotifyLoadDomain, qemu_driver);
316

317
    qemudAutostartConfigs(qemu_driver);
318
    qemuDriverUnlock(qemu_driver);
319 320

    return 0;
321 322
}

323 324 325 326 327 328 329 330 331 332
/**
 * 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) {
333
    unsigned int i;
334
    int active = 0;
335

336 337 338
    if (!qemu_driver)
        return 0;

339 340 341 342 343 344 345 346
    qemuDriverLock(qemu_driver);
    for (i = 0 ; i < qemu_driver->domains.count ; i++) {
        virDomainObjPtr vm = qemu_driver->domains.objs[i];
        virDomainObjLock(vm);
        if (virDomainIsActive(vm))
            active = 1;
        virDomainObjUnlock(vm);
    }
347

348 349
    qemuDriverUnlock(qemu_driver);
    return active;
350 351
}

352 353 354 355 356 357 358
/**
 * qemudShutdown:
 *
 * Shutdown the QEmu daemon, it will stop all active domains and networks
 */
static int
qemudShutdown(void) {
359
    unsigned int i;
360

361
    if (!qemu_driver)
362
        return -1;
363

364
    qemuDriverLock(qemu_driver);
365 366
    virCapabilitiesFree(qemu_driver->caps);

367
    /* shutdown active VMs */
368 369
    for (i = 0 ; i < qemu_driver->domains.count ; i++) {
        virDomainObjPtr dom = qemu_driver->domains.objs[i];
370
        virDomainObjLock(dom);
371 372
        if (virDomainIsActive(dom))
            qemudShutdownVMDaemon(NULL, qemu_driver, dom);
373
        virDomainObjUnlock(dom);
374
    }
375

376
    virDomainObjListFree(&qemu_driver->domains);
377

378
    VIR_FREE(qemu_driver->logDir);
379 380 381
    VIR_FREE(qemu_driver->configDir);
    VIR_FREE(qemu_driver->autostartDir);
    VIR_FREE(qemu_driver->vncTLSx509certdir);
J
Jim Meyering 已提交
382
    VIR_FREE(qemu_driver->vncListen);
D
Daniel P. Berrange 已提交
383

384 385
    /* Free domain callback list */
    virDomainEventCallbackListFree(qemu_driver->domainEventCallbacks);
386 387 388 389
    virDomainEventQueueFree(qemu_driver->domainEventQueue);

    if (qemu_driver->domainEventTimer != -1)
        virEventRemoveTimeout(qemu_driver->domainEventTimer);
390

391 392 393
    if (qemu_driver->brctl)
        brShutdown(qemu_driver->brctl);

394
    qemuDriverUnlock(qemu_driver);
395
    VIR_FREE(qemu_driver);
396 397

    return 0;
398 399 400
}

/* Return -1 for error, 1 to continue reading and 0 for success */
401
typedef int qemudHandlerMonitorOutput(virConnectPtr conn,
402
                                      virDomainObjPtr vm,
403 404 405 406
                                      const char *output,
                                      int fd);

static int
407
qemudReadMonitorOutput(virConnectPtr conn,
408
                       virDomainObjPtr vm,
409 410 411 412
                       int fd,
                       char *buf,
                       int buflen,
                       qemudHandlerMonitorOutput func,
413 414
                       const char *what,
                       int timeout)
415 416 417 418 419 420 421 422 423 424
{
    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) {
425
            qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
426
                             _("QEMU quit during %s startup\n%s"), what, buf);
427 428 429 430 431 432 433 434
            return -1;
        }
        if (ret < 0) {
            struct pollfd pfd = { .fd = fd, .events = POLLIN };
            if (errno == EINTR)
                continue;

            if (errno != EAGAIN) {
435
                qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
436
                                 _("Failure while reading %s startup output: %s"),
437 438 439 440
                                 what, strerror(errno));
                return -1;
            }

441
            ret = poll(&pfd, 1, timeout);
442
            if (ret == 0) {
443
                qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
444
                                 _("Timed out while reading %s startup output"), what);
445 446 447
                return -1;
            } else if (ret == -1) {
                if (errno != EINTR) {
448
                    qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
449
                                     _("Failure while reading %s startup output: %s"),
450 451 452 453 454 455 456 457 458
                                     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;

459
                qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
460
                                 _("Failure while reading %s startup output"), what);
461 462 463 464 465
                return -1;
            }
        } else {
            got += ret;
            buf[got] = '\0';
466
            if ((ret = func(conn, vm, buf, fd)) != 1)
467 468 469 470
                return ret;
        }
    }

471
    qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
472
                     _("Out of space while reading %s startup output"), what);
473 474 475 476 477
    return -1;

}

static int
478
qemudCheckMonitorPrompt(virConnectPtr conn ATTRIBUTE_UNUSED,
479
                        virDomainObjPtr vm,
480 481 482 483 484 485 486 487 488 489 490
                        const char *output,
                        int fd)
{
    if (strstr(output, "(qemu) ") == NULL)
        return 1; /* keep reading */

    vm->monitor = fd;

    return 0;
}

491
static int qemudOpenMonitor(virConnectPtr conn,
492
                            virDomainObjPtr vm,
493
                            const char *monitor) {
494 495 496 497
    int monfd;
    char buf[1024];
    int ret = -1;

D
Daniel Veillard 已提交
498
    if ((monfd = open(monitor, O_RDWR)) < 0) {
499
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
500
                         _("Unable to open monitor path %s"), monitor);
501 502 503
        return -1;
    }
    if (qemudSetCloseExec(monfd) < 0) {
504
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
505
                         "%s", _("Unable to set monitor close-on-exec flag"));
506 507 508
        goto error;
    }
    if (qemudSetNonBlock(monfd) < 0) {
509
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
510
                         "%s", _("Unable to put monitor into non-blocking mode"));
511 512 513
        goto error;
    }

514
    ret = qemudReadMonitorOutput(conn,
515
                                 vm, monfd,
516 517
                                 buf, sizeof(buf),
                                 qemudCheckMonitorPrompt,
518
                                 "monitor", 10000);
519 520 521 522 523

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

524 525 526 527 528
 error:
    close(monfd);
    return ret;
}

529 530
static int qemudExtractMonitorPath(virConnectPtr conn,
                                   const char *haystack,
531
                                   size_t *offset,
532
                                   char **path) {
533
    static const char needle[] = "char device redirected to";
534
    char *tmp, *dev;
535

536
    VIR_FREE(*path);
537
    /* First look for our magic string */
538 539 540 541 542
    if (!(tmp = strstr(haystack + *offset, needle))) {
        return 1;
    }
    tmp += sizeof(needle);
    dev = tmp;
543

544 545 546 547 548
    /*
     * And look for first whitespace character and nul terminate
     * to mark end of the pty path
     */
    while (*tmp) {
549
        if (c_isspace(*tmp)) {
550 551 552 553 554 555 556
            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';
557
            /* ... now further update offset till we get EOL */
558
            *offset = tmp - haystack;
559 560
            return 0;
        }
561
        tmp++;
562 563 564 565 566
    }

    /*
     * We found a path, but didn't find any whitespace,
     * so it must be still incomplete - we should at
567 568
     * least see a \n - indicate that we want to carry
     * on trying again
569
     */
570
    return 1;
571 572 573
}

static int
574
qemudFindCharDevicePTYs(virConnectPtr conn,
575
                        virDomainObjPtr vm,
576 577
                        const char *output,
                        int fd ATTRIBUTE_UNUSED)
578
{
579
    char *monitor = NULL;
580
    size_t offset = 0;
581
    int ret, i;
582 583 584 585 586

    /* 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.... */
587

588
    /* So first comes the monitor device */
589 590
    if ((ret = qemudExtractMonitorPath(conn, output, &offset, &monitor)) != 0)
        goto cleanup;
591

592
    /* then the serial devices */
593 594
    for (i = 0 ; i < vm->def->nserials ; i++) {
        virDomainChrDefPtr chr = vm->def->serials[i];
595 596 597 598
        if (chr->type == VIR_DOMAIN_CHR_TYPE_PTY) {
            if ((ret = qemudExtractMonitorPath(conn, output, &offset,
                                               &chr->data.file.path)) != 0)
                goto cleanup;
599 600 601 602
        }
    }

    /* and finally the parallel devices */
603 604
    for (i = 0 ; i < vm->def->nparallels ; i++) {
        virDomainChrDefPtr chr = vm->def->parallels[i];
605 606 607 608
        if (chr->type == VIR_DOMAIN_CHR_TYPE_PTY) {
            if ((ret = qemudExtractMonitorPath(conn, output, &offset,
                                               &chr->data.file.path)) != 0)
                goto cleanup;
609 610 611 612
        }
    }

    /* Got them all, so now open the monitor console */
613
    ret = qemudOpenMonitor(conn, vm, monitor);
614 615 616 617

cleanup:
    VIR_FREE(monitor);
    return ret;
618 619
}

620
static int qemudWaitForMonitor(virConnectPtr conn,
621
                               virDomainObjPtr vm) {
622
    char buf[1024]; /* Plenty of space to get startup greeting */
623
    int ret = qemudReadMonitorOutput(conn,
624
                                     vm, vm->stderr_fd,
625
                                     buf, sizeof(buf),
626
                                     qemudFindCharDevicePTYs,
627
                                     "console", 3000);
628 629

    buf[sizeof(buf)-1] = '\0';
630 631

    if (safewrite(vm->logfile, buf, strlen(buf)) < 0) {
632
        /* Log, but ignore failures to write logfile for VM */
633
        qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s\n"),
634 635 636 637 638
                 strerror(errno));
    }
    return ret;
}

639 640
static int
qemudDetectVcpuPIDs(virConnectPtr conn,
641
                    virDomainObjPtr vm) {
642 643 644 645 646 647
    char *qemucpus = NULL;
    char *line;
    int lastVcpu = -1;

    /* Only KVM has seperate threads for CPUs,
       others just use main QEMU process for CPU */
648
    if (vm->def->virtType != VIR_DOMAIN_VIRT_KVM)
649 650 651 652 653 654 655 656 657 658
        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;
    }

659
    if (vm->def->virtType != VIR_DOMAIN_VIRT_KVM) {
660 661 662 663
        vm->vcpupids[0] = vm->pid;
        return 0;
    }

664
    if (qemudMonitorCommand(vm, "info cpus", &qemucpus) < 0) {
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
        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;

723
    VIR_FREE(qemucpus);
724 725 726 727
    return 0;

error:
    VIR_FREE(vm->vcpupids);
728 729
    vm->nvcpupids = 0;
    VIR_FREE(qemucpus);
730 731 732 733 734 735 736 737

    /* 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;
}

738 739
static int
qemudInitCpus(virConnectPtr conn,
D
Daniel Veillard 已提交
740 741
              virDomainObjPtr vm,
              const char *migrateFrom) {
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    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 已提交
757 758 759 760 761 762
    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++)
763
            CPU_SET(i, &mask);
D
Daniel P. Berrange 已提交
764
    }
765 766 767 768 769 770 771 772 773 774 775 776

    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 已提交
777 778
    if (migrateFrom == NULL) {
        /* Allow the CPUS to start executing */
779
        if (qemudMonitorCommand(vm, "cont", &info) < 0) {
D
Daniel Veillard 已提交
780 781 782 783 784
            qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                             "%s", _("resume operation failed"));
            return -1;
        }
        VIR_FREE(info);
785 786 787 788 789 790
    }

    return 0;
}


791
static int qemudNextFreeVNCPort(struct qemud_driver *driver ATTRIBUTE_UNUSED) {
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    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;
}

827 828 829
static virDomainPtr qemudDomainLookupByName(virConnectPtr conn,
                                            const char *name);

830 831
static int qemudStartVMDaemon(virConnectPtr conn,
                              struct qemud_driver *driver,
832 833
                              virDomainObjPtr vm,
                              const char *migrateFrom) {
834
    const char **argv = NULL, **tmp;
835
    const char **progenv = NULL;
836
    int i, ret;
837
    char logfile[PATH_MAX];
838
    struct stat sb;
839 840
    int *tapfds = NULL;
    int ntapfds = 0;
841
    unsigned int qemuCmdFlags;
842
    fd_set keepfd;
843
    const char *emulator;
G
Guido Günther 已提交
844 845
    uid_t uid = geteuid();
    mode_t logmode;
846 847

    FD_ZERO(&keepfd);
848

849
    if (virDomainIsActive(vm)) {
850
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
851
                         "%s", _("VM is already active"));
852 853 854
        return -1;
    }

855 856 857
    if (vm->def->graphics &&
        vm->def->graphics->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
        vm->def->graphics->data.vnc.autoport) {
858
        int port = qemudNextFreeVNCPort(driver);
859
        if (port < 0) {
860
            qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
861
                             "%s", _("Unable to find an unused VNC port"));
862 863
            return -1;
        }
864 865
        vm->def->graphics->data.vnc.port = port;
    }
866

867
    if ((strlen(driver->logDir) + /* path */
868 869 870 871
         1 + /* Separator */
         strlen(vm->def->name) + /* basename */
         4 + /* suffix .log */
         1 /* NULL */) > PATH_MAX) {
872
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
873
                         _("config file path too long: %s/%s.log"),
874
                         driver->logDir, vm->def->name);
875 876
        return -1;
    }
877
    strcpy(logfile, driver->logDir);
878 879 880 881
    strcat(logfile, "/");
    strcat(logfile, vm->def->name);
    strcat(logfile, ".log");

882
    if (virFileMakePath(driver->logDir) < 0) {
883
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
884
                         _("cannot create log directory %s: %s"),
885
                         driver->logDir, strerror(errno));
886 887 888
        return -1;
    }

G
Guido Günther 已提交
889 890 891 892 893 894
    logmode = O_CREAT | O_WRONLY;
    if (uid != 0)
        logmode |= O_TRUNC;
    else
        logmode |= O_APPEND;
    if ((vm->logfile = open(logfile, logmode, S_IRUSR | S_IWUSR)) < 0) {
895
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
896
                         _("failed to create logfile %s: %s"),
897 898 899
                         logfile, strerror(errno));
        return -1;
    }
900 901
    if (qemudSetCloseExec(vm->logfile) < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
902
                         _("Unable to set VM logfile close-on-exec flag %s"),
903 904 905 906 907
                         strerror(errno));
        close(vm->logfile);
        vm->logfile = -1;
        return -1;
    }
908

909 910 911 912 913 914
    emulator = vm->def->emulator;
    if (!emulator)
        emulator = virDomainDefDefaultEmulator(conn, vm->def, driver->caps);
    if (!emulator)
        return -1;

915 916 917 918
    /* 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
     */
919
    if (stat(emulator, &sb) < 0) {
920 921
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                         _("Cannot find QEMU binary %s: %s"),
922
                         emulator,
923 924 925 926
                         strerror(errno));
        return -1;
    }

927
    if (qemudExtractVersionInfo(emulator,
928 929 930 931
                                NULL,
                                &qemuCmdFlags) < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                         _("Cannot determine QEMU argv syntax %s"),
932
                         emulator);
933 934
        return -1;
    }
935

936
    vm->def->id = driver->nextvmid++;
937
    if (qemudBuildCommandLine(conn, driver, vm,
938
                              qemuCmdFlags, &argv, &progenv,
939
                              &tapfds, &ntapfds, migrateFrom) < 0) {
940
        close(vm->logfile);
941
        vm->def->id = -1;
942 943 944 945
        vm->logfile = -1;
        return -1;
    }

946 947 948 949 950 951 952 953 954 955
    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++;
    }
956 957
    tmp = argv;
    while (*tmp) {
958
        if (safewrite(vm->logfile, *tmp, strlen(*tmp)) < 0)
959
            qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s\n"),
960
                     errno, strerror(errno));
961
        if (safewrite(vm->logfile, " ", 1) < 0)
962
            qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s\n"),
963 964 965
                     errno, strerror(errno));
        tmp++;
    }
966
    if (safewrite(vm->logfile, "\n", 1) < 0)
967
        qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s\n"),
968 969
                 errno, strerror(errno));

970 971 972
    vm->stdout_fd = -1;
    vm->stderr_fd = -1;

973 974 975
    for (i = 0 ; i < ntapfds ; i++)
        FD_SET(tapfds[i], &keepfd);

976
    ret = virExec(conn, argv, progenv, &keepfd, &vm->pid,
977 978
                  vm->stdin_fd, &vm->stdout_fd, &vm->stderr_fd,
                  VIR_EXEC_NONBLOCK);
979
    if (ret == 0)
980
        vm->state = migrateFrom ? VIR_DOMAIN_PAUSED : VIR_DOMAIN_RUNNING;
981 982
    else
        vm->def->id = -1;
983

984
    for (i = 0 ; argv[i] ; i++)
985 986
        VIR_FREE(argv[i]);
    VIR_FREE(argv);
987

988 989 990 991
    for (i = 0 ; progenv[i] ; i++)
        VIR_FREE(progenv[i]);
    VIR_FREE(progenv);

992 993 994
    if (tapfds) {
        for (i = 0 ; i < ntapfds ; i++) {
            close(tapfds[i]);
995
        }
996
        VIR_FREE(tapfds);
997 998
    }

999
    if (ret == 0) {
1000
        if (((vm->stdout_watch = virEventAddHandle(vm->stdout_fd,
1001 1002 1003 1004 1005
                                                   VIR_EVENT_HANDLE_READABLE |
                                                   VIR_EVENT_HANDLE_ERROR |
                                                   VIR_EVENT_HANDLE_HANGUP,
                                                   qemudDispatchVMEvent,
                                                   driver, NULL)) < 0) ||
1006 1007 1008 1009 1010
            ((vm->stderr_watch = virEventAddHandle(vm->stderr_fd,
                                                   VIR_EVENT_HANDLE_READABLE |
                                                   VIR_EVENT_HANDLE_ERROR |
                                                   VIR_EVENT_HANDLE_HANGUP,
                                                   qemudDispatchVMEvent,
1011
                                                   driver, NULL)) < 0) ||
1012 1013 1014
            (qemudWaitForMonitor(conn, vm) < 0) ||
            (qemudDetectVcpuPIDs(conn, vm) < 0) ||
            (qemudInitCpus(conn, vm, migrateFrom) < 0)) {
1015 1016 1017
            qemudShutdownVMDaemon(conn, driver, vm);
            return -1;
        }
1018 1019
    }

1020
    return ret;
1021 1022
}

1023
static int qemudVMData(struct qemud_driver *driver ATTRIBUTE_UNUSED,
1024
                       virDomainObjPtr vm, int fd) {
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
    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';

1041
        if (safewrite(vm->logfile, buf, ret) < 0) {
1042
            /* Log, but ignore failures to write logfile for VM */
1043
            qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s\n"),
1044 1045 1046 1047 1048 1049
                     strerror(errno));
        }
    }
}


1050
static void qemudShutdownVMDaemon(virConnectPtr conn ATTRIBUTE_UNUSED,
1051 1052
                                  struct qemud_driver *driver, virDomainObjPtr vm) {
    if (!virDomainIsActive(vm))
1053
        return;
1054

1055
    qemudLog(QEMUD_INFO, _("Shutting down VM '%s'\n"), vm->def->name);
1056 1057 1058

    kill(vm->pid, SIGTERM);

1059 1060
    qemudVMData(driver, vm, vm->stdout_fd);
    qemudVMData(driver, vm, vm->stderr_fd);
1061

1062 1063
    virEventRemoveHandle(vm->stdout_watch);
    virEventRemoveHandle(vm->stderr_watch);
1064 1065

    if (close(vm->logfile) < 0)
1066
        qemudLog(QEMUD_WARN, _("Unable to close logfile %d: %s\n"),
1067
                 errno, strerror(errno));
1068 1069
    close(vm->stdout_fd);
    close(vm->stderr_fd);
1070 1071 1072
    if (vm->monitor != -1)
        close(vm->monitor);
    vm->logfile = -1;
1073 1074
    vm->stdout_fd = -1;
    vm->stderr_fd = -1;
1075 1076 1077 1078 1079
    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 已提交
1080
            qemudLog(QEMUD_WARN,
1081
                     "%s", _("Got unexpected pid, damn\n"));
1082 1083 1084 1085
        }
    }

    vm->pid = -1;
1086
    vm->def->id = -1;
1087
    vm->state = VIR_DOMAIN_SHUTOFF;
1088
    VIR_FREE(vm->vcpupids);
1089
    vm->nvcpupids = 0;
1090 1091

    if (vm->newDef) {
1092
        virDomainDefFree(vm->def);
1093
        vm->def = vm->newDef;
1094
        vm->def->id = -1;
1095 1096 1097 1098 1099
        vm->newDef = NULL;
    }
}


1100
static void
1101
qemudDispatchVMEvent(int watch, int fd, int events, void *opaque) {
1102
    struct qemud_driver *driver = opaque;
1103
    virDomainObjPtr vm = NULL;
1104
    virDomainEventPtr event = NULL;
1105
    unsigned int i;
1106
    int quit = 0, failed = 0;
1107

1108
    qemuDriverLock(driver);
1109
    for (i = 0 ; i < driver->domains.count ; i++) {
1110 1111 1112 1113 1114 1115
        virDomainObjPtr tmpvm = driver->domains.objs[i];
        virDomainObjLock(tmpvm);
        if (virDomainIsActive(tmpvm) &&
            (tmpvm->stdout_watch == watch ||
             tmpvm->stderr_watch == watch)) {
            vm = tmpvm;
1116
            break;
1117
        }
1118
        virDomainObjUnlock(tmpvm);
1119 1120 1121
    }

    if (!vm)
1122
        goto cleanup;
1123

1124 1125
    if (vm->stdout_fd != fd &&
        vm->stderr_fd != fd) {
1126 1127 1128 1129 1130 1131 1132 1133
        failed = 1;
    } else {
        if (events & VIR_EVENT_HANDLE_READABLE) {
            if (qemudVMData(driver, vm, fd) < 0)
                failed = 1;
        } else {
            quit = 1;
        }
1134 1135
    }

1136
    if (failed || quit) {
1137 1138 1139 1140 1141
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         quit ?
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN :
                                         VIR_DOMAIN_EVENT_STOPPED_FAILED);
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
        qemudShutdownVMDaemon(NULL, driver, vm);
        if (!vm->persistent) {
            virDomainRemoveInactive(&driver->domains,
                                    vm);
            vm = NULL;
        }
    }

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
1153 1154
    if (event)
        qemuDomainEventQueue(driver, event);
1155
    qemuDriverUnlock(driver);
1156 1157
}

1158
static int
1159
qemudMonitorCommand (const virDomainObjPtr vm,
1160 1161
                     const char *cmd,
                     char **reply) {
D
Daniel P. Berrange 已提交
1162 1163
    int size = 0;
    char *buf = NULL;
1164
    size_t cmdlen = strlen(cmd);
D
Daniel P. Berrange 已提交
1165

1166 1167 1168
    if (safewrite(vm->monitor, cmd, cmdlen) != cmdlen)
        return -1;
    if (safewrite(vm->monitor, "\r", 1) != 1)
D
Daniel P. Berrange 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
        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 已提交
1181

1182 1183
            if (got == 0)
                goto error;
D
Daniel P. Berrange 已提交
1184 1185 1186 1187 1188
            if (got < 0) {
                if (errno == EINTR)
                    continue;
                if (errno == EAGAIN)
                    break;
1189
                goto error;
1190
            }
1191
            if (VIR_REALLOC_N(buf, size+got+1) < 0)
1192 1193
                goto error;

D
Daniel P. Berrange 已提交
1194 1195 1196 1197
            memmove(buf+size, data, got);
            buf[size+got] = '\0';
            size += got;
        }
1198

D
Daniel P. Berrange 已提交
1199
        /* Look for QEMU prompt to indicate completion */
D
Daniel P. Berrange 已提交
1200
        if (buf && ((tmp = strstr(buf, "\n(qemu) ")) != NULL)) {
C
Cole Robinson 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
            char *commptr = NULL, *nlptr = NULL;

            /* Preserve the newline */
            tmp[1] = '\0';

            /* The monitor doesn't dump clean output after we have written to
             * it. Every character we write dumps a bunch of useless stuff,
             * so the result looks like "cXcoXcomXcommXcommaXcommanXcommand"
             * Try to throw away everything before the first full command
             * occurence, and inbetween the command and the newline starting
             * the response
             */
            if ((commptr = strstr(buf, cmd)))
                memmove(buf, commptr, strlen(commptr)+1);
            if ((nlptr = strchr(buf, '\n')))
                memmove(buf+strlen(cmd), nlptr, strlen(nlptr)+1);

D
Daniel P. Berrange 已提交
1218 1219 1220 1221 1222 1223 1224
            break;
        }
    pollagain:
        /* Need to wait for more data */
        if (poll(&fd, 1, -1) < 0) {
            if (errno == EINTR)
                goto pollagain;
1225
            goto error;
D
Daniel P. Berrange 已提交
1226 1227 1228
        }
    }

1229 1230
    /* Log, but ignore failures to write logfile for VM */
    if (safewrite(vm->logfile, buf, strlen(buf)) < 0)
1231
        qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s\n"),
1232 1233
                 strerror(errno));

D
Daniel P. Berrange 已提交
1234 1235
    *reply = buf;
    return 0;
1236 1237 1238 1239 1240

 error:
    if (buf) {
        /* Log, but ignore failures to write logfile for VM */
        if (safewrite(vm->logfile, buf, strlen(buf)) < 0)
1241
            qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s\n"),
1242
                     strerror(errno));
1243
        VIR_FREE(buf);
1244 1245
    }
    return -1;
D
Daniel P. Berrange 已提交
1246 1247
}

1248 1249 1250 1251 1252 1253
/**
 * qemudProbe:
 *
 * Probe for the availability of the qemu driver, assume the
 * presence of QEmu emulation if the binaries are installed
 */
1254
static int qemudProbe(void)
1255 1256 1257
{
    if ((virFileExists("/usr/bin/qemu")) ||
        (virFileExists("/usr/bin/qemu-kvm")) ||
G
Guido Günther 已提交
1258
        (virFileExists("/usr/bin/kvm")) ||
1259 1260 1261 1262
        (virFileExists("/usr/bin/xenner")))
        return 1;

    return 0;
1263
}
1264

1265
static virDrvOpenStatus qemudOpen(virConnectPtr conn,
1266
                                  virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1267
                                  int flags ATTRIBUTE_UNUSED) {
1268 1269 1270
    uid_t uid = getuid();

    if (qemu_driver == NULL)
1271
        goto decline;
1272

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
    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)
1284 1285
        goto decline;

1286
    if (STRNEQ (conn->uri->scheme, "qemu"))
1287 1288
        goto decline;

1289
    if (uid != 0) {
1290
        if (STRNEQ (conn->uri->path, "/session"))
1291
            goto decline;
1292
    } else { /* root */
1293 1294
        if (STRNEQ (conn->uri->path, "/system") &&
            STRNEQ (conn->uri->path, "/session"))
1295
            goto decline;
1296 1297 1298 1299 1300
    }

    conn->privateData = qemu_driver;

    return VIR_DRV_OPEN_SUCCESS;
1301 1302

 decline:
1303
    return VIR_DRV_OPEN_DECLINED;
1304 1305 1306
}

static int qemudClose(virConnectPtr conn) {
1307
    struct qemud_driver *driver = conn->privateData;
1308 1309 1310

    /* Get rid of callbacks registered for this conn */
    virDomainEventCallbackListRemoveConn(conn, driver->domainEventCallbacks);
1311 1312 1313 1314 1315 1316

    conn->privateData = NULL;

    return 0;
}

D
Daniel Veillard 已提交
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
/* 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;
    }
}

1327
static const char *qemudGetType(virConnectPtr conn ATTRIBUTE_UNUSED) {
1328
    return "QEMU";
1329 1330
}

1331 1332 1333 1334 1335

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

    int r, fd;
1336

1337 1338
    fd = open(KVM_DEVICE, O_RDONLY);
    if (fd < 0) {
1339
        qemudLog(QEMUD_WARN, _("Unable to open %s: %s\n"), KVM_DEVICE, strerror(errno));
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
        return maxvcpus;
    }

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

    close(fd);
    return maxvcpus;
}


1352
static int qemudGetMaxVCPUs(virConnectPtr conn, const char *type) {
1353 1354 1355
    if (!type)
        return 16;

1356
    if (STRCASEEQ(type, "qemu"))
1357 1358 1359 1360
        return 16;

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

1364
    if (STRCASEEQ(type, "kqemu"))
1365
        return 1;
1366 1367 1368

    qemudReportError(conn, NULL, NULL, VIR_ERR_INVALID_ARG,
                     _("unknown type '%s'"), type);
1369 1370 1371
    return -1;
}

1372 1373 1374
static int qemudGetNodeInfo(virConnectPtr conn,
                            virNodeInfoPtr nodeinfo) {
    return virNodeInfoPopulate(conn, nodeinfo);
1375 1376 1377
}


1378
static char *qemudGetCapabilities(virConnectPtr conn) {
1379
    struct qemud_driver *driver = conn->privateData;
1380
    char *xml;
1381

1382
    qemuDriverLock(driver);
1383
    if ((xml = virCapabilitiesFormatXML(driver->caps)) == NULL)
1384 1385
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY,
                 "%s", _("failed to allocate space for capabilities support"));
1386
    qemuDriverUnlock(driver);
1387

1388
    return xml;
1389 1390 1391
}


1392 1393 1394 1395 1396 1397 1398 1399
#if HAVE_NUMACTL
static int
qemudNodeGetCellsFreeMemory(virConnectPtr conn,
                            unsigned long long *freeMems,
                            int startCell,
                            int maxCells)
{
    int n, lastCell, numCells;
1400
    int ret = -1;
1401 1402 1403 1404

    if (numa_available() < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_SUPPORT,
                         "%s", _("NUMA not supported on this host"));
1405
        goto cleanup;
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
    }
    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"));
1416
            goto cleanup;
1417 1418 1419
        }
        freeMems[numCells++] = mem;
    }
1420 1421 1422 1423
    ret = numCells;

cleanup:
    return ret;
1424 1425 1426 1427 1428
}

static unsigned long long
qemudNodeGetFreeMemory (virConnectPtr conn)
{
1429
    unsigned long long freeMem = -1;
1430
    int n;
1431

1432 1433 1434
    if (numa_available() < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_SUPPORT,
                         "%s", _("NUMA not supported on this host"));
1435
        goto cleanup;
1436 1437 1438 1439 1440 1441 1442
    }

    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"));
1443
            goto cleanup;
1444 1445 1446 1447
        }
        freeMem += mem;
    }

1448
cleanup:
1449 1450 1451 1452
    return freeMem;
}

#endif
1453

D
Daniel P. Berrange 已提交
1454 1455 1456
static int qemudGetProcessInfo(unsigned long long *cpuTime, int pid) {
    char proc[PATH_MAX];
    FILE *pidinfo;
1457
    unsigned long long usertime, systime;
D
Daniel P. Berrange 已提交
1458 1459 1460 1461 1462 1463

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

    if (!(pidinfo = fopen(proc, "r"))) {
1464
        /*printf("cannot read pid info");*/
D
Daniel P. Berrange 已提交
1465 1466 1467 1468 1469
        /* VM probably shut down, so fake 0 */
        *cpuTime = 0;
        return 0;
    }

1470
    if (fscanf(pidinfo, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %llu %llu", &usertime, &systime) != 2) {
1471
        qemudDebug("not enough arg");
D
Daniel P. Berrange 已提交
1472 1473 1474 1475 1476 1477 1478 1479
        return -1;
    }

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

1482
    qemudDebug("Got %llu %llu %llu", usertime, systime, *cpuTime);
D
Daniel P. Berrange 已提交
1483 1484 1485 1486 1487 1488 1489

    fclose(pidinfo);

    return 0;
}


1490
static virDomainPtr qemudDomainLookupByID(virConnectPtr conn,
1491
                                          int id) {
1492 1493 1494 1495
    struct qemud_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;

1496
    qemuDriverLock(driver);
1497
    vm  = virDomainFindByID(&driver->domains, id);
1498
    qemuDriverUnlock(driver);
1499 1500

    if (!vm) {
1501
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
1502
        goto cleanup;
1503 1504
    }

1505
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
1506
    if (dom) dom->id = vm->def->id;
1507 1508

cleanup:
1509 1510
    if (vm)
        virDomainObjUnlock(vm);
1511 1512
    return dom;
}
1513

1514
static virDomainPtr qemudDomainLookupByUUID(virConnectPtr conn,
1515
                                            const unsigned char *uuid) {
1516 1517 1518
    struct qemud_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
1519

1520
    qemuDriverLock(driver);
1521
    vm = virDomainFindByUUID(&driver->domains, uuid);
1522 1523
    qemuDriverUnlock(driver);

1524
    if (!vm) {
1525
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
1526
        goto cleanup;
1527 1528
    }

1529
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
1530
    if (dom) dom->id = vm->def->id;
1531 1532

cleanup:
1533 1534
    if (vm)
        virDomainObjUnlock(vm);
1535 1536
    return dom;
}
1537

1538
static virDomainPtr qemudDomainLookupByName(virConnectPtr conn,
1539
                                            const char *name) {
1540 1541 1542
    struct qemud_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
1543

1544
    qemuDriverLock(driver);
1545
    vm = virDomainFindByName(&driver->domains, name);
1546 1547
    qemuDriverUnlock(driver);

1548
    if (!vm) {
1549
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
1550
        goto cleanup;
1551 1552
    }

1553
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
1554
    if (dom) dom->id = vm->def->id;
1555 1556

cleanup:
1557 1558
    if (vm)
        virDomainObjUnlock(vm);
1559 1560 1561
    return dom;
}

1562
static int qemudGetVersion(virConnectPtr conn, unsigned long *version) {
1563 1564 1565
    struct qemud_driver *driver = conn->privateData;
    int ret = -1;

1566
    qemuDriverLock(driver);
1567
    if (qemudExtractVersion(conn, driver) < 0)
1568
        goto cleanup;
1569

1570
    *version = qemu_driver->qemuVersion;
1571 1572 1573
    ret = 0;

cleanup:
1574
    qemuDriverUnlock(driver);
1575
    return ret;
D
Daniel P. Berrange 已提交
1576 1577
}

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
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;
}

1600
static int qemudListDomains(virConnectPtr conn, int *ids, int nids) {
1601
    struct qemud_driver *driver = conn->privateData;
1602 1603
    int got = 0, i;

1604 1605 1606
    qemuDriverLock(driver);
    for (i = 0 ; i < driver->domains.count && got < nids ; i++) {
        virDomainObjLock(driver->domains.objs[i]);
1607 1608
        if (virDomainIsActive(driver->domains.objs[i]))
            ids[got++] = driver->domains.objs[i]->def->id;
1609 1610 1611
        virDomainObjUnlock(driver->domains.objs[i]);
    }
    qemuDriverUnlock(driver);
1612

D
Daniel P. Berrange 已提交
1613 1614
    return got;
}
1615

1616
static int qemudNumDomains(virConnectPtr conn) {
1617
    struct qemud_driver *driver = conn->privateData;
1618 1619
    int n = 0, i;

1620 1621 1622
    qemuDriverLock(driver);
    for (i = 0 ; i < driver->domains.count ; i++) {
        virDomainObjLock(driver->domains.objs[i]);
1623
        if (virDomainIsActive(driver->domains.objs[i]))
1624
            n++;
1625 1626 1627
        virDomainObjUnlock(driver->domains.objs[i]);
    }
    qemuDriverUnlock(driver);
1628

1629
    return n;
D
Daniel P. Berrange 已提交
1630
}
1631

1632
static virDomainPtr qemudDomainCreate(virConnectPtr conn, const char *xml,
1633
                                      unsigned int flags ATTRIBUTE_UNUSED) {
1634
    struct qemud_driver *driver = conn->privateData;
1635
    virDomainDefPtr def;
1636
    virDomainObjPtr vm = NULL;
1637
    virDomainPtr dom = NULL;
1638
    virDomainEventPtr event = NULL;
D
Daniel P. Berrange 已提交
1639

1640
    qemuDriverLock(driver);
1641
    if (!(def = virDomainDefParseString(conn, driver->caps, xml)))
1642
        goto cleanup;
1643

1644
    vm = virDomainFindByName(&driver->domains, def->name);
1645
    if (vm) {
1646
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
1647
                         _("domain '%s' is already defined"),
1648
                         def->name);
1649
        goto cleanup;
1650
    }
1651
    vm = virDomainFindByUUID(&driver->domains, def->uuid);
1652 1653 1654 1655 1656
    if (vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(def->uuid, uuidstr);
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
1657
                         _("domain with uuid '%s' is already defined"),
1658
                         uuidstr);
1659
        goto cleanup;
1660
    }
1661

1662 1663
    if (!(vm = virDomainAssignDef(conn,
                                  &driver->domains,
1664 1665 1666 1667
                                  def)))
        goto cleanup;

    def = NULL;
D
Daniel P. Berrange 已提交
1668

1669 1670 1671
    if (qemudStartVMDaemon(conn, driver, vm, NULL) < 0) {
        virDomainRemoveInactive(&driver->domains,
                                vm);
1672
        vm = NULL;
1673
        goto cleanup;
D
Daniel P. Berrange 已提交
1674
    }
1675 1676 1677 1678

    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
D
Daniel P. Berrange 已提交
1679

1680
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
1681
    if (dom) dom->id = vm->def->id;
1682 1683 1684

cleanup:
    virDomainDefFree(def);
1685 1686
    if (vm)
        virDomainObjUnlock(vm);
1687 1688
    if (event)
        qemuDomainEventQueue(driver, event);
1689
    qemuDriverUnlock(driver);
1690
    return dom;
D
Daniel P. Berrange 已提交
1691 1692 1693
}


1694
static int qemudDomainSuspend(virDomainPtr dom) {
1695
    struct qemud_driver *driver = dom->conn->privateData;
D
Daniel P. Berrange 已提交
1696
    char *info;
1697 1698
    virDomainObjPtr vm;
    int ret = -1;
1699
    virDomainEventPtr event = NULL;
1700

1701
    qemuDriverLock(driver);
1702
    vm = virDomainFindByID(&driver->domains, dom->id);
1703 1704
    qemuDriverUnlock(driver);

D
Daniel P. Berrange 已提交
1705
    if (!vm) {
1706
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN, _("no domain with matching id %d"), dom->id);
1707
        goto cleanup;
D
Daniel P. Berrange 已提交
1708
    }
1709
    if (!virDomainIsActive(vm)) {
1710 1711
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("domain is not running"));
1712
        goto cleanup;
D
Daniel P. Berrange 已提交
1713
    }
1714
    if (vm->state != VIR_DOMAIN_PAUSED) {
1715
        if (qemudMonitorCommand(vm, "stop", &info) < 0) {
1716 1717 1718 1719 1720 1721
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                             "%s", _("suspend operation failed"));
            goto cleanup;
        }
        vm->state = VIR_DOMAIN_PAUSED;
        qemudDebug("Reply %s", info);
1722 1723 1724
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_SUSPENDED,
                                         VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1725
        VIR_FREE(info);
D
Daniel P. Berrange 已提交
1726
    }
1727 1728 1729
    ret = 0;

cleanup:
1730 1731
    if (vm)
        virDomainObjUnlock(vm);
1732 1733 1734 1735 1736 1737

    if (event) {
        qemuDriverLock(driver);
        qemuDomainEventQueue(driver, event);
        qemuDriverUnlock(driver);
    }
1738
    return ret;
D
Daniel P. Berrange 已提交
1739 1740 1741
}


1742
static int qemudDomainResume(virDomainPtr dom) {
1743
    struct qemud_driver *driver = dom->conn->privateData;
D
Daniel P. Berrange 已提交
1744
    char *info;
1745 1746
    virDomainObjPtr vm;
    int ret = -1;
1747
    virDomainEventPtr event = NULL;
1748

1749
    qemuDriverLock(driver);
1750
    vm = virDomainFindByID(&driver->domains, dom->id);
1751 1752
    qemuDriverUnlock(driver);

D
Daniel P. Berrange 已提交
1753
    if (!vm) {
1754 1755
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         _("no domain with matching id %d"), dom->id);
1756
        goto cleanup;
D
Daniel P. Berrange 已提交
1757
    }
1758
    if (!virDomainIsActive(vm)) {
1759 1760
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("domain is not running"));
1761
        goto cleanup;
D
Daniel P. Berrange 已提交
1762
    }
1763
    if (vm->state == VIR_DOMAIN_PAUSED) {
1764
        if (qemudMonitorCommand(vm, "cont", &info) < 0) {
1765 1766 1767 1768 1769 1770
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                             "%s", _("resume operation failed"));
            goto cleanup;
        }
        vm->state = VIR_DOMAIN_RUNNING;
        qemudDebug("Reply %s", info);
1771 1772 1773
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_RESUMED,
                                         VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1774
        VIR_FREE(info);
D
Daniel P. Berrange 已提交
1775
    }
1776 1777 1778
    ret = 0;

cleanup:
1779 1780
    if (vm)
        virDomainObjUnlock(vm);
1781 1782 1783 1784 1785
    if (event) {
        qemuDriverLock(driver);
        qemuDomainEventQueue(driver, event);
        qemuDriverUnlock(driver);
    }
1786
    return ret;
D
Daniel P. Berrange 已提交
1787 1788 1789
}


1790
static int qemudDomainShutdown(virDomainPtr dom) {
1791 1792
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
1793
    char* info;
1794
    int ret = -1;
1795

1796
    qemuDriverLock(driver);
1797
    vm = virDomainFindByID(&driver->domains, dom->id);
1798 1799
    qemuDriverUnlock(driver);

1800 1801
    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1802
                         _("no domain with matching id %d"), dom->id);
1803
        goto cleanup;
1804 1805
    }

1806
    if (qemudMonitorCommand(vm, "system_powerdown", &info) < 0) {
1807
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
1808
                         "%s", _("shutdown operation failed"));
1809
        goto cleanup;
1810
    }
D
Daniel Veillard 已提交
1811
    VIR_FREE(info);
1812 1813 1814
    ret = 0;

cleanup:
1815 1816
    if (vm)
        virDomainObjUnlock(vm);
1817
    return ret;
1818 1819 1820
}


1821
static int qemudDomainDestroy(virDomainPtr dom) {
1822 1823 1824
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1825
    virDomainEventPtr event = NULL;
1826

1827
    qemuDriverLock(driver);
1828
    vm  = virDomainFindByID(&driver->domains, dom->id);
D
Daniel P. Berrange 已提交
1829
    if (!vm) {
1830
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1831
                         _("no domain with matching id %d"), dom->id);
1832
        goto cleanup;
D
Daniel P. Berrange 已提交
1833
    }
1834

1835
    qemudShutdownVMDaemon(dom->conn, driver, vm);
1836 1837 1838
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1839
    if (!vm->persistent) {
1840 1841
        virDomainRemoveInactive(&driver->domains,
                                vm);
1842 1843
        vm = NULL;
    }
1844 1845 1846
    ret = 0;

cleanup:
1847 1848
    if (vm)
        virDomainObjUnlock(vm);
1849 1850
    if (event)
        qemuDomainEventQueue(driver, event);
1851
    qemuDriverUnlock(driver);
1852
    return ret;
D
Daniel P. Berrange 已提交
1853 1854 1855
}


1856
static char *qemudDomainGetOSType(virDomainPtr dom) {
1857 1858 1859
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *type = NULL;
1860

1861
    qemuDriverLock(driver);
1862
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1863
    qemuDriverUnlock(driver);
1864 1865
    if (!vm) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1866
                         "%s", _("no domain with matching uuid"));
1867
        goto cleanup;
1868 1869
    }

1870
    if (!(type = strdup(vm->def->os.type)))
1871 1872
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_MEMORY,
                         "%s", _("failed to allocate space for ostype"));
1873 1874

cleanup:
1875 1876
    if (vm)
        virDomainObjUnlock(vm);
1877 1878 1879
    return type;
}

1880 1881
/* Returns max memory in kb, 0 if error */
static unsigned long qemudDomainGetMaxMemory(virDomainPtr dom) {
1882 1883 1884
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    unsigned long ret = 0;
1885

1886
    qemuDriverLock(driver);
1887
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1888 1889
    qemuDriverUnlock(driver);

1890
    if (!vm) {
1891 1892 1893
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1894
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1895
                         _("no domain with matching uuid '%s'"), uuidstr);
1896
        goto cleanup;
1897 1898
    }

1899 1900 1901
    ret = vm->def->maxmem;

cleanup:
1902 1903
    if (vm)
        virDomainObjUnlock(vm);
1904
    return ret;
1905 1906 1907
}

static int qemudDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) {
1908 1909 1910
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1911

1912
    qemuDriverLock(driver);
1913
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1914 1915
    qemuDriverUnlock(driver);

1916
    if (!vm) {
1917 1918 1919
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1920
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1921
                         _("no domain with matching uuid '%s'"), uuidstr);
1922
        goto cleanup;
1923 1924 1925 1926
    }

    if (newmax < vm->def->memory) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
1927
                         "%s", _("cannot set max memory lower than current memory"));
1928
        goto cleanup;;
1929 1930 1931
    }

    vm->def->maxmem = newmax;
1932 1933 1934
    ret = 0;

cleanup:
1935 1936
    if (vm)
        virDomainObjUnlock(vm);
1937
    return ret;
1938 1939 1940
}

static int qemudDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
1941 1942 1943
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1944

1945
    qemuDriverLock(driver);
1946
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1947
    qemuDriverUnlock(driver);
1948
    if (!vm) {
1949 1950 1951
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1952
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
1953
                         _("no domain with matching uuid '%s'"), uuidstr);
1954
        goto cleanup;
1955 1956
    }

1957
    if (virDomainIsActive(vm)) {
1958
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
1959
                         "%s", _("cannot set memory of an active domain"));
1960
        goto cleanup;
1961 1962 1963 1964
    }

    if (newmem > vm->def->maxmem) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
1965
                         "%s", _("cannot set memory higher than max memory"));
1966
        goto cleanup;
1967 1968 1969
    }

    vm->def->memory = newmem;
1970 1971 1972
    ret = 0;

cleanup:
1973 1974
    if (vm)
        virDomainObjUnlock(vm);
1975
    return ret;
1976 1977
}

1978
static int qemudDomainGetInfo(virDomainPtr dom,
1979
                              virDomainInfoPtr info) {
1980 1981 1982 1983
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

1984
    qemuDriverLock(driver);
1985
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1986
    qemuDriverUnlock(driver);
D
Daniel P. Berrange 已提交
1987
    if (!vm) {
1988 1989
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
1990
        goto cleanup;
D
Daniel P. Berrange 已提交
1991 1992
    }

1993
    info->state = vm->state;
D
Daniel P. Berrange 已提交
1994

1995
    if (!virDomainIsActive(vm)) {
1996
        info->cpuTime = 0;
D
Daniel P. Berrange 已提交
1997
    } else {
1998
        if (qemudGetProcessInfo(&(info->cpuTime), vm->pid) < 0) {
1999
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, ("cannot read cputime for domain"));
2000
            goto cleanup;
D
Daniel P. Berrange 已提交
2001 2002 2003
        }
    }

2004 2005 2006
    info->maxMem = vm->def->maxmem;
    info->memory = vm->def->memory;
    info->nrVirtCpu = vm->def->vcpus;
2007 2008 2009
    ret = 0;

cleanup:
2010 2011
    if (vm)
        virDomainObjUnlock(vm);
2012
    return ret;
D
Daniel P. Berrange 已提交
2013 2014 2015
}


D
Daniel P. Berrange 已提交
2016
static char *qemudEscape(const char *in, int shell)
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
{
    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 已提交
2038 2039 2040 2041
            if (shell)
                len += 5;
            else
                len += 1;
2042 2043 2044 2045 2046 2047 2048
            break;
        default:
            len += 1;
            break;
        }
    }

2049
    if (VIR_ALLOC_N(out, len + 1) < 0)
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
        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 已提交
2068 2069 2070 2071 2072 2073 2074 2075 2076
            if (shell) {
                out[j++] = '\'';
                out[j++] = '\\';
                out[j++] = '\\';
                out[j++] = '\'';
                out[j++] = '\'';
            } else {
                out[j++] = in[i];
            }
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
            break;
        default:
            out[j++] = in[i];
            break;
        }
    }
    out[j] = '\0';

    return out;
}

2088 2089 2090 2091 2092
static char *qemudEscapeMonitorArg(const char *in)
{
    return qemudEscape(in, 0);
}

D
Daniel P. Berrange 已提交
2093 2094 2095 2096
static char *qemudEscapeShellArg(const char *in)
{
    return qemudEscape(in, 1);
}
2097

2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
#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];
};

2109
static int qemudDomainSave(virDomainPtr dom,
2110
                           const char *path) {
2111 2112 2113 2114 2115 2116 2117
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *command = NULL;
    char *info = NULL;
    int fd = -1;
    char *safe_path = NULL;
    char *xml = NULL;
2118
    struct qemud_save_header header;
2119
    int ret = -1;
2120
    virDomainEventPtr event = NULL;
2121 2122 2123 2124 2125

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

2126
    qemuDriverLock(driver);
2127 2128
    vm = virDomainFindByID(&driver->domains, dom->id);

D
Daniel P. Berrange 已提交
2129
    if (!vm) {
2130
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
2131
                         _("no domain with matching id %d"), dom->id);
2132
        goto cleanup;
D
Daniel P. Berrange 已提交
2133
    }
2134

2135
    if (!virDomainIsActive(vm)) {
2136
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2137
                         "%s", _("domain is not running"));
2138
        goto cleanup;
D
Daniel P. Berrange 已提交
2139
    }
2140 2141 2142 2143 2144 2145

    /* Pause */
    if (vm->state == VIR_DOMAIN_RUNNING) {
        header.was_running = 1;
        if (qemudDomainSuspend(dom) != 0) {
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2146
                             "%s", _("failed to pause domain"));
2147
            goto cleanup;
2148 2149 2150 2151
        }
    }

    /* Get XML for the domain */
2152
    xml = virDomainDefFormat(dom->conn, vm->def, VIR_DOMAIN_XML_SECURE);
2153 2154
    if (!xml) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2155
                         "%s", _("failed to get domain xml"));
2156
        goto cleanup;
2157 2158 2159 2160 2161 2162
    }
    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,
2163
                         _("failed to create '%s'"), path);
2164
        goto cleanup;
2165 2166 2167 2168
    }

    if (safewrite(fd, &header, sizeof(header)) != sizeof(header)) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2169
                         "%s", _("failed to write save header"));
2170
        goto cleanup;
2171 2172 2173 2174
    }

    if (safewrite(fd, xml, header.xml_len) != header.xml_len) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2175
                         "%s", _("failed to write xml"));
2176
        goto cleanup;
2177 2178
    }

2179 2180 2181 2182 2183 2184 2185
    if (close(fd) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         _("unable to save file %s %s"),
                         path, strerror(errno));
        goto cleanup;
    }
    fd = -1;
2186 2187 2188 2189 2190

    /* Migrate to file */
    safe_path = qemudEscapeShellArg(path);
    if (!safe_path) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2191
                         "%s", _("out of memory"));
2192
        goto cleanup;
2193 2194 2195
    }
    if (asprintf (&command, "migrate \"exec:"
                  "dd of='%s' oflag=append conv=notrunc 2>/dev/null"
2196
                  "\"", safe_path) == -1) {
2197
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2198
                         "%s", _("out of memory"));
2199 2200
        command = NULL;
        goto cleanup;
2201 2202
    }

2203
    if (qemudMonitorCommand(vm, command, &info) < 0) {
2204
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2205
                         "%s", _("migrate operation failed"));
2206
        goto cleanup;
2207 2208
    }

2209 2210 2211 2212 2213 2214 2215 2216
    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"));
2217
        goto cleanup;
2218 2219
    }

2220 2221
    /* Shut it down */
    qemudShutdownVMDaemon(dom->conn, driver, vm);
2222 2223 2224
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
2225
    if (!vm->persistent) {
2226 2227
        virDomainRemoveInactive(&driver->domains,
                                vm);
2228 2229
        vm = NULL;
    }
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
    ret = 0;

cleanup:
    if (fd != -1)
        close(fd);
    VIR_FREE(xml);
    VIR_FREE(safe_path);
    VIR_FREE(command);
    VIR_FREE(info);
    if (ret != 0)
        unlink(path);
2241 2242
    if (vm)
        virDomainObjUnlock(vm);
2243 2244
    if (event)
        qemuDomainEventQueue(driver, event);
2245
    qemuDriverUnlock(driver);
2246
    return ret;
D
Daniel P. Berrange 已提交
2247 2248 2249
}


2250
static int qemudDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) {
2251 2252
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
2253
    int max;
2254
    int ret = -1;
2255

2256
    qemuDriverLock(driver);
2257
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2258 2259
    qemuDriverUnlock(driver);

2260
    if (!vm) {
2261 2262 2263
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
2264
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
2265
                         _("no domain with matching uuid '%s'"), uuidstr);
2266
        goto cleanup;
2267 2268
    }

2269
    if (virDomainIsActive(vm)) {
2270
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT, "%s",
2271
                         _("cannot change vcpu count of an active domain"));
2272
        goto cleanup;
2273 2274 2275 2276 2277
    }

    if ((max = qemudDomainGetMaxVcpus(dom)) < 0) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                         _("could not determine max vcpus for the domain"));
2278
        goto cleanup;
2279 2280 2281 2282 2283 2284
    }

    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);
2285
        goto cleanup;
2286 2287 2288
    }

    vm->def->vcpus = nvcpus;
2289 2290 2291
    ret = 0;

cleanup:
2292 2293
    if (vm)
        virDomainObjUnlock(vm);
2294
    return ret;
2295 2296
}

2297 2298 2299 2300 2301 2302 2303

#if HAVE_SCHED_GETAFFINITY
static int
qemudDomainPinVcpu(virDomainPtr dom,
                   unsigned int vcpu,
                   unsigned char *cpumap,
                   int maplen) {
2304 2305
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
2306 2307 2308
    cpu_set_t mask;
    int i, maxcpu;
    virNodeInfo nodeinfo;
2309
    int ret = -1;
2310

2311
    qemuDriverLock(driver);
2312
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2313 2314
    qemuDriverUnlock(driver);

2315
    if (!virDomainIsActive(vm)) {
2316 2317
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                         "%s",_("cannot pin vcpus on an inactive domain"));
2318
        goto cleanup;
2319 2320 2321 2322 2323 2324
    }

    if (vcpu > (vm->nvcpupids-1)) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                         _("vcpu number out of range %d > %d"),
                         vcpu, vm->nvcpupids);
2325
        goto cleanup;
2326 2327 2328
    }

    if (virNodeInfoPopulate(dom->conn, &nodeinfo) < 0)
2329
        goto cleanup;
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344

    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));
2345
            goto cleanup;
2346 2347 2348 2349
        }
    } else {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                         "%s", _("cpu affinity is not supported"));
2350
        goto cleanup;
2351
    }
2352
    ret = 0;
2353

2354
cleanup:
2355 2356
    if (vm)
        virDomainObjUnlock(vm);
2357
    return ret;
2358 2359 2360 2361 2362 2363 2364 2365
}

static int
qemudDomainGetVcpus(virDomainPtr dom,
                    virVcpuInfoPtr info,
                    int maxinfo,
                    unsigned char *cpumaps,
                    int maplen) {
2366 2367
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
2368 2369
    virNodeInfo nodeinfo;
    int i, v, maxcpu;
2370
    int ret = -1;
2371

2372
    qemuDriverLock(driver);
2373
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2374 2375
    qemuDriverUnlock(driver);

2376
    if (!virDomainIsActive(vm)) {
2377 2378
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                         "%s",_("cannot pin vcpus on an inactive domain"));
2379
        goto cleanup;
2380 2381 2382
    }

    if (virNodeInfoPopulate(dom->conn, &nodeinfo) < 0)
2383
        goto cleanup;
2384 2385 2386 2387 2388 2389 2390 2391 2392

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

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

2393 2394 2395 2396 2397 2398 2399 2400
    if (maxinfo >= 1) {
        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 */
            }
2401 2402
        }

2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
        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));
                        goto cleanup;
                    }

                    for (i = 0 ; i < maxcpu ; i++)
                        if (CPU_ISSET(i, &mask))
                            VIR_USE_CPU(cpumap, i);
2420
                }
2421 2422 2423 2424
            } else {
                qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                                 "%s", _("cpu affinity is not available"));
                goto cleanup;
2425 2426 2427
            }
        }
    }
2428
    ret = maxinfo;
2429

2430
cleanup:
2431 2432
    if (vm)
        virDomainObjUnlock(vm);
2433
    return ret;
2434 2435 2436 2437
}
#endif /* HAVE_SCHED_GETAFFINITY */


2438
static int qemudDomainGetMaxVcpus(virDomainPtr dom) {
2439 2440
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
2441
    const char *type;
2442
    int ret = -1;
2443

2444
    qemuDriverLock(driver);
2445
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2446 2447
    qemuDriverUnlock(driver);

2448
    if (!vm) {
2449 2450 2451
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
2452
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
2453
                         _("no domain with matching uuid '%s'"), uuidstr);
2454
        goto cleanup;
2455 2456
    }

2457
    if (!(type = virDomainVirtTypeToString(vm->def->virtType))) {
2458 2459 2460
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         _("unknown virt type in domain definition '%d'"),
                         vm->def->virtType);
2461
        goto cleanup;
2462 2463
    }

2464
    ret = qemudGetMaxVCPUs(dom->conn, type);
2465

2466
cleanup:
2467 2468
    if (vm)
        virDomainObjUnlock(vm);
2469 2470 2471 2472
    return ret;
}


2473
static int qemudDomainRestore(virConnectPtr conn,
2474 2475 2476
                              const char *path) {
    struct qemud_driver *driver = conn->privateData;
    virDomainDefPtr def = NULL;
2477
    virDomainObjPtr vm = NULL;
2478 2479 2480
    int fd = -1;
    int ret = -1;
    char *xml = NULL;
2481
    struct qemud_save_header header;
2482
    virDomainEventPtr event = NULL;
2483

2484
    qemuDriverLock(driver);
2485 2486 2487
    /* Verify the header and read the XML */
    if ((fd = open(path, O_RDONLY)) < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2488
                         "%s", _("cannot read domain image"));
2489
        goto cleanup;
2490 2491 2492 2493
    }

    if (saferead(fd, &header, sizeof(header)) != sizeof(header)) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2494
                         "%s", _("failed to read qemu header"));
2495
        goto cleanup;
2496 2497 2498 2499
    }

    if (memcmp(header.magic, QEMUD_SAVE_MAGIC, sizeof(header.magic)) != 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2500
                         "%s", _("image magic is incorrect"));
2501
        goto cleanup;
2502 2503 2504 2505
    }

    if (header.version > QEMUD_SAVE_VERSION) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2506
                         _("image version is not supported (%d > %d)"),
2507
                         header.version, QEMUD_SAVE_VERSION);
2508
        goto cleanup;
2509 2510
    }

2511
    if (VIR_ALLOC_N(xml, header.xml_len) < 0) {
2512
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2513
                         "%s", _("out of memory"));
2514
        goto cleanup;
2515 2516 2517 2518
    }

    if (saferead(fd, xml, header.xml_len) != header.xml_len) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2519
                         "%s", _("failed to read XML"));
2520
        goto cleanup;
2521 2522 2523
    }

    /* Create a domain from this XML */
2524
    if (!(def = virDomainDefParseString(conn, driver->caps, xml))) {
2525
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2526
                         "%s", _("failed to parse XML"));
2527
        goto cleanup;
2528 2529 2530
    }

    /* Ensure the name and UUID don't already exist in an active VM */
2531
    vm = virDomainFindByUUID(&driver->domains, def->uuid);
2532
    if (!vm)
2533
        vm = virDomainFindByName(&driver->domains, def->name);
2534
    if (vm && virDomainIsActive(vm)) {
2535
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2536
                         _("domain is already active as '%s'"), vm->def->name);
2537
        goto cleanup;
2538 2539
    }

2540 2541 2542
    if (!(vm = virDomainAssignDef(conn,
                                  &driver->domains,
                                  def))) {
2543
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2544
                         "%s", _("failed to assign new VM"));
2545
        goto cleanup;
2546
    }
2547
    def = NULL;
2548 2549

    /* Set the migration source and start it up. */
2550
    vm->stdin_fd = fd;
2551
    ret = qemudStartVMDaemon(conn, driver, vm, "stdio");
2552
    close(fd);
2553
    fd = -1;
2554
    vm->stdin_fd = -1;
2555
    if (ret < 0) {
2556
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2557
                         "%s", _("failed to start VM"));
2558
        if (!vm->persistent) {
2559 2560
            virDomainRemoveInactive(&driver->domains,
                                    vm);
2561 2562
            vm = NULL;
        }
2563
        goto cleanup;
2564 2565
    }

2566 2567 2568
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
2569

2570 2571 2572
    /* If it was running before, resume it now. */
    if (header.was_running) {
        char *info;
2573
        if (qemudMonitorCommand(vm, "cont", &info) < 0) {
2574
            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2575
                             "%s", _("failed to resume domain"));
2576
            goto cleanup;
2577
        }
2578
        VIR_FREE(info);
2579 2580
        vm->state = VIR_DOMAIN_RUNNING;
    }
2581
    ret = 0;
2582

2583 2584 2585 2586 2587
cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
    if (fd != -1)
        close(fd);
2588 2589
    if (vm)
        virDomainObjUnlock(vm);
2590 2591
    if (event)
        qemuDomainEventQueue(driver, event);
2592
    qemuDriverUnlock(driver);
2593
    return ret;
D
Daniel P. Berrange 已提交
2594 2595 2596
}


2597
static char *qemudDomainDumpXML(virDomainPtr dom,
2598
                                int flags ATTRIBUTE_UNUSED) {
2599 2600 2601 2602
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *ret = NULL;

2603
    qemuDriverLock(driver);
2604
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2605 2606
    qemuDriverUnlock(driver);

D
Daniel P. Berrange 已提交
2607
    if (!vm) {
2608 2609
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
2610
        goto cleanup;
D
Daniel P. Berrange 已提交
2611 2612
    }

2613 2614 2615 2616 2617 2618
    ret = virDomainDefFormat(dom->conn,
                             (flags & VIR_DOMAIN_XML_INACTIVE) && vm->newDef ?
                             vm->newDef : vm->def,
                             flags);

cleanup:
2619 2620
    if (vm)
        virDomainObjUnlock(vm);
2621
    return ret;
D
Daniel P. Berrange 已提交
2622 2623 2624
}


2625
static int qemudListDefinedDomains(virConnectPtr conn,
2626
                            char **const names, int nnames) {
2627
    struct qemud_driver *driver = conn->privateData;
2628
    int got = 0, i;
2629

2630
    qemuDriverLock(driver);
2631
    for (i = 0 ; i < driver->domains.count && got < nnames ; i++) {
2632
        virDomainObjLock(driver->domains.objs[i]);
2633 2634
        if (!virDomainIsActive(driver->domains.objs[i])) {
            if (!(names[got++] = strdup(driver->domains.objs[i]->def->name))) {
2635
                qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY,
2636
                                 "%s", _("failed to allocate space for VM name string"));
2637
                virDomainObjUnlock(driver->domains.objs[i]);
2638 2639
                goto cleanup;
            }
2640
        }
2641
        virDomainObjUnlock(driver->domains.objs[i]);
D
Daniel P. Berrange 已提交
2642
    }
2643

2644
    qemuDriverUnlock(driver);
D
Daniel P. Berrange 已提交
2645
    return got;
2646 2647 2648

 cleanup:
    for (i = 0 ; i < got ; i++)
2649
        VIR_FREE(names[i]);
2650
    qemuDriverUnlock(driver);
2651
    return -1;
D
Daniel P. Berrange 已提交
2652 2653
}

2654
static int qemudNumDefinedDomains(virConnectPtr conn) {
2655
    struct qemud_driver *driver = conn->privateData;
2656 2657
    int n = 0, i;

2658
    qemuDriverLock(driver);
2659 2660
    for (i = 0 ; i < driver->domains.count ; i++)
        if (!virDomainIsActive(driver->domains.objs[i]))
2661
            n++;
2662
    qemuDriverUnlock(driver);
2663

2664
    return n;
D
Daniel P. Berrange 已提交
2665 2666 2667
}


2668
static int qemudDomainStart(virDomainPtr dom) {
2669 2670 2671
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
2672
    virDomainEventPtr event = NULL;
2673

2674
    qemuDriverLock(driver);
2675
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2676 2677
    qemuDriverUnlock(driver);

2678
    if (!vm) {
2679
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
2680
                         "%s", _("no domain with matching uuid"));
2681
        goto cleanup;
2682 2683
    }

2684
    ret = qemudStartVMDaemon(dom->conn, driver, vm, NULL);
2685
    if (ret != -1)
2686 2687 2688
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_STARTED,
                                         VIR_DOMAIN_EVENT_STARTED_BOOTED);
2689 2690

cleanup:
2691 2692
    if (vm)
        virDomainObjUnlock(vm);
2693 2694 2695 2696 2697
    if (event) {
        qemuDriverLock(driver);
        qemuDomainEventQueue(driver, event);
        qemuDriverUnlock(driver);
    }
2698
    return ret;
D
Daniel P. Berrange 已提交
2699 2700 2701
}


2702
static virDomainPtr qemudDomainDefine(virConnectPtr conn, const char *xml) {
2703
    struct qemud_driver *driver = conn->privateData;
2704
    virDomainDefPtr def;
2705
    virDomainObjPtr vm = NULL;
2706
    virDomainPtr dom = NULL;
2707
    virDomainEventPtr event = NULL;
2708
    int newVM = 1;
2709

2710
    qemuDriverLock(driver);
2711
    if (!(def = virDomainDefParseString(conn, driver->caps, xml)))
2712
        goto cleanup;
2713

2714
    vm = virDomainFindByName(&driver->domains, def->name);
2715 2716
    if (vm) {
        virDomainObjUnlock(vm);
2717
        newVM = 0;
2718
    }
2719

2720 2721 2722 2723
    if (!(vm = virDomainAssignDef(conn,
                                  &driver->domains,
                                  def))) {
        virDomainDefFree(def);
2724
        goto cleanup;
2725
    }
2726
    vm->persistent = 1;
2727

2728 2729
    if (virDomainSaveConfig(conn,
                            driver->configDir,
2730
                            vm->newDef ? vm->newDef : vm->def) < 0) {
2731 2732
        virDomainRemoveInactive(&driver->domains,
                                vm);
2733
        vm = NULL;
2734
        goto cleanup;
2735 2736
    }

2737 2738 2739 2740 2741
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_DEFINED,
                                     newVM ?
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2742

2743
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
2744
    if (dom) dom->id = vm->def->id;
2745 2746

cleanup:
2747 2748
    if (vm)
        virDomainObjUnlock(vm);
2749 2750
    if (event)
        qemuDomainEventQueue(driver, event);
2751
    qemuDriverUnlock(driver);
2752
    return dom;
D
Daniel P. Berrange 已提交
2753 2754
}

2755
static int qemudDomainUndefine(virDomainPtr dom) {
2756 2757
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
2758
    virDomainEventPtr event = NULL;
2759
    int ret = -1;
D
Daniel P. Berrange 已提交
2760

2761
    qemuDriverLock(driver);
2762
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2763

D
Daniel P. Berrange 已提交
2764
    if (!vm) {
2765 2766
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
2767
        goto cleanup;
D
Daniel P. Berrange 已提交
2768 2769
    }

2770
    if (virDomainIsActive(vm)) {
2771 2772
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot delete active domain"));
2773
        goto cleanup;
D
Daniel P. Berrange 已提交
2774 2775
    }

2776 2777 2778
    if (!vm->persistent) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot undefine transient domain"));
2779
        goto cleanup;
2780 2781 2782
    }

    if (virDomainDeleteConfig(dom->conn, driver->configDir, driver->autostartDir, vm) < 0)
2783
        goto cleanup;
D
Daniel P. Berrange 已提交
2784

2785 2786 2787
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2788

2789 2790
    virDomainRemoveInactive(&driver->domains,
                            vm);
2791
    vm = NULL;
2792
    ret = 0;
D
Daniel P. Berrange 已提交
2793

2794
cleanup:
2795 2796
    if (vm)
        virDomainObjUnlock(vm);
2797 2798
    if (event)
        qemuDomainEventQueue(driver, event);
2799
    qemuDriverUnlock(driver);
2800
    return ret;
D
Daniel P. Berrange 已提交
2801 2802
}

2803
/* Return the disks name for use in monitor commands */
2804
static char *qemudDiskDeviceName(const virConnectPtr conn,
2805
                                 const virDomainDiskDefPtr disk) {
2806 2807 2808 2809 2810 2811

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

    if (virDiskNameToBusDeviceIndex(disk, &busid, &devid) < 0) {
2812
        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
2813 2814 2815 2816 2817 2818 2819
                         _("cannot convert disk '%s' to bus/device index"),
                         disk->dst);
        return NULL;
    }

    switch (disk->bus) {
        case VIR_DOMAIN_DISK_BUS_IDE:
2820 2821 2822 2823
            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);
2824 2825
            break;
        case VIR_DOMAIN_DISK_BUS_SCSI:
2826 2827 2828 2829
            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);
2830 2831 2832 2833 2834 2835 2836 2837
            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:
2838
            qemudReportError(conn, NULL, NULL, VIR_ERR_NO_SUPPORT,
2839 2840 2841 2842 2843 2844
                             _("Unsupported disk name mapping for bus '%s'"),
                             virDomainDiskBusTypeToString(disk->bus));
            return NULL;
    }

    if (ret == -1) {
2845
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
2846 2847 2848 2849 2850 2851
        return NULL;
    }

    return devname;
}

2852 2853
static int qemudDomainChangeEjectableMedia(virConnectPtr conn,
                                           virDomainObjPtr vm,
2854 2855
                                           virDomainDeviceDefPtr dev)
{
2856
    virDomainDiskDefPtr origdisk = NULL, newdisk;
2857
    char *cmd, *reply, *safe_path;
2858
    char *devname = NULL;
2859
    unsigned int qemuCmdFlags;
2860
    int i;
2861

2862
    origdisk = NULL;
2863
    newdisk = dev->data.disk;
2864 2865 2866 2867
    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];
2868
            break;
2869
        }
2870 2871 2872
    }

    if (!origdisk) {
2873
        qemudReportError(conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
2874 2875 2876 2877 2878 2879 2880 2881 2882
                         _("No device with bus '%s' and target '%s'"),
                         virDomainDiskBusTypeToString(newdisk->bus),
                         newdisk->dst);
        return -1;
    }

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

    if (qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE) {
2890
        if (!(devname = qemudDiskDeviceName(conn, newdisk)))
2891 2892 2893 2894 2895 2896 2897 2898 2899
            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 {
2900
            qemudReportError(conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
2901 2902 2903 2904 2905 2906 2907 2908
                             _("Emulator version does not support removable "
                               "media for device '%s' and target '%s'"),
                               virDomainDiskDeviceTypeToString(newdisk->device),
                               newdisk->dst);
            return -1;
        }

        if (!devname) {
2909
            qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
2910 2911 2912
            return -1;
        }
    }
2913

2914
    if (newdisk->src) {
2915 2916
        safe_path = qemudEscapeMonitorArg(newdisk->src);
        if (!safe_path) {
2917
            qemudReportError(conn, dom, NULL, VIR_ERR_NO_MEMORY, NULL);
2918
            VIR_FREE(devname);
2919 2920
            return -1;
        }
2921
        if (asprintf (&cmd, "change %s \"%s\"", devname, safe_path) == -1) {
2922
            qemudReportError(conn, dom, NULL, VIR_ERR_NO_MEMORY, NULL);
2923
            VIR_FREE(safe_path);
2924
            VIR_FREE(devname);
2925 2926
            return -1;
        }
2927
        VIR_FREE(safe_path);
2928

2929
    } else if (asprintf(&cmd, "eject %s", devname) == -1) {
2930
        qemudReportError(conn, dom, NULL, VIR_ERR_NO_MEMORY, NULL);
2931
        VIR_FREE(devname);
2932 2933
        return -1;
    }
2934
    VIR_FREE(devname);
2935

2936
    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
2937
        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
C
Cole Robinson 已提交
2938
                         "%s", _("could not change cdrom media"));
2939
        VIR_FREE(cmd);
2940 2941
        return -1;
    }
2942 2943 2944 2945

    /* If the command failed qemu prints:
     * device not found, device is locked ...
     * No message is printed on success it seems */
2946
    DEBUG ("ejectable media change reply: %s", reply);
2947
    if (strstr(reply, "\ndevice ")) {
2948
        qemudReportError (conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
C
Cole Robinson 已提交
2949
                          _("changing cdrom media failed: %s"), reply);
2950 2951 2952 2953
        VIR_FREE(reply);
        VIR_FREE(cmd);
        return -1;
    }
2954 2955
    VIR_FREE(reply);
    VIR_FREE(cmd);
2956

2957 2958
    VIR_FREE(origdisk->src);
    origdisk->src = newdisk->src;
2959
    newdisk->src = NULL;
2960
    origdisk->type = newdisk->type;
2961 2962 2963
    return 0;
}

2964 2965 2966
static int qemudDomainAttachPciDiskDevice(virConnectPtr conn,
                                          virDomainObjPtr vm,
                                          virDomainDeviceDefPtr dev)
2967 2968
{
    int ret, i;
2969
    char *cmd, *reply, *s;
2970 2971 2972 2973 2974
    char *safe_path;
    const char* type = virDomainDiskBusTypeToString(dev->data.disk->bus);

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

    if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
2982
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
2983 2984 2985 2986 2987
        return -1;
    }

    safe_path = qemudEscapeMonitorArg(dev->data.disk->src);
    if (!safe_path) {
2988
        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2989 2990 2991 2992 2993 2994 2995 2996
                         "%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) {
2997
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
2998 2999 3000
        return ret;
    }

3001
    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
3002
        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3003 3004 3005 3006 3007 3008 3009 3010
                         _("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... */
3011 3012 3013 3014 3015 3016
#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 已提交
3017
            qemudLog(QEMUD_WARN, "%s", _("Unable to parse slot number\n"));
3018
    } else {
3019
        qemudReportError (conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
                          _("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;
}
3034

3035 3036 3037
static int qemudDomainAttachUsbMassstorageDevice(virConnectPtr conn,
                                                 virDomainObjPtr vm,
                                                 virDomainDeviceDefPtr dev)
3038
{
3039 3040
    int ret, i;
    char *safe_path;
3041 3042
    char *cmd, *reply;

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

3051
    if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
3052
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
3053
        return -1;
3054 3055
    }

3056 3057
    safe_path = qemudEscapeMonitorArg(dev->data.disk->src);
    if (!safe_path) {
3058
        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3059 3060 3061 3062 3063 3064
                         "%s", _("out of memory"));
        return -1;
    }

    ret = asprintf(&cmd, "usb_add disk:%s", safe_path);
    VIR_FREE(safe_path);
3065
    if (ret == -1) {
3066
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
3067 3068 3069
        return ret;
    }

3070
    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
3071
        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3072
                         "%s", _("cannot attach usb disk"));
3073 3074 3075 3076 3077 3078 3079 3080
        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 ")) {
3081
        qemudReportError (conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3082
                          "%s",
3083
                          _("adding usb disk failed"));
3084 3085 3086 3087
        VIR_FREE(reply);
        VIR_FREE(cmd);
        return -1;
    }
3088

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

3093 3094 3095 3096 3097
    VIR_FREE(reply);
    VIR_FREE(cmd);
    return 0;
}

3098 3099 3100
static int qemudDomainAttachHostDevice(virConnectPtr conn,
                                       virDomainObjPtr vm,
                                       virDomainDeviceDefPtr dev)
3101 3102 3103 3104
{
    int ret;
    char *cmd, *reply;

3105
    if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0) {
3106
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
3107 3108
        return -1;
    }
3109

3110
    if (dev->data.hostdev->source.subsys.u.usb.vendor) {
3111
        ret = asprintf(&cmd, "usb_add host:%.4x:%.4x",
3112 3113
                       dev->data.hostdev->source.subsys.u.usb.vendor,
                       dev->data.hostdev->source.subsys.u.usb.product);
3114 3115
    } else {
        ret = asprintf(&cmd, "usb_add host:%.3d.%.3d",
3116 3117
                       dev->data.hostdev->source.subsys.u.usb.bus,
                       dev->data.hostdev->source.subsys.u.usb.device);
3118 3119
    }
    if (ret == -1) {
3120
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
3121 3122 3123
        return -1;
    }

3124
    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
3125
        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3126 3127 3128 3129 3130 3131 3132 3133 3134
                         "%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 ")) {
3135
        qemudReportError (conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3136 3137 3138 3139 3140 3141
                          "%s",
                          _("adding usb device failed"));
        VIR_FREE(reply);
        VIR_FREE(cmd);
        return -1;
    }
3142

3143
    vm->def->hostdevs[vm->def->nhostdevs++] = dev->data.hostdev;
3144

3145 3146
    VIR_FREE(reply);
    VIR_FREE(cmd);
3147 3148 3149
    return 0;
}

3150 3151
static int qemudDomainAttachDevice(virDomainPtr dom,
                                   const char *xml) {
3152 3153 3154 3155
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    virDomainDeviceDefPtr dev = NULL;
    int ret = -1;
3156

3157
    qemuDriverLock(driver);
3158
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3159
    if (!vm) {
3160
        qemuDriverUnlock(driver);
3161 3162
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
3163
        goto cleanup;
3164 3165 3166
    }

    if (!virDomainIsActive(vm)) {
3167
        qemuDriverUnlock(driver);
3168 3169
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot attach device on inactive domain"));
3170
        goto cleanup;
3171 3172
    }

3173 3174 3175
    dev = virDomainDeviceDefParse(dom->conn,
                                  driver->caps,
                                  vm->def, xml);
3176
    qemuDriverUnlock(driver);
3177 3178 3179
    if (dev == NULL)
        goto cleanup;

3180

3181 3182
    if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
        switch (dev->data.disk->device) {
3183 3184
        case VIR_DOMAIN_DISK_DEVICE_CDROM:
        case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
3185
            ret = qemudDomainChangeEjectableMedia(dom->conn, vm, dev);
3186 3187 3188
            break;
        case VIR_DOMAIN_DISK_DEVICE_DISK:
            if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_USB) {
3189
                ret = qemudDomainAttachUsbMassstorageDevice(dom->conn, vm, dev);
3190 3191
            } else if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_SCSI ||
                       dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_VIRTIO) {
3192
                ret = qemudDomainAttachPciDiskDevice(dom->conn, vm, dev);
3193 3194 3195 3196 3197 3198
            }
            break;
        default:
            qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                             "%s", _("this disk device type cannot be attached"));
            goto cleanup;
3199
        }
3200
    } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV &&
3201 3202
               dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
               dev->data.hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
3203
        ret = qemudDomainAttachHostDevice(dom->conn, vm, dev);
3204
    } else {
3205
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
3206
                         "%s", _("this device type cannot be attached"));
3207
        goto cleanup;
3208 3209
    }

3210 3211
cleanup:
    virDomainDeviceDefFree(dev);
3212 3213
    if (vm)
        virDomainObjUnlock(vm);
3214 3215 3216
    return ret;
}

3217 3218
static int qemudDomainDetachPciDiskDevice(virConnectPtr conn,
                                          virDomainObjPtr vm, virDomainDeviceDefPtr dev)
3219 3220
{
    int i, ret = -1;
3221 3222
    char *cmd = NULL;
    char *reply = NULL;
3223 3224 3225 3226 3227 3228 3229 3230 3231 3232
    virDomainDiskDefPtr detach = NULL;

    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) {
3233
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3234
                         _("disk %s not found"), dev->data.disk->dst);
3235
        goto cleanup;
3236 3237 3238
    }

    if (detach->slotnum < 1) {
3239
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3240
                         _("disk %s cannot be detached - invalid slot number %d"),
3241
                           detach->dst, detach->slotnum);
3242
        goto cleanup;
3243 3244
    }

3245 3246 3247 3248
    if (asprintf(&cmd, "pci_del 0 %d", detach->slotnum) < 0) {
        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        cmd = NULL;
        goto cleanup;
3249 3250
    }

3251
    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
3252
        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3253
                          _("failed to execute detach disk %s command"), detach->dst);
3254
        goto cleanup;
3255 3256 3257 3258 3259 3260
    }

    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")) {
3261
        qemudReportError (conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3262
                          _("failed to detach disk %s: invalid slot %d"),
3263 3264
                          detach->dst, detach->slotnum);
        goto cleanup;
3265 3266 3267 3268 3269
    }

    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) {
3270 3271
            qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
            goto cleanup;
3272 3273 3274 3275 3276 3277 3278 3279
        }
        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;
3280 3281

cleanup:
3282 3283 3284 3285 3286 3287 3288
    VIR_FREE(reply);
    VIR_FREE(cmd);
    return ret;
}

static int qemudDomainDetachDevice(virDomainPtr dom,
                                   const char *xml) {
3289 3290 3291 3292
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    virDomainDeviceDefPtr dev = NULL;
    int ret = -1;
3293

3294
    qemuDriverLock(driver);
3295
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3296
    if (!vm) {
3297
        qemuDriverUnlock(driver);
3298 3299
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
3300
        goto cleanup;
3301 3302 3303
    }

    if (!virDomainIsActive(vm)) {
3304
        qemuDriverUnlock(driver);
3305 3306
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot attach device on inactive domain"));
3307
        goto cleanup;
3308 3309 3310
    }

    dev = virDomainDeviceDefParse(dom->conn, driver->caps, vm->def, xml);
3311
    qemuDriverUnlock(driver);
3312 3313 3314
    if (dev == NULL)
        goto cleanup;

3315 3316 3317 3318 3319

    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))
3320
        ret = qemudDomainDetachPciDiskDevice(dom->conn, vm, dev);
3321
    else
3322 3323 3324
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                         "%s", _("only SCSI or virtio disk device can be detached dynamically"));

3325 3326
cleanup:
    virDomainDeviceDefFree(dev);
3327 3328
    if (vm)
        virDomainObjUnlock(vm);
3329 3330 3331
    return ret;
}

3332
static int qemudDomainGetAutostart(virDomainPtr dom,
3333
                                   int *autostart) {
3334 3335 3336
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
3337

3338
    qemuDriverLock(driver);
3339
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3340 3341
    qemuDriverUnlock(driver);

3342
    if (!vm) {
3343 3344
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
3345
        goto cleanup;
3346 3347 3348
    }

    *autostart = vm->autostart;
3349
    ret = 0;
3350

3351
cleanup:
3352 3353
    if (vm)
        virDomainObjUnlock(vm);
3354
    return ret;
3355 3356
}

3357
static int qemudDomainSetAutostart(virDomainPtr dom,
3358
                                   int autostart) {
3359 3360
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
3361 3362
    char *configFile = NULL, *autostartLink = NULL;
    int ret = -1;
3363

3364
    qemuDriverLock(driver);
3365
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3366 3367
    qemuDriverUnlock(driver);

3368
    if (!vm) {
3369 3370
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                         "%s", _("no domain with matching uuid"));
3371
        goto cleanup;
3372 3373
    }

3374 3375 3376
    if (!vm->persistent) {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot set autostart for transient domain"));
3377
        goto cleanup;
3378 3379
    }

3380 3381
    autostart = (autostart != 0);

3382 3383 3384 3385 3386
    if (vm->autostart != autostart) {
        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;
3387

3388 3389
        if (autostart) {
            int err;
3390

3391 3392 3393 3394 3395 3396
            if ((err = virFileMakePath(driver->autostartDir))) {
                qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                                 _("cannot create autostart directory %s: %s"),
                                 driver->autostartDir, strerror(err));
                goto cleanup;
            }
3397

3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410
            if (symlink(configFile, autostartLink) < 0) {
                qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                                 _("Failed to create symlink '%s to '%s': %s"),
                                 autostartLink, configFile, strerror(errno));
                goto cleanup;
            }
        } else {
            if (unlink(autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
                qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                                 _("Failed to delete symlink '%s': %s"),
                                 autostartLink, strerror(errno));
                goto cleanup;
            }
3411 3412
        }

3413
        vm->autostart = autostart;
3414
    }
3415
    ret = 0;
3416

3417 3418 3419
cleanup:
    VIR_FREE(configFile);
    VIR_FREE(autostartLink);
3420 3421
    if (vm)
        virDomainObjUnlock(vm);
3422
    return ret;
3423 3424
}

3425 3426 3427 3428 3429 3430 3431 3432 3433
/* 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)
{
3434
    struct qemud_driver *driver = dom->conn->privateData;
3435
    char *dummy, *info = NULL;
3436
    const char *p, *eol;
3437
    const char *qemu_dev_name = NULL;
3438
    size_t len;
3439
    int i, ret = -1;
3440
    virDomainObjPtr vm;
3441
    virDomainDiskDefPtr disk = NULL;
3442

3443
    qemuDriverLock(driver);
3444
    vm = virDomainFindByID(&driver->domains, dom->id);
3445
    qemuDriverUnlock(driver);
3446 3447 3448
    if (!vm) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                          _("no domain with matching id %d"), dom->id);
3449
        goto cleanup;
3450
    }
3451
    if (!virDomainIsActive (vm)) {
3452 3453
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("domain is not running"));
3454
        goto cleanup;
3455 3456
    }

3457 3458 3459 3460 3461 3462 3463 3464
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(path, vm->def->disks[i]->dst)) {
            disk = vm->def->disks[i];
            break;
        }
    }

    if (!disk) {
3465 3466
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                          _("invalid path: %s"), path);
3467
        goto cleanup;
3468 3469
    }

3470
    qemu_dev_name = qemudDiskDeviceName(dom->conn, disk);
3471
    if (!qemu_dev_name)
3472
        goto cleanup;
3473 3474
    len = strlen (qemu_dev_name);

3475
    if (qemudMonitorCommand (vm, "info blockstats", &info) < 0) {
3476 3477
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("'info blockstats' command failed"));
3478
        goto cleanup;
3479 3480 3481 3482 3483 3484 3485 3486
    }
    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.
     */
C
Cole Robinson 已提交
3487
    if (strstr(info, "\ninfo ")) {
3488 3489 3490
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                          "%s",
                          _("'info blockstats' not supported by this qemu"));
3491
        goto cleanup;
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
    }

    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) {
3518
                if (STRPREFIX (p, "rd_bytes=")) {
3519 3520 3521
                    p += 9;
                    if (virStrToLong_ll (p, &dummy, 10, &stats->rd_bytes) == -1)
                        DEBUG ("error reading rd_bytes: %s", p);
3522
                } else if (STRPREFIX (p, "wr_bytes=")) {
3523 3524 3525
                    p += 9;
                    if (virStrToLong_ll (p, &dummy, 10, &stats->wr_bytes) == -1)
                        DEBUG ("error reading wr_bytes: %s", p);
3526
                } else if (STRPREFIX (p, "rd_operations=")) {
3527 3528 3529
                    p += 14;
                    if (virStrToLong_ll (p, &dummy, 10, &stats->rd_req) == -1)
                        DEBUG ("error reading rd_req: %s", p);
3530
                } else if (STRPREFIX (p, "wr_operations=")) {
3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541
                    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++;
            }
3542
            ret = 0;
3543
            goto cleanup;
3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
        }

        /* 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);
3555
 cleanup:
3556 3557
    VIR_FREE(qemu_dev_name);
    VIR_FREE(info);
3558 3559
    if (vm)
        virDomainObjUnlock(vm);
3560
    return ret;
3561 3562
}

3563
#ifdef __linux__
3564 3565 3566 3567 3568
static int
qemudDomainInterfaceStats (virDomainPtr dom,
                           const char *path,
                           struct _virDomainInterfaceStats *stats)
{
3569 3570
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
3571
    int i;
3572
    int ret = -1;
3573

3574
    qemuDriverLock(driver);
3575
    vm = virDomainFindByID(&driver->domains, dom->id);
3576 3577
    qemuDriverUnlock(driver);

3578 3579
    if (!vm) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
3580
                          _("no domain with matching id %d"), dom->id);
3581
        goto cleanup;
3582 3583
    }

3584
    if (!virDomainIsActive(vm)) {
3585
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3586
                         "%s", _("domain is not running"));
3587
        goto cleanup;
3588 3589 3590 3591
    }

    if (!path || path[0] == '\0') {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
3592
                         "%s", _("NULL or empty path"));
3593
        goto cleanup;
3594 3595 3596
    }

    /* Check the path is one of the domain's network interfaces. */
3597 3598
    for (i = 0 ; i < vm->def->nnets ; i++) {
        if (vm->def->nets[i]->ifname &&
3599 3600 3601 3602
            STREQ (vm->def->nets[i]->ifname, path)) {
            ret = 0;
            break;
        }
3603 3604
    }

3605 3606 3607 3608 3609
    if (ret == 0)
        ret = linuxDomainInterfaceStats (dom->conn, path, stats);
    else
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                          _("invalid path, '%s' is not a known interface"), path);
3610

3611
cleanup:
3612 3613
    if (vm)
        virDomainObjUnlock(vm);
3614 3615
    return ret;
}
3616
#else
3617 3618 3619 3620
static int
qemudDomainInterfaceStats (virDomainPtr dom,
                           const char *path ATTRIBUTE_UNUSED,
                           struct _virDomainInterfaceStats *stats ATTRIBUTE_UNUSED)
3621 3622 3623 3624
    qemudReportError (dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                      "%s", __FUNCTION__);
    return -1;
}
3625
#endif
3626

3627 3628 3629 3630 3631 3632 3633
static int
qemudDomainBlockPeek (virDomainPtr dom,
                      const char *path,
                      unsigned long long offset, size_t size,
                      void *buffer,
                      unsigned int flags ATTRIBUTE_UNUSED)
{
3634 3635 3636
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int fd = -1, ret = -1, i;
3637

3638
    qemuDriverLock(driver);
3639
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3640 3641
    qemuDriverUnlock(driver);

3642 3643
    if (!vm) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
J
Jim Meyering 已提交
3644
                          "%s", _("no domain with matching uuid"));
3645
        goto cleanup;
3646 3647 3648 3649
    }

    if (!path || path[0] == '\0') {
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
3650
                         "%s", _("NULL or empty path"));
3651
        goto cleanup;
3652 3653 3654
    }

    /* Check the path belongs to this domain. */
3655 3656
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (vm->def->disks[i]->src != NULL &&
3657 3658 3659 3660
            STREQ (vm->def->disks[i]->src, path)) {
            ret = 0;
            break;
        }
3661 3662
    }

3663 3664 3665 3666 3667 3668 3669 3670 3671
    if (ret == 0) {
        ret = -1;
        /* 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 cleanup;
        }
3672

3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687
        /* 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 cleanup;
        }

        ret = 0;
    } else {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                          "%s", _("invalid path"));
3688 3689
    }

3690 3691 3692
cleanup:
    if (fd >= 0)
        close (fd);
3693 3694
    if (vm)
        virDomainObjUnlock(vm);
3695 3696 3697
    return ret;
}

R
Richard W.M. Jones 已提交
3698 3699 3700 3701 3702 3703
static int
qemudDomainMemoryPeek (virDomainPtr dom,
                       unsigned long long offset, size_t size,
                       void *buffer,
                       unsigned int flags)
{
3704 3705 3706
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char cmd[256], *info = NULL;
R
Richard W.M. Jones 已提交
3707 3708 3709
    char tmp[] = TEMPDIR "/qemu.mem.XXXXXX";
    int fd = -1, ret = -1;

3710
    qemuDriverLock(driver);
3711
    vm = virDomainFindByID(&driver->domains, dom->id);
3712
    qemuDriverUnlock(driver);
R
Richard W.M. Jones 已提交
3713 3714 3715 3716

    if (!vm) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                          _("no domain with matching id %d"), dom->id);
3717 3718 3719 3720 3721 3722 3723
        goto cleanup;
    }

    if (flags != VIR_MEMORY_VIRTUAL) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
                          "%s", _("QEMU driver only supports virtual memory addrs"));
        goto cleanup;
R
Richard W.M. Jones 已提交
3724 3725
    }

3726
    if (!virDomainIsActive(vm)) {
R
Richard W.M. Jones 已提交
3727 3728
        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                         "%s", _("domain is not running"));
3729
        goto cleanup;
R
Richard W.M. Jones 已提交
3730 3731 3732 3733 3734 3735
    }

    /* Create a temporary filename. */
    if ((fd = mkstemp (tmp)) == -1) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
                          "%s", strerror (errno));
3736
        goto cleanup;
R
Richard W.M. Jones 已提交
3737 3738 3739 3740
    }

    /* Issue the memsave command. */
    snprintf (cmd, sizeof cmd, "memsave %llu %zi \"%s\"", offset, size, tmp);
3741
    if (qemudMonitorCommand (vm, cmd, &info) < 0) {
R
Richard W.M. Jones 已提交
3742
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3743
                          "%s", _("'memsave' command failed"));
3744
        goto cleanup;
R
Richard W.M. Jones 已提交
3745 3746 3747 3748 3749 3750 3751 3752
    }

    DEBUG ("memsave reply: %s", 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));
3753
        goto cleanup;
R
Richard W.M. Jones 已提交
3754 3755 3756
    }

    ret = 0;
3757 3758 3759

cleanup:
    VIR_FREE(info);
R
Richard W.M. Jones 已提交
3760 3761
    if (fd >= 0) close (fd);
    unlink (tmp);
3762 3763
    if (vm)
        virDomainObjUnlock(vm);
R
Richard W.M. Jones 已提交
3764 3765 3766
    return ret;
}

3767

3768 3769
static int
qemudDomainEventRegister (virConnectPtr conn,
3770
                          virConnectDomainEventCallback callback,
3771 3772
                          void *opaque,
                          virFreeCallback freecb)
3773
{
3774 3775 3776
    struct qemud_driver *driver = conn->privateData;
    int ret;

3777
    qemuDriverLock(driver);
3778 3779
    ret = virDomainEventCallbackListAdd(conn, driver->domainEventCallbacks,
                                        callback, opaque, freecb);
3780
    qemuDriverUnlock(driver);
3781

3782
    return ret;
3783 3784 3785 3786
}

static int
qemudDomainEventDeregister (virConnectPtr conn,
3787
                            virConnectDomainEventCallback callback)
3788
{
3789 3790 3791
    struct qemud_driver *driver = conn->privateData;
    int ret;

3792
    qemuDriverLock(driver);
3793 3794 3795 3796 3797 3798
    if (driver->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDelete(conn, driver->domainEventCallbacks,
                                                   callback);
    else
        ret = virDomainEventCallbackListRemove(conn, driver->domainEventCallbacks,
                                               callback);
3799
    qemuDriverUnlock(driver);
3800

3801
    return ret;
3802 3803
}

3804 3805 3806 3807 3808
static void qemuDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
                                        virConnectDomainEventCallback cb,
                                        void *cbopaque,
                                        void *opaque)
3809
{
3810
    struct qemud_driver *driver = opaque;
3811

3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855
    /* Drop the lock whle dispatching, for sake of re-entrancy */
    qemuDriverUnlock(driver);
    virDomainEventDispatchDefaultFunc(conn, event, cb, cbopaque, NULL);
    qemuDriverLock(driver);
}

static void qemuDomainEventFlush(int timer ATTRIBUTE_UNUSED, void *opaque)
{
    struct qemud_driver *driver = opaque;
    virDomainEventQueue tempQueue;

    qemuDriverLock(driver);

    driver->domainEventDispatching = 1;

    /* Copy the queue, so we're reentrant safe */
    tempQueue.count = driver->domainEventQueue->count;
    tempQueue.events = driver->domainEventQueue->events;
    driver->domainEventQueue->count = 0;
    driver->domainEventQueue->events = NULL;

    virEventUpdateTimeout(driver->domainEventTimer, -1);
    virDomainEventQueueDispatch(&tempQueue,
                                driver->domainEventCallbacks,
                                qemuDomainEventDispatchFunc,
                                driver);

    /* Purge any deleted callbacks */
    virDomainEventCallbackListPurgeMarked(driver->domainEventCallbacks);

    driver->domainEventDispatching = 0;
    qemuDriverUnlock(driver);
}


/* driver must be locked before calling */
static void qemuDomainEventQueue(struct qemud_driver *driver,
                                 virDomainEventPtr event)
{
    if (virDomainEventQueuePush(driver->domainEventQueue,
                                event) < 0)
        virDomainEventFree(event);
    if (qemu_driver->domainEventQueue->count == 1)
        virEventUpdateTimeout(driver->domainEventTimer, 0);
3856 3857
}

D
Daniel Veillard 已提交
3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
/* 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;
3876 3877
    struct qemud_driver *driver = dconn->privateData;
    virDomainDefPtr def = NULL;
D
Daniel Veillard 已提交
3878 3879 3880 3881 3882
    virDomainObjPtr vm = NULL;
    int this_port;
    char hostname [HOST_NAME_MAX+1];
    char migrateFrom [64];
    const char *p;
3883
    virDomainEventPtr event = NULL;
3884 3885 3886
    int ret = -1;;

    *uri_out = NULL;
D
Daniel Veillard 已提交
3887

3888
    qemuDriverLock(driver);
D
Daniel Veillard 已提交
3889 3890 3891
    if (!dom_xml) {
        qemudReportError (dconn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                          "%s", _("no domain XML passed"));
3892
        goto cleanup;
D
Daniel Veillard 已提交
3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912
    }

    /* 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));
3913
            goto cleanup;
D
Daniel Veillard 已提交
3914 3915 3916 3917 3918 3919
        }

        /* Caller frees */
        if (asprintf(uri_out, "tcp:%s:%d", hostname, this_port) < 0) {
            qemudReportError (dconn, NULL, NULL, VIR_ERR_NO_MEMORY,
                              "%s", strerror (errno));
3920 3921
            *uri_out = NULL;
            goto cleanup;
D
Daniel Veillard 已提交
3922 3923 3924 3925 3926 3927 3928 3929 3930
        }
    } 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"));
3931
            goto cleanup;
D
Daniel Veillard 已提交
3932 3933 3934 3935 3936 3937 3938 3939 3940
        }

        /* 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"));
3941
            goto cleanup;
D
Daniel Veillard 已提交
3942 3943 3944 3945 3946 3947 3948
        }
    }

    /* 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"));
3949
        goto cleanup;
D
Daniel Veillard 已提交
3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965
    }

    /* 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"));
3966
        goto cleanup;
D
Daniel Veillard 已提交
3967 3968 3969 3970 3971 3972 3973 3974 3975
    }
#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);
3976
            goto cleanup;
D
Daniel Veillard 已提交
3977 3978 3979 3980 3981 3982 3983 3984
        }
    }

    if (!(vm = virDomainAssignDef(dconn,
                                  &driver->domains,
                                  def))) {
        qemudReportError (dconn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("failed to assign new VM"));
3985
        goto cleanup;
D
Daniel Veillard 已提交
3986
    }
3987
    def = NULL;
D
Daniel Veillard 已提交
3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998

    /* 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"));
3999
        if (!vm->persistent) {
D
Daniel Veillard 已提交
4000
            virDomainRemoveInactive(&driver->domains, vm);
4001 4002
            vm = NULL;
        }
4003
        goto cleanup;
D
Daniel Veillard 已提交
4004
    }
4005 4006 4007 4008

    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_MIGRATED);
4009
    ret = 0;
D
Daniel Veillard 已提交
4010

4011 4012 4013 4014 4015
cleanup:
    virDomainDefFree(def);
    if (ret != 0) {
        VIR_FREE(*uri_out);
    }
4016 4017
    if (vm)
        virDomainObjUnlock(vm);
4018 4019
    if (event)
        qemuDomainEventQueue(driver, event);
4020
    qemuDriverUnlock(driver);
4021
    return ret;
D
Daniel Veillard 已提交
4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033
}

/* 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)
{
4034 4035
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
4036
    virDomainEventPtr event = NULL;
D
Daniel Veillard 已提交
4037 4038
    char *safe_uri;
    char cmd[HOST_NAME_MAX+50];
4039 4040
    char *info = NULL;
    int ret = -1;
D
Daniel Veillard 已提交
4041

4042
    qemuDriverLock(driver);
4043
    vm = virDomainFindByID(&driver->domains, dom->id);
D
Daniel Veillard 已提交
4044 4045 4046
    if (!vm) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                          _("no domain with matching id %d"), dom->id);
4047
        goto cleanup;
D
Daniel Veillard 已提交
4048 4049 4050 4051 4052
    }

    if (!virDomainIsActive(vm)) {
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("domain is not running"));
4053
        goto cleanup;
D
Daniel Veillard 已提交
4054 4055
    }

4056 4057 4058
    if (!(flags & VIR_MIGRATE_LIVE)) {
        /* Pause domain for non-live migration */
        snprintf(cmd, sizeof cmd, "%s", "stop");
4059
        qemudMonitorCommand (vm, cmd, &info);
4060 4061 4062
        DEBUG ("stop reply: %s", info);
        VIR_FREE(info);

4063 4064 4065 4066 4067 4068
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_SUSPENDED,
                                         VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED);
        if (event)
            qemuDomainEventQueue(driver, event);
        event = NULL;
4069 4070
    }

D
Daniel Veillard 已提交
4071 4072 4073
    if (resource > 0) {
        /* Issue migrate_set_speed command.  Don't worry if it fails. */
        snprintf (cmd, sizeof cmd, "migrate_set_speed %lum", resource);
4074
        qemudMonitorCommand (vm, cmd, &info);
D
Daniel Veillard 已提交
4075 4076 4077 4078 4079 4080 4081 4082 4083 4084

        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));
4085
        goto cleanup;
D
Daniel Veillard 已提交
4086 4087 4088 4089
    }
    snprintf (cmd, sizeof cmd, "migrate \"%s\"", safe_uri);
    VIR_FREE (safe_uri);

4090
    if (qemudMonitorCommand (vm, cmd, &info) < 0) {
D
Daniel Veillard 已提交
4091 4092
        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          "%s", _("migrate operation failed"));
4093
        goto cleanup;
D
Daniel Veillard 已提交
4094 4095 4096 4097 4098 4099 4100 4101
    }

    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);
4102
        goto cleanup;
D
Daniel Veillard 已提交
4103 4104 4105 4106
    }

    /* Clean up the source domain. */
    qemudShutdownVMDaemon (dom->conn, driver, vm);
4107 4108 4109 4110

    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_MIGRATED);
4111
    if (!vm->persistent) {
D
Daniel Veillard 已提交
4112
        virDomainRemoveInactive(&driver->domains, vm);
4113 4114
        vm = NULL;
    }
4115
    ret = 0;
D
Daniel Veillard 已提交
4116

4117 4118
cleanup:
    VIR_FREE(info);
4119 4120
    if (vm)
        virDomainObjUnlock(vm);
4121 4122
    if (event)
        qemuDomainEventQueue(driver, event);
4123
    qemuDriverUnlock(driver);
4124
    return ret;
D
Daniel Veillard 已提交
4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136
}

/* 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)
{
4137 4138 4139
    struct qemud_driver *driver = dconn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
4140
    virDomainEventPtr event = NULL;
D
Daniel Veillard 已提交
4141 4142
    char *info = NULL;

4143
    qemuDriverLock(driver);
4144
    vm = virDomainFindByName(&driver->domains, dname);
D
Daniel Veillard 已提交
4145 4146 4147
    if (!vm) {
        qemudReportError (dconn, NULL, NULL, VIR_ERR_INVALID_DOMAIN,
                          _("no domain with matching name %s"), dname);
4148
        goto cleanup;
D
Daniel Veillard 已提交
4149 4150 4151 4152 4153 4154 4155 4156 4157
    }

    /* 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;
4158 4159 4160
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_RESUMED,
                                         VIR_DOMAIN_EVENT_RESUMED_MIGRATED);
D
Daniel Veillard 已提交
4161 4162
    } else {
        qemudShutdownVMDaemon (dconn, driver, vm);
4163 4164 4165
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_FAILED);
4166
        if (!vm->persistent) {
D
Daniel Veillard 已提交
4167
            virDomainRemoveInactive(&driver->domains, vm);
4168 4169
            vm = NULL;
        }
D
Daniel Veillard 已提交
4170
    }
4171 4172

cleanup:
4173 4174
    if (vm)
        virDomainObjUnlock(vm);
4175 4176
    if (event)
        qemuDomainEventQueue(driver, event);
4177
    qemuDriverUnlock(driver);
4178
    return dom;
D
Daniel Veillard 已提交
4179 4180
}

4181 4182 4183 4184 4185
static virDriver qemuDriver = {
    VIR_DRV_QEMU,
    "QEMU",
    qemudOpen, /* open */
    qemudClose, /* close */
D
Daniel Veillard 已提交
4186
    qemudSupportsFeature, /* supports_feature */
4187 4188
    qemudGetType, /* type */
    qemudGetVersion, /* version */
4189
    qemudGetHostname, /* hostname */
4190
    NULL, /* URI  */
4191 4192 4193 4194 4195
    qemudGetMaxVCPUs, /* getMaxVcpus */
    qemudGetNodeInfo, /* nodeGetInfo */
    qemudGetCapabilities, /* getCapabilities */
    qemudListDomains, /* listDomains */
    qemudNumDomains, /* numOfDomains */
4196
    qemudDomainCreate, /* domainCreateXML */
4197 4198 4199 4200 4201
    qemudDomainLookupByID, /* domainLookupByID */
    qemudDomainLookupByUUID, /* domainLookupByUUID */
    qemudDomainLookupByName, /* domainLookupByName */
    qemudDomainSuspend, /* domainSuspend */
    qemudDomainResume, /* domainResume */
4202
    qemudDomainShutdown, /* domainShutdown */
4203 4204 4205
    NULL, /* domainReboot */
    qemudDomainDestroy, /* domainDestroy */
    qemudDomainGetOSType, /* domainGetOSType */
4206 4207 4208
    qemudDomainGetMaxMemory, /* domainGetMaxMemory */
    qemudDomainSetMaxMemory, /* domainSetMaxMemory */
    qemudDomainSetMemory, /* domainSetMemory */
4209 4210 4211 4212
    qemudDomainGetInfo, /* domainGetInfo */
    qemudDomainSave, /* domainSave */
    qemudDomainRestore, /* domainRestore */
    NULL, /* domainCoreDump */
4213
    qemudDomainSetVcpus, /* domainSetVcpus */
4214 4215 4216 4217
#if HAVE_SCHED_GETAFFINITY
    qemudDomainPinVcpu, /* domainPinVcpu */
    qemudDomainGetVcpus, /* domainGetVcpus */
#else
4218 4219
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
4220
#endif
4221
    qemudDomainGetMaxVcpus, /* domainGetMaxVcpus */
4222 4223 4224 4225 4226 4227
    qemudDomainDumpXML, /* domainDumpXML */
    qemudListDefinedDomains, /* listDomains */
    qemudNumDefinedDomains, /* numOfDomains */
    qemudDomainStart, /* domainCreate */
    qemudDomainDefine, /* domainDefineXML */
    qemudDomainUndefine, /* domainUndefine */
4228
    qemudDomainAttachDevice, /* domainAttachDevice */
4229
    qemudDomainDetachDevice, /* domainDetachDevice */
4230 4231 4232 4233 4234
    qemudDomainGetAutostart, /* domainGetAutostart */
    qemudDomainSetAutostart, /* domainSetAutostart */
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
D
Daniel Veillard 已提交
4235 4236
    NULL, /* domainMigratePrepare (v1) */
    qemudDomainMigratePerform, /* domainMigratePerform */
4237
    NULL, /* domainMigrateFinish */
4238
    qemudDomainBlockStats, /* domainBlockStats */
4239
    qemudDomainInterfaceStats, /* domainInterfaceStats */
4240
    qemudDomainBlockPeek, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
4241
    qemudDomainMemoryPeek, /* domainMemoryPeek */
4242 4243 4244 4245
#if HAVE_NUMACTL
    qemudNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    qemudNodeGetFreeMemory,  /* getFreeMemory */
#else
4246
    NULL, /* nodeGetCellsFreeMemory */
4247
    NULL, /* getFreeMemory */
4248
#endif
4249 4250
    qemudDomainEventRegister, /* domainEventRegister */
    qemudDomainEventDeregister, /* domainEventDeregister */
D
Daniel Veillard 已提交
4251 4252
    qemudDomainMigratePrepare2, /* domainMigratePrepare2 */
    qemudDomainMigrateFinish2, /* domainMigrateFinish2 */
4253 4254 4255
};


4256
static virStateDriver qemuStateDriver = {
4257 4258 4259 4260
    .initialize = qemudStartup,
    .cleanup = qemudShutdown,
    .reload = qemudReload,
    .active = qemudActive,
4261
};
4262

4263
int qemuRegister(void) {
4264 4265 4266 4267 4268
    virRegisterDriver(&qemuDriver);
    virRegisterStateDriver(&qemuStateDriver);
    return 0;
}