qemu_driver.c 321.8 KB
Newer Older
D
Daniel P. Berrange 已提交
1 2 3
/*
 * driver.c: core driver methods for managing qemu guests
 *
4
 * Copyright (C) 2006-2011 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
#include <sys/types.h>
#include <sys/poll.h>
28
#include <sys/time.h>
D
Daniel P. Berrange 已提交
29 30 31
#include <dirent.h>
#include <limits.h>
#include <string.h>
32
#include <stdbool.h>
D
Daniel P. Berrange 已提交
33 34 35 36 37
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
38
#include <sys/utsname.h>
39 40 41 42
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <paths.h>
43
#include <stdio.h>
44
#include <sys/wait.h>
45
#include <sys/ioctl.h>
46
#include <sys/un.h>
D
Daniel P. Berrange 已提交
47

48

49
#include "virterror_internal.h"
50
#include "logging.h"
51
#include "datatypes.h"
52 53
#include "qemu_driver.h"
#include "qemu_conf.h"
54
#include "qemu_capabilities.h"
55
#include "qemu_command.h"
56
#include "qemu_cgroup.h"
57
#include "qemu_hostdev.h"
58
#include "qemu_hotplug.h"
59
#include "qemu_monitor.h"
60
#include "qemu_bridge_filter.h"
61
#include "qemu_audit.h"
J
Jim Meyering 已提交
62
#include "c-ctype.h"
63
#include "event.h"
64
#include "buf.h"
65
#include "util.h"
66
#include "nodeinfo.h"
67
#include "stats_linux.h"
68
#include "capabilities.h"
69
#include "memory.h"
70
#include "uuid.h"
71
#include "domain_conf.h"
72 73
#include "node_device_conf.h"
#include "pci.h"
74
#include "hostusb.h"
75
#include "processinfo.h"
C
Chris Lalancette 已提交
76
#include "libvirt_internal.h"
77
#include "xml.h"
78
#include "cpu/cpu.h"
79
#include "macvtap.h"
80
#include "sysinfo.h"
81
#include "domain_nwfilter.h"
82
#include "hooks.h"
83
#include "storage_file.h"
84
#include "files.h"
85
#include "fdstream.h"
86
#include "configmake.h"
H
Hu Tao 已提交
87
#include "threadpool.h"
88

89 90
#define VIR_FROM_THIS VIR_FROM_QEMU

91 92 93
#define QEMU_VNC_PORT_MIN  5900
#define QEMU_VNC_PORT_MAX  65535

94 95
#define QEMU_NB_MEM_PARAM  3

96

97 98
#define timeval_to_ms(tv)       (((tv).tv_sec * 1000ull) + ((tv).tv_usec / 1000))

H
Hu Tao 已提交
99 100 101 102 103 104 105 106
struct watchdogEvent
{
    virDomainObjPtr vm;
    int action;
};

static void processWatchdogEvent(void *data, void *opaque);

107 108
static int qemudShutdown(void);

109 110 111
static void qemuDomainEventFlush(int timer, void *opaque);
static void qemuDomainEventQueue(struct qemud_driver *driver,
                                 virDomainEventPtr event);
112

J
Jiri Denemark 已提交
113 114
static int qemudDomainObjStart(virConnectPtr conn,
                               struct qemud_driver *driver,
115 116
                               virDomainObjPtr vm,
                               bool start_paused);
J
Jiri Denemark 已提交
117

118 119
static int qemudStartVMDaemon(virConnectPtr conn,
                              struct qemud_driver *driver,
120
                              virDomainObjPtr vm,
121
                              const char *migrateFrom,
122
                              bool start_paused,
123
                              int stdin_fd,
124 125
                              const char *stdin_path,
                              enum virVMOperationType vmop);
126

127
static void qemudShutdownVMDaemon(struct qemud_driver *driver,
128 129
                                  virDomainObjPtr vm,
                                  int migrated);
130

131
static int qemudDomainGetMaxVcpus(virDomainPtr dom);
132

133
static int qemuDetectVcpuPIDs(struct qemud_driver *driver,
134
                              virDomainObjPtr vm);
135

136 137 138
static int qemudVMFiltersInstantiate(virConnectPtr conn,
                                     virDomainDefPtr def);

J
Jim Meyering 已提交
139
static struct qemud_driver *qemu_driver = NULL;
140

141

H
Hu Tao 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static int doStartCPUs(struct qemud_driver *driver, virDomainObjPtr vm, virConnectPtr conn)
{
    int ret;
    qemuDomainObjPrivatePtr priv = vm->privateData;

    qemuDomainObjEnterMonitorWithDriver(driver, vm);
    ret = qemuMonitorStartCPUs(priv->mon, conn);
    if (ret == 0) {
        vm->state = VIR_DOMAIN_RUNNING;
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);

    return ret;
}

H
Hu Tao 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
static int doStopCPUs(struct qemud_driver *driver, virDomainObjPtr vm)
{
    int ret;
    int oldState = vm->state;
    qemuDomainObjPrivatePtr priv = vm->privateData;

    vm->state = VIR_DOMAIN_PAUSED;
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
    ret = qemuMonitorStopCPUs(priv->mon);
    qemuDomainObjExitMonitorWithDriver(driver, vm);
    if (ret < 0) {
        vm->state = oldState;
    }
    return ret;
}

173

174
static int
175
qemudLogFD(struct qemud_driver *driver, const char* name, bool append)
176
{
177
    char *logfile;
178
    mode_t logmode;
179
    int fd = -1;
180

181
    if (virAsprintf(&logfile, "%s/%s.log", driver->logDir, name) < 0) {
182
        virReportOOMError();
183 184 185 186
        return -1;
    }

    logmode = O_CREAT | O_WRONLY;
187
    /* Only logrotate files in /var/log, so only append if running privileged */
188
    if (driver->privileged || append)
189
        logmode |= O_APPEND;
190 191 192
    else
        logmode |= O_TRUNC;

193
    if ((fd = open(logfile, logmode, S_IRUSR | S_IWUSR)) < 0) {
194
        virReportSystemError(errno,
195 196
                             _("failed to create logfile %s"),
                             logfile);
197
        VIR_FREE(logfile);
198 199
        return -1;
    }
200
    VIR_FREE(logfile);
201
    if (virSetCloseExec(fd) < 0) {
202
        virReportSystemError(errno, "%s",
203
                             _("Unable to set VM logfile close-on-exec flag"));
204
        VIR_FORCE_CLOSE(fd);
205 206 207 208 209 210
        return -1;
    }
    return fd;
}


211
static int
212
qemudLogReadFD(const char* logDir, const char* name, off_t pos)
213
{
214
    char *logfile;
215
    mode_t logmode = O_RDONLY;
216
    int fd = -1;
217

218
    if (virAsprintf(&logfile, "%s/%s.log", logDir, name) < 0) {
219 220 221
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("failed to build logfile name %s/%s.log"),
                        logDir, name);
222 223 224 225
        return -1;
    }

    if ((fd = open(logfile, logmode)) < 0) {
226
        virReportSystemError(errno,
227 228
                             _("failed to create logfile %s"),
                             logfile);
229
        VIR_FREE(logfile);
230 231
        return -1;
    }
232
    if (virSetCloseExec(fd) < 0) {
233
        virReportSystemError(errno, "%s",
234
                             _("Unable to set VM logfile close-on-exec flag"));
235
        VIR_FORCE_CLOSE(fd);
236
        VIR_FREE(logfile);
237 238
        return -1;
    }
239
    if (pos < 0 || lseek(fd, pos, SEEK_SET) < 0) {
240
        virReportSystemError(pos < 0 ? 0 : errno,
241 242
                             _("Unable to seek to %lld in %s"),
                             (long long) pos, logfile);
243
        VIR_FORCE_CLOSE(fd);
244
    }
245
    VIR_FREE(logfile);
246 247 248 249
    return fd;
}


250 251 252 253 254 255 256 257 258
struct qemuAutostartData {
    struct qemud_driver *driver;
    virConnectPtr conn;
};
static void
qemuAutostartDomain(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
{
    virDomainObjPtr vm = payload;
    struct qemuAutostartData *data = opaque;
259
    virErrorPtr err;
260 261

    virDomainObjLock(vm);
262 263 264 265 266 267 268 269 270
    virResetLastError();
    if (qemuDomainObjBeginJobWithDriver(data->driver, vm) < 0) {
        err = virGetLastError();
        VIR_ERROR(_("Failed to start job on VM '%s': %s"),
                  vm->def->name,
                  err ? err->message : _("unknown error"));
    } else {
        if (vm->autostart &&
            !virDomainObjIsActive(vm) &&
271
            qemudDomainObjStart(data->conn, data->driver, vm, false) < 0) {
272
            err = virGetLastError();
273
            VIR_ERROR(_("Failed to autostart VM '%s': %s"),
274
                      vm->def->name,
275
                      err ? err->message : _("unknown error"));
276
        }
277 278 279

        if (qemuDomainObjEndJob(vm) == 0)
            vm = NULL;
280
    }
281 282 283

    if (vm)
        virDomainObjUnlock(vm);
284 285
}

286 287
static void
qemudAutostartConfigs(struct qemud_driver *driver) {
288 289 290 291 292
    /* 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
     */
293 294 295
    virConnectPtr conn = virConnectOpen(driver->privileged ?
                                        "qemu:///system" :
                                        "qemu:///session");
296
    /* Ignoring NULL conn which is mostly harmless here */
297
    struct qemuAutostartData data = { driver, conn };
298

299
    qemuDriverLock(driver);
300
    virHashForEach(driver->domains.objs, qemuAutostartDomain, &data);
301
    qemuDriverUnlock(driver);
302

303 304
    if (conn)
        virConnectClose(conn);
305 306
}

307 308 309 310 311 312 313 314 315

/**
 * qemudRemoveDomainStatus
 *
 * remove all state files of a domain from statedir
 *
 * Returns 0 on success
 */
static int
316
qemudRemoveDomainStatus(struct qemud_driver *driver,
317 318
                        virDomainObjPtr vm)
{
319
    char ebuf[1024];
320 321 322
    char *file = NULL;

    if (virAsprintf(&file, "%s/%s.xml", driver->stateDir, vm->def->name) < 0) {
323
        virReportOOMError();
D
Daniel Veillard 已提交
324
        return(-1);
325 326
    }

327
    if (unlink(file) < 0 && errno != ENOENT && errno != ENOTDIR)
328
        VIR_WARN("Failed to remove domain XML for %s: %s",
D
Daniel Veillard 已提交
329 330 331
                 vm->def->name, virStrerror(errno, ebuf, sizeof(ebuf)));
    VIR_FREE(file);

332
    if (virFileDeletePid(driver->stateDir, vm->def->name) != 0)
333
        VIR_WARN("Failed to remove PID file for %s: %s",
334
                 vm->def->name, virStrerror(errno, ebuf, sizeof(ebuf)));
335

D
Daniel Veillard 已提交
336

337
    return 0;
338 339
}

340 341 342 343 344 345 346 347 348 349 350 351 352

/*
 * This is a callback registered with a qemuMonitorPtr  instance,
 * and to be invoked when the monitor console hits an end of file
 * condition, or error, thus indicating VM shutdown should be
 * performed
 */
static void
qemuHandleMonitorEOF(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
                     virDomainObjPtr vm,
                     int hasError) {
    struct qemud_driver *driver = qemu_driver;
    virDomainEventPtr event = NULL;
353
    qemuDomainObjPrivatePtr priv;
354

355
    VIR_DEBUG("Received EOF on %p '%s'", vm, vm->def->name);
356

357 358
    virDomainObjLock(vm);

359 360 361 362 363 364
    if (!virDomainObjIsActive(vm)) {
        VIR_DEBUG("Domain %p is not active, ignoring EOF", vm);
        virDomainObjUnlock(vm);
        return;
    }

365 366 367 368 369 370 371
    priv = vm->privateData;
    if (!hasError && priv->monJSON && !priv->gotShutdown) {
        VIR_DEBUG("Monitor connection to '%s' closed without SHUTDOWN event; "
                  "assuming the domain crashed", vm->def->name);
        hasError = 1;
    }

372 373 374 375 376 377
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     hasError ?
                                     VIR_DOMAIN_EVENT_STOPPED_FAILED :
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);

378
    qemudShutdownVMDaemon(driver, vm, 0);
379 380
    qemuDomainStopAudit(vm, hasError ? "failed" : "shutdown");

381 382 383 384 385 386 387 388 389 390 391 392 393
    if (!vm->persistent)
        virDomainRemoveInactive(&driver->domains, vm);
    else
        virDomainObjUnlock(vm);

    if (event) {
        qemuDriverLock(driver);
        qemuDomainEventQueue(driver, event);
        qemuDriverUnlock(driver);
    }
}


394 395 396
static virDomainDiskDefPtr
findDomainDiskByPath(virDomainObjPtr vm,
                     const char *path)
397 398 399 400 401 402 403
{
    int i;

    for (i = 0; i < vm->def->ndisks; i++) {
        virDomainDiskDefPtr disk;

        disk = vm->def->disks[i];
404 405
        if (disk->src != NULL && STREQ(disk->src, path))
            return disk;
406
    }
407 408 409 410

    qemuReportError(VIR_ERR_INTERNAL_ERROR,
                    _("no disk found with path %s"),
                    path);
411 412 413
    return NULL;
}

414 415 416 417 418 419
static virDomainDiskDefPtr
findDomainDiskByAlias(virDomainObjPtr vm,
                      const char *alias)
{
    int i;

420 421 422
    if (STRPREFIX(alias, QEMU_DRIVE_HOST_PREFIX))
        alias += strlen(QEMU_DRIVE_HOST_PREFIX);

423 424 425 426 427 428 429 430 431 432 433 434 435 436
    for (i = 0; i < vm->def->ndisks; i++) {
        virDomainDiskDefPtr disk;

        disk = vm->def->disks[i];
        if (disk->info.alias != NULL && STREQ(disk->info.alias, alias))
            return disk;
    }

    qemuReportError(VIR_ERR_INTERNAL_ERROR,
                    _("no disk found with alias %s"),
                    alias);
    return NULL;
}

437
static int
438 439 440 441
getVolumeQcowPassphrase(virConnectPtr conn,
                        virDomainDiskDefPtr disk,
                        char **secretRet,
                        size_t *secretLen)
442 443 444 445 446
{
    virSecretPtr secret;
    char *passphrase;
    unsigned char *data;
    size_t size;
447
    int ret = -1;
448
    virStorageEncryptionPtr enc;
449

450 451 452 453 454 455 456
    if (!disk->encryption) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("disk %s does not have any encryption information"),
                        disk->src);
        return -1;
    }
    enc = disk->encryption;
457 458

    if (!conn) {
459
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
460
                        "%s", _("cannot find secrets without a connection"));
461
        goto cleanup;
462 463 464 465 466
    }

    if (conn->secretDriver == NULL ||
        conn->secretDriver->lookupByUUID == NULL ||
        conn->secretDriver->getValue == NULL) {
467 468
        qemuReportError(VIR_ERR_NO_SUPPORT, "%s",
                        _("secret storage not supported"));
469
        goto cleanup;
470 471 472 473 474 475
    }

    if (enc->format != VIR_STORAGE_ENCRYPTION_FORMAT_QCOW ||
        enc->nsecrets != 1 ||
        enc->secrets[0]->type !=
        VIR_STORAGE_ENCRYPTION_SECRET_TYPE_PASSPHRASE) {
476
        qemuReportError(VIR_ERR_XML_ERROR,
477
                        _("invalid <encryption> for volume %s"), disk->src);
478
        goto cleanup;
479 480 481 482 483
    }

    secret = conn->secretDriver->lookupByUUID(conn,
                                              enc->secrets[0]->uuid);
    if (secret == NULL)
484
        goto cleanup;
485 486 487 488
    data = conn->secretDriver->getValue(secret, &size,
                                        VIR_SECRET_GET_VALUE_INTERNAL_CALL);
    virUnrefSecret(secret);
    if (data == NULL)
489
        goto cleanup;
490 491 492 493

    if (memchr(data, '\0', size) != NULL) {
        memset(data, 0, size);
        VIR_FREE(data);
494
        qemuReportError(VIR_ERR_XML_ERROR,
495
                        _("format='qcow' passphrase for %s must not contain a "
496
                          "'\\0'"), disk->src);
497
        goto cleanup;
498 499 500 501 502
    }

    if (VIR_ALLOC_N(passphrase, size + 1) < 0) {
        memset(data, 0, size);
        VIR_FREE(data);
503
        virReportOOMError();
504
        goto cleanup;
505 506 507 508 509 510 511 512 513 514
    }
    memcpy(passphrase, data, size);
    passphrase[size] = '\0';

    memset(data, 0, size);
    VIR_FREE(data);

    *secretRet = passphrase;
    *secretLen = size;

515 516 517
    ret = 0;

cleanup:
518 519
    return ret;
}
520

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
static int
findVolumeQcowPassphrase(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
                         virConnectPtr conn,
                         virDomainObjPtr vm,
                         const char *path,
                         char **secretRet,
                         size_t *secretLen)
{
    virDomainDiskDefPtr disk;
    int ret = -1;

    virDomainObjLock(vm);
    disk = findDomainDiskByPath(vm, path);

    if (!disk)
        goto cleanup;

    ret = getVolumeQcowPassphrase(conn, disk, secretRet, secretLen);

cleanup:
    virDomainObjUnlock(vm);
542
    return ret;
543 544
}

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

static int
qemuHandleDomainReset(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
                      virDomainObjPtr vm)
{
    struct qemud_driver *driver = qemu_driver;
    virDomainEventPtr event;

    virDomainObjLock(vm);
    event = virDomainEventRebootNewFromObj(vm);
    virDomainObjUnlock(vm);

    if (event) {
        qemuDriverLock(driver);
        qemuDomainEventQueue(driver, event);
        qemuDriverUnlock(driver);
    }

    return 0;
}


567 568 569 570 571 572 573 574 575 576 577 578
static int
qemuHandleDomainShutdown(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
                         virDomainObjPtr vm)
{
    virDomainObjLock(vm);
    ((qemuDomainObjPrivatePtr) vm->privateData)->gotShutdown = true;
    virDomainObjUnlock(vm);

    return 0;
}


579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
static int
qemuHandleDomainStop(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
                     virDomainObjPtr vm)
{
    struct qemud_driver *driver = qemu_driver;
    virDomainEventPtr event = NULL;

    virDomainObjLock(vm);
    if (vm->state == VIR_DOMAIN_RUNNING) {
        VIR_DEBUG("Transitioned guest %s to paused state due to unknown event", vm->def->name);

        vm->state = VIR_DOMAIN_PAUSED;
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_SUSPENDED,
                                         VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);

        if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
            VIR_WARN("Unable to save status on vm %s after IO error", vm->def->name);
    }
    virDomainObjUnlock(vm);

    if (event) {
        qemuDriverLock(driver);
        if (event)
            qemuDomainEventQueue(driver, event);
        qemuDriverUnlock(driver);
    }

    return 0;
}


611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
static int
qemuHandleDomainRTCChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
                          virDomainObjPtr vm,
                          long long offset)
{
    struct qemud_driver *driver = qemu_driver;
    virDomainEventPtr event;

    virDomainObjLock(vm);
    event = virDomainEventRTCChangeNewFromObj(vm, offset);

    if (vm->def->clock.offset == VIR_DOMAIN_CLOCK_OFFSET_VARIABLE)
        vm->def->clock.data.adjustment = offset;

    if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
        VIR_WARN0("unable to save domain status with RTC change");

    virDomainObjUnlock(vm);

    if (event) {
        qemuDriverLock(driver);
        qemuDomainEventQueue(driver, event);
        qemuDriverUnlock(driver);
    }

    return 0;
}


640 641 642 643 644 645
static int
qemuHandleDomainWatchdog(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
                         virDomainObjPtr vm,
                         int action)
{
    struct qemud_driver *driver = qemu_driver;
646 647
    virDomainEventPtr watchdogEvent = NULL;
    virDomainEventPtr lifecycleEvent = NULL;
648 649

    virDomainObjLock(vm);
650 651 652 653 654 655 656 657 658 659 660 661 662 663
    watchdogEvent = virDomainEventWatchdogNewFromObj(vm, action);

    if (action == VIR_DOMAIN_EVENT_WATCHDOG_PAUSE &&
        vm->state == VIR_DOMAIN_RUNNING) {
        VIR_DEBUG("Transitioned guest %s to paused state due to watchdog", vm->def->name);

        vm->state = VIR_DOMAIN_PAUSED;
        lifecycleEvent = virDomainEventNewFromObj(vm,
                                                  VIR_DOMAIN_EVENT_SUSPENDED,
                                                  VIR_DOMAIN_EVENT_SUSPENDED_WATCHDOG);

        if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
            VIR_WARN("Unable to save status on vm %s after IO error", vm->def->name);
    }
H
Hu Tao 已提交
664 665 666 667 668 669 670 671 672 673 674

    if (vm->def->watchdog->action == VIR_DOMAIN_WATCHDOG_ACTION_DUMP) {
        struct watchdogEvent *wdEvent;
        if (VIR_ALLOC(wdEvent) == 0) {
            wdEvent->action = VIR_DOMAIN_WATCHDOG_ACTION_DUMP;
            wdEvent->vm = vm;
            ignore_value(virThreadPoolSendJob(driver->workerPool, wdEvent));
        } else
            virReportOOMError();
    }

675 676
    virDomainObjUnlock(vm);

677
    if (watchdogEvent || lifecycleEvent) {
678
        qemuDriverLock(driver);
679 680 681 682
        if (watchdogEvent)
            qemuDomainEventQueue(driver, watchdogEvent);
        if (lifecycleEvent)
            qemuDomainEventQueue(driver, lifecycleEvent);
683 684 685 686 687 688 689
        qemuDriverUnlock(driver);
    }

    return 0;
}


690 691 692 693
static int
qemuHandleDomainIOError(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
                        virDomainObjPtr vm,
                        const char *diskAlias,
694 695
                        int action,
                        const char *reason)
696 697
{
    struct qemud_driver *driver = qemu_driver;
698
    virDomainEventPtr ioErrorEvent = NULL;
699
    virDomainEventPtr ioErrorEvent2 = NULL;
700
    virDomainEventPtr lifecycleEvent = NULL;
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
    const char *srcPath;
    const char *devAlias;
    virDomainDiskDefPtr disk;

    virDomainObjLock(vm);
    disk = findDomainDiskByAlias(vm, diskAlias);

    if (disk) {
        srcPath = disk->src;
        devAlias = disk->info.alias;
    } else {
        srcPath = "";
        devAlias = "";
    }

716
    ioErrorEvent = virDomainEventIOErrorNewFromObj(vm, srcPath, devAlias, action);
717
    ioErrorEvent2 = virDomainEventIOErrorReasonNewFromObj(vm, srcPath, devAlias, action, reason);
718 719 720 721 722 723 724 725 726 727 728 729 730

    if (action == VIR_DOMAIN_EVENT_IO_ERROR_PAUSE &&
        vm->state == VIR_DOMAIN_RUNNING) {
        VIR_DEBUG("Transitioned guest %s to paused state due to IO error", vm->def->name);

        vm->state = VIR_DOMAIN_PAUSED;
        lifecycleEvent = virDomainEventNewFromObj(vm,
                                                  VIR_DOMAIN_EVENT_SUSPENDED,
                                                  VIR_DOMAIN_EVENT_SUSPENDED_IOERROR);

        if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
            VIR_WARN("Unable to save status on vm %s after IO error", vm->def->name);
    }
731 732
    virDomainObjUnlock(vm);

733
    if (ioErrorEvent || ioErrorEvent2 || lifecycleEvent) {
734
        qemuDriverLock(driver);
735 736
        if (ioErrorEvent)
            qemuDomainEventQueue(driver, ioErrorEvent);
737 738
        if (ioErrorEvent2)
            qemuDomainEventQueue(driver, ioErrorEvent2);
739 740
        if (lifecycleEvent)
            qemuDomainEventQueue(driver, lifecycleEvent);
741 742 743 744 745 746 747
        qemuDriverUnlock(driver);
    }

    return 0;
}


748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 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 827 828 829 830 831 832 833 834 835 836 837 838 839
static int
qemuHandleDomainGraphics(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
                         virDomainObjPtr vm,
                         int phase,
                         int localFamily,
                         const char *localNode,
                         const char *localService,
                         int remoteFamily,
                         const char *remoteNode,
                         const char *remoteService,
                         const char *authScheme,
                         const char *x509dname,
                         const char *saslUsername)
{
    struct qemud_driver *driver = qemu_driver;
    virDomainEventPtr event;
    virDomainEventGraphicsAddressPtr localAddr = NULL;
    virDomainEventGraphicsAddressPtr remoteAddr = NULL;
    virDomainEventGraphicsSubjectPtr subject = NULL;
    int i;

    virDomainObjLock(vm);

    if (VIR_ALLOC(localAddr) < 0)
        goto no_memory;
    localAddr->family = localFamily;
    if (!(localAddr->service = strdup(localService)) ||
        !(localAddr->node = strdup(localNode)))
        goto no_memory;

    if (VIR_ALLOC(remoteAddr) < 0)
        goto no_memory;
    remoteAddr->family = remoteFamily;
    if (!(remoteAddr->service = strdup(remoteService)) ||
        !(remoteAddr->node = strdup(remoteNode)))
        goto no_memory;

    if (VIR_ALLOC(subject) < 0)
        goto no_memory;
    if (x509dname) {
        if (VIR_REALLOC_N(subject->identities, subject->nidentity+1) < 0)
            goto no_memory;
        if (!(subject->identities[subject->nidentity].type = strdup("x509dname")) ||
            !(subject->identities[subject->nidentity].name = strdup(x509dname)))
            goto no_memory;
        subject->nidentity++;
    }
    if (saslUsername) {
        if (VIR_REALLOC_N(subject->identities, subject->nidentity+1) < 0)
            goto no_memory;
        if (!(subject->identities[subject->nidentity].type = strdup("saslUsername")) ||
            !(subject->identities[subject->nidentity].name = strdup(saslUsername)))
            goto no_memory;
        subject->nidentity++;
    }

    event = virDomainEventGraphicsNewFromObj(vm, phase, localAddr, remoteAddr, authScheme, subject);
    virDomainObjUnlock(vm);

    if (event) {
        qemuDriverLock(driver);
        qemuDomainEventQueue(driver, event);
        qemuDriverUnlock(driver);
    }

    return 0;

no_memory:
    virReportOOMError();
    if (localAddr) {
        VIR_FREE(localAddr->service);
        VIR_FREE(localAddr->node);
        VIR_FREE(localAddr);
    }
    if (remoteAddr) {
        VIR_FREE(remoteAddr->service);
        VIR_FREE(remoteAddr->node);
        VIR_FREE(remoteAddr);
    }
    if (subject) {
        for (i = 0 ; i < subject->nidentity ; i++) {
            VIR_FREE(subject->identities[i].type);
            VIR_FREE(subject->identities[i].name);
        }
        VIR_FREE(subject->identities);
        VIR_FREE(subject);
    }

    return -1;
}


840 841 842 843 844 845 846 847 848
static void qemuHandleMonitorDestroy(qemuMonitorPtr mon,
                                     virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    if (priv->mon == mon)
        priv->mon = NULL;
    virDomainObjUnref(vm);
}

849
static qemuMonitorCallbacks monitorCallbacks = {
850
    .destroy = qemuHandleMonitorDestroy,
851 852
    .eofNotify = qemuHandleMonitorEOF,
    .diskSecretLookup = findVolumeQcowPassphrase,
853
    .domainShutdown = qemuHandleDomainShutdown,
854
    .domainStop = qemuHandleDomainStop,
855
    .domainReset = qemuHandleDomainReset,
856
    .domainRTCChange = qemuHandleDomainRTCChange,
857
    .domainWatchdog = qemuHandleDomainWatchdog,
858
    .domainIOError = qemuHandleDomainIOError,
859
    .domainGraphics = qemuHandleDomainGraphics,
860 861
};

862
static int
863
qemuConnectMonitor(struct qemud_driver *driver, virDomainObjPtr vm)
864
{
865
    qemuDomainObjPrivatePtr priv = vm->privateData;
866
    int ret = -1;
867

868
    if (virSecurityManagerSetSocketLabel(driver->securityManager, vm) < 0) {
869 870
        VIR_ERROR(_("Failed to set security context for monitor for %s"),
                  vm->def->name);
871 872 873
        goto error;
    }

874 875 876 877 878 879 880 881 882 883 884
    /* Hold an extra reference because we can't allow 'vm' to be
     * deleted while the monitor is active */
    virDomainObjRef(vm);

    priv->mon = qemuMonitorOpen(vm,
                                priv->monConfig,
                                priv->monJSON,
                                &monitorCallbacks);

    if (priv->mon == NULL)
        virDomainObjUnref(vm);
885

886
    if (virSecurityManagerClearSocketLabel(driver->securityManager, vm) < 0) {
887 888
        VIR_ERROR(_("Failed to clear security context for monitor for %s"),
                  vm->def->name);
889
        goto error;
890
    }
891

892 893 894 895 896 897
    if (priv->mon == NULL) {
        VIR_INFO("Failed to connect monitor for %s", vm->def->name);
        goto error;
    }


898 899 900 901
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
    ret = qemuMonitorSetCapabilities(priv->mon);
    qemuDomainObjExitMonitorWithDriver(driver, vm);

902
error:
903 904

    return ret;
905
}
906

907 908 909 910
struct virReconnectDomainData {
    virConnectPtr conn;
    struct qemud_driver *driver;
};
911 912 913
/*
 * Open an existing VM's monitor, re-detect VCPU threads
 * and re-reserve the security labels in use
914
 */
915 916
static void
qemuReconnectDomain(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
917
{
918
    virDomainObjPtr obj = payload;
919 920
    struct virReconnectDomainData *data = opaque;
    struct qemud_driver *driver = data->driver;
921
    qemuDomainObjPrivatePtr priv;
922
    unsigned long long qemuCmdFlags;
923
    virConnectPtr conn = data->conn;
924 925

    virDomainObjLock(obj);
926

927 928
    VIR_DEBUG("Reconnect monitor to %p '%s'", obj, obj->def->name);

929 930
    priv = obj->privateData;

931 932 933 934
    /* Hold an extra reference because we can't allow 'vm' to be
     * deleted if qemuConnectMonitor() failed */
    virDomainObjRef(obj);

935
    /* XXX check PID liveliness & EXE path */
936
    if (qemuConnectMonitor(driver, obj) < 0)
937
        goto error;
938

939 940 941 942
    if (qemuUpdateActivePciHostdevs(driver, obj->def) < 0) {
        goto error;
    }

943 944 945
    /* XXX we should be persisting the original flags in the XML
     * not re-detecting them, since the binary may have changed
     * since launch time */
946 947 948
    if (qemuCapsExtractVersionInfo(obj->def->emulator,
                                   NULL,
                                   &qemuCmdFlags) >= 0 &&
949
        (qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE)) {
950 951
        priv->persistentAddrs = 1;

952 953 954 955
        if (!(priv->pciaddrs = qemuDomainPCIAddressSetCreate(obj->def)) ||
            qemuAssignDevicePCISlots(obj->def, priv->pciaddrs) < 0)
            goto error;
    }
956

957
    if (virSecurityManagerReserveLabel(driver->securityManager, obj) < 0)
958
        goto error;
959

960 961 962
    if (qemudVMFiltersInstantiate(conn, obj->def))
        goto error;

963 964
    if (obj->def->id >= driver->nextvmid)
        driver->nextvmid = obj->def->id + 1;
965

966 967
    if (virDomainObjUnref(obj) > 0)
        virDomainObjUnlock(obj);
968
    return;
969

970
error:
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
    if (!virDomainObjIsActive(obj)) {
        if (virDomainObjUnref(obj) > 0)
            virDomainObjUnlock(obj);
        return;
    }

    if (virDomainObjUnref(obj) > 0) {
        /* We can't get the monitor back, so must kill the VM
         * to remove danger of it ending up running twice if
         * user tries to start it again later */
        qemudShutdownVMDaemon(driver, obj, 0);
        if (!obj->persistent)
            virDomainRemoveInactive(&driver->domains, obj);
        else
            virDomainObjUnlock(obj);
    }
987
}
988

989
/**
990
 * qemudReconnectDomains
991 992 993 994 995
 *
 * Try to re-open the resources for live VMs that we care
 * about.
 */
static void
996
qemuReconnectDomains(virConnectPtr conn, struct qemud_driver *driver)
997
{
998 999
    struct virReconnectDomainData data = {conn, driver};
    virHashForEach(driver->domains.objs, qemuReconnectDomain, &data);
1000 1001
}

1002

1003
static int
1004
qemuSecurityInit(struct qemud_driver *driver)
1005
{
1006 1007 1008 1009
    virSecurityManagerPtr mgr = virSecurityManagerNew(driver->securityDriverName,
                                                      driver->allowDiskFormatProbing);
    if (!mgr)
        goto error;
1010

1011 1012 1013 1014 1015 1016 1017
    if (driver->privileged) {
        virSecurityManagerPtr dac = virSecurityManagerNewDAC(driver->user,
                                                             driver->group,
                                                             driver->allowDiskFormatProbing,
                                                             driver->dynamicOwnership);
        if (!dac)
            goto error;
1018

1019 1020 1021
        if (!(driver->securityManager = virSecurityManagerNewStack(mgr,
                                                                   dac)))
            goto error;
1022
    } else {
1023
        driver->securityManager = mgr;
1024 1025
    }

1026
    return 0;
1027 1028 1029 1030 1031

error:
    VIR_ERROR0(_("Failed to initialize security drivers"));
    virSecurityManagerFree(mgr);
    return -1;
1032
}
1033 1034


1035 1036
static virCapsPtr
qemuCreateCapabilities(virCapsPtr oldcaps,
1037
                       struct qemud_driver *driver)
1038 1039 1040 1041
{
    virCapsPtr caps;

    /* Basic host arch / guest machine capabilities */
1042
    if (!(caps = qemuCapsInit(oldcaps))) {
1043
        virReportOOMError();
1044 1045 1046
        return NULL;
    }

1047 1048 1049 1050 1051 1052 1053 1054
    if (driver->allowDiskFormatProbing) {
        caps->defaultDiskDriverName = NULL;
        caps->defaultDiskDriverType = NULL;
    } else {
        caps->defaultDiskDriverName = "qemu";
        caps->defaultDiskDriverType = "raw";
    }

1055 1056
    qemuDomainSetPrivateDataHooks(caps);
    qemuDomainSetNamespaceHooks(caps);
1057

1058 1059 1060 1061 1062
    if (virGetHostUUID(caps->host.host_uuid)) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot get the host uuid"));
        goto err_exit;
    }
1063 1064

    /* Security driver data */
1065
    const char *doi, *model;
1066

1067 1068 1069
    doi = virSecurityManagerGetDOI(driver->securityManager);
    model = virSecurityManagerGetModel(driver->securityManager);
    if (STRNEQ(model, "none")) {
1070 1071 1072 1073 1074 1075
        if (!(caps->host.secModel.model = strdup(model)))
            goto no_memory;
        if (!(caps->host.secModel.doi = strdup(doi)))
            goto no_memory;
    }

1076 1077 1078
    VIR_DEBUG("Initialized caps for security driver \"%s\" with "
              "DOI \"%s\"", model, doi);

1079 1080 1081
    return caps;

no_memory:
1082
    virReportOOMError();
1083
err_exit:
1084 1085 1086
    virCapabilitiesFree(caps);
    return NULL;
}
1087

C
Chris Lalancette 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
static void qemuDomainSnapshotLoad(void *payload,
                                   const char *name ATTRIBUTE_UNUSED,
                                   void *data)
{
    virDomainObjPtr vm = (virDomainObjPtr)payload;
    char *baseDir = (char *)data;
    char *snapDir = NULL;
    DIR *dir = NULL;
    struct dirent *entry;
    char *xmlStr;
    int ret;
    char *fullpath;
    virDomainSnapshotDefPtr def = NULL;
    char ebuf[1024];

    virDomainObjLock(vm);
    if (virAsprintf(&snapDir, "%s/%s", baseDir, vm->def->name) < 0) {
1105
        VIR_ERROR(_("Failed to allocate memory for snapshot directory for domain %s"),
C
Chris Lalancette 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114
                   vm->def->name);
        goto cleanup;
    }

    VIR_INFO("Scanning for snapshots for domain %s in %s", vm->def->name,
             snapDir);

    if (!(dir = opendir(snapDir))) {
        if (errno != ENOENT)
1115
            VIR_ERROR(_("Failed to open snapshot directory %s for domain %s: %s"),
C
Chris Lalancette 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
                      snapDir, vm->def->name,
                      virStrerror(errno, ebuf, sizeof(ebuf)));
        goto cleanup;
    }

    while ((entry = readdir(dir))) {
        if (entry->d_name[0] == '.')
            continue;

        /* NB: ignoring errors, so one malformed config doesn't
           kill the whole process */
        VIR_INFO("Loading snapshot file '%s'", entry->d_name);

        if (virAsprintf(&fullpath, "%s/%s", snapDir, entry->d_name) < 0) {
1130
            VIR_ERROR0(_("Failed to allocate memory for path"));
C
Chris Lalancette 已提交
1131 1132 1133 1134 1135 1136
            continue;
        }

        ret = virFileReadAll(fullpath, 1024*1024*1, &xmlStr);
        if (ret < 0) {
            /* Nothing we can do here, skip this one */
1137
            VIR_ERROR(_("Failed to read snapshot file %s: %s"), fullpath,
C
Chris Lalancette 已提交
1138
                      virStrerror(errno, ebuf, sizeof(ebuf)));
1139
            VIR_FREE(fullpath);
C
Chris Lalancette 已提交
1140 1141 1142 1143 1144 1145
            continue;
        }

        def = virDomainSnapshotDefParseString(xmlStr, 0);
        if (def == NULL) {
            /* Nothing we can do here, skip this one */
1146
            VIR_ERROR(_("Failed to parse snapshot XML from file '%s'"), fullpath);
1147
            VIR_FREE(fullpath);
C
Chris Lalancette 已提交
1148 1149 1150 1151
            VIR_FREE(xmlStr);
            continue;
        }

1152
        virDomainSnapshotAssignDef(&vm->snapshots, def);
C
Chris Lalancette 已提交
1153

1154
        VIR_FREE(fullpath);
C
Chris Lalancette 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
        VIR_FREE(xmlStr);
    }

    /* FIXME: qemu keeps internal track of snapshots.  We can get access
     * to this info via the "info snapshots" monitor command for running
     * domains, or via "qemu-img snapshot -l" for shutoff domains.  It would
     * be nice to update our internal state based on that, but there is a
     * a problem.  qemu doesn't track all of the same metadata that we do.
     * In particular we wouldn't be able to fill in the <parent>, which is
     * pretty important in our metadata.
     */

    virResetLastError();

cleanup:
    if (dir)
        closedir(dir);
    VIR_FREE(snapDir);
    virDomainObjUnlock(vm);
}

1176 1177 1178 1179 1180 1181
/**
 * qemudStartup:
 *
 * Initialization function for the QEmu daemon
 */
static int
1182
qemudStartup(int privileged) {
1183
    char *base = NULL;
1184
    char *driverConf = NULL;
1185
    int rc;
1186
    virConnectPtr conn = NULL;
1187

1188
    if (VIR_ALLOC(qemu_driver) < 0)
1189 1190
        return -1;

1191
    if (virMutexInit(&qemu_driver->lock) < 0) {
1192
        VIR_ERROR0(_("cannot initialize mutex"));
1193 1194 1195
        VIR_FREE(qemu_driver);
        return -1;
    }
1196
    qemuDriverLock(qemu_driver);
1197
    qemu_driver->privileged = privileged;
1198

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

1202 1203 1204
    if (virDomainObjListInit(&qemu_driver->domains) < 0)
        goto out_of_memory;

1205
    /* Init callback list */
1206
    if (VIR_ALLOC(qemu_driver->domainEventCallbacks) < 0)
1207
        goto out_of_memory;
1208 1209 1210 1211 1212 1213
    if (!(qemu_driver->domainEventQueue = virDomainEventQueueNew()))
        goto out_of_memory;

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

1215 1216 1217 1218 1219
    /* Allocate bitmap for vnc port reservation */
    if ((qemu_driver->reservedVNCPorts =
         virBitmapAlloc(QEMU_VNC_PORT_MAX - QEMU_VNC_PORT_MIN)) == NULL)
        goto out_of_memory;

1220 1221 1222 1223
    /* read the host sysinfo */
    if (privileged)
        qemu_driver->hostsysinfo = virSysinfoRead();

1224
    if (privileged) {
1225
        if (virAsprintf(&qemu_driver->logDir,
1226
                        "%s/log/libvirt/qemu", LOCALSTATEDIR) == -1)
1227
            goto out_of_memory;
1228

1229
        if ((base = strdup (SYSCONFDIR "/libvirt")) == NULL)
1230
            goto out_of_memory;
1231 1232

        if (virAsprintf(&qemu_driver->stateDir,
1233
                      "%s/run/libvirt/qemu", LOCALSTATEDIR) == -1)
1234
            goto out_of_memory;
1235 1236

        if (virAsprintf(&qemu_driver->libDir,
1237
                      "%s/lib/libvirt/qemu", LOCALSTATEDIR) == -1)
1238 1239 1240
            goto out_of_memory;

        if (virAsprintf(&qemu_driver->cacheDir,
1241
                      "%s/cache/libvirt/qemu", LOCALSTATEDIR) == -1)
1242
            goto out_of_memory;
1243
        if (virAsprintf(&qemu_driver->saveDir,
1244
                      "%s/lib/libvirt/qemu/save", LOCALSTATEDIR) == -1)
1245
            goto out_of_memory;
C
Chris Lalancette 已提交
1246
        if (virAsprintf(&qemu_driver->snapshotDir,
1247
                        "%s/lib/libvirt/qemu/snapshot", LOCALSTATEDIR) == -1)
C
Chris Lalancette 已提交
1248
            goto out_of_memory;
H
Hu Tao 已提交
1249 1250 1251
        if (virAsprintf(&qemu_driver->autoDumpPath,
                        "%s/lib/libvirt/qemu/dump", LOCALSTATEDIR) == -1)
            goto out_of_memory;
1252
    } else {
1253
        uid_t uid = geteuid();
1254
        char *userdir = virGetUserDirectory(uid);
1255
        if (!userdir)
1256
            goto error;
1257

1258
        if (virAsprintf(&qemu_driver->logDir,
1259 1260
                        "%s/.libvirt/qemu/log", userdir) == -1) {
            VIR_FREE(userdir);
1261
            goto out_of_memory;
1262
        }
1263

1264 1265
        if (virAsprintf(&base, "%s/.libvirt", userdir) == -1) {
            VIR_FREE(userdir);
1266
            goto out_of_memory;
1267 1268
        }
        VIR_FREE(userdir);
1269 1270 1271

        if (virAsprintf(&qemu_driver->stateDir, "%s/qemu/run", base) == -1)
            goto out_of_memory;
1272 1273 1274 1275
        if (virAsprintf(&qemu_driver->libDir, "%s/qemu/lib", base) == -1)
            goto out_of_memory;
        if (virAsprintf(&qemu_driver->cacheDir, "%s/qemu/cache", base) == -1)
            goto out_of_memory;
1276 1277
        if (virAsprintf(&qemu_driver->saveDir, "%s/qemu/save", base) == -1)
            goto out_of_memory;
C
Chris Lalancette 已提交
1278 1279
        if (virAsprintf(&qemu_driver->snapshotDir, "%s/qemu/snapshot", base) == -1)
            goto out_of_memory;
H
Hu Tao 已提交
1280 1281
        if (virAsprintf(&qemu_driver->autoDumpPath, "%s/qemu/dump", base) == -1)
            goto out_of_memory;
1282 1283
    }

L
Laine Stump 已提交
1284
    if (virFileMakePath(qemu_driver->stateDir) != 0) {
1285
        char ebuf[1024];
1286
        VIR_ERROR(_("Failed to create state dir '%s': %s"),
1287
                  qemu_driver->stateDir, virStrerror(errno, ebuf, sizeof ebuf));
1288
        goto error;
1289
    }
L
Laine Stump 已提交
1290
    if (virFileMakePath(qemu_driver->libDir) != 0) {
1291
        char ebuf[1024];
1292
        VIR_ERROR(_("Failed to create lib dir '%s': %s"),
1293 1294 1295
                  qemu_driver->libDir, virStrerror(errno, ebuf, sizeof ebuf));
        goto error;
    }
L
Laine Stump 已提交
1296
    if (virFileMakePath(qemu_driver->cacheDir) != 0) {
1297
        char ebuf[1024];
1298
        VIR_ERROR(_("Failed to create cache dir '%s': %s"),
1299 1300 1301
                  qemu_driver->cacheDir, virStrerror(errno, ebuf, sizeof ebuf));
        goto error;
    }
1302 1303 1304 1305 1306 1307
    if (virFileMakePath(qemu_driver->saveDir) != 0) {
        char ebuf[1024];
        VIR_ERROR(_("Failed to create save dir '%s': %s"),
                  qemu_driver->saveDir, virStrerror(errno, ebuf, sizeof ebuf));
        goto error;
    }
C
Chris Lalancette 已提交
1308 1309 1310 1311 1312 1313
    if (virFileMakePath(qemu_driver->snapshotDir) != 0) {
        char ebuf[1024];
        VIR_ERROR(_("Failed to create save dir '%s': %s"),
                  qemu_driver->snapshotDir, virStrerror(errno, ebuf, sizeof ebuf));
        goto error;
    }
H
Hu Tao 已提交
1314 1315 1316 1317 1318 1319
    if (virFileMakePath(qemu_driver->autoDumpPath) != 0) {
        char ebuf[1024];
        VIR_ERROR(_("Failed to create dump dir '%s': %s"),
                  qemu_driver->autoDumpPath, virStrerror(errno, ebuf, sizeof ebuf));
        goto error;
    }
1320 1321 1322 1323

    /* Configuration paths are either ~/.libvirt/qemu/... (session) or
     * /etc/libvirt/qemu/... (system).
     */
1324 1325 1326
    if (virAsprintf(&driverConf, "%s/qemu.conf", base) < 0 ||
        virAsprintf(&qemu_driver->configDir, "%s/qemu", base) < 0 ||
        virAsprintf(&qemu_driver->autostartDir, "%s/qemu/autostart", base) < 0)
1327 1328
        goto out_of_memory;

1329
    VIR_FREE(base);
1330

1331 1332 1333 1334 1335 1336 1337
    rc = virCgroupForDriver("qemu", &qemu_driver->cgroup, privileged, 1);
    if (rc < 0) {
        char buf[1024];
        VIR_WARN("Unable to create cgroup for driver: %s",
                 virStrerror(-rc, buf, sizeof(buf)));
    }

1338 1339 1340
    if (qemudLoadDriverConfig(qemu_driver, driverConf) < 0) {
        goto error;
    }
1341
    VIR_FREE(driverConf);
1342

1343
    if (qemuSecurityInit(qemu_driver) < 0)
1344
        goto error;
D
Daniel P. Berrange 已提交
1345

1346
    if ((qemu_driver->caps = qemuCreateCapabilities(NULL,
1347
                                                    qemu_driver)) == NULL)
1348
        goto error;
1349

1350
    if ((qemu_driver->activePciHostdevs = pciDeviceListNew()) == NULL)
1351 1352
        goto error;

1353 1354
    if (privileged) {
        if (chown(qemu_driver->libDir, qemu_driver->user, qemu_driver->group) < 0) {
1355
            virReportSystemError(errno,
1356 1357 1358 1359 1360
                                 _("unable to set ownership of '%s' to user %d:%d"),
                                 qemu_driver->libDir, qemu_driver->user, qemu_driver->group);
            goto error;
        }
        if (chown(qemu_driver->cacheDir, qemu_driver->user, qemu_driver->group) < 0) {
1361
            virReportSystemError(errno,
1362 1363 1364 1365
                                 _("unable to set ownership of '%s' to %d:%d"),
                                 qemu_driver->cacheDir, qemu_driver->user, qemu_driver->group);
            goto error;
        }
1366 1367 1368 1369 1370 1371
        if (chown(qemu_driver->saveDir, qemu_driver->user, qemu_driver->group) < 0) {
            virReportSystemError(errno,
                                 _("unable to set ownership of '%s' to %d:%d"),
                                 qemu_driver->saveDir, qemu_driver->user, qemu_driver->group);
            goto error;
        }
C
Chris Lalancette 已提交
1372 1373 1374 1375 1376 1377
        if (chown(qemu_driver->snapshotDir, qemu_driver->user, qemu_driver->group) < 0) {
            virReportSystemError(errno,
                                 _("unable to set ownership of '%s' to %d:%d"),
                                 qemu_driver->snapshotDir, qemu_driver->user, qemu_driver->group);
            goto error;
        }
1378 1379
    }

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    /* If hugetlbfs is present, then we need to create a sub-directory within
     * it, since we can't assume the root mount point has permissions that
     * will let our spawned QEMU instances use it.
     *
     * NB the check for '/', since user may config "" to disable hugepages
     * even when mounted
     */
    if (qemu_driver->hugetlbfs_mount &&
        qemu_driver->hugetlbfs_mount[0] == '/') {
        char *mempath = NULL;
        if (virAsprintf(&mempath, "%s/libvirt/qemu", qemu_driver->hugetlbfs_mount) < 0)
            goto out_of_memory;

        if ((rc = virFileMakePath(mempath)) != 0) {
1394
            virReportSystemError(rc,
1395 1396 1397 1398 1399 1400
                                 _("unable to create hugepage path %s"), mempath);
            VIR_FREE(mempath);
            goto error;
        }
        if (qemu_driver->privileged &&
            chown(mempath, qemu_driver->user, qemu_driver->group) < 0) {
1401
            virReportSystemError(errno,
1402 1403 1404 1405 1406 1407 1408 1409 1410
                                 _("unable to set ownership on %s to %d:%d"),
                                 mempath, qemu_driver->user, qemu_driver->group);
            VIR_FREE(mempath);
            goto error;
        }

        qemu_driver->hugepage_path = mempath;
    }

1411
    /* Get all the running persistent or transient configs first */
1412
    if (virDomainLoadAllConfigs(qemu_driver->caps,
1413 1414 1415 1416 1417 1418
                                &qemu_driver->domains,
                                qemu_driver->stateDir,
                                NULL,
                                1, NULL, NULL) < 0)
        goto error;

1419 1420 1421 1422 1423
    conn = virConnectOpen(qemu_driver->privileged ?
                          "qemu:///system" :
                          "qemu:///session");

    qemuReconnectDomains(conn, qemu_driver);
1424 1425

    /* Then inactive persistent configs */
1426
    if (virDomainLoadAllConfigs(qemu_driver->caps,
1427 1428
                                &qemu_driver->domains,
                                qemu_driver->configDir,
1429
                                qemu_driver->autostartDir,
1430
                                0, NULL, NULL) < 0)
1431
        goto error;
C
Chris Lalancette 已提交
1432 1433 1434 1435 1436


    virHashForEach(qemu_driver->domains.objs, qemuDomainSnapshotLoad,
                   qemu_driver->snapshotDir);

1437 1438
    qemuDriverUnlock(qemu_driver);

1439 1440
    qemudAutostartConfigs(qemu_driver);

H
Hu Tao 已提交
1441 1442 1443 1444
    qemu_driver->workerPool = virThreadPoolNew(0, 1, processWatchdogEvent, qemu_driver);
    if (!qemu_driver->workerPool)
        goto error;

1445 1446
    if (conn)
        virConnectClose(conn);
1447

1448 1449
    return 0;

1450
out_of_memory:
1451
    virReportOOMError();
1452 1453 1454
error:
    if (qemu_driver)
        qemuDriverUnlock(qemu_driver);
1455 1456
    if (conn)
        virConnectClose(conn);
1457
    VIR_FREE(base);
1458
    VIR_FREE(driverConf);
1459
    qemudShutdown();
1460 1461 1462
    return -1;
}

1463 1464 1465 1466
static void qemudNotifyLoadDomain(virDomainObjPtr vm, int newVM, void *opaque)
{
    struct qemud_driver *driver = opaque;

1467 1468 1469 1470 1471 1472 1473 1474
    if (newVM) {
        virDomainEventPtr event =
            virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_DEFINED,
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED);
        if (event)
            qemuDomainEventQueue(driver, event);
    }
1475 1476
}

1477 1478 1479 1480 1481 1482 1483 1484
/**
 * qemudReload:
 *
 * Function to restart the QEmu daemon, it will recheck the configuration
 * files and update its state and the networking
 */
static int
qemudReload(void) {
1485 1486 1487
    if (!qemu_driver)
        return 0;

1488
    qemuDriverLock(qemu_driver);
1489
    virDomainLoadAllConfigs(qemu_driver->caps,
1490 1491
                            &qemu_driver->domains,
                            qemu_driver->configDir,
1492
                            qemu_driver->autostartDir,
1493
                            0, qemudNotifyLoadDomain, qemu_driver);
1494
    qemuDriverUnlock(qemu_driver);
1495

1496
    qemudAutostartConfigs(qemu_driver);
1497 1498

    return 0;
1499 1500
}

1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
/**
 * 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) {
1511
    int active = 0;
1512

1513 1514 1515
    if (!qemu_driver)
        return 0;

1516
    /* XXX having to iterate here is not great because it requires many locks */
1517
    qemuDriverLock(qemu_driver);
1518
    active = virDomainObjListNumOfDomains(&qemu_driver->domains, 1);
1519 1520
    qemuDriverUnlock(qemu_driver);
    return active;
1521 1522
}

1523 1524 1525 1526 1527 1528 1529
/**
 * qemudShutdown:
 *
 * Shutdown the QEmu daemon, it will stop all active domains and networks
 */
static int
qemudShutdown(void) {
1530
    int i;
1531

1532
    if (!qemu_driver)
1533
        return -1;
1534

1535
    qemuDriverLock(qemu_driver);
1536
    pciDeviceListFree(qemu_driver->activePciHostdevs);
1537 1538
    virCapabilitiesFree(qemu_driver->caps);

1539
    virDomainObjListDeinit(&qemu_driver->domains);
1540
    virBitmapFree(qemu_driver->reservedVNCPorts);
1541

1542 1543
    virSysinfoDefFree(qemu_driver->hostsysinfo);

1544 1545
    VIR_FREE(qemu_driver->configDir);
    VIR_FREE(qemu_driver->autostartDir);
E
Eric Blake 已提交
1546
    VIR_FREE(qemu_driver->logDir);
1547
    VIR_FREE(qemu_driver->stateDir);
1548 1549
    VIR_FREE(qemu_driver->libDir);
    VIR_FREE(qemu_driver->cacheDir);
1550
    VIR_FREE(qemu_driver->saveDir);
C
Chris Lalancette 已提交
1551
    VIR_FREE(qemu_driver->snapshotDir);
H
Hu Tao 已提交
1552
    VIR_FREE(qemu_driver->autoDumpPath);
1553
    VIR_FREE(qemu_driver->vncTLSx509certdir);
J
Jim Meyering 已提交
1554
    VIR_FREE(qemu_driver->vncListen);
1555
    VIR_FREE(qemu_driver->vncPassword);
1556
    VIR_FREE(qemu_driver->vncSASLdir);
E
Eric Blake 已提交
1557 1558 1559
    VIR_FREE(qemu_driver->spiceTLSx509certdir);
    VIR_FREE(qemu_driver->spiceListen);
    VIR_FREE(qemu_driver->spicePassword);
1560 1561
    VIR_FREE(qemu_driver->hugetlbfs_mount);
    VIR_FREE(qemu_driver->hugepage_path);
E
Eric Blake 已提交
1562 1563 1564
    VIR_FREE(qemu_driver->saveImageFormat);
    VIR_FREE(qemu_driver->dumpImageFormat);

1565 1566
    virSecurityManagerFree(qemu_driver->securityManager);

E
Eric Blake 已提交
1567
    ebtablesContextFree(qemu_driver->ebtables);
D
Daniel P. Berrange 已提交
1568

1569 1570 1571 1572 1573 1574
    if (qemu_driver->cgroupDeviceACL) {
        for (i = 0 ; qemu_driver->cgroupDeviceACL[i] != NULL ; i++)
            VIR_FREE(qemu_driver->cgroupDeviceACL[i]);
        VIR_FREE(qemu_driver->cgroupDeviceACL);
    }

1575 1576
    /* Free domain callback list */
    virDomainEventCallbackListFree(qemu_driver->domainEventCallbacks);
1577 1578 1579 1580
    virDomainEventQueueFree(qemu_driver->domainEventQueue);

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

1582 1583 1584
    if (qemu_driver->brctl)
        brShutdown(qemu_driver->brctl);

1585 1586
    virCgroupFree(&qemu_driver->cgroup);

1587
    qemuDriverUnlock(qemu_driver);
1588
    virMutexDestroy(&qemu_driver->lock);
H
Hu Tao 已提交
1589
    virThreadPoolFree(qemu_driver->workerPool);
1590
    VIR_FREE(qemu_driver);
1591 1592

    return 0;
1593 1594
}

1595
typedef int qemuLogHandleOutput(virDomainObjPtr vm,
1596 1597
                                const char *output,
                                int fd);
1598 1599 1600 1601 1602

/*
 * Returns -1 for error, 0 on success
 */
static int
1603
qemudReadLogOutput(virDomainObjPtr vm,
1604 1605
                   int fd,
                   char *buf,
G
Guido Günther 已提交
1606
                   size_t buflen,
1607
                   qemuLogHandleOutput func,
1608 1609 1610
                   const char *what,
                   int timeout)
{
1611
    int retries = (timeout*10);
1612
    int got = 0;
1613 1614 1615
    buf[0] = '\0';

    while (retries) {
1616
        ssize_t func_ret, ret;
1617
        int isdead = 0;
G
Guido Günther 已提交
1618

1619
        func_ret = func(vm, buf, fd);
1620

1621 1622
        if (kill(vm->pid, 0) == -1 && errno == ESRCH)
            isdead = 1;
1623

1624 1625
        /* Any failures should be detected before we read the log, so we
         * always have something useful to report on failure. */
1626 1627
        ret = saferead(fd, buf+got, buflen-got-1);
        if (ret < 0) {
1628
            virReportSystemError(errno,
1629 1630 1631 1632 1633
                                 _("Failure while reading %s log output"),
                                 what);
            return -1;
        }

1634 1635 1636
        got += ret;
        buf[got] = '\0';
        if (got == buflen-1) {
1637
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
1638 1639
                            _("Out of space while reading %s log output: %s"),
                            what, buf);
1640 1641 1642 1643
            return -1;
        }

        if (isdead) {
1644
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
1645 1646
                            _("Process exited while reading %s log output: %s"),
                            what, buf);
1647 1648 1649
            return -1;
        }

1650 1651
        if (func_ret <= 0)
            return func_ret;
1652 1653 1654 1655

        usleep(100*1000);
        retries--;
    }
1656

1657
    qemuReportError(VIR_ERR_INTERNAL_ERROR,
1658 1659
                    _("Timed out while reading %s log output: %s"),
                    what, buf);
1660 1661 1662
    return -1;
}

1663

1664 1665 1666 1667 1668 1669 1670 1671
/*
 * Look at a chunk of data from the QEMU stdout logs and try to
 * find a TTY device, as indicated by a line like
 *
 * char device redirected to /dev/pts/3
 *
 * Returns -1 for error, 0 success, 1 continue reading
 */
1672
static int
1673
qemudExtractTTYPath(const char *haystack,
1674 1675
                    size_t *offset,
                    char **path)
1676
{
1677
    static const char needle[] = "char device redirected to";
1678
    char *tmp, *dev;
1679

1680
    VIR_FREE(*path);
1681
    /* First look for our magic string */
1682 1683 1684 1685 1686
    if (!(tmp = strstr(haystack + *offset, needle))) {
        return 1;
    }
    tmp += sizeof(needle);
    dev = tmp;
1687

1688 1689 1690 1691 1692
    /*
     * And look for first whitespace character and nul terminate
     * to mark end of the pty path
     */
    while (*tmp) {
1693
        if (c_isspace(*tmp)) {
1694 1695
            *path = strndup(dev, tmp-dev);
            if (*path == NULL) {
1696
                virReportOOMError();
1697 1698
                return -1;
            }
1699

1700
            /* ... now further update offset till we get EOL */
1701
            *offset = tmp - haystack;
1702 1703
            return 0;
        }
1704
        tmp++;
1705 1706 1707 1708 1709
    }

    /*
     * We found a path, but didn't find any whitespace,
     * so it must be still incomplete - we should at
1710 1711
     * least see a \n - indicate that we want to carry
     * on trying again
1712
     */
1713
    return 1;
1714 1715
}

1716
static int
1717
qemudFindCharDevicePTYsMonitor(virDomainObjPtr vm,
1718 1719 1720 1721 1722 1723 1724
                               virHashTablePtr paths)
{
    int i;

#define LOOKUP_PTYS(array, arraylen, idprefix)                            \
    for (i = 0 ; i < (arraylen) ; i++) {                                  \
        virDomainChrDefPtr chr = (array)[i];                              \
1725
        if (chr->source.type == VIR_DOMAIN_CHR_TYPE_PTY) {                \
1726 1727 1728 1729 1730 1731 1732
            char id[16];                                                  \
                                                                          \
            if (snprintf(id, sizeof(id), idprefix "%i", i) >= sizeof(id)) \
                return -1;                                                \
                                                                          \
            const char *path = (const char *) virHashLookup(paths, id);   \
            if (path == NULL) {                                           \
1733
                if (chr->source.data.file.path == NULL) {                 \
1734 1735
                    /* neither the log output nor 'info chardev' had a */ \
                    /* pty path for this chardev, report an error */      \
1736 1737 1738
                    qemuReportError(VIR_ERR_INTERNAL_ERROR,               \
                                    _("no assigned pty for device %s"), id); \
                    return -1;                                            \
1739 1740 1741 1742 1743
                } else {                                                  \
                    /* 'info chardev' had no pty path for this chardev, */\
                    /* but the log output had, so we're fine */           \
                    continue;                                             \
                }                                                         \
1744 1745
            }                                                             \
                                                                          \
1746 1747
            VIR_FREE(chr->source.data.file.path);                         \
            chr->source.data.file.path = strdup(path);                    \
1748
                                                                          \
1749
            if (chr->source.data.file.path == NULL) {                     \
1750
                virReportOOMError();                                      \
1751 1752
                return -1;                                                \
            }                                                             \
1753 1754 1755 1756 1757 1758
        }                                                                 \
    }

    LOOKUP_PTYS(vm->def->serials,   vm->def->nserials,   "serial");
    LOOKUP_PTYS(vm->def->parallels, vm->def->nparallels, "parallel");
    LOOKUP_PTYS(vm->def->channels,  vm->def->nchannels,  "channel");
C
Cole Robinson 已提交
1759 1760
    if (vm->def->console)
        LOOKUP_PTYS(&vm->def->console, 1,  "console");
1761
#undef LOOKUP_PTYS
1762 1763 1764 1765

    return 0;
}

1766
static int
1767
qemudFindCharDevicePTYs(virDomainObjPtr vm,
1768 1769
                        const char *output,
                        int fd ATTRIBUTE_UNUSED)
1770
{
1771
    size_t offset = 0;
1772
    int ret, i;
1773 1774

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

1778
    /* first comes the serial devices */
1779 1780
    for (i = 0 ; i < vm->def->nserials ; i++) {
        virDomainChrDefPtr chr = vm->def->serials[i];
1781
        if (chr->source.type == VIR_DOMAIN_CHR_TYPE_PTY) {
1782
            if ((ret = qemudExtractTTYPath(output, &offset,
1783
                                           &chr->source.data.file.path)) != 0)
1784
                return ret;
1785 1786 1787
        }
    }

1788
    /* then the parallel devices */
1789 1790
    for (i = 0 ; i < vm->def->nparallels ; i++) {
        virDomainChrDefPtr chr = vm->def->parallels[i];
1791
        if (chr->source.type == VIR_DOMAIN_CHR_TYPE_PTY) {
1792
            if ((ret = qemudExtractTTYPath(output, &offset,
1793
                                           &chr->source.data.file.path)) != 0)
1794
                return ret;
1795 1796 1797
        }
    }

1798 1799 1800
    /* then the channel devices */
    for (i = 0 ; i < vm->def->nchannels ; i++) {
        virDomainChrDefPtr chr = vm->def->channels[i];
1801
        if (chr->source.type == VIR_DOMAIN_CHR_TYPE_PTY) {
1802
            if ((ret = qemudExtractTTYPath(output, &offset,
1803
                                           &chr->source.data.file.path)) != 0)
1804 1805 1806 1807
                return ret;
        }
    }

1808
    return 0;
1809 1810
}

1811 1812 1813 1814 1815
static void qemudFreePtyPath(void *payload, const char *name ATTRIBUTE_UNUSED)
{
    VIR_FREE(payload);
}

1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
static void
qemuReadLogFD(int logfd, char *buf, int maxlen, int off)
{
    int ret;
    char *tmpbuf = buf + off;

    ret = saferead(logfd, tmpbuf, maxlen - off - 1);
    if (ret < 0) {
        ret = 0;
    }

    tmpbuf[ret] = '\0';
}

1830
static int
1831
qemudWaitForMonitor(struct qemud_driver* driver,
1832
                    virDomainObjPtr vm, off_t pos)
1833
{
1834
    char buf[4096] = ""; /* Plenty of space to get startup greeting */
1835
    int logfd;
1836
    int ret = -1;
1837
    virHashTablePtr paths = NULL;
1838

1839
    if ((logfd = qemudLogReadFD(driver->logDir, vm->def->name, pos)) < 0)
1840
        return -1;
1841

1842 1843 1844 1845
    if (qemudReadLogOutput(vm, logfd, buf, sizeof(buf),
                           qemudFindCharDevicePTYs,
                           "console", 30) < 0)
        goto closelog;
1846

1847
    VIR_DEBUG("Connect monitor to %p '%s'", vm, vm->def->name);
1848 1849 1850
    if (qemuConnectMonitor(driver, vm) < 0) {
        goto cleanup;
    }
1851

1852 1853 1854 1855
    /* Try to get the pty path mappings again via the monitor. This is much more
     * reliable if it's available.
     * Note that the monitor itself can be on a pty, so we still need to try the
     * log output method. */
1856
    paths = virHashCreate(0);
1857
    if (paths == NULL) {
1858
        virReportOOMError();
1859 1860 1861
        goto cleanup;
    }

1862
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
1863 1864
    qemuDomainObjPrivatePtr priv = vm->privateData;
    ret = qemuMonitorGetPtyPaths(priv->mon, paths);
1865
    qemuDomainObjExitMonitorWithDriver(driver, vm);
1866 1867 1868

    VIR_DEBUG("qemuMonitorGetPtyPaths returned %i", ret);
    if (ret == 0) {
1869
        ret = qemudFindCharDevicePTYsMonitor(vm, paths);
1870 1871 1872 1873 1874 1875 1876
    }

cleanup:
    if (paths) {
        virHashFree(paths, qemudFreePtyPath);
    }

1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
    if (kill(vm->pid, 0) == -1 && errno == ESRCH) {
        /* VM is dead, any other error raised in the interim is probably
         * not as important as the qemu cmdline output */
        qemuReadLogFD(logfd, buf, sizeof(buf), strlen(buf));
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("process exited while connecting to monitor: %s"),
                        buf);
        ret = -1;
    }

closelog:
1888
    if (VIR_CLOSE(logfd) < 0) {
1889
        char ebuf[4096];
1890
        VIR_WARN("Unable to close logfile: %s",
1891 1892 1893
                 virStrerror(errno, ebuf, sizeof ebuf));
    }

1894
    return ret;
1895 1896
}

1897
static int
1898
qemuDetectVcpuPIDs(struct qemud_driver *driver,
1899 1900 1901
                   virDomainObjPtr vm) {
    pid_t *cpupids = NULL;
    int ncpupids;
1902
    qemuDomainObjPrivatePtr priv = vm->privateData;
1903

1904
    if (vm->def->virtType != VIR_DOMAIN_VIRT_KVM) {
1905 1906
        priv->nvcpupids = 1;
        if (VIR_ALLOC_N(priv->vcpupids, priv->nvcpupids) < 0) {
1907
            virReportOOMError();
1908 1909
            return -1;
        }
1910
        priv->vcpupids[0] = vm->pid;
1911 1912 1913
        return 0;
    }

1914
    /* What follows is now all KVM specific */
1915

1916
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
1917
    if ((ncpupids = qemuMonitorGetCPUInfo(priv->mon, &cpupids)) < 0) {
1918
        qemuDomainObjExitMonitorWithDriver(driver, vm);
1919
        return -1;
1920
    }
1921
    qemuDomainObjExitMonitorWithDriver(driver, vm);
1922

1923 1924 1925
    /* Treat failure to get VCPU<->PID mapping as non-fatal */
    if (ncpupids == 0)
        return 0;
1926

1927
    if (ncpupids != vm->def->vcpus) {
1928
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
1929 1930 1931
                        _("got wrong number of vCPU pids from QEMU monitor. "
                          "got %d, wanted %d"),
                        ncpupids, vm->def->vcpus);
1932 1933 1934
        VIR_FREE(cpupids);
        return -1;
    }
1935

1936 1937
    priv->nvcpupids = ncpupids;
    priv->vcpupids = cpupids;
1938 1939 1940
    return 0;
}

1941 1942 1943
/*
 * To be run between fork/exec of QEMU only
 */
1944
static int
1945 1946
qemudInitCpuAffinity(virDomainObjPtr vm)
{
1947
    int i, hostcpus, maxcpu = QEMUD_CPUMASK_LEN;
1948
    virNodeInfo nodeinfo;
1949 1950
    unsigned char *cpumap;
    int cpumaplen;
1951 1952

    DEBUG0("Setting CPU affinity");
1953

1954
    if (nodeGetInfo(NULL, &nodeinfo) < 0)
1955 1956 1957 1958
        return -1;

    /* setaffinity fails if you set bits for CPUs which
     * aren't present, so we have to limit ourselves */
1959 1960 1961
    hostcpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;
1962

1963 1964
    cpumaplen = VIR_CPU_MAPLEN(maxcpu);
    if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) {
1965
        virReportOOMError();
1966 1967 1968
        return -1;
    }

D
Daniel P. Berrange 已提交
1969
    if (vm->def->cpumask) {
1970 1971 1972
        /* XXX why don't we keep 'cpumask' in the libvirt cpumap
         * format to start with ?!?! */
        for (i = 0 ; i < maxcpu && i < vm->def->cpumasklen ; i++)
D
Daniel P. Berrange 已提交
1973
            if (vm->def->cpumask[i])
1974
                VIR_USE_CPU(cpumap, i);
D
Daniel P. Berrange 已提交
1975
    } else {
1976 1977 1978 1979
        /* You may think this is redundant, but we can't assume libvirtd
         * itself is running on all pCPUs, so we need to explicitly set
         * the spawned QEMU instance to all pCPUs if no map is given in
         * its config file */
D
Daniel P. Berrange 已提交
1980
        for (i = 0 ; i < maxcpu ; i++)
1981
            VIR_USE_CPU(cpumap, i);
D
Daniel P. Berrange 已提交
1982
    }
1983

1984 1985 1986 1987 1988 1989 1990 1991
    /* We are pressuming we are running between fork/exec of QEMU
     * so use '0' to indicate our own process ID. No threads are
     * running at this point
     */
    if (virProcessInfoSetAffinity(0, /* Self */
                                  cpumap, cpumaplen, maxcpu) < 0) {
        VIR_FREE(cpumap);
        return -1;
1992
    }
1993
    VIR_FREE(cpumap);
1994 1995 1996 1997 1998

    return 0;
}


1999
static int
2000 2001 2002 2003
qemuInitPasswords(virConnectPtr conn,
                  struct qemud_driver *driver,
                  virDomainObjPtr vm,
                  unsigned long long qemuCmdFlags) {
2004
    int ret = 0;
2005
    qemuDomainObjPrivatePtr priv = vm->privateData;
2006

2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
    if (vm->def->ngraphics == 1) {
        if (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
            ret = qemuDomainChangeGraphicsPasswords(driver, vm,
                                                    VIR_DOMAIN_GRAPHICS_TYPE_VNC,
                                                    &vm->def->graphics[0]->data.vnc.auth,
                                                    driver->vncPassword);
        } else if (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SPICE) {
            ret = qemuDomainChangeGraphicsPasswords(driver, vm,
                                                    VIR_DOMAIN_GRAPHICS_TYPE_SPICE,
                                                    &vm->def->graphics[0]->data.spice.auth,
                                                    driver->spicePassword);
        }
2019 2020
    }

2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
    if (ret < 0)
        goto cleanup;

    if (qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE) {
        int i;

        for (i = 0 ; i < vm->def->ndisks ; i++) {
            char *secret;
            size_t secretLen;

            if (!vm->def->disks[i]->encryption ||
                !vm->def->disks[i]->src)
                continue;

            if (getVolumeQcowPassphrase(conn,
                                        vm->def->disks[i],
                                        &secret, &secretLen) < 0)
                goto cleanup;

            qemuDomainObjEnterMonitorWithDriver(driver, vm);
            ret = qemuMonitorSetDrivePassphrase(priv->mon,
                                                vm->def->disks[i]->info.alias,
                                                secret);
2044
            VIR_FREE(secret);
2045 2046 2047 2048 2049 2050 2051
            qemuDomainObjExitMonitorWithDriver(driver, vm);
            if (ret < 0)
                goto cleanup;
        }
    }

cleanup:
2052
    return ret;
2053 2054 2055
}


2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
#define QEMU_PCI_VENDOR_INTEL     0x8086
#define QEMU_PCI_VENDOR_LSI_LOGIC 0x1000
#define QEMU_PCI_VENDOR_REDHAT    0x1af4
#define QEMU_PCI_VENDOR_CIRRUS    0x1013
#define QEMU_PCI_VENDOR_REALTEK   0x10ec
#define QEMU_PCI_VENDOR_AMD       0x1022
#define QEMU_PCI_VENDOR_ENSONIQ   0x1274
#define QEMU_PCI_VENDOR_VMWARE    0x15ad
#define QEMU_PCI_VENDOR_QEMU      0x1234

#define QEMU_PCI_PRODUCT_DISK_VIRTIO 0x1001

2068 2069
#define QEMU_PCI_PRODUCT_BALLOON_VIRTIO 0x1002

2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
#define QEMU_PCI_PRODUCT_NIC_NE2K     0x8029
#define QEMU_PCI_PRODUCT_NIC_PCNET    0x2000
#define QEMU_PCI_PRODUCT_NIC_RTL8139  0x8139
#define QEMU_PCI_PRODUCT_NIC_E1000    0x100E
#define QEMU_PCI_PRODUCT_NIC_VIRTIO   0x1000

#define QEMU_PCI_PRODUCT_VGA_CIRRUS 0x00b8
#define QEMU_PCI_PRODUCT_VGA_VMWARE 0x0405
#define QEMU_PCI_PRODUCT_VGA_STDVGA 0x1111

#define QEMU_PCI_PRODUCT_AUDIO_AC97    0x2415
#define QEMU_PCI_PRODUCT_AUDIO_ES1370  0x5000

#define QEMU_PCI_PRODUCT_CONTROLLER_PIIX 0x7010
#define QEMU_PCI_PRODUCT_CONTROLLER_LSI  0x0012

#define QEMU_PCI_PRODUCT_WATCHDOG_I63000ESB 0x25ab

static int
qemuAssignNextPCIAddress(virDomainDeviceInfo *info,
                         int vendor,
                         int product,
                         qemuMonitorPCIAddress *addrs,
                         int naddrs)
{
    int found = 0;
    int i;

    VIR_DEBUG("Look for %x:%x out of %d", vendor, product, naddrs);

    for (i = 0 ; (i < naddrs) && !found; i++) {
        VIR_DEBUG("Maybe %x:%x", addrs[i].vendor, addrs[i].product);
        if (addrs[i].vendor == vendor &&
            addrs[i].product == product) {
            VIR_DEBUG("Match %d", i);
            found = 1;
            break;
        }
    }
    if (!found) {
        return -1;
    }

    /* Blank it out so this device isn't matched again */
    addrs[i].vendor = 0;
    addrs[i].product = 0;

    if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
        info->type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;

    if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
        info->addr.pci.domain = addrs[i].addr.domain;
        info->addr.pci.bus = addrs[i].addr.bus;
        info->addr.pci.slot = addrs[i].addr.slot;
        info->addr.pci.function = addrs[i].addr.function;
    }

    return 0;
}

static int
qemuGetPCIDiskVendorProduct(virDomainDiskDefPtr def,
                            unsigned *vendor,
                            unsigned *product)
{
    switch (def->bus) {
    case VIR_DOMAIN_DISK_BUS_VIRTIO:
        *vendor = QEMU_PCI_VENDOR_REDHAT;
        *product = QEMU_PCI_PRODUCT_DISK_VIRTIO;
        break;

    default:
        return -1;
    }

    return 0;
}

static int
qemuGetPCINetVendorProduct(virDomainNetDefPtr def,
                            unsigned *vendor,
                            unsigned *product)
{
    if (!def->model)
        return -1;

    if (STREQ(def->model, "ne2k_pci")) {
        *vendor = QEMU_PCI_VENDOR_REALTEK;
        *product = QEMU_PCI_PRODUCT_NIC_NE2K;
    } else if (STREQ(def->model, "pcnet")) {
        *vendor = QEMU_PCI_VENDOR_AMD;
        *product = QEMU_PCI_PRODUCT_NIC_PCNET;
    } else if (STREQ(def->model, "rtl8139")) {
        *vendor = QEMU_PCI_VENDOR_REALTEK;
        *product = QEMU_PCI_PRODUCT_NIC_RTL8139;
    } else if (STREQ(def->model, "e1000")) {
        *vendor = QEMU_PCI_VENDOR_INTEL;
        *product = QEMU_PCI_PRODUCT_NIC_E1000;
    } else if (STREQ(def->model, "virtio")) {
        *vendor = QEMU_PCI_VENDOR_REDHAT;
        *product = QEMU_PCI_PRODUCT_NIC_VIRTIO;
    } else {
        VIR_INFO("Unexpected NIC model %s, cannot get PCI address",
                 def->model);
        return -1;
    }
    return 0;
}

static int
qemuGetPCIControllerVendorProduct(virDomainControllerDefPtr def,
                                  unsigned *vendor,
                                  unsigned *product)
{
    switch (def->type) {
    case VIR_DOMAIN_CONTROLLER_TYPE_SCSI:
        *vendor = QEMU_PCI_VENDOR_LSI_LOGIC;
        *product = QEMU_PCI_PRODUCT_CONTROLLER_LSI;
        break;

    case VIR_DOMAIN_CONTROLLER_TYPE_FDC:
        /* XXX we could put in the ISA bridge address, but
           that's not technically the FDC's address */
        return -1;

    case VIR_DOMAIN_CONTROLLER_TYPE_IDE:
        *vendor = QEMU_PCI_VENDOR_INTEL;
        *product = QEMU_PCI_PRODUCT_CONTROLLER_PIIX;
        break;

    default:
        VIR_INFO("Unexpected controller type %s, cannot get PCI address",
                 virDomainControllerTypeToString(def->type));
        return -1;
    }

    return 0;
}

static int
qemuGetPCIVideoVendorProduct(virDomainVideoDefPtr def,
                             unsigned *vendor,
                             unsigned *product)
{
    switch (def->type) {
    case VIR_DOMAIN_VIDEO_TYPE_CIRRUS:
        *vendor = QEMU_PCI_VENDOR_CIRRUS;
        *product = QEMU_PCI_PRODUCT_VGA_CIRRUS;
        break;

    case VIR_DOMAIN_VIDEO_TYPE_VGA:
        *vendor = QEMU_PCI_VENDOR_QEMU;
        *product = QEMU_PCI_PRODUCT_VGA_STDVGA;
        break;

    case VIR_DOMAIN_VIDEO_TYPE_VMVGA:
        *vendor = QEMU_PCI_VENDOR_VMWARE;
        *product = QEMU_PCI_PRODUCT_VGA_VMWARE;
        break;

    default:
        return -1;
    }
    return 0;
}

static int
qemuGetPCISoundVendorProduct(virDomainSoundDefPtr def,
                             unsigned *vendor,
                             unsigned *product)
{
    switch (def->model) {
    case VIR_DOMAIN_SOUND_MODEL_ES1370:
        *vendor = QEMU_PCI_VENDOR_ENSONIQ;
        *product = QEMU_PCI_PRODUCT_AUDIO_ES1370;
        break;

    case VIR_DOMAIN_SOUND_MODEL_AC97:
        *vendor = QEMU_PCI_VENDOR_INTEL;
        *product = QEMU_PCI_PRODUCT_AUDIO_AC97;
        break;

    default:
        return -1;
    }

    return 0;
}

static int
qemuGetPCIWatchdogVendorProduct(virDomainWatchdogDefPtr def,
                                unsigned *vendor,
                                unsigned *product)
{
    switch (def->model) {
    case VIR_DOMAIN_WATCHDOG_MODEL_I6300ESB:
        *vendor = QEMU_PCI_VENDOR_INTEL;
        *product = QEMU_PCI_PRODUCT_WATCHDOG_I63000ESB;
        break;

    default:
        return -1;
    }

    return 0;
}


2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
static int
qemuGetPCIMemballoonVendorProduct(virDomainMemballoonDefPtr def,
                                  unsigned *vendor,
                                  unsigned *product)
{
    switch (def->model) {
    case VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO:
        *vendor = QEMU_PCI_VENDOR_REDHAT;
        *product = QEMU_PCI_PRODUCT_BALLOON_VIRTIO;
        break;

    default:
        return -1;
    }

    return 0;
}


2297 2298 2299 2300 2301 2302 2303 2304 2305
/*
 * This entire method assumes that PCI devices in 'info pci'
 * match ordering of devices specified on the command line
 * wrt to devices of matching vendor+product
 *
 * XXXX this might not be a valid assumption if we assign
 * some static addrs on CLI. Have to check that...
 */
static int
2306
qemuDetectPCIAddresses(virDomainObjPtr vm,
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
                       qemuMonitorPCIAddress *addrs,
                       int naddrs)
{
    unsigned int vendor = 0, product = 0;
    int i;

    /* XXX should all these vendor/product IDs be kept in the
     * actual device data structure instead ?
     */

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (qemuGetPCIDiskVendorProduct(vm->def->disks[i], &vendor, &product) < 0)
            continue;

        if (qemuAssignNextPCIAddress(&(vm->def->disks[i]->info),
                                     vendor, product,
                                     addrs, naddrs) < 0) {
2324 2325 2326
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            _("cannot find PCI address for VirtIO disk %s"),
                            vm->def->disks[i]->dst);
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
            return -1;
        }
    }

    for (i = 0 ; i < vm->def->nnets ; i++) {
        if (qemuGetPCINetVendorProduct(vm->def->nets[i], &vendor, &product) < 0)
            continue;

        if (qemuAssignNextPCIAddress(&(vm->def->nets[i]->info),
                                     vendor, product,
                                     addrs,  naddrs) < 0) {
2338 2339 2340
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            _("cannot find PCI address for %s NIC"),
                            vm->def->nets[i]->model);
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
            return -1;
        }
    }

    for (i = 0 ; i < vm->def->ncontrollers ; i++) {
        if (qemuGetPCIControllerVendorProduct(vm->def->controllers[i], &vendor, &product) < 0)
            continue;

        if (qemuAssignNextPCIAddress(&(vm->def->controllers[i]->info),
                                     vendor, product,
                                     addrs,  naddrs) < 0) {
2352 2353 2354
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            _("cannot find PCI address for controller %s"),
                            virDomainControllerTypeToString(vm->def->controllers[i]->type));
2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
            return -1;
        }
    }

    for (i = 0 ; i < vm->def->nvideos ; i++) {
        if (qemuGetPCIVideoVendorProduct(vm->def->videos[i], &vendor, &product) < 0)
            continue;

        if (qemuAssignNextPCIAddress(&(vm->def->videos[i]->info),
                                     vendor, product,
                                     addrs,  naddrs) < 0) {
2366 2367 2368
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            _("cannot find PCI address for video adapter %s"),
                            virDomainVideoTypeToString(vm->def->videos[i]->type));
2369 2370 2371 2372 2373 2374 2375 2376 2377
            return -1;
        }
    }

    for (i = 0 ; i < vm->def->nsounds ; i++) {
        if (qemuGetPCISoundVendorProduct(vm->def->sounds[i], &vendor, &product) < 0)
            continue;

        if (qemuAssignNextPCIAddress(&(vm->def->sounds[i]->info),
2378
                                    vendor, product,
2379
                                     addrs,  naddrs) < 0) {
2380 2381 2382
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            _("cannot find PCI address for sound adapter %s"),
                            virDomainSoundModelTypeToString(vm->def->sounds[i]->model));
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
            return -1;
        }
    }


    if (vm->def->watchdog &&
        qemuGetPCIWatchdogVendorProduct(vm->def->watchdog, &vendor, &product) == 0) {
        if (qemuAssignNextPCIAddress(&(vm->def->watchdog->info),
                                     vendor, product,
                                     addrs,  naddrs) < 0) {
2393 2394 2395
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            _("cannot find PCI address for watchdog %s"),
                            virDomainWatchdogModelTypeToString(vm->def->watchdog->model));
2396 2397 2398 2399
            return -1;
        }
    }

2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
    if (vm->def->memballoon &&
        qemuGetPCIMemballoonVendorProduct(vm->def->memballoon, &vendor, &product) == 0) {
        if (qemuAssignNextPCIAddress(&(vm->def->memballoon->info),
                                     vendor, product,
                                     addrs, naddrs) < 0) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            _("cannot find PCI address for balloon %s"),
                            virDomainMemballoonModelTypeToString(vm->def->memballoon->model));
            return -1;
        }
    }

2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437
    /* XXX console (virtio) */


    /* ... and now things we don't have in our xml */

    /* XXX USB controller ? */

    /* XXX what about other PCI devices (ie bridges) */

    return 0;
}

static int
qemuInitPCIAddresses(struct qemud_driver *driver,
                     virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int naddrs;
    int ret;
    qemuMonitorPCIAddress *addrs = NULL;

    qemuDomainObjEnterMonitorWithDriver(driver, vm);
    naddrs = qemuMonitorGetAllPCIAddresses(priv->mon,
                                           &addrs);
    qemuDomainObjExitMonitorWithDriver(driver, vm);

2438
    ret = qemuDetectPCIAddresses(vm, addrs, naddrs);
2439 2440 2441 2442 2443 2444

    VIR_FREE(addrs);

    return ret;
}

2445 2446 2447

static int qemudNextFreePort(struct qemud_driver *driver,
                             int startPort) {
2448 2449
    int i;

2450
    for (i = startPort ; i < QEMU_VNC_PORT_MAX; i++) {
2451 2452 2453
        int fd;
        int reuse = 1;
        struct sockaddr_in addr;
2454 2455 2456 2457 2458 2459 2460 2461 2462
        bool used = false;

        if (virBitmapGetBit(driver->reservedVNCPorts,
                            i - QEMU_VNC_PORT_MIN, &used) < 0)
            VIR_DEBUG("virBitmapGetBit failed on bit %d", i - QEMU_VNC_PORT_MIN);

        if (used)
            continue;

2463 2464 2465 2466 2467 2468 2469 2470
        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) {
2471
            VIR_FORCE_CLOSE(fd);
2472 2473 2474 2475 2476
            break;
        }

        if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
            /* Not in use, lets grab it */
2477
            VIR_FORCE_CLOSE(fd);
2478 2479 2480 2481 2482 2483
            /* Add port to bitmap of reserved ports */
            if (virBitmapSetBit(driver->reservedVNCPorts,
                                i - QEMU_VNC_PORT_MIN) < 0) {
                VIR_DEBUG("virBitmapSetBit failed on bit %d",
                          i - QEMU_VNC_PORT_MIN);
            }
2484 2485
            return i;
        }
2486
        VIR_FORCE_CLOSE(fd);
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497

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

2498

2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
static void
qemuReturnPort(struct qemud_driver *driver,
                int port)
{
    if (port < QEMU_VNC_PORT_MIN)
        return;

    if (virBitmapClearBit(driver->reservedVNCPorts,
                          port - QEMU_VNC_PORT_MIN) < 0)
        VIR_DEBUG("Could not mark port %d as unused", port);
}


2512 2513 2514 2515 2516 2517 2518
static int
qemuAssignPCIAddresses(virDomainDefPtr def)
{
    int ret = -1;
    unsigned long long qemuCmdFlags = 0;
    qemuDomainPCIAddressSetPtr addrs = NULL;

2519 2520 2521
    if (qemuCapsExtractVersionInfo(def->emulator,
                                   NULL,
                                   &qemuCmdFlags) < 0)
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540
        goto cleanup;

    if (qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE) {
        if (!(addrs = qemuDomainPCIAddressSetCreate(def)))
            goto cleanup;

        if (qemuAssignDevicePCISlots(def, addrs) < 0)
            goto cleanup;
    }

    ret = 0;

cleanup:
    qemuDomainPCIAddressSetFree(addrs);

    return ret;
}


2541 2542 2543 2544 2545 2546
static int
qemuPrepareChardevDevice(virDomainDefPtr def ATTRIBUTE_UNUSED,
                         virDomainChrDefPtr dev,
                         void *opaque ATTRIBUTE_UNUSED)
{
    int fd;
2547
    if (dev->source.type != VIR_DOMAIN_CHR_TYPE_FILE)
2548 2549
        return 0;

2550 2551
    if ((fd = open(dev->source.data.file.path,
                   O_CREAT | O_APPEND, S_IRUSR|S_IWUSR)) < 0) {
2552 2553
        virReportSystemError(errno,
                             _("Unable to pre-create chardev file '%s'"),
2554
                             dev->source.data.file.path);
2555 2556 2557
        return -1;
    }

2558
    VIR_FORCE_CLOSE(fd);
2559 2560 2561 2562 2563

    return 0;
}


2564 2565 2566 2567
struct qemudHookData {
    virConnectPtr conn;
    virDomainObjPtr vm;
    struct qemud_driver *driver;
2568 2569 2570
};

static int qemudSecurityHook(void *data) {
2571 2572
    struct qemudHookData *h = data;

2573 2574 2575
    /* This must take place before exec(), so that all QEMU
     * memory allocation is on the correct NUMA node
     */
2576
    if (qemuAddToCgroup(h->driver, h->vm->def) < 0)
2577 2578
        return -1;

2579 2580 2581
    /* This must be done after cgroup placement to avoid resetting CPU
     * affinity */
    if (qemudInitCpuAffinity(h->vm) < 0)
2582
        return -1;
2583

2584
    if (virSecurityManagerSetProcessLabel(h->driver->securityManager, h->vm) < 0)
2585 2586 2587
        return -1;

    return 0;
2588 2589
}

2590
static int
2591
qemuPrepareMonitorChr(struct qemud_driver *driver,
2592
                      virDomainChrSourceDefPtr monConfig,
2593 2594
                      const char *vm)
{
2595 2596
    monConfig->type = VIR_DOMAIN_CHR_TYPE_UNIX;
    monConfig->data.nix.listen = true;
2597

2598
    if (virAsprintf(&monConfig->data.nix.path, "%s/%s.monitor",
2599
                    driver->libDir, vm) < 0) {
2600
        virReportOOMError();
2601 2602 2603 2604 2605 2606
        return -1;
    }

    return 0;
}

2607 2608 2609 2610
static int qemuDomainSnapshotSetCurrentActive(virDomainObjPtr vm,
                                              char *snapshotDir);
static int qemuDomainSnapshotSetCurrentInactive(virDomainObjPtr vm,
                                                char *snapshotDir);
C
Chris Lalancette 已提交
2611

2612

2613 2614
#define START_POSTFIX ": starting up\n"
#define SHUTDOWN_POSTFIX ": shutting down\n"
2615

2616 2617
static int qemudStartVMDaemon(virConnectPtr conn,
                              struct qemud_driver *driver,
2618
                              virDomainObjPtr vm,
2619
                              const char *migrateFrom,
2620
                              bool start_paused,
2621
                              int stdin_fd,
2622 2623
                              const char *stdin_path,
                              enum virVMOperationType vmop) {
E
Eric Blake 已提交
2624
    int ret;
2625
    unsigned long long qemuCmdFlags;
2626
    off_t pos = -1;
2627
    char ebuf[1024];
2628
    char *pidfile = NULL;
2629
    int logfile = -1;
2630
    char *timestamp;
2631
    qemuDomainObjPrivatePtr priv = vm->privateData;
E
Eric Blake 已提交
2632
    virCommandPtr cmd = NULL;
2633

2634
    struct qemudHookData hookData;
2635 2636 2637 2638
    hookData.conn = conn;
    hookData.vm = vm;
    hookData.driver = driver;

2639 2640
    DEBUG0("Beginning VM startup process");

D
Daniel P. Berrange 已提交
2641
    if (virDomainObjIsActive(vm)) {
2642 2643
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("VM is already active"));
2644 2645 2646
        return -1;
    }

2647 2648 2649 2650 2651 2652 2653 2654
    /* Do this upfront, so any part of the startup process can add
     * runtime state to vm->def that won't be persisted. This let's us
     * report implicit runtime defaults in the XML, like vnc listen/socket
     */
    DEBUG0("Setting current domain def as transient");
    if (virDomainObjSetDefTransient(driver->caps, vm, true) < 0)
        goto cleanup;

2655 2656 2657 2658 2659
    /* Must be run before security labelling */
    DEBUG0("Preparing host devices");
    if (qemuPrepareHostDevices(driver, vm->def) < 0)
        goto cleanup;

2660 2661 2662 2663 2664 2665 2666
    DEBUG0("Preparing chr devices");
    if (virDomainChrDefForeach(vm->def,
                               true,
                               qemuPrepareChardevDevice,
                               NULL) < 0)
        goto cleanup;

2667 2668
    /* If you are using a SecurityDriver with dynamic labelling,
       then generate a security label for isolation */
2669
    DEBUG0("Generating domain security label (if required)");
2670 2671 2672
    if (virSecurityManagerGenLabel(driver->securityManager, vm) < 0) {
        qemuDomainSecurityLabelAudit(vm, false);
        goto cleanup;
2673
    }
2674
    qemuDomainSecurityLabelAudit(vm, true);
2675

2676
    DEBUG0("Generating setting domain security labels (if required)");
2677 2678
    if (virSecurityManagerSetAllLabel(driver->securityManager,
                                      vm, stdin_path) < 0)
2679
        goto cleanup;
2680

2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
    if (stdin_fd != -1) {
        /* if there's an fd to migrate from, and it's a pipe, put the
         * proper security label on it
         */
        struct stat stdin_sb;

        DEBUG0("setting security label on pipe used for migration");

        if (fstat(stdin_fd, &stdin_sb) < 0) {
            virReportSystemError(errno,
                                 _("cannot stat fd %d"), stdin_fd);
            goto cleanup;
        }
        if (S_ISFIFO(stdin_sb.st_mode) &&
            virSecurityManagerSetFDLabel(driver->securityManager, vm, stdin_fd) < 0)
            goto cleanup;
    }

2699 2700 2701
    /* Ensure no historical cgroup for this VM is lying around bogus
     * settings */
    DEBUG0("Ensuring no historical cgroup is lying around");
2702
    qemuRemoveCgroup(driver, vm, 1);
2703

2704 2705
    if (vm->def->ngraphics == 1) {
        if (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
2706
            !vm->def->graphics[0]->data.vnc.socket &&
2707
            vm->def->graphics[0]->data.vnc.autoport) {
2708
            int port = qemudNextFreePort(driver, QEMU_VNC_PORT_MIN);
2709 2710 2711 2712 2713 2714 2715 2716
            if (port < 0) {
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                "%s", _("Unable to find an unused VNC port"));
                goto cleanup;
            }
            vm->def->graphics[0]->data.vnc.port = port;
        } else if (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SPICE &&
                   vm->def->graphics[0]->data.spice.autoport) {
2717
            int port = qemudNextFreePort(driver, QEMU_VNC_PORT_MIN);
2718 2719
            int tlsPort = -1;
            if (port < 0) {
2720
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
2721
                                "%s", _("Unable to find an unused SPICE port"));
2722 2723 2724
                goto cleanup;
            }

2725 2726 2727 2728 2729
            if (driver->spiceTLS) {
                tlsPort = qemudNextFreePort(driver, port + 1);
                if (tlsPort < 0) {
                    qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                    "%s", _("Unable to find an unused SPICE TLS port"));
2730
                    qemuReturnPort(driver, port);
2731 2732 2733 2734
                    goto cleanup;
                }
            }

2735 2736
            vm->def->graphics[0]->data.spice.port = port;
            vm->def->graphics[0]->data.spice.tlsPort = tlsPort;
2737
        }
2738
    }
2739

L
Laine Stump 已提交
2740
    if (virFileMakePath(driver->logDir) != 0) {
2741
        virReportSystemError(errno,
2742 2743
                             _("cannot create log directory %s"),
                             driver->logDir);
2744
        goto cleanup;
2745 2746
    }

2747
    DEBUG0("Creating domain log file");
2748
    if ((logfile = qemudLogFD(driver, vm->def->name, false)) < 0)
2749
        goto cleanup;
2750

E
Eric Blake 已提交
2751
    DEBUG0("Determining emulator version");
2752 2753 2754
    if (qemuCapsExtractVersionInfo(vm->def->emulator,
                                   NULL,
                                   &qemuCmdFlags) < 0)
2755
        goto cleanup;
2756

2757
    DEBUG0("Setting up domain cgroup (if required)");
2758
    if (qemuSetupCgroup(driver, vm) < 0)
2759 2760
        goto cleanup;

2761
    if (VIR_ALLOC(priv->monConfig) < 0) {
2762
        virReportOOMError();
2763 2764 2765
        goto cleanup;
    }

2766
    DEBUG0("Preparing monitor state");
2767
    if (qemuPrepareMonitorChr(driver, priv->monConfig, vm->def->name) < 0)
2768
        goto cleanup;
2769

D
Daniel P. Berrange 已提交
2770 2771 2772
#if HAVE_YAJL
    if (qemuCmdFlags & QEMUD_CMD_FLAG_MONITOR_JSON)
        priv->monJSON = 1;
2773
    else
D
Daniel P. Berrange 已提交
2774
#endif
2775
        priv->monJSON = 0;
D
Daniel P. Berrange 已提交
2776

2777
    priv->monitor_warned = 0;
2778
    priv->gotShutdown = false;
2779

D
Daniel P. Berrange 已提交
2780
    if ((ret = virFileDeletePid(driver->stateDir, vm->def->name)) != 0) {
2781
        virReportSystemError(ret,
D
Daniel P. Berrange 已提交
2782 2783 2784 2785 2786
                             _("Cannot remove stale PID file for %s"),
                             vm->def->name);
        goto cleanup;
    }

2787
    if (!(pidfile = virFilePid(driver->stateDir, vm->def->name))) {
2788
        virReportSystemError(errno,
2789 2790 2791
                             "%s", _("Failed to build pidfile path."));
        goto cleanup;
    }
D
Daniel P. Berrange 已提交
2792

2793
    /*
M
Matthew Booth 已提交
2794
     * Normally PCI addresses are assigned in the virDomainCreate
2795 2796 2797 2798 2799
     * or virDomainDefine methods. We might still need to assign
     * some here to cope with the question of upgrades. Regardless
     * we also need to populate the PCi address set cache for later
     * use in hotplug
     */
2800
    if (qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE) {
2801
        DEBUG0("Assigning domain PCI addresses");
2802
        /* Populate cache with current addresses */
2803 2804 2805 2806 2807 2808 2809
        if (priv->pciaddrs) {
            qemuDomainPCIAddressSetFree(priv->pciaddrs);
            priv->pciaddrs = NULL;
        }
        if (!(priv->pciaddrs = qemuDomainPCIAddressSetCreate(vm->def)))
            goto cleanup;

2810 2811

        /* Assign any remaining addresses */
2812 2813
        if (qemuAssignDevicePCISlots(vm->def, priv->pciaddrs) < 0)
            goto cleanup;
2814 2815 2816 2817

        priv->persistentAddrs = 1;
    } else {
        priv->persistentAddrs = 0;
2818 2819
    }

2820
    DEBUG0("Building emulator command line");
2821
    vm->def->id = driver->nextvmid++;
2822 2823
    if (!(cmd = qemuBuildCommandLine(conn, driver, vm->def, priv->monConfig,
                                     priv->monJSON != 0, qemuCmdFlags,
2824
                                     migrateFrom, stdin_fd,
2825
                                     vm->current_snapshot, vmop)))
C
Chris Lalancette 已提交
2826 2827
        goto cleanup;

2828
    if (qemuDomainSnapshotSetCurrentInactive(vm, driver->snapshotDir) < 0)
2829
        goto cleanup;
2830

2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
    /* now that we know it is about to start call the hook if present */
    if (virHookPresent(VIR_HOOK_DRIVER_QEMU)) {
        char *xml = virDomainDefFormat(vm->def, 0);
        int hookret;

        hookret = virHookCall(VIR_HOOK_DRIVER_QEMU, vm->def->name,
                    VIR_HOOK_QEMU_OP_START, VIR_HOOK_SUBOP_BEGIN, NULL, xml);
        VIR_FREE(xml);

        /*
         * If the script raised an error abort the launch
         */
        if (hookret < 0)
            goto cleanup;
    }

2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859
    if ((timestamp = virTimestamp()) == NULL) {
        virReportOOMError();
        goto cleanup;
    } else {
        if (safewrite(logfile, timestamp, strlen(timestamp)) < 0 ||
            safewrite(logfile, START_POSTFIX, strlen(START_POSTFIX)) < 0) {
            VIR_WARN("Unable to write timestamp to logfile: %s",
                     virStrerror(errno, ebuf, sizeof ebuf));
        }

        VIR_FREE(timestamp);
    }

E
Eric Blake 已提交
2860
    virCommandWriteArgLog(cmd, logfile);
2861

2862
    if ((pos = lseek(logfile, 0, SEEK_END)) < 0)
2863
        VIR_WARN("Unable to seek to end of logfile: %s",
2864
                 virStrerror(errno, ebuf, sizeof ebuf));
2865

2866 2867
    VIR_DEBUG("Clear emulator capabilities: %d",
              driver->clearEmulatorCapabilities);
E
Eric Blake 已提交
2868 2869 2870 2871
    if (driver->clearEmulatorCapabilities)
        virCommandClearCaps(cmd);

    virCommandSetPreExecHook(cmd, qemudSecurityHook, &hookData);
2872

E
Eric Blake 已提交
2873 2874 2875 2876 2877 2878 2879
    virCommandSetOutputFD(cmd, &logfile);
    virCommandSetErrorFD(cmd, &logfile);
    virCommandNonblockingFDs(cmd);
    virCommandSetPidFile(cmd, pidfile);
    virCommandDaemonize(cmd);

    ret = virCommandRun(cmd, NULL);
2880
    VIR_FREE(pidfile);
G
Guido Günther 已提交
2881 2882 2883

    /* wait for qemu process to to show up */
    if (ret == 0) {
2884
        if (virFileReadPid(driver->stateDir, vm->def->name, &vm->pid)) {
2885
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
2886
                            _("Domain %s didn't show up"), vm->def->name);
2887
            ret = -1;
G
Guido Günther 已提交
2888
        }
E
Eric Blake 已提交
2889
#if 0
2890
    } else if (ret == -2) {
E
Eric Blake 已提交
2891 2892 2893 2894 2895
        /*
         * XXX this is bogus. It isn't safe to set vm->pid = child
         * because the child no longer exists.
         */

2896 2897 2898 2899 2900 2901 2902 2903 2904
        /* The virExec process that launches the daemon failed. Pending on
         * when it failed (we can't determine for sure), there may be
         * extra info in the domain log (if the hook failed for example).
         *
         * Pretend like things succeeded, and let 'WaitForMonitor' report
         * the log contents for us.
         */
        vm->pid = child;
        ret = 0;
E
Eric Blake 已提交
2905
#endif
2906
    }
2907

2908 2909 2910
    if (migrateFrom)
        start_paused = true;
    vm->state = start_paused ? VIR_DOMAIN_PAUSED : VIR_DOMAIN_RUNNING;
2911

2912
    if (ret == -1) /* The VM failed to start; tear filters before taps */
2913
        virDomainConfVMNWFilterTeardown(vm);
2914

2915
    if (ret == -1) /* The VM failed to start */
2916 2917
        goto cleanup;

2918
    DEBUG0("Waiting for monitor to show up");
2919
    if (qemudWaitForMonitor(driver, vm, pos) < 0)
2920
        goto cleanup;
2921

2922
    DEBUG0("Detecting VCPU PIDs");
2923
    if (qemuDetectVcpuPIDs(driver, vm) < 0)
2924
        goto cleanup;
2925

2926
    DEBUG0("Setting any required VM passwords");
2927
    if (qemuInitPasswords(conn, driver, vm, qemuCmdFlags) < 0)
2928
        goto cleanup;
2929

D
Daniel P. Berrange 已提交
2930 2931 2932
    /* If we have -device, then addresses are assigned explicitly.
     * If not, then we have to detect dynamic ones here */
    if (!(qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE)) {
2933
        DEBUG0("Determining domain device PCI addresses");
D
Daniel P. Berrange 已提交
2934
        if (qemuInitPCIAddresses(driver, vm) < 0)
2935
            goto cleanup;
D
Daniel P. Berrange 已提交
2936
    }
2937

2938
    DEBUG0("Setting initial memory amount");
2939
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
2940
    if (qemuMonitorSetBalloon(priv->mon, vm->def->mem.cur_balloon) < 0) {
2941
        qemuDomainObjExitMonitorWithDriver(driver, vm);
2942
        goto cleanup;
2943
    }
H
Hu Tao 已提交
2944
    qemuDomainObjExitMonitorWithDriver(driver, vm);
2945

2946
    if (!start_paused) {
2947
        DEBUG0("Starting domain CPUs");
2948
        /* Allow the CPUS to start executing */
H
Hu Tao 已提交
2949
        if (doStartCPUs(driver, vm, conn) < 0) {
2950
            if (virGetLastError() == NULL)
2951 2952
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                "%s", _("resume operation failed"));
2953
            goto cleanup;
2954 2955
        }
    }
2956

2957

2958
    DEBUG0("Writing domain status to disk");
2959
    if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
2960 2961
        goto cleanup;

E
Eric Blake 已提交
2962
    virCommandFree(cmd);
2963
    VIR_FORCE_CLOSE(logfile);
2964

2965
    return 0;
2966 2967

cleanup:
2968 2969 2970
    /* We jump here if we failed to start the VM for any reason, or
     * if we failed to initialize the now running VM. kill it off and
     * pretend we never started it */
E
Eric Blake 已提交
2971
    virCommandFree(cmd);
2972
    VIR_FORCE_CLOSE(logfile);
E
Eric Blake 已提交
2973
    qemudShutdownVMDaemon(driver, vm, 0);
2974 2975

    return -1;
2976 2977
}

2978
static void qemudShutdownVMDaemon(struct qemud_driver *driver,
2979 2980
                                  virDomainObjPtr vm,
                                  int migrated) {
D
Daniel P. Berrange 已提交
2981
    int ret;
2982
    int retries = 0;
2983
    qemuDomainObjPrivatePtr priv = vm->privateData;
2984
    virErrorPtr orig_err;
2985 2986
    virDomainDefPtr def;
    int i;
2987 2988 2989
    int logfile = -1;
    char *timestamp;
    char ebuf[1024];
D
Daniel P. Berrange 已提交
2990

2991 2992
    VIR_DEBUG("Shutting down VM '%s' pid=%d migrated=%d",
              vm->def->name, vm->pid, migrated);
2993

E
Eric Blake 已提交
2994 2995 2996 2997 2998
    if (!virDomainObjIsActive(vm)) {
        VIR_DEBUG("VM '%s' not active", vm->def->name);
        return;
    }

2999
    if ((logfile = qemudLogFD(driver, vm->def->name, true)) < 0) {
3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
        /* To not break the normal domain shutdown process, skip the
         * timestamp log writing if failed on opening log file. */
        VIR_WARN("Unable to open logfile: %s",
                  virStrerror(errno, ebuf, sizeof ebuf));
    } else {
        if ((timestamp = virTimestamp()) == NULL) {
            virReportOOMError();
        } else {
            if (safewrite(logfile, timestamp, strlen(timestamp)) < 0 ||
                safewrite(logfile, SHUTDOWN_POSTFIX,
                          strlen(SHUTDOWN_POSTFIX)) < 0) {
                VIR_WARN("Unable to write timestamp to logfile: %s",
                         virStrerror(errno, ebuf, sizeof ebuf));
            }

            VIR_FREE(timestamp);
        }

        if (VIR_CLOSE(logfile) < 0)
             VIR_WARN("Unable to close logfile: %s",
                      virStrerror(errno, ebuf, sizeof ebuf));
    }

3023 3024 3025 3026
    /* This method is routinely used in clean up paths. Disable error
     * reporting so we don't squash a legit error. */
    orig_err = virSaveLastError();

3027
    virDomainConfVMNWFilterTeardown(vm);
S
Stefan Berger 已提交
3028

3029
    if (driver->macFilter) {
3030
        def = vm->def;
3031 3032 3033 3034
        for (i = 0 ; i < def->nnets ; i++) {
            virDomainNetDefPtr net = def->nets[i];
            if (net->ifname == NULL)
                continue;
3035
            if ((errno = networkDisallowMacOnPort(driver, net->ifname,
3036
                                                  net->mac))) {
3037
                virReportSystemError(errno,
3038 3039 3040 3041 3042 3043
             _("failed to remove ebtables rule to allow MAC address on  '%s'"),
                                     net->ifname);
            }
        }
    }

3044
    /* This will safely handle a non-running guest with pid=0 or pid=-1*/
G
Guido Günther 已提交
3045 3046
    if (virKillProcess(vm->pid, 0) == 0 &&
        virKillProcess(vm->pid, SIGTERM) < 0)
3047
        virReportSystemError(errno,
3048 3049
                             _("Failed to send SIGTERM to %s (%d)"),
                             vm->def->name, vm->pid);
3050

3051 3052
    if (priv->mon)
        qemuMonitorClose(priv->mon);
3053

3054
    if (priv->monConfig) {
3055 3056 3057
        if (priv->monConfig->type == VIR_DOMAIN_CHR_TYPE_UNIX)
            unlink(priv->monConfig->data.nix.path);
        virDomainChrSourceDefFree(priv->monConfig);
3058
        priv->monConfig = NULL;
3059 3060
    }

G
Guido Günther 已提交
3061 3062
    /* shut it off for sure */
    virKillProcess(vm->pid, SIGKILL);
3063

3064 3065 3066 3067 3068 3069 3070 3071 3072 3073
    /* now that we know it's stopped call the hook if present */
    if (virHookPresent(VIR_HOOK_DRIVER_QEMU)) {
        char *xml = virDomainDefFormat(vm->def, 0);

        /* we can't stop the operation even if the script raised an error */
        virHookCall(VIR_HOOK_DRIVER_QEMU, vm->def->name,
                    VIR_HOOK_QEMU_OP_STOPPED, VIR_HOOK_SUBOP_END, NULL, xml);
        VIR_FREE(xml);
    }

3074
    /* Reset Security Labels */
3075 3076 3077
    virSecurityManagerRestoreAllLabel(driver->securityManager,
                                      vm, migrated);
    virSecurityManagerReleaseLabel(driver->securityManager, vm);
3078

3079 3080 3081 3082 3083 3084 3085
    /* Clear out dynamically assigned labels */
    if (vm->def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC) {
        VIR_FREE(vm->def->seclabel.model);
        VIR_FREE(vm->def->seclabel.label);
        VIR_FREE(vm->def->seclabel.imagelabel);
    }

D
Daniel P. Berrange 已提交
3086
    virDomainDefClearDeviceAliases(vm->def);
3087 3088 3089 3090 3091
    if (!priv->persistentAddrs) {
        virDomainDefClearPCIAddresses(vm->def);
        qemuDomainPCIAddressSetFree(priv->pciaddrs);
        priv->pciaddrs = NULL;
    }
3092

3093
    qemuDomainReAttachHostDevices(driver, vm->def);
3094

S
Stefan Berger 已提交
3095 3096 3097 3098
#if WITH_MACVTAP
    def = vm->def;
    for (i = 0; i < def->nnets; i++) {
        virDomainNetDefPtr net = def->nets[i];
3099
        if (net->type == VIR_DOMAIN_NET_TYPE_DIRECT) {
3100
            delMacvtap(net->ifname, net->mac, net->data.direct.linkdev,
3101
                       &net->data.direct.virtPortProfile);
3102 3103
            VIR_FREE(net->ifname);
        }
S
Stefan Berger 已提交
3104 3105 3106
    }
#endif

3107
retry:
3108
    if ((ret = qemuRemoveCgroup(driver, vm, 0)) < 0) {
3109 3110 3111 3112 3113 3114 3115 3116
        if (ret == -EBUSY && (retries++ < 5)) {
            usleep(200*1000);
            goto retry;
        }
        VIR_WARN("Failed to remove cgroup for %s",
                 vm->def->name);
    }

3117
    qemudRemoveDomainStatus(driver, vm);
D
Daniel P. Berrange 已提交
3118

3119 3120 3121 3122 3123
    /* Remove VNC port from port reservation bitmap, but only if it was
       reserved by the driver (autoport=yes)
    */
    if ((vm->def->ngraphics == 1) &&
        vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
3124 3125 3126 3127 3128 3129 3130 3131
        vm->def->graphics[0]->data.vnc.autoport) {
        qemuReturnPort(driver, vm->def->graphics[0]->data.vnc.port);
    }
    if ((vm->def->ngraphics == 1) &&
        vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SPICE &&
        vm->def->graphics[0]->data.spice.autoport) {
        qemuReturnPort(driver, vm->def->graphics[0]->data.spice.port);
        qemuReturnPort(driver, vm->def->graphics[0]->data.spice.tlsPort);
3132 3133
    }

3134
    vm->pid = -1;
3135
    vm->def->id = -1;
3136
    vm->state = VIR_DOMAIN_SHUTOFF;
3137 3138
    VIR_FREE(priv->vcpupids);
    priv->nvcpupids = 0;
3139 3140

    if (vm->newDef) {
3141
        virDomainDefFree(vm->def);
3142
        vm->def = vm->newDef;
3143
        vm->def->id = -1;
3144 3145
        vm->newDef = NULL;
    }
3146 3147 3148 3149 3150

    if (orig_err) {
        virSetError(orig_err);
        virFreeError(orig_err);
    }
3151 3152
}

3153
static virDrvOpenStatus qemudOpen(virConnectPtr conn,
3154
                                  virConnectAuthPtr auth ATTRIBUTE_UNUSED,
3155
                                  int flags ATTRIBUTE_UNUSED) {
3156
    if (conn->uri == NULL) {
3157 3158 3159
        if (qemu_driver == NULL)
            return VIR_DRV_OPEN_DECLINED;

3160
        conn->uri = xmlParseURI(qemu_driver->privileged ?
3161 3162
                                "qemu:///system" :
                                "qemu:///session");
3163
        if (!conn->uri) {
3164
            virReportOOMError();
3165 3166
            return VIR_DRV_OPEN_ERROR;
        }
3167 3168 3169 3170 3171 3172 3173 3174 3175 3176
    } else {
        /* If URI isn't 'qemu' its definitely not for us */
        if (conn->uri->scheme == NULL ||
            STRNEQ(conn->uri->scheme, "qemu"))
            return VIR_DRV_OPEN_DECLINED;

        /* Allow remote driver to deal with URIs with hostname server */
        if (conn->uri->server != NULL)
            return VIR_DRV_OPEN_DECLINED;

3177
        if (qemu_driver == NULL) {
3178 3179
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("qemu state driver is not active"));
3180 3181 3182
            return VIR_DRV_OPEN_ERROR;
        }

3183
        if (conn->uri->path == NULL) {
3184 3185 3186 3187 3188
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            _("no QEMU URI path given, try %s"),
                            qemu_driver->privileged
                            ? "qemu:///system"
                            : "qemu:///session");
3189 3190 3191
                return VIR_DRV_OPEN_ERROR;
        }

3192
        if (qemu_driver->privileged) {
3193 3194
            if (STRNEQ (conn->uri->path, "/system") &&
                STRNEQ (conn->uri->path, "/session")) {
3195 3196 3197
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                _("unexpected QEMU URI path '%s', try qemu:///system"),
                                conn->uri->path);
3198 3199 3200 3201
                return VIR_DRV_OPEN_ERROR;
            }
        } else {
            if (STRNEQ (conn->uri->path, "/session")) {
3202 3203 3204
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                _("unexpected QEMU URI path '%s', try qemu:///session"),
                                conn->uri->path);
3205 3206 3207
                return VIR_DRV_OPEN_ERROR;
            }
        }
3208 3209 3210 3211 3212 3213 3214
    }
    conn->privateData = qemu_driver;

    return VIR_DRV_OPEN_SUCCESS;
}

static int qemudClose(virConnectPtr conn) {
3215
    struct qemud_driver *driver = conn->privateData;
3216 3217

    /* Get rid of callbacks registered for this conn */
3218
    qemuDriverLock(driver);
3219
    virDomainEventCallbackListRemoveConn(conn, driver->domainEventCallbacks);
3220
    qemuDriverUnlock(driver);
3221 3222 3223 3224 3225 3226

    conn->privateData = NULL;

    return 0;
}

D
Daniel Veillard 已提交
3227 3228 3229 3230 3231
/* Which features are supported by this driver? */
static int
qemudSupportsFeature (virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
    switch (feature) {
3232 3233 3234 3235 3236
    case VIR_DRV_FEATURE_MIGRATION_V2:
    case VIR_DRV_FEATURE_MIGRATION_P2P:
        return 1;
    default:
        return 0;
D
Daniel Veillard 已提交
3237 3238 3239
    }
}

3240
static const char *qemudGetType(virConnectPtr conn ATTRIBUTE_UNUSED) {
3241
    return "QEMU";
3242 3243
}

3244

3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257
static int qemuIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Trivially secure, since always inside the daemon */
    return 1;
}

static int qemuIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Not encrypted, but remote driver takes care of that */
    return 0;
}


3258 3259 3260 3261
static int kvmGetMaxVCPUs(void) {
    int maxvcpus = 1;

    int r, fd;
3262

3263 3264
    fd = open(KVM_DEVICE, O_RDONLY);
    if (fd < 0) {
3265
        virReportSystemError(errno, _("Unable to open %s"), KVM_DEVICE);
3266
        return -1;
3267 3268 3269 3270 3271 3272
    }

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

3273
    VIR_FORCE_CLOSE(fd);
3274 3275 3276 3277
    return maxvcpus;
}


3278
static int qemudGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED, const char *type) {
3279 3280 3281
    if (!type)
        return 16;

3282
    if (STRCASEEQ(type, "qemu"))
3283 3284
        return 16;

3285
    if (STRCASEEQ(type, "kvm"))
3286
        return kvmGetMaxVCPUs();
3287

3288
    if (STRCASEEQ(type, "kqemu"))
3289
        return 1;
3290

3291 3292
    qemuReportError(VIR_ERR_INVALID_ARG,
                    _("unknown type '%s'"), type);
3293 3294 3295
    return -1;
}

3296

3297
static char *qemudGetCapabilities(virConnectPtr conn) {
3298
    struct qemud_driver *driver = conn->privateData;
3299
    virCapsPtr caps = NULL;
3300
    char *xml = NULL;
3301

3302
    qemuDriverLock(driver);
3303

3304
    if ((caps = qemuCreateCapabilities(qemu_driver->caps,
3305
                                       qemu_driver)) == NULL) {
3306 3307 3308
        virCapabilitiesFree(caps);
        goto cleanup;
    }
3309

3310
    virCapabilitiesFree(qemu_driver->caps);
3311 3312 3313
    qemu_driver->caps = caps;

    if ((xml = virCapabilitiesFormatXML(driver->caps)) == NULL)
3314
        virReportOOMError();
3315 3316

cleanup:
3317
    qemuDriverUnlock(driver);
3318

3319
    return xml;
3320 3321 3322
}


3323 3324 3325 3326 3327
static int
qemudGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, int pid,
                    int tid)
{
    char *proc;
D
Daniel P. Berrange 已提交
3328
    FILE *pidinfo;
3329
    unsigned long long usertime, systime;
3330 3331
    int cpu;
    int ret;
D
Daniel P. Berrange 已提交
3332

3333
    if (tid)
3334
        ret = virAsprintf(&proc, "/proc/%d/task/%d/stat", pid, tid);
3335
    else
3336 3337
        ret = virAsprintf(&proc, "/proc/%d/stat", pid);
    if (ret < 0)
D
Daniel P. Berrange 已提交
3338 3339 3340 3341
        return -1;

    if (!(pidinfo = fopen(proc, "r"))) {
        /* VM probably shut down, so fake 0 */
3342 3343 3344 3345
        if (cpuTime)
            *cpuTime = 0;
        if (lastCpu)
            *lastCpu = 0;
3346
        VIR_FREE(proc);
D
Daniel P. Berrange 已提交
3347 3348
        return 0;
    }
3349
    VIR_FREE(proc);
D
Daniel P. Berrange 已提交
3350

3351 3352 3353 3354 3355 3356 3357 3358 3359 3360
    /* See 'man proc' for information about what all these fields are. We're
     * only interested in a very few of them */
    if (fscanf(pidinfo,
               /* pid -> stime */
               "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %llu %llu"
               /* cutime -> endcode */
               "%*d %*d %*d %*d %*d %*u %*u %*d %*u %*u %*u %*u"
               /* startstack -> processor */
               "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*d %d",
               &usertime, &systime, &cpu) != 3) {
3361
        VIR_FORCE_FCLOSE(pidinfo);
3362 3363
        VIR_WARN0("cannot parse process status data");
        errno = -EINVAL;
D
Daniel P. Berrange 已提交
3364 3365 3366 3367 3368 3369 3370 3371
        return -1;
    }

    /* We got jiffies
     * We want nanoseconds
     * _SC_CLK_TCK is jiffies per second
     * So calulate thus....
     */
3372 3373 3374 3375 3376
    if (cpuTime)
        *cpuTime = 1000ull * 1000ull * 1000ull * (usertime + systime) / (unsigned long long)sysconf(_SC_CLK_TCK);
    if (lastCpu)
        *lastCpu = cpu;

D
Daniel P. Berrange 已提交
3377

3378 3379
    VIR_DEBUG("Got status for %d/%d user=%llu sys=%llu cpu=%d",
              pid, tid, usertime, systime, cpu);
D
Daniel P. Berrange 已提交
3380

3381
    VIR_FORCE_FCLOSE(pidinfo);
D
Daniel P. Berrange 已提交
3382 3383 3384 3385 3386

    return 0;
}


3387
static virDomainPtr qemudDomainLookupByID(virConnectPtr conn,
3388
                                          int id) {
3389 3390 3391 3392
    struct qemud_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;

3393
    qemuDriverLock(driver);
3394
    vm  = virDomainFindByID(&driver->domains, id);
3395
    qemuDriverUnlock(driver);
3396 3397

    if (!vm) {
3398 3399
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching id %d"), id);
3400
        goto cleanup;
3401 3402
    }

3403
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
3404
    if (dom) dom->id = vm->def->id;
3405 3406

cleanup:
3407 3408
    if (vm)
        virDomainObjUnlock(vm);
3409 3410
    return dom;
}
3411

3412
static virDomainPtr qemudDomainLookupByUUID(virConnectPtr conn,
3413
                                            const unsigned char *uuid) {
3414 3415 3416
    struct qemud_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
3417

3418
    qemuDriverLock(driver);
3419
    vm = virDomainFindByUUID(&driver->domains, uuid);
3420 3421
    qemuDriverUnlock(driver);

3422
    if (!vm) {
3423 3424
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(uuid, uuidstr);
3425 3426
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
3427
        goto cleanup;
3428 3429
    }

3430
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
3431
    if (dom) dom->id = vm->def->id;
3432 3433

cleanup:
3434 3435
    if (vm)
        virDomainObjUnlock(vm);
3436 3437
    return dom;
}
3438

3439
static virDomainPtr qemudDomainLookupByName(virConnectPtr conn,
3440
                                            const char *name) {
3441 3442 3443
    struct qemud_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
3444

3445
    qemuDriverLock(driver);
3446
    vm = virDomainFindByName(&driver->domains, name);
3447 3448
    qemuDriverUnlock(driver);

3449
    if (!vm) {
3450 3451
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching name '%s'"), name);
3452
        goto cleanup;
3453 3454
    }

3455
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
3456
    if (dom) dom->id = vm->def->id;
3457 3458

cleanup:
3459 3460
    if (vm)
        virDomainObjUnlock(vm);
3461 3462 3463
    return dom;
}

3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474

static int qemuDomainIsActive(virDomainPtr dom)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    qemuDriverLock(driver);
    obj = virDomainFindByUUID(&driver->domains, dom->uuid);
    qemuDriverUnlock(driver);
    if (!obj) {
3475
        qemuReportError(VIR_ERR_NO_DOMAIN, NULL);
3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

cleanup:
    if (obj)
        virDomainObjUnlock(obj);
    return ret;
}

static int qemuDomainIsPersistent(virDomainPtr dom)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    qemuDriverLock(driver);
    obj = virDomainFindByUUID(&driver->domains, dom->uuid);
    qemuDriverUnlock(driver);
    if (!obj) {
3496
        qemuReportError(VIR_ERR_NO_DOMAIN, NULL);
3497 3498 3499 3500 3501 3502 3503 3504 3505 3506
        goto cleanup;
    }
    ret = obj->persistent;

cleanup:
    if (obj)
        virDomainObjUnlock(obj);
    return ret;
}

3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526
static int qemuDomainIsUpdated(virDomainPtr dom)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    qemuDriverLock(driver);
    obj = virDomainFindByUUID(&driver->domains, dom->uuid);
    qemuDriverUnlock(driver);
    if (!obj) {
        qemuReportError(VIR_ERR_NO_DOMAIN, NULL);
        goto cleanup;
    }
    ret = obj->updated;

cleanup:
    if (obj)
        virDomainObjUnlock(obj);
    return ret;
}
3527

3528
static int qemudGetVersion(virConnectPtr conn, unsigned long *version) {
3529 3530 3531
    struct qemud_driver *driver = conn->privateData;
    int ret = -1;

3532
    qemuDriverLock(driver);
3533
    if (qemuCapsExtractVersion(driver->caps, &driver->qemuVersion) < 0)
3534
        goto cleanup;
3535

3536
    *version = driver->qemuVersion;
3537 3538 3539
    ret = 0;

cleanup:
3540
    qemuDriverUnlock(driver);
3541
    return ret;
D
Daniel P. Berrange 已提交
3542 3543
}

3544
static int qemudListDomains(virConnectPtr conn, int *ids, int nids) {
3545
    struct qemud_driver *driver = conn->privateData;
3546
    int n;
3547

3548
    qemuDriverLock(driver);
3549
    n = virDomainObjListGetActiveIDs(&driver->domains, ids, nids);
3550
    qemuDriverUnlock(driver);
3551

3552
    return n;
D
Daniel P. Berrange 已提交
3553
}
3554

3555
static int qemudNumDomains(virConnectPtr conn) {
3556
    struct qemud_driver *driver = conn->privateData;
3557
    int n;
3558

3559
    qemuDriverLock(driver);
3560
    n = virDomainObjListNumOfDomains(&driver->domains, 1);
3561
    qemuDriverUnlock(driver);
3562

3563
    return n;
D
Daniel P. Berrange 已提交
3564
}
3565

3566
static virDomainPtr qemudDomainCreate(virConnectPtr conn, const char *xml,
3567
                                      unsigned int flags) {
3568
    struct qemud_driver *driver = conn->privateData;
3569
    virDomainDefPtr def;
3570
    virDomainObjPtr vm = NULL;
3571
    virDomainPtr dom = NULL;
3572
    virDomainEventPtr event = NULL;
D
Daniel P. Berrange 已提交
3573

3574
    virCheckFlags(VIR_DOMAIN_START_PAUSED, NULL);
3575

3576
    qemuDriverLock(driver);
3577
    if (!(def = virDomainDefParseString(driver->caps, xml,
3578
                                        VIR_DOMAIN_XML_INACTIVE)))
3579
        goto cleanup;
3580

3581
    if (virSecurityManagerVerify(driver->securityManager, def) < 0)
3582 3583
        goto cleanup;

3584 3585
    if (virDomainObjIsDuplicate(&driver->domains, def, 1) < 0)
        goto cleanup;
3586

3587 3588 3589 3590 3591 3592
    if (qemudCanonicalizeMachine(driver, def) < 0)
        goto cleanup;

    if (qemuAssignPCIAddresses(def) < 0)
        goto cleanup;

3593
    if (!(vm = virDomainAssignDef(driver->caps,
3594
                                  &driver->domains,
3595
                                  def, false)))
3596 3597 3598
        goto cleanup;

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

3600 3601 3602
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup; /* XXXX free the 'vm' we created ? */

3603 3604
    if (qemudStartVMDaemon(conn, driver, vm, NULL,
                           (flags & VIR_DOMAIN_START_PAUSED) != 0,
3605
                           -1, NULL, VIR_VM_OP_CREATE) < 0) {
3606
        qemuDomainStartAudit(vm, "booted", false);
3607 3608 3609
        if (qemuDomainObjEndJob(vm) > 0)
            virDomainRemoveInactive(&driver->domains,
                                    vm);
3610
        vm = NULL;
3611
        goto cleanup;
D
Daniel P. Berrange 已提交
3612
    }
3613 3614 3615 3616

    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
3617
    qemuDomainStartAudit(vm, "booted", true);
D
Daniel P. Berrange 已提交
3618

3619
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
3620
    if (dom) dom->id = vm->def->id;
3621

3622 3623 3624
    if (vm &&
        qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
3625

3626 3627
cleanup:
    virDomainDefFree(def);
3628 3629
    if (vm)
        virDomainObjUnlock(vm);
3630 3631
    if (event)
        qemuDomainEventQueue(driver, event);
3632
    qemuDriverUnlock(driver);
3633
    return dom;
D
Daniel P. Berrange 已提交
3634 3635 3636
}


3637
static int qemudDomainSuspend(virDomainPtr dom) {
3638 3639 3640
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
3641
    virDomainEventPtr event = NULL;
3642
    qemuDomainObjPrivatePtr priv;
3643

3644
    qemuDriverLock(driver);
3645
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3646

D
Daniel P. Berrange 已提交
3647
    if (!vm) {
3648 3649
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
3650 3651
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
3652
        goto cleanup;
D
Daniel P. Berrange 已提交
3653
    }
D
Daniel P. Berrange 已提交
3654
    if (!virDomainObjIsActive(vm)) {
3655 3656
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
3657
        goto cleanup;
D
Daniel P. Berrange 已提交
3658
    }
3659 3660 3661

    priv = vm->privateData;

3662
    if (priv->jobActive == QEMU_JOB_MIGRATION_OUT) {
3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676
        if (vm->state != VIR_DOMAIN_PAUSED) {
            VIR_DEBUG("Requesting domain pause on %s",
                      vm->def->name);
            priv->jobSignals |= QEMU_JOB_SIGNAL_SUSPEND;
        }
        ret = 0;
        goto cleanup;
    } else {
        if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
            goto cleanup;

        if (!virDomainObjIsActive(vm)) {
            qemuReportError(VIR_ERR_OPERATION_INVALID,
                            "%s", _("domain is not running"));
3677
            goto endjob;
3678
        }
3679
        if (vm->state != VIR_DOMAIN_PAUSED) {
H
Hu Tao 已提交
3680
            if (doStopCPUs(driver, vm) < 0) {
3681
                goto endjob;
3682
            }
3683 3684 3685 3686 3687 3688 3689
            event = virDomainEventNewFromObj(vm,
                                             VIR_DOMAIN_EVENT_SUSPENDED,
                                             VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
        }
        if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
            goto endjob;
        ret = 0;
D
Daniel P. Berrange 已提交
3690
    }
3691

3692
endjob:
3693 3694
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
3695

3696
cleanup:
3697 3698
    if (vm)
        virDomainObjUnlock(vm);
3699

3700
    if (event)
3701
        qemuDomainEventQueue(driver, event);
3702
    qemuDriverUnlock(driver);
3703
    return ret;
D
Daniel P. Berrange 已提交
3704 3705 3706
}


3707
static int qemudDomainResume(virDomainPtr dom) {
3708 3709 3710
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
3711
    virDomainEventPtr event = NULL;
3712

3713
    qemuDriverLock(driver);
3714
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3715

D
Daniel P. Berrange 已提交
3716
    if (!vm) {
3717 3718
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
3719 3720
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
3721
        goto cleanup;
D
Daniel P. Berrange 已提交
3722
    }
3723 3724 3725 3726

    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;

D
Daniel P. Berrange 已提交
3727
    if (!virDomainObjIsActive(vm)) {
3728 3729
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
3730
        goto endjob;
D
Daniel P. Berrange 已提交
3731
    }
3732
    if (vm->state == VIR_DOMAIN_PAUSED) {
H
Hu Tao 已提交
3733
        if (doStartCPUs(driver, vm, dom->conn) < 0) {
3734
            if (virGetLastError() == NULL)
3735 3736
                qemuReportError(VIR_ERR_OPERATION_FAILED,
                                "%s", _("resume operation failed"));
3737
            goto endjob;
3738
        }
3739 3740 3741
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_RESUMED,
                                         VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
D
Daniel P. Berrange 已提交
3742
    }
3743
    if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
3744
        goto endjob;
3745 3746
    ret = 0;

3747
endjob:
3748 3749
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
3750

3751
cleanup:
3752 3753
    if (vm)
        virDomainObjUnlock(vm);
3754
    if (event)
3755
        qemuDomainEventQueue(driver, event);
3756
    qemuDriverUnlock(driver);
3757
    return ret;
D
Daniel P. Berrange 已提交
3758 3759 3760
}


3761
static int qemudDomainShutdown(virDomainPtr dom) {
3762 3763 3764
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
3765

3766
    qemuDriverLock(driver);
3767
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3768 3769
    qemuDriverUnlock(driver);

3770
    if (!vm) {
3771 3772
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
3773 3774
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
3775
        goto cleanup;
3776 3777
    }

3778 3779 3780
    if (qemuDomainObjBeginJob(vm) < 0)
        goto cleanup;

D
Daniel P. Berrange 已提交
3781
    if (!virDomainObjIsActive(vm)) {
3782 3783
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
3784
        goto endjob;
3785 3786
    }

3787
    qemuDomainObjPrivatePtr priv = vm->privateData;
3788 3789 3790
    qemuDomainObjEnterMonitor(vm);
    ret = qemuMonitorSystemPowerdown(priv->mon);
    qemuDomainObjExitMonitor(vm);
3791

3792
endjob:
3793 3794
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
3795

3796
cleanup:
3797 3798
    if (vm)
        virDomainObjUnlock(vm);
3799
    return ret;
3800 3801 3802
}


3803
static int qemudDomainDestroy(virDomainPtr dom) {
3804 3805 3806
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
3807
    virDomainEventPtr event = NULL;
3808

3809
    qemuDriverLock(driver);
3810
    vm  = virDomainFindByUUID(&driver->domains, dom->uuid);
D
Daniel P. Berrange 已提交
3811
    if (!vm) {
3812 3813
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
3814 3815
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
3816
        goto cleanup;
D
Daniel P. Berrange 已提交
3817
    }
3818 3819 3820 3821

    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;

D
Daniel P. Berrange 已提交
3822
    if (!virDomainObjIsActive(vm)) {
3823 3824
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
3825
        goto endjob;
3826
    }
3827

3828
    qemudShutdownVMDaemon(driver, vm, 0);
3829 3830 3831
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
3832 3833
    qemuDomainStopAudit(vm, "destroyed");

3834
    if (!vm->persistent) {
3835 3836 3837
        if (qemuDomainObjEndJob(vm) > 0)
            virDomainRemoveInactive(&driver->domains,
                                    vm);
3838 3839
        vm = NULL;
    }
3840 3841
    ret = 0;

3842
endjob:
3843 3844 3845
    if (vm &&
        qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
3846

3847
cleanup:
3848 3849
    if (vm)
        virDomainObjUnlock(vm);
3850 3851
    if (event)
        qemuDomainEventQueue(driver, event);
3852
    qemuDriverUnlock(driver);
3853
    return ret;
D
Daniel P. Berrange 已提交
3854 3855 3856
}


3857
static char *qemudDomainGetOSType(virDomainPtr dom) {
3858 3859 3860
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *type = NULL;
3861

3862
    qemuDriverLock(driver);
3863
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3864
    qemuDriverUnlock(driver);
3865
    if (!vm) {
3866 3867
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
3868 3869
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
3870
        goto cleanup;
3871 3872
    }

3873
    if (!(type = strdup(vm->def->os.type)))
3874
        virReportOOMError();
3875 3876

cleanup:
3877 3878
    if (vm)
        virDomainObjUnlock(vm);
3879 3880 3881
    return type;
}

3882 3883
/* Returns max memory in kb, 0 if error */
static unsigned long qemudDomainGetMaxMemory(virDomainPtr dom) {
3884 3885 3886
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    unsigned long ret = 0;
3887

3888
    qemuDriverLock(driver);
3889
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3890 3891
    qemuDriverUnlock(driver);

3892
    if (!vm) {
3893 3894
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
3895 3896
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
3897
        goto cleanup;
3898 3899
    }

3900
    ret = vm->def->mem.max_balloon;
3901 3902

cleanup:
3903 3904
    if (vm)
        virDomainObjUnlock(vm);
3905
    return ret;
3906 3907 3908
}

static int qemudDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
3909
    struct qemud_driver *driver = dom->conn->privateData;
3910
    qemuDomainObjPrivatePtr priv;
3911
    virDomainObjPtr vm;
3912
    int ret = -1, r;
3913

3914
    qemuDriverLock(driver);
3915
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3916
    qemuDriverUnlock(driver);
3917
    if (!vm) {
3918 3919
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
3920 3921
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
3922
        goto cleanup;
3923 3924
    }

3925
    if (newmem > vm->def->mem.max_balloon) {
3926 3927
        qemuReportError(VIR_ERR_INVALID_ARG,
                        "%s", _("cannot set memory higher than max memory"));
3928
        goto cleanup;
3929 3930
    }

3931 3932 3933
    if (qemuDomainObjBeginJob(vm) < 0)
        goto cleanup;

3934 3935 3936 3937 3938 3939
    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
        goto endjob;
    }

3940 3941 3942 3943 3944 3945
    priv = vm->privateData;
    qemuDomainObjEnterMonitor(vm);
    r = qemuMonitorSetBalloon(priv->mon, newmem);
    qemuDomainObjExitMonitor(vm);
    if (r < 0)
        goto endjob;
3946

3947 3948 3949 3950 3951
    /* Lack of balloon support is a fatal error */
    if (r == 0) {
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        "%s", _("cannot set memory of an active domain"));
        goto endjob;
3952
    }
3953

3954
    ret = 0;
3955
endjob:
3956 3957
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
3958

3959
cleanup:
3960 3961
    if (vm)
        virDomainObjUnlock(vm);
3962
    return ret;
3963 3964
}

3965
static int qemudDomainGetInfo(virDomainPtr dom,
3966
                              virDomainInfoPtr info) {
3967 3968 3969
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
3970 3971
    int err;
    unsigned long balloon;
3972

3973
    qemuDriverLock(driver);
3974
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3975
    qemuDriverUnlock(driver);
D
Daniel P. Berrange 已提交
3976
    if (!vm) {
3977 3978
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
3979 3980
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
3981
        goto cleanup;
D
Daniel P. Berrange 已提交
3982 3983
    }

3984
    info->state = vm->state;
D
Daniel P. Berrange 已提交
3985

D
Daniel P. Berrange 已提交
3986
    if (!virDomainObjIsActive(vm)) {
3987
        info->cpuTime = 0;
D
Daniel P. Berrange 已提交
3988
    } else {
3989
        if (qemudGetProcessInfo(&(info->cpuTime), NULL, vm->pid, 0) < 0) {
E
Eric Blake 已提交
3990 3991
            qemuReportError(VIR_ERR_OPERATION_FAILED, "%s",
                            _("cannot read cputime for domain"));
3992
            goto cleanup;
D
Daniel P. Berrange 已提交
3993 3994 3995
        }
    }

3996
    info->maxMem = vm->def->mem.max_balloon;
3997

D
Daniel P. Berrange 已提交
3998
    if (virDomainObjIsActive(vm)) {
3999
        qemuDomainObjPrivatePtr priv = vm->privateData;
4000 4001 4002

        if ((vm->def->memballoon != NULL) &&
            (vm->def->memballoon->model == VIR_DOMAIN_MEMBALLOON_MODEL_NONE)) {
4003
            info->memory = vm->def->mem.max_balloon;
4004
        } else if (!priv->jobActive) {
4005 4006
            if (qemuDomainObjBeginJob(vm) < 0)
                goto cleanup;
4007 4008 4009 4010 4011 4012 4013 4014 4015
            if (!virDomainObjIsActive(vm))
                err = 0;
            else {
                qemuDomainObjEnterMonitor(vm);
                err = qemuMonitorGetBalloonInfo(priv->mon, &balloon);
                qemuDomainObjExitMonitor(vm);
            }
            if (qemuDomainObjEndJob(vm) == 0) {
                vm = NULL;
4016 4017 4018
                goto cleanup;
            }

4019 4020
            if (err < 0)
                goto cleanup;
4021 4022
            if (err == 0)
                /* Balloon not supported, so maxmem is always the allocation */
4023
                info->memory = vm->def->mem.max_balloon;
4024 4025 4026
            else
                info->memory = balloon;
        } else {
4027
            info->memory = vm->def->mem.cur_balloon;
4028
        }
4029
    } else {
4030
        info->memory = vm->def->mem.cur_balloon;
4031 4032
    }

4033
    info->nrVirtCpu = vm->def->vcpus;
4034 4035 4036
    ret = 0;

cleanup:
4037 4038
    if (vm)
        virDomainObjUnlock(vm);
4039
    return ret;
D
Daniel P. Berrange 已提交
4040 4041 4042
}


4043 4044 4045 4046 4047 4048 4049 4050 4051
/** qemuDomainMigrateOffline:
 * Pause domain for non-live migration.
 */
static int
qemuDomainMigrateOffline(struct qemud_driver *driver,
                         virDomainObjPtr vm)
{
    int ret;

H
Hu Tao 已提交
4052
    ret = doStopCPUs(driver, vm);
4053 4054 4055 4056 4057 4058 4059 4060
    if (ret == 0) {
        virDomainEventPtr event;

        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_SUSPENDED,
                                         VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED);
        if (event)
            qemuDomainEventQueue(driver, event);
H
Hu Tao 已提交
4061
    }
4062 4063 4064 4065 4066

    return ret;
}


4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080
static int
qemuDomainWaitForMigrationComplete(struct qemud_driver *driver, virDomainObjPtr vm)
{
    int ret = -1;
    int status;
    unsigned long long memProcessed;
    unsigned long long memRemaining;
    unsigned long long memTotal;
    qemuDomainObjPrivatePtr priv = vm->privateData;

    priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED;

    while (priv->jobInfo.type == VIR_DOMAIN_JOB_UNBOUNDED) {
        /* Poll every 50ms for progress & to allow cancellation */
4081
        struct timespec ts = { .tv_sec = 0, .tv_nsec = 50 * 1000 * 1000ull };
4082 4083
        struct timeval now;
        int rc;
O
Osier Yang 已提交
4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099
        const char *job;

        switch (priv->jobActive) {
            case QEMU_JOB_MIGRATION_OUT:
                job = _("migration job");
                break;
            case QEMU_JOB_SAVE:
                job = _("domain save job");
                break;
            case QEMU_JOB_DUMP:
                job = _("domain core dump job");
                break;
            default:
                job = _("job");
        }

4100

4101
        if (!virDomainObjIsActive(vm)) {
E
Eric Blake 已提交
4102
            qemuReportError(VIR_ERR_INTERNAL_ERROR, _("%s: %s"),
O
Osier Yang 已提交
4103
                            job, _("guest unexpectedly quit"));
4104 4105 4106
            goto cleanup;
        }

4107 4108
        if (priv->jobSignals & QEMU_JOB_SIGNAL_CANCEL) {
            priv->jobSignals ^= QEMU_JOB_SIGNAL_CANCEL;
O
Osier Yang 已提交
4109
            VIR_DEBUG0("Cancelling job at client request");
4110 4111 4112 4113
            qemuDomainObjEnterMonitorWithDriver(driver, vm);
            rc = qemuMonitorMigrateCancel(priv->mon);
            qemuDomainObjExitMonitorWithDriver(driver, vm);
            if (rc < 0) {
O
Osier Yang 已提交
4114
                VIR_WARN0("Unable to cancel job");
4115
            }
4116 4117 4118 4119 4120
        } else if (priv->jobSignals & QEMU_JOB_SIGNAL_SUSPEND) {
            priv->jobSignals ^= QEMU_JOB_SIGNAL_SUSPEND;
            VIR_DEBUG0("Pausing domain for non-live migration");
            if (qemuDomainMigrateOffline(driver, vm) < 0)
                VIR_WARN0("Unable to pause domain");
4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131
        } else if (priv->jobSignals & QEMU_JOB_SIGNAL_MIGRATE_DOWNTIME) {
            unsigned long long ms = priv->jobSignalsData.migrateDowntime;

            priv->jobSignals ^= QEMU_JOB_SIGNAL_MIGRATE_DOWNTIME;
            priv->jobSignalsData.migrateDowntime = 0;
            VIR_DEBUG("Setting migration downtime to %llums", ms);
            qemuDomainObjEnterMonitorWithDriver(driver, vm);
            rc = qemuMonitorSetMigrationDowntime(priv->mon, ms);
            qemuDomainObjExitMonitorWithDriver(driver, vm);
            if (rc < 0)
                VIR_WARN0("Unable to set migration downtime");
4132 4133
        }

4134 4135 4136 4137
        /* Repeat check because the job signals might have caused
         * guest to die
         */
        if (!virDomainObjIsActive(vm)) {
E
Eric Blake 已提交
4138
            qemuReportError(VIR_ERR_INTERNAL_ERROR, _("%s: %s"),
O
Osier Yang 已提交
4139
                            job, _("guest unexpectedly quit"));
4140 4141 4142
            goto cleanup;
        }

4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161
        qemuDomainObjEnterMonitorWithDriver(driver, vm);
        rc = qemuMonitorGetMigrationStatus(priv->mon,
                                           &status,
                                           &memProcessed,
                                           &memRemaining,
                                           &memTotal);
        qemuDomainObjExitMonitorWithDriver(driver, vm);

        if (rc < 0) {
            priv->jobInfo.type = VIR_DOMAIN_JOB_FAILED;
            goto cleanup;
        }

        if (gettimeofday(&now, NULL) < 0) {
            priv->jobInfo.type = VIR_DOMAIN_JOB_FAILED;
            virReportSystemError(errno, "%s",
                                 _("cannot get time of day"));
            goto cleanup;
        }
4162
        priv->jobInfo.timeElapsed = timeval_to_ms(now) - priv->jobStart;
4163 4164 4165 4166 4167

        switch (status) {
        case QEMU_MONITOR_MIGRATION_STATUS_INACTIVE:
            priv->jobInfo.type = VIR_DOMAIN_JOB_NONE;
            qemuReportError(VIR_ERR_OPERATION_FAILED,
E
Eric Blake 已提交
4168
                            _("%s: %s"), job, _("is not active"));
4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188
            break;

        case QEMU_MONITOR_MIGRATION_STATUS_ACTIVE:
            priv->jobInfo.dataTotal = memTotal;
            priv->jobInfo.dataRemaining = memRemaining;
            priv->jobInfo.dataProcessed = memProcessed;

            priv->jobInfo.memTotal = memTotal;
            priv->jobInfo.memRemaining = memRemaining;
            priv->jobInfo.memProcessed = memProcessed;
            break;

        case QEMU_MONITOR_MIGRATION_STATUS_COMPLETED:
            priv->jobInfo.type = VIR_DOMAIN_JOB_COMPLETED;
            ret = 0;
            break;

        case QEMU_MONITOR_MIGRATION_STATUS_ERROR:
            priv->jobInfo.type = VIR_DOMAIN_JOB_FAILED;
            qemuReportError(VIR_ERR_OPERATION_FAILED,
E
Eric Blake 已提交
4189
                            _("%s: %s"), job, _("unexpectedly failed"));
4190 4191 4192 4193 4194
            break;

        case QEMU_MONITOR_MIGRATION_STATUS_CANCELLED:
            priv->jobInfo.type = VIR_DOMAIN_JOB_CANCELLED;
            qemuReportError(VIR_ERR_OPERATION_FAILED,
E
Eric Blake 已提交
4195
                            _("%s: %s"), job, _("canceled by client"));
4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212
            break;
        }

        virDomainObjUnlock(vm);
        qemuDriverUnlock(driver);

        nanosleep(&ts, NULL);

        qemuDriverLock(driver);
        virDomainObjLock(vm);
    }

cleanup:
    return ret;
}


4213
#define QEMUD_SAVE_MAGIC "LibvirtQemudSave"
4214 4215 4216
#define QEMUD_SAVE_VERSION 2

enum qemud_save_formats {
4217 4218 4219
    QEMUD_SAVE_FORMAT_RAW = 0,
    QEMUD_SAVE_FORMAT_GZIP = 1,
    QEMUD_SAVE_FORMAT_BZIP2 = 2,
4220 4221
    /*
     * Deprecated by xz and never used as part of a release
4222
     * QEMUD_SAVE_FORMAT_LZMA
4223 4224
     */
    QEMUD_SAVE_FORMAT_XZ = 3,
4225
    QEMUD_SAVE_FORMAT_LZOP = 4,
4226 4227 4228
    /* Note: add new members only at the end.
       These values are used in the on-disk format.
       Do not change or re-use numbers. */
4229 4230

    QEMUD_SAVE_FORMAT_LAST
4231
};
4232

4233 4234 4235 4236 4237
VIR_ENUM_DECL(qemudSaveCompression)
VIR_ENUM_IMPL(qemudSaveCompression, QEMUD_SAVE_FORMAT_LAST,
              "raw",
              "gzip",
              "bzip2",
4238 4239
              "xz",
              "lzop")
4240

4241 4242 4243 4244 4245
struct qemud_save_header {
    char magic[sizeof(QEMUD_SAVE_MAGIC)-1];
    int version;
    int xml_len;
    int was_running;
4246 4247
    int compressed;
    int unused[15];
4248 4249
};

4250 4251 4252 4253 4254 4255 4256
struct fileOpHookData {
    virDomainPtr dom;
    const char *path;
    char *xml;
    struct qemud_save_header *header;
};

4257
/* return -errno on failure, or 0 on success */
4258 4259 4260 4261 4262
static int qemudDomainSaveFileOpHook(int fd, void *data) {
    struct fileOpHookData *hdata = data;
    int ret = 0;

    if (safewrite(fd, hdata->header, sizeof(*hdata->header)) != sizeof(*hdata->header)) {
4263
        ret = -errno;
4264
        qemuReportError(VIR_ERR_OPERATION_FAILED,
4265 4266
                        _("failed to write header to domain save file '%s'"),
                        hdata->path);
4267 4268 4269 4270
        goto endjob;
    }

    if (safewrite(fd, hdata->xml, hdata->header->xml_len) != hdata->header->xml_len) {
4271
        ret = -errno;
4272 4273 4274 4275 4276 4277 4278 4279
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                         _("failed to write xml to '%s'"), hdata->path);
        goto endjob;
    }
endjob:
    return ret;
}

4280 4281 4282
/* this internal function expects the driver lock to already be held on entry */
static int qemudDomainSaveFlag(struct qemud_driver *driver, virDomainPtr dom,
                               virDomainObjPtr vm, const char *path,
4283
                               int compressed)
4284
{
4285
    char *xml = NULL;
4286
    struct qemud_save_header header;
4287 4288
    struct fileOpHookData hdata;
    int bypassSecurityDriver = 0;
4289
    int ret = -1;
4290
    int rc;
4291
    virDomainEventPtr event = NULL;
4292
    qemuDomainObjPrivatePtr priv;
4293 4294
    struct stat sb;
    int is_reg = 0;
4295
    unsigned long long offset;
4296
    virCgroupPtr cgroup = NULL;
4297 4298 4299 4300 4301

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

4302
    header.compressed = compressed;
4303

4304
    priv = vm->privateData;
4305

4306 4307 4308
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;

D
Daniel P. Berrange 已提交
4309
    if (!virDomainObjIsActive(vm)) {
4310 4311
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
4312
        goto endjob;
D
Daniel P. Berrange 已提交
4313
    }
4314

O
Osier Yang 已提交
4315 4316
    priv->jobActive = QEMU_JOB_SAVE;

4317 4318 4319
    memset(&priv->jobInfo, 0, sizeof(priv->jobInfo));
    priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED;

4320 4321 4322
    /* Pause */
    if (vm->state == VIR_DOMAIN_RUNNING) {
        header.was_running = 1;
H
Hu Tao 已提交
4323
        if (doStopCPUs(driver, vm) < 0)
4324
            goto endjob;
4325 4326 4327 4328 4329 4330

        if (!virDomainObjIsActive(vm)) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("guest unexpectedly quit"));
            goto endjob;
        }
4331 4332 4333
    }

    /* Get XML for the domain */
4334
    xml = virDomainDefFormat(vm->def, VIR_DOMAIN_XML_SECURE);
4335
    if (!xml) {
4336 4337
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("failed to get domain xml"));
4338
        goto endjob;
4339 4340 4341
    }
    header.xml_len = strlen(xml) + 1;

4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354
    /* path might be a pre-existing block dev, in which case
     * we need to skip the create step, and also avoid unlink
     * in the failure case */
    if (stat(path, &sb) < 0) {
        /* Avoid throwing an error here, since it is possible
         * that with NFS we can't actually stat() the file.
         * The subsequent codepaths will still raise an error
         * if a truely fatal problem is hit */
        is_reg = 1;
    } else {
        is_reg = S_ISREG(sb.st_mode);
    }

4355 4356 4357 4358 4359
    offset = sizeof(header) + header.xml_len;

    /* Due to way we append QEMU state on our header with dd,
     * we need to ensure there's a 512 byte boundary. Unfortunately
     * we don't have an explicit offset in the header, so we fake
4360 4361
     * it by padding the XML string with NULLs.
     */
4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374
    if (offset % QEMU_MONITOR_MIGRATE_TO_FILE_BS) {
        unsigned long long pad =
            QEMU_MONITOR_MIGRATE_TO_FILE_BS -
            (offset % QEMU_MONITOR_MIGRATE_TO_FILE_BS);

        if (VIR_REALLOC_N(xml, header.xml_len + pad) < 0) {
            virReportOOMError();
            goto endjob;
        }
        memset(xml + header.xml_len, 0, pad);
        offset += pad;
        header.xml_len += pad;
    }
4375

4376 4377 4378 4379 4380 4381
    /* Setup hook data needed by virFileOperation hook function */
    hdata.dom = dom;
    hdata.path = path;
    hdata.xml = xml;
    hdata.header = &header;

4382 4383
    /* Write header to file, followed by XML */

4384
    /* First try creating the file as root */
4385 4386 4387 4388
    if (!is_reg) {
        int fd = open(path, O_WRONLY | O_TRUNC);
        if (fd < 0) {
            virReportSystemError(errno, _("unable to open %s"), path);
4389 4390
            goto endjob;
        }
4391
        if (qemudDomainSaveFileOpHook(fd, &hdata) < 0) {
4392
            VIR_FORCE_CLOSE(fd);
4393 4394
            goto endjob;
        }
4395
        if (VIR_CLOSE(fd) < 0) {
4396
            virReportSystemError(errno, _("unable to close %s"), path);
4397 4398
            goto endjob;
        }
4399 4400 4401 4402 4403
    } else {
        if ((rc = virFileOperation(path, O_CREAT|O_TRUNC|O_WRONLY,
                                  S_IRUSR|S_IWUSR,
                                  getuid(), getgid(),
                                  qemudDomainSaveFileOpHook, &hdata,
4404
                                  0)) < 0) {
4405
            /* If we failed as root, and the error was permission-denied
4406 4407
               (EACCES or EPERM), assume it's on a network-connected share
               where root access is restricted (eg, root-squashed NFS). If the
4408 4409 4410 4411
               qemu user (driver->user) is non-root, just set a flag to
               bypass security driver shenanigans, and retry the operation
               after doing setuid to qemu user */

4412
            if (((rc != -EACCES) && (rc != -EPERM)) ||
4413
                driver->user == getuid()) {
4414
                virReportSystemError(-rc, _("Failed to create domain save file '%s'"),
4415 4416 4417
                                     path);
                goto endjob;
            }
4418

4419
            /* On Linux we can also verify the FS-type of the directory. */
4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437
            switch (virStorageFileIsSharedFS(path)) {
                case 1:
                   /* it was on a network share, so we'll continue
                    * as outlined above
                    */
                   break;

                case -1:
                   virReportSystemError(errno,
                                        _("Failed to create domain save file "
                                          "'%s': couldn't determine fs type"),
                                        path);
                   goto endjob;
                   break;

                case 0:
                default:
                   /* local file - log the error returned by virFileOperation */
4438
                   virReportSystemError(-rc,
4439 4440 4441 4442
                                        _("Failed to create domain save file '%s'"),
                                        path);
                   goto endjob;
                   break;
4443 4444 4445

            }

4446
            /* Retry creating the file as driver->user */
4447

4448 4449 4450 4451
            if ((rc = virFileOperation(path, O_CREAT|O_TRUNC|O_WRONLY,
                                       S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP,
                                       driver->user, driver->group,
                                       qemudDomainSaveFileOpHook, &hdata,
4452 4453
                                       VIR_FILE_OP_AS_UID)) < 0) {
                virReportSystemError(-rc, _("Error from child process creating '%s'"),
4454
                                 path);
4455 4456
                goto endjob;
            }
4457

4458 4459 4460
            /* Since we had to setuid to create the file, and the fstype
               is NFS, we assume it's a root-squashing NFS share, and that
               the security driver stuff would have failed anyway */
4461

4462 4463
            bypassSecurityDriver = 1;
        }
4464
    }
4465

4466

4467 4468 4469 4470
    if (!is_reg &&
        qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
        if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) != 0) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
4471
                            _("Unable to find cgroup for %s"),
4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483
                            vm->def->name);
            goto endjob;
        }
        rc = virCgroupAllowDevicePath(cgroup, path);
        if (rc != 0) {
            virReportSystemError(-rc,
                                 _("Unable to allow device %s for %s"),
                                 path, vm->def->name);
            goto endjob;
        }
    }

4484
    if ((!bypassSecurityDriver) &&
4485 4486
        virSecurityManagerSetSavedStateLabel(driver->securityManager,
                                             vm, path) < 0)
4487 4488
        goto endjob;

4489 4490
    if (header.compressed == QEMUD_SAVE_FORMAT_RAW) {
        const char *args[] = { "cat", NULL };
M
Matthias Bolte 已提交
4491
        qemuDomainObjEnterMonitorWithDriver(driver, vm);
4492 4493 4494
        rc = qemuMonitorMigrateToFile(priv->mon,
                                      QEMU_MONITOR_MIGRATE_BACKGROUND,
                                      args, path, offset);
M
Matthias Bolte 已提交
4495
        qemuDomainObjExitMonitorWithDriver(driver, vm);
4496
    } else {
4497
        const char *prog = qemudSaveCompressionTypeToString(header.compressed);
4498 4499 4500 4501 4502
        const char *args[] = {
            prog,
            "-c",
            NULL
        };
M
Matthias Bolte 已提交
4503
        qemuDomainObjEnterMonitorWithDriver(driver, vm);
4504 4505 4506
        rc = qemuMonitorMigrateToFile(priv->mon,
                                      QEMU_MONITOR_MIGRATE_BACKGROUND,
                                      args, path, offset);
M
Matthias Bolte 已提交
4507
        qemuDomainObjExitMonitorWithDriver(driver, vm);
4508 4509
    }

4510 4511 4512 4513 4514
    if (rc < 0)
        goto endjob;

    rc = qemuDomainWaitForMigrationComplete(driver, vm);

4515
    if (rc < 0)
4516
        goto endjob;
4517

4518
    if ((!bypassSecurityDriver) &&
4519 4520
        virSecurityManagerRestoreSavedStateLabel(driver->securityManager,
                                                 vm, path) < 0)
4521
        VIR_WARN("failed to restore save state label on %s", path);
4522

4523 4524
    if (cgroup != NULL) {
        rc = virCgroupDenyDevicePath(cgroup, path);
4525 4526 4527
        if (rc != 0)
            VIR_WARN("Unable to deny device %s for %s %d",
                     path, vm->def->name, rc);
4528 4529
    }

4530 4531
    ret = 0;

4532
    /* Shut it down */
4533
    qemudShutdownVMDaemon(driver, vm, 0);
4534
    qemuDomainStopAudit(vm, "saved");
4535 4536 4537
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
4538
    if (!vm->persistent) {
4539 4540 4541
        if (qemuDomainObjEndJob(vm) > 0)
            virDomainRemoveInactive(&driver->domains,
                                    vm);
4542 4543
        vm = NULL;
    }
4544

4545
endjob:
4546
    if (vm) {
4547
        if (ret != 0) {
4548
            if (header.was_running && virDomainObjIsActive(vm)) {
H
Hu Tao 已提交
4549
                rc = doStartCPUs(driver, vm, dom->conn);
4550 4551 4552
                if (rc < 0)
                    VIR_WARN0("Unable to resume guest CPUs after save failure");
            }
4553

4554 4555 4556 4557 4558
            if (cgroup != NULL) {
                rc = virCgroupDenyDevicePath(cgroup, path);
                if (rc != 0)
                    VIR_WARN("Unable to deny device %s for %s: %d",
                             path, vm->def->name, rc);
4559
            }
4560 4561

            if ((!bypassSecurityDriver) &&
4562 4563
                virSecurityManagerRestoreSavedStateLabel(driver->securityManager,
                                                         vm, path) < 0)
4564
                VIR_WARN("failed to restore save state label on %s", path);
4565 4566
        }

4567
        if (qemuDomainObjEndJob(vm) == 0)
4568
            vm = NULL;
4569
    }
4570

4571 4572
cleanup:
    VIR_FREE(xml);
4573
    if (ret != 0 && is_reg)
4574
        unlink(path);
4575 4576
    if (event)
        qemuDomainEventQueue(driver, event);
4577
    virCgroupFree(&cgroup);
4578
    return ret;
D
Daniel P. Berrange 已提交
4579 4580
}

4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596
/* Returns true if a compression program is available in PATH */
static bool qemudCompressProgramAvailable(enum qemud_save_formats compress)
{
    const char *prog;
    char *c;

    if (compress == QEMUD_SAVE_FORMAT_RAW)
        return true;
    prog = qemudSaveCompressionTypeToString(compress);
    c = virFindFileInPath(prog);
    if (!c)
        return false;
    VIR_FREE(c);
    return true;
}

4597 4598 4599 4600
static int qemudDomainSave(virDomainPtr dom, const char *path)
{
    struct qemud_driver *driver = dom->conn->privateData;
    int compressed;
4601 4602 4603 4604
    int ret = -1;
    virDomainObjPtr vm = NULL;

    qemuDriverLock(driver);
4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615

    if (driver->saveImageFormat == NULL)
        compressed = QEMUD_SAVE_FORMAT_RAW;
    else {
        compressed = qemudSaveCompressionTypeFromString(driver->saveImageFormat);
        if (compressed < 0) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            "%s", _("Invalid save image format specified "
                                    "in configuration file"));
            return -1;
        }
4616 4617 4618 4619 4620 4621
        if (!qemudCompressProgramAvailable(compressed)) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            "%s", _("Compression program for image format "
                                    "in configuration file isn't available"));
            return -1;
        }
4622 4623
    }

4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    ret = qemudDomainSaveFlag(driver, dom, vm, path, compressed);

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);

    return ret;
4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663
}

static char *
qemuDomainManagedSavePath(struct qemud_driver *driver, virDomainObjPtr vm) {
    char *ret;

    if (virAsprintf(&ret, "%s/%s.save", driver->saveDir, vm->def->name) < 0) {
        virReportOOMError();
        return(NULL);
    }

    return(ret);
}

static int
qemuDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
    char *name = NULL;
    int ret = -1;
    int compressed;

4664
    virCheckFlags(0, -1);
4665 4666 4667 4668 4669 4670 4671 4672

    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
4673
        goto cleanup;
4674 4675
    }

4676 4677 4678 4679 4680 4681
    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
        goto cleanup;
    }

4682 4683
    name = qemuDomainManagedSavePath(driver, vm);
    if (name == NULL)
4684
        goto cleanup;
4685 4686 4687 4688

    VIR_DEBUG("Saving state to %s", name);

    compressed = QEMUD_SAVE_FORMAT_RAW;
4689
    ret = qemudDomainSaveFlag(driver, dom, vm, name, compressed);
4690 4691 4692 4693 4694

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
4695 4696 4697
    VIR_FREE(name);

    return ret;
4698 4699 4700 4701 4702 4703 4704 4705 4706 4707
}

static int
qemuDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
    int ret = -1;
    char *name = NULL;

4708
    virCheckFlags(0, -1);
4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741

    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    name = qemuDomainManagedSavePath(driver, vm);
    if (name == NULL)
        goto cleanup;

    ret = virFileExists(name);

cleanup:
    VIR_FREE(name);
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return ret;
}

static int
qemuDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
    int ret = -1;
    char *name = NULL;

4742
    virCheckFlags(0, -1);
4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766

    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    name = qemuDomainManagedSavePath(driver, vm);
    if (name == NULL)
        goto cleanup;

    ret = unlink(name);

cleanup:
    VIR_FREE(name);
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return ret;
}
D
Daniel P. Berrange 已提交
4767

H
Hu Tao 已提交
4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792
static int doCoreDump(struct qemud_driver *driver,
                      virDomainObjPtr vm,
                      const char *path,
                      enum qemud_save_formats compress)
{
    int fd = -1;
    int ret = -1;
    qemuDomainObjPrivatePtr priv;

    priv = vm->privateData;

    /* Create an empty file with appropriate ownership.  */
    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("failed to create '%s'"), path);
        goto cleanup;
    }

    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno,
                             _("unable to save file %s"),
                             path);
        goto cleanup;
    }

4793 4794
    if (virSecurityManagerSetSavedStateLabel(driver->securityManager,
                                             vm, path) < 0)
H
Hu Tao 已提交
4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825
        goto cleanup;

    qemuDomainObjEnterMonitorWithDriver(driver, vm);
    if (compress == QEMUD_SAVE_FORMAT_RAW) {
        const char *args[] = {
            "cat",
            NULL,
        };
        ret = qemuMonitorMigrateToFile(priv->mon,
                                       QEMU_MONITOR_MIGRATE_BACKGROUND,
                                       args, path, 0);
    } else {
        const char *prog = qemudSaveCompressionTypeToString(compress);
        const char *args[] = {
            prog,
            "-c",
            NULL,
        };
        ret = qemuMonitorMigrateToFile(priv->mon,
                                       QEMU_MONITOR_MIGRATE_BACKGROUND,
                                       args, path, 0);
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);
    if (ret < 0)
        goto cleanup;

    ret = qemuDomainWaitForMigrationComplete(driver, vm);

    if (ret < 0)
        goto cleanup;

4826 4827
    if (virSecurityManagerRestoreSavedStateLabel(driver->securityManager,
                                                 vm, path) < 0)
H
Hu Tao 已提交
4828 4829 4830 4831 4832 4833 4834 4835
        goto cleanup;

cleanup:
    if (ret != 0)
        unlink(path);
    return ret;
}

4836 4837 4838 4839 4840
static enum qemud_save_formats
getCompressionType(struct qemud_driver *driver)
{
    int compress = QEMUD_SAVE_FORMAT_RAW;

4841 4842 4843 4844 4845 4846 4847
    /*
     * We reuse "save" flag for "dump" here. Then, we can support the same
     * format in "save" and "dump".
     */
    if (driver->dumpImageFormat) {
        compress = qemudSaveCompressionTypeFromString(driver->dumpImageFormat);
        if (compress < 0) {
4848 4849 4850 4851
            qemuReportError(VIR_ERR_OPERATION_FAILED, "%s",
                            _("Invalid dump image format specified in "
                              "configuration file, using raw"));
            return QEMUD_SAVE_FORMAT_RAW;
4852
        }
4853 4854 4855
        if (!qemudCompressProgramAvailable(compress)) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            "%s", _("Compression program for dump image format "
4856 4857 4858
                                    "in configuration file isn't available, "
                                    "using raw"));
            return QEMUD_SAVE_FORMAT_RAW;
4859
        }
4860
    }
4861 4862 4863 4864 4865 4866 4867 4868 4869
    return compress;
}

static int qemudDomainCoreDump(virDomainPtr dom,
                               const char *path,
                               int flags ATTRIBUTE_UNUSED) {
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int resume = 0, paused = 0;
H
Hu Tao 已提交
4870
    int ret = -1;
4871 4872 4873
    virDomainEventPtr event = NULL;
    qemuDomainObjPrivatePtr priv;

P
Paolo Bonzini 已提交
4874 4875 4876 4877 4878 4879
    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);

    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
4880 4881
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
P
Paolo Bonzini 已提交
4882 4883
        goto cleanup;
    }
4884
    priv = vm->privateData;
P
Paolo Bonzini 已提交
4885

4886
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
4887 4888
        goto cleanup;

D
Daniel P. Berrange 已提交
4889
    if (!virDomainObjIsActive(vm)) {
4890 4891
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
4892
        goto endjob;
P
Paolo Bonzini 已提交
4893 4894
    }

O
Osier Yang 已提交
4895 4896
    priv->jobActive = QEMU_JOB_DUMP;

P
Paolo Bonzini 已提交
4897 4898
    /* Migrate will always stop the VM, so the resume condition is
       independent of whether the stop command is issued.  */
P
Paolo Bonzini 已提交
4899 4900 4901
    resume = (vm->state == VIR_DOMAIN_RUNNING);

    /* Pause domain for non-live dump */
P
Paolo Bonzini 已提交
4902
    if (!(flags & VIR_DUMP_LIVE) && vm->state == VIR_DOMAIN_RUNNING) {
H
Hu Tao 已提交
4903
        if (doStopCPUs(driver, vm) < 0)
4904
            goto endjob;
P
Paolo Bonzini 已提交
4905
        paused = 1;
4906 4907 4908 4909 4910 4911

        if (!virDomainObjIsActive(vm)) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("guest unexpectedly quit"));
            goto endjob;
        }
P
Paolo Bonzini 已提交
4912 4913
    }

H
Hu Tao 已提交
4914
    ret = doCoreDump(driver, vm, path, getCompressionType(driver));
4915 4916 4917 4918
    if (ret < 0)
        goto endjob;

    paused = 1;
4919 4920

endjob:
4921
    if ((ret == 0) && (flags & VIR_DUMP_CRASH)) {
4922
        qemudShutdownVMDaemon(driver, vm, 0);
4923
        qemuDomainStopAudit(vm, "crashed");
4924 4925 4926 4927 4928
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
    }

P
Paolo Bonzini 已提交
4929 4930 4931
    /* Since the monitor is always attached to a pty for libvirt, it
       will support synchronous operations so we always get here after
       the migration is complete.  */
4932
    else if (resume && paused && virDomainObjIsActive(vm)) {
H
Hu Tao 已提交
4933
        if (doStartCPUs(driver, vm, dom->conn) < 0) {
4934
            if (virGetLastError() == NULL)
4935 4936
                qemuReportError(VIR_ERR_OPERATION_FAILED,
                                "%s", _("resuming after dump failed"));
P
Paolo Bonzini 已提交
4937 4938
        }
    }
4939

4940 4941
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
4942
    else if ((ret == 0) && (flags & VIR_DUMP_CRASH) && !vm->persistent) {
4943 4944 4945 4946
        virDomainRemoveInactive(&driver->domains,
                                vm);
        vm = NULL;
    }
4947 4948

cleanup:
P
Paolo Bonzini 已提交
4949 4950
    if (vm)
        virDomainObjUnlock(vm);
4951 4952
    if (event)
        qemuDomainEventQueue(driver, event);
4953
    qemuDriverUnlock(driver);
P
Paolo Bonzini 已提交
4954 4955 4956
    return ret;
}

H
Hu Tao 已提交
4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993
static void processWatchdogEvent(void *data, void *opaque)
{
    int ret;
    struct watchdogEvent *wdEvent = data;
    struct qemud_driver *driver = opaque;

    switch (wdEvent->action) {
    case VIR_DOMAIN_WATCHDOG_ACTION_DUMP:
        {
            char *dumpfile;
            int i;

            i = virAsprintf(&dumpfile, "%s/%s-%u",
                            driver->autoDumpPath,
                            wdEvent->vm->def->name,
                            (unsigned int)time(NULL));

            qemuDriverLock(driver);
            virDomainObjLock(wdEvent->vm);

            if (qemuDomainObjBeginJobWithDriver(driver, wdEvent->vm) < 0)
                break;

            if (!virDomainObjIsActive(wdEvent->vm)) {
                qemuReportError(VIR_ERR_OPERATION_INVALID,
                                "%s", _("domain is not running"));
                break;
            }

            ret = doCoreDump(driver,
                             wdEvent->vm,
                             dumpfile,
                             getCompressionType(driver));
            if (ret < 0)
                qemuReportError(VIR_ERR_OPERATION_FAILED,
                                "%s", _("Dump failed"));

H
Hu Tao 已提交
4994
            ret = doStartCPUs(driver, wdEvent->vm, NULL);
H
Hu Tao 已提交
4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011

            if (ret < 0)
                qemuReportError(VIR_ERR_OPERATION_FAILED,
                                "%s", _("Resuming after dump failed"));

            if (qemuDomainObjEndJob(wdEvent->vm) > 0)
                virDomainObjUnlock(wdEvent->vm);

            qemuDriverUnlock(driver);

            VIR_FREE(dumpfile);
        }
        break;
    }

    VIR_FREE(wdEvent);
}
P
Paolo Bonzini 已提交
5012

5013 5014 5015 5016 5017 5018
static int qemudDomainHotplugVcpus(virDomainObjPtr vm, unsigned int nvcpus)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int i, rc;
    int ret = -1;

5019 5020
    qemuDomainObjEnterMonitor(vm);

5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050
    /* We need different branches here, because we want to offline
     * in reverse order to onlining, so any partial fail leaves us in a
     * reasonably sensible state */
    if (nvcpus > vm->def->vcpus) {
        for (i = vm->def->vcpus ; i < nvcpus ; i++) {
            /* Online new CPU */
            rc = qemuMonitorSetCPU(priv->mon, i, 1);
            if (rc == 0)
                goto unsupported;
            if (rc < 0)
                goto cleanup;

            vm->def->vcpus++;
        }
    } else {
        for (i = vm->def->vcpus - 1 ; i >= nvcpus ; i--) {
            /* Offline old CPU */
            rc = qemuMonitorSetCPU(priv->mon, i, 0);
            if (rc == 0)
                goto unsupported;
            if (rc < 0)
                goto cleanup;

            vm->def->vcpus--;
        }
    }

    ret = 0;

cleanup:
5051
    qemuDomainObjExitMonitor(vm);
5052 5053 5054 5055 5056 5057 5058 5059 5060
    return ret;

unsupported:
    qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("cannot change vcpu count of this domain"));
    goto cleanup;
}


5061 5062 5063 5064
static int
qemudDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                         unsigned int flags)
{
5065 5066
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
5067
    virDomainDefPtr persistentDef;
5068 5069
    const char * type;
    int max;
5070
    int ret = -1;
5071

5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
    if ((flags & (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG)) == 0 ||
        (flags & (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_LIVE)) ==
         (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_LIVE)) {
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("invalid flag combination: (0x%x)"), flags);
        return -1;
    }
    if (!nvcpus || (unsigned short) nvcpus != nvcpus) {
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("argument out of range: %d"), nvcpus);
5088 5089 5090
        return -1;
    }

5091
    qemuDriverLock(driver);
5092
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
5093 5094
    qemuDriverUnlock(driver);

5095
    if (!vm) {
5096 5097
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
5098 5099
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
5100
        goto cleanup;
5101 5102
    }

5103 5104 5105
    if (qemuDomainObjBeginJob(vm) < 0)
        goto cleanup;

5106
    if (!virDomainObjIsActive(vm) && (flags & VIR_DOMAIN_VCPU_LIVE)) {
5107 5108
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                         "%s", _("domain is not running"));
5109
        goto endjob;
5110 5111
    }

5112 5113 5114 5115 5116 5117
    if (!vm->persistent && (flags & VIR_DOMAIN_VCPU_CONFIG)) {
        qemuReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("cannot change persistent config of a transient domain"));
        goto endjob;
    }

5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130
    if (!(type = virDomainVirtTypeToString(vm->def->virtType))) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("unknown virt type in domain definition '%d'"),
                        vm->def->virtType);
        goto endjob;
    }

    if ((max = qemudGetMaxVCPUs(NULL, type)) < 0) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("could not determine max vcpus for the domain"));
        goto endjob;
    }

C
Cole Robinson 已提交
5131
    if (!(flags & VIR_DOMAIN_VCPU_MAXIMUM) && vm->def->maxvcpus < max) {
5132 5133 5134
        max = vm->def->maxvcpus;
    }

5135 5136 5137 5138 5139 5140 5141
    if (nvcpus > max) {
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("requested vcpus is greater than max allowable"
                          " vcpus for the domain: %d > %d"), nvcpus, max);
        goto endjob;
    }

5142 5143 5144
    if (!(persistentDef = virDomainObjGetPersistentDef(driver->caps, vm)))
        goto endjob;

5145 5146
    switch (flags) {
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_CONFIG:
5147 5148 5149
        persistentDef->maxvcpus = nvcpus;
        if (nvcpus < persistentDef->vcpus)
            persistentDef->vcpus = nvcpus;
5150 5151 5152 5153
        ret = 0;
        break;

    case VIR_DOMAIN_VCPU_CONFIG:
5154
        persistentDef->vcpus = nvcpus;
5155 5156 5157 5158 5159 5160 5161 5162 5163
        ret = 0;
        break;

    case VIR_DOMAIN_VCPU_LIVE:
        ret = qemudDomainHotplugVcpus(vm, nvcpus);
        break;

    case VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG:
        ret = qemudDomainHotplugVcpus(vm, nvcpus);
5164 5165 5166
        if (ret == 0) {
            persistentDef->vcpus = nvcpus;
        }
5167 5168
        break;
    }
5169

5170 5171 5172 5173
    /* Save the persistent config to disk */
    if (flags & VIR_DOMAIN_VCPU_CONFIG)
        ret = virDomainSaveConfig(driver->configDir, persistentDef);

5174 5175 5176
endjob:
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
5177

5178
cleanup:
5179 5180
    if (vm)
        virDomainObjUnlock(vm);
5181
    return ret;
5182 5183
}

5184 5185 5186 5187 5188 5189
static int
qemudDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
{
    return qemudDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_VCPU_LIVE);
}

5190 5191 5192 5193 5194 5195

static int
qemudDomainPinVcpu(virDomainPtr dom,
                   unsigned int vcpu,
                   unsigned char *cpumap,
                   int maplen) {
5196 5197
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
5198
    int maxcpu, hostcpus;
5199
    virNodeInfo nodeinfo;
5200
    int ret = -1;
5201
    qemuDomainObjPrivatePtr priv;
5202

5203
    qemuDriverLock(driver);
5204
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
5205 5206
    qemuDriverUnlock(driver);

5207 5208 5209
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
5210 5211
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
5212 5213 5214
        goto cleanup;
    }

D
Daniel P. Berrange 已提交
5215
    if (!virDomainObjIsActive(vm)) {
5216 5217
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s",_("cannot pin vcpus on an inactive domain"));
5218
        goto cleanup;
5219 5220
    }

5221 5222 5223
    priv = vm->privateData;

    if (vcpu > (priv->nvcpupids-1)) {
5224 5225 5226
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("vcpu number out of range %d > %d"),
                        vcpu, priv->nvcpupids);
5227
        goto cleanup;
5228 5229
    }

5230
    if (nodeGetInfo(dom->conn, &nodeinfo) < 0)
5231
        goto cleanup;
5232

5233
    hostcpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
5234
    maxcpu = maplen * 8;
5235 5236
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;
5237

5238 5239
    if (priv->vcpupids != NULL) {
        if (virProcessInfoSetAffinity(priv->vcpupids[vcpu],
5240
                                      cpumap, maplen, maxcpu) < 0)
5241
            goto cleanup;
5242
    } else {
5243 5244
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        "%s", _("cpu affinity is not supported"));
5245
        goto cleanup;
5246
    }
5247
    ret = 0;
5248

5249
cleanup:
5250 5251
    if (vm)
        virDomainObjUnlock(vm);
5252
    return ret;
5253 5254 5255 5256 5257 5258 5259 5260
}

static int
qemudDomainGetVcpus(virDomainPtr dom,
                    virVcpuInfoPtr info,
                    int maxinfo,
                    unsigned char *cpumaps,
                    int maplen) {
5261 5262
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
5263
    virNodeInfo nodeinfo;
5264
    int i, v, maxcpu, hostcpus;
5265
    int ret = -1;
5266
    qemuDomainObjPrivatePtr priv;
5267

5268
    qemuDriverLock(driver);
5269
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
5270 5271
    qemuDriverUnlock(driver);

5272 5273 5274
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
5275 5276
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
5277 5278 5279
        goto cleanup;
    }

D
Daniel P. Berrange 已提交
5280
    if (!virDomainObjIsActive(vm)) {
5281 5282 5283
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s",
                        _("cannot list vcpu pinning for an inactive domain"));
5284
        goto cleanup;
5285 5286
    }

5287 5288
    priv = vm->privateData;

5289
    if (nodeGetInfo(dom->conn, &nodeinfo) < 0)
5290
        goto cleanup;
5291

5292
    hostcpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
5293
    maxcpu = maplen * 8;
5294 5295
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;
5296 5297

    /* Clamp to actual number of vcpus */
5298 5299
    if (maxinfo > priv->nvcpupids)
        maxinfo = priv->nvcpupids;
5300

5301 5302 5303 5304 5305 5306
    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;
5307

5308
                if (priv->vcpupids != NULL &&
5309 5310 5311
                    qemudGetProcessInfo(&(info[i].cpuTime),
                                        &(info[i].cpu),
                                        vm->pid,
5312
                                        priv->vcpupids[i]) < 0) {
5313
                    virReportSystemError(errno, "%s",
5314 5315 5316
                                         _("cannot get vCPU placement & pCPU time"));
                    goto cleanup;
                }
5317
            }
5318 5319
        }

5320 5321
        if (cpumaps != NULL) {
            memset(cpumaps, 0, maplen * maxinfo);
5322
            if (priv->vcpupids != NULL) {
5323 5324 5325
                for (v = 0 ; v < maxinfo ; v++) {
                    unsigned char *cpumap = VIR_GET_CPUMAP(cpumaps, maplen, v);

5326
                    if (virProcessInfoGetAffinity(priv->vcpupids[v],
5327
                                                  cpumap, maplen, maxcpu) < 0)
5328
                        goto cleanup;
5329
                }
5330
            } else {
5331 5332
                qemuReportError(VIR_ERR_NO_SUPPORT,
                                "%s", _("cpu affinity is not available"));
5333
                goto cleanup;
5334 5335 5336
            }
        }
    }
5337
    ret = maxinfo;
5338

5339
cleanup:
5340 5341
    if (vm)
        virDomainObjUnlock(vm);
5342
    return ret;
5343 5344 5345
}


5346 5347 5348
static int
qemudDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
{
5349 5350
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
5351
    virDomainDefPtr def;
5352
    int ret = -1;
5353

5354 5355 5356 5357 5358 5359 5360 5361
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* Exactly one of LIVE or CONFIG must be set.  */
    if (!(flags & VIR_DOMAIN_VCPU_LIVE) == !(flags & VIR_DOMAIN_VCPU_CONFIG)) {
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("invalid flag combination: (0x%x)"), flags);
5362 5363 5364
        return -1;
    }

5365
    qemuDriverLock(driver);
5366
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
5367 5368
    qemuDriverUnlock(driver);

5369
    if (!vm) {
5370 5371
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
5372 5373
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
5374
        goto cleanup;
5375 5376
    }

5377 5378 5379 5380 5381 5382 5383 5384 5385
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
        if (!virDomainObjIsActive(vm)) {
            qemuReportError(VIR_ERR_OPERATION_INVALID, "%s",
                            _("domain not active"));
            goto cleanup;
        }
        def = vm->def;
    } else {
        def = vm->newDef ? vm->newDef : vm->def;
5386 5387
    }

5388
    ret = (flags & VIR_DOMAIN_VCPU_MAXIMUM) ? def->maxvcpus : def->vcpus;
5389

5390
cleanup:
5391 5392
    if (vm)
        virDomainObjUnlock(vm);
5393 5394 5395
    return ret;
}

5396 5397 5398 5399 5400 5401 5402
static int
qemudDomainGetMaxVcpus(virDomainPtr dom)
{
    return qemudDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                          VIR_DOMAIN_VCPU_MAXIMUM));
}

5403 5404 5405 5406 5407 5408 5409 5410 5411
static int qemudDomainGetSecurityLabel(virDomainPtr dom, virSecurityLabelPtr seclabel)
{
    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

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

5412 5413
    memset(seclabel, 0, sizeof(*seclabel));

5414 5415 5416
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
5417 5418
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
5419 5420 5421
        goto cleanup;
    }

5422
    if (!virDomainVirtTypeToString(vm->def->virtType)) {
5423 5424 5425
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("unknown virt type in domain definition '%d'"),
                        vm->def->virtType);
5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442
        goto cleanup;
    }

    /*
     * Theoretically, the pid can be replaced during this operation and
     * return the label of a different process.  If atomicity is needed,
     * further validation will be required.
     *
     * Comment from Dan Berrange:
     *
     *   Well the PID as stored in the virDomainObjPtr can't be changed
     *   because you've got a locked object.  The OS level PID could have
     *   exited, though and in extreme circumstances have cycled through all
     *   PIDs back to ours. We could sanity check that our PID still exists
     *   after reading the label, by checking that our FD connecting to the
     *   QEMU monitor hasn't seen SIGHUP/ERR on poll().
     */
D
Daniel P. Berrange 已提交
5443
    if (virDomainObjIsActive(vm)) {
5444 5445
        if (virSecurityManagerGetProcessLabel(driver->securityManager,
                                              vm, seclabel) < 0) {
5446 5447 5448
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            "%s", _("Failed to get security label"));
            goto cleanup;
5449 5450 5451 5452 5453 5454 5455 5456
        }
    }

    ret = 0;

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
5457
    qemuDriverUnlock(driver);
5458 5459 5460
    return ret;
}

5461 5462
static int qemudNodeGetSecurityModel(virConnectPtr conn,
                                     virSecurityModelPtr secmodel)
5463 5464 5465
{
    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
    char *p;
5466
    int ret = 0;
5467

5468
    qemuDriverLock(driver);
5469 5470 5471 5472 5473
    memset(secmodel, 0, sizeof(*secmodel));

    /* NULL indicates no driver, which we treat as
     * success, but simply return no data in *secmodel */
    if (driver->caps->host.secModel.model == NULL)
5474
        goto cleanup;
5475

5476 5477
    p = driver->caps->host.secModel.model;
    if (strlen(p) >= VIR_SECURITY_MODEL_BUFLEN-1) {
5478 5479 5480
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("security model string exceeds max %d bytes"),
                        VIR_SECURITY_MODEL_BUFLEN-1);
5481 5482
        ret = -1;
        goto cleanup;
5483 5484 5485 5486 5487
    }
    strcpy(secmodel->model, p);

    p = driver->caps->host.secModel.doi;
    if (strlen(p) >= VIR_SECURITY_DOI_BUFLEN-1) {
5488 5489 5490
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("security DOI string exceeds max %d bytes"),
                        VIR_SECURITY_DOI_BUFLEN-1);
5491 5492
        ret = -1;
        goto cleanup;
5493 5494
    }
    strcpy(secmodel->doi, p);
5495 5496 5497 5498

cleanup:
    qemuDriverUnlock(driver);
    return ret;
5499 5500
}

5501 5502 5503 5504 5505 5506
/* qemudOpenAsUID() - pipe/fork/setuid/open a file, and return the
   pipe fd to caller, so that it can read from the file. Also return
   the pid of the child process, so the caller can wait for it to exit
   after it's finished reading (to avoid a zombie, if nothing
   else). */

5507 5508 5509
static int
qemudOpenAsUID(const char *path, uid_t uid, gid_t gid, pid_t *child_pid)
{
5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536
    int pipefd[2];
    int fd = -1;

    *child_pid = -1;

    if (pipe(pipefd) < 0) {
        virReportSystemError(errno,
                             _("failed to create pipe to read '%s'"),
                             path);
        pipefd[0] = pipefd[1] = -1;
        goto parent_cleanup;
    }

    int forkRet = virFork(child_pid);

    if (*child_pid < 0) {
        virReportSystemError(errno,
                             _("failed to fork child to read '%s'"),
                             path);
        goto parent_cleanup;
    }

    if (*child_pid > 0) {

        /* parent */

        /* parent doesn't need the write side of the pipe */
5537
        VIR_FORCE_CLOSE(pipefd[1]);
5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548

        if (forkRet < 0) {
            virReportSystemError(errno,
                                 _("failed in parent after forking child to read '%s'"),
                                 path);
            goto parent_cleanup;
        }
        /* caller gets the read side of the pipe */
        fd = pipefd[0];
        pipefd[0] = -1;
parent_cleanup:
5549 5550
        VIR_FORCE_CLOSE(pipefd[0]);
        VIR_FORCE_CLOSE(pipefd[1]);
5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574
        if ((fd < 0) && (*child_pid > 0)) {
            /* a child process was started and subsequently an error
               occurred in the parent, so we need to wait for it to
               exit, but its status is inconsequential. */
            while ((waitpid(*child_pid, NULL, 0) == -1)
                   && (errno == EINTR)) {
                /* empty */
            }
            *child_pid = -1;
        }
        return fd;
    }

    /* child */

    /* setuid to the qemu user, then open the file, read it,
       and stuff it into the pipe for the parent process to
       read */
    int exit_code;
    char *buf = NULL;
    size_t bufsize = 1024 * 1024;
    int bytesread;

    /* child doesn't need the read side of the pipe */
5575
    VIR_FORCE_CLOSE(pipefd[0]);
5576 5577 5578 5579 5580 5581 5582 5583 5584

    if (forkRet < 0) {
        exit_code = errno;
        virReportSystemError(errno,
                             _("failed in child after forking to read '%s'"),
                             path);
        goto child_cleanup;
    }

5585 5586 5587
    if (virSetUIDGID(uid, gid) < 0) {
       exit_code = errno;
       goto child_cleanup;
5588 5589
    }

5590 5591 5592 5593 5594 5595 5596 5597
    if ((fd = open(path, O_RDONLY)) < 0) {
        exit_code = errno;
        virReportSystemError(errno,
                             _("cannot open '%s' as uid %d"),
                             path, uid);
        goto child_cleanup;
    }

5598 5599 5600 5601 5602 5603
    if (VIR_ALLOC_N(buf, bufsize) < 0) {
        exit_code = ENOMEM;
        virReportOOMError();
        goto child_cleanup;
    }

5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623
    /* read from fd and write to pipefd[1] until EOF */
    do {
        if ((bytesread = saferead(fd, buf, bufsize)) < 0) {
            exit_code = errno;
            virReportSystemError(errno,
                                 _("child failed reading from '%s'"),
                                 path);
            goto child_cleanup;
        }
        if (safewrite(pipefd[1], buf, bytesread) != bytesread) {
            exit_code = errno;
            virReportSystemError(errno, "%s",
                                 _("child failed writing to pipe"));
            goto child_cleanup;
        }
    } while (bytesread > 0);
    exit_code = 0;

child_cleanup:
    VIR_FREE(buf);
5624 5625
    VIR_FORCE_CLOSE(fd);
    VIR_FORCE_CLOSE(pipefd[1]);
5626 5627 5628
    _exit(exit_code);
}

J
Jiri Denemark 已提交
5629 5630 5631 5632
static int qemudDomainSaveImageClose(int fd, pid_t read_pid, int *status)
{
    int ret = 0;

5633 5634 5635 5636
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno, "%s",
                             _("cannot close file"));
    }
J
Jiri Denemark 已提交
5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652

    if (read_pid != -1) {
        /* reap the process that read the file */
        while ((ret = waitpid(read_pid, status, 0)) == -1
               && errno == EINTR) {
            /* empty */
        }
    } else if (status) {
        *status = 0;
    }

    return ret;
}

static int ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5)
qemudDomainSaveImageOpen(struct qemud_driver *driver,
5653 5654 5655 5656
                         const char *path,
                         virDomainDefPtr *ret_def,
                         struct qemud_save_header *ret_header,
                         pid_t *ret_read_pid)
J
Jiri Denemark 已提交
5657 5658
{
    int fd;
5659
    pid_t read_pid = -1;
5660
    struct qemud_save_header header;
J
Jiri Denemark 已提交
5661 5662
    char *xml = NULL;
    virDomainDefPtr def = NULL;
5663 5664

    if ((fd = open(path, O_RDONLY)) < 0) {
5665 5666 5667
        if ((driver->user == 0) || (getuid() != 0)) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            "%s", _("cannot read domain image"));
J
Jiri Denemark 已提交
5668
            goto error;
5669 5670 5671 5672 5673 5674
        }

        /* Opening as root failed, but qemu runs as a different user
           that might have better luck. Create a pipe, then fork a
           child process to run as the qemu user, which will hopefully
           have the necessary authority to read the file. */
5675 5676
        if ((fd = qemudOpenAsUID(path,
                                 driver->user, driver->group, &read_pid)) < 0) {
5677
            /* error already reported */
J
Jiri Denemark 已提交
5678
            goto error;
5679
        }
5680 5681 5682
    }

    if (saferead(fd, &header, sizeof(header)) != sizeof(header)) {
5683 5684
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("failed to read qemu header"));
J
Jiri Denemark 已提交
5685
        goto error;
5686 5687 5688
    }

    if (memcmp(header.magic, QEMUD_SAVE_MAGIC, sizeof(header.magic)) != 0) {
5689 5690
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("image magic is incorrect"));
J
Jiri Denemark 已提交
5691
        goto error;
5692 5693 5694
    }

    if (header.version > QEMUD_SAVE_VERSION) {
5695 5696 5697
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("image version is not supported (%d > %d)"),
                        header.version, QEMUD_SAVE_VERSION);
J
Jiri Denemark 已提交
5698
        goto error;
5699 5700
    }

5701 5702 5703
    if (header.xml_len <= 0) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("invalid XML length: %d"), header.xml_len);
J
Jiri Denemark 已提交
5704
        goto error;
5705 5706
    }

5707 5708
    if (VIR_ALLOC_N(xml, header.xml_len) < 0) {
        virReportOOMError();
J
Jiri Denemark 已提交
5709
        goto error;
5710 5711 5712
    }

    if (saferead(fd, xml, header.xml_len) != header.xml_len) {
5713 5714
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("failed to read XML"));
J
Jiri Denemark 已提交
5715
        goto error;
5716 5717 5718
    }

    /* Create a domain from this XML */
5719
    if (!(def = virDomainDefParseString(driver->caps, xml,
5720
                                        VIR_DOMAIN_XML_INACTIVE))) {
5721 5722
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("failed to parse XML"));
J
Jiri Denemark 已提交
5723
        goto error;
5724 5725
    }

J
Jiri Denemark 已提交
5726
    VIR_FREE(xml);
5727

J
Jiri Denemark 已提交
5728 5729 5730
    *ret_def = def;
    *ret_header = header;
    *ret_read_pid = read_pid;
5731

J
Jiri Denemark 已提交
5732
    return fd;
5733

J
Jiri Denemark 已提交
5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759
error:
    virDomainDefFree(def);
    VIR_FREE(xml);
    qemudDomainSaveImageClose(fd, read_pid, NULL);

    return -1;
}

static int ATTRIBUTE_NONNULL(6)
qemudDomainSaveImageStartVM(virConnectPtr conn,
                            struct qemud_driver *driver,
                            virDomainObjPtr vm,
                            int fd,
                            pid_t read_pid,
                            const struct qemud_save_header *header,
                            const char *path)
{
    int ret = -1;
    virDomainEventPtr event;
    int intermediatefd = -1;
    pid_t intermediate_pid = -1;
    int childstat;
    int wait_ret;
    int status;

    if (header->version == 2) {
5760
        const char *intermediate_argv[3] = { NULL, "-dc", NULL };
J
Jiri Denemark 已提交
5761
        const char *prog = qemudSaveCompressionTypeToString(header->compressed);
5762
        if (prog == NULL) {
5763 5764
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            _("Invalid compressed save format %d"),
J
Jiri Denemark 已提交
5765 5766
                            header->compressed);
            goto out;
5767
        }
5768

J
Jiri Denemark 已提交
5769
        if (header->compressed != QEMUD_SAVE_FORMAT_RAW) {
5770
            intermediate_argv[0] = prog;
5771 5772
            intermediatefd = fd;
            fd = -1;
5773
            if (virExec(intermediate_argv, NULL, NULL,
5774
                        &intermediate_pid, intermediatefd, &fd, NULL, 0) < 0) {
5775 5776 5777
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                _("Failed to start decompression binary %s"),
                                intermediate_argv[0]);
J
Jiri Denemark 已提交
5778
                goto out;
5779 5780 5781
            }
        }
    }
J
Jiri Denemark 已提交
5782

5783
    /* Set the migration source and start it up. */
5784 5785
    ret = qemudStartVMDaemon(conn, driver, vm, "stdio", true, fd, path,
                             VIR_VM_OP_RESTORE);
J
Jiri Denemark 已提交
5786

5787
    if (intermediate_pid != -1) {
5788 5789 5790 5791 5792 5793 5794 5795 5796
        if (ret < 0) {
            /* if there was an error setting up qemu, the intermediate process will
             * wait forever to write to stdout, so we must manually kill it.
             */
            VIR_FORCE_CLOSE(intermediatefd);
            VIR_FORCE_CLOSE(fd);
            kill(intermediate_pid, SIGTERM);
        }

5797 5798
        /* Wait for intermediate process to exit */
        while (waitpid(intermediate_pid, &childstat, 0) == -1 &&
J
Jiri Denemark 已提交
5799 5800 5801
               errno == EINTR) {
            /* empty */
        }
5802
    }
5803
    VIR_FORCE_CLOSE(intermediatefd);
J
Jiri Denemark 已提交
5804 5805

    wait_ret = qemudDomainSaveImageClose(fd, read_pid, &status);
5806
    fd = -1;
5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827
    if (read_pid != -1) {
        if (wait_ret == -1) {
            virReportSystemError(errno,
                                 _("failed to wait for process reading '%s'"),
                                 path);
            ret = -1;
        } else if (!WIFEXITED(status)) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            _("child process exited abnormally reading '%s'"),
                            path);
            ret = -1;
        } else {
            int exit_status = WEXITSTATUS(status);
            if (exit_status != 0) {
                virReportSystemError(exit_status,
                                     _("child process returned error reading '%s'"),
                                     path);
                ret = -1;
            }
        }
    }
J
Jiri Denemark 已提交
5828

5829 5830
    if (ret < 0) {
        qemuDomainStartAudit(vm, "restored", false);
J
Jiri Denemark 已提交
5831
        goto out;
5832
    }
5833

5834 5835 5836
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
5837
    qemuDomainStartAudit(vm, "restored", true);
J
Jiri Denemark 已提交
5838 5839 5840
    if (event)
        qemuDomainEventQueue(driver, event);

5841

5842
    /* If it was running before, resume it now. */
J
Jiri Denemark 已提交
5843
    if (header->was_running) {
H
Hu Tao 已提交
5844
        if (doStartCPUs(driver, vm, conn) < 0) {
5845
            if (virGetLastError() == NULL)
5846 5847
                qemuReportError(VIR_ERR_OPERATION_FAILED,
                                "%s", _("failed to resume domain"));
J
Jiri Denemark 已提交
5848
            goto out;
5849
        }
5850 5851
        if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0) {
            VIR_WARN("Failed to save status on vm %s", vm->def->name);
J
Jiri Denemark 已提交
5852
            goto out;
5853
        }
5854
    }
J
Jiri Denemark 已提交
5855

5856
    ret = 0;
5857

J
Jiri Denemark 已提交
5858
out:
5859 5860
    if (virSecurityManagerRestoreSavedStateLabel(driver->securityManager,
                                                 vm, path) < 0)
5861 5862
        VIR_WARN("failed to restore save state label on %s", path);

J
Jiri Denemark 已提交
5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887
    return ret;
}

static int qemudDomainRestore(virConnectPtr conn,
                              const char *path) {
    struct qemud_driver *driver = conn->privateData;
    virDomainDefPtr def = NULL;
    virDomainObjPtr vm = NULL;
    int fd = -1;
    pid_t read_pid = -1;
    int ret = -1;
    struct qemud_save_header header;

    qemuDriverLock(driver);

    fd = qemudDomainSaveImageOpen(driver, path, &def, &header, &read_pid);
    if (fd < 0)
        goto cleanup;

    if (virDomainObjIsDuplicate(&driver->domains, def, 1) < 0)
        goto cleanup;

    if (!(vm = virDomainAssignDef(driver->caps,
                                  &driver->domains,
                                  def, true))) {
5888
        /* virDomainAssignDef already set the error */
J
Jiri Denemark 已提交
5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899
        goto cleanup;
    }
    def = NULL;

    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;

    ret = qemudDomainSaveImageStartVM(conn, driver, vm, fd,
                                      read_pid, &header, path);

    if (qemuDomainObjEndJob(vm) == 0)
5900
        vm = NULL;
J
Jiri Denemark 已提交
5901 5902 5903 5904
    else if (ret < 0 && !vm->persistent) {
        virDomainRemoveInactive(&driver->domains, vm);
        vm = NULL;
    }
5905

5906 5907
cleanup:
    virDomainDefFree(def);
J
Jiri Denemark 已提交
5908
    qemudDomainSaveImageClose(fd, read_pid, NULL);
5909 5910 5911
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
5912
    return ret;
D
Daniel P. Berrange 已提交
5913 5914
}

J
Jiri Denemark 已提交
5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955
static int qemudDomainObjRestore(virConnectPtr conn,
                                 struct qemud_driver *driver,
                                 virDomainObjPtr vm,
                                 const char *path)
{
    virDomainDefPtr def = NULL;
    int fd = -1;
    pid_t read_pid = -1;
    int ret = -1;
    struct qemud_save_header header;

    fd = qemudDomainSaveImageOpen(driver, path, &def, &header, &read_pid);
    if (fd < 0)
        goto cleanup;

    if (STRNEQ(vm->def->name, def->name) ||
        memcmp(vm->def->uuid, def->uuid, VIR_UUID_BUFLEN)) {
        char vm_uuidstr[VIR_UUID_STRING_BUFLEN];
        char def_uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(vm->def->uuid, vm_uuidstr);
        virUUIDFormat(def->uuid, def_uuidstr);
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("cannot restore domain '%s' uuid %s from a file"
                          " which belongs to domain '%s' uuid %s"),
                        vm->def->name, vm_uuidstr,
                        def->name, def_uuidstr);
        goto cleanup;
    }

    virDomainObjAssignDef(vm, def, true);
    def = NULL;

    ret = qemudDomainSaveImageStartVM(conn, driver, vm, fd,
                                      read_pid, &header, path);

cleanup:
    virDomainDefFree(def);
    qemudDomainSaveImageClose(fd, read_pid, NULL);
    return ret;
}

D
Daniel P. Berrange 已提交
5956

5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994
static char *qemudVMDumpXML(struct qemud_driver *driver,
                            virDomainObjPtr vm,
                            int flags)
{
    char *ret = NULL;
    virCPUDefPtr cpu = NULL;
    virDomainDefPtr def;
    virCPUDefPtr def_cpu;

    if ((flags & VIR_DOMAIN_XML_INACTIVE) && vm->newDef)
        def = vm->newDef;
    else
        def = vm->def;
    def_cpu = def->cpu;

    /* Update guest CPU requirements according to host CPU */
    if ((flags & VIR_DOMAIN_XML_UPDATE_CPU) && def_cpu && def_cpu->model) {
        if (!driver->caps || !driver->caps->host.cpu) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            "%s", _("cannot get host CPU capabilities"));
            goto cleanup;
        }

        if (!(cpu = virCPUDefCopy(def_cpu))
            || cpuUpdate(cpu, driver->caps->host.cpu))
            goto cleanup;
        def->cpu = cpu;
    }

    ret = virDomainDefFormat(def, flags);

cleanup:
    def->cpu = def_cpu;
    virCPUDefFree(cpu);
    return ret;
}


5995
static char *qemudDomainDumpXML(virDomainPtr dom,
5996
                                int flags) {
5997 5998 5999
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *ret = NULL;
6000 6001
    unsigned long balloon;
    int err;
6002

6003
    qemuDriverLock(driver);
6004
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
6005

D
Daniel P. Berrange 已提交
6006
    if (!vm) {
6007 6008
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
6009 6010
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
6011
        goto cleanup;
D
Daniel P. Berrange 已提交
6012 6013
    }

6014 6015 6016 6017
    /* Refresh current memory based on balloon info if supported */
    if ((vm->def->memballoon != NULL) &&
        (vm->def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_NONE) &&
        (virDomainObjIsActive(vm))) {
6018
        qemuDomainObjPrivatePtr priv = vm->privateData;
6019 6020 6021
        /* Don't delay if someone's using the monitor, just use
         * existing most recent data instead */
        if (!priv->jobActive) {
6022
            if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
6023 6024
                goto cleanup;

6025
            qemuDomainObjEnterMonitorWithDriver(driver, vm);
6026
            err = qemuMonitorGetBalloonInfo(priv->mon, &balloon);
6027
            qemuDomainObjExitMonitorWithDriver(driver, vm);
6028 6029 6030 6031
            if (qemuDomainObjEndJob(vm) == 0) {
                vm = NULL;
                goto cleanup;
            }
6032 6033 6034
            if (err < 0)
                goto cleanup;
            if (err > 0)
6035
                vm->def->mem.cur_balloon = balloon;
6036 6037
            /* err == 0 indicates no balloon support, so ignore it */
        }
6038
    }
6039

6040
    ret = qemudVMDumpXML(driver, vm, flags);
6041 6042

cleanup:
6043 6044
    if (vm)
        virDomainObjUnlock(vm);
6045
    qemuDriverUnlock(driver);
6046
    return ret;
D
Daniel P. Berrange 已提交
6047 6048 6049
}


6050 6051 6052 6053
static char *qemuDomainXMLFromNative(virConnectPtr conn,
                                     const char *format,
                                     const char *config,
                                     unsigned int flags ATTRIBUTE_UNUSED) {
6054
    struct qemud_driver *driver = conn->privateData;
6055 6056 6057 6058
    virDomainDefPtr def = NULL;
    char *xml = NULL;

    if (STRNEQ(format, QEMU_CONFIG_FORMAT_ARGV)) {
6059 6060
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("unsupported config type %s"), format);
6061 6062 6063
        goto cleanup;
    }

6064
    qemuDriverLock(driver);
6065
    def = qemuParseCommandLineString(driver->caps, config);
6066
    qemuDriverUnlock(driver);
6067 6068 6069
    if (!def)
        goto cleanup;

6070
    xml = virDomainDefFormat(def, VIR_DOMAIN_XML_INACTIVE);
6071 6072 6073 6074 6075 6076

cleanup:
    virDomainDefFree(def);
    return xml;
}

6077 6078 6079 6080 6081 6082
static char *qemuDomainXMLToNative(virConnectPtr conn,
                                   const char *format,
                                   const char *xmlData,
                                   unsigned int flags ATTRIBUTE_UNUSED) {
    struct qemud_driver *driver = conn->privateData;
    virDomainDefPtr def = NULL;
6083
    virDomainChrSourceDef monConfig;
6084
    unsigned long long qemuCmdFlags;
E
Eric Blake 已提交
6085
    virCommandPtr cmd = NULL;
6086 6087 6088
    char *ret = NULL;
    int i;

6089 6090
    qemuDriverLock(driver);

6091
    if (STRNEQ(format, QEMU_CONFIG_FORMAT_ARGV)) {
6092 6093
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("unsupported config type %s"), format);
6094 6095 6096
        goto cleanup;
    }

6097
    def = virDomainDefParseString(driver->caps, xmlData, 0);
6098 6099 6100
    if (!def)
        goto cleanup;

6101 6102
    /* Since we're just exporting args, we can't do bridge/network/direct
     * setups, since libvirt will normally create TAP/macvtap devices
6103 6104 6105 6106 6107
     * directly. We convert those configs into generic 'ethernet'
     * config and assume the user has suitable 'ifup-qemu' scripts
     */
    for (i = 0 ; i < def->nnets ; i++) {
        virDomainNetDefPtr net = def->nets[i];
6108
        int bootIndex = net->bootIndex;
6109 6110
        if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK ||
            net->type == VIR_DOMAIN_NET_TYPE_DIRECT) {
6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130
            VIR_FREE(net->data.network.name);

            memset(net, 0, sizeof *net);

            net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
            net->data.ethernet.dev = NULL;
            net->data.ethernet.script = NULL;
            net->data.ethernet.ipaddr = NULL;
        } else if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
            char *brname = net->data.bridge.brname;
            char *script = net->data.bridge.script;
            char *ipaddr = net->data.bridge.ipaddr;

            memset(net, 0, sizeof *net);

            net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
            net->data.ethernet.dev = brname;
            net->data.ethernet.script = script;
            net->data.ethernet.ipaddr = ipaddr;
        }
6131
        net->bootIndex = bootIndex;
6132 6133 6134 6135
    }
    for (i = 0 ; i < def->ngraphics ; i++) {
        if (def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
            def->graphics[i]->data.vnc.autoport)
6136
            def->graphics[i]->data.vnc.port = QEMU_VNC_PORT_MIN;
6137 6138
    }

6139 6140 6141
    if (qemuCapsExtractVersionInfo(def->emulator,
                                   NULL,
                                   &qemuCmdFlags) < 0)
6142 6143
        goto cleanup;

6144
    if (qemuPrepareMonitorChr(driver, &monConfig, def->name) < 0)
6145
        goto cleanup;
6146

6147 6148
    if (!(cmd = qemuBuildCommandLine(conn, driver, def,
                                     &monConfig, false, qemuCmdFlags,
6149
                                     NULL, -1, NULL, VIR_VM_OP_NO_OP)))
6150 6151
        goto cleanup;

E
Eric Blake 已提交
6152
    ret = virCommandToString(cmd);
6153 6154

cleanup:
6155
    qemuDriverUnlock(driver);
6156

E
Eric Blake 已提交
6157
    virCommandFree(cmd);
6158 6159 6160 6161 6162
    virDomainDefFree(def);
    return ret;
}


6163
static int qemudListDefinedDomains(virConnectPtr conn,
6164
                            char **const names, int nnames) {
6165
    struct qemud_driver *driver = conn->privateData;
6166
    int n;
6167

6168
    qemuDriverLock(driver);
6169
    n = virDomainObjListGetInactiveNames(&driver->domains, names, nnames);
6170
    qemuDriverUnlock(driver);
6171
    return n;
D
Daniel P. Berrange 已提交
6172 6173
}

6174
static int qemudNumDefinedDomains(virConnectPtr conn) {
6175
    struct qemud_driver *driver = conn->privateData;
6176
    int n;
6177

6178
    qemuDriverLock(driver);
6179
    n = virDomainObjListNumOfDomains(&driver->domains, 0);
6180
    qemuDriverUnlock(driver);
6181

6182
    return n;
D
Daniel P. Berrange 已提交
6183 6184 6185
}


J
Jiri Denemark 已提交
6186 6187
static int qemudDomainObjStart(virConnectPtr conn,
                               struct qemud_driver *driver,
6188 6189
                               virDomainObjPtr vm,
                               bool start_paused)
J
Jiri Denemark 已提交
6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209
{
    int ret = -1;
    char *managed_save;

    /*
     * If there is a managed saved state restore it instead of starting
     * from scratch. In any case the old state is removed.
     */
    managed_save = qemuDomainManagedSavePath(driver, vm);
    if ((managed_save) && (virFileExists(managed_save))) {
        ret = qemudDomainObjRestore(conn, driver, vm, managed_save);

        if (unlink(managed_save) < 0) {
            VIR_WARN("Failed to remove the managed state %s", managed_save);
        }

        if (ret == 0)
            goto cleanup;
    }

6210 6211
    ret = qemudStartVMDaemon(conn, driver, vm, NULL, start_paused, -1, NULL,
                             VIR_VM_OP_CREATE);
6212 6213
    qemuDomainStartAudit(vm, "booted", ret >= 0);
    if (ret >= 0) {
J
Jiri Denemark 已提交
6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226
        virDomainEventPtr event =
            virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
        if (event)
            qemuDomainEventQueue(driver, event);
    }

cleanup:
    VIR_FREE(managed_save);
    return ret;
}

6227 6228 6229
static int
qemudDomainStartWithFlags(virDomainPtr dom, unsigned int flags)
{
6230 6231 6232
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
6233

6234
    virCheckFlags(VIR_DOMAIN_START_PAUSED, -1);
6235

6236
    qemuDriverLock(driver);
6237
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
6238

6239
    if (!vm) {
6240 6241
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
6242 6243
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
6244
        goto cleanup;
6245 6246
    }

6247 6248 6249 6250
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;

    if (virDomainObjIsActive(vm)) {
6251 6252
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is already running"));
6253 6254 6255
        goto endjob;
    }

6256 6257
    ret = qemudDomainObjStart(dom->conn, driver, vm,
                              (flags & VIR_DOMAIN_START_PAUSED) != 0);
6258

6259
endjob:
6260 6261
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
6262

6263
cleanup:
6264 6265
    if (vm)
        virDomainObjUnlock(vm);
6266
    qemuDriverUnlock(driver);
6267
    return ret;
D
Daniel P. Berrange 已提交
6268 6269
}

6270 6271 6272 6273 6274 6275
static int
qemudDomainStart(virDomainPtr dom)
{
    return qemudDomainStartWithFlags(dom, 0);
}

6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290
static int
qemudCanonicalizeMachineFromInfo(virDomainDefPtr def,
                                 virCapsGuestDomainInfoPtr info,
                                 char **canonical)
{
    int i;

    *canonical = NULL;

    for (i = 0; i < info->nmachines; i++) {
        virCapsGuestMachinePtr machine = info->machines[i];

        if (!machine->canonical)
            continue;

6291
        if (STRNEQ(def->os.machine, machine->name))
6292 6293 6294
            continue;

        if (!(*canonical = strdup(machine->canonical))) {
6295
            virReportOOMError();
6296 6297 6298 6299 6300 6301 6302 6303 6304
            return -1;
        }

        break;
    }

    return 0;
}

6305 6306 6307 6308 6309 6310
static int
qemudCanonicalizeMachineDirect(virDomainDefPtr def, char **canonical)
{
    virCapsGuestMachinePtr *machines = NULL;
    int i, nmachines = 0;

6311
    if (qemuCapsProbeMachineTypes(def->emulator, &machines, &nmachines) < 0)
6312 6313 6314 6315 6316 6317
        return -1;

    for (i = 0; i < nmachines; i++) {
        if (!machines[i]->canonical)
            continue;

6318
        if (STRNEQ(def->os.machine, machines[i]->name))
6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330
            continue;

        *canonical = machines[i]->canonical;
        machines[i]->canonical = NULL;
        break;
    }

    virCapabilitiesFreeMachines(machines, nmachines);

    return 0;
}

6331 6332
int
qemudCanonicalizeMachine(struct qemud_driver *driver, virDomainDefPtr def)
6333 6334 6335 6336 6337 6338
{
    char *canonical = NULL;
    int i;

    for (i = 0; i < driver->caps->nguests; i++) {
        virCapsGuestPtr guest = driver->caps->guests[i];
6339
        virCapsGuestDomainInfoPtr info;
6340 6341 6342
        int j;

        for (j = 0; j < guest->arch.ndomains; j++) {
6343
            info = &guest->arch.domains[j]->info;
6344

6345 6346 6347 6348 6349 6350 6351 6352 6353
            if (!info->emulator || !STREQ(info->emulator, def->emulator))
                continue;

            if (!info->nmachines)
                info = &guest->arch.defaultInfo;

            if (qemudCanonicalizeMachineFromInfo(def, info, &canonical) < 0)
                return -1;
            goto out;
6354 6355
        }

6356 6357 6358 6359
        info = &guest->arch.defaultInfo;

        if (info->emulator && STREQ(info->emulator, def->emulator)) {
            if (qemudCanonicalizeMachineFromInfo(def, info, &canonical) < 0)
6360 6361 6362 6363
                return -1;
            goto out;
        }
    }
6364 6365 6366 6367

    if (qemudCanonicalizeMachineDirect(def, &canonical) < 0)
        return -1;

6368 6369 6370 6371 6372 6373 6374
out:
    if (canonical) {
        VIR_FREE(def->os.machine);
        def->os.machine = canonical;
    }
    return 0;
}
D
Daniel P. Berrange 已提交
6375

6376
static virDomainPtr qemudDomainDefine(virConnectPtr conn, const char *xml) {
6377
    struct qemud_driver *driver = conn->privateData;
6378
    virDomainDefPtr def;
6379
    virDomainObjPtr vm = NULL;
6380
    virDomainPtr dom = NULL;
6381
    virDomainEventPtr event = NULL;
6382
    int dupVM;
6383

6384
    qemuDriverLock(driver);
6385
    if (!(def = virDomainDefParseString(driver->caps, xml,
6386
                                        VIR_DOMAIN_XML_INACTIVE)))
6387
        goto cleanup;
6388

6389
    if (virSecurityManagerVerify(driver->securityManager, def) < 0)
6390 6391
        goto cleanup;

6392 6393
    if ((dupVM = virDomainObjIsDuplicate(&driver->domains, def, 0)) < 0)
        goto cleanup;
6394

6395
    if (qemudCanonicalizeMachine(driver, def) < 0)
6396 6397
        goto cleanup;

6398 6399 6400
    if (qemuAssignPCIAddresses(def) < 0)
        goto cleanup;

6401
    if (!(vm = virDomainAssignDef(driver->caps,
6402
                                  &driver->domains,
6403
                                  def, false))) {
6404
        goto cleanup;
6405
    }
6406
    def = NULL;
6407
    vm->persistent = 1;
6408

6409
    if (virDomainSaveConfig(driver->configDir,
6410
                            vm->newDef ? vm->newDef : vm->def) < 0) {
6411 6412
        virDomainRemoveInactive(&driver->domains,
                                vm);
6413
        vm = NULL;
6414
        goto cleanup;
6415 6416
    }

6417 6418
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_DEFINED,
6419
                                     !dupVM ?
6420 6421
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
6422

6423
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
6424
    if (dom) dom->id = vm->def->id;
6425 6426

cleanup:
6427
    virDomainDefFree(def);
6428 6429
    if (vm)
        virDomainObjUnlock(vm);
6430 6431
    if (event)
        qemuDomainEventQueue(driver, event);
6432
    qemuDriverUnlock(driver);
6433
    return dom;
D
Daniel P. Berrange 已提交
6434 6435
}

6436
static int qemudDomainUndefine(virDomainPtr dom) {
6437 6438
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
6439
    virDomainEventPtr event = NULL;
6440
    int ret = -1;
D
Daniel P. Berrange 已提交
6441

6442
    qemuDriverLock(driver);
6443
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
6444

D
Daniel P. Berrange 已提交
6445
    if (!vm) {
6446 6447
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
6448 6449
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
6450
        goto cleanup;
D
Daniel P. Berrange 已提交
6451 6452
    }

D
Daniel P. Berrange 已提交
6453
    if (virDomainObjIsActive(vm)) {
6454 6455
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot delete active domain"));
6456
        goto cleanup;
D
Daniel P. Berrange 已提交
6457 6458
    }

6459
    if (!vm->persistent) {
6460
        qemuReportError(VIR_ERR_OPERATION_INVALID,
6461
                        "%s", _("cannot undefine transient domain"));
6462
        goto cleanup;
6463 6464
    }

6465
    if (virDomainDeleteConfig(driver->configDir, driver->autostartDir, vm) < 0)
6466
        goto cleanup;
D
Daniel P. Berrange 已提交
6467

6468 6469 6470
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
6471

6472 6473
    virDomainRemoveInactive(&driver->domains,
                            vm);
6474
    vm = NULL;
6475
    ret = 0;
D
Daniel P. Berrange 已提交
6476

6477
cleanup:
6478 6479
    if (vm)
        virDomainObjUnlock(vm);
6480 6481
    if (event)
        qemuDomainEventQueue(driver, event);
6482
    qemuDriverUnlock(driver);
6483
    return ret;
D
Daniel P. Berrange 已提交
6484 6485
}

6486

6487 6488
static int qemudDomainAttachDevice(virDomainPtr dom,
                                   const char *xml)
6489
{
6490 6491 6492 6493
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    virDomainDeviceDefPtr dev = NULL;
    unsigned long long qemuCmdFlags;
6494
    virCgroupPtr cgroup = NULL;
6495
    int ret = -1;
6496

6497 6498 6499 6500 6501 6502 6503
    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
6504 6505 6506
        goto cleanup;
    }

6507 6508
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;
6509

6510 6511 6512 6513 6514
    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot attach device on inactive domain"));
        goto endjob;
    }
6515

6516 6517 6518 6519
    dev = virDomainDeviceDefParse(driver->caps, vm->def, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
    if (dev == NULL)
        goto endjob;
6520

6521 6522 6523 6524
    if (qemuCapsExtractVersionInfo(vm->def->emulator,
                                   NULL,
                                   &qemuCmdFlags) < 0)
        goto endjob;
6525

6526 6527 6528 6529
    if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
        if (qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
            if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) !=0 ) {
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
6530
                                _("Unable to find cgroup for %s"),
6531 6532 6533 6534 6535
                                vm->def->name);
                goto endjob;
            }
            if (qemuSetupDiskCgroup(driver, cgroup, dev->data.disk) < 0)
                goto endjob;
6536 6537
        }

6538 6539 6540 6541 6542 6543 6544 6545 6546 6547
        switch (dev->data.disk->device) {
        case VIR_DOMAIN_DISK_DEVICE_CDROM:
        case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
            ret = qemuDomainChangeEjectableMedia(driver, vm,
                                                 dev->data.disk,
                                                 qemuCmdFlags,
                                                 false);
            if (ret == 0)
                dev->data.disk = NULL;
            break;
6548

6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571
        case VIR_DOMAIN_DISK_DEVICE_DISK:
            if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_USB) {
                ret = qemuDomainAttachUsbMassstorageDevice(driver, vm,
                                                           dev->data.disk, qemuCmdFlags);
                if (ret == 0)
                    dev->data.disk = NULL;
            } else if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_VIRTIO) {
                ret = qemuDomainAttachPciDiskDevice(driver, vm,
                                                    dev->data.disk, qemuCmdFlags);
                if (ret == 0)
                    dev->data.disk = NULL;
            } else if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
                ret = qemuDomainAttachSCSIDisk(driver, vm,
                                               dev->data.disk, qemuCmdFlags);
                if (ret == 0)
                    dev->data.disk = NULL;
            } else {
                qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                _("disk bus '%s' cannot be hotplugged."),
                                virDomainDiskBusTypeToString(dev->data.disk->bus));
                /* fallthrough */
            }
            break;
6572

6573 6574 6575 6576 6577
        default:
            qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("disk device type '%s' cannot be hotplugged"),
                            virDomainDiskDeviceTypeToString(dev->data.disk->device));
            /* Fallthrough */
6578
        }
6579 6580 6581 6582
        if (ret != 0 && cgroup) {
            if (qemuTeardownDiskCgroup(driver, cgroup, dev->data.disk) < 0)
                VIR_WARN("Failed to teardown cgroup for disk path %s",
                         NULLSTR(dev->data.disk->src));
6583
        }
6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594
    } else if (dev->type == VIR_DOMAIN_DEVICE_CONTROLLER) {
        if (dev->data.controller->type == VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
            ret = qemuDomainAttachPciControllerDevice(driver, vm,
                                                      dev->data.controller, qemuCmdFlags);
            if (ret == 0)
                dev->data.controller = NULL;
        } else {
            qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("disk controller bus '%s' cannot be hotplugged."),
                            virDomainControllerTypeToString(dev->data.controller->type));
            /* fallthrough */
6595
        }
6596 6597 6598 6599 6600 6601 6602 6603 6604 6605
    } else if (dev->type == VIR_DOMAIN_DEVICE_NET) {
        ret = qemuDomainAttachNetDevice(dom->conn, driver, vm,
                                        dev->data.net, qemuCmdFlags);
        if (ret == 0)
            dev->data.net = NULL;
    } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
        ret = qemuDomainAttachHostDevice(driver, vm,
                                         dev->data.hostdev, qemuCmdFlags);
        if (ret == 0)
            dev->data.hostdev = NULL;
6606
    } else {
6607 6608 6609 6610
        qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("device type '%s' cannot be attached"),
                        virDomainDeviceTypeToString(dev->type));
        goto endjob;
6611 6612
    }

6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627
    if (!ret && virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
        ret = -1;

endjob:
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;

cleanup:
    if (cgroup)
        virCgroupFree(&cgroup);

    virDomainDeviceDefFree(dev);
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
6628 6629 6630
    return ret;
}

6631 6632 6633 6634 6635 6636 6637 6638
static int qemudDomainAttachDeviceFlags(virDomainPtr dom,
                                        const char *xml,
                                        unsigned int flags) {
    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot modify the persistent configuration of a domain"));
        return -1;
    }
6639

6640 6641
    return qemudDomainAttachDevice(dom, xml);
}
6642

6643

6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654
static int qemuDomainUpdateDeviceFlags(virDomainPtr dom,
                                       const char *xml,
                                       unsigned int flags)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    virDomainDeviceDefPtr dev = NULL;
    unsigned long long qemuCmdFlags;
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    bool force = (flags & VIR_DOMAIN_DEVICE_MODIFY_FORCE) != 0;
6655

6656 6657 6658 6659
    virCheckFlags(VIR_DOMAIN_DEVICE_MODIFY_CURRENT |
                  VIR_DOMAIN_DEVICE_MODIFY_LIVE |
                  VIR_DOMAIN_DEVICE_MODIFY_CONFIG |
                  VIR_DOMAIN_DEVICE_MODIFY_FORCE, -1);
6660

6661 6662 6663
    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot modify the persistent configuration of a domain"));
6664 6665 6666
        return -1;
    }

6667 6668 6669 6670 6671 6672 6673 6674
    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
6675 6676
    }

6677 6678 6679 6680 6681 6682 6683
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;

    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot attach device on inactive domain"));
        goto endjob;
6684 6685
    }

6686 6687 6688 6689 6690 6691 6692 6693 6694
    dev = virDomainDeviceDefParse(driver->caps, vm->def, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
    if (dev == NULL)
        goto endjob;

    if (qemuCapsExtractVersionInfo(vm->def->emulator,
                                   NULL,
                                   &qemuCmdFlags) < 0)
        goto endjob;
6695

6696 6697 6698 6699 6700
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
        if (qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
            if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) !=0 ) {
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
6701
                                _("Unable to find cgroup for %s"),
6702 6703 6704 6705 6706
                                vm->def->name);
                goto endjob;
            }
            if (qemuSetupDiskCgroup(driver, cgroup, dev->data.disk) < 0)
                goto endjob;
6707 6708
        }

6709 6710 6711 6712 6713 6714 6715 6716 6717 6718
        switch (dev->data.disk->device) {
        case VIR_DOMAIN_DISK_DEVICE_CDROM:
        case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
            ret = qemuDomainChangeEjectableMedia(driver, vm,
                                                 dev->data.disk,
                                                 qemuCmdFlags,
                                                 force);
            if (ret == 0)
                dev->data.disk = NULL;
            break;
6719

6720

6721 6722 6723 6724 6725 6726
        default:
            qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("disk bus '%s' cannot be updated."),
                            virDomainDiskBusTypeToString(dev->data.disk->bus));
            break;
        }
6727

6728 6729 6730 6731 6732
        if (ret != 0 && cgroup) {
            if (qemuTeardownDiskCgroup(driver, cgroup, dev->data.disk) < 0)
                VIR_WARN("Failed to teardown cgroup for disk path %s",
                         NULLSTR(dev->data.disk->src));
        }
6733
        break;
6734 6735 6736

    case VIR_DOMAIN_DEVICE_GRAPHICS:
        ret = qemuDomainChangeGraphics(driver, vm, dev->data.graphics);
6737
        break;
6738

6739
    default:
6740
        qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6741 6742 6743
                        _("disk device type '%s' cannot be updated"),
                        virDomainDiskDeviceTypeToString(dev->data.disk->device));
        break;
6744 6745
    }

6746 6747 6748 6749 6750 6751 6752 6753 6754 6755
    if (!ret && virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
        ret = -1;

endjob:
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;

cleanup:
    if (cgroup)
        virCgroupFree(&cgroup);
6756

6757 6758 6759 6760
    virDomainDeviceDefFree(dev);
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
6761 6762 6763
    return ret;
}

6764

6765 6766
static int qemudDomainDetachDevice(virDomainPtr dom,
                                   const char *xml) {
6767 6768
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
6769
    unsigned long long qemuCmdFlags;
6770 6771
    virDomainDeviceDefPtr dev = NULL;
    int ret = -1;
6772

6773
    qemuDriverLock(driver);
6774
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
6775
    if (!vm) {
6776 6777
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
6778 6779
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
6780
        goto cleanup;
6781 6782
    }

6783 6784 6785
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;

D
Daniel P. Berrange 已提交
6786
    if (!virDomainObjIsActive(vm)) {
6787 6788
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot detach device on inactive domain"));
6789
        goto endjob;
6790 6791
    }

6792
    dev = virDomainDeviceDefParse(driver->caps, vm->def, xml,
6793
                                  VIR_DOMAIN_XML_INACTIVE);
6794
    if (dev == NULL)
6795
        goto endjob;
6796

6797 6798 6799
    if (qemuCapsExtractVersionInfo(vm->def->emulator,
                                   NULL,
                                   &qemuCmdFlags) < 0)
6800
        goto endjob;
6801 6802

    if (dev->type == VIR_DOMAIN_DEVICE_DISK &&
6803 6804
        dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
        if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_VIRTIO) {
6805
            ret = qemuDomainDetachPciDiskDevice(driver, vm, dev, qemuCmdFlags);
6806 6807
        }
        else if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
6808 6809
            ret = qemuDomainDetachSCSIDiskDevice(driver, vm, dev,
                                                 qemuCmdFlags);
6810 6811
        }
        else {
6812
            qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6813 6814
                            _("This type of disk cannot be hot unplugged"));
        }
6815
    } else if (dev->type == VIR_DOMAIN_DEVICE_NET) {
6816
        ret = qemuDomainDetachNetDevice(driver, vm, dev, qemuCmdFlags);
6817 6818
    } else if (dev->type == VIR_DOMAIN_DEVICE_CONTROLLER) {
        if (dev->data.controller->type == VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
6819 6820
            ret = qemuDomainDetachPciControllerDevice(driver, vm, dev,
                                                      qemuCmdFlags);
6821
        } else {
6822
            qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6823 6824
                            _("disk controller bus '%s' cannot be hotunplugged."),
                            virDomainControllerTypeToString(dev->data.controller->type));
6825 6826
            /* fallthrough */
        }
6827
    } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
6828
        ret = qemuDomainDetachHostDevice(driver, vm, dev, qemuCmdFlags);
6829
    } else {
6830
        qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6831
                        "%s", _("This type of device cannot be hot unplugged"));
6832
    }
6833

6834
    if (!ret && virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
6835 6836
        ret = -1;

6837
endjob:
6838 6839
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
6840

6841 6842
cleanup:
    virDomainDeviceDefFree(dev);
6843 6844
    if (vm)
        virDomainObjUnlock(vm);
6845
    qemuDriverUnlock(driver);
6846 6847 6848
    return ret;
}

6849 6850 6851 6852
static int qemudDomainDetachDeviceFlags(virDomainPtr dom,
                                        const char *xml,
                                        unsigned int flags) {
    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
6853 6854
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot modify the persistent configuration of a domain"));
6855 6856 6857 6858 6859 6860
        return -1;
    }

    return qemudDomainDetachDevice(dom, xml);
}

6861
static int qemudDomainGetAutostart(virDomainPtr dom,
6862
                                   int *autostart) {
6863 6864 6865
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
6866

6867
    qemuDriverLock(driver);
6868
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
6869 6870
    qemuDriverUnlock(driver);

6871
    if (!vm) {
6872 6873
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
6874 6875
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
6876
        goto cleanup;
6877 6878 6879
    }

    *autostart = vm->autostart;
6880
    ret = 0;
6881

6882
cleanup:
6883 6884
    if (vm)
        virDomainObjUnlock(vm);
6885
    return ret;
6886 6887
}

6888
static int qemudDomainSetAutostart(virDomainPtr dom,
6889
                                   int autostart) {
6890 6891
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
6892 6893
    char *configFile = NULL, *autostartLink = NULL;
    int ret = -1;
6894

6895
    qemuDriverLock(driver);
6896
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
6897

6898
    if (!vm) {
6899 6900
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
6901 6902
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
6903
        goto cleanup;
6904 6905
    }

6906
    if (!vm->persistent) {
6907
        qemuReportError(VIR_ERR_OPERATION_INVALID,
6908
                        "%s", _("cannot set autostart for transient domain"));
6909
        goto cleanup;
6910 6911
    }

6912 6913
    autostart = (autostart != 0);

6914
    if (vm->autostart != autostart) {
6915
        if ((configFile = virDomainConfigFile(driver->configDir, vm->def->name)) == NULL)
6916
            goto cleanup;
6917
        if ((autostartLink = virDomainConfigFile(driver->autostartDir, vm->def->name)) == NULL)
6918
            goto cleanup;
6919

6920 6921
        if (autostart) {
            int err;
6922

6923
            if ((err = virFileMakePath(driver->autostartDir))) {
6924
                virReportSystemError(err,
6925 6926
                                     _("cannot create autostart directory %s"),
                                     driver->autostartDir);
6927 6928
                goto cleanup;
            }
6929

6930
            if (symlink(configFile, autostartLink) < 0) {
6931
                virReportSystemError(errno,
6932 6933
                                     _("Failed to create symlink '%s to '%s'"),
                                     autostartLink, configFile);
6934 6935 6936 6937
                goto cleanup;
            }
        } else {
            if (unlink(autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
6938
                virReportSystemError(errno,
6939 6940
                                     _("Failed to delete symlink '%s'"),
                                     autostartLink);
6941 6942
                goto cleanup;
            }
6943 6944
        }

6945
        vm->autostart = autostart;
6946
    }
6947
    ret = 0;
6948

6949 6950 6951
cleanup:
    VIR_FREE(configFile);
    VIR_FREE(autostartLink);
6952 6953
    if (vm)
        virDomainObjUnlock(vm);
6954
    qemuDriverUnlock(driver);
6955
    return ret;
6956 6957
}

6958 6959 6960 6961 6962

static char *qemuGetSchedulerType(virDomainPtr dom,
                                  int *nparams)
{
    struct qemud_driver *driver = dom->conn->privateData;
6963
    char *ret = NULL;
6964

6965
    qemuDriverLock(driver);
6966
    if (!qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
6967 6968
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        __FUNCTION__);
6969
        goto cleanup;
6970 6971 6972 6973 6974 6975 6976
    }

    if (nparams)
        *nparams = 1;

    ret = strdup("posix");
    if (!ret)
6977
        virReportOOMError();
6978 6979 6980

cleanup:
    qemuDriverUnlock(driver);
6981 6982 6983
    return ret;
}

6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050

static int qemuDomainSetMemoryParameters(virDomainPtr dom,
                                         virMemoryParameterPtr params,
                                         int nparams,
                                         unsigned int flags ATTRIBUTE_UNUSED)
{
    struct qemud_driver *driver = dom->conn->privateData;
    int i;
    virCgroupPtr group = NULL;
    virDomainObjPtr vm = NULL;
    int ret = -1;

    qemuDriverLock(driver);
    if (!qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_MEMORY)) {
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        __FUNCTION__);
        goto cleanup;
    }

    vm = virDomainFindByUUID(&driver->domains, dom->uuid);

    if (vm == NULL) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("No such domain %s"), dom->uuid);
        goto cleanup;
    }

    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("cannot find cgroup for domain %s"), vm->def->name);
        goto cleanup;
    }

    ret = 0;
    for (i = 0; i < nparams; i++) {
        virMemoryParameterPtr param = &params[i];

        if (STREQ(param->field, VIR_DOMAIN_MEMORY_HARD_LIMIT)) {
            int rc;
            if (param->type != VIR_DOMAIN_MEMORY_PARAM_ULLONG) {
                qemuReportError(VIR_ERR_INVALID_ARG, "%s",
                                _("invalid type for memory hard_limit tunable, expected a 'ullong'"));
                ret = -1;
                continue;
            }

            rc = virCgroupSetMemoryHardLimit(group, params[i].value.ul);
            if (rc != 0) {
                virReportSystemError(-rc, "%s",
                                     _("unable to set memory hard_limit tunable"));
                ret = -1;
            }
        } else if (STREQ(param->field, VIR_DOMAIN_MEMORY_SOFT_LIMIT)) {
            int rc;
            if (param->type != VIR_DOMAIN_MEMORY_PARAM_ULLONG) {
                qemuReportError(VIR_ERR_INVALID_ARG, "%s",
                                _("invalid type for memory soft_limit tunable, expected a 'ullong'"));
                ret = -1;
                continue;
            }

            rc = virCgroupSetMemorySoftLimit(group, params[i].value.ul);
            if (rc != 0) {
                virReportSystemError(-rc, "%s",
                                     _("unable to set memory soft_limit tunable"));
                ret = -1;
            }
7051
        } else if (STREQ(param->field, VIR_DOMAIN_MEMORY_SWAP_HARD_LIMIT)) {
7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084
            int rc;
            if (param->type != VIR_DOMAIN_MEMORY_PARAM_ULLONG) {
                qemuReportError(VIR_ERR_INVALID_ARG, "%s",
                                _("invalid type for swap_hard_limit tunable, expected a 'ullong'"));
                ret = -1;
                continue;
            }

            rc = virCgroupSetSwapHardLimit(group, params[i].value.ul);
            if (rc != 0) {
                virReportSystemError(-rc, "%s",
                                     _("unable to set swap_hard_limit tunable"));
                ret = -1;
            }
        } else if (STREQ(param->field, VIR_DOMAIN_MEMORY_MIN_GUARANTEE)) {
            qemuReportError(VIR_ERR_INVALID_ARG,
                            _("Memory tunable `%s' not implemented"), param->field);
            ret = -1;
        } else {
            qemuReportError(VIR_ERR_INVALID_ARG,
                            _("Parameter `%s' not supported"), param->field);
            ret = -1;
        }
    }

cleanup:
    virCgroupFree(&group);
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return ret;
}

7085 7086 7087 7088 7089 7090 7091 7092 7093
static int qemuDomainGetMemoryParameters(virDomainPtr dom,
                                         virMemoryParameterPtr params,
                                         int *nparams,
                                         unsigned int flags ATTRIBUTE_UNUSED)
{
    struct qemud_driver *driver = dom->conn->privateData;
    int i;
    virCgroupPtr group = NULL;
    virDomainObjPtr vm = NULL;
7094
    unsigned long long val;
7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144
    int ret = -1;
    int rc;

    qemuDriverLock(driver);

    if (!qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_MEMORY)) {
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        __FUNCTION__);
        goto cleanup;
    }

    vm = virDomainFindByUUID(&driver->domains, dom->uuid);

    if (vm == NULL) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("No such domain %s"), dom->uuid);
        goto cleanup;
    }

    if ((*nparams) == 0) {
        /* Current number of memory parameters supported by cgroups */
        *nparams = QEMU_NB_MEM_PARAM;
        ret = 0;
        goto cleanup;
    }

    if ((*nparams) != QEMU_NB_MEM_PARAM) {
        qemuReportError(VIR_ERR_INVALID_ARG,
                        "%s", _("Invalid parameter count"));
        goto cleanup;
    }

    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("cannot find cgroup for domain %s"), vm->def->name);
        goto cleanup;
    }

    for (i = 0; i < *nparams; i++) {
        virMemoryParameterPtr param = &params[i];
        val = 0;
        param->value.ul = 0;
        param->type = VIR_DOMAIN_MEMORY_PARAM_ULLONG;

        switch(i) {
        case 0: /* fill memory hard limit here */
            rc = virCgroupGetMemoryHardLimit(group, &val);
            if (rc != 0) {
                virReportSystemError(-rc, "%s",
                                     _("unable to get memory hard limit"));
7145
                goto cleanup;
7146 7147 7148 7149
            }
            if (virStrcpyStatic(param->field, VIR_DOMAIN_MEMORY_HARD_LIMIT) == NULL) {
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                "%s", _("Field memory hard limit too long for destination"));
7150
                goto cleanup;
7151 7152 7153 7154 7155 7156 7157 7158 7159
            }
            param->value.ul = val;
            break;

        case 1: /* fill memory soft limit here */
            rc = virCgroupGetMemorySoftLimit(group, &val);
            if (rc != 0) {
                virReportSystemError(-rc, "%s",
                                     _("unable to get memory soft limit"));
7160
                goto cleanup;
7161 7162 7163 7164
            }
            if (virStrcpyStatic(param->field, VIR_DOMAIN_MEMORY_SOFT_LIMIT) == NULL) {
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                "%s", _("Field memory soft limit too long for destination"));
7165
                goto cleanup;
7166 7167 7168 7169 7170 7171 7172 7173 7174
            }
            param->value.ul = val;
            break;

        case 2: /* fill swap hard limit here */
            rc = virCgroupGetSwapHardLimit(group, &val);
            if (rc != 0) {
                virReportSystemError(-rc, "%s",
                                     _("unable to get swap hard limit"));
7175
                goto cleanup;
7176
            }
7177
            if (virStrcpyStatic(param->field, VIR_DOMAIN_MEMORY_SWAP_HARD_LIMIT) == NULL) {
7178 7179
                qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                "%s", _("Field swap hard limit too long for destination"));
7180
                goto cleanup;
7181 7182 7183 7184 7185 7186 7187 7188 7189 7190
            }
            param->value.ul = val;
            break;

        default:
            break;
            /* should not hit here */
        }
    }

7191 7192
    ret = 0;

7193 7194 7195 7196 7197 7198 7199 7200 7201
cleanup:
    if (group)
        virCgroupFree(&group);
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return ret;
}

7202 7203 7204 7205 7206 7207 7208 7209 7210 7211
static int qemuSetSchedulerParameters(virDomainPtr dom,
                                      virSchedParameterPtr params,
                                      int nparams)
{
    struct qemud_driver *driver = dom->conn->privateData;
    int i;
    virCgroupPtr group = NULL;
    virDomainObjPtr vm = NULL;
    int ret = -1;

7212
    qemuDriverLock(driver);
7213
    if (!qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
7214 7215
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        __FUNCTION__);
7216
        goto cleanup;
7217 7218 7219 7220 7221
    }

    vm = virDomainFindByUUID(&driver->domains, dom->uuid);

    if (vm == NULL) {
7222 7223
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("No such domain %s"), dom->uuid);
7224 7225 7226 7227
        goto cleanup;
    }

    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
7228 7229
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("cannot find cgroup for domain %s"), vm->def->name);
7230 7231 7232 7233 7234 7235 7236 7237 7238
        goto cleanup;
    }

    for (i = 0; i < nparams; i++) {
        virSchedParameterPtr param = &params[i];

        if (STREQ(param->field, "cpu_shares")) {
            int rc;
            if (param->type != VIR_DOMAIN_SCHED_FIELD_ULLONG) {
7239 7240
                qemuReportError(VIR_ERR_INVALID_ARG, "%s",
                                _("invalid type for cpu_shares tunable, expected a 'ullong'"));
7241 7242 7243 7244 7245
                goto cleanup;
            }

            rc = virCgroupSetCpuShares(group, params[i].value.ul);
            if (rc != 0) {
7246
                virReportSystemError(-rc, "%s",
7247 7248 7249 7250
                                     _("unable to set cpu shares tunable"));
                goto cleanup;
            }
        } else {
7251 7252
            qemuReportError(VIR_ERR_INVALID_ARG,
                            _("Invalid parameter `%s'"), param->field);
7253 7254 7255 7256 7257 7258 7259 7260 7261
            goto cleanup;
        }
    }
    ret = 0;

cleanup:
    virCgroupFree(&group);
    if (vm)
        virDomainObjUnlock(vm);
7262
    qemuDriverUnlock(driver);
7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276
    return ret;
}

static int qemuGetSchedulerParameters(virDomainPtr dom,
                                      virSchedParameterPtr params,
                                      int *nparams)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virCgroupPtr group = NULL;
    virDomainObjPtr vm = NULL;
    unsigned long long val;
    int ret = -1;
    int rc;

7277
    qemuDriverLock(driver);
7278
    if (!qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
7279 7280
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        __FUNCTION__);
7281
        goto cleanup;
7282 7283 7284
    }

    if ((*nparams) != 1) {
7285 7286
        qemuReportError(VIR_ERR_INVALID_ARG,
                        "%s", _("Invalid parameter count"));
7287
        goto cleanup;
7288 7289 7290 7291 7292
    }

    vm = virDomainFindByUUID(&driver->domains, dom->uuid);

    if (vm == NULL) {
7293 7294
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("No such domain %s"), dom->uuid);
7295 7296 7297 7298
        goto cleanup;
    }

    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
7299 7300
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("cannot find cgroup for domain %s"), vm->def->name);
7301 7302 7303 7304 7305
        goto cleanup;
    }

    rc = virCgroupGetCpuShares(group, &val);
    if (rc != 0) {
7306
        virReportSystemError(-rc, "%s",
7307 7308 7309 7310 7311
                             _("unable to get cpu shares tunable"));
        goto cleanup;
    }
    params[0].value.ul = val;
    params[0].type = VIR_DOMAIN_SCHED_FIELD_ULLONG;
C
Chris Lalancette 已提交
7312
    if (virStrcpyStatic(params[0].field, "cpu_shares") == NULL) {
7313 7314
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("Field cpu_shares too long for destination"));
C
Chris Lalancette 已提交
7315 7316
        goto cleanup;
    }
7317 7318 7319 7320 7321 7322 7323

    ret = 0;

cleanup:
    virCgroupFree(&group);
    if (vm)
        virDomainObjUnlock(vm);
7324
    qemuDriverUnlock(driver);
7325 7326 7327 7328
    return ret;
}


7329 7330 7331 7332 7333 7334 7335 7336 7337
/* 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)
{
7338
    struct qemud_driver *driver = dom->conn->privateData;
7339
    int i, ret = -1;
7340
    virDomainObjPtr vm;
7341
    virDomainDiskDefPtr disk = NULL;
7342

7343
    qemuDriverLock(driver);
7344
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
7345
    qemuDriverUnlock(driver);
7346
    if (!vm) {
7347 7348
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
7349 7350
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
7351
        goto cleanup;
7352
    }
7353 7354 7355 7356

    if (qemuDomainObjBeginJob(vm) < 0)
        goto cleanup;

D
Daniel P. Berrange 已提交
7357
    if (!virDomainObjIsActive (vm)) {
7358 7359
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
7360
        goto endjob;
7361 7362
    }

7363 7364 7365 7366 7367 7368 7369 7370
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(path, vm->def->disks[i]->dst)) {
            disk = vm->def->disks[i];
            break;
        }
    }

    if (!disk) {
7371 7372
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("invalid path: %s"), path);
7373
        goto endjob;
7374 7375
    }

7376
    if (!disk->info.alias) {
7377 7378
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("missing disk device alias name for %s"), disk->dst);
7379
        goto endjob;
7380
    }
7381

7382
    qemuDomainObjPrivatePtr priv = vm->privateData;
7383 7384
    qemuDomainObjEnterMonitor(vm);
    ret = qemuMonitorGetBlockStatsInfo(priv->mon,
7385
                                       disk->info.alias,
7386 7387 7388 7389 7390 7391
                                       &stats->rd_req,
                                       &stats->rd_bytes,
                                       &stats->wr_req,
                                       &stats->wr_bytes,
                                       &stats->errs);
    qemuDomainObjExitMonitor(vm);
7392

7393
endjob:
7394 7395
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
7396

7397
cleanup:
7398 7399
    if (vm)
        virDomainObjUnlock(vm);
7400
    return ret;
7401 7402
}

7403
#ifdef __linux__
7404 7405 7406 7407 7408
static int
qemudDomainInterfaceStats (virDomainPtr dom,
                           const char *path,
                           struct _virDomainInterfaceStats *stats)
{
7409 7410
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
7411
    int i;
7412
    int ret = -1;
7413

7414
    qemuDriverLock(driver);
7415
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
7416 7417
    qemuDriverUnlock(driver);

7418
    if (!vm) {
7419 7420
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
7421 7422
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
7423
        goto cleanup;
7424 7425
    }

D
Daniel P. Berrange 已提交
7426
    if (!virDomainObjIsActive(vm)) {
7427 7428
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
7429
        goto cleanup;
7430 7431 7432
    }

    /* Check the path is one of the domain's network interfaces. */
7433 7434
    for (i = 0 ; i < vm->def->nnets ; i++) {
        if (vm->def->nets[i]->ifname &&
7435 7436 7437 7438
            STREQ (vm->def->nets[i]->ifname, path)) {
            ret = 0;
            break;
        }
7439 7440
    }

7441
    if (ret == 0)
7442
        ret = linuxDomainInterfaceStats(path, stats);
7443
    else
7444 7445
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("invalid path, '%s' is not a known interface"), path);
7446

7447
cleanup:
7448 7449
    if (vm)
        virDomainObjUnlock(vm);
7450 7451
    return ret;
}
7452
#else
7453 7454 7455 7456
static int
qemudDomainInterfaceStats (virDomainPtr dom,
                           const char *path ATTRIBUTE_UNUSED,
                           struct _virDomainInterfaceStats *stats ATTRIBUTE_UNUSED)
7457 7458
    qemuReportError(VIR_ERR_NO_SUPPORT,
                    "%s", __FUNCTION__);
7459 7460
    return -1;
}
7461
#endif
7462

7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478
static int
qemudDomainMemoryStats (virDomainPtr dom,
                        struct _virDomainMemoryStat *stats,
                        unsigned int nr_stats)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    unsigned int ret = -1;

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

    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
7479 7480
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
7481 7482 7483
        goto cleanup;
    }

7484 7485 7486
    if (qemuDomainObjBeginJob(vm) < 0)
        goto cleanup;

7487 7488 7489
    if (virDomainObjIsActive(vm)) {
        qemuDomainObjPrivatePtr priv = vm->privateData;
        qemuDomainObjEnterMonitor(vm);
7490
        ret = qemuMonitorGetMemoryStats(priv->mon, stats, nr_stats);
7491 7492
        qemuDomainObjExitMonitor(vm);
    } else {
7493 7494
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
7495 7496
    }

7497 7498 7499
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;

7500 7501 7502 7503 7504 7505
cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    return ret;
}

7506 7507 7508 7509 7510 7511 7512
static int
qemudDomainBlockPeek (virDomainPtr dom,
                      const char *path,
                      unsigned long long offset, size_t size,
                      void *buffer,
                      unsigned int flags ATTRIBUTE_UNUSED)
{
7513 7514 7515
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int fd = -1, ret = -1, i;
7516

7517
    qemuDriverLock(driver);
7518
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
7519 7520
    qemuDriverUnlock(driver);

7521
    if (!vm) {
7522 7523
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
7524 7525
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
7526
        goto cleanup;
7527 7528 7529
    }

    if (!path || path[0] == '\0') {
7530 7531
        qemuReportError(VIR_ERR_INVALID_ARG,
                        "%s", _("NULL or empty path"));
7532
        goto cleanup;
7533 7534 7535
    }

    /* Check the path belongs to this domain. */
7536 7537
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (vm->def->disks[i]->src != NULL &&
7538 7539 7540 7541
            STREQ (vm->def->disks[i]->src, path)) {
            ret = 0;
            break;
        }
7542 7543
    }

7544 7545 7546 7547 7548
    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) {
7549 7550
            virReportSystemError(errno,
                                 _("%s: failed to open"), path);
7551 7552
            goto cleanup;
        }
7553

7554 7555 7556 7557 7558 7559
        /* 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) {
7560 7561
            virReportSystemError(errno,
                                 _("%s: failed to seek or read"), path);
7562 7563 7564 7565 7566
            goto cleanup;
        }

        ret = 0;
    } else {
7567 7568
        qemuReportError(VIR_ERR_INVALID_ARG,
                        "%s", _("invalid path"));
7569 7570
    }

7571
cleanup:
7572
    VIR_FORCE_CLOSE(fd);
7573 7574
    if (vm)
        virDomainObjUnlock(vm);
7575 7576 7577
    return ret;
}

R
Richard W.M. Jones 已提交
7578 7579 7580 7581 7582 7583
static int
qemudDomainMemoryPeek (virDomainPtr dom,
                       unsigned long long offset, size_t size,
                       void *buffer,
                       unsigned int flags)
{
7584 7585
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
7586
    char *tmp = NULL;
R
Richard W.M. Jones 已提交
7587 7588
    int fd = -1, ret = -1;

7589
    qemuDriverLock(driver);
7590
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
7591
    qemuDriverUnlock(driver);
R
Richard W.M. Jones 已提交
7592 7593

    if (!vm) {
7594 7595
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
7596 7597
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
7598 7599 7600
        goto cleanup;
    }

7601
    if (flags != VIR_MEMORY_VIRTUAL && flags != VIR_MEMORY_PHYSICAL) {
7602 7603
        qemuReportError(VIR_ERR_INVALID_ARG,
                        "%s", _("flags parameter must be VIR_MEMORY_VIRTUAL or VIR_MEMORY_PHYSICAL"));
7604
        goto cleanup;
R
Richard W.M. Jones 已提交
7605 7606
    }

7607 7608 7609
    if (qemuDomainObjBeginJob(vm) < 0)
        goto cleanup;

D
Daniel P. Berrange 已提交
7610
    if (!virDomainObjIsActive(vm)) {
7611 7612
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
7613
        goto endjob;
R
Richard W.M. Jones 已提交
7614 7615
    }

7616
    if (virAsprintf(&tmp, "%s/qemu.mem.XXXXXX", driver->cacheDir) < 0) {
7617
        virReportOOMError();
7618
        goto endjob;
7619 7620
    }

R
Richard W.M. Jones 已提交
7621 7622
    /* Create a temporary filename. */
    if ((fd = mkstemp (tmp)) == -1) {
7623 7624
        virReportSystemError(errno,
                             _("mkstemp(\"%s\") failed"), tmp);
7625
        goto endjob;
R
Richard W.M. Jones 已提交
7626 7627
    }

7628
    qemuDomainObjPrivatePtr priv = vm->privateData;
7629
    qemuDomainObjEnterMonitor(vm);
7630
    if (flags == VIR_MEMORY_VIRTUAL) {
7631 7632
        if (qemuMonitorSaveVirtualMemory(priv->mon, offset, size, tmp) < 0) {
            qemuDomainObjExitMonitor(vm);
7633
            goto endjob;
7634
        }
7635
    } else {
7636 7637
        if (qemuMonitorSavePhysicalMemory(priv->mon, offset, size, tmp) < 0) {
            qemuDomainObjExitMonitor(vm);
7638
            goto endjob;
7639
        }
R
Richard W.M. Jones 已提交
7640
    }
7641
    qemuDomainObjExitMonitor(vm);
R
Richard W.M. Jones 已提交
7642 7643 7644

    /* Read the memory file into buffer. */
    if (saferead (fd, buffer, size) == (ssize_t) -1) {
7645 7646 7647
        virReportSystemError(errno,
                             _("failed to read temporary file "
                               "created with template %s"), tmp);
7648
        goto endjob;
R
Richard W.M. Jones 已提交
7649 7650 7651
    }

    ret = 0;
7652

7653
endjob:
7654 7655
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
7656

7657
cleanup:
7658
    VIR_FREE(tmp);
7659
    VIR_FORCE_CLOSE(fd);
R
Richard W.M. Jones 已提交
7660
    unlink (tmp);
7661 7662
    if (vm)
        virDomainObjUnlock(vm);
R
Richard W.M. Jones 已提交
7663 7664 7665
    return ret;
}

7666

7667 7668 7669 7670 7671 7672 7673 7674 7675 7676
static int qemuDomainGetBlockInfo(virDomainPtr dom,
                                  const char *path,
                                  virDomainBlockInfoPtr info,
                                  unsigned int flags) {
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
    int fd = -1;
    off_t end;
    virStorageFileMetadata meta;
7677
    virDomainDiskDefPtr disk = NULL;
7678 7679
    struct stat sb;
    int i;
7680
    int format;
7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704

    virCheckFlags(0, -1);

    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    qemuDriverUnlock(driver);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    if (!path || path[0] == '\0') {
        qemuReportError(VIR_ERR_INVALID_ARG,
                        "%s", _("NULL or empty path"));
        goto cleanup;
    }

    /* Check the path belongs to this domain. */
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (vm->def->disks[i]->src != NULL &&
            STREQ (vm->def->disks[i]->src, path)) {
7705
            disk = vm->def->disks[i];
7706 7707 7708 7709
            break;
        }
    }

7710
    if (!disk) {
7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("invalid path %s not assigned to domain"), path);
        goto cleanup;
    }

    /* The path is correct, now try to open it and get its size. */
    fd = open (path, O_RDONLY);
    if (fd == -1) {
        virReportSystemError(errno,
                             _("failed to open path '%s'"), path);
        goto cleanup;
    }

    /* Probe for magic formats */
7725 7726 7727 7728 7729 7730 7731 7732
    if (disk->driverType) {
        if ((format = virStorageFileFormatTypeFromString(disk->driverType)) < 0) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            _("unknown disk format %s for %s"),
                            disk->driverType, disk->src);
            goto cleanup;
        }
    } else {
7733 7734 7735 7736 7737 7738 7739
        if (driver->allowDiskFormatProbing) {
            if ((format = virStorageFileProbeFormat(disk->src)) < 0)
                goto cleanup;
        } else {
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
                            _("no disk format for %s and probing is disabled"),
                            disk->src);
7740
            goto cleanup;
7741
        }
7742 7743 7744 7745 7746
    }

    if (virStorageFileGetMetadataFromFD(path, fd,
                                        format,
                                        &meta) < 0)
7747 7748 7749 7750 7751 7752 7753 7754 7755 7756
        goto cleanup;

    /* Get info for normal formats */
    if (fstat(fd, &sb) < 0) {
        virReportSystemError(errno,
                             _("cannot stat file '%s'"), path);
        goto cleanup;
    }

    if (S_ISREG(sb.st_mode)) {
7757
#ifndef WIN32
7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785
        info->physical = (unsigned long long)sb.st_blocks *
            (unsigned long long)DEV_BSIZE;
#else
        info->physical = sb.st_size;
#endif
        /* Regular files may be sparse, so logical size (capacity) is not same
         * as actual physical above
         */
        info->capacity = sb.st_size;
    } else {
        /* NB. Because we configure with AC_SYS_LARGEFILE, off_t should
         * be 64 bits on all platforms.
         */
        end = lseek (fd, 0, SEEK_END);
        if (end == (off_t)-1) {
            virReportSystemError(errno,
                                 _("failed to seek to end of %s"), path);
            goto cleanup;
        }
        info->physical = end;
        info->capacity = end;
    }

    /* If the file we probed has a capacity set, then override
     * what we calculated from file/block extents */
    if (meta.capacity)
        info->capacity = meta.capacity;

7786
    /* Set default value .. */
7787 7788
    info->allocation = info->physical;

7789 7790 7791
    /* ..but if guest is running & not using raw
       disk format and on a block device, then query
       highest allocated extent from QEMU */
7792
    if (disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK &&
7793
        format != VIR_STORAGE_FILE_RAW &&
7794 7795 7796 7797
        S_ISBLK(sb.st_mode)) {
        qemuDomainObjPrivatePtr priv = vm->privateData;
        if (qemuDomainObjBeginJob(vm) < 0)
            goto cleanup;
7798 7799 7800 7801 7802 7803 7804 7805 7806
        if (!virDomainObjIsActive(vm))
            ret = 0;
        else {
            qemuDomainObjEnterMonitor(vm);
            ret = qemuMonitorGetBlockExtent(priv->mon,
                                            disk->info.alias,
                                            &info->allocation);
            qemuDomainObjExitMonitor(vm);
        }
7807 7808 7809 7810 7811 7812

        if (qemuDomainObjEndJob(vm) == 0)
            vm = NULL;
    } else {
        ret = 0;
    }
7813 7814

cleanup:
7815
    VIR_FORCE_CLOSE(fd);
7816 7817 7818 7819 7820 7821
    if (vm)
        virDomainObjUnlock(vm);
    return ret;
}


7822
static int
7823 7824 7825 7826
qemuDomainEventRegister(virConnectPtr conn,
                        virConnectDomainEventCallback callback,
                        void *opaque,
                        virFreeCallback freecb)
7827
{
7828 7829 7830
    struct qemud_driver *driver = conn->privateData;
    int ret;

7831
    qemuDriverLock(driver);
7832 7833
    ret = virDomainEventCallbackListAdd(conn, driver->domainEventCallbacks,
                                        callback, opaque, freecb);
7834
    qemuDriverUnlock(driver);
7835

7836
    return ret;
7837 7838
}

7839

7840
static int
7841 7842
qemuDomainEventDeregister(virConnectPtr conn,
                          virConnectDomainEventCallback callback)
7843
{
7844 7845 7846
    struct qemud_driver *driver = conn->privateData;
    int ret;

7847
    qemuDriverLock(driver);
7848 7849 7850 7851 7852 7853
    if (driver->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDelete(conn, driver->domainEventCallbacks,
                                                   callback);
    else
        ret = virDomainEventCallbackListRemove(conn, driver->domainEventCallbacks,
                                               callback);
7854
    qemuDriverUnlock(driver);
7855

7856
    return ret;
7857 7858
}

7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901

static int
qemuDomainEventRegisterAny(virConnectPtr conn,
                           virDomainPtr dom,
                           int eventID,
                           virConnectDomainEventGenericCallback callback,
                           void *opaque,
                           virFreeCallback freecb)
{
    struct qemud_driver *driver = conn->privateData;
    int ret;

    qemuDriverLock(driver);
    ret = virDomainEventCallbackListAddID(conn,
                                          driver->domainEventCallbacks,
                                          dom, eventID,
                                          callback, opaque, freecb);
    qemuDriverUnlock(driver);

    return ret;
}


static int
qemuDomainEventDeregisterAny(virConnectPtr conn,
                             int callbackID)
{
    struct qemud_driver *driver = conn->privateData;
    int ret;

    qemuDriverLock(driver);
    if (driver->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDeleteID(conn, driver->domainEventCallbacks,
                                                     callbackID);
    else
        ret = virDomainEventCallbackListRemoveID(conn, driver->domainEventCallbacks,
                                                 callbackID);
    qemuDriverUnlock(driver);

    return ret;
}


7902 7903
static void qemuDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
7904
                                        virConnectDomainEventGenericCallback cb,
7905 7906
                                        void *cbopaque,
                                        void *opaque)
7907
{
7908
    struct qemud_driver *driver = opaque;
7909

7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953
    /* 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);
7954 7955
}

D
Daniel Veillard 已提交
7956 7957
/* Migration support. */

7958 7959 7960 7961 7962
static bool ATTRIBUTE_NONNULL(1)
qemuDomainIsMigratable(virDomainDefPtr def)
{
    if (def->nhostdevs > 0) {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
7963
            "%s", _("Domain with assigned host devices cannot be migrated"));
7964 7965 7966 7967 7968 7969
        return false;
    }

    return true;
}

C
Chris Lalancette 已提交
7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990
/* Prepare is the first step, and it runs on the destination host.
 *
 * This version starts an empty VM listening on a localhost TCP port, and
 * sets up the corresponding virStream to handle the incoming data.
 */
static int
qemudDomainMigratePrepareTunnel(virConnectPtr dconn,
                                virStreamPtr st,
                                unsigned long flags,
                                const char *dname,
                                unsigned long resource ATTRIBUTE_UNUSED,
                                const char *dom_xml)
{
    struct qemud_driver *driver = dconn->privateData;
    virDomainDefPtr def = NULL;
    virDomainObjPtr vm = NULL;
    char *migrateFrom;
    virDomainEventPtr event = NULL;
    int ret = -1;
    int internalret;
    char *unixfile = NULL;
7991
    unsigned long long qemuCmdFlags;
7992 7993 7994 7995 7996 7997 7998 7999
    qemuDomainObjPrivatePtr priv = NULL;
    struct timeval now;

    if (gettimeofday(&now, NULL) < 0) {
        virReportSystemError(errno, "%s",
                             _("cannot get time of day"));
        return -1;
    }
C
Chris Lalancette 已提交
8000 8001 8002

    qemuDriverLock(driver);
    if (!dom_xml) {
8003 8004
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("no domain XML passed"));
C
Chris Lalancette 已提交
8005 8006 8007
        goto cleanup;
    }
    if (!(flags & VIR_MIGRATE_TUNNELLED)) {
8008
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
C
Chris Lalancette 已提交
8009 8010 8011 8012
                         "%s", _("PrepareTunnel called but no TUNNELLED flag set"));
        goto cleanup;
    }
    if (st == NULL) {
8013 8014
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("tunnelled migration requested but NULL stream passed"));
C
Chris Lalancette 已提交
8015 8016 8017 8018
        goto cleanup;
    }

    /* Parse the domain XML. */
8019
    if (!(def = virDomainDefParseString(driver->caps, dom_xml,
C
Chris Lalancette 已提交
8020
                                        VIR_DOMAIN_XML_INACTIVE))) {
8021
        qemuReportError(VIR_ERR_OPERATION_FAILED,
8022 8023
                        "%s", _("failed to parse XML, libvirt version may be "
                                "different between source and destination host"));
C
Chris Lalancette 已提交
8024 8025 8026
        goto cleanup;
    }

8027 8028 8029
    if (!qemuDomainIsMigratable(def))
        goto cleanup;

C
Chris Lalancette 已提交
8030
    /* Target domain name, maybe renamed. */
8031 8032 8033 8034 8035 8036
    if (dname) {
        VIR_FREE(def->name);
        def->name = strdup(dname);
        if (def->name == NULL)
            goto cleanup;
    }
C
Chris Lalancette 已提交
8037

8038 8039
    if (virDomainObjIsDuplicate(&driver->domains, def, 1) < 0)
        goto cleanup;
C
Chris Lalancette 已提交
8040

8041
    if (!(vm = virDomainAssignDef(driver->caps,
C
Chris Lalancette 已提交
8042
                                  &driver->domains,
8043
                                  def, true))) {
8044
        /* virDomainAssignDef already set the error */
C
Chris Lalancette 已提交
8045 8046 8047
        goto cleanup;
    }
    def = NULL;
8048
    priv = vm->privateData;
C
Chris Lalancette 已提交
8049

8050 8051
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;
8052
    priv->jobActive = QEMU_JOB_MIGRATION_OUT;
8053

C
Chris Lalancette 已提交
8054 8055 8056 8057
    /* Domain starts inactive, even if the domain XML had an id field. */
    vm->def->id = -1;

    if (virAsprintf(&unixfile, "%s/qemu.tunnelmigrate.dest.%s",
8058
                    driver->libDir, vm->def->name) < 0) {
8059
        virReportOOMError();
8060
        goto endjob;
C
Chris Lalancette 已提交
8061 8062 8063 8064
    }
    unlink(unixfile);

    /* check that this qemu version supports the interactive exec */
8065
    if (qemuCapsExtractVersionInfo(vm->def->emulator, NULL, &qemuCmdFlags) < 0) {
8066 8067 8068
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Cannot determine QEMU argv syntax %s"),
                        vm->def->emulator);
8069
        goto endjob;
C
Chris Lalancette 已提交
8070 8071 8072 8073 8074 8075
    }
    if (qemuCmdFlags & QEMUD_CMD_FLAG_MIGRATE_QEMU_UNIX)
        internalret = virAsprintf(&migrateFrom, "unix:%s", unixfile);
    else if (qemuCmdFlags & QEMUD_CMD_FLAG_MIGRATE_QEMU_EXEC)
        internalret = virAsprintf(&migrateFrom, "exec:nc -U -l %s", unixfile);
    else {
8076 8077
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("Destination qemu is too old to support tunnelled migration"));
8078
        goto endjob;
C
Chris Lalancette 已提交
8079 8080
    }
    if (internalret < 0) {
8081
        virReportOOMError();
8082
        goto endjob;
C
Chris Lalancette 已提交
8083 8084 8085 8086
    }
    /* Start the QEMU daemon, with the same command-line arguments plus
     * -incoming unix:/path/to/file or exec:nc -U /path/to/file
     */
8087
    internalret = qemudStartVMDaemon(dconn, driver, vm, migrateFrom, true,
8088
                                     -1, NULL, VIR_VM_OP_MIGRATE_IN_START);
C
Chris Lalancette 已提交
8089 8090
    VIR_FREE(migrateFrom);
    if (internalret < 0) {
8091
        qemuDomainStartAudit(vm, "migrated", false);
C
Chris Lalancette 已提交
8092 8093 8094 8095 8096 8097 8098
        /* Note that we don't set an error here because qemudStartVMDaemon
         * should have already done that.
         */
        if (!vm->persistent) {
            virDomainRemoveInactive(&driver->domains, vm);
            vm = NULL;
        }
8099
        goto endjob;
C
Chris Lalancette 已提交
8100 8101
    }

8102 8103 8104
    if (virFDStreamConnectUNIX(st,
                               unixfile,
                               false) < 0) {
8105
        qemuDomainStartAudit(vm, "migrated", false);
8106
        qemudShutdownVMDaemon(driver, vm, 0);
8107
        if (!vm->persistent) {
8108 8109
            if (qemuDomainObjEndJob(vm) > 0)
                virDomainRemoveInactive(&driver->domains, vm);
8110 8111
            vm = NULL;
        }
8112
        virReportSystemError(errno,
C
Chris Lalancette 已提交
8113 8114
                             _("cannot open unix socket '%s' for tunnelled migration"),
                             unixfile);
8115
        goto endjob;
C
Chris Lalancette 已提交
8116 8117
    }

8118
    qemuDomainStartAudit(vm, "migrated", true);
8119

C
Chris Lalancette 已提交
8120 8121 8122 8123 8124
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_MIGRATED);
    ret = 0;

8125
endjob:
8126 8127 8128
    if (vm &&
        qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
8129

8130 8131 8132 8133 8134 8135 8136 8137 8138
    /* We set a fake job active which is held across
     * API calls until the finish() call. This prevents
     * any other APIs being invoked while incoming
     * migration is taking place
     */
    if (vm &&
        virDomainObjIsActive(vm)) {
        priv->jobActive = QEMU_JOB_MIGRATION_IN;
        priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED;
8139
        priv->jobStart = timeval_to_ms(now);
8140 8141
    }

C
Chris Lalancette 已提交
8142 8143
cleanup:
    virDomainDefFree(def);
8144 8145
    if (unixfile)
        unlink(unixfile);
C
Chris Lalancette 已提交
8146 8147 8148 8149 8150 8151 8152 8153 8154
    VIR_FREE(unixfile);
    if (vm)
        virDomainObjUnlock(vm);
    if (event)
        qemuDomainEventQueue(driver, event);
    qemuDriverUnlock(driver);
    return ret;
}

D
Daniel Veillard 已提交
8155 8156 8157 8158
/* Prepare is the first step, and it runs on the destination host.
 *
 * This starts an empty VM listening on a TCP port.
 */
8159
static int ATTRIBUTE_NONNULL (5)
D
Daniel Veillard 已提交
8160 8161 8162 8163 8164
qemudDomainMigratePrepare2 (virConnectPtr dconn,
                            char **cookie ATTRIBUTE_UNUSED,
                            int *cookielen ATTRIBUTE_UNUSED,
                            const char *uri_in,
                            char **uri_out,
C
Chris Lalancette 已提交
8165
                            unsigned long flags,
D
Daniel Veillard 已提交
8166 8167 8168 8169 8170
                            const char *dname,
                            unsigned long resource ATTRIBUTE_UNUSED,
                            const char *dom_xml)
{
    static int port = 0;
8171 8172
    struct qemud_driver *driver = dconn->privateData;
    virDomainDefPtr def = NULL;
D
Daniel Veillard 已提交
8173 8174
    virDomainObjPtr vm = NULL;
    int this_port;
C
Chris Lalancette 已提交
8175
    char *hostname = NULL;
D
Daniel Veillard 已提交
8176 8177
    char migrateFrom [64];
    const char *p;
8178
    virDomainEventPtr event = NULL;
8179
    int ret = -1;
8180
    int internalret;
8181 8182 8183 8184 8185 8186 8187 8188
    qemuDomainObjPrivatePtr priv = NULL;
    struct timeval now;

    if (gettimeofday(&now, NULL) < 0) {
        virReportSystemError(errno, "%s",
                             _("cannot get time of day"));
        return -1;
    }
8189

C
Chris Lalancette 已提交
8190 8191 8192 8193 8194 8195 8196 8197 8198
    virCheckFlags(VIR_MIGRATE_LIVE |
                  VIR_MIGRATE_PEER2PEER |
                  VIR_MIGRATE_TUNNELLED |
                  VIR_MIGRATE_PERSIST_DEST |
                  VIR_MIGRATE_UNDEFINE_SOURCE |
                  VIR_MIGRATE_PAUSED |
                  VIR_MIGRATE_NON_SHARED_DISK |
                  VIR_MIGRATE_NON_SHARED_INC, -1);

8199
    *uri_out = NULL;
D
Daniel Veillard 已提交
8200

8201
    qemuDriverLock(driver);
C
Chris Lalancette 已提交
8202 8203 8204 8205
    if (flags & VIR_MIGRATE_TUNNELLED) {
        /* this is a logical error; we never should have gotten here with
         * VIR_MIGRATE_TUNNELLED set
         */
8206 8207
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("Tunnelled migration requested but invalid RPC method called"));
C
Chris Lalancette 已提交
8208 8209 8210
        goto cleanup;
    }

D
Daniel Veillard 已提交
8211
    if (!dom_xml) {
8212 8213
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("no domain XML passed"));
8214
        goto cleanup;
D
Daniel Veillard 已提交
8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231
    }

    /* 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 */
C
Chris Lalancette 已提交
8232
        if ((hostname = virGetHostname(NULL)) == NULL)
8233
            goto cleanup;
D
Daniel Veillard 已提交
8234

C
Chris Lalancette 已提交
8235 8236 8237 8238 8239 8240
        if (STRPREFIX(hostname, "localhost")) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("hostname on destination resolved to localhost, but migration requires an FQDN"));
            goto cleanup;
        }

8241 8242 8243 8244 8245
        /* XXX this really should have been a properly well-formed
         * URI, but we can't add in tcp:// now without breaking
         * compatability with old targets. We at least make the
         * new targets accept both syntaxes though.
         */
D
Daniel Veillard 已提交
8246
        /* Caller frees */
8247 8248
        internalret = virAsprintf(uri_out, "tcp:%s:%d", hostname, this_port);
        if (internalret < 0) {
8249
            virReportOOMError();
8250
            goto cleanup;
D
Daniel Veillard 已提交
8251 8252 8253 8254 8255 8256
        }
    } 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.
         */
8257
        if (!STRPREFIX (uri_in, "tcp:")) {
8258 8259
            qemuReportError (VIR_ERR_INVALID_ARG,
                             "%s", _("only tcp URIs are supported for KVM/QEMU migrations"));
8260
            goto cleanup;
D
Daniel Veillard 已提交
8261 8262 8263 8264
        }

        /* Get the port number. */
        p = strrchr (uri_in, ':');
8265 8266 8267 8268 8269 8270 8271 8272
        if (p == strchr(uri_in, ':')) {
            /* Generate a port */
            this_port = QEMUD_MIGRATION_FIRST_PORT + port++;
            if (port == QEMUD_MIGRATION_NUM_PORTS)
                port = 0;

            /* Caller frees */
            if (virAsprintf(uri_out, "%s:%d", uri_in, this_port) < 0) {
8273
                virReportOOMError();
8274 8275 8276 8277 8278 8279 8280
                goto cleanup;
            }

        } else {
            p++; /* definitely has a ':' in it, see above */
            this_port = virParseNumber (&p);
            if (this_port == -1 || p-uri_in != strlen (uri_in)) {
8281 8282
                qemuReportError(VIR_ERR_INVALID_ARG,
                                "%s", _("URI ended with incorrect ':port'"));
8283 8284
                goto cleanup;
            }
D
Daniel Veillard 已提交
8285 8286 8287
        }
    }

8288
    if (*uri_out)
8289 8290
        VIR_DEBUG("Generated uri_out=%s", *uri_out);

D
Daniel Veillard 已提交
8291
    /* Parse the domain XML. */
8292
    if (!(def = virDomainDefParseString(driver->caps, dom_xml,
8293
                                        VIR_DOMAIN_XML_INACTIVE))) {
8294 8295
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("failed to parse XML"));
8296
        goto cleanup;
D
Daniel Veillard 已提交
8297 8298
    }

8299 8300 8301
    if (!qemuDomainIsMigratable(def))
        goto cleanup;

D
Daniel Veillard 已提交
8302
    /* Target domain name, maybe renamed. */
8303 8304 8305 8306 8307 8308
    if (dname) {
        VIR_FREE(def->name);
        def->name = strdup(dname);
        if (def->name == NULL)
            goto cleanup;
    }
D
Daniel Veillard 已提交
8309

8310 8311
    if (virDomainObjIsDuplicate(&driver->domains, def, 1) < 0)
        goto cleanup;
D
Daniel Veillard 已提交
8312

8313
    if (!(vm = virDomainAssignDef(driver->caps,
D
Daniel Veillard 已提交
8314
                                  &driver->domains,
8315
                                  def, true))) {
8316
        /* virDomainAssignDef already set the error */
8317
        goto cleanup;
D
Daniel Veillard 已提交
8318
    }
8319
    def = NULL;
8320
    priv = vm->privateData;
D
Daniel Veillard 已提交
8321

8322 8323
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;
8324
    priv->jobActive = QEMU_JOB_MIGRATION_OUT;
8325

D
Daniel Veillard 已提交
8326 8327 8328 8329 8330 8331 8332
    /* 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);
8333
    if (qemudStartVMDaemon (dconn, driver, vm, migrateFrom, true,
8334
                            -1, NULL, VIR_VM_OP_MIGRATE_IN_START) < 0) {
8335
        qemuDomainStartAudit(vm, "migrated", false);
8336 8337 8338
        /* Note that we don't set an error here because qemudStartVMDaemon
         * should have already done that.
         */
8339
        if (!vm->persistent) {
8340 8341
            if (qemuDomainObjEndJob(vm) > 0)
                virDomainRemoveInactive(&driver->domains, vm);
8342 8343
            vm = NULL;
        }
8344
        goto endjob;
D
Daniel Veillard 已提交
8345
    }
8346

8347
    qemuDomainStartAudit(vm, "migrated", true);
8348 8349 8350
    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_MIGRATED);
8351
    ret = 0;
D
Daniel Veillard 已提交
8352

8353
endjob:
8354 8355 8356
    if (vm &&
        qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
8357

8358 8359 8360 8361 8362 8363 8364 8365 8366
    /* We set a fake job active which is held across
     * API calls until the finish() call. This prevents
     * any other APIs being invoked while incoming
     * migration is taking place
     */
    if (vm &&
        virDomainObjIsActive(vm)) {
        priv->jobActive = QEMU_JOB_MIGRATION_IN;
        priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED;
8367
        priv->jobStart = timeval_to_ms(now);
8368 8369
    }

8370
cleanup:
C
Chris Lalancette 已提交
8371
    VIR_FREE(hostname);
8372
    virDomainDefFree(def);
C
Chris Lalancette 已提交
8373
    if (ret != 0)
8374
        VIR_FREE(*uri_out);
8375 8376
    if (vm)
        virDomainObjUnlock(vm);
8377 8378
    if (event)
        qemuDomainEventQueue(driver, event);
8379
    qemuDriverUnlock(driver);
8380
    return ret;
C
Chris Lalancette 已提交
8381 8382 8383

}

8384 8385 8386 8387

/* Perform migration using QEMU's native TCP migrate support,
 * not encrypted obviously
 */
8388
static int doNativeMigrate(struct qemud_driver *driver,
8389 8390
                           virDomainObjPtr vm,
                           const char *uri,
8391
                           unsigned int flags,
8392 8393 8394 8395
                           const char *dname ATTRIBUTE_UNUSED,
                           unsigned long resource)
{
    int ret = -1;
8396
    xmlURIPtr uribits = NULL;
8397
    qemuDomainObjPrivatePtr priv = vm->privateData;
8398
    unsigned int background_flags = QEMU_MONITOR_MIGRATE_BACKGROUND;
8399

8400 8401 8402 8403 8404
    /* Issue the migrate command. */
    if (STRPREFIX(uri, "tcp:") && !STRPREFIX(uri, "tcp://")) {
        /* HACK: source host generates bogus URIs, so fix them up */
        char *tmpuri;
        if (virAsprintf(&tmpuri, "tcp://%s", uri + strlen("tcp:")) < 0) {
8405
            virReportOOMError();
8406 8407 8408 8409 8410 8411 8412 8413
            goto cleanup;
        }
        uribits = xmlParseURI(tmpuri);
        VIR_FREE(tmpuri);
    } else {
        uribits = xmlParseURI(uri);
    }
    if (!uribits) {
8414 8415
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("cannot parse URI %s"), uri);
8416 8417 8418
        goto cleanup;
    }

8419
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
8420
    if (resource > 0 &&
8421
        qemuMonitorSetMigrationSpeed(priv->mon, resource) < 0) {
8422
        qemuDomainObjExitMonitorWithDriver(driver, vm);
8423
        goto cleanup;
8424
    }
8425

8426 8427 8428 8429 8430 8431 8432 8433
    if (flags & VIR_MIGRATE_NON_SHARED_DISK)
        background_flags |= QEMU_MONITOR_MIGRATE_NON_SHARED_DISK;

    if (flags & VIR_MIGRATE_NON_SHARED_INC)
        background_flags |= QEMU_MONITOR_MIGRATE_NON_SHARED_INC;

    if (qemuMonitorMigrateToHost(priv->mon, background_flags, uribits->server,
                                 uribits->port) < 0) {
8434
        qemuDomainObjExitMonitorWithDriver(driver, vm);
8435 8436
        goto cleanup;
    }
8437
    qemuDomainObjExitMonitorWithDriver(driver, vm);
8438

8439
    if (qemuDomainWaitForMigrationComplete(driver, vm) < 0)
8440 8441 8442 8443 8444 8445 8446 8447 8448 8449
        goto cleanup;

    ret = 0;

cleanup:
    xmlFreeURI(uribits);
    return ret;
}


8450 8451
#define TUNNEL_SEND_BUF_SIZE 65536

8452
static int doTunnelSendAll(virStreamPtr st,
8453 8454
                           int sock)
{
8455 8456 8457 8458 8459 8460 8461 8462
    char *buffer;
    int nbytes = TUNNEL_SEND_BUF_SIZE;

    if (VIR_ALLOC_N(buffer, TUNNEL_SEND_BUF_SIZE) < 0) {
        virReportOOMError();
        virStreamAbort(st);
        return -1;
    }
8463 8464 8465 8466 8467

    /* XXX should honour the 'resource' parameter here */
    for (;;) {
        nbytes = saferead(sock, buffer, nbytes);
        if (nbytes < 0) {
8468
            virReportSystemError(errno, "%s",
8469
                                 _("tunnelled migration failed to read from qemu"));
8470 8471
            virStreamAbort(st);
            VIR_FREE(buffer);
8472 8473 8474 8475 8476 8477 8478
            return -1;
        }
        else if (nbytes == 0)
            /* EOF; get out of here */
            break;

        if (virStreamSend(st, buffer, nbytes) < 0) {
8479 8480
            qemuReportError(VIR_ERR_OPERATION_FAILED, "%s",
                            _("Failed to write migration data to remote libvirtd"));
8481
            VIR_FREE(buffer);
8482 8483 8484 8485
            return -1;
        }
    }

8486 8487
    VIR_FREE(buffer);

8488 8489 8490 8491 8492 8493 8494
    if (virStreamFinish(st) < 0)
        /* virStreamFinish set the error for us */
        return -1;

    return 0;
}

C
Chris Lalancette 已提交
8495
static int doTunnelMigrate(virDomainPtr dom,
8496
                           struct qemud_driver *driver,
8497
                           virConnectPtr dconn,
C
Chris Lalancette 已提交
8498
                           virDomainObjPtr vm,
8499
                           const char *dom_xml,
C
Chris Lalancette 已提交
8500 8501 8502 8503 8504
                           const char *uri,
                           unsigned long flags,
                           const char *dname,
                           unsigned long resource)
{
8505
    qemuDomainObjPrivatePtr priv = vm->privateData;
8506 8507
    int client_sock = -1;
    int qemu_sock = -1;
C
Chris Lalancette 已提交
8508 8509
    struct sockaddr_un sa_qemu, sa_client;
    socklen_t addrlen;
8510
    virDomainPtr ddomain = NULL;
C
Chris Lalancette 已提交
8511
    int retval = -1;
8512
    virStreamPtr st = NULL;
C
Chris Lalancette 已提交
8513 8514
    char *unixfile = NULL;
    int internalret;
8515
    unsigned long long qemuCmdFlags;
C
Chris Lalancette 已提交
8516 8517
    int status;
    unsigned long long transferred, remaining, total;
8518
    unsigned int background_flags = QEMU_MONITOR_MIGRATE_BACKGROUND;
C
Chris Lalancette 已提交
8519

8520 8521 8522 8523 8524 8525 8526 8527
    /*
     * The order of operations is important here to avoid touching
     * the source VM until we are very sure we can successfully
     * start the migration operation.
     *
     *   1. setup local support infrastructure (eg sockets)
     *   2. setup destination fully
     *   3. start migration on source
C
Chris Lalancette 已提交
8528 8529
     */

8530

8531
    /* Stage 1. setup local support infrastructure */
C
Chris Lalancette 已提交
8532 8533

    if (virAsprintf(&unixfile, "%s/qemu.tunnelmigrate.src.%s",
8534
                    driver->libDir, vm->def->name) < 0) {
8535
        virReportOOMError();
8536
        goto cleanup;
C
Chris Lalancette 已提交
8537 8538 8539 8540
    }

    qemu_sock = socket(AF_UNIX, SOCK_STREAM, 0);
    if (qemu_sock < 0) {
8541
        virReportSystemError(errno, "%s",
C
Chris Lalancette 已提交
8542
                             _("cannot open tunnelled migration socket"));
8543
        goto cleanup;
C
Chris Lalancette 已提交
8544 8545 8546 8547 8548
    }
    memset(&sa_qemu, 0, sizeof(sa_qemu));
    sa_qemu.sun_family = AF_UNIX;
    if (virStrcpy(sa_qemu.sun_path, unixfile,
                  sizeof(sa_qemu.sun_path)) == NULL) {
8549 8550 8551
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Unix socket '%s' too big for destination"),
                        unixfile);
8552
        goto cleanup;
C
Chris Lalancette 已提交
8553 8554 8555
    }
    unlink(unixfile);
    if (bind(qemu_sock, (struct sockaddr *)&sa_qemu, sizeof(sa_qemu)) < 0) {
8556
        virReportSystemError(errno,
C
Chris Lalancette 已提交
8557 8558
                             _("Cannot bind to unix socket '%s' for tunnelled migration"),
                             unixfile);
8559
        goto cleanup;
C
Chris Lalancette 已提交
8560 8561
    }
    if (listen(qemu_sock, 1) < 0) {
8562
        virReportSystemError(errno,
C
Chris Lalancette 已提交
8563 8564
                             _("Cannot listen on unix socket '%s' for tunnelled migration"),
                             unixfile);
8565
        goto cleanup;
C
Chris Lalancette 已提交
8566 8567
    }

8568 8569 8570 8571 8572 8573 8574
    if (chown(unixfile, qemu_driver->user, qemu_driver->group) < 0) {
        virReportSystemError(errno,
                             _("Cannot change unix socket '%s' owner"),
                             unixfile);
        goto cleanup;
    }

C
Chris Lalancette 已提交
8575
    /* check that this qemu version supports the unix migration */
8576
    if (qemuCapsExtractVersionInfo(vm->def->emulator, NULL, &qemuCmdFlags) < 0) {
8577 8578 8579
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Cannot extract Qemu version from '%s'"),
                        vm->def->emulator);
8580 8581 8582 8583 8584
        goto cleanup;
    }

    if (!(qemuCmdFlags & QEMUD_CMD_FLAG_MIGRATE_QEMU_UNIX) &&
        !(qemuCmdFlags & QEMUD_CMD_FLAG_MIGRATE_QEMU_EXEC)) {
8585 8586
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("Source qemu is too old to support tunnelled migration"));
8587
        goto cleanup;
C
Chris Lalancette 已提交
8588
    }
8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600


    /* Stage 2. setup destination fully
     *
     * Once stage 2 has completed successfully, we *must* call finish
     * to cleanup the target whether we succeed or fail
     */
    st = virStreamNew(dconn, 0);
    if (st == NULL)
        /* virStreamNew only fails on OOM, and it reports the error itself */
        goto cleanup;

8601
    qemuDomainObjEnterRemoteWithDriver(driver, vm);
8602 8603 8604
    internalret = dconn->driver->domainMigratePrepareTunnel(dconn, st,
                                                            flags, dname,
                                                            resource, dom_xml);
8605
    qemuDomainObjExitRemoteWithDriver(driver, vm);
8606 8607 8608 8609 8610

    if (internalret < 0)
        /* domainMigratePrepareTunnel sets the error for us */
        goto cleanup;

8611 8612 8613 8614 8615 8616 8617 8618 8619
    /* the domain may have shutdown or crashed while we had the locks dropped
     * in qemuDomainObjEnterRemoteWithDriver, so check again
     */
    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("guest unexpectedly quit"));
        goto cleanup;
    }

8620
    /*   3. start migration on source */
8621
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
8622 8623 8624 8625 8626 8627 8628 8629
    if (flags & VIR_MIGRATE_NON_SHARED_DISK)
        background_flags |= QEMU_MONITOR_MIGRATE_NON_SHARED_DISK;
    if (flags & VIR_MIGRATE_NON_SHARED_INC)
        background_flags |= QEMU_MONITOR_MIGRATE_NON_SHARED_INC;
    if (qemuCmdFlags & QEMUD_CMD_FLAG_MIGRATE_QEMU_UNIX){
        internalret = qemuMonitorMigrateToUnix(priv->mon, background_flags,
                                               unixfile);
    }
C
Chris Lalancette 已提交
8630 8631
    else if (qemuCmdFlags & QEMUD_CMD_FLAG_MIGRATE_QEMU_EXEC) {
        const char *args[] = { "nc", "-U", unixfile, NULL };
8632
        internalret = qemuMonitorMigrateToCommand(priv->mon, QEMU_MONITOR_MIGRATE_BACKGROUND, args);
8633 8634
    } else {
        internalret = -1;
C
Chris Lalancette 已提交
8635
    }
8636
    qemuDomainObjExitMonitorWithDriver(driver, vm);
C
Chris Lalancette 已提交
8637
    if (internalret < 0) {
8638 8639
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("tunnelled migration monitor command failed"));
8640
        goto finish;
C
Chris Lalancette 已提交
8641 8642
    }

8643 8644 8645 8646 8647 8648
    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("guest unexpectedly quit"));
        goto cleanup;
    }

8649 8650 8651
    /* From this point onwards we *must* call cancel to abort the
     * migration on source if anything goes wrong */

C
Chris Lalancette 已提交
8652 8653 8654
    /* it is also possible that the migrate didn't fail initially, but
     * rather failed later on.  Check the output of "info migrate"
     */
8655
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
8656 8657
    if (qemuMonitorGetMigrationStatus(priv->mon,
                                      &status,
C
Chris Lalancette 已提交
8658 8659 8660
                                      &transferred,
                                      &remaining,
                                      &total) < 0) {
8661
        qemuDomainObjExitMonitorWithDriver(driver, vm);
8662
        goto cancel;
C
Chris Lalancette 已提交
8663
    }
8664
    qemuDomainObjExitMonitorWithDriver(driver, vm);
C
Chris Lalancette 已提交
8665 8666

    if (status == QEMU_MONITOR_MIGRATION_STATUS_ERROR) {
8667 8668
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s",_("migrate failed"));
8669
        goto cancel;
C
Chris Lalancette 已提交
8670 8671 8672 8673 8674 8675
    }

    addrlen = sizeof(sa_client);
    while ((client_sock = accept(qemu_sock, (struct sockaddr *)&sa_client, &addrlen)) < 0) {
        if (errno == EAGAIN || errno == EINTR)
            continue;
8676
        virReportSystemError(errno, "%s",
C
Chris Lalancette 已提交
8677
                             _("tunnelled migration failed to accept from qemu"));
8678
        goto cancel;
C
Chris Lalancette 已提交
8679 8680
    }

8681
    retval = doTunnelSendAll(st, client_sock);
8682

8683
cancel:
8684
    if (retval != 0 && virDomainObjIsActive(vm)) {
8685
        qemuDomainObjEnterMonitorWithDriver(driver, vm);
8686
        qemuMonitorMigrateCancel(priv->mon);
8687
        qemuDomainObjExitMonitorWithDriver(driver, vm);
8688
    }
C
Chris Lalancette 已提交
8689

8690
finish:
C
Chris Lalancette 已提交
8691
    dname = dname ? dname : dom->name;
8692
    qemuDomainObjEnterRemoteWithDriver(driver, vm);
C
Chris Lalancette 已提交
8693 8694
    ddomain = dconn->driver->domainMigrateFinish2
        (dconn, dname, NULL, 0, uri, flags, retval);
8695
    qemuDomainObjExitRemoteWithDriver(driver, vm);
8696 8697

cleanup:
8698 8699
    VIR_FORCE_CLOSE(client_sock);
    VIR_FORCE_CLOSE(qemu_sock);
8700

C
Chris Lalancette 已提交
8701 8702 8703
    if (ddomain)
        virUnrefDomain(ddomain);

8704 8705 8706 8707
    if (unixfile) {
        unlink(unixfile);
        VIR_FREE(unixfile);
    }
C
Chris Lalancette 已提交
8708

8709 8710 8711
    if (st)
        /* don't call virStreamFree(), because that resets any pending errors */
        virUnrefStream(st);
8712 8713 8714 8715
    return retval;
}


8716 8717 8718 8719
/* This is essentially a simplified re-impl of
 * virDomainMigrateVersion2 from libvirt.c, but running in source
 * libvirtd context, instead of client app context */
static int doNonTunnelMigrate(virDomainPtr dom,
8720
                              struct qemud_driver *driver,
8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731
                              virConnectPtr dconn,
                              virDomainObjPtr vm,
                              const char *dom_xml,
                              const char *uri ATTRIBUTE_UNUSED,
                              unsigned long flags,
                              const char *dname,
                              unsigned long resource)
{
    virDomainPtr ddomain = NULL;
    int retval = -1;
    char *uri_out = NULL;
8732
    int rc;
8733

8734
    qemuDomainObjEnterRemoteWithDriver(driver, vm);
8735 8736
    /* NB we don't pass 'uri' into this, since that's the libvirtd
     * URI in this context - so we let dest pick it */
8737 8738 8739 8740 8741 8742 8743 8744 8745
    rc = dconn->driver->domainMigratePrepare2(dconn,
                                              NULL, /* cookie */
                                              0, /* cookielen */
                                              NULL, /* uri */
                                              &uri_out,
                                              flags, dname,
                                              resource, dom_xml);
    qemuDomainObjExitRemoteWithDriver(driver, vm);
    if (rc < 0)
8746 8747 8748
        /* domainMigratePrepare2 sets the error for us */
        goto cleanup;

8749 8750 8751 8752 8753 8754 8755 8756 8757
    /* the domain may have shutdown or crashed while we had the locks dropped
     * in qemuDomainObjEnterRemoteWithDriver, so check again
     */
    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("guest unexpectedly quit"));
        goto cleanup;
    }

8758
    if (uri_out == NULL) {
8759 8760
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("domainMigratePrepare2 did not set uri"));
8761
        goto cleanup;
8762 8763
    }

8764
    if (doNativeMigrate(driver, vm, uri_out, flags, dname, resource) < 0)
8765 8766 8767 8768 8769 8770
        goto finish;

    retval = 0;

finish:
    dname = dname ? dname : dom->name;
8771
    qemuDomainObjEnterRemoteWithDriver(driver, vm);
8772 8773
    ddomain = dconn->driver->domainMigrateFinish2
        (dconn, dname, NULL, 0, uri_out, flags, retval);
8774
    qemuDomainObjExitRemoteWithDriver(driver, vm);
8775 8776 8777 8778 8779 8780 8781 8782 8783

    if (ddomain)
        virUnrefDomain(ddomain);

cleanup:
    return retval;
}


8784
static int doPeer2PeerMigrate(virDomainPtr dom,
8785
                              struct qemud_driver *driver,
8786 8787 8788 8789 8790 8791 8792 8793 8794
                              virDomainObjPtr vm,
                              const char *uri,
                              unsigned long flags,
                              const char *dname,
                              unsigned long resource)
{
    int ret = -1;
    virConnectPtr dconn = NULL;
    char *dom_xml;
8795
    bool p2p;
8796 8797 8798 8799 8800

    /* the order of operations is important here; we make sure the
     * destination side is completely setup before we touch the source
     */

8801
    qemuDomainObjEnterRemoteWithDriver(driver, vm);
8802
    dconn = virConnectOpen(uri);
8803
    qemuDomainObjExitRemoteWithDriver(driver, vm);
8804
    if (dconn == NULL) {
8805 8806
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("Failed to connect to remote libvirt URI %s"), uri);
8807 8808
        return -1;
    }
8809 8810 8811 8812 8813 8814

    qemuDomainObjEnterRemoteWithDriver(driver, vm);
    p2p = VIR_DRV_SUPPORTS_FEATURE(dconn->driver, dconn,
                                   VIR_DRV_FEATURE_MIGRATION_P2P);
    qemuDomainObjExitRemoteWithDriver(driver, vm);
    if (!p2p) {
8815 8816
        qemuReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        _("Destination libvirt does not support peer-to-peer migration protocol"));
8817 8818 8819
        goto cleanup;
    }

8820 8821 8822 8823 8824 8825 8826
    /* domain may have been stopped while we were talking to remote daemon */
    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("guest unexpectedly quit"));
        goto cleanup;
    }

8827 8828 8829
    dom_xml = qemudVMDumpXML(driver, vm,
                             VIR_DOMAIN_XML_SECURE |
                             VIR_DOMAIN_XML_UPDATE_CPU);
8830
    if (!dom_xml) {
8831 8832
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("failed to get domain xml"));
8833 8834 8835
        goto cleanup;
    }

8836
    if (flags & VIR_MIGRATE_TUNNELLED)
8837
        ret = doTunnelMigrate(dom, driver, dconn, vm, dom_xml, uri, flags, dname, resource);
8838
    else
8839
        ret = doNonTunnelMigrate(dom, driver, dconn, vm, dom_xml, uri, flags, dname, resource);
8840 8841 8842

cleanup:
    VIR_FREE(dom_xml);
C
Chris Lalancette 已提交
8843
    /* don't call virConnectClose(), because that resets any pending errors */
8844
    qemuDomainObjEnterRemoteWithDriver(driver, vm);
C
Chris Lalancette 已提交
8845
    virUnrefConnect(dconn);
8846
    qemuDomainObjExitRemoteWithDriver(driver, vm);
C
Chris Lalancette 已提交
8847

8848
    return ret;
D
Daniel Veillard 已提交
8849 8850
}

8851

D
Daniel Veillard 已提交
8852 8853 8854 8855 8856 8857
/* 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,
8858
                           unsigned long flags,
8859
                           const char *dname,
D
Daniel Veillard 已提交
8860 8861
                           unsigned long resource)
{
8862 8863
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
8864
    virDomainEventPtr event = NULL;
8865
    int ret = -1;
8866
    int resume = 0;
8867
    qemuDomainObjPrivatePtr priv;
D
Daniel Veillard 已提交
8868

C
Chris Lalancette 已提交
8869 8870 8871 8872 8873 8874 8875 8876 8877
    virCheckFlags(VIR_MIGRATE_LIVE |
                  VIR_MIGRATE_PEER2PEER |
                  VIR_MIGRATE_TUNNELLED |
                  VIR_MIGRATE_PERSIST_DEST |
                  VIR_MIGRATE_UNDEFINE_SOURCE |
                  VIR_MIGRATE_PAUSED |
                  VIR_MIGRATE_NON_SHARED_DISK |
                  VIR_MIGRATE_NON_SHARED_INC, -1);

8878
    qemuDriverLock(driver);
8879
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
D
Daniel Veillard 已提交
8880
    if (!vm) {
8881 8882
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
8883 8884
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
8885
        goto cleanup;
D
Daniel Veillard 已提交
8886
    }
8887
    priv = vm->privateData;
D
Daniel Veillard 已提交
8888

8889 8890
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;
8891
    priv->jobActive = QEMU_JOB_MIGRATION_OUT;
8892

D
Daniel P. Berrange 已提交
8893
    if (!virDomainObjIsActive(vm)) {
8894 8895
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
8896
        goto endjob;
D
Daniel Veillard 已提交
8897 8898
    }

8899 8900 8901
    memset(&priv->jobInfo, 0, sizeof(priv->jobInfo));
    priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED;

8902
    resume = vm->state == VIR_DOMAIN_RUNNING;
8903
    if (!(flags & VIR_MIGRATE_LIVE) && vm->state == VIR_DOMAIN_RUNNING) {
8904
        if (qemuDomainMigrateOffline(driver, vm) < 0)
8905
            goto endjob;
8906 8907
    }

8908
    if ((flags & (VIR_MIGRATE_TUNNELLED | VIR_MIGRATE_PEER2PEER))) {
8909
        if (doPeer2PeerMigrate(dom, driver, vm, uri, flags, dname, resource) < 0)
8910
            /* doPeer2PeerMigrate already set the error, so just get out */
8911
            goto endjob;
8912
    } else {
8913
        if (doNativeMigrate(driver, vm, uri, flags, dname, resource) < 0)
8914
            goto endjob;
8915 8916
    }

D
Daniel Veillard 已提交
8917
    /* Clean up the source domain. */
8918
    qemudShutdownVMDaemon(driver, vm, 1);
8919
    qemuDomainStopAudit(vm, "migrated");
8920
    resume = 0;
8921 8922 8923 8924

    event = virDomainEventNewFromObj(vm,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_MIGRATED);
C
Chris Lalancette 已提交
8925
    if (!vm->persistent || (flags & VIR_MIGRATE_UNDEFINE_SOURCE)) {
8926
        virDomainDeleteConfig(driver->configDir, driver->autostartDir, vm);
8927 8928
        if (qemuDomainObjEndJob(vm) > 0)
            virDomainRemoveInactive(&driver->domains, vm);
8929 8930
        vm = NULL;
    }
8931
    ret = 0;
D
Daniel Veillard 已提交
8932

8933
endjob:
8934
    if (resume && vm->state == VIR_DOMAIN_PAUSED) {
8935
        /* we got here through some sort of failure; start the domain again */
H
Hu Tao 已提交
8936
        if (doStartCPUs(driver, vm, dom->conn) < 0) {
8937 8938 8939 8940
            /* Hm, we already know we are in error here.  We don't want to
             * overwrite the previous error, though, so we just throw something
             * to the logs and hope for the best
             */
8941
            VIR_ERROR(_("Failed to resume guest %s after failure"),
8942
                      vm->def->name);
8943 8944 8945 8946 8947 8948
        }

        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_RESUMED,
                                         VIR_DOMAIN_EVENT_RESUMED_MIGRATED);
    }
8949 8950 8951
    if (vm &&
        qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
8952

8953
cleanup:
8954 8955
    if (vm)
        virDomainObjUnlock(vm);
8956 8957
    if (event)
        qemuDomainEventQueue(driver, event);
8958
    qemuDriverUnlock(driver);
8959
    return ret;
D
Daniel Veillard 已提交
8960 8961
}

8962
#if WITH_MACVTAP
8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996
static void
qemudVPAssociatePortProfiles(virDomainDefPtr def) {
    int i;
    int last_good_net = -1;
    virDomainNetDefPtr net;

    for (i = 0; i < def->nnets; i++) {
        net = def->nets[i];
        if (net->type == VIR_DOMAIN_NET_TYPE_DIRECT) {
            if (vpAssociatePortProfileId(net->ifname,
                                         net->mac,
                                         net->data.direct.linkdev,
                                         &net->data.direct.virtPortProfile,
                                         def->uuid,
                                         VIR_VM_OP_MIGRATE_IN_FINISH) != 0)
                goto err_exit;
        }
        last_good_net = i;
    }

    return;

err_exit:
    for (i = 0; i < last_good_net; i++) {
        net = def->nets[i];
        if (net->type == VIR_DOMAIN_NET_TYPE_DIRECT) {
            vpDisassociatePortProfileId(net->ifname,
                                        net->mac,
                                        net->data.direct.linkdev,
                                        &net->data.direct.virtPortProfile,
                                        VIR_VM_OP_MIGRATE_IN_FINISH);
        }
    }
}
8997 8998 8999 9000
#else /* !WITH_MACVTAP */
static void
qemudVPAssociatePortProfiles(virDomainDefPtr def ATTRIBUTE_UNUSED) { }
#endif /* WITH_MACVTAP */
9001

D
Daniel Veillard 已提交
9002 9003 9004 9005 9006 9007 9008
/* 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,
C
Chris Lalancette 已提交
9009
                           unsigned long flags,
D
Daniel Veillard 已提交
9010 9011
                           int retcode)
{
9012 9013 9014
    struct qemud_driver *driver = dconn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
9015
    virDomainEventPtr event = NULL;
9016
    virErrorPtr orig_err;
C
Chris Lalancette 已提交
9017
    int newVM = 1;
9018
    qemuDomainObjPrivatePtr priv = NULL;
D
Daniel Veillard 已提交
9019

C
Chris Lalancette 已提交
9020 9021 9022 9023 9024 9025 9026 9027 9028
    virCheckFlags(VIR_MIGRATE_LIVE |
                  VIR_MIGRATE_PEER2PEER |
                  VIR_MIGRATE_TUNNELLED |
                  VIR_MIGRATE_PERSIST_DEST |
                  VIR_MIGRATE_UNDEFINE_SOURCE |
                  VIR_MIGRATE_PAUSED |
                  VIR_MIGRATE_NON_SHARED_DISK |
                  VIR_MIGRATE_NON_SHARED_INC, NULL);

9029 9030 9031
    /* Migration failed. Save the current error so nothing squashes it */
    orig_err = virSaveLastError();

9032
    qemuDriverLock(driver);
9033
    vm = virDomainFindByName(&driver->domains, dname);
D
Daniel Veillard 已提交
9034
    if (!vm) {
9035 9036
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching name '%s'"), dname);
9037
        goto cleanup;
D
Daniel Veillard 已提交
9038 9039
    }

9040 9041 9042 9043 9044 9045 9046 9047 9048
    priv = vm->privateData;
    if (priv->jobActive != QEMU_JOB_MIGRATION_IN) {
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("domain '%s' is not processing incoming migration"), dname);
        goto cleanup;
    }
    priv->jobActive = QEMU_JOB_NONE;
    memset(&priv->jobInfo, 0, sizeof(priv->jobInfo));

9049 9050 9051
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;

D
Daniel Veillard 已提交
9052 9053 9054 9055
    /* Did the migration go as planned?  If yes, return the domain
     * object, but if no, clean up the empty qemu process.
     */
    if (retcode == 0) {
9056 9057 9058 9059 9060 9061
        if (!virDomainObjIsActive(vm)) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("guest unexpectedly quit"));
            goto cleanup;
        }

9062 9063
        qemudVPAssociatePortProfiles(vm->def);

C
Chris Lalancette 已提交
9064 9065 9066 9067 9068
        if (flags & VIR_MIGRATE_PERSIST_DEST) {
            if (vm->persistent)
                newVM = 0;
            vm->persistent = 1;

9069
            if (virDomainSaveConfig(driver->configDir, vm->def) < 0) {
C
Chris Lalancette 已提交
9070 9071 9072 9073 9074 9075 9076 9077 9078 9079
                /* Hmpf.  Migration was successful, but making it persistent
                 * was not.  If we report successful, then when this domain
                 * shuts down, management tools are in for a surprise.  On the
                 * other hand, if we report failure, then the management tools
                 * might try to restart the domain on the source side, even
                 * though the domain is actually running on the destination.
                 * Return a NULL dom pointer, and hope that this is a rare
                 * situation and management tools are smart.
                 */
                vm = NULL;
9080
                goto endjob;
C
Chris Lalancette 已提交
9081 9082 9083 9084 9085 9086 9087 9088 9089
            }

            event = virDomainEventNewFromObj(vm,
                                             VIR_DOMAIN_EVENT_DEFINED,
                                             newVM ?
                                             VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                             VIR_DOMAIN_EVENT_DEFINED_UPDATED);
            if (event)
                qemuDomainEventQueue(driver, event);
9090
            event = NULL;
C
Chris Lalancette 已提交
9091 9092

        }
D
Daniel Veillard 已提交
9093
        dom = virGetDomain (dconn, vm->def->name, vm->def->uuid);
9094

9095 9096 9097 9098 9099
        if (!(flags & VIR_MIGRATE_PAUSED)) {
            /* run 'cont' on the destination, which allows migration on qemu
             * >= 0.10.6 to work properly.  This isn't strictly necessary on
             * older qemu's, but it also doesn't hurt anything there
             */
H
Hu Tao 已提交
9100
            if (doStartCPUs(driver, vm, dconn) < 0) {
9101
                if (virGetLastError() == NULL)
9102 9103
                    qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                    "%s", _("resume operation failed"));
9104 9105
                goto endjob;
            }
9106 9107
        }

9108 9109 9110
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_RESUMED,
                                         VIR_DOMAIN_EVENT_RESUMED_MIGRATED);
9111 9112 9113 9114 9115 9116
        if (vm->state == VIR_DOMAIN_PAUSED) {
            qemuDomainEventQueue(driver, event);
            event = virDomainEventNewFromObj(vm,
                                             VIR_DOMAIN_EVENT_SUSPENDED,
                                             VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
        }
9117 9118 9119 9120
        if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0) {
            VIR_WARN("Failed to save status on vm %s", vm->def->name);
            goto endjob;
        }
D
Daniel Veillard 已提交
9121
    } else {
9122
        qemudShutdownVMDaemon(driver, vm, 1);
9123
        qemuDomainStopAudit(vm, "failed");
9124 9125 9126
        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_FAILED);
9127
        if (!vm->persistent) {
9128 9129
            if (qemuDomainObjEndJob(vm) > 0)
                virDomainRemoveInactive(&driver->domains, vm);
9130 9131
            vm = NULL;
        }
D
Daniel Veillard 已提交
9132
    }
9133

9134
endjob:
9135 9136 9137
    if (vm &&
        qemuDomainObjEndJob(vm) == 0)
        vm = NULL;
9138

9139
cleanup:
9140 9141 9142 9143
    if (orig_err) {
        virSetError(orig_err);
        virFreeError(orig_err);
    }
9144 9145
    if (vm)
        virDomainObjUnlock(vm);
9146 9147
    if (event)
        qemuDomainEventQueue(driver, event);
9148
    qemuDriverUnlock(driver);
9149
    return dom;
D
Daniel Veillard 已提交
9150 9151
}

9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167
static int
qemudNodeDeviceGetPciInfo (virNodeDevicePtr dev,
                           unsigned *domain,
                           unsigned *bus,
                           unsigned *slot,
                           unsigned *function)
{
    virNodeDeviceDefPtr def = NULL;
    virNodeDevCapsDefPtr cap;
    char *xml = NULL;
    int ret = -1;

    xml = virNodeDeviceGetXMLDesc(dev, 0);
    if (!xml)
        goto out;

9168
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE);
9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185
    if (!def)
        goto out;

    cap = def->caps;
    while (cap) {
        if (cap->type == VIR_NODE_DEV_CAP_PCI_DEV) {
            *domain   = cap->data.pci_dev.domain;
            *bus      = cap->data.pci_dev.bus;
            *slot     = cap->data.pci_dev.slot;
            *function = cap->data.pci_dev.function;
            break;
        }

        cap = cap->next;
    }

    if (!cap) {
9186 9187
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("device %s is not a PCI device"), dev->name);
9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200
        goto out;
    }

    ret = 0;
out:
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

static int
qemudNodeDeviceDettach (virNodeDevicePtr dev)
{
9201
    struct qemud_driver *driver = dev->conn->privateData;
9202 9203 9204 9205 9206 9207 9208
    pciDevice *pci;
    unsigned domain, bus, slot, function;
    int ret = -1;

    if (qemudNodeDeviceGetPciInfo(dev, &domain, &bus, &slot, &function) < 0)
        return -1;

9209
    pci = pciGetDevice(domain, bus, slot, function);
9210 9211 9212
    if (!pci)
        return -1;

9213 9214
    qemuDriverLock(driver);
    if (pciDettachDevice(pci, driver->activePciHostdevs) < 0)
9215 9216 9217 9218
        goto out;

    ret = 0;
out:
9219
    qemuDriverUnlock(driver);
9220
    pciFreeDevice(pci);
9221 9222 9223 9224 9225 9226
    return ret;
}

static int
qemudNodeDeviceReAttach (virNodeDevicePtr dev)
{
9227
    struct qemud_driver *driver = dev->conn->privateData;
9228 9229 9230 9231 9232 9233 9234
    pciDevice *pci;
    unsigned domain, bus, slot, function;
    int ret = -1;

    if (qemudNodeDeviceGetPciInfo(dev, &domain, &bus, &slot, &function) < 0)
        return -1;

9235
    pci = pciGetDevice(domain, bus, slot, function);
9236 9237 9238
    if (!pci)
        return -1;

9239 9240
    qemuDriverLock(driver);
    if (pciReAttachDevice(pci, driver->activePciHostdevs) < 0)
9241 9242 9243 9244
        goto out;

    ret = 0;
out:
9245
    qemuDriverUnlock(driver);
9246
    pciFreeDevice(pci);
9247 9248 9249 9250 9251 9252
    return ret;
}

static int
qemudNodeDeviceReset (virNodeDevicePtr dev)
{
9253
    struct qemud_driver *driver = dev->conn->privateData;
9254 9255 9256 9257 9258 9259 9260
    pciDevice *pci;
    unsigned domain, bus, slot, function;
    int ret = -1;

    if (qemudNodeDeviceGetPciInfo(dev, &domain, &bus, &slot, &function) < 0)
        return -1;

9261
    pci = pciGetDevice(domain, bus, slot, function);
9262 9263 9264
    if (!pci)
        return -1;

9265 9266
    qemuDriverLock(driver);

9267
    if (pciResetDevice(pci, driver->activePciHostdevs, NULL) < 0)
9268 9269 9270 9271
        goto out;

    ret = 0;
out:
9272
    qemuDriverUnlock(driver);
9273
    pciFreeDevice(pci);
9274 9275 9276
    return ret;
}

9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287
static int
qemuCPUCompare(virConnectPtr conn,
               const char *xmlDesc,
               unsigned int flags ATTRIBUTE_UNUSED)
{
    struct qemud_driver *driver = conn->privateData;
    int ret = VIR_CPU_COMPARE_ERROR;

    qemuDriverLock(driver);

    if (!driver->caps || !driver->caps->host.cpu) {
9288 9289
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        "%s", _("cannot get host CPU capabilities"));
9290 9291
    }
    else
9292
        ret = cpuCompareXML(driver->caps->host.cpu, xmlDesc);
9293 9294 9295 9296 9297 9298

    qemuDriverUnlock(driver);

    return ret;
}

9299

9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312
static char *
qemuCPUBaseline(virConnectPtr conn ATTRIBUTE_UNUSED,
                const char **xmlCPUs,
                unsigned int ncpus,
                unsigned int flags ATTRIBUTE_UNUSED)
{
    char *cpu;

    cpu = cpuBaselineXML(xmlCPUs, ncpus, NULL, 0);

    return cpu;
}

9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335

static int qemuDomainGetJobInfo(virDomainPtr dom,
                                virDomainJobInfoPtr info) {
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
    qemuDomainObjPrivatePtr priv;

    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    qemuDriverUnlock(driver);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    priv = vm->privateData;

    if (virDomainObjIsActive(vm)) {
        if (priv->jobActive) {
9336 9337
            struct timeval now;

9338
            memcpy(info, &priv->jobInfo, sizeof(*info));
9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349

            /* Refresh elapsed time again just to ensure it
             * is fully updated. This is primarily for benefit
             * of incoming migration which we don't currently
             * monitor actively in the background thread
             */
            if (gettimeofday(&now, NULL) < 0) {
                virReportSystemError(errno, "%s",
                                     _("cannot get time of day"));
                goto cleanup;
            }
9350
            info->timeElapsed = timeval_to_ms(now) - priv->jobStart;
9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369
        } else {
            memset(info, 0, sizeof(*info));
            info->type = VIR_DOMAIN_JOB_NONE;
        }
    } else {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
        goto cleanup;
    }

    ret = 0;

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    return ret;
}


9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391
static int qemuDomainAbortJob(virDomainPtr dom) {
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
    qemuDomainObjPrivatePtr priv;

    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    qemuDriverUnlock(driver);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    priv = vm->privateData;

    if (virDomainObjIsActive(vm)) {
        if (priv->jobActive) {
            VIR_DEBUG("Requesting cancellation of job on vm %s", vm->def->name);
9392
            priv->jobSignals |= QEMU_JOB_SIGNAL_CANCEL;
9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412
        } else {
            qemuReportError(VIR_ERR_OPERATION_INVALID,
                            "%s", _("no job is active on the domain"));
            goto cleanup;
        }
    } else {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
        goto cleanup;
    }

    ret = 0;

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    return ret;
}


9413 9414 9415 9416 9417 9418 9419 9420 9421 9422
static int
qemuDomainMigrateSetMaxDowntime(virDomainPtr dom,
                                unsigned long long downtime,
                                unsigned int flags)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    qemuDomainObjPrivatePtr priv;
    int ret = -1;

9423
    virCheckFlags(0, -1);
9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443

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

    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
        goto cleanup;
    }

    priv = vm->privateData;

9444
    if (priv->jobActive != QEMU_JOB_MIGRATION_OUT) {
9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not being migrated"));
        goto cleanup;
    }

    VIR_DEBUG("Requesting migration downtime change to %llums", downtime);
    priv->jobSignals |= QEMU_JOB_SIGNAL_MIGRATE_DOWNTIME;
    priv->jobSignalsData.migrateDowntime = downtime;
    ret = 0;

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return ret;
}

C
Chris Lalancette 已提交
9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475
static char *qemuFindQemuImgBinary(void)
{
    char *ret;

    ret = virFindFileInPath("kvm-img");
    if (ret == NULL)
        ret = virFindFileInPath("qemu-img");
    if (ret == NULL)
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("unable to find kvm-img or qemu-img"));

    return ret;
}

9476 9477 9478
static int qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm,
                                           virDomainSnapshotObjPtr snapshot,
                                           char *snapshotDir)
C
Chris Lalancette 已提交
9479 9480 9481 9482 9483 9484 9485 9486 9487 9488
{
    int fd = -1;
    char *newxml = NULL;
    int ret = -1;
    char *snapDir = NULL;
    char *snapFile = NULL;
    int err;
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    virUUIDFormat(vm->def->uuid, uuidstr);
9489
    newxml = virDomainSnapshotDefFormat(uuidstr, snapshot->def, 1);
C
Chris Lalancette 已提交
9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505
    if (newxml == NULL) {
        virReportOOMError();
        return -1;
    }

    if (virAsprintf(&snapDir, "%s/%s", snapshotDir, vm->def->name) < 0) {
        virReportOOMError();
        goto cleanup;
    }
    err = virFileMakePath(snapDir);
    if (err < 0) {
        virReportSystemError(err, _("cannot create snapshot directory '%s'"),
                             snapDir);
        goto cleanup;
    }

9506
    if (virAsprintf(&snapFile, "%s/%s.xml", snapDir, snapshot->def->name) < 0) {
C
Chris Lalancette 已提交
9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527
        virReportOOMError();
        goto cleanup;
    }
    fd = open(snapFile, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR);
    if (fd < 0) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("failed to create snapshot file '%s'"), snapFile);
        goto cleanup;
    }
    if (safewrite(fd, newxml, strlen(newxml)) != strlen(newxml)) {
        virReportSystemError(errno, _("Failed to write snapshot data to %s"),
                             snapFile);
        goto cleanup;
    }

    ret = 0;

cleanup:
    VIR_FREE(snapFile);
    VIR_FREE(snapDir);
    VIR_FREE(newxml);
9528
    VIR_FORCE_CLOSE(fd);
C
Chris Lalancette 已提交
9529 9530 9531
    return ret;
}

9532 9533
static int qemuDomainSnapshotSetCurrentActive(virDomainObjPtr vm,
                                              char *snapshotDir)
C
Chris Lalancette 已提交
9534 9535 9536 9537
{
    if (vm->current_snapshot) {
        vm->current_snapshot->def->active = 1;

9538 9539
        return qemuDomainSnapshotWriteMetadata(vm, vm->current_snapshot,
                                               snapshotDir);
C
Chris Lalancette 已提交
9540 9541 9542 9543 9544
    }

    return 0;
}

9545 9546
static int qemuDomainSnapshotSetCurrentInactive(virDomainObjPtr vm,
                                                char *snapshotDir)
C
Chris Lalancette 已提交
9547 9548 9549 9550
{
    if (vm->current_snapshot) {
        vm->current_snapshot->def->active = 0;

9551 9552
        return qemuDomainSnapshotWriteMetadata(vm, vm->current_snapshot,
                                               snapshotDir);
C
Chris Lalancette 已提交
9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571
    }

    return 0;
}


static int qemuDomainSnapshotIsAllowed(virDomainObjPtr vm)
{
    int i;

    /* FIXME: we need to figure out what else here might succeed; in
     * particular, if it's a raw device but on LVM, we could probably make
     * that succeed as well
     */
    for (i = 0; i < vm->def->ndisks; i++) {
        if (vm->def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
            (!vm->def->disks[i]->driverType ||
             STRNEQ(vm->def->disks[i]->driverType, "qcow2"))) {
            qemuReportError(VIR_ERR_OPERATION_INVALID,
9572 9573
                            _("Disk '%s' does not support snapshotting"),
                            vm->def->disks[i]->src);
C
Chris Lalancette 已提交
9574 9575 9576 9577 9578 9579 9580 9581 9582
            return 0;
        }
    }

    return 1;
}

static virDomainSnapshotPtr qemuDomainSnapshotCreateXML(virDomainPtr domain,
                                                        const char *xmlDesc,
9583
                                                        unsigned int flags)
C
Chris Lalancette 已提交
9584 9585 9586 9587 9588 9589 9590 9591 9592 9593
{
    struct qemud_driver *driver = domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    virDomainSnapshotDefPtr def;
    const char *qemuimgarg[] = { NULL, "snapshot", "-c", NULL, NULL, NULL };
    int i;

9594 9595
    virCheckFlags(0, NULL);

C
Chris Lalancette 已提交
9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656
    qemuDriverLock(driver);
    virUUIDFormat(domain->uuid, uuidstr);
    vm = virDomainFindByUUID(&driver->domains, domain->uuid);
    if (!vm) {
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    /* in a perfect world, we would allow qemu to tell us this.  The problem
     * is that qemu only does this check device-by-device; so if you had a
     * domain that booted from a large qcow2 device, but had a secondary raw
     * device attached, you wouldn't find out that you can't snapshot your
     * guest until *after* it had spent the time to snapshot the boot device.
     * This is probably a bug in qemu, but we'll work around it here for now.
     */
    if (!qemuDomainSnapshotIsAllowed(vm))
        goto cleanup;

    if (!(def = virDomainSnapshotDefParseString(xmlDesc, 1)))
        goto cleanup;

    if (!(snap = virDomainSnapshotAssignDef(&vm->snapshots, def)))
        goto cleanup;

    /* actually do the snapshot */
    if (!virDomainObjIsActive(vm)) {
        qemuimgarg[0] = qemuFindQemuImgBinary();
        if (qemuimgarg[0] == NULL)
            /* qemuFindQemuImgBinary set the error */
            goto cleanup;

        qemuimgarg[3] = snap->def->name;

        for (i = 0; i < vm->def->ndisks; i++) {
            /* FIXME: we also need to handle LVM here */
            /* FIXME: if we fail halfway through this loop, we are in an
             * inconsistent state.  I'm not quite sure what to do about that
             */
            if (vm->def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
                if (!vm->def->disks[i]->driverType ||
                    STRNEQ(vm->def->disks[i]->driverType, "qcow2")) {
                    qemuReportError(VIR_ERR_OPERATION_INVALID,
                                    _("Disk device '%s' does not support snapshotting"),
                                    vm->def->disks[i]->info.alias);
                    goto cleanup;
                }

                qemuimgarg[4] = vm->def->disks[i]->src;

                if (virRun(qemuimgarg, NULL) < 0) {
                    virReportSystemError(errno,
                                         _("Failed to run '%s' to create snapshot '%s' from disk '%s'"),
                                         qemuimgarg[0], snap->def->name,
                                         vm->def->disks[i]->src);
                    goto cleanup;
                }
            }
        }
    }
    else {
9657 9658 9659 9660 9661
        qemuDomainObjPrivatePtr priv;
        int ret;

        if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
            goto cleanup;
C
Chris Lalancette 已提交
9662 9663
        priv = vm->privateData;
        qemuDomainObjEnterMonitorWithDriver(driver, vm);
9664
        ret = qemuMonitorCreateSnapshot(priv->mon, def->name);
C
Chris Lalancette 已提交
9665
        qemuDomainObjExitMonitorWithDriver(driver, vm);
9666
        if (qemuDomainObjEndJob(vm) == 0) {
9667
            vm = NULL;
9668 9669
            goto cleanup;
        }
9670 9671
        if (ret < 0)
            goto cleanup;
C
Chris Lalancette 已提交
9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691
    }

    snap->def->state = vm->state;

    /* FIXME: if we fail after this point, there's not a whole lot we can
     * do; we've successfully taken the snapshot, and we are now running
     * on it, so we have to go forward the best we can
     */

    if (vm->current_snapshot) {
        def->parent = strdup(vm->current_snapshot->def->name);
        if (def->parent == NULL) {
            virReportOOMError();
            goto cleanup;
        }
    }

    /* Now we set the new current_snapshot for the domain */
    vm->current_snapshot = snap;

9692 9693 9694
    if (qemuDomainSnapshotWriteMetadata(vm, vm->current_snapshot,
                                        driver->snapshotDir) < 0)
        /* qemuDomainSnapshotWriteMetadata set the error */
C
Chris Lalancette 已提交
9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708
        goto cleanup;

    snapshot = virGetDomainSnapshot(domain, snap->def->name);

cleanup:
    VIR_FREE(qemuimgarg[0]);
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return snapshot;
}

static int qemuDomainSnapshotListNames(virDomainPtr domain, char **names,
                                       int nameslen,
9709
                                       unsigned int flags)
C
Chris Lalancette 已提交
9710 9711 9712 9713 9714
{
    struct qemud_driver *driver = domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    int n = -1;

9715 9716
    virCheckFlags(0, -1);

C
Chris Lalancette 已提交
9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736
    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, domain->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(domain->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    n = virDomainSnapshotObjListGetNames(&vm->snapshots, names, nameslen);

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return n;
}

static int qemuDomainSnapshotNum(virDomainPtr domain,
9737
                                 unsigned int flags)
C
Chris Lalancette 已提交
9738 9739 9740 9741 9742
{
    struct qemud_driver *driver = domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    int n = -1;

9743 9744
    virCheckFlags(0, -1);

C
Chris Lalancette 已提交
9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765
    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, domain->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(domain->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    n = virDomainSnapshotObjListNum(&vm->snapshots);

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return n;
}

static virDomainSnapshotPtr qemuDomainSnapshotLookupByName(virDomainPtr domain,
                                                           const char *name,
9766
                                                           unsigned int flags)
C
Chris Lalancette 已提交
9767 9768 9769 9770 9771 9772
{
    struct qemud_driver *driver = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;

9773 9774
    virCheckFlags(0, NULL);

C
Chris Lalancette 已提交
9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801
    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, domain->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(domain->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    snap = virDomainSnapshotFindByName(&vm->snapshots, name);
    if (!snap) {
        qemuReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                        _("no snapshot with matching name '%s'"), name);
        goto cleanup;
    }

    snapshot = virGetDomainSnapshot(domain, snap->def->name);

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return snapshot;
}

static int qemuDomainHasCurrentSnapshot(virDomainPtr domain,
9802
                                        unsigned int flags)
C
Chris Lalancette 已提交
9803 9804 9805 9806 9807
{
    struct qemud_driver *driver = domain->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

9808 9809
    virCheckFlags(0, -1);

C
Chris Lalancette 已提交
9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829
    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, domain->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(domain->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    ret = (vm->current_snapshot != NULL);

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return ret;
}

static virDomainSnapshotPtr qemuDomainSnapshotCurrent(virDomainPtr domain,
9830
                                                      unsigned int flags)
C
Chris Lalancette 已提交
9831 9832 9833 9834 9835
{
    struct qemud_driver *driver = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainSnapshotPtr snapshot = NULL;

9836 9837
    virCheckFlags(0, NULL);

C
Chris Lalancette 已提交
9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863
    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, domain->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(domain->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    if (!vm->current_snapshot) {
        qemuReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT, "%s",
                        _("the domain does not have a current snapshot"));
        goto cleanup;
    }

    snapshot = virGetDomainSnapshot(domain, vm->current_snapshot->def->name);

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return snapshot;
}

static char *qemuDomainSnapshotDumpXML(virDomainSnapshotPtr snapshot,
9864
                                       unsigned int flags)
C
Chris Lalancette 已提交
9865 9866 9867 9868 9869 9870 9871
{
    struct qemud_driver *driver = snapshot->domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    char *xml = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    char uuidstr[VIR_UUID_STRING_BUFLEN];

9872 9873
    virCheckFlags(0, NULL);

C
Chris Lalancette 已提交
9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900
    qemuDriverLock(driver);
    virUUIDFormat(snapshot->domain->uuid, uuidstr);
    vm = virDomainFindByUUID(&driver->domains, snapshot->domain->uuid);
    if (!vm) {
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
    if (!snap) {
        qemuReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                        _("no domain snapshot with matching name '%s'"),
                        snapshot->name);
        goto cleanup;
    }

    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, 0);

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return xml;
}

static int qemuDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
9901
                                      unsigned int flags)
C
Chris Lalancette 已提交
9902 9903 9904 9905 9906 9907 9908 9909 9910 9911
{
    struct qemud_driver *driver = snapshot->domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    int ret = -1;
    virDomainSnapshotObjPtr snap = NULL;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    virDomainEventPtr event = NULL;
    qemuDomainObjPrivatePtr priv;
    int rc;

9912 9913
    virCheckFlags(0, -1);

C
Chris Lalancette 已提交
9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944
    qemuDriverLock(driver);
    virUUIDFormat(snapshot->domain->uuid, uuidstr);
    vm = virDomainFindByUUID(&driver->domains, snapshot->domain->uuid);
    if (!vm) {
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
    if (!snap) {
        qemuReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                        _("no domain snapshot with matching name '%s'"),
                        snapshot->name);
        goto cleanup;
    }

    vm->current_snapshot = snap;

    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;

    if (snap->def->state == VIR_DOMAIN_RUNNING
        || snap->def->state == VIR_DOMAIN_PAUSED) {

        if (virDomainObjIsActive(vm)) {
            priv = vm->privateData;
            qemuDomainObjEnterMonitorWithDriver(driver, vm);
            rc = qemuMonitorLoadSnapshot(priv->mon, snap->def->name);
            qemuDomainObjExitMonitorWithDriver(driver, vm);
            if (rc < 0)
9945
                goto endjob;
C
Chris Lalancette 已提交
9946 9947
        }
        else {
9948
            if (qemuDomainSnapshotSetCurrentActive(vm, driver->snapshotDir) < 0)
9949
                goto endjob;
C
Chris Lalancette 已提交
9950 9951

            rc = qemudStartVMDaemon(snapshot->domain->conn, driver, vm, NULL,
9952
                                    false, -1, NULL, VIR_VM_OP_CREATE);
9953
            qemuDomainStartAudit(vm, "from-snapshot", rc >= 0);
9954
            if (qemuDomainSnapshotSetCurrentInactive(vm, driver->snapshotDir) < 0)
9955
                goto endjob;
C
Chris Lalancette 已提交
9956
            if (rc < 0)
9957
                goto endjob;
C
Chris Lalancette 已提交
9958 9959 9960 9961 9962 9963
        }

        if (snap->def->state == VIR_DOMAIN_PAUSED) {
            /* qemu unconditionally starts the domain running again after
             * loadvm, so let's pause it to keep consistency
             */
H
Hu Tao 已提交
9964 9965
            rc = doStopCPUs(driver, vm);
            if (rc < 0)
9966
                goto endjob;
C
Chris Lalancette 已提交
9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984
        }

        event = virDomainEventNewFromObj(vm,
                                         VIR_DOMAIN_EVENT_STARTED,
                                         VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
    }
    else {
        /* qemu is a little funny with running guests and the restoration
         * of snapshots.  If the snapshot was taken online,
         * then after a "loadvm" monitor command, the VM is set running
         * again.  If the snapshot was taken offline, then after a "loadvm"
         * monitor command the VM is left paused.  Unpausing it leads to
         * the memory state *before* the loadvm with the disk *after* the
         * loadvm, which obviously is bound to corrupt something.
         * Therefore we destroy the domain and set it to "off" in this case.
         */

        if (virDomainObjIsActive(vm)) {
9985
            qemudShutdownVMDaemon(driver, vm, 0);
9986
            qemuDomainStopAudit(vm, "from-snapshot");
C
Chris Lalancette 已提交
9987 9988 9989
            event = virDomainEventNewFromObj(vm,
                                             VIR_DOMAIN_EVENT_STOPPED,
                                             VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
9990 9991 9992 9993
            if (!vm->persistent) {
                if (qemuDomainObjEndJob(vm) > 0)
                    virDomainRemoveInactive(&driver->domains, vm);
                vm = NULL;
9994
                goto cleanup;
9995
            }
C
Chris Lalancette 已提交
9996 9997
        }

9998
        if (qemuDomainSnapshotSetCurrentActive(vm, driver->snapshotDir) < 0)
9999
            goto endjob;
C
Chris Lalancette 已提交
10000 10001 10002 10003 10004 10005
    }

    vm->state = snap->def->state;

    ret = 0;

10006
endjob:
C
Chris Lalancette 已提交
10007 10008 10009
    if (vm && qemuDomainObjEndJob(vm) == 0)
        vm = NULL;

10010
cleanup:
C
Chris Lalancette 已提交
10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134
    if (event)
        qemuDomainEventQueue(driver, event);
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);

    return ret;
}

static int qemuDomainSnapshotDiscard(struct qemud_driver *driver,
                                     virDomainObjPtr vm,
                                     virDomainSnapshotObjPtr snap)
{
    const char *qemuimgarg[] = { NULL, "snapshot", "-d", NULL, NULL, NULL };
    char *snapFile = NULL;
    int ret = -1;
    int i;
    qemuDomainObjPrivatePtr priv;
    virDomainSnapshotObjPtr parentsnap;

    if (!virDomainObjIsActive(vm)) {
        qemuimgarg[0] = qemuFindQemuImgBinary();
        if (qemuimgarg[0] == NULL)
            /* qemuFindQemuImgBinary set the error */
            goto cleanup;

        qemuimgarg[3] = snap->def->name;

        for (i = 0; i < vm->def->ndisks; i++) {
            /* FIXME: we also need to handle LVM here */
            if (vm->def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
                if (!vm->def->disks[i]->driverType ||
                    STRNEQ(vm->def->disks[i]->driverType, "qcow2")) {
                    /* we continue on even in the face of error, since other
                     * disks in this VM may have this snapshot in place
                     */
                    continue;
                }

                qemuimgarg[4] = vm->def->disks[i]->src;

                if (virRun(qemuimgarg, NULL) < 0) {
                    /* we continue on even in the face of error, since other
                     * disks in this VM may have this snapshot in place
                     */
                    continue;
                }
            }
        }
    }
    else {
        priv = vm->privateData;
        qemuDomainObjEnterMonitorWithDriver(driver, vm);
        /* we continue on even in the face of error */
        qemuMonitorDeleteSnapshot(priv->mon, snap->def->name);
        qemuDomainObjExitMonitorWithDriver(driver, vm);
    }

    if (snap == vm->current_snapshot) {
        if (snap->def->parent) {
            parentsnap = virDomainSnapshotFindByName(&vm->snapshots,
                                                     snap->def->parent);
            if (!parentsnap) {
                qemuReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                                _("no domain snapshot parent with matching name '%s'"),
                                snap->def->parent);
                goto cleanup;
            }

            /* Now we set the new current_snapshot for the domain */
            vm->current_snapshot = parentsnap;
        }
        else
            vm->current_snapshot = NULL;
    }

    if (virAsprintf(&snapFile, "%s/%s/%s.xml", driver->snapshotDir,
                    vm->def->name, snap->def->name) < 0) {
        virReportOOMError();
        goto cleanup;
    }
    unlink(snapFile);

    virDomainSnapshotObjListRemove(&vm->snapshots, snap);

    ret = 0;

cleanup:
    VIR_FREE(snapFile);
    VIR_FREE(qemuimgarg[0]);

    return ret;
}

struct snap_remove {
    struct qemud_driver *driver;
    virDomainObjPtr vm;
    char *parent;
    int err;
};

static void qemuDomainSnapshotDiscardChildren(void *payload,
                                              const char *name ATTRIBUTE_UNUSED,
                                              void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    struct snap_remove *curr = data;
    struct snap_remove this;

    if (snap->def->parent && STREQ(snap->def->parent, curr->parent)) {
        this.driver = curr->driver;
        this.vm = curr->vm;
        this.parent = snap->def->name;
        this.err = 0;
        virHashForEach(curr->vm->snapshots.objs,
                       qemuDomainSnapshotDiscardChildren, &this);

        if (this.err)
            curr->err = this.err;
        else
            this.err = qemuDomainSnapshotDiscard(curr->driver, curr->vm, snap);
    }
}

10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171
struct snap_reparent {
    struct qemud_driver *driver;
    virDomainSnapshotObjPtr snap;
    virDomainObjPtr vm;
    int err;
};

static void
qemuDomainSnapshotReparentChildren(void *payload,
                                   const char *name ATTRIBUTE_UNUSED,
                                   void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    struct snap_reparent *rep = data;

    if (rep->err < 0) {
        return;
    }

    if (snap->def->parent && STREQ(snap->def->parent, rep->snap->def->name)) {
        VIR_FREE(snap->def->parent);

        if (rep->snap->def->parent != NULL) {
            snap->def->parent = strdup(rep->snap->def->parent);

            if (snap->def->parent == NULL) {
                virReportOOMError();
                rep->err = -1;
                return;
            }
        }

        rep->err = qemuDomainSnapshotWriteMetadata(rep->vm, snap,
                                                   rep->driver->snapshotDir);
    }
}

C
Chris Lalancette 已提交
10172 10173 10174 10175 10176 10177 10178 10179 10180
static int qemuDomainSnapshotDelete(virDomainSnapshotPtr snapshot,
                                    unsigned int flags)
{
    struct qemud_driver *driver = snapshot->domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    int ret = -1;
    virDomainSnapshotObjPtr snap = NULL;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    struct snap_remove rem;
10181
    struct snap_reparent rep;
C
Chris Lalancette 已提交
10182

10183 10184
    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN, -1);

C
Chris Lalancette 已提交
10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201
    qemuDriverLock(driver);
    virUUIDFormat(snapshot->domain->uuid, uuidstr);
    vm = virDomainFindByUUID(&driver->domains, snapshot->domain->uuid);
    if (!vm) {
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
    if (!snap) {
        qemuReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                        _("no domain snapshot with matching name '%s'"),
                        snapshot->name);
        goto cleanup;
    }

10202 10203 10204
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;

C
Chris Lalancette 已提交
10205 10206 10207 10208 10209 10210 10211 10212
    if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN) {
        rem.driver = driver;
        rem.vm = vm;
        rem.parent = snap->def->name;
        rem.err = 0;
        virHashForEach(vm->snapshots.objs, qemuDomainSnapshotDiscardChildren,
                       &rem);
        if (rem.err < 0)
10213
            goto endjob;
10214 10215 10216 10217 10218 10219 10220 10221 10222
    } else {
        rep.driver = driver;
        rep.snap = snap;
        rep.vm = vm;
        rep.err = 0;
        virHashForEach(vm->snapshots.objs, qemuDomainSnapshotReparentChildren,
                       &rep);
        if (rep.err < 0)
            goto endjob;
C
Chris Lalancette 已提交
10223 10224 10225 10226
    }

    ret = qemuDomainSnapshotDiscard(driver, vm, snap);

10227 10228 10229 10230
endjob:
    if (qemuDomainObjEndJob(vm) == 0)
        vm = NULL;

C
Chris Lalancette 已提交
10231 10232 10233 10234 10235 10236
cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return ret;
}
10237

10238 10239 10240 10241 10242 10243 10244
static int qemuDomainMonitorCommand(virDomainPtr domain, const char *cmd,
                                    char **result, unsigned int flags)
{
    struct qemud_driver *driver = domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    int ret = -1;
    qemuDomainObjPrivatePtr priv;
10245
    bool hmp;
10246

10247
    virCheckFlags(VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP, -1);
10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272

    qemuDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, domain->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(domain->uuid, uuidstr);
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
        goto cleanup;
   }

    priv = vm->privateData;

    if (!priv->monitor_warned) {
        VIR_INFO("Qemu monitor command '%s' executed; libvirt results may be unpredictable!",
                 cmd);
        priv->monitor_warned = 1;
    }

10273 10274
    hmp = !!(flags & VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP);

10275 10276 10277
    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
        goto cleanup;
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
10278
    ret = qemuMonitorArbitraryCommand(priv->mon, cmd, result, hmp);
10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291
    qemuDomainObjExitMonitorWithDriver(driver, vm);
    if (qemuDomainObjEndJob(vm) == 0) {
        vm = NULL;
        goto cleanup;
    }

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return ret;
}

10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348

static int
qemuDomainOpenConsole(virDomainPtr dom,
                      const char *devname,
                      virStreamPtr st,
                      unsigned int flags)
{
    struct qemud_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    int ret = -1;
    int i;
    virDomainChrDefPtr chr = NULL;

    virCheckFlags(0, -1);

    qemuDriverLock(driver);
    virUUIDFormat(dom->uuid, uuidstr);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    if (!vm) {
        qemuReportError(VIR_ERR_NO_DOMAIN,
                        _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
        goto cleanup;
    }

    if (devname) {
        if (vm->def->console &&
            STREQ(devname, vm->def->console->info.alias))
            chr = vm->def->console;
        for (i = 0 ; !chr && i < vm->def->nserials ; i++) {
            if (STREQ(devname, vm->def->serials[i]->info.alias))
                chr = vm->def->serials[i];
        }
        for (i = 0 ; !chr && i < vm->def->nparallels ; i++) {
            if (STREQ(devname, vm->def->parallels[i]->info.alias))
                chr = vm->def->parallels[i];
        }
    } else {
        if (vm->def->console)
            chr = vm->def->console;
        else if (vm->def->nserials)
            chr = vm->def->serials[0];
    }

    if (!chr) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("cannot find character device %s"),
                        NULLSTR(devname));
        goto cleanup;
    }

10349
    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_PTY) {
10350 10351 10352 10353 10354 10355
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("character device %s is not using a PTY"),
                        NULLSTR(devname));
        goto cleanup;
    }

10356
    if (virFDStreamOpenFile(st, chr->source.data.file.path, O_RDWR) < 0)
10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367
        goto cleanup;

    ret = 0;
cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    qemuDriverUnlock(driver);
    return ret;
}


10368 10369 10370 10371 10372
static virDriver qemuDriver = {
    VIR_DRV_QEMU,
    "QEMU",
    qemudOpen, /* open */
    qemudClose, /* close */
D
Daniel Veillard 已提交
10373
    qemudSupportsFeature, /* supports_feature */
10374 10375
    qemudGetType, /* type */
    qemudGetVersion, /* version */
10376
    NULL, /* libvirtVersion (impl. in libvirt.c) */
10377
    virGetHostname, /* getHostname */
E
Eric Blake 已提交
10378
    NULL, /* getSysinfo */
10379
    qemudGetMaxVCPUs, /* getMaxVcpus */
10380
    nodeGetInfo, /* nodeGetInfo */
10381 10382 10383
    qemudGetCapabilities, /* getCapabilities */
    qemudListDomains, /* listDomains */
    qemudNumDomains, /* numOfDomains */
10384
    qemudDomainCreate, /* domainCreateXML */
10385 10386 10387 10388 10389
    qemudDomainLookupByID, /* domainLookupByID */
    qemudDomainLookupByUUID, /* domainLookupByUUID */
    qemudDomainLookupByName, /* domainLookupByName */
    qemudDomainSuspend, /* domainSuspend */
    qemudDomainResume, /* domainResume */
10390
    qemudDomainShutdown, /* domainShutdown */
10391 10392 10393
    NULL, /* domainReboot */
    qemudDomainDestroy, /* domainDestroy */
    qemudDomainGetOSType, /* domainGetOSType */
10394
    qemudDomainGetMaxMemory, /* domainGetMaxMemory */
10395
    NULL, /* domainSetMaxMemory */
10396
    qemudDomainSetMemory, /* domainSetMemory */
10397 10398 10399
    qemudDomainGetInfo, /* domainGetInfo */
    qemudDomainSave, /* domainSave */
    qemudDomainRestore, /* domainRestore */
P
Paolo Bonzini 已提交
10400
    qemudDomainCoreDump, /* domainCoreDump */
10401
    qemudDomainSetVcpus, /* domainSetVcpus */
10402 10403
    qemudDomainSetVcpusFlags, /* domainSetVcpusFlags */
    qemudDomainGetVcpusFlags, /* domainGetVcpusFlags */
10404 10405
    qemudDomainPinVcpu, /* domainPinVcpu */
    qemudDomainGetVcpus, /* domainGetVcpus */
10406
    qemudDomainGetMaxVcpus, /* domainGetMaxVcpus */
10407 10408
    qemudDomainGetSecurityLabel, /* domainGetSecurityLabel */
    qemudNodeGetSecurityModel, /* nodeGetSecurityModel */
10409
    qemudDomainDumpXML, /* domainDumpXML */
10410
    qemuDomainXMLFromNative, /* domainXmlFromNative */
10411
    qemuDomainXMLToNative, /* domainXMLToNative */
10412 10413
    qemudListDefinedDomains, /* listDefinedDomains */
    qemudNumDefinedDomains, /* numOfDefinedDomains */
10414
    qemudDomainStart, /* domainCreate */
10415
    qemudDomainStartWithFlags, /* domainCreateWithFlags */
10416 10417
    qemudDomainDefine, /* domainDefineXML */
    qemudDomainUndefine, /* domainUndefine */
10418
    qemudDomainAttachDevice, /* domainAttachDevice */
10419
    qemudDomainAttachDeviceFlags, /* domainAttachDeviceFlags */
10420
    qemudDomainDetachDevice, /* domainDetachDevice */
10421
    qemudDomainDetachDeviceFlags, /* domainDetachDeviceFlags */
10422
    qemuDomainUpdateDeviceFlags, /* domainUpdateDeviceFlags */
10423 10424
    qemudDomainGetAutostart, /* domainGetAutostart */
    qemudDomainSetAutostart, /* domainSetAutostart */
10425 10426 10427
    qemuGetSchedulerType, /* domainGetSchedulerType */
    qemuGetSchedulerParameters, /* domainGetSchedulerParameters */
    qemuSetSchedulerParameters, /* domainSetSchedulerParameters */
D
Daniel Veillard 已提交
10428 10429
    NULL, /* domainMigratePrepare (v1) */
    qemudDomainMigratePerform, /* domainMigratePerform */
10430
    NULL, /* domainMigrateFinish */
10431
    qemudDomainBlockStats, /* domainBlockStats */
10432
    qemudDomainInterfaceStats, /* domainInterfaceStats */
10433
    qemudDomainMemoryStats, /* domainMemoryStats */
10434
    qemudDomainBlockPeek, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
10435
    qemudDomainMemoryPeek, /* domainMemoryPeek */
10436
    qemuDomainGetBlockInfo, /* domainGetBlockInfo */
10437 10438
    nodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    nodeGetFreeMemory,  /* getFreeMemory */
10439 10440
    qemuDomainEventRegister, /* domainEventRegister */
    qemuDomainEventDeregister, /* domainEventDeregister */
D
Daniel Veillard 已提交
10441 10442
    qemudDomainMigratePrepare2, /* domainMigratePrepare2 */
    qemudDomainMigrateFinish2, /* domainMigrateFinish2 */
10443 10444 10445
    qemudNodeDeviceDettach, /* nodeDeviceDettach */
    qemudNodeDeviceReAttach, /* nodeDeviceReAttach */
    qemudNodeDeviceReset, /* nodeDeviceReset */
C
Chris Lalancette 已提交
10446
    qemudDomainMigratePrepareTunnel, /* domainMigratePrepareTunnel */
10447 10448 10449 10450
    qemuIsEncrypted, /* isEncrypted */
    qemuIsSecure, /* isSecure */
    qemuDomainIsActive, /* domainIsActive */
    qemuDomainIsPersistent, /* domainIsPersistent */
10451
    qemuDomainIsUpdated, /* domainIsUpdated */
10452
    qemuCPUCompare, /* cpuCompare */
10453
    qemuCPUBaseline, /* cpuBaseline */
10454
    qemuDomainGetJobInfo, /* domainGetJobInfo */
10455
    qemuDomainAbortJob, /* domainAbortJob */
10456
    qemuDomainMigrateSetMaxDowntime, /* domainMigrateSetMaxDowntime */
10457 10458
    qemuDomainEventRegisterAny, /* domainEventRegisterAny */
    qemuDomainEventDeregisterAny, /* domainEventDeregisterAny */
10459 10460 10461
    qemuDomainManagedSave, /* domainManagedSave */
    qemuDomainHasManagedSaveImage, /* domainHasManagedSaveImage */
    qemuDomainManagedSaveRemove, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
10462 10463 10464 10465 10466 10467 10468 10469 10470
    qemuDomainSnapshotCreateXML, /* domainSnapshotCreateXML */
    qemuDomainSnapshotDumpXML, /* domainSnapshotDumpXML */
    qemuDomainSnapshotNum, /* domainSnapshotNum */
    qemuDomainSnapshotListNames, /* domainSnapshotListNames */
    qemuDomainSnapshotLookupByName, /* domainSnapshotLookupByName */
    qemuDomainHasCurrentSnapshot, /* domainHasCurrentSnapshot */
    qemuDomainSnapshotCurrent, /* domainSnapshotCurrent */
    qemuDomainRevertToSnapshot, /* domainRevertToSnapshot */
    qemuDomainSnapshotDelete, /* domainSnapshotDelete */
10471
    qemuDomainMonitorCommand, /* qemuDomainMonitorCommand */
10472
    qemuDomainSetMemoryParameters, /* domainSetMemoryParameters */
10473
    qemuDomainGetMemoryParameters, /* domainGetMemoryParameters */
10474
    qemuDomainOpenConsole, /* domainOpenConsole */
10475 10476 10477
};


10478
static virStateDriver qemuStateDriver = {
10479
    .name = "QEMU",
10480 10481 10482 10483
    .initialize = qemudStartup,
    .cleanup = qemudShutdown,
    .reload = qemudReload,
    .active = qemudActive,
10484
};
10485

S
Stefan Berger 已提交
10486
static int
10487
qemudVMFilterRebuild(virConnectPtr conn ATTRIBUTE_UNUSED,
S
Stefan Berger 已提交
10488 10489 10490
                     virHashIterator iter, void *data)
{
    virHashForEach(qemu_driver->domains.objs, iter, data);
10491

S
Stefan Berger 已提交
10492 10493 10494
    return 0;
}

10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516
static int
qemudVMFiltersInstantiate(virConnectPtr conn,
                          virDomainDefPtr def)
{
    int err = 0;
    int i;

    if (!conn)
        return 1;

    for (i = 0 ; i < def->nnets ; i++) {
        virDomainNetDefPtr net = def->nets[i];
        if ((net->filter) && (net->ifname)) {
           if (virDomainConfNWFilterInstantiate(conn, net)) {
                err = 1;
                break;
            }
        }
    }

    return err;
}
S
Stefan Berger 已提交
10517

10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530

static void
qemudVMDriverLock(void) {
    qemuDriverLock(qemu_driver);
};


static void
qemudVMDriverUnlock(void) {
    qemuDriverUnlock(qemu_driver);
};


S
Stefan Berger 已提交
10531 10532 10533
static virNWFilterCallbackDriver qemuCallbackDriver = {
    .name = "QEMU",
    .vmFilterRebuild = qemudVMFilterRebuild,
10534 10535
    .vmDriverLock = qemudVMDriverLock,
    .vmDriverUnlock = qemudVMDriverUnlock,
S
Stefan Berger 已提交
10536 10537
};

10538
int qemuRegister(void) {
10539 10540
    virRegisterDriver(&qemuDriver);
    virRegisterStateDriver(&qemuStateDriver);
S
Stefan Berger 已提交
10541
    virNWFilterRegisterCallbackDriver(&qemuCallbackDriver);
10542 10543
    return 0;
}