openvz_conf.c 19.2 KB
Newer Older
1 2 3 4 5
/*
 * openvz_conf.c: config functions for managing OpenVZ VEs
 *
 * Copyright (C) 2006, 2007 Binary Karma
 * Copyright (C) 2006 Shuveb Hussain
6
 * Copyright (C) 2007 Anoop Joe Cyriac
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
22
 * Authors:
23 24 25
 * Shuveb Hussain <shuveb@binarykarma.com>
 * Anoop Joe Cyriac <anoop@binarykarma.com>
 *
26 27
 */

28
#include <config.h>
J
Jim Meyering 已提交
29

30 31 32 33 34 35 36 37
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
#include <strings.h>
#include <time.h>
38 39 40 41
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
D
Daniel P. Berrange 已提交
42
#include <string.h>
43
#include <sys/utsname.h>
44
#include <sys/wait.h>
45

46
#include "virterror_internal.h"
47
#include "openvz_conf.h"
48 49
#include "uuid.h"
#include "buf.h"
50
#include "memory.h"
51
#include "util.h"
52
#include "nodeinfo.h"
53

54 55
static char *openvzLocateConfDir(void);
static int openvzGetVPSUUID(int vpsid, char *uuidstr);
56
static int openvzLocateConfFile(int vpsid, char *conffile, int maxlen, const char *ext);
57
static int openvzAssignUUIDs(void);
58

59
int
60
strtoI(const char *str)
61 62 63
{
    int val;

64 65
    if (virStrToLong_i(str, NULL, 10, &val) < 0)
        return 0 ;
66

67 68 69
    return val;
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

static int
openvzExtractVersionInfo(const char *cmd, int *retversion)
{
    const char *const vzarg[] = { cmd, "--help", NULL };
    const char *const vzenv[] = { "LC_ALL=C", NULL };
    pid_t child;
    int newstdout = -1;
    int ret = -1, status;
    unsigned int major, minor, micro;
    unsigned int version;

    if (retversion)
        *retversion = 0;

    if (virExec(NULL, vzarg, vzenv, NULL,
                &child, -1, &newstdout, NULL, VIR_EXEC_NONE) < 0)
        return -1;

    char *help = NULL;
90
    int len = virFileReadLimFD(newstdout, 4096, &help);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    if (len < 0)
        goto cleanup2;

    if (sscanf(help, "vzctl version %u.%u.%u",
               &major, &minor, &micro) != 3) {
        goto cleanup2;
    }

    version = (major * 1000 * 1000) + (minor * 1000) + micro;

    if (retversion)
        *retversion = version;

    ret = 0;

cleanup2:
    VIR_FREE(help);
    if (close(newstdout) < 0)
        ret = -1;

rewait:
    if (waitpid(child, &status, 0) != child) {
        if (errno == EINTR)
            goto rewait;
        ret = -1;
    }

    return ret;
}

int openvzExtractVersion(virConnectPtr conn,
                         struct openvz_driver *driver)
{
    if (driver->version > 0)
        return 0;

    if (openvzExtractVersionInfo(VZCTL, &driver->version) < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                    "%s", _("Cound not extract vzctl version"));
        return -1;
    }

    return 0;
}


137 138 139 140 141 142 143 144 145 146 147 148
virCapsPtr openvzCapsInit(void)
{
    struct utsname utsname;
    virCapsPtr caps;
    virCapsGuestPtr guest;

    uname(&utsname);

    if ((caps = virCapabilitiesNew(utsname.machine,
                                   0, 0)) == NULL)
        goto no_memory;

149 150
    virCapabilitiesSetMacPrefix(caps, (unsigned char[]){ 0x52, 0x54, 0x00 });

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "exe",
                                         utsname.machine,
                                         sizeof(int) == 4 ? 32 : 8,
                                         NULL,
                                         NULL,
                                         0,
                                         NULL)) == NULL)
        goto no_memory;

    if (virCapabilitiesAddGuestDomain(guest,
                                      "openvz",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto no_memory;
    return caps;

no_memory:
    virCapabilitiesFree(caps);
    return NULL;
}


176 177 178 179
static int
openvzReadNetworkConf(virConnectPtr conn,
                      virDomainDefPtr def,
                      int veid) {
180
    int ret;
181
    virDomainNetDefPtr net;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    char temp[4096];
    char *token, *saveptr = NULL;

    /*parse routing network configuration*
     * Sample from config:
     *   IP_ADDRESS="1.1.1.1 1.1.1.2"
     *   splited IPs by space
     */
    ret = openvzReadConfigParam(veid, "IP_ADDRESS", temp, sizeof(temp));
    if (ret < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                 _("Cound not read 'IP_ADDRESS' from config for container %d"),
                  veid);
        goto error;
    } else if (ret > 0) {
        token = strtok_r(temp, " ", &saveptr);
        while (token != NULL) {
199
            if (VIR_ALLOC(net) < 0)
200 201 202 203 204 205 206 207
                goto no_memory;

            net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
            net->data.ethernet.ipaddr = strdup(token);

            if (net->data.ethernet.ipaddr == NULL)
                goto no_memory;

208 209 210 211 212
            if (VIR_REALLOC_N(def->nets, def->nnets + 1) < 0)
                goto no_memory;
            def->nets[def->nnets++] = net;
            net = NULL;

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            token = strtok_r(NULL, " ", &saveptr);
        }
    }

    /*parse bridge devices*/
    /*Sample from config:
     *NETIF="ifname=eth10,mac=00:18:51:C1:05:EE,host_ifname=veth105.10,host_mac=00:18:51:8F:D9:F3"
     *devices splited by ';'
     */
    ret = openvzReadConfigParam(veid, "NETIF", temp, sizeof(temp));
    if (ret < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                     _("Cound not read 'NETIF' from config for container %d"),
                     veid);
        goto error;
    } else if (ret > 0) {
        token = strtok_r(temp, ";", &saveptr);
        while (token != NULL) {
            /*add new device to list*/
232
            if (VIR_ALLOC(net) < 0)
233 234 235 236 237 238 239 240 241 242 243 244 245
                goto no_memory;

            net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;

            char *p = token, *next = token;
            char cpy_temp[32];
            int len;

            /*parse string*/
            do {
                while (*next != '\0' && *next != ',') next++;
                if (STRPREFIX(p, "ifname=")) {
                    p += 7;
246 247 248
                    /* skip in libvirt */
                } else if (STRPREFIX(p, "host_ifname=")) {
                    p += 12;
249 250 251
                    len = next - p;
                    if (len > 16) {
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
252
                                "%s", _("Too long network device name"));
253 254 255
                        goto error;
                    }

256 257 258 259 260 261 262 263 264 265 266 267 268 269
                    if (VIR_ALLOC_N(net->ifname, len+1) < 0)
                        goto no_memory;

                    strncpy(net->ifname, p, len);
                    net->ifname[len] = '\0';
                } else if (STRPREFIX(p, "bridge=")) {
                    p += 7;
                    len = next - p;
                    if (len > 16) {
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                                "%s", _("Too long bridge device name"));
                        goto error;
                    }

270 271 272 273 274 275 276 277 278 279
                    if (VIR_ALLOC_N(net->data.bridge.brname, len+1) < 0)
                        goto no_memory;

                    strncpy(net->data.bridge.brname, p, len);
                    net->data.bridge.brname[len] = '\0';
                } else if (STRPREFIX(p, "mac=")) {
                    p += 4;
                    len = next - p;
                    if (len != 17) { //should be 17
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
280
                              "%s", _("Wrong length MAC address"));
281 282 283 284
                        goto error;
                    }
                    strncpy(cpy_temp, p, len);
                    cpy_temp[len] = '\0';
285
                    if (virParseMacAddr(cpy_temp, net->mac) < 0) {
286
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
287
                              "%s", _("Wrong MAC address"));
288 289 290 291 292 293
                        goto error;
                    }
                }
                p = ++next;
            } while (p < token + strlen(token));

294 295 296 297 298
            if (VIR_REALLOC_N(def->nets, def->nnets + 1) < 0)
                goto no_memory;
            def->nets[def->nnets++] = net;
            net = NULL;

299 300 301 302
            token = strtok_r(NULL, ";", &saveptr);
        }
    }

303
    return 0;
304 305 306 307
no_memory:
    openvzError(conn, VIR_ERR_NO_MEMORY, NULL);
error:
    virDomainNetDefFree(net);
308
    return -1;
309 310 311
}


312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
static int
openvzReadFSConf(virConnectPtr conn,
                 virDomainDefPtr def,
                 int veid) {
    int ret;
    virDomainFSDefPtr fs = NULL;
    char temp[4096];

    ret = openvzReadConfigParam(veid, "OSTEMPLATE", temp, sizeof(temp));
    if (ret < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                    _("Cound not read 'OSTEMPLATE' from config for container %d"),
                    veid);
        goto error;
    } else if (ret > 0) {
        if (VIR_ALLOC(fs) < 0)
            goto no_memory;

        fs->type = VIR_DOMAIN_FS_TYPE_TEMPLATE;
        fs->src = strdup(temp);
        fs->dst = strdup("/");

        if (fs->src == NULL || fs->dst == NULL)
            goto no_memory;

        if (VIR_REALLOC_N(def->fss, def->nfss + 1) < 0)
            goto no_memory;
        def->fss[def->nfss++] = fs;
        fs = NULL;

    }

    return 0;
no_memory:
    openvzError(conn, VIR_ERR_NO_MEMORY, NULL);
error:
    virDomainFSDefFree(fs);
    return -1;
}


353 354 355 356 357 358
/* Free all memory associated with a openvz_driver structure */
void
openvzFreeDriver(struct openvz_driver *driver)
{
    if (!driver)
        return;
359

360
    virDomainObjListFree(&driver->domains);
361
    virCapabilitiesFree(driver->caps);
362
}
D
Daniel Veillard 已提交
363 364 365



366
int openvzLoadDomains(struct openvz_driver *driver) {
367 368 369
    FILE *fp;
    int veid, ret;
    char status[16];
370
    char uuidstr[VIR_UUID_STRING_BUFLEN];
371
    virDomainObjPtr dom = NULL;
372
    char temp[50];
373

374 375
    if (openvzAssignUUIDs() < 0)
        return -1;
376

377
    if ((fp = popen(VZLIST " -a -ovpsid,status -H 2>/dev/null", "r")) == NULL) {
J
Jim Meyering 已提交
378
        openvzError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("popen failed"));
379 380 381 382
        return -1;
    }

    while(!feof(fp)) {
383
        if (fscanf(fp, "%d %s\n", &veid, status) != 2) {
384 385
            if (feof(fp))
                break;
386

387
            openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
388
                        "%s", _("Failed to parse vzlist output"));
389
            goto cleanup;
390
        }
391

392 393 394
        if (VIR_ALLOC(dom) < 0 ||
            VIR_ALLOC(dom->def) < 0)
            goto no_memory;
395

396 397 398 399
        if (STREQ(status, "stopped"))
            dom->state = VIR_DOMAIN_SHUTOFF;
        else
            dom->state = VIR_DOMAIN_RUNNING;
400

401 402
        dom->pid = veid;
        dom->def->id = dom->state == VIR_DOMAIN_SHUTOFF ? -1 : veid;
403

404 405 406
        if (asprintf(&dom->def->name, "%i", veid) < 0) {
            dom->def->name = NULL;
            goto no_memory;
407
        }
408

409
        openvzGetVPSUUID(veid, uuidstr);
410
        ret = virUUIDParse(uuidstr, dom->def->uuid);
411

412 413
        if (ret == -1) {
            openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
414
                        "%s", _("UUID in config file malformed"));
415
            goto cleanup;
416
        }
417

418 419 420 421
        if (!(dom->def->os.type = strdup("exe")))
            goto no_memory;
        if (!(dom->def->os.init = strdup("/sbin/init")))
            goto no_memory;
422

423 424
        ret = openvzReadConfigParam(veid, "CPUS", temp, sizeof(temp));
        if (ret < 0) {
425
            openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
426
                        _("Cound not read config for container %d"),
427 428
                        veid);
            goto cleanup;
429
        } else if (ret > 0) {
430
            dom->def->vcpus = strtoI(temp);
431 432
        }

433 434 435
        if (ret == 0 || dom->def->vcpus == 0)
            dom->def->vcpus = openvzGetNodeCPUs();

436
        /* XXX load rest of VM config data .... */
437

438
        openvzReadNetworkConf(NULL, dom->def, veid);
439
        openvzReadFSConf(NULL, dom->def, veid);
440

441 442 443 444 445 446
        if (VIR_REALLOC_N(driver->domains.objs,
                          driver->domains.count + 1) < 0)
            goto no_memory;

        driver->domains.objs[driver->domains.count++] = dom;
        dom = NULL;
447
    }
448

449
    fclose(fp);
450

451
    return 0;
452

453 454
 no_memory:
    openvzError(NULL, VIR_ERR_NO_MEMORY, NULL);
455

456 457 458 459
 cleanup:
    fclose(fp);
    virDomainObjFree(dom);
    return -1;
460 461
}

462 463 464 465 466 467 468
unsigned int
openvzGetNodeCPUs(void)
{
    virNodeInfo nodeinfo;

    if (virNodeInfoPopulate(NULL, &nodeinfo) < 0) {
        openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
469
                    "%s", _("Cound not read nodeinfo"));
470 471 472 473 474
        return 0;
    }

    return nodeinfo.cpus;
}
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

int
openvzWriteConfigParam(int vpsid, const char *param, const char *value)
{
    char conf_file[PATH_MAX];
    char temp_file[PATH_MAX];
    char line[PATH_MAX] ;
    int fd, temp_fd;

    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
        return -1;
    if (openvzLocateConfFile(vpsid, temp_file, PATH_MAX, "tmp")<0)
        return -1;

    fd = open(conf_file, O_RDONLY);
    if (fd == -1)
        return -1;
    temp_fd = open(temp_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (temp_fd == -1) {
        close(fd);
        return -1;
    }

    while(1) {
        if (openvz_readline(fd, line, sizeof(line)) <= 0)
            break;

502
        if (!(STRPREFIX(line, param) && line[strlen(param)] == '=')) {
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
            if (safewrite(temp_fd, line, strlen(line)) !=
                strlen(line))
                goto error;
        }
    }

    if (safewrite(temp_fd, param, strlen(param)) < 0 ||
        safewrite(temp_fd, "=\"", 2) < 0 ||
        safewrite(temp_fd, value, strlen(value)) < 0 ||
        safewrite(temp_fd, "\"\n", 2) < 0)
        goto error;

    if (close(fd) < 0)
        goto error;
    fd = -1;
    if (close(temp_fd) < 0)
        goto error;
    temp_fd = -1;

    if (rename(temp_file, conf_file) < 0)
        goto error;

    return 0;

error:
    if (fd != -1)
        close(fd);
    if (temp_fd != -1)
        close(temp_fd);
    unlink(temp_file);
    return -1;
}

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
/*
* Read parameter from container config
* sample: 133, "OSTEMPLATE", value, 1024
* return: -1 - error
*	   0 - don't found
*          1 - OK
*/
int
openvzReadConfigParam(int vpsid ,const char * param, char *value, int maxlen)
{
    char conf_file[PATH_MAX] ;
    char line[PATH_MAX] ;
    int ret, found = 0;
    int fd ;
    char * sf, * token;
    char *saveptr = NULL;

553
    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
554 555 556 557 558
        return -1;

    value[0] = 0;

    fd = open(conf_file, O_RDONLY);
D
Daniel Veillard 已提交
559
    if (fd == -1)
560 561 562 563 564 565 566
        return -1;

    while(1) {
        ret = openvz_readline(fd, line, sizeof(line));
        if(ret <= 0)
            break;
        saveptr = NULL;
D
Daniel Veillard 已提交
567
        if (STREQLEN(line, param, strlen(param))) {
568 569
            sf = line;
            sf += strlen(param);
570 571 572 573 574 575 576
            if (sf[0] == '=' && sf[1] != '\0' ) {
                sf ++;
                if ((token = strtok_r(sf,"\"\t\n", &saveptr)) != NULL) {
                    strncpy(value, token, maxlen) ;
                    value[maxlen-1] = '\0';
                    found = 1;
                }
577
            }
D
Daniel Veillard 已提交
578
       }
579 580 581 582 583 584 585 586 587
    }
    close(fd);

    if (ret == 0 && found)
        ret = 1;

    return ret ;
}

588 589 590 591 592
/* Locate config file of container
* return -1 - error
*         0 - OK
*/
static int
593
openvzLocateConfFile(int vpsid, char *conffile, int maxlen, const char *ext)
594 595 596 597 598 599 600 601
{
    char * confdir;
    int ret = 0;

    confdir = openvzLocateConfDir();
    if (confdir == NULL)
        return -1;

602 603
    if (snprintf(conffile, maxlen, "%s/%d.%s",
                 confdir, vpsid, ext ? ext : "conf") >= maxlen)
604 605 606 607 608 609
        ret = -1;

    VIR_FREE(confdir);
    return ret;
}

610
static char
611
*openvzLocateConfDir(void)
612 613 614 615 616 617
{
    const char *conf_dir_list[] = {"/etc/vz/conf", "/usr/local/etc/conf", NULL};
    int i=0;

    while(conf_dir_list[i]) {
        if(!access(conf_dir_list[i], F_OK))
618
            return strdup(conf_dir_list[i]);
619 620 621 622 623 624 625
        i ++;
    }

    return NULL;
}

/* Richard Steven's classic readline() function */
626
int
627
openvz_readline(int fd, char *ptr, int maxlen)
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
{
    int n, rc;
    char c;

    for(n = 1; n < maxlen; n ++) {
        if( (rc = read(fd, &c, 1)) == 1) {
            *ptr++ = c;
            if(c == '\n')
                break;
        }
        else if(rc == 0) {
            if(n == 1)
                return 0; /* EOF condition */
            else
                break;
        }
        else
            return -1; /* error */
    }
    *ptr = 0;
    return n;
}

651
static int
652
openvzGetVPSUUID(int vpsid, char *uuidstr)
653 654 655
{
    char conf_file[PATH_MAX];
    char line[1024];
656
    char uuidbuf[1024];
657 658 659
    char iden[1024];
    int fd, ret;

660
    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
661
       return -1;
662

663
    fd = open(conf_file, O_RDONLY);
664 665 666 667 668 669 670 671 672
    if(fd == -1)
        return -1;

    while(1) {
        ret = openvz_readline(fd, line, sizeof(line));
        if(ret == -1)
            return -1;

        if(ret == 0) { /* EoF, UUID was not found */
673
            uuidstr[0] = 0;
674 675 676
            break;
        }

677
        sscanf(line, "%s %s\n", iden, uuidbuf);
678
        if(STREQ(iden, "#UUID:")) {
679
            strncpy(uuidstr, uuidbuf, VIR_UUID_STRING_BUFLEN);
680 681 682
            break;
        }
    }
683 684
    close(fd);

685 686 687 688 689 690
    return 0;
}

/* Do actual checking for UUID presence in conf file,
 * assign if not present.
 */
691 692
int
openvzSetDefinedUUID(int vpsid, unsigned char *uuid)
693 694
{
    char conf_file[PATH_MAX];
695
    char uuidstr[VIR_UUID_STRING_BUFLEN];
696 697 698

    if (uuid == NULL)
        return -1;
699

700
    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
701
       return -1;
702

J
Jim Meyering 已提交
703
    if (openvzGetVPSUUID(vpsid, uuidstr))
704 705
        return -1;

J
Jim Meyering 已提交
706
    if (uuidstr[0] == 0) {
707 708 709
        FILE *fp = fopen(conf_file, "a"); /* append */
        if (fp == NULL)
          return -1;
710

711 712
        virUUIDFormat(uuid, uuidstr);

713 714 715 716 717
        /* Record failure if fprintf or fclose fails,
           and be careful always to close the stream.  */
        if ((fprintf(fp, "\n#UUID: %s\n", uuidstr) < 0)
            + (fclose(fp) == EOF))
            return -1;
718 719 720 721 722
    }

    return 0;
}

723 724 725 726 727 728 729 730 731
static int
openvzSetUUID(int vpsid){
    unsigned char uuid[VIR_UUID_BUFLEN];

    virUUIDGenerate(uuid);

    return openvzSetDefinedUUID(vpsid, uuid);
}

732 733 734 735 736 737 738
/*
 * Scan VPS config files and see if they have a UUID.
 * If not, assign one. Just append one to the config
 * file as comment so that the OpenVZ tools ignore it.
 *
 */

739
static int openvzAssignUUIDs(void)
740 741 742 743 744 745 746 747
{
    DIR *dp;
    struct dirent *dent;
    char *conf_dir;
    int vpsid, res;
    char ext[8];

    conf_dir = openvzLocateConfDir();
748 749
    if (conf_dir == NULL)
        return -1;
750 751 752

    dp = opendir(conf_dir);
    if(dp == NULL) {
753
        VIR_FREE(conf_dir);
754 755 756 757 758
        return 0;
    }

    while((dent = readdir(dp))) {
        res = sscanf(dent->d_name, "%d.%5s", &vpsid, ext);
759
        if(!(res == 2 && STREQ(ext, "conf")))
760 761 762 763 764
            continue;
        if(vpsid > 0) /* '0.conf' belongs to the host, ignore it */
            openvzSetUUID(vpsid);
    }
    closedir(dp);
765
    VIR_FREE(conf_dir);
766 767
    return 0;
}