xm_internal.c 39.6 KB
Newer Older
1
/*
2
 * xm_internal.c: helper routines for dealing with inactive domains
3
 *
4
 * Copyright (C) 2006-2007, 2009-2014 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 57
VIR_LOG_INIT("xen.xm_internal");

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

66 67 68 69 70
/* 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

71 72
char * xenXMAutoAssignMac(void);

73 74 75 76 77 78 79
#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"
80
#define XM_XML_ERROR "Invalid xml"
81

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

95 96

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

105 106 107 108
struct xenXMConfigReaperData {
    xenUnifiedPrivatePtr priv;
    time_t now;
};
109

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

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

131 132

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

139
    if (!(conf = virConfReadFile(filename, 0)))
140 141
        return NULL;

142
    def = xenParseXM(conf, priv->caps, priv->xmlopt);
143 144 145 146 147 148
    virConfFree(conf);

    return def;
}

static int
149 150 151 152
xenXMConfigSaveFile(virConnectPtr conn,
                    const char *filename,
                    virDomainDefPtr def)
{
153 154 155
    virConfPtr conf;
    int ret;

156
    if (!(conf = xenFormatXM(conn, def)))
157 158 159 160 161 162 163
        return -1;

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

D
Daniel P. Berrange 已提交
164 165 166

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

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

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


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

200
    VIR_DEBUG("Adding file %s %lld", filename, (long long)now);
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

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

    if (!(entry->def = xenXMConfigReadFile(conn, entry->filename))) {
249
        VIR_DEBUG("Failed to read %s", entry->filename);
250
        if (!newborn)
251
            virHashSteal(priv->configCache, filename);
E
Eric Blake 已提交
252
        VIR_FREE(entry->filename);
253 254 255 256 257 258 259
        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) {
260
        if (virHashAddEntry(priv->configCache, entry->filename, entry) < 0) {
261
            virDomainDefFree(entry->def);
E
Eric Blake 已提交
262
            VIR_FREE(entry->filename);
263
            VIR_FREE(entry);
264 265
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("xenXMConfigCacheRefresh: virHashAddEntry"));
266 267 268 269 270 271 272
            return -1;
        }
    }

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

    return 0;
}
289

290
/* This method is called by various methods to scan /etc/xen
D
Daniel P. Berrange 已提交
291 292 293 294 295 296
 * (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 已提交
297
 * calling this function
D
Daniel P. Berrange 已提交
298
 */
299 300 301
int
xenXMConfigCacheRefresh(virConnectPtr conn)
{
302
    xenUnifiedPrivatePtr priv = conn->privateData;
303 304 305 306
    DIR *dh;
    struct dirent *ent;
    time_t now = time(NULL);
    int ret = -1;
307
    struct xenXMConfigReaperData args;
308 309

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

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

319
    priv->lastRefresh = now;
320 321

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

E
Eric Blake 已提交
329
    while ((ret = virDirRead(dh, &ent, priv->configDir)) > 0) {
330
        struct stat st;
331
        char *path;
332 333 334 335 336 337

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

        /* Like 'dot' files... */
338
        if (STRPREFIX(ent->d_name, "."))
339 340
            continue;
        /* ...and the XenD server config file */
341
        if (STRPREFIX(ent->d_name, XEND_CONFIG_FILE))
342 343
            continue;
        /* ...and random PCI config cruft */
344
        if (STRPREFIX(ent->d_name, XEND_PCI_CONFIG_PREFIX))
345 346
            continue;
        /* ...and the example domain configs */
347
        if (STRPREFIX(ent->d_name, XM_EXAMPLE_PREFIX))
348 349
            continue;
        /* ...and the QEMU networking script */
350
        if (STRPREFIX(ent->d_name, QEMU_IF_SCRIPT))
351 352 353 354 355 356 357 358 359
            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 */
360
        if (!(path = virFileBuildPath(priv->configDir, ent->d_name, NULL))) {
J
Ján Tomko 已提交
361
            VIR_DIR_CLOSE(dh);
362 363
            return -1;
        }
364 365 366 367

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

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

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

    /* 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 */
385 386
    args.now = now;
    args.priv = priv;
387
    virHashRemoveSet(priv->configCache, xenXMConfigReaper, &args);
388

J
Ján Tomko 已提交
389
    VIR_DIR_CLOSE(dh);
390

391
    return ret;
392 393 394 395
}


/*
396 397 398 399
 * 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
400
 */
401
int
402 403 404
xenXMOpen(virConnectPtr conn,
          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
          unsigned int flags)
405
{
406 407
    xenUnifiedPrivatePtr priv = conn->privateData;

408
    virCheckFlags(VIR_CONNECT_RO, -1);
E
Eric Blake 已提交
409

410 411
    priv->configDir = XM_CONFIG_DIR;

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

426
    return 0;
427 428 429
}

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

438 439
    virHashFree(priv->nameConfigMap);
    virHashFree(priv->configCache);
440

441
    return 0;
442 443
}

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

    return 0;
}


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

D
Daniel P. Berrange 已提交
474
    xenUnifiedLock(priv);
475

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

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

    memset(info, 0, sizeof(virDomainInfo));
483
    info->maxMem = virDomainDefGetMemoryTotal(entry->def);
484
    info->memory = entry->def->mem.cur_balloon;
485
    info->nrVirtCpu = virDomainDefGetVcpus(entry->def);
486 487 488
    info->state = VIR_DOMAIN_SHUTOFF;
    info->cpuTime = 0;

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

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


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

E
Eric Blake 已提交
511 512
    /* Flags checked by virDomainDefFormat */

D
Daniel P. Berrange 已提交
513
    xenUnifiedLock(priv);
514

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

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

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

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


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

545 546 547 548
    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);
549
        return -1;
550
    }
551

D
Daniel P. Berrange 已提交
552
    xenUnifiedLock(priv);
553

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

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

560
    entry->def->mem.cur_balloon = memory;
561 562
    if (entry->def->mem.cur_balloon > virDomainDefGetMemoryTotal(entry->def))
        entry->def->mem.cur_balloon = virDomainDefGetMemoryTotal(entry->def);
563 564 565 566

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

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

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

589 590 591 592
    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);
593
        return -1;
594
    }
595

D
Daniel P. Berrange 已提交
596
    xenUnifiedLock(priv);
597

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

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

604 605
    if (entry->def->mem.cur_balloon > memory)
        entry->def->mem.cur_balloon = memory;
606

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

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

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

D
Daniel P. Berrange 已提交
632
    xenUnifiedLock(priv);
633

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

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

640
    ret = virDomainDefGetMemoryTotal(entry->def);
641

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

647 648
/*
 * xenXMDomainSetVcpusFlags:
649 650
 * @conn: the connection object
 * @def: domain configuration
651 652 653 654 655
 * @nvcpus: number of vcpus
 * @flags: bitwise-ORd from virDomainVcpuFlags
 *
 * Change virtual CPUs allocation of domain according to flags.
 *
656
 * Returns 0 on success, -1 if an error message was issued
657 658
 */
int
659 660
xenXMDomainSetVcpusFlags(virConnectPtr conn,
                         virDomainDefPtr def,
661
                         unsigned int vcpus,
662 663
                         unsigned int flags)
{
664
    xenUnifiedPrivatePtr priv = 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
        return -1;
    }

    xenUnifiedLock(priv);

682
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
683 684 685 686 687 688
        goto cleanup;

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

    /* Hypervisor maximum. */
689
    if ((max = xenUnifiedConnectGetMaxVcpus(conn, NULL)) < 0) {
690 691
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not determine max vcpus for the domain"));
692 693 694 695 696
        goto cleanup;
    }
    /* Can't specify a current larger than stored maximum; but
     * reducing maximum can silently reduce current.  */
    if (!(flags & VIR_DOMAIN_VCPU_MAXIMUM))
697 698
        max = virDomainDefGetVcpusMax(entry->def);

699
    if (vcpus > max) {
700 701 702
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested vcpus is greater than max allowable"
                         " vcpus for the domain: %d > %d"), vcpus, max);
703 704 705 706
        goto cleanup;
    }

    if (flags & VIR_DOMAIN_VCPU_MAXIMUM) {
707 708
        if (virDomainDefSetVcpusMax(entry->def, vcpus) < 0)
            goto cleanup;
709
    } else {
710 711
        if (virDomainDefSetVcpus(entry->def, vcpus) < 0)
            goto cleanup;
712 713 714
    }

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

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

726 727
/**
 * xenXMDomainGetVcpusFlags:
728 729
 * @conn: the connection object
 * @def: domain configuration
730 731 732 733 734
 * @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
735
 * issued
736 737
 */
int
738 739 740
xenXMDomainGetVcpusFlags(virConnectPtr conn,
                         virDomainDefPtr def,
                         unsigned int flags)
741
{
742
    xenUnifiedPrivatePtr priv = conn->privateData;
743 744
    const char *filename;
    xenXMConfCachePtr entry;
745
    int ret = -1;
746

E
Eric Blake 已提交
747 748 749 750
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

751
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
752
        virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("domain not active"));
753 754 755 756 757
        return -1;
    }

    xenUnifiedLock(priv);

758
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
759 760 761 762 763
        goto cleanup;

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

764 765 766
    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
        ret = virDomainDefGetVcpusMax(entry->def);
    else
767
        ret = virDomainDefGetVcpus(entry->def);
768

769
 cleanup:
770 771 772 773
    xenUnifiedUnlock(priv);
    return ret;
}

774 775
/**
 * xenXMDomainPinVcpu:
776 777
 * @conn: the connection object
 * @def: domain configuration
778 779 780 781 782 783 784 785
 * @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
 */
786
int
787 788
xenXMDomainPinVcpu(virConnectPtr conn,
                   virDomainDefPtr def,
789 790 791
                   unsigned int vcpu ATTRIBUTE_UNUSED,
                   unsigned char *cpumap,
                   int maplen)
792
{
793
    xenUnifiedPrivatePtr priv = conn->privateData;
794 795 796 797
    const char *filename;
    xenXMConfCachePtr entry;
    int ret = -1;

798
    if (maplen > (int)sizeof(cpumap_t)) {
799
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
800 801 802
        return -1;
    }

D
Daniel P. Berrange 已提交
803
    xenUnifiedLock(priv);
804

805
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name))) {
806
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("virHashLookup"));
D
Daniel P. Berrange 已提交
807
        goto cleanup;
808
    }
809
    if (!(entry = virHashLookup(priv->configCache, filename))) {
810 811
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("can't retrieve config file for domain"));
D
Daniel P. Berrange 已提交
812
        goto cleanup;
813 814
    }

H
Hu Tao 已提交
815
    virBitmapFree(entry->def->cpumask);
L
liguang 已提交
816 817 818
    entry->def->cpumask = virBitmapNewData(cpumap, maplen);
    if (!entry->def->cpumask)
        goto cleanup;
819
    if (xenXMConfigSaveFile(conn, entry->filename, entry->def) < 0)
820 821 822 823 824
        goto cleanup;

    ret = 0;

 cleanup:
D
Daniel P. Berrange 已提交
825
    xenUnifiedUnlock(priv);
826
    return ret;
827 828
}

829 830 831
/*
 * Find an inactive domain based on its name
 */
832
virDomainDefPtr
833 834 835
xenXMDomainLookupByName(virConnectPtr conn, const char *domname)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
836
    const char *filename;
837
    xenXMConfCachePtr entry;
838
    virDomainDefPtr ret = NULL;
839

D
Daniel P. Berrange 已提交
840
    xenUnifiedLock(priv);
841

842
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
843
        goto cleanup;
844

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

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

851
    ret = virDomainDefNewFull(domname, entry->def->uuid, -1);
852

853
 cleanup:
D
Daniel P. Berrange 已提交
854
    xenUnifiedUnlock(priv);
855
    return ret;
856 857 858 859 860 861
}


/*
 * Hash table iterator to search for a domain based on UUID
 */
862 863 864 865 866
static int
xenXMDomainSearchForUUID(const void *payload,
                         const void *name ATTRIBUTE_UNUSED,
                         const void *data)
{
867 868
    const unsigned char *wantuuid = data;
    const xenXMConfCache *entry = payload;
869

870
    if (!memcmp(entry->def->uuid, wantuuid, VIR_UUID_BUFLEN))
871
        return 1;
872

873
    return 0;
874 875 876 877 878
}

/*
 * Find an inactive domain based on its UUID
 */
879
virDomainDefPtr
880 881 882
xenXMDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
883
    xenXMConfCachePtr entry;
884
    virDomainDefPtr ret = NULL;
885

D
Daniel P. Berrange 已提交
886
    xenUnifiedLock(priv);
887

888
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
889
        goto cleanup;
890

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

894
    ret = virDomainDefNewFull(entry->def->name, uuid, -1);
895

896
 cleanup:
D
Daniel P. Berrange 已提交
897
    xenUnifiedUnlock(priv);
898
    return ret;
899 900 901 902 903 904
}


/*
 * Start a domain from an existing defined config file
 */
905
int
906 907
xenXMDomainCreate(virConnectPtr conn,
                  virDomainDefPtr def)
908
{
909
    char *sexpr;
D
Daniel P. Berrange 已提交
910
    int ret = -1;
911
    xenUnifiedPrivatePtr priv = conn->privateData;
912
    const char *filename;
913
    xenXMConfCachePtr entry = NULL;
914

D
Daniel P. Berrange 已提交
915 916
    xenUnifiedLock(priv);

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

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

923
    if (!(sexpr = xenFormatSxpr(conn, entry->def)))
D
Daniel P. Berrange 已提交
924
        goto error;
925

926
    ret = xenDaemonDomainCreateXML(conn, sexpr);
927
    VIR_FREE(sexpr);
D
Daniel P. Berrange 已提交
928 929
    if (ret != 0)
        goto error;
930

931
    if ((ret = xenDaemonDomainLookupByName_ids(conn, def->name,
D
Daniel P. Berrange 已提交
932 933
                                               entry->def->uuid)) < 0)
        goto error;
934
    def->id = ret;
935

936
    if (xend_wait_for_devices(conn, def->name) < 0)
D
Daniel P. Berrange 已提交
937
        goto error;
938

939
    if (xenDaemonDomainResume(conn, entry->def) < 0)
D
Daniel P. Berrange 已提交
940
        goto error;
941

D
Daniel P. Berrange 已提交
942
    xenUnifiedUnlock(priv);
943
    return 0;
944

D
Daniel P. Berrange 已提交
945
 error:
946 947 948
    if (def->id != -1 && entry) {
        xenDaemonDomainDestroy(conn, entry->def);
        def->id = -1;
949
    }
D
Daniel P. Berrange 已提交
950
    xenUnifiedUnlock(priv);
951
    return -1;
952 953
}

954 955 956 957
/*
 * Create a config file for a domain, based on an XML
 * document describing its config
 */
958 959
int
xenXMDomainDefineXML(virConnectPtr conn, virDomainDefPtr def)
960
{
961
    char *filename = NULL;
962
    const char *oldfilename;
963
    virConfPtr conf = NULL;
964
    xenXMConfCachePtr entry = NULL;
965
    xenUnifiedPrivatePtr priv = conn->privateData;
966

D
Daniel P. Berrange 已提交
967 968
    xenUnifiedLock(priv);

969
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0) {
D
Daniel P. Berrange 已提交
970
        xenUnifiedUnlock(priv);
971
        return -1;
D
Daniel P. Berrange 已提交
972
    }
973

974
    if (!(conf = xenFormatXM(conn, def)))
975 976
        goto error;

977 978 979 980 981 982 983 984 985 986 987
    /*
     * 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);
988 989 990
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("domain '%s' is already defined with uuid %s"),
                           entry->def->name, uuidstr);
991 992 993 994 995 996
            entry = NULL;
            goto error;
        }
        entry = NULL;
    }

997
    if (virHashLookup(priv->nameConfigMap, def->name)) {
998 999
        /* domain exists, we will overwrite it */

1000
        if (!(oldfilename = (char *)virHashLookup(priv->nameConfigMap, def->name))) {
1001 1002
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("can't retrieve config filename for domain to overwrite"));
1003 1004 1005
            goto error;
        }

1006
        if (!(entry = virHashLookup(priv->configCache, oldfilename))) {
1007 1008
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("can't retrieve config entry for domain to overwrite"));
1009 1010 1011 1012
            goto error;
        }

        /* Remove the name -> filename mapping */
1013
        if (virHashRemoveEntry(priv->nameConfigMap, def->name) < 0) {
1014 1015
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("failed to remove old domain from config map"));
1016 1017 1018 1019
            goto error;
        }

        /* Remove the config record itself */
1020
        if (virHashRemoveEntry(priv->configCache, oldfilename) < 0) {
1021 1022
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("failed to remove old domain from config map"));
1023 1024 1025 1026
            goto error;
        }

        entry = NULL;
1027
    }
1028

1029
    if (!(filename = virFileBuildPath(priv->configDir, def->name, NULL)))
1030 1031
        goto error;

1032
    if (virConfWriteFile(filename, conf) < 0)
1033 1034
        goto error;

1035
    if (VIR_ALLOC(entry) < 0)
1036 1037
        goto error;

1038
    if ((entry->refreshedAt = time(NULL)) == ((time_t)-1)) {
1039 1040
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to get current time"));
1041
        goto error;
1042
    }
1043

1044
    if (VIR_STRDUP(entry->filename, filename) < 0)
E
Eric Blake 已提交
1045
        goto error;
1046
    entry->def = def;
1047

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

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

D
Daniel P. Berrange 已提交
1061
    xenUnifiedUnlock(priv);
1062
    VIR_FREE(filename);
1063
    return 0;
1064 1065

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

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

    xenUnifiedLock(priv);
1088

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

1092
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1093
        goto cleanup;
1094 1095

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

1098
    /* Remove the name -> filename mapping */
1099
    if (virHashRemoveEntry(priv->nameConfigMap, def->name) < 0)
1100
        goto cleanup;
1101

1102 1103 1104
    /* Remove the config record itself */
    if (virHashRemoveEntry(priv->configCache, entry->filename) < 0)
        goto cleanup;
1105

D
Daniel P. Berrange 已提交
1106 1107
    ret = 0;

1108
 cleanup:
D
Daniel P. Berrange 已提交
1109 1110
    xenUnifiedUnlock(priv);
    return ret;
1111 1112 1113 1114
}

struct xenXMListIteratorContext {
    virConnectPtr conn;
1115
    int oom;
1116 1117
    int max;
    int count;
1118
    char ** names;
1119 1120
};

1121
static int
1122 1123 1124 1125
xenXMListIterator(void *payload ATTRIBUTE_UNUSED,
                  const void *name,
                  void *data)
{
1126
    struct xenXMListIteratorContext *ctx = data;
1127
    virDomainDefPtr def = NULL;
1128

1129
    if (ctx->oom)
1130
        return 0;
1131

1132
    if (ctx->count == ctx->max)
1133
        return 0;
1134

1135 1136
    def = xenDaemonLookupByName(ctx->conn, name);
    if (!def) {
1137
        if (VIR_STRDUP(ctx->names[ctx->count], name) < 0)
1138 1139 1140
            ctx->oom = 1;
        else
            ctx->count++;
1141
    } else {
1142
        virDomainDefFree(def);
1143
    }
1144
    return 0;
1145 1146 1147 1148 1149 1150 1151
}


/*
 * List all defined domains, filtered to remove any which
 * are currently running
 */
1152 1153 1154 1155
int
xenXMListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
1156
    struct xenXMListIteratorContext ctx;
1157 1158
    size_t i;
    int ret = -1;
1159

D
Daniel P. Berrange 已提交
1160
    xenUnifiedLock(priv);
1161

1162
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
1163
        goto cleanup;
1164

1165 1166
    if (maxnames > virHashSize(priv->configCache))
        maxnames = virHashSize(priv->configCache);
1167 1168

    ctx.conn = conn;
1169
    ctx.oom = 0;
1170 1171 1172 1173
    ctx.count = 0;
    ctx.max = maxnames;
    ctx.names = names;

1174
    virHashForEach(priv->nameConfigMap, xenXMListIterator, &ctx);
1175 1176 1177 1178 1179 1180 1181

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

D
Daniel P. Berrange 已提交
1182 1183
    ret = ctx.count;

1184
 cleanup:
D
Daniel P. Berrange 已提交
1185 1186
    xenUnifiedUnlock(priv);
    return ret;
1187 1188 1189 1190 1191 1192
}

/*
 * Return the maximum number of defined domains - not filtered
 * based on number running
 */
1193 1194 1195 1196
int
xenXMNumOfDefinedDomains(virConnectPtr conn)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
D
Daniel P. Berrange 已提交
1197
    int ret = -1;
1198

D
Daniel P. Berrange 已提交
1199
    xenUnifiedLock(priv);
1200

1201
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
1202
        goto cleanup;
1203

D
Daniel P. Berrange 已提交
1204 1205
    ret = virHashSize(priv->nameConfigMap);

1206
 cleanup:
D
Daniel P. Berrange 已提交
1207 1208
    xenUnifiedUnlock(priv);
    return ret;
1209 1210
}

1211

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

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

1241
    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) ||
1242
        (minidef->id != -1 && flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)) {
1243 1244
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Xm driver only supports modifying persistent config"));
1245
        return -1;
1246
    }
1247

D
Daniel P. Berrange 已提交
1248 1249
    xenUnifiedLock(priv);

1250
    if (!(filename = virHashLookup(priv->nameConfigMap, minidef->name)))
D
Daniel P. Berrange 已提交
1251
        goto cleanup;
1252
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1253
        goto cleanup;
1254
    def = entry->def;
1255

1256 1257
    if (!(dev = virDomainDeviceDefParse(xml, entry->def,
                                        priv->caps,
1258
                                        priv->xmlopt,
1259
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE)))
D
Daniel P. Berrange 已提交
1260
        goto cleanup;
1261

1262 1263 1264
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
    {
1265
        if (virDomainDiskInsert(def, dev->data.disk) < 0)
1266
            goto cleanup;
1267
        dev->data.disk = NULL;
1268
    }
1269
    break;
1270

1271 1272
    case VIR_DOMAIN_DEVICE_NET:
    {
1273
        if (VIR_APPEND_ELEMENT(def->nets, def->nnets, dev->data.net) < 0)
1274
            goto cleanup;
1275
        break;
1276 1277
    }

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

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

    ret = 0;

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


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

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

1328
    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) ||
1329
        (minidef->id != -1 && flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)) {
1330 1331
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Xm driver only supports modifying persistent config"));
1332
        return -1;
1333
    }
D
Daniel P. Berrange 已提交
1334 1335 1336

    xenUnifiedLock(priv);

1337
    if (!(filename = virHashLookup(priv->nameConfigMap, minidef->name)))
D
Daniel P. Berrange 已提交
1338
        goto cleanup;
1339
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1340
        goto cleanup;
1341
    def = entry->def;
1342

1343 1344
    if (!(dev = virDomainDeviceDefParse(xml, entry->def,
                                        priv->caps,
1345
                                        priv->xmlopt,
1346 1347
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE |
                                        VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE)))
D
Daniel P. Berrange 已提交
1348
        goto cleanup;
1349

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

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

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

    ret = 0;

 cleanup:
1392
    virDomainDeviceDefFree(dev);
D
Daniel P. Berrange 已提交
1393
    xenUnifiedUnlock(priv);
1394
    return ret;
1395 1396
}

R
Richard W.M. Jones 已提交
1397
int
1398 1399
xenXMDomainBlockPeek(virConnectPtr conn ATTRIBUTE_UNUSED,
                     virDomainDefPtr def ATTRIBUTE_UNUSED,
1400 1401 1402 1403
                     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 已提交
1404
{
1405 1406
    virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                   _("block peeking not implemented"));
R
Richard W.M. Jones 已提交
1407 1408 1409
    return -1;
}

1410

1411
static char *
1412
xenXMAutostartLinkName(virDomainDefPtr def)
1413 1414
{
    char *ret;
1415
    if (virAsprintf(&ret, "/etc/xen/auto/%s", def->name) < 0)
1416
        return NULL;
1417 1418 1419
    return ret;
}

1420
static char *
1421
xenXMDomainConfigName(virDomainDefPtr def)
1422 1423
{
    char *ret;
1424
    if (virAsprintf(&ret, "/etc/xen/%s", def->name) < 0)
1425
        return NULL;
1426 1427 1428
    return ret;
}

1429
int
1430 1431
xenXMDomainGetAutostart(virDomainDefPtr def,
                        int *autostart)
1432
{
1433
    char *config = xenXMDomainConfigName(def);
1434 1435
    int ret = -1;

1436
    if (!config)
1437 1438
        goto cleanup;

1439
    *autostart = virFileRelLinkPointsTo("/etc/xen/auto/", def->name, config);
1440
    if (*autostart < 0) {
1441
        virReportSystemError(errno,
1442 1443 1444
                             _("cannot check link /etc/xen/auto/%s points "
                               "to config %s"),
                             def->name, config);
1445 1446 1447 1448 1449
        goto cleanup;
    }

    ret = 0;

1450
 cleanup:
1451 1452 1453 1454 1455
    VIR_FREE(config);
    return ret;
}


1456
int
1457 1458
xenXMDomainSetAutostart(virDomainDefPtr def,
                        int autostart)
1459
{
1460 1461
    char *linkname = xenXMAutostartLinkName(def);
    char *config = xenXMDomainConfigName(def);
1462 1463
    int ret = -1;

1464
    if (!linkname || !config)
1465 1466 1467 1468 1469
        goto cleanup;

    if (autostart) {
        if (symlink(config, linkname) < 0 &&
            errno != EEXIST) {
1470
            virReportSystemError(errno,
1471 1472
                                 _("failed to create link %s to %s"),
                                 config, linkname);
1473 1474 1475 1476 1477
            goto cleanup;
        }
    } else {
        if (unlink(linkname)  < 0 &&
            errno != ENOENT) {
1478
            virReportSystemError(errno,
1479 1480
                                 _("failed to remove link %s"),
                                 linkname);
1481 1482 1483 1484 1485
            goto cleanup;
        }
    }
    ret = 0;

1486
 cleanup:
1487 1488 1489 1490 1491
    VIR_FREE(linkname);
    VIR_FREE(config);

    return ret;
}