xm_internal.c 39.7 KB
Newer Older
1 2 3
/*
 * xm_internal.h: helper routines for dealing with inactive domains
 *
4
 * Copyright (C) 2006-2007, 2009-2013 Red Hat, Inc.
5
 * Copyright (C) 2006 Daniel P. Berrange
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
22 23 24
 *
 */

25
#include <config.h>
26

27 28 29
#include <dirent.h>
#include <time.h>
#include <sys/stat.h>
30
#include <limits.h>
31 32
#include <string.h>
#include <errno.h>
33 34 35 36 37

#include <unistd.h>
#include <stdint.h>
#include <xen/dom0_ops.h>

38
#include "virerror.h"
39
#include "virfile.h"
40
#include "datatypes.h"
41
#include "xm_internal.h"
42
#include "xen_driver.h"
43
#include "xend_internal.h"
44
#include "xen_sxpr.h"
45
#include "xen_xm.h"
46
#include "virhash.h"
47
#include "virbuffer.h"
48
#include "viruuid.h"
49
#include "viralloc.h"
50
#include "virlog.h"
51
#include "count-one-bits.h"
52
#include "virstring.h"
53

54
#define VIR_FROM_THIS VIR_FROM_XENXM
55

56
#ifdef WITH_RHEL5_API
57 58
# define XEND_CONFIG_MAX_VERS_NET_TYPE_IOEMU 0
# define XEND_CONFIG_MIN_VERS_PVFB_NEWCONF 2
59
#else
60 61
# define XEND_CONFIG_MAX_VERS_NET_TYPE_IOEMU 3
# define XEND_CONFIG_MIN_VERS_PVFB_NEWCONF 3
62 63
#endif

64 65 66 67 68
/* The true Xen limit varies but so far is always way
   less than 1024, which is the Linux kernel limit according
   to sched.h, so we'll match that for now */
#define XEN_MAX_PHYSICAL_CPU 1024

69 70
char * xenXMAutoAssignMac(void);

71 72 73 74 75 76 77
#define XM_REFRESH_INTERVAL 10

#define XM_CONFIG_DIR "/etc/xen"
#define XM_EXAMPLE_PREFIX "xmexample"
#define XEND_CONFIG_FILE "xend-config.sxp"
#define XEND_PCI_CONFIG_PREFIX "xend-pci-"
#define QEMU_IF_SCRIPT "qemu-ifup"
78
#define XM_XML_ERROR "Invalid xml"
79

80
#ifndef WITH_XEN_INOTIFY
81 82 83 84
static int xenInotifyActive(virConnectPtr conn ATTRIBUTE_UNUSED)
{
   return 0;
}
85
#else
86 87
static int xenInotifyActive(virConnectPtr conn)
{
88
   xenUnifiedPrivatePtr priv = conn->privateData;
89
   return priv->inotifyWatch > 0;
90
}
91
#endif
92

93 94

/* Release memory associated with a cached config object */
95
static void xenXMConfigFree(void *payload, const void *key ATTRIBUTE_UNUSED) {
96
    xenXMConfCachePtr entry = (xenXMConfCachePtr)payload;
97
    virDomainDefFree(entry->def);
E
Eric Blake 已提交
98
    VIR_FREE(entry->filename);
99
    VIR_FREE(entry);
100 101
}

102 103 104 105
struct xenXMConfigReaperData {
    xenUnifiedPrivatePtr priv;
    time_t now;
};
106

107
/* Remove any configs which were not refreshed recently */
108 109 110 111 112
static int
xenXMConfigReaper(const void *payload,
                  const void *key ATTRIBUTE_UNUSED,
                  const void *data)
{
113
    const struct xenXMConfigReaperData *args = data;
114 115
    xenXMConfCachePtr entry = (xenXMConfCachePtr)payload;

116 117
    /* We're going to purge this config file, so check if it
       is currently mapped as owner of a named domain. */
118
    if (entry->refreshedAt != args->now) {
119
        const char *olddomname = entry->def->name;
120
        char *nameowner = (char *)virHashLookup(args->priv->nameConfigMap, olddomname);
121
        if (nameowner && STREQ(nameowner, key)) {
122
            virHashRemoveEntry(args->priv->nameConfigMap, olddomname);
123
        }
124
        return 1;
125
    }
126
    return 0;
127 128
}

129 130

static virDomainDefPtr
131 132
xenXMConfigReadFile(virConnectPtr conn, const char *filename)
{
133 134
    virConfPtr conf;
    virDomainDefPtr def;
135
    xenUnifiedPrivatePtr priv = conn->privateData;
136

137
    if (!(conf = virConfReadFile(filename, 0)))
138 139
        return NULL;

M
Markus Groß 已提交
140
    def = xenParseXM(conf, priv->xendConfigVersion, priv->caps);
141 142 143 144 145 146
    virConfFree(conf);

    return def;
}

static int
147 148 149 150
xenXMConfigSaveFile(virConnectPtr conn,
                    const char *filename,
                    virDomainDefPtr def)
{
151
    virConfPtr conf;
152
    xenUnifiedPrivatePtr priv = conn->privateData;
153 154
    int ret;

M
Markus Groß 已提交
155
    if (!(conf = xenFormatXM(conn, def, priv->xendConfigVersion)))
156 157 158 159 160 161 162
        return -1;

    ret = virConfWriteFile(filename, conf);
    virConfFree(conf);
    return ret;
}

D
Daniel P. Berrange 已提交
163 164 165

/*
 * Caller must hold the lock on 'conn->privateData' before
J
Ján Tomko 已提交
166
 * calling this function
D
Daniel P. Berrange 已提交
167
 */
168
int
169
xenXMConfigCacheRemoveFile(virConnectPtr conn, const char *filename)
170
{
171
    xenUnifiedPrivatePtr priv = conn->privateData;
172 173
    xenXMConfCachePtr entry;

174
    entry = virHashLookup(priv->configCache, filename);
175
    if (!entry) {
176
        VIR_DEBUG("No config entry for %s", filename);
177 178 179
        return 0;
    }

180 181
    virHashRemoveEntry(priv->nameConfigMap, entry->def->name);
    virHashRemoveEntry(priv->configCache, filename);
182
    VIR_DEBUG("Removed %s %s", entry->def->name, filename);
183 184 185 186
    return 0;
}


D
Daniel P. Berrange 已提交
187 188
/*
 * Caller must hold the lock on 'conn->privateData' before
J
Ján Tomko 已提交
189
 * calling this function
D
Daniel P. Berrange 已提交
190
 */
191 192 193
int
xenXMConfigCacheAddFile(virConnectPtr conn, const char *filename)
{
194
    xenUnifiedPrivatePtr priv = conn->privateData;
195 196 197 198 199
    xenXMConfCachePtr entry;
    struct stat st;
    int newborn = 0;
    time_t now = time(NULL);

200
    VIR_DEBUG("Adding file %s", filename);
201 202 203

    /* Get modified time */
    if ((stat(filename, &st) < 0)) {
204
        virReportSystemError(errno,
205 206
                             _("cannot stat: %s"),
                             filename);
207 208 209 210 211 212
        return -1;
    }

    /* Ignore zero length files, because inotify fires before
       any content has actually been created */
    if (st.st_size == 0) {
213
        VIR_DEBUG("Ignoring zero length file %s", filename);
214 215 216 217 218
        return -1;
    }

    /* If we already have a matching entry and it is not
    modified, then carry on to next one*/
219
    if ((entry = virHashLookup(priv->configCache, filename))) {
220 221 222 223 224 225 226 227 228 229
        char *nameowner;

        if (entry->refreshedAt >= st.st_mtime) {
            entry->refreshedAt = now;
            /* return success if up-to-date */
            return 0;
        }

        /* If we currently own the name, then release it and
            re-acquire it later - just in case it was renamed */
230
        nameowner = (char *)virHashLookup(priv->nameConfigMap, entry->def->name);
231
        if (nameowner && STREQ(nameowner, filename)) {
232
            virHashRemoveEntry(priv->nameConfigMap, entry->def->name);
233 234 235 236 237 238 239 240
        }

        /* Clear existing config entry which needs refresh */
        virDomainDefFree(entry->def);
        entry->def = NULL;
    } else { /* Completely new entry */
        newborn = 1;
        if (VIR_ALLOC(entry) < 0) {
241
            virReportOOMError();
242 243
            return -1;
        }
E
Eric Blake 已提交
244 245 246 247 248
        if ((entry->filename = strdup(filename)) == NULL) {
            virReportOOMError();
            VIR_FREE(entry);
            return -1;
        }
249 250 251 252
    }
    entry->refreshedAt = now;

    if (!(entry->def = xenXMConfigReadFile(conn, entry->filename))) {
253
        VIR_DEBUG("Failed to read %s", entry->filename);
254
        if (!newborn)
255
            virHashSteal(priv->configCache, filename);
E
Eric Blake 已提交
256
        VIR_FREE(entry->filename);
257 258 259 260 261 262 263
        VIR_FREE(entry);
        return -1;
    }

    /* If its a completely new entry, it must be stuck into
        the cache (refresh'd entries are already registered) */
    if (newborn) {
264
        if (virHashAddEntry(priv->configCache, entry->filename, entry) < 0) {
265
            virDomainDefFree(entry->def);
E
Eric Blake 已提交
266
            VIR_FREE(entry->filename);
267
            VIR_FREE(entry);
268 269
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("xenXMConfigCacheRefresh: virHashAddEntry"));
270 271 272 273 274 275 276
            return -1;
        }
    }

    /* See if we need to map this config file in as the primary owner
        * of the domain in question
        */
277
    if (!virHashLookup(priv->nameConfigMap, entry->def->name)) {
E
Eric Blake 已提交
278 279
        if (virHashAddEntry(priv->nameConfigMap, entry->def->name,
                            entry->filename) < 0) {
280
            virHashSteal(priv->configCache, filename);
281
            virDomainDefFree(entry->def);
E
Eric Blake 已提交
282
            VIR_FREE(entry->filename);
283 284 285
            VIR_FREE(entry);
        }
    }
286
    VIR_DEBUG("Added config %s %s", entry->def->name, filename);
287 288 289

    return 0;
}
290

291
/* This method is called by various methods to scan /etc/xen
D
Daniel P. Berrange 已提交
292 293 294 295 296 297
 * (or whatever directory was set by  LIBVIRT_XM_CONFIG_DIR
 * environment variable) and process any domain configs. It
 * has rate-limited so never rescans more frequently than
 * once every X seconds
 *
 * Caller must hold the lock on 'conn->privateData' before
J
Ján Tomko 已提交
298
 * calling this function
D
Daniel P. Berrange 已提交
299
 */
300 301 302
int
xenXMConfigCacheRefresh(virConnectPtr conn)
{
303
    xenUnifiedPrivatePtr priv = conn->privateData;
304 305 306 307
    DIR *dh;
    struct dirent *ent;
    time_t now = time(NULL);
    int ret = -1;
308
    struct xenXMConfigReaperData args;
309 310

    if (now == ((time_t)-1)) {
311
        virReportSystemError(errno,
312
                             "%s", _("cannot get time of day"));
313
        return -1;
314 315 316
    }

    /* Rate limit re-scans */
317
    if ((now - priv->lastRefresh) < XM_REFRESH_INTERVAL)
318
        return 0;
319

320
    priv->lastRefresh = now;
321 322

    /* Process the files in the config dir */
323
    if (!(dh = opendir(priv->configDir))) {
324
        virReportSystemError(errno,
325
                             _("cannot read directory %s"),
326
                             priv->configDir);
327
        return -1;
328 329 330 331
    }

    while ((ent = readdir(dh))) {
        struct stat st;
332
        char *path;
333 334 335 336 337 338

        /*
         * Skip a bunch of crufty files that clearly aren't config files
         */

        /* Like 'dot' files... */
339
        if (STRPREFIX(ent->d_name, "."))
340 341
            continue;
        /* ...and the XenD server config file */
342
        if (STRPREFIX(ent->d_name, XEND_CONFIG_FILE))
343 344
            continue;
        /* ...and random PCI config cruft */
345
        if (STRPREFIX(ent->d_name, XEND_PCI_CONFIG_PREFIX))
346 347
            continue;
        /* ...and the example domain configs */
348
        if (STRPREFIX(ent->d_name, XM_EXAMPLE_PREFIX))
349 350
            continue;
        /* ...and the QEMU networking script */
351
        if (STRPREFIX(ent->d_name, QEMU_IF_SCRIPT))
352 353 354 355 356 357 358 359 360
            continue;

        /* ...and editor backups */
        if (ent->d_name[0] == '#')
            continue;
        if (ent->d_name[strlen(ent->d_name)-1] == '~')
            continue;

        /* Build the full file path */
361 362 363 364
        if (!(path = virFileBuildPath(priv->configDir, ent->d_name, NULL))) {
            closedir(dh);
            return -1;
        }
365 366 367 368

        /* Skip anything which isn't a file (takes care of scripts/ subdir */
        if ((stat(path, &st) < 0) ||
            (!S_ISREG(st.st_mode))) {
369
            VIR_FREE(path);
370 371 372 373 374
            continue;
        }

        /* If we already have a matching entry and it is not
           modified, then carry on to next one*/
375
        if (xenXMConfigCacheAddFile(conn, path) < 0) {
J
Ján Tomko 已提交
376
            /* Ignoring errors, since a lot of stuff goes wrong in /etc/xen */
377
        }
378 379

        VIR_FREE(path);
380 381 382 383 384 385
    }

    /* Reap all entries which were not changed, by comparing
       their refresh timestamp - the timestamp should match
       'now' if they were refreshed. If timestamp doesn't match
       then the config is no longer on disk */
386 387
    args.now = now;
    args.priv = priv;
388
    virHashRemoveSet(priv->configCache, xenXMConfigReaper, &args);
389 390
    ret = 0;

391
    closedir(dh);
392

393
    return ret;
394 395 396 397
}


/*
398 399 400 401
 * The XM driver keeps a cache of config files as virDomainDefPtr
 * objects in the xenUnifiedPrivatePtr. Optionally inotify lets
 * us watch for changes (see separate driver), otherwise we poll
 * every few seconds
402
 */
403
int
404 405 406
xenXMOpen(virConnectPtr conn,
          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
          unsigned int flags)
407
{
408 409
    xenUnifiedPrivatePtr priv = conn->privateData;

410
    virCheckFlags(VIR_CONNECT_RO, -1);
E
Eric Blake 已提交
411

412 413
    priv->configDir = XM_CONFIG_DIR;

414
    priv->configCache = virHashCreate(50, xenXMConfigFree);
415
    if (!priv->configCache)
416
        return -1;
417
    priv->nameConfigMap = virHashCreate(50, NULL);
418
    if (!priv->nameConfigMap) {
419
        virHashFree(priv->configCache);
420
        priv->configCache = NULL;
421
        return -1;
422
    }
423 424 425 426
    /* Force the cache to be reloaded next time that
     * xenXMConfigCacheRefresh is called.
     */
    priv->lastRefresh = 0;
427

428
    return 0;
429 430 431
}

/*
432 433
 * Free the cached config files associated with this
 * connection
434
 */
435 436 437
int
xenXMClose(virConnectPtr conn)
{
438 439
    xenUnifiedPrivatePtr priv = conn->privateData;

440 441
    virHashFree(priv->nameConfigMap);
    virHashFree(priv->configCache);
442

443
    return 0;
444 445
}

446 447 448 449
/*
 * Since these are all offline domains, the state is always SHUTOFF.
 */
int
450 451
xenXMDomainGetState(virConnectPtr conn ATTRIBUTE_UNUSED,
                    virDomainDefPtr def ATTRIBUTE_UNUSED,
452 453
                    int *state,
                    int *reason)
454 455 456 457 458 459 460 461 462
{
    *state = VIR_DOMAIN_SHUTOFF;
    if (reason)
        *reason = 0;

    return 0;
}


463 464
/*
 * Since these are all offline domains, we only return info about
465
 * VCPUs and memory.
466
 */
467
int
468 469 470
xenXMDomainGetInfo(virConnectPtr conn,
                   virDomainDefPtr def,
                   virDomainInfoPtr info)
471
{
472
    xenUnifiedPrivatePtr priv = conn->privateData;
473
    const char *filename;
474 475
    xenXMConfCachePtr entry;

D
Daniel P. Berrange 已提交
476
    xenUnifiedLock(priv);
477

478
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
479
        goto error;
480

481
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
482
        goto error;
483 484

    memset(info, 0, sizeof(virDomainInfo));
485 486
    info->maxMem = entry->def->mem.max_balloon;
    info->memory = entry->def->mem.cur_balloon;
487
    info->nrVirtCpu = entry->def->vcpus;
488 489 490
    info->state = VIR_DOMAIN_SHUTOFF;
    info->cpuTime = 0;

D
Daniel P. Berrange 已提交
491
    xenUnifiedUnlock(priv);
492
    return 0;
493

D
Daniel P. Berrange 已提交
494 495 496
error:
    xenUnifiedUnlock(priv);
    return -1;
497 498 499
}


500 501
/*
 * Turn a config record into a lump of XML describing the
502
 * domain, suitable for later feeding for virDomainCreateXML
503
 */
504 505 506
virDomainDefPtr
xenXMDomainGetXMLDesc(virConnectPtr conn,
                      virDomainDefPtr def)
E
Eric Blake 已提交
507
{
508
    xenUnifiedPrivatePtr priv = conn->privateData;
509 510
    const char *filename;
    xenXMConfCachePtr entry;
511
    virDomainDefPtr ret = NULL;
512

E
Eric Blake 已提交
513 514
    /* Flags checked by virDomainDefFormat */

D
Daniel P. Berrange 已提交
515
    xenUnifiedLock(priv);
516

517
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
518
        goto cleanup;
519

520
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
521 522
        goto cleanup;

523 524 525 526
    ret = virDomainDefCopy(entry->def,
                           priv->caps,
                           priv->xmlopt,
                           false);
527

D
Daniel P. Berrange 已提交
528 529 530
cleanup:
    xenUnifiedUnlock(priv);
    return ret;
531 532 533
}


534 535 536
/*
 * Update amount of memory in the config file
 */
537
int
538 539 540
xenXMDomainSetMemory(virConnectPtr conn,
                     virDomainDefPtr def,
                     unsigned long memory)
541
{
542
    xenUnifiedPrivatePtr priv = conn->privateData;
543
    const char *filename;
544
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
545
    int ret = -1;
546

547 548 549 550
    if (memory < 1024 * MIN_XEN_GUEST_SIZE) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Memory %lu too small, min %lu"),
                       memory, (unsigned long)1024 * MIN_XEN_GUEST_SIZE);
551
        return -1;
552
    }
553

D
Daniel P. Berrange 已提交
554
    xenUnifiedLock(priv);
555

556
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
557
        goto cleanup;
558

559
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
560
        goto cleanup;
561

562 563 564
    entry->def->mem.cur_balloon = memory;
    if (entry->def->mem.cur_balloon > entry->def->mem.max_balloon)
        entry->def->mem.cur_balloon = entry->def->mem.max_balloon;
565 566 567 568

    /* If this fails, should we try to undo our changes to the
     * in-memory representation of the config file. I say not!
     */
569
    if (xenXMConfigSaveFile(conn, entry->filename, entry->def) < 0)
D
Daniel P. Berrange 已提交
570 571
        goto cleanup;
    ret = 0;
572

D
Daniel P. Berrange 已提交
573 574 575
cleanup:
    xenUnifiedUnlock(priv);
    return ret;
576 577 578 579 580
}

/*
 * Update maximum memory limit in config
 */
581
int
582 583 584
xenXMDomainSetMaxMemory(virConnectPtr conn,
                        virDomainDefPtr def,
                        unsigned long memory)
585
{
586
    xenUnifiedPrivatePtr priv = conn->privateData;
587
    const char *filename;
588
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
589
    int ret = -1;
590

591 592 593 594
    if (memory < 1024 * MIN_XEN_GUEST_SIZE) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Memory %lu too small, min %lu"),
                       memory, (unsigned long)1024 * MIN_XEN_GUEST_SIZE);
595
        return -1;
596
    }
597

D
Daniel P. Berrange 已提交
598
    xenUnifiedLock(priv);
599

600
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
601
        goto cleanup;
602

603
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
604
        goto cleanup;
605

606 607 608
    entry->def->mem.max_balloon = memory;
    if (entry->def->mem.cur_balloon > entry->def->mem.max_balloon)
        entry->def->mem.cur_balloon = entry->def->mem.max_balloon;
609 610 611 612

    /* If this fails, should we try to undo our changes to the
     * in-memory representation of the config file. I say not!
     */
613
    if (xenXMConfigSaveFile(conn, entry->filename, entry->def) < 0)
D
Daniel P. Berrange 已提交
614 615
        goto cleanup;
    ret = 0;
616

D
Daniel P. Berrange 已提交
617 618 619
cleanup:
    xenUnifiedUnlock(priv);
    return ret;
620 621 622 623 624
}

/*
 * Get max memory limit from config
 */
625
unsigned long long
626 627
xenXMDomainGetMaxMemory(virConnectPtr conn,
                        virDomainDefPtr def)
628
{
629
    xenUnifiedPrivatePtr priv = conn->privateData;
630
    const char *filename;
631
    xenXMConfCachePtr entry;
632
    unsigned long long ret = 0;
633

D
Daniel P. Berrange 已提交
634
    xenUnifiedLock(priv);
635

636
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
637
        goto cleanup;
638

639
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
640 641
        goto cleanup;

642
    ret = entry->def->mem.max_balloon;
643

D
Daniel P. Berrange 已提交
644 645 646
cleanup:
    xenUnifiedUnlock(priv);
    return ret;
647 648
}

649 650 651 652 653 654 655 656
/*
 * xenXMDomainSetVcpusFlags:
 * @domain: pointer to domain object
 * @nvcpus: number of vcpus
 * @flags: bitwise-ORd from virDomainVcpuFlags
 *
 * Change virtual CPUs allocation of domain according to flags.
 *
657
 * Returns 0 on success, -1 if an error message was issued
658 659
 */
int
660 661
xenXMDomainSetVcpusFlags(virDomainPtr domain,
                         unsigned int vcpus,
662 663
                         unsigned int flags)
{
664
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
665 666 667 668 669
    const char *filename;
    xenXMConfCachePtr entry;
    int ret = -1;
    int max;

E
Eric Blake 已提交
670 671 672 673
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

674
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
675 676
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("domain is not running"));
677 678 679 680 681 682 683 684 685 686 687 688
        return -1;
    }

    xenUnifiedLock(priv);

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
        goto cleanup;

    if (!(entry = virHashLookup(priv->configCache, filename)))
        goto cleanup;

    /* Hypervisor maximum. */
689
    if ((max = xenUnifiedConnectGetMaxVcpus(domain->conn, NULL)) < 0) {
690 691
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not determine max vcpus for the domain"));
692 693 694 695 696 697 698
        goto cleanup;
    }
    /* Can't specify a current larger than stored maximum; but
     * reducing maximum can silently reduce current.  */
    if (!(flags & VIR_DOMAIN_VCPU_MAXIMUM))
        max = entry->def->maxvcpus;
    if (vcpus > max) {
699 700 701
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested vcpus is greater than max allowable"
                         " vcpus for the domain: %d > %d"), vcpus, max);
702 703 704 705 706 707 708 709 710 711 712 713
        goto cleanup;
    }

    if (flags & VIR_DOMAIN_VCPU_MAXIMUM) {
        entry->def->maxvcpus = vcpus;
        if (entry->def->vcpus > vcpus)
            entry->def->vcpus = vcpus;
    } else {
        entry->def->vcpus = vcpus;
    }

    /* If this fails, should we try to undo our changes to the
714 715
     * in-memory representation of the config file. I say not!
     */
716
    if (xenXMConfigSaveFile(domain->conn, entry->filename, entry->def) < 0)
D
Daniel P. Berrange 已提交
717 718
        goto cleanup;
    ret = 0;
719

D
Daniel P. Berrange 已提交
720 721 722
cleanup:
    xenUnifiedUnlock(priv);
    return ret;
723 724
}

725 726 727 728 729 730 731 732
/**
 * xenXMDomainGetVcpusFlags:
 * @domain: pointer to domain object
 * @flags: bitwise-ORd from virDomainVcpuFlags
 *
 * Extract information about virtual CPUs of domain according to flags.
 *
 * Returns the number of vcpus on success, -1 if an error message was
733
 * issued
734 735 736 737
 */
int
xenXMDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
{
738
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
739 740 741 742
    const char *filename;
    xenXMConfCachePtr entry;
    int ret = -2;

E
Eric Blake 已提交
743 744 745 746
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

747
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
748
        virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("domain not active"));
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
        return -1;
    }

    xenUnifiedLock(priv);

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
        goto cleanup;

    if (!(entry = virHashLookup(priv->configCache, filename)))
        goto cleanup;

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

cleanup:
    xenUnifiedUnlock(priv);
    return ret;
}

768 769 770 771 772 773 774 775 776 777 778
/**
 * xenXMDomainPinVcpu:
 * @domain: pointer to domain object
 * @vcpu: virtual CPU number (reserved)
 * @cpumap: pointer to a bit map of real CPUs (in 8-bit bytes)
 * @maplen: length of cpumap in bytes
 *
 * Set the vcpu affinity in config
 *
 * Returns 0 for success; -1 (with errno) on error
 */
779 780 781 782 783
int
xenXMDomainPinVcpu(virDomainPtr domain,
                   unsigned int vcpu ATTRIBUTE_UNUSED,
                   unsigned char *cpumap,
                   int maplen)
784
{
785
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
786 787 788 789
    const char *filename;
    xenXMConfCachePtr entry;
    int ret = -1;

790
    if (maplen > (int)sizeof(cpumap_t)) {
791
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
792 793 794
        return -1;
    }

D
Daniel P. Berrange 已提交
795
    xenUnifiedLock(priv);
796 797

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name))) {
798
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("virHashLookup"));
D
Daniel P. Berrange 已提交
799
        goto cleanup;
800
    }
801
    if (!(entry = virHashLookup(priv->configCache, filename))) {
802 803
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("can't retrieve config file for domain"));
D
Daniel P. Berrange 已提交
804
        goto cleanup;
805 806
    }

H
Hu Tao 已提交
807
    virBitmapFree(entry->def->cpumask);
L
liguang 已提交
808 809 810
    entry->def->cpumask = virBitmapNewData(cpumap, maplen);
    if (!entry->def->cpumask)
        goto cleanup;
811
    if (xenXMConfigSaveFile(domain->conn, entry->filename, entry->def) < 0)
812 813 814 815 816
        goto cleanup;

    ret = 0;

 cleanup:
D
Daniel P. Berrange 已提交
817
    xenUnifiedUnlock(priv);
818
    return ret;
819 820
}

821 822 823
/*
 * Find an inactive domain based on its name
 */
824
virDomainDefPtr
825 826 827
xenXMDomainLookupByName(virConnectPtr conn, const char *domname)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
828
    const char *filename;
829
    xenXMConfCachePtr entry;
830
    virDomainDefPtr ret = NULL;
831

D
Daniel P. Berrange 已提交
832
    xenUnifiedLock(priv);
833

834
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
835
        goto cleanup;
836

837
    if (!(filename = virHashLookup(priv->nameConfigMap, domname)))
D
Daniel P. Berrange 已提交
838
        goto cleanup;
839

D
Daniel P. Berrange 已提交
840 841
    if (!(entry = virHashLookup(priv->configCache, filename)))
        goto cleanup;
842

843
    ret = virDomainDefNew(domname, entry->def->uuid, -1);
844

D
Daniel P. Berrange 已提交
845 846
cleanup:
    xenUnifiedUnlock(priv);
847
    return ret;
848 849 850 851 852 853
}


/*
 * Hash table iterator to search for a domain based on UUID
 */
854 855 856 857 858
static int
xenXMDomainSearchForUUID(const void *payload,
                         const void *name ATTRIBUTE_UNUSED,
                         const void *data)
{
859 860 861
    const unsigned char *wantuuid = (const unsigned char *)data;
    const xenXMConfCachePtr entry = (const xenXMConfCachePtr)payload;

862
    if (!memcmp(entry->def->uuid, wantuuid, VIR_UUID_BUFLEN))
863
        return 1;
864

865
    return 0;
866 867 868 869 870
}

/*
 * Find an inactive domain based on its UUID
 */
871
virDomainDefPtr
872 873 874
xenXMDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
875
    xenXMConfCachePtr entry;
876
    virDomainDefPtr ret = NULL;
877

D
Daniel P. Berrange 已提交
878
    xenUnifiedLock(priv);
879

880
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
881
        goto cleanup;
882

D
Daniel P. Berrange 已提交
883 884
    if (!(entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID, (const void *)uuid)))
        goto cleanup;
885

886
    ret = virDomainDefNew(entry->def->name, uuid, -1);
887

D
Daniel P. Berrange 已提交
888 889
cleanup:
    xenUnifiedUnlock(priv);
890
    return ret;
891 892 893 894 895 896
}


/*
 * Start a domain from an existing defined config file
 */
897
int
898 899
xenXMDomainCreate(virConnectPtr conn,
                  virDomainDefPtr def)
900
{
901
    char *sexpr;
D
Daniel P. Berrange 已提交
902
    int ret = -1;
903
    xenUnifiedPrivatePtr priv = conn->privateData;
904
    const char *filename;
905
    xenXMConfCachePtr entry = NULL;
906

D
Daniel P. Berrange 已提交
907 908
    xenUnifiedLock(priv);

909
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
910
        goto error;
911

912
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
913
        goto error;
914

915
    if (!(sexpr = xenFormatSxpr(conn, entry->def, priv->xendConfigVersion)))
D
Daniel P. Berrange 已提交
916
        goto error;
917

918
    ret = xenDaemonDomainCreateXML(conn, sexpr);
919
    VIR_FREE(sexpr);
D
Daniel P. Berrange 已提交
920 921
    if (ret != 0)
        goto error;
922

923
    if ((ret = xenDaemonDomainLookupByName_ids(conn, def->name,
D
Daniel P. Berrange 已提交
924 925
                                               entry->def->uuid)) < 0)
        goto error;
926
    def->id = ret;
927

928
    if (xend_wait_for_devices(conn, def->name) < 0)
D
Daniel P. Berrange 已提交
929
        goto error;
930

931
    if (xenDaemonDomainResume(conn, entry->def) < 0)
D
Daniel P. Berrange 已提交
932
        goto error;
933

D
Daniel P. Berrange 已提交
934
    xenUnifiedUnlock(priv);
935
    return 0;
936

D
Daniel P. Berrange 已提交
937
 error:
938 939 940
    if (def->id != -1 && entry) {
        xenDaemonDomainDestroy(conn, entry->def);
        def->id = -1;
941
    }
D
Daniel P. Berrange 已提交
942
    xenUnifiedUnlock(priv);
943
    return -1;
944 945
}

946 947 948 949
/*
 * Create a config file for a domain, based on an XML
 * document describing its config
 */
950 951
int
xenXMDomainDefineXML(virConnectPtr conn, virDomainDefPtr def)
952
{
953
    char *filename = NULL;
954
    const char *oldfilename;
955
    virConfPtr conf = NULL;
956
    xenXMConfCachePtr entry = NULL;
957
    xenUnifiedPrivatePtr priv = conn->privateData;
958

D
Daniel P. Berrange 已提交
959 960
    xenUnifiedLock(priv);

961
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0) {
D
Daniel P. Berrange 已提交
962
        xenUnifiedUnlock(priv);
963
        return -1;
D
Daniel P. Berrange 已提交
964
    }
965

966 967 968
    if (!(conf = xenFormatXM(conn, def, priv->xendConfigVersion)))
        goto error;

969 970 971 972 973 974 975 976 977 978 979
    /*
     * check that if there is another domain defined with the same uuid
     * it has the same name
     */
    if ((entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID,
                               (const void *)&(def->uuid))) != NULL) {
        if ((entry->def != NULL) && (entry->def->name != NULL) &&
            (STRNEQ(def->name, entry->def->name))) {
            char uuidstr[VIR_UUID_STRING_BUFLEN];

            virUUIDFormat(entry->def->uuid, uuidstr);
980 981 982
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("domain '%s' is already defined with uuid %s"),
                           entry->def->name, uuidstr);
983 984 985 986 987 988
            entry = NULL;
            goto error;
        }
        entry = NULL;
    }

989
    if (virHashLookup(priv->nameConfigMap, def->name)) {
990 991
        /* domain exists, we will overwrite it */

992
        if (!(oldfilename = (char *)virHashLookup(priv->nameConfigMap, def->name))) {
993 994
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("can't retrieve config filename for domain to overwrite"));
995 996 997
            goto error;
        }

998
        if (!(entry = virHashLookup(priv->configCache, oldfilename))) {
999 1000
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("can't retrieve config entry for domain to overwrite"));
1001 1002 1003 1004
            goto error;
        }

        /* Remove the name -> filename mapping */
1005
        if (virHashRemoveEntry(priv->nameConfigMap, def->name) < 0) {
1006 1007
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("failed to remove old domain from config map"));
1008 1009 1010 1011
            goto error;
        }

        /* Remove the config record itself */
1012
        if (virHashRemoveEntry(priv->configCache, oldfilename) < 0) {
1013 1014
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("failed to remove old domain from config map"));
1015 1016 1017 1018
            goto error;
        }

        entry = NULL;
1019
    }
1020

1021
    if (!(filename = virFileBuildPath(priv->configDir, def->name, NULL)))
1022 1023
        goto error;

1024
    if (virConfWriteFile(filename, conf) < 0)
1025 1026
        goto error;

1027
    if (VIR_ALLOC(entry) < 0) {
1028
        virReportOOMError();
1029
        goto error;
1030
    }
1031

1032
    if ((entry->refreshedAt = time(NULL)) == ((time_t)-1)) {
1033 1034
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to get current time"));
1035
        goto error;
1036
    }
1037

E
Eric Blake 已提交
1038 1039 1040 1041
    if ((entry->filename = strdup(filename)) == NULL) {
        virReportOOMError();
        goto error;
    }
1042
    entry->def = def;
1043

1044
    if (virHashAddEntry(priv->configCache, filename, entry) < 0) {
1045 1046
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to store config file handle"));
1047
        goto error;
1048
    }
1049

1050
    if (virHashAddEntry(priv->nameConfigMap, def->name, entry->filename) < 0) {
1051
        virHashSteal(priv->configCache, filename);
1052 1053
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to store config file handle"));
1054
        goto error;
1055 1056
    }

D
Daniel P. Berrange 已提交
1057
    xenUnifiedUnlock(priv);
1058
    VIR_FREE(filename);
1059
    return 0;
1060 1061

 error:
1062
    VIR_FREE(filename);
1063 1064
    if (entry)
        VIR_FREE(entry->filename);
1065
    VIR_FREE(entry);
1066
    virConfFree(conf);
D
Daniel P. Berrange 已提交
1067
    xenUnifiedUnlock(priv);
1068
    return -1;
1069 1070 1071 1072 1073
}

/*
 * Delete a domain from disk
 */
1074
int
1075 1076
xenXMDomainUndefine(virConnectPtr conn,
                    virDomainDefPtr def)
1077
{
1078
    xenUnifiedPrivatePtr priv = conn->privateData;
1079
    const char *filename;
1080
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
1081 1082 1083
    int ret = -1;

    xenUnifiedLock(priv);
1084

1085
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
1086
        goto cleanup;
1087

1088
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1089
        goto cleanup;
1090 1091

    if (unlink(entry->filename) < 0)
D
Daniel P. Berrange 已提交
1092
        goto cleanup;
1093

1094
    /* Remove the name -> filename mapping */
1095
    if (virHashRemoveEntry(priv->nameConfigMap, def->name) < 0)
1096
        goto cleanup;
1097

1098 1099 1100
    /* Remove the config record itself */
    if (virHashRemoveEntry(priv->configCache, entry->filename) < 0)
        goto cleanup;
1101

D
Daniel P. Berrange 已提交
1102 1103 1104 1105 1106
    ret = 0;

cleanup:
    xenUnifiedUnlock(priv);
    return ret;
1107 1108 1109 1110
}

struct xenXMListIteratorContext {
    virConnectPtr conn;
1111
    int oom;
1112 1113
    int max;
    int count;
1114
    char ** names;
1115 1116
};

1117 1118
static void
xenXMListIterator(void *payload ATTRIBUTE_UNUSED, const void *name, void *data) {
1119
    struct xenXMListIteratorContext *ctx = data;
1120
    virDomainDefPtr def = NULL;
1121

1122 1123 1124
    if (ctx->oom)
        return;

1125 1126 1127
    if (ctx->count == ctx->max)
        return;

1128 1129
    def = xenDaemonLookupByName(ctx->conn, name);
    if (!def) {
1130 1131 1132 1133
        if (!(ctx->names[ctx->count] = strdup(name)))
            ctx->oom = 1;
        else
            ctx->count++;
1134
    } else {
1135
        virDomainDefFree(def);
1136 1137 1138 1139 1140 1141 1142 1143
    }
}


/*
 * List all defined domains, filtered to remove any which
 * are currently running
 */
1144 1145 1146 1147
int
xenXMListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
1148
    struct xenXMListIteratorContext ctx;
1149
    int i, ret = -1;
1150

D
Daniel P. Berrange 已提交
1151
    xenUnifiedLock(priv);
1152

1153
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
1154
        goto cleanup;
1155

1156 1157
    if (maxnames > virHashSize(priv->configCache))
        maxnames = virHashSize(priv->configCache);
1158 1159

    ctx.conn = conn;
1160
    ctx.oom = 0;
1161 1162 1163 1164
    ctx.count = 0;
    ctx.max = maxnames;
    ctx.names = names;

1165
    virHashForEach(priv->nameConfigMap, xenXMListIterator, &ctx);
1166 1167 1168 1169 1170

    if (ctx.oom) {
        for (i = 0; i < ctx.count; i++)
            VIR_FREE(ctx.names[i]);

1171
        virReportOOMError();
1172 1173 1174
        goto cleanup;
    }

D
Daniel P. Berrange 已提交
1175 1176 1177 1178 1179
    ret = ctx.count;

cleanup:
    xenUnifiedUnlock(priv);
    return ret;
1180 1181 1182 1183 1184 1185
}

/*
 * Return the maximum number of defined domains - not filtered
 * based on number running
 */
1186 1187 1188 1189
int
xenXMNumOfDefinedDomains(virConnectPtr conn)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
D
Daniel P. Berrange 已提交
1190
    int ret = -1;
1191

D
Daniel P. Berrange 已提交
1192
    xenUnifiedLock(priv);
1193

1194
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
1195
        goto cleanup;
1196

D
Daniel P. Berrange 已提交
1197 1198 1199 1200 1201
    ret = virHashSize(priv->nameConfigMap);

cleanup:
    xenUnifiedUnlock(priv);
    return ret;
1202 1203
}

1204

1205
/**
1206
 * xenXMDomainAttachDeviceFlags:
1207 1208
 * @domain: pointer to domain object
 * @xml: pointer to XML description of device
1209
 * @flags: an OR'ed set of virDomainDeviceModifyFlags
J
Jim Meyering 已提交
1210
 *
1211 1212
 * Create a virtual device attachment to backend.
 * XML description is translated into config file.
1213 1214
 * This driver only supports device allocation to
 * persisted config.
J
Jim Meyering 已提交
1215
 *
1216 1217
 * Returns 0 in case of success, -1 in case of failure.
 */
1218
int
1219 1220
xenXMDomainAttachDeviceFlags(virDomainPtr domain,
                             const char *xml,
E
Eric Blake 已提交
1221 1222
                             unsigned int flags)
{
1223 1224
    const char *filename = NULL;
    xenXMConfCachePtr entry = NULL;
1225 1226
    int ret = -1;
    virDomainDeviceDefPtr dev = NULL;
1227
    virDomainDefPtr def;
1228
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
1229

E
Eric Blake 已提交
1230 1231
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1);

1232
    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) ||
E
Eric Blake 已提交
1233
        (domain->id != -1 && flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)) {
1234 1235
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Xm driver only supports modifying persistent config"));
1236
        return -1;
1237
    }
1238

D
Daniel P. Berrange 已提交
1239 1240
    xenUnifiedLock(priv);

1241
    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
1242
        goto cleanup;
1243
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1244
        goto cleanup;
1245
    def = entry->def;
1246

1247 1248
    if (!(dev = virDomainDeviceDefParse(xml, entry->def,
                                        priv->caps,
1249
                                        priv->xmlopt,
1250
                                        VIR_DOMAIN_XML_INACTIVE)))
D
Daniel P. Berrange 已提交
1251
        goto cleanup;
1252

1253 1254 1255
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
    {
1256
        if (virDomainDiskInsert(def, dev->data.disk) < 0) {
1257
            virReportOOMError();
1258
            goto cleanup;
1259 1260
        }
        dev->data.disk = NULL;
1261
    }
1262
    break;
1263

1264 1265
    case VIR_DOMAIN_DEVICE_NET:
    {
1266
        if (VIR_REALLOC_N(def->nets, def->nnets+1) < 0) {
1267
            virReportOOMError();
1268 1269 1270
            goto cleanup;
        }
        def->nets[def->nnets++] = dev->data.net;
1271 1272
        dev->data.net = NULL;
        break;
1273 1274
    }

1275
    default:
1276 1277
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Xm driver only supports adding disk or network devices"));
1278 1279 1280 1281 1282 1283
        goto cleanup;
    }

    /* If this fails, should we try to undo our changes to the
     * in-memory representation of the config file. I say not!
     */
1284
    if (xenXMConfigSaveFile(domain->conn, entry->filename, entry->def) < 0)
1285 1286 1287 1288 1289
        goto cleanup;

    ret = 0;

 cleanup:
1290
    virDomainDeviceDefFree(dev);
D
Daniel P. Berrange 已提交
1291
    xenUnifiedUnlock(priv);
1292 1293 1294 1295 1296
    return ret;
}


/**
1297
 * xenXMDomainDetachDeviceFlags:
1298 1299
 * @domain: pointer to domain object
 * @xml: pointer to XML description of device
1300
 * @flags: an OR'ed set of virDomainDeviceModifyFlags
J
Jim Meyering 已提交
1301
 *
1302
 * Destroy a virtual device attachment to backend.
1303 1304
 * This driver only supports device deallocation from
 * persisted config.
1305 1306 1307
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
1308
int
1309 1310 1311 1312
xenXMDomainDetachDeviceFlags(virDomainPtr domain,
                             const char *xml,
                             unsigned int flags)
{
1313 1314
    const char *filename = NULL;
    xenXMConfCachePtr entry = NULL;
1315
    virDomainDeviceDefPtr dev = NULL;
1316
    virDomainDefPtr def;
1317
    int ret = -1;
1318
    int i;
1319
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
1320

E
Eric Blake 已提交
1321 1322
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1);

1323
    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) ||
E
Eric Blake 已提交
1324
        (domain->id != -1 && flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)) {
1325 1326
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Xm driver only supports modifying persistent config"));
1327
        return -1;
1328
    }
D
Daniel P. Berrange 已提交
1329 1330 1331

    xenUnifiedLock(priv);

1332
    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
1333
        goto cleanup;
1334
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1335
        goto cleanup;
1336
    def = entry->def;
1337

1338 1339
    if (!(dev = virDomainDeviceDefParse(xml, entry->def,
                                        priv->caps,
1340
                                        priv->xmlopt,
1341
                                        VIR_DOMAIN_XML_INACTIVE)))
D
Daniel P. Berrange 已提交
1342
        goto cleanup;
1343

1344 1345 1346
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
    {
1347 1348
        for (i = 0 ; i < def->ndisks ; i++) {
            if (def->disks[i]->dst &&
1349
                dev->data.disk->dst &&
1350 1351 1352 1353 1354
                STREQ(def->disks[i]->dst, dev->data.disk->dst)) {
                virDomainDiskDefFree(def->disks[i]);
                if (i < (def->ndisks - 1))
                    memmove(def->disks + i,
                            def->disks + i + 1,
1355 1356
                            sizeof(*def->disks) *
                            (def->ndisks - (i + 1)));
1357
                def->ndisks--;
1358
                break;
1359 1360
            }
        }
1361 1362 1363 1364 1365
        break;
    }

    case VIR_DOMAIN_DEVICE_NET:
    {
1366
        for (i = 0 ; i < def->nnets ; i++) {
1367
            if (!virMacAddrCmp(&def->nets[i]->mac, &dev->data.net->mac)) {
1368 1369 1370 1371
                virDomainNetDefFree(def->nets[i]);
                if (i < (def->nnets - 1))
                    memmove(def->nets + i,
                            def->nets + i + 1,
1372 1373
                            sizeof(*def->nets) *
                            (def->nnets - (i + 1)));
1374
                def->nnets--;
1375 1376 1377 1378
                break;
            }
        }
        break;
1379
    }
1380
    default:
1381 1382 1383
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("device type '%s' cannot be detached"),
                       virDomainDeviceTypeToString(dev->type));
1384 1385 1386 1387 1388 1389
        goto cleanup;
    }

    /* If this fails, should we try to undo our changes to the
     * in-memory representation of the config file. I say not!
     */
1390
    if (xenXMConfigSaveFile(domain->conn, entry->filename, entry->def) < 0)
1391 1392 1393 1394 1395
        goto cleanup;

    ret = 0;

 cleanup:
1396
    virDomainDeviceDefFree(dev);
D
Daniel P. Berrange 已提交
1397
    xenUnifiedUnlock(priv);
1398
    return ret;
1399 1400
}

R
Richard W.M. Jones 已提交
1401
int
1402 1403 1404 1405 1406
xenXMDomainBlockPeek(virDomainPtr dom ATTRIBUTE_UNUSED,
                     const char *path ATTRIBUTE_UNUSED,
                     unsigned long long offset ATTRIBUTE_UNUSED,
                     size_t size ATTRIBUTE_UNUSED,
                     void *buffer ATTRIBUTE_UNUSED)
R
Richard W.M. Jones 已提交
1407
{
1408 1409
    virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                   _("block peeking not implemented"));
R
Richard W.M. Jones 已提交
1410 1411 1412
    return -1;
}

1413

1414 1415
static char *
xenXMAutostartLinkName(virDomainPtr dom)
1416 1417
{
    char *ret;
1418 1419
    if (virAsprintf(&ret, "/etc/xen/auto/%s", dom->name) < 0)
        return NULL;
1420 1421 1422
    return ret;
}

1423 1424
static char *
xenXMDomainConfigName(virDomainPtr dom)
1425 1426
{
    char *ret;
1427 1428
    if (virAsprintf(&ret, "/etc/xen/%s", dom->name) < 0)
        return NULL;
1429 1430 1431
    return ret;
}

1432 1433
int
xenXMDomainGetAutostart(virDomainPtr dom, int *autostart)
1434 1435 1436 1437 1438 1439
{
    char *linkname = xenXMAutostartLinkName(dom);
    char *config = xenXMDomainConfigName(dom);
    int ret = -1;

    if (!linkname || !config) {
1440
        virReportOOMError();
1441 1442 1443 1444 1445
        goto cleanup;
    }

    *autostart = virFileLinkPointsTo(linkname, config);
    if (*autostart < 0) {
1446
        virReportSystemError(errno,
1447 1448
                             _("cannot check link %s points to config %s"),
                             linkname, config);
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
        goto cleanup;
    }

    ret = 0;

cleanup:
    VIR_FREE(linkname);
    VIR_FREE(config);
    return ret;
}


1461 1462
int
xenXMDomainSetAutostart(virDomainPtr dom, int autostart)
1463 1464 1465 1466 1467 1468
{
    char *linkname = xenXMAutostartLinkName(dom);
    char *config = xenXMDomainConfigName(dom);
    int ret = -1;

    if (!linkname || !config) {
1469
        virReportOOMError();
1470 1471 1472 1473 1474 1475
        goto cleanup;
    }

    if (autostart) {
        if (symlink(config, linkname) < 0 &&
            errno != EEXIST) {
1476
            virReportSystemError(errno,
1477 1478
                                 _("failed to create link %s to %s"),
                                 config, linkname);
1479 1480 1481 1482 1483
            goto cleanup;
        }
    } else {
        if (unlink(linkname)  < 0 &&
            errno != ENOENT) {
1484
            virReportSystemError(errno,
1485 1486
                                 _("failed to remove link %s"),
                                 linkname);
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
            goto cleanup;
        }
    }
    ret = 0;

cleanup:
    VIR_FREE(linkname);
    VIR_FREE(config);

    return ret;
}