phyp_driver.c 105.8 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2015 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright IBM Corp. 2009
 *
 * phyp_driver.c: ssh layer to access Power Hypervisors
 *
 * 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 22 23 24
 */

#include <config.h>

#include <sys/types.h>
25
#include <sys/stat.h>
26 27
#include <stdarg.h>
#include <unistd.h>
28 29 30 31 32
#include <libssh2.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
33 34
#include <fcntl.h>
#include <domain_event.h>
35
#include <poll.h>
36 37

#include "internal.h"
38
#include "virauth.h"
39
#include "datatypes.h"
40
#include "virbuffer.h"
41
#include "viralloc.h"
42
#include "virlog.h"
43
#include "driver.h"
44
#include "virerror.h"
45
#include "viruuid.h"
46
#include "domain_conf.h"
47
#include "storage_conf.h"
E
Eric Blake 已提交
48
#include "virfile.h"
E
Eduardo Otubo 已提交
49
#include "interface_conf.h"
50
#include "phyp_driver.h"
51
#include "virstring.h"
52 53 54

#define VIR_FROM_THIS VIR_FROM_PHYP

55 56
VIR_LOG_INIT("phyp.phyp_driver");

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#define LPAR_EXEC_ERR (-1)
#define SSH_CONN_ERR (-2)         /* error while trying to connect to remote host */
#define SSH_CMD_ERR (-3)          /* error while trying to execute the remote cmd */

/* This is the lpar (domain) struct that relates
 * the ID with UUID generated by the API
 * */
typedef struct _lpar lpar_t;
typedef lpar_t *lparPtr;
struct _lpar {
    unsigned char uuid[VIR_UUID_BUFLEN];
    int id;
};

/* Struct that holds how many lpars (domains) we're
 * handling and a pointer to an array of lpar structs
 * */
typedef struct _uuid_table uuid_table_t;
typedef uuid_table_t *uuid_tablePtr;
struct _uuid_table {
    size_t nlpars;
    lparPtr *lpars;
};

/* This is the main structure of the driver
 * */
typedef struct _phyp_driver phyp_driver_t;
typedef phyp_driver_t *phyp_driverPtr;
struct _phyp_driver {
86 87 88
    LIBSSH2_SESSION *session;
    int sock;

89 90 91 92 93 94 95 96 97 98 99 100
    uuid_tablePtr uuid_table;
    virCapsPtr caps;
    virDomainXMLOptionPtr xmlopt;
    int vios_id;

    /* system_type:
     * 0 = hmc
     * 127 = ivm
     * */
    int system_type;
    char *managed_system;
};
101

102 103 104 105
/*
 * URI: phyp://user@[hmc|ivm]/managed_system
 * */

106 107 108 109 110
enum {
    HMC = 0,
    PHYP_IFACENAME_SIZE = 24,
    PHYP_MAC_SIZE = 12,
};
111

112 113 114
static int
waitsocket(int socket_fd, LIBSSH2_SESSION * session)
{
115
    struct pollfd fds[1];
116
    int dir;
117

118 119
    memset(fds, 0, sizeof(fds));
    fds[0].fd = socket_fd;
120

121 122
    /* now make sure we wait in the correct direction */
    dir = libssh2_session_block_directions(session);
123

124
    if (dir & LIBSSH2_SESSION_BLOCK_INBOUND)
125
        fds[0].events |= POLLIN;
126

127
    if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
128
        fds[0].events |= POLLOUT;
129

130
    return poll(fds, ARRAY_CARDINALITY(fds), -1);
131
}
132

133 134
/* this function is the layer that manipulates the ssh channel itself
 * and executes the commands on the remote machine */
135 136 137
static char *phypExec(LIBSSH2_SESSION *, const char *, int *, virConnectPtr)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
    ATTRIBUTE_NONNULL(4);
138
static char *
139
phypExec(LIBSSH2_SESSION *session, const char *cmd, int *exit_status,
140 141
         virConnectPtr conn)
{
142
    phyp_driverPtr phyp_driver = conn->privateData;
143 144
    LIBSSH2_CHANNEL *channel;
    virBuffer tex_ret = VIR_BUFFER_INITIALIZER;
145 146
    char *buffer = NULL;
    size_t buffer_size = 16384;
147 148
    int exitcode;
    int bytecount = 0;
149
    int sock = phyp_driver->sock;
150
    int rc = 0;
151

152
    if (VIR_ALLOC_N(buffer, buffer_size) < 0)
153 154
        return NULL;

155 156 157 158
    /* Exec non-blocking on the remove host */
    while ((channel = libssh2_channel_open_session(session)) == NULL &&
           libssh2_session_last_error(session, NULL, NULL, 0) ==
           LIBSSH2_ERROR_EAGAIN) {
159 160 161 162 163
        if (waitsocket(sock, session) < 0 && errno != EINTR) {
            virReportSystemError(errno, "%s",
                                 _("unable to wait on libssh2 socket"));
            goto err;
        }
164 165
    }

166
    if (channel == NULL)
167
        goto err;
168

169 170
    while ((rc = libssh2_channel_exec(channel, cmd)) ==
           LIBSSH2_ERROR_EAGAIN) {
171 172 173 174 175
        if (waitsocket(sock, session) < 0 && errno != EINTR) {
            virReportSystemError(errno, "%s",
                                 _("unable to wait on libssh2 socket"));
            goto err;
        }
176
    }
177

178
    if (rc != 0)
179
        goto err;
180

181 182 183
    for (;;) {
        /* loop until we block */
        do {
184
            rc = libssh2_channel_read(channel, buffer, buffer_size);
185 186
            if (rc > 0) {
                bytecount += rc;
187
                virBufferAdd(&tex_ret, buffer, -1);
188 189 190
            }
        }
        while (rc > 0);
191

192 193 194
        /* this is due to blocking that would occur otherwise so we loop on
         * this condition */
        if (rc == LIBSSH2_ERROR_EAGAIN) {
195 196 197 198 199
            if (waitsocket(sock, session) < 0 && errno != EINTR) {
                virReportSystemError(errno, "%s",
                                     _("unable to wait on libssh2 socket"));
                goto err;
            }
200 201 202
        } else {
            break;
        }
E
Eduardo Otubo 已提交
203 204
    }

205
    exitcode = 127;
206

207
    while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) {
208 209 210 211 212
        if (waitsocket(sock, session) < 0 && errno != EINTR) {
            virReportSystemError(errno, "%s",
                                 _("unable to wait on libssh2 socket"));
            goto err;
        }
213 214
    }

215
    if (rc == 0)
216
        exitcode = libssh2_channel_get_exit_status(channel);
217

218 219 220
    (*exit_status) = exitcode;
    libssh2_channel_free(channel);
    channel = NULL;
221 222
    VIR_FREE(buffer);

223
    if (virBufferCheckError(&tex_ret) < 0)
224 225
        return NULL;
    return virBufferContentAndReset(&tex_ret);
226

227
 err:
228 229 230 231
    (*exit_status) = SSH_CMD_ERR;
    virBufferFreeAndReset(&tex_ret);
    VIR_FREE(buffer);
    return NULL;
232 233
}

234 235 236 237 238 239 240 241 242 243 244
/* Convenience wrapper function */
static char *phypExecBuffer(LIBSSH2_SESSION *, virBufferPtr buf, int *,
                            virConnectPtr, bool) ATTRIBUTE_NONNULL(1)
    ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
static char *
phypExecBuffer(LIBSSH2_SESSION *session, virBufferPtr buf, int *exit_status,
               virConnectPtr conn, bool strip_newline)
{
    char *cmd;
    char *ret;

245
    if (virBufferCheckError(buf) < 0)
246 247 248 249 250 251 252 253 254 255 256 257
        return NULL;
    cmd = virBufferContentAndReset(buf);
    ret = phypExec(session, cmd, exit_status, conn);
    VIR_FREE(cmd);
    if (ret && *exit_status == 0 && strip_newline) {
        char *nl = strchr(ret, '\n');
        if (nl)
            *nl = '\0';
    }
    return ret;
}

E
Eric Blake 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
/* Convenience wrapper function */
static int phypExecInt(LIBSSH2_SESSION *, virBufferPtr, virConnectPtr, int *)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
static int
phypExecInt(LIBSSH2_SESSION *session, virBufferPtr buf, virConnectPtr conn,
            int *result)
{
    char *str;
    int ret;
    char *char_ptr;

    str = phypExecBuffer(session, buf, &ret, conn, true);
    if (!str || ret) {
        VIR_FREE(str);
        return -1;
    }
    ret = virStrToLong_i(str, &char_ptr, 10, result);
    if (ret == 0 && *char_ptr)
        VIR_WARN("ignoring suffix during integer parsing of '%s'", str);
    VIR_FREE(str);
    return ret;
}

281
static int
282
phypGetSystemType(virConnectPtr conn)
283
{
284 285
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = phyp_driver->session;
286 287
    char *ret = NULL;
    int exit_status = 0;
288

289
    ret = phypExec(session, "lshmc -V", &exit_status, conn);
290

291 292
    VIR_FREE(ret);
    return exit_status;
293 294
}

295
static int
296
phypGetVIOSPartitionID(virConnectPtr conn)
297
{
298
    phyp_driverPtr phyp_driver = conn->privateData;
299
    LIBSSH2_SESSION *session = phyp_driver->session;
300 301 302 303
    int system_type = phyp_driver->system_type;
    int id = -1;
    char *managed_system = phyp_driver->managed_system;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
304

305 306
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
307
        virBufferAsprintf(&buf, " -m %s", managed_system);
E
Eric Blake 已提交
308 309
    virBufferAddLit(&buf, " -r lpar -F lpar_id,lpar_env"
                    "|sed -n '/vioserver/ {\n s/,.*$//\n p\n}'");
E
Eric Blake 已提交
310
    phypExecInt(session, &buf, conn, &id);
311
    return id;
312
}
313

314

315 316 317 318 319
static virCapsPtr
phypCapsInit(void)
{
    virCapsPtr caps;
    virCapsGuestPtr guest;
320

321
    if ((caps = virCapabilitiesNew(virArchFromHost(),
322
                                   false, false)) == NULL)
323
        goto no_memory;
324

325
    /* Some machines have problematic NUMA topology causing
326 327 328
     * unexpected failures. We don't want to break the QEMU
     * driver in this scenario, so log errors & carry on
     */
M
Martin Kletzander 已提交
329
    if (virCapabilitiesInitNUMA(caps) < 0) {
330
        virCapabilitiesFreeNUMAInfo(caps);
331
        VIR_WARN
332
            ("Failed to query host NUMA topology, disabling NUMA capabilities");
333 334
    }

335 336 337
    if (virCapabilitiesInitCaches(caps) < 0)
        VIR_WARN("Failed to get host CPU cache info");

338
    if ((guest = virCapabilitiesAddGuest(caps,
339
                                         VIR_DOMAIN_OSTYPE_LINUX,
340
                                         caps->host.arch,
341 342
                                         NULL, NULL, 0, NULL)) == NULL)
        goto no_memory;
343

344 345
    if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_PHYP,
                                      NULL, NULL, 0, NULL) == NULL)
346
        goto no_memory;
347

348
    return caps;
349

350
 no_memory:
351
    virObjectUnref(caps);
352 353
    return NULL;
}
354

355 356 357 358 359 360 361 362 363
/* This is a generic function that won't be used directly by
 * libvirt api. The function returns the number of domains
 * in different states: Running, Not Activated and all:
 *
 * type: 0 - Running
 *       1 - Not Activated
 *       * - All
 * */
static int
364
phypConnectNumOfDomainsGeneric(virConnectPtr conn, unsigned int type)
365 366
{
    phyp_driverPtr phyp_driver = conn->privateData;
367
    LIBSSH2_SESSION *session = phyp_driver->session;
368
    int system_type = phyp_driver->system_type;
369
    int ndom = -1;
370 371 372
    char *managed_system = phyp_driver->managed_system;
    const char *state;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
373

374
    if (type == 0) {
375
        state = "|grep Running";
376
    } else if (type == 1) {
377 378 379 380
        if (system_type == HMC) {
            state = "|grep \"Not Activated\"";
        } else {
            state = "|grep \"Open Firmware\"";
381
        }
382
    } else {
383
        state = " ";
384
    }
385

386 387
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
388 389
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F lpar_id,state %s |grep -c '^[0-9][0-9]*'",
390
                      state);
E
Eric Blake 已提交
391
    phypExecInt(session, &buf, conn, &ndom);
392
    return ndom;
393 394
}

395 396 397 398 399 400 401 402
/* This is a generic function that won't be used directly by
 * libvirt api. The function returns the ids of domains
 * in different states: Running, and all:
 *
 * type: 0 - Running
 *       1 - all
 * */
static int
403 404
phypConnectListDomainsGeneric(virConnectPtr conn, int *ids, int nids,
                              unsigned int type)
405
{
E
Eduardo Otubo 已提交
406
    phyp_driverPtr phyp_driver = conn->privateData;
407
    LIBSSH2_SESSION *session = phyp_driver->session;
E
Eduardo Otubo 已提交
408
    int system_type = phyp_driver->system_type;
409
    char *managed_system = phyp_driver->managed_system;
410
    int exit_status = 0;
411
    int got = -1;
412
    char *ret = NULL;
413
    char *line, *next_line;
414
    const char *state;
E
Eduardo Otubo 已提交
415 416
    virBuffer buf = VIR_BUFFER_INITIALIZER;

417 418 419 420 421
    if (type == 0)
        state = "|grep Running";
    else
        state = " ";

E
Eduardo Otubo 已提交
422 423
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
424 425
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F lpar_id,state %s | sed -e 's/,.*$//'",
426
                      state);
427
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
428

429
    if (exit_status < 0 || ret == NULL)
430
        goto cleanup;
431 432 433 434 435 436 437 438

    /* I need to parse the textual return in order to get the ids */
    line = ret;
    got = 0;
    while (*line && got < nids) {
        if (virStrToLong_i(line, &next_line, 10, &ids[got]) == -1) {
            VIR_ERROR(_("Cannot parse number from '%s'"), line);
            got = -1;
439
            goto cleanup;
440
        }
441 442 443 444
        got++;
        line = next_line;
        while (*line == '\n')
            line++; /* skip \n */
445
    }
446

447
 cleanup:
448
    VIR_FREE(ret);
449
    return got;
450 451
}

452 453
static int
phypUUIDTable_WriteFile(virConnectPtr conn)
454
{
455 456
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
457
    size_t i = 0;
458 459 460 461 462
    int fd = -1;
    char local_file[] = "./uuid_table";

    if ((fd = creat(local_file, 0755)) == -1)
        goto err;
463

464
    for (i = 0; i < uuid_table->nlpars; i++) {
465 466 467
        if (safewrite(fd, &uuid_table->lpars[i]->id,
                      sizeof(uuid_table->lpars[i]->id)) !=
            sizeof(uuid_table->lpars[i]->id)) {
468
            VIR_ERROR(_("Unable to write information to local file."));
469 470 471 472 473
            goto err;
        }

        if (safewrite(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN) !=
            VIR_UUID_BUFLEN) {
474
            VIR_ERROR(_("Unable to write information to local file."));
475
            goto err;
476 477 478
        }
    }

479 480 481 482 483
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno, _("Could not close %s"),
                             local_file);
        goto err;
    }
484 485
    return 0;

486
 err:
487
    VIR_FORCE_CLOSE(fd);
488 489 490
    return -1;
}

491 492
static int
phypUUIDTable_Push(virConnectPtr conn)
493
{
494 495
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = phyp_driver->session;
496 497 498 499
    LIBSSH2_CHANNEL *channel = NULL;
    struct stat local_fileinfo;
    char buffer[1024];
    int rc = 0;
500
    FILE *f = NULL;
501 502 503 504
    size_t nread, sent;
    char *ptr;
    char local_file[] = "./uuid_table";
    char *remote_file = NULL;
505
    int ret = -1;
506

507
    if (virAsprintf(&remote_file, "/home/%s/libvirt_uuid_table",
508
                    NULLSTR(conn->uri->user)) < 0)
509
        goto cleanup;
510

511
    if (stat(local_file, &local_fileinfo) == -1) {
512
        VIR_WARN("Unable to stat local file.");
513
        goto cleanup;
514
    }
515

516
    if (!(f = fopen(local_file, "rb"))) {
517
        VIR_WARN("Unable to open local file.");
518
        goto cleanup;
519
    }
520

521 522 523 524
    do {
        channel =
            libssh2_scp_send(session, remote_file,
                             0x1FF & local_fileinfo.st_mode,
525
                             (unsigned long)local_fileinfo.st_size);
526

527 528
        if ((!channel) && (libssh2_session_last_errno(session) !=
                           LIBSSH2_ERROR_EAGAIN))
529
            goto cleanup;
530
    } while (!channel);
531

532
    do {
533
        nread = fread(buffer, 1, sizeof(buffer), f);
534
        if (nread <= 0) {
535
            if (feof(f)) {
536 537 538 539
                /* end of file */
                break;
            } else {
                VIR_ERROR(_("Failed to read from %s"), local_file);
540
                goto cleanup;
541 542 543 544
            }
        }
        ptr = buffer;
        sent = 0;
545

546 547 548 549 550 551 552 553 554 555 556 557 558
        do {
            /* write the same data over and over, until error or completion */
            rc = libssh2_channel_write(channel, ptr, nread);
            if (LIBSSH2_ERROR_EAGAIN == rc) {   /* must loop around */
                continue;
            } else if (rc > 0) {
                /* rc indicates how many bytes were written this time */
                sent += rc;
            }
            ptr += sent;
            nread -= sent;
        } while (rc > 0 && sent < nread);
    } while (1);
559

560
    ret = 0;
561

562
 cleanup:
563
    VIR_FREE(remote_file);
564 565 566 567 568 569 570
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
571 572
    VIR_FORCE_FCLOSE(f);
    return ret;
573 574 575
}

static int
576
phypUUIDTable_RemLpar(virConnectPtr conn, int id)
577
{
E
Eduardo Otubo 已提交
578
    phyp_driverPtr phyp_driver = conn->privateData;
579
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
580
    size_t i = 0;
E
Eduardo Otubo 已提交
581

582 583 584 585 586
    for (i = 0; i <= uuid_table->nlpars; i++) {
        if (uuid_table->lpars[i]->id == id) {
            uuid_table->lpars[i]->id = -1;
            memset(uuid_table->lpars[i]->uuid, 0, VIR_UUID_BUFLEN);
        }
587 588
    }

589
    if (phypUUIDTable_WriteFile(conn) == -1)
590 591
        goto err;

592
    if (phypUUIDTable_Push(conn) == -1)
593 594
        goto err;

595
    return 0;
596

597
 err:
598
    return -1;
599 600
}

601 602
static int
phypUUIDTable_AddLpar(virConnectPtr conn, unsigned char *uuid, int id)
603
{
E
Eduardo Otubo 已提交
604
    phyp_driverPtr phyp_driver = conn->privateData;
605
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
606
    lparPtr item = NULL;
E
Eduardo Otubo 已提交
607

608
    if (VIR_ALLOC(item) < 0)
609
        goto err;
610

611 612
    item->id = id;
    memcpy(item->uuid, uuid, VIR_UUID_BUFLEN);
613

614 615
    if (VIR_APPEND_ELEMENT_COPY(uuid_table->lpars, uuid_table->nlpars, item) < 0)
        goto err;
616

617 618
    if (phypUUIDTable_WriteFile(conn) == -1)
        goto err;
619

620
    if (phypUUIDTable_Push(conn) == -1)
621 622
        goto err;

623
    return 0;
624

625
 err:
626
    VIR_FREE(item);
627
    return -1;
628 629
}

630 631
static int
phypUUIDTable_ReadFile(virConnectPtr conn)
632
{
E
Eduardo Otubo 已提交
633
    phyp_driverPtr phyp_driver = conn->privateData;
634
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
635
    size_t i = 0;
636 637 638 639
    int fd = -1;
    char local_file[] = "./uuid_table";
    int rc = 0;
    int id;
640

641
    if ((fd = open(local_file, O_RDONLY)) == -1) {
642
        VIR_WARN("Unable to read information from local file.");
643
        goto err;
644 645
    }

646 647 648
    /* Creating a new data base and writing to local file */
    if (VIR_ALLOC_N(uuid_table->lpars, uuid_table->nlpars) >= 0) {
        for (i = 0; i < uuid_table->nlpars; i++) {
649

650 651
            rc = read(fd, &id, sizeof(int));
            if (rc == sizeof(int)) {
652
                if (VIR_ALLOC(uuid_table->lpars[i]) < 0)
653 654 655
                    goto err;
                uuid_table->lpars[i]->id = id;
            } else {
656
                VIR_WARN
657
                    ("Unable to read from information from local file.");
658 659
                goto err;
            }
660

661 662
            rc = read(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN);
            if (rc != VIR_UUID_BUFLEN) {
663
                VIR_WARN("Unable to read information from local file.");
664 665
                goto err;
            }
666
        }
667
    }
668

669
    VIR_FORCE_CLOSE(fd);
670
    return 0;
671

672
 err:
673
    VIR_FORCE_CLOSE(fd);
674
    return -1;
675 676
}

677 678
static int
phypUUIDTable_Pull(virConnectPtr conn)
679
{
680 681
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = phyp_driver->session;
682 683 684 685
    LIBSSH2_CHANNEL *channel = NULL;
    struct stat fileinfo;
    char buffer[1024];
    int rc = 0;
686
    int fd = -1;
687 688 689 690 691 692
    int got = 0;
    int amount = 0;
    int total = 0;
    int sock = 0;
    char local_file[] = "./uuid_table";
    char *remote_file = NULL;
693
    int ret = -1;
E
Eduardo Otubo 已提交
694

695
    if (virAsprintf(&remote_file, "/home/%s/libvirt_uuid_table",
696
                    NULLSTR(conn->uri->user)) < 0)
697
        goto cleanup;
698

699 700 701
    /* Trying to stat the remote file. */
    do {
        channel = libssh2_scp_recv(session, remote_file, &fileinfo);
702

703 704 705
        if (!channel) {
            if (libssh2_session_last_errno(session) !=
                LIBSSH2_ERROR_EAGAIN) {
706
                goto cleanup;
707
            } else {
708 709 710
                if (waitsocket(sock, session) < 0 && errno != EINTR) {
                    virReportSystemError(errno, "%s",
                                         _("unable to wait on libssh2 socket"));
711
                    goto cleanup;
712
                }
713 714 715
            }
        }
    } while (!channel);
716

717 718
    /* Creating a new data base based on remote file */
    if ((fd = creat(local_file, 0755)) == -1)
719
        goto cleanup;
720

721 722 723 724
    /* Request a file via SCP */
    while (got < fileinfo.st_size) {
        do {
            amount = sizeof(buffer);
725

726
            if ((fileinfo.st_size - got) < amount)
727
                amount = fileinfo.st_size - got;
E
Eduardo Otubo 已提交
728

729 730 731
            rc = libssh2_channel_read(channel, buffer, amount);
            if (rc > 0) {
                if (safewrite(fd, buffer, rc) != rc)
732
                    VIR_WARN
733
                        ("Unable to write information to local file.");
734

735 736 737 738
                got += rc;
                total += rc;
            }
        } while (rc > 0);
739

740 741 742 743
        if ((rc == LIBSSH2_ERROR_EAGAIN)
            && (got < fileinfo.st_size)) {
            /* this is due to blocking that would occur otherwise
             * so we loop on this condition */
744

745 746 747 748
            /* now we wait */
            if (waitsocket(sock, session) < 0 && errno != EINTR) {
                virReportSystemError(errno, "%s",
                                     _("unable to wait on libssh2 socket"));
749
                goto cleanup;
750
            }
751 752 753 754
            continue;
        }
        break;
    }
755 756 757
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno, _("Could not close %s"),
                             local_file);
758
        goto cleanup;
759
    }
760

761
    ret = 0;
762

763
 cleanup:
764 765 766 767 768 769 770
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
771 772
    VIR_FORCE_CLOSE(fd);
    return ret;
773 774
}

775 776
static int
phypUUIDTable_Init(virConnectPtr conn)
777
{
E
Eric Blake 已提交
778
    uuid_tablePtr uuid_table = NULL;
779 780 781 782
    phyp_driverPtr phyp_driver;
    int nids_numdomains = 0;
    int nids_listdomains = 0;
    int *ids = NULL;
783
    size_t i = 0;
E
Eric Blake 已提交
784 785
    int ret = -1;
    bool table_created = false;
E
Eduardo Otubo 已提交
786

787
    if ((nids_numdomains = phypConnectNumOfDomainsGeneric(conn, 2)) < 0)
E
Eric Blake 已提交
788
        goto cleanup;
789

790
    if (VIR_ALLOC_N(ids, nids_numdomains) < 0)
E
Eric Blake 已提交
791
        goto cleanup;
792

793
    if ((nids_listdomains =
794
         phypConnectListDomainsGeneric(conn, ids, nids_numdomains, 1)) < 0)
E
Eric Blake 已提交
795
        goto cleanup;
796

797
    /* exit early if there are no domains */
E
Eric Blake 已提交
798 799 800 801 802
    if (nids_numdomains == 0 && nids_listdomains == 0) {
        ret = 0;
        goto cleanup;
    }
    if (nids_numdomains != nids_listdomains) {
803
        VIR_ERROR(_("Unable to determine number of domains."));
E
Eric Blake 已提交
804
        goto cleanup;
805
    }
806

807 808 809
    phyp_driver = conn->privateData;
    uuid_table = phyp_driver->uuid_table;
    uuid_table->nlpars = nids_listdomains;
810

811 812 813
    /* try to get the table from server */
    if (phypUUIDTable_Pull(conn) == -1) {
        /* file not found in the server, creating a new one */
E
Eric Blake 已提交
814
        table_created = true;
815 816
        if (VIR_ALLOC_N(uuid_table->lpars, uuid_table->nlpars) >= 0) {
            for (i = 0; i < uuid_table->nlpars; i++) {
817
                if (VIR_ALLOC(uuid_table->lpars[i]) < 0)
E
Eric Blake 已提交
818
                    goto cleanup;
819
                uuid_table->lpars[i]->id = ids[i];
820

821 822 823 824
                if (virUUIDGenerate(uuid_table->lpars[i]->uuid) < 0)
                    VIR_WARN("Unable to generate UUID for domain %d",
                             ids[i]);
            }
825
        } else {
E
Eric Blake 已提交
826
            goto cleanup;
827
        }
828

829
        if (phypUUIDTable_WriteFile(conn) == -1)
E
Eric Blake 已提交
830
            goto cleanup;
831

832
        if (phypUUIDTable_Push(conn) == -1)
E
Eric Blake 已提交
833
            goto cleanup;
834 835
    } else {
        if (phypUUIDTable_ReadFile(conn) == -1)
E
Eric Blake 已提交
836
            goto cleanup;
837
    }
838

E
Eric Blake 已提交
839
    ret = 0;
840

841
 cleanup:
E
Eric Blake 已提交
842
    if (ret < 0 && table_created) {
843
        for (i = 0; i < uuid_table->nlpars; i++)
E
Eric Blake 已提交
844 845 846
            VIR_FREE(uuid_table->lpars[i]);
        VIR_FREE(uuid_table->lpars);
    }
847
    VIR_FREE(ids);
E
Eric Blake 已提交
848
    return ret;
849 850
}

851 852
static void
phypUUIDTable_Free(uuid_tablePtr uuid_table)
853
{
854
    size_t i;
855

856 857 858 859 860 861 862 863
    if (uuid_table == NULL)
        return;

    for (i = 0; i < uuid_table->nlpars; i++)
        VIR_FREE(uuid_table->lpars[i]);

    VIR_FREE(uuid_table->lpars);
    VIR_FREE(uuid_table);
864 865
}

866 867 868 869
#define SPECIALCHARACTER_CASES \
    case '&': case ';': case '`': case '@': case '"': case '|': case '*': \
    case '?': case '~': case '<': case '>': case '^': case '(': case ')': \
    case '[': case ']': case '{': case '}': case '$': case '%': case '#': \
870 871 872 873
    case '\\': case '\n': case '\r': case '\t':

static bool
contains_specialcharacters(const char *src)
874
{
875
    size_t len = strlen(src);
876 877
    size_t i = 0;

878
    if (len == 0)
879
        return false;
880

881 882
    for (i = 0; i < len; i++) {
        switch (src[i]) {
883 884 885 886
        SPECIALCHARACTER_CASES
            return true;
        default:
            continue;
887 888 889
        }
    }

890 891
    return false;
}
892

893 894 895 896 897 898 899 900 901 902
static char *
escape_specialcharacters(const char *src)
{
    size_t len = strlen(src);
    size_t i = 0, j = 0;
    char *dst;

    if (len == 0)
        return NULL;

903
    if (VIR_ALLOC_N(dst, len + 1) < 0)
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
        return NULL;

    for (i = 0; i < len; i++) {
        switch (src[i]) {
        SPECIALCHARACTER_CASES
            continue;
        default:
            dst[j] = src[i];
            j++;
        }
    }

    dst[j] = '\0';

    return dst;
919 920
}

921 922 923
static LIBSSH2_SESSION *
openSSHSession(virConnectPtr conn, virConnectAuthPtr auth,
               int *internal_socket)
924
{
925 926 927 928
    LIBSSH2_SESSION *session;
    const char *hostname = conn->uri->server;
    char *username = NULL;
    char *password = NULL;
929
    int sock = -1;
930 931 932 933 934 935
    int rc;
    struct addrinfo *ai = NULL, *cur;
    struct addrinfo hints;
    int ret;
    char *pubkey = NULL;
    char *pvtkey = NULL;
936
    char *userhome = virGetUserDirectory();
937
    struct stat pvt_stat, pub_stat;
938

939 940
    if (userhome == NULL)
        goto err;
E
Eduardo Otubo 已提交
941

942
    if (virAsprintf(&pubkey, "%s/.ssh/id_rsa.pub", userhome) < 0)
943
        goto err;
944

945
    if (virAsprintf(&pvtkey, "%s/.ssh/id_rsa", userhome) < 0)
946 947
        goto err;

948
    if (conn->uri->user != NULL) {
949
        if (VIR_STRDUP(username, conn->uri->user) < 0)
950 951
            goto err;
    } else {
952 953
        if (!(username = virAuthGetUsername(conn, auth, "ssh", NULL,
                                            conn->uri->server)))
954 955
            goto err;
    }
956

957 958 959 960
    memset(&hints, 0, sizeof(hints));
    hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
961

962 963
    ret = getaddrinfo(hostname, "22", &hints, &ai);
    if (ret != 0) {
964 965
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Error while getting %s address info"), hostname);
966 967
        goto err;
    }
968

969 970 971 972 973
    cur = ai;
    while (cur != NULL) {
        sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
        if (sock >= 0) {
            if (connect(sock, cur->ai_addr, cur->ai_addrlen) == 0) {
974
                freeaddrinfo(ai);
975 976
                goto connected;
            }
977
            VIR_FORCE_CLOSE(sock);
978 979 980
        }
        cur = cur->ai_next;
    }
981

982 983
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Failed to connect to %s"), hostname);
984 985
    freeaddrinfo(ai);
    goto err;
986

987
 connected:
988

989
    (*internal_socket) = sock;
990

991 992 993
    /* Create a session instance */
    session = libssh2_session_init();
    if (!session)
994 995
        goto err;

996 997
    /* tell libssh2 we want it all done non-blocking */
    libssh2_session_set_blocking(session, 0);
998

999
    while ((rc = libssh2_session_startup(session, sock)) ==
1000
           LIBSSH2_ERROR_EAGAIN);
1001
    if (rc) {
1002 1003
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Failure establishing SSH session."));
1004 1005
        goto disconnect;
    }
1006

1007 1008 1009 1010 1011
    /* Trying authentication by pubkey */
    if (stat(pvtkey, &pvt_stat) || stat(pubkey, &pub_stat)) {
        rc = LIBSSH2_ERROR_SOCKET_NONE;
        goto keyboard_interactive;
    }
1012

1013 1014 1015 1016 1017
    while ((rc =
            libssh2_userauth_publickey_fromfile(session, username,
                                                pubkey,
                                                pvtkey,
                                                NULL)) ==
1018
           LIBSSH2_ERROR_EAGAIN);
1019

1020
 keyboard_interactive:
1021 1022 1023
    if (rc == LIBSSH2_ERROR_SOCKET_NONE
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED) {
1024

1025 1026
        if (!(password = virAuthGetPassword(conn, auth, "ssh", username,
                                            conn->uri->server)))
1027
            goto disconnect;
1028

1029 1030 1031
        while ((rc =
                libssh2_userauth_password(session, username,
                                          password)) ==
1032
               LIBSSH2_ERROR_EAGAIN);
1033

1034
        if (rc) {
1035 1036
            virReportError(VIR_ERR_AUTH_FAILED,
                           "%s", _("Authentication failed"));
1037
            goto disconnect;
1038
        } else {
1039
            goto exit;
1040
        }
1041

1042 1043
    } else if (rc == LIBSSH2_ERROR_NONE) {
        goto exit;
1044

1045 1046
    } else if (rc == LIBSSH2_ERROR_ALLOC || rc == LIBSSH2_ERROR_SOCKET_SEND
               || rc == LIBSSH2_ERROR_SOCKET_TIMEOUT) {
1047 1048 1049
        goto err;
    }

1050
 disconnect:
1051 1052
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1053
 err:
1054
    VIR_FORCE_CLOSE(sock);
1055 1056 1057 1058 1059
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
1060
    return NULL;
1061

1062
 exit:
1063 1064 1065 1066 1067 1068
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
    return session;
1069 1070
}

1071 1072

static int
1073
phypDomainDefPostParse(virDomainDefPtr def ATTRIBUTE_UNUSED,
1074
                       virCapsPtr caps ATTRIBUTE_UNUSED,
1075
                       unsigned int parseFlags ATTRIBUTE_UNUSED,
1076 1077
                       void *opaque ATTRIBUTE_UNUSED,
                       void *parseOpaque ATTRIBUTE_UNUSED)
1078 1079 1080 1081 1082 1083 1084 1085 1086
{
    return 0;
}


static int
phypDomainDeviceDefPostParse(virDomainDeviceDefPtr dev ATTRIBUTE_UNUSED,
                             const virDomainDef *def ATTRIBUTE_UNUSED,
                             virCapsPtr caps ATTRIBUTE_UNUSED,
1087
                             unsigned int parseFlags ATTRIBUTE_UNUSED,
1088 1089
                             void *opaque ATTRIBUTE_UNUSED,
                             void *parseOpaque ATTRIBUTE_UNUSED)
1090 1091 1092 1093 1094 1095 1096 1097
{
    return 0;
}


virDomainDefParserConfig virPhypDriverDomainDefParserConfig = {
    .devicesPostParseCallback = phypDomainDeviceDefPostParse,
    .domainPostParseCallback = phypDomainDefPostParse,
1098
    .features = VIR_DOMAIN_DEF_FEATURE_NAME_SLASH,
1099 1100 1101
};


1102
static virDrvOpenStatus
1103
phypConnectOpen(virConnectPtr conn,
1104 1105 1106
                virConnectAuthPtr auth,
                virConfPtr conf ATTRIBUTE_UNUSED,
                unsigned int flags)
1107 1108
{
    LIBSSH2_SESSION *session = NULL;
1109
    int internal_socket = -1;
1110 1111 1112 1113
    uuid_tablePtr uuid_table = NULL;
    phyp_driverPtr phyp_driver = NULL;
    char *char_ptr;
    char *managed_system = NULL;
E
Eduardo Otubo 已提交
1114

E
Eric Blake 已提交
1115 1116
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1117
    if (VIR_ALLOC(phyp_driver) < 0)
1118
        goto failure;
1119

1120
    phyp_driver->sock = -1;
1121

1122
    if (VIR_ALLOC(uuid_table) < 0)
1123
        goto failure;
1124

1125
    if (conn->uri->path[0] != '\0') {
1126
        /* need to shift one byte in order to remove the first "/" of URI component */
1127 1128
        if (VIR_STRDUP(managed_system,
                       conn->uri->path + (conn->uri->path[0] == '/')) < 0)
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
            goto failure;

        /* here we are handling only the first component of the path,
         * so skipping the second:
         * */
        char_ptr = strchr(managed_system, '/');

        if (char_ptr)
            *char_ptr = '\0';

1139
        if (contains_specialcharacters(conn->uri->path)) {
1140 1141 1142
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s",
                           _("Error parsing 'path'. Invalid characters."));
1143 1144 1145 1146 1147
            goto failure;
        }
    }

    if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) {
1148 1149
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Error while opening SSH session."));
1150 1151 1152
        goto failure;
    }

1153 1154
    phyp_driver->session = session;
    phyp_driver->sock = internal_socket;
1155 1156 1157 1158 1159 1160 1161 1162

    uuid_table->nlpars = 0;
    uuid_table->lpars = NULL;

    if (conn->uri->path)
        phyp_driver->managed_system = managed_system;

    phyp_driver->uuid_table = uuid_table;
1163
    if ((phyp_driver->caps = phypCapsInit()) == NULL)
1164
        goto failure;
1165

1166
    if (!(phyp_driver->xmlopt = virDomainXMLOptionNew(&virPhypDriverDomainDefParserConfig,
1167
                                                      NULL, NULL, NULL, NULL)))
1168 1169
        goto failure;

1170
    conn->privateData = phyp_driver;
1171

1172 1173
    if ((phyp_driver->system_type = phypGetSystemType(conn)) == -1)
        goto failure;
1174

1175 1176
    if (phypUUIDTable_Init(conn) == -1)
        goto failure;
1177

1178 1179 1180 1181 1182 1183 1184
    if (phyp_driver->system_type == HMC) {
        if ((phyp_driver->vios_id = phypGetVIOSPartitionID(conn)) == -1)
            goto failure;
    }

    return VIR_DRV_OPEN_SUCCESS;

1185
 failure:
1186 1187
    VIR_FREE(managed_system);

1188
    if (phyp_driver != NULL) {
1189
        virObjectUnref(phyp_driver->caps);
1190
        virObjectUnref(phyp_driver->xmlopt);
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
        VIR_FREE(phyp_driver);
    }

    phypUUIDTable_Free(uuid_table);

    if (session != NULL) {
        libssh2_session_disconnect(session, "Disconnecting...");
        libssh2_session_free(session);
    }

1201
    VIR_FORCE_CLOSE(internal_socket);
1202 1203

    return VIR_DRV_OPEN_ERROR;
1204 1205 1206
}

static int
1207
phypConnectClose(virConnectPtr conn)
1208
{
1209
    phyp_driverPtr phyp_driver = conn->privateData;
1210
    LIBSSH2_SESSION *session = phyp_driver->session;
1211

1212 1213
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1214

1215
    virObjectUnref(phyp_driver->caps);
1216
    virObjectUnref(phyp_driver->xmlopt);
1217 1218
    phypUUIDTable_Free(phyp_driver->uuid_table);
    VIR_FREE(phyp_driver->managed_system);
1219
    VIR_FORCE_CLOSE(phyp_driver->sock);
1220
    VIR_FREE(phyp_driver);
1221 1222
    return 0;
}
1223 1224


1225
static int
1226
phypConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1227 1228 1229 1230
{
    /* Phyp uses an SSH tunnel, so is always encrypted */
    return 1;
}
1231

1232 1233

static int
1234
phypConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1235 1236 1237
{
    /* Phyp uses an SSH tunnel, so is always secure */
    return 1;
1238 1239
}

1240 1241

static int
1242
phypConnectIsAlive(virConnectPtr conn)
1243
{
1244
    phyp_driverPtr phyp_driver = conn->privateData;
1245 1246 1247 1248
    /* XXX we should be able to do something better but this is simple, safe,
     * and good enough for now. In worst case, the function will return true
     * even though the connection is not alive.
     */
1249
    if (phyp_driver->session)
1250 1251 1252 1253 1254 1255
        return 1;
    else
        return 0;
}


1256
static int
1257
phypDomainIsUpdated(virDomainPtr conn ATTRIBUTE_UNUSED)
1258 1259 1260
{
    return 0;
}
1261 1262

/* return the lpar_id given a name and a managed system name */
1263
static int
1264 1265
phypGetLparID(LIBSSH2_SESSION * session, const char *managed_system,
              const char *name, virConnectPtr conn)
1266
{
1267
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1268
    int system_type = phyp_driver->system_type;
1269
    int lpar_id = -1;
E
Eduardo Otubo 已提交
1270 1271
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1272
    virBufferAddLit(&buf, "lssyscfg -r lpar");
E
Eduardo Otubo 已提交
1273
    if (system_type == HMC)
1274 1275
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --filter lpar_names=%s -F lpar_id", name);
E
Eric Blake 已提交
1276
    phypExecInt(session, &buf, conn, &lpar_id);
1277
    return lpar_id;
1278 1279
}

1280 1281 1282 1283
/* return the lpar name given a lpar_id and a managed system name */
static char *
phypGetLparNAME(LIBSSH2_SESSION * session, const char *managed_system,
                unsigned int lpar_id, virConnectPtr conn)
1284 1285
{
    phyp_driverPtr phyp_driver = conn->privateData;
1286 1287 1288 1289
    int system_type = phyp_driver->system_type;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1290

1291 1292
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
1293 1294
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --filter lpar_ids=%d -F name", lpar_id);
1295
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1296

1297
    if (exit_status < 0)
1298 1299
        VIR_FREE(ret);
    return ret;
1300 1301
}

1302 1303 1304 1305 1306 1307 1308 1309 1310

/* Search into the uuid_table for a lpar_uuid given a lpar_id
 * and a managed system name
 *
 * return:  0 - record found
 *         -1 - not found
 * */
static int
phypGetLparUUID(unsigned char *uuid, int lpar_id, virConnectPtr conn)
1311 1312
{
    phyp_driverPtr phyp_driver = conn->privateData;
1313 1314
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    lparPtr *lpars = uuid_table->lpars;
1315
    size_t i = 0;
1316

1317 1318
    for (i = 0; i < uuid_table->nlpars; i++) {
        if (lpars[i]->id == lpar_id) {
1319
            memcpy(uuid, lpars[i]->uuid, VIR_UUID_BUFLEN);
1320 1321 1322
            return 0;
        }
    }
1323

1324
    return -1;
1325 1326
}

1327 1328 1329 1330 1331 1332 1333 1334
/*
 * type:
 * 0 - maxmem
 * 1 - memory
 * */
static unsigned long
phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id,
               int type)
1335
{
1336
    phyp_driverPtr phyp_driver = conn->privateData;
1337
    LIBSSH2_SESSION *session = phyp_driver->session;
1338 1339 1340
    int system_type = phyp_driver->system_type;
    int memory = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1341

1342 1343
    if (type != 1 && type != 0)
        return 0;
1344

1345 1346
    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
1347 1348
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1349 1350
                      " -r mem --level lpar -F %s --filter lpar_ids=%d",
                      type ? "curr_mem" : "curr_max_mem", lpar_id);
E
Eric Blake 已提交
1351
    phypExecInt(session, &buf, conn, &memory);
1352
    return memory;
1353 1354
}

1355 1356 1357
static unsigned long
phypGetLparCPUGeneric(virConnectPtr conn, const char *managed_system,
                      int lpar_id, int type)
1358
{
1359
    phyp_driverPtr phyp_driver = conn->privateData;
1360
    LIBSSH2_SESSION *session = phyp_driver->session;
E
Eduardo Otubo 已提交
1361
    int system_type = phyp_driver->system_type;
1362
    int vcpus = 0;
E
Eduardo Otubo 已提交
1363
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1364

1365
    virBufferAddLit(&buf, "lshwres");
E
Eduardo Otubo 已提交
1366
    if (system_type == HMC)
1367 1368
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1369 1370
                      " -r proc --level lpar -F %s --filter lpar_ids=%d",
                      type ? "curr_max_procs" : "curr_procs", lpar_id);
E
Eric Blake 已提交
1371
    phypExecInt(session, &buf, conn, &vcpus);
1372
    return vcpus;
1373
}
1374

1375 1376 1377 1378
static unsigned long
phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id)
{
    return phypGetLparCPUGeneric(conn, managed_system, lpar_id, 0);
1379 1380
}

1381
static int
1382
phypDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
1383 1384 1385
{
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    char *managed_system = phyp_driver->managed_system;
1386

1387
    if (flags != (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_MAXIMUM)) {
1388
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
1389 1390 1391
        return -1;
    }

1392 1393 1394
    return phypGetLparCPUGeneric(dom->conn, managed_system, dom->id, 1);
}

1395
static int
1396
phypDomainGetMaxVcpus(virDomainPtr dom)
1397 1398 1399 1400 1401
{
    return phypDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                         VIR_DOMAIN_VCPU_MAXIMUM));
}

1402 1403 1404
static int
phypGetRemoteSlot(virConnectPtr conn, const char *managed_system,
                  const char *lpar_name)
1405
{
1406
    phyp_driverPtr phyp_driver = conn->privateData;
1407
    LIBSSH2_SESSION *session = phyp_driver->session;
E
Eduardo Otubo 已提交
1408
    int system_type = phyp_driver->system_type;
1409
    int remote_slot = -1;
E
Eduardo Otubo 已提交
1410 1411
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1412
    virBufferAddLit(&buf, "lshwres");
E
Eduardo Otubo 已提交
1413
    if (system_type == HMC)
1414 1415
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r virtualio --rsubtype scsi -F "
1416
                      "remote_slot_num --filter lpar_names=%s", lpar_name);
E
Eric Blake 已提交
1417
    phypExecInt(session, &buf, conn, &remote_slot);
1418
    return remote_slot;
1419 1420
}

1421 1422 1423 1424 1425 1426
/* XXX - is this needed? */
static char *phypGetBackingDevice(virConnectPtr, const char *, char *)
    ATTRIBUTE_UNUSED;
static char *
phypGetBackingDevice(virConnectPtr conn, const char *managed_system,
                     char *lpar_name)
1427 1428
{
    phyp_driverPtr phyp_driver = conn->privateData;
1429
    LIBSSH2_SESSION *session = phyp_driver->session;
1430 1431 1432 1433 1434 1435 1436
    int system_type = phyp_driver->system_type;
    char *ret = NULL;
    int remote_slot = 0;
    int exit_status = 0;
    char *char_ptr;
    char *backing_device = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1437

1438 1439 1440 1441 1442 1443
    if ((remote_slot =
         phypGetRemoteSlot(conn, managed_system, lpar_name)) == -1)
        return NULL;

    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
1444 1445
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r virtualio --rsubtype scsi -F "
1446
                      "backing_devices --filter slots=%d", remote_slot);
1447
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1448

1449
    if (exit_status < 0 || ret == NULL)
1450
        goto cleanup;
1451

1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
    /* here is a little trick to deal returns of this kind:
     *
     * 0x8100000000000000//lv01
     *
     * the information we really need is only lv01, so we
     * need to skip a lot of things on the string.
     * */
    char_ptr = strchr(ret, '/');

    if (char_ptr) {
        char_ptr++;
        if (char_ptr[0] == '/')
            char_ptr++;
        else
1466
            goto cleanup;
1467

1468
        if (VIR_STRDUP(backing_device, char_ptr) < 0)
1469
            goto cleanup;
1470
    } else {
1471
        VIR_STEAL_PTR(backing_device, ret);
1472 1473 1474 1475 1476 1477 1478
    }

    char_ptr = strchr(backing_device, '\n');

    if (char_ptr)
        *char_ptr = '\0';

1479
 cleanup:
1480
    VIR_FREE(ret);
1481

1482
    return backing_device;
1483 1484 1485 1486 1487 1488
}

static char *
phypGetLparProfile(virConnectPtr conn, int lpar_id)
{
    phyp_driverPtr phyp_driver = conn->privateData;
1489
    LIBSSH2_SESSION *session = phyp_driver->session;
1490 1491 1492 1493 1494 1495 1496 1497
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
1498 1499
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1500 1501
                      " -r prof --filter lpar_ids=%d -F name|head -n 1",
                      lpar_id);
1502
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1503

1504
    if (exit_status < 0)
1505 1506
        VIR_FREE(ret);
    return ret;
1507 1508 1509 1510 1511 1512
}

static int
phypGetVIOSNextSlotNumber(virConnectPtr conn)
{
    phyp_driverPtr phyp_driver = conn->privateData;
1513
    LIBSSH2_SESSION *session = phyp_driver->session;
1514 1515 1516 1517
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    char *profile = NULL;
1518
    int slot = -1;
1519 1520 1521
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!(profile = phypGetLparProfile(conn, vios_id))) {
1522
        VIR_ERROR(_("Unable to get VIOS profile name."));
1523
        return -1;
1524 1525 1526 1527 1528
    }

    virBufferAddLit(&buf, "lssyscfg");

    if (system_type == HMC)
1529
        virBufferAsprintf(&buf, " -m %s", managed_system);
1530

1531
    virBufferAsprintf(&buf, " -r prof --filter "
1532 1533 1534 1535 1536
                      "profile_names=%s -F virtual_eth_adapters,"
                      "virtual_opti_pool_id,virtual_scsi_adapters,"
                      "virtual_serial_adapters|sed -e 's/\"//g' -e "
                      "'s/,/\\n/g'|sed -e 's/\\(^[0-9][0-9]\\*\\).*$/\\1/'"
                      "|sort|tail -n 1", profile);
E
Eric Blake 已提交
1537 1538 1539
    if (phypExecInt(session, &buf, conn, &slot) < 0)
        return -1;
    return slot + 1;
1540 1541 1542 1543 1544
}

static int
phypCreateServerSCSIAdapter(virConnectPtr conn)
{
1545
    int result = -1;
1546
    phyp_driverPtr phyp_driver = conn->privateData;
1547
    LIBSSH2_SESSION *session = phyp_driver->session;
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    char *profile = NULL;
    int slot = 0;
    char *vios_name = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!
        (vios_name =
         phypGetLparNAME(session, managed_system, vios_id, conn))) {
1561
        VIR_ERROR(_("Unable to get VIOS name"));
1562
        goto cleanup;
1563 1564 1565
    }

    if (!(profile = phypGetLparProfile(conn, vios_id))) {
1566
        VIR_ERROR(_("Unable to get VIOS profile name."));
1567
        goto cleanup;
1568 1569 1570
    }

    if ((slot = phypGetVIOSNextSlotNumber(conn)) == -1) {
1571
        VIR_ERROR(_("Unable to get free slot number"));
1572
        goto cleanup;
1573 1574 1575 1576 1577 1578 1579
    }

    /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
     * be appended to this list
     * */
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
1580 1581
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r prof --filter lpar_ids=%d,profile_names=%s"
1582 1583
                      " -F virtual_scsi_adapters|sed -e s/\\\"//g",
                      vios_id, profile);
1584
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1585 1586

    if (exit_status < 0 || ret == NULL)
1587
        goto cleanup;
1588 1589 1590 1591 1592 1593

    /* Here I change the VIOS configuration to append the new adapter
     * with the free slot I got with phypGetVIOSNextSlotNumber.
     * */
    virBufferAddLit(&buf, "chsyscfg");
    if (system_type == HMC)
1594 1595
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r prof -i 'name=%s,lpar_id=%d,"
1596 1597
                      "\"virtual_scsi_adapters=%s,%d/server/any/any/1\"'",
                      vios_name, vios_id, ret, slot);
1598
    VIR_FREE(ret);
1599
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1600 1601

    if (exit_status < 0 || ret == NULL)
1602
        goto cleanup;
1603 1604 1605 1606 1607 1608

    /* Finally I add the new scsi adapter to VIOS using the same slot
     * I used in the VIOS configuration.
     * */
    virBufferAddLit(&buf, "chhwres -r virtualio --rsubtype scsi");
    if (system_type == HMC)
1609 1610
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1611 1612
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      vios_name, slot);
1613
    VIR_FREE(ret);
1614
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1615 1616

    if (exit_status < 0 || ret == NULL)
1617
        goto cleanup;
1618

1619
    result = 0;
1620

1621
 cleanup:
1622 1623 1624
    VIR_FREE(profile);
    VIR_FREE(vios_name);
    VIR_FREE(ret);
1625 1626

    return result;
1627 1628 1629 1630 1631 1632
}

static char *
phypGetVIOSFreeSCSIAdapter(virConnectPtr conn)
{
    phyp_driverPtr phyp_driver = conn->privateData;
1633
    LIBSSH2_SESSION *session = phyp_driver->session;
1634 1635 1636 1637 1638 1639 1640 1641
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
1642
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1643 1644
                          managed_system, vios_id);

1645
    virBufferAddLit(&buf, "lsmap -all -field svsa backing -fmt , ");
1646 1647 1648 1649

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');

1650
    virBufferAddLit(&buf, "|sed '/,[^.*]/d; s/,//g; q'");
1651
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1652

1653
    if (exit_status < 0)
1654 1655
        VIR_FREE(ret);
    return ret;
1656 1657 1658 1659
}


static int
1660
phypDomainAttachDevice(virDomainPtr domain, const char *xml)
1661
{
1662
    int result = -1;
1663 1664
    virConnectPtr conn = domain->conn;
    phyp_driverPtr phyp_driver = domain->conn->privateData;
1665
    LIBSSH2_SESSION *session = phyp_driver->session;
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    char *scsi_adapter = NULL;
    int slot = 0;
    char *vios_name = NULL;
    char *profile = NULL;
    virDomainDeviceDefPtr dev = NULL;
    virDomainDefPtr def = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *domain_name = NULL;

1680
    if (!(def = virDomainDefNew()))
E
Eric Blake 已提交
1681 1682
        goto cleanup;

1683
    domain_name = escape_specialcharacters(domain->name);
1684

1685
    if (domain_name == NULL)
1686
        goto cleanup;
1687

1688
    def->os.type = VIR_DOMAIN_OSTYPE_LINUX;
1689

1690
    dev = virDomainDeviceDefParse(xml, def, phyp_driver->caps, NULL,
1691
                                  VIR_DOMAIN_DEF_PARSE_INACTIVE);
1692
    if (!dev)
1693
        goto cleanup;
1694 1695 1696 1697

    if (!
        (vios_name =
         phypGetLparNAME(session, managed_system, vios_id, conn))) {
1698
        VIR_ERROR(_("Unable to get VIOS name"));
1699
        goto cleanup;
1700 1701 1702 1703 1704 1705 1706 1707
    }

    /* First, let's look for a free SCSI Adapter
     * */
    if (!(scsi_adapter = phypGetVIOSFreeSCSIAdapter(conn))) {
        /* If not found, let's create one.
         * */
        if (phypCreateServerSCSIAdapter(conn) == -1) {
1708
            VIR_ERROR(_("Unable to create new virtual adapter"));
1709
            goto cleanup;
1710 1711
        } else {
            if (!(scsi_adapter = phypGetVIOSFreeSCSIAdapter(conn))) {
1712
                VIR_ERROR(_("Unable to create new virtual adapter"));
1713
                goto cleanup;
1714 1715 1716 1717 1718
            }
        }
    }

    if (system_type == HMC)
1719
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1720 1721
                          managed_system, vios_id);

1722
    virBufferAsprintf(&buf, "mkvdev -vdev %s -vadapter %s",
1723
                      virDomainDiskGetSource(dev->data.disk), scsi_adapter);
1724 1725 1726

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1727
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1728 1729

    if (exit_status < 0 || ret == NULL)
1730
        goto cleanup;
1731 1732

    if (!(profile = phypGetLparProfile(conn, domain->id))) {
1733
        VIR_ERROR(_("Unable to get VIOS profile name."));
1734
        goto cleanup;
1735 1736 1737 1738 1739 1740
    }

    /* Let's get the slot number for the adapter we just created
     * */
    virBufferAddLit(&buf, "lshwres -r virtualio --rsubtype scsi");
    if (system_type == HMC)
1741 1742
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1743
                      " slot_num,backing_device|grep %s|cut -d, -f1",
1744
                      virDomainDiskGetSource(dev->data.disk));
E
Eric Blake 已提交
1745
    if (phypExecInt(session, &buf, conn, &slot) < 0)
1746
        goto cleanup;
1747 1748 1749 1750 1751 1752

    /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
     * be appended to this list
     * */
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
1753 1754
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1755 1756 1757
                      " -r prof --filter lpar_ids=%d,profile_names=%s"
                      " -F virtual_scsi_adapters|sed -e 's/\"//g'",
                      vios_id, profile);
1758
    VIR_FREE(ret);
1759
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1760 1761

    if (exit_status < 0 || ret == NULL)
1762
        goto cleanup;
1763 1764 1765 1766 1767 1768

    /* Here I change the LPAR configuration to append the new adapter
     * with the new slot we just created
     * */
    virBufferAddLit(&buf, "chsyscfg");
    if (system_type == HMC)
1769 1770
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1771 1772 1773 1774
                      " -r prof -i 'name=%s,lpar_id=%d,"
                      "\"virtual_scsi_adapters=%s,%d/client/%d/%s/0\"'",
                      domain_name, domain->id, ret, slot,
                      vios_id, vios_name);
E
Eric Blake 已提交
1775
    if (phypExecInt(session, &buf, conn, &slot) < 0)
1776
        goto cleanup;
1777 1778 1779 1780 1781 1782

    /* Finally I add the new scsi adapter to VIOS using the same slot
     * I used in the VIOS configuration.
     * */
    virBufferAddLit(&buf, "chhwres -r virtualio --rsubtype scsi");
    if (system_type == HMC)
1783 1784
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1785 1786
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      domain_name, slot);
1787
    VIR_FREE(ret);
1788
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1789 1790

    if (exit_status < 0 || ret == NULL) {
1791
        VIR_ERROR(_
1792 1793
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    "Contact your support to enable this feature."));
1794
        goto cleanup;
1795 1796
    }

1797
    result = 0;
1798

1799
 cleanup:
1800
    VIR_FREE(ret);
1801 1802
    virDomainDeviceDefFree(dev);
    virDomainDefFree(def);
1803 1804
    VIR_FREE(vios_name);
    VIR_FREE(scsi_adapter);
1805 1806 1807 1808
    VIR_FREE(profile);
    VIR_FREE(domain_name);

    return result;
1809 1810
}

1811
static char *
1812
phypStorageVolGetKey(virConnectPtr conn, const char *name)
1813 1814
{
    phyp_driverPtr phyp_driver = conn->privateData;
1815
    LIBSSH2_SESSION *session = phyp_driver->session;
1816 1817 1818 1819 1820 1821 1822 1823
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
1824
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1825 1826
                          managed_system, vios_id);

1827
    virBufferAsprintf(&buf, "lslv %s -field lvid", name);
1828 1829 1830 1831

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');

1832
    virBufferAddLit(&buf, "|sed -e 's/^LV IDENTIFIER://' -e 's/ //g'");
1833
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1834

1835
    if (exit_status < 0)
1836 1837
        VIR_FREE(ret);
    return ret;
1838 1839 1840 1841 1842 1843
}

static char *
phypGetStoragePoolDevice(virConnectPtr conn, char *name)
{
    phyp_driverPtr phyp_driver = conn->privateData;
1844
    LIBSSH2_SESSION *session = phyp_driver->session;
1845 1846 1847 1848 1849 1850 1851 1852
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
1853
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1854 1855
                          managed_system, vios_id);

1856
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field name", name);
1857 1858 1859 1860

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');

1861
    virBufferAddLit(&buf, "|sed '1d; s/ //g'");
1862
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1863

1864
    if (exit_status < 0)
1865 1866
        VIR_FREE(ret);
    return ret;
1867 1868 1869 1870 1871 1872
}

static unsigned long int
phypGetStoragePoolSize(virConnectPtr conn, char *name)
{
    phyp_driverPtr phyp_driver = conn->privateData;
1873
    LIBSSH2_SESSION *session = phyp_driver->session;
1874 1875 1876
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
1877
    int sp_size = -1;
1878 1879 1880
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
1881
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1882 1883
                          managed_system, vios_id);

1884
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field size", name);
1885 1886 1887 1888

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');

1889
    virBufferAddLit(&buf, "|sed '1d; s/ //g'");
E
Eric Blake 已提交
1890
    phypExecInt(session, &buf, conn, &sp_size);
1891
    return sp_size;
1892 1893
}

1894
static char *
1895
phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
1896
                unsigned int capacity)
1897 1898
{
    phyp_driverPtr phyp_driver = conn->privateData;
1899
    LIBSSH2_SESSION *session = phyp_driver->session;
1900 1901 1902 1903 1904 1905
    int vios_id = phyp_driver->vios_id;
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1906
    char *key = NULL;
1907 1908

    if (system_type == HMC)
1909
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1910 1911
                          managed_system, vios_id);

1912
    virBufferAsprintf(&buf, "mklv -lv %s %s %d", lvname, spname, capacity);
1913 1914 1915

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1916
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1917 1918

    if (exit_status < 0) {
1919
        VIR_ERROR(_("Unable to create Volume: %s"), NULLSTR(ret));
1920
        goto cleanup;
1921 1922
    }

1923
    key = phypStorageVolGetKey(conn, lvname);
1924

1925
 cleanup:
1926 1927
    VIR_FREE(ret);

1928
    return key;
1929 1930 1931
}

static virStorageVolPtr
1932
phypStorageVolLookupByName(virStoragePoolPtr pool, const char *volname)
1933
{
1934 1935
    char *key;
    virStorageVolPtr vol;
1936

1937
    key = phypStorageVolGetKey(pool->conn, volname);
1938

1939
    if (key == NULL)
1940 1941
        return NULL;

1942
    vol = virGetStorageVol(pool->conn, pool->name, volname, key, NULL, NULL);
1943 1944 1945 1946

    VIR_FREE(key);

    return vol;
1947 1948 1949 1950 1951 1952 1953 1954 1955
}

static virStorageVolPtr
phypStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xml, unsigned int flags)
{
    virCheckFlags(0, NULL);

    virStorageVolPtr vol = NULL;
1956
    virStorageVolPtr dup_vol = NULL;
1957
    char *key = NULL;
1958
    VIR_AUTOPTR(virStorageVolDef) voldef = NULL;
1959
    VIR_AUTOPTR(virStoragePoolDef) spdef = NULL;
1960

1961
    if (VIR_ALLOC(spdef) < 0)
1962 1963 1964 1965 1966 1967 1968
        return NULL;

    /* Filling spdef manually
     * */
    if (pool->name != NULL) {
        spdef->name = pool->name;
    } else {
1969
        VIR_ERROR(_("Unable to determine storage pool's name."));
1970 1971 1972 1973
        goto err;
    }

    if (memcpy(spdef->uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
1974
        VIR_ERROR(_("Unable to determine storage pool's uuid."));
1975 1976 1977 1978 1979
        goto err;
    }

    if ((spdef->capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
1980
        VIR_ERROR(_("Unable to determine storage pools's size."));
1981 1982 1983
        goto err;
    }

J
Ján Tomko 已提交
1984
    /* Information not available */
1985 1986 1987 1988 1989 1990
    spdef->allocation = 0;
    spdef->available = 0;

    spdef->source.ndevice = 1;

    /*XXX source adapter not working properly, should show hdiskX */
1991
    if ((spdef->source.adapter.data.scsi_host.name =
1992
         phypGetStoragePoolDevice(pool->conn, pool->name)) == NULL) {
1993
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
1994 1995 1996
        goto err;
    }

1997
    if ((voldef = virStorageVolDefParseString(spdef, xml, 0)) == NULL) {
1998
        VIR_ERROR(_("Error parsing volume XML."));
1999 2000 2001 2002
        goto err;
    }

    /* checking if this name already exists on this system */
2003
    if ((dup_vol = phypStorageVolLookupByName(pool, voldef->name)) != NULL) {
2004
        VIR_ERROR(_("StoragePool name already exists."));
2005
        virObjectUnref(dup_vol);
2006 2007 2008 2009 2010 2011 2012
        goto err;
    }

    /* The key must be NULL, the Power Hypervisor creates a key
     * in the moment you create the volume.
     * */
    if (voldef->key) {
2013
        VIR_ERROR(_("Key must be empty, Power Hypervisor will create one for you."));
2014 2015 2016
        goto err;
    }

2017
    if (!voldef->target.capacity) {
2018
        VIR_ERROR(_("Capacity cannot be empty."));
2019 2020 2021
        goto err;
    }

2022
    key = phypBuildVolume(pool->conn, voldef->name, spdef->name,
2023
                          voldef->target.capacity);
2024 2025

    if (key == NULL)
2026 2027 2028 2029
        goto err;

    if ((vol =
         virGetStorageVol(pool->conn, pool->name, voldef->name,
2030
                          key, NULL, NULL)) == NULL)
2031 2032
        goto err;

2033 2034
    VIR_FREE(key);

2035 2036
    return vol;

2037
 err:
2038
    VIR_FREE(key);
2039
    virObjectUnref(vol);
2040 2041 2042 2043
    return NULL;
}

static char *
2044
phypStorageVolGetPhysicalVolumeByStoragePool(virStorageVolPtr vol, char *sp)
2045 2046 2047
{
    virConnectPtr conn = vol->conn;
    phyp_driverPtr phyp_driver = conn->privateData;
2048
    LIBSSH2_SESSION *session = phyp_driver->session;
2049 2050 2051 2052 2053 2054 2055 2056
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2057
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2058 2059
                          managed_system, vios_id);

2060
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field pvname", sp);
2061 2062 2063 2064

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');

2065
    virBufferAddLit(&buf, "|sed 1d");
2066
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2067

2068
    if (exit_status < 0)
2069 2070
        VIR_FREE(ret);
    return ret;
2071 2072 2073
}

static virStorageVolPtr
2074
phypStorageVolLookupByPath(virConnectPtr conn, const char *volname)
2075 2076
{
    phyp_driverPtr phyp_driver = conn->privateData;
2077
    LIBSSH2_SESSION *session = phyp_driver->session;
2078 2079 2080 2081
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
2082
    char *ret = NULL;
2083 2084
    char *key = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2085
    virStorageVolPtr vol = NULL;
2086 2087

    if (system_type == HMC)
2088
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2089 2090
                          managed_system, vios_id);

2091
    virBufferAsprintf(&buf, "lslv %s -field vgname", volname);
2092 2093 2094 2095

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');

2096
    virBufferAddLit(&buf, "|sed -e 's/^VOLUME GROUP://g' -e 's/ //g'");
2097
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2098

2099
    if (exit_status < 0 || ret == NULL)
2100
        goto cleanup;
2101

2102
    key = phypStorageVolGetKey(conn, volname);
2103

2104
    if (key == NULL)
2105
        goto cleanup;
2106

2107
    vol = virGetStorageVol(conn, ret, volname, key, NULL, NULL);
2108

2109
 cleanup:
2110
    VIR_FREE(ret);
2111 2112 2113
    VIR_FREE(key);

    return vol;
2114 2115 2116 2117 2118 2119
}

static int
phypGetStoragePoolUUID(virConnectPtr conn, unsigned char *uuid,
                       const char *name)
{
2120
    int result = -1;
2121
    phyp_driverPtr phyp_driver = conn->privateData;
2122
    LIBSSH2_SESSION *session = phyp_driver->session;
2123 2124 2125 2126 2127 2128 2129 2130
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2131
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2132 2133
                          managed_system, vios_id);

2134
    virBufferAsprintf(&buf, "lsdev -dev %s -attr vgserial_id", name);
2135 2136 2137 2138

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');

2139
    virBufferAddLit(&buf, "|sed '1,2d'");
2140
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2141 2142

    if (exit_status < 0 || ret == NULL)
2143
        goto cleanup;
2144

2145
    if (memcpy(uuid, ret, VIR_UUID_BUFLEN) == NULL)
2146
        goto cleanup;
2147

2148
    result = 0;
2149

2150
 cleanup:
2151
    VIR_FREE(ret);
2152 2153

    return result;
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
}

static virStoragePoolPtr
phypStoragePoolLookupByName(virConnectPtr conn, const char *name)
{
    unsigned char uuid[VIR_UUID_BUFLEN];

    if (phypGetStoragePoolUUID(conn, uuid, name) == -1)
        return NULL;

2164
    return virGetStoragePool(conn, name, uuid, NULL, NULL);
2165 2166 2167
}

static char *
2168
phypStorageVolGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
2169
{
2170 2171 2172
    virStorageVolDef voldef;
    virStoragePoolDef pool;
    virStoragePoolPtr sp;
2173
    char *xml = NULL;
2174

2175 2176 2177
    virCheckFlags(0, NULL);

    memset(&voldef, 0, sizeof(virStorageVolDef));
2178
    memset(&pool, 0, sizeof(virStoragePoolDef));
2179

2180
    sp = phypStoragePoolLookupByName(vol->conn, vol->pool);
2181 2182

    if (!sp)
2183
        goto cleanup;
2184 2185 2186 2187

    if (sp->name != NULL) {
        pool.name = sp->name;
    } else {
2188
        VIR_ERROR(_("Unable to determine storage sp's name."));
2189
        goto cleanup;
2190 2191
    }

2192
    if (memcpy(pool.uuid, sp->uuid, VIR_UUID_BUFLEN) == NULL) {
2193
        VIR_ERROR(_("Unable to determine storage sp's uuid."));
2194
        goto cleanup;
2195 2196 2197
    }

    if ((pool.capacity = phypGetStoragePoolSize(sp->conn, sp->name)) == -1) {
2198
        VIR_ERROR(_("Unable to determine storage sps's size."));
2199
        goto cleanup;
2200 2201
    }

J
Ján Tomko 已提交
2202
    /* Information not available */
2203 2204 2205 2206 2207
    pool.allocation = 0;
    pool.available = 0;

    pool.source.ndevice = 1;

2208
    if ((pool.source.adapter.data.scsi_host.name =
2209
         phypGetStoragePoolDevice(sp->conn, sp->name)) == NULL) {
2210
        VIR_ERROR(_("Unable to determine storage sps's source adapter."));
2211
        goto cleanup;
2212 2213
    }

2214
    if (vol->name != NULL) {
2215
        voldef.name = vol->name;
2216
    } else {
2217
        VIR_ERROR(_("Unable to determine storage pool's name."));
2218
        goto cleanup;
2219 2220
    }

2221
    if (VIR_STRDUP(voldef.key, vol->key) < 0)
2222
        goto cleanup;
2223 2224 2225

    voldef.type = VIR_STORAGE_POOL_LOGICAL;

2226 2227 2228 2229
    xml = virStorageVolDefFormat(&pool, &voldef);

    VIR_FREE(voldef.key);

2230
 cleanup:
2231
    virObjectUnref(sp);
2232
    return xml;
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
}

/* The Volume Group path here will be treated as suggested in the
 * email on the libvirt mailling list. As soon as I can't get the
 * path for every volume, the path will be a representation in
 * the form:
 *
 * /physical_volume/storage_pool/logical_volume
 *
 * */
static char *
2244
phypStorageVolGetPath(virStorageVolPtr vol)
2245 2246 2247
{
    virConnectPtr conn = vol->conn;
    phyp_driverPtr phyp_driver = conn->privateData;
2248
    LIBSSH2_SESSION *session = phyp_driver->session;
2249 2250 2251 2252
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
2253
    char *ret = NULL;
2254 2255
    char *path = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2256
    char *pv;
2257 2258

    if (system_type == HMC)
2259
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2260 2261
                          managed_system, vios_id);

2262
    virBufferAsprintf(&buf, "lslv %s -field vgname", vol->name);
2263 2264 2265 2266

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');

2267
    virBufferAsprintf(&buf,
2268
                      "|sed -e 's/^VOLUME GROUP://g' -e 's/ //g'");
2269
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2270

2271
    if (exit_status < 0 || ret == NULL)
2272
        goto cleanup;
2273

2274
    pv = phypStorageVolGetPhysicalVolumeByStoragePool(vol, ret);
2275

2276 2277
    if (!pv)
        goto cleanup;
2278

2279
    if (virAsprintf(&path, "/%s/%s/%s", pv, ret, vol->name) < 0)
2280
        goto cleanup;
2281

2282
 cleanup:
2283
    VIR_FREE(ret);
2284
    VIR_FREE(path);
2285 2286

    return path;
2287 2288 2289 2290 2291 2292
}

static int
phypStoragePoolListVolumes(virStoragePoolPtr pool, char **const volumes,
                           int nvolumes)
{
2293
    bool success = false;
2294 2295
    virConnectPtr conn = pool->conn;
    phyp_driverPtr phyp_driver = conn->privateData;
2296
    LIBSSH2_SESSION *session = phyp_driver->session;
2297 2298 2299 2300 2301
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    int got = 0;
2302
    size_t i;
2303 2304
    char *ret = NULL;
    char *volumes_list = NULL;
E
Eric Blake 已提交
2305
    char *char_ptr = NULL;
2306 2307 2308
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2309
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2310 2311
                          managed_system, vios_id);

2312
    virBufferAsprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
2313 2314 2315 2316

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');

2317
    virBufferAddLit(&buf, "|sed '1,2d'");
2318
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2319 2320

    /* I need to parse the textual return in order to get the volumes */
2321
    if (exit_status < 0 || ret == NULL) {
2322
        goto cleanup;
2323
    } else {
2324 2325 2326
        volumes_list = ret;

        while (got < nvolumes) {
E
Eric Blake 已提交
2327
            char_ptr = strchr(volumes_list, '\n');
2328

E
Eric Blake 已提交
2329 2330
            if (char_ptr) {
                *char_ptr = '\0';
2331
                if (VIR_STRDUP(volumes[got++], volumes_list) < 0)
2332
                    goto cleanup;
E
Eric Blake 已提交
2333 2334
                char_ptr++;
                volumes_list = char_ptr;
2335
            } else {
2336
                break;
2337
            }
2338 2339 2340
        }
    }

2341 2342
    success = true;

2343
 cleanup:
2344 2345 2346 2347 2348 2349
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(volumes[i]);

        got = -1;
    }
2350
    VIR_FREE(ret);
2351
    return got;
2352 2353 2354 2355 2356 2357 2358
}

static int
phypStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
    virConnectPtr conn = pool->conn;
    phyp_driverPtr phyp_driver = conn->privateData;
2359
    LIBSSH2_SESSION *session = phyp_driver->session;
2360
    int system_type = phyp_driver->system_type;
2361
    int nvolumes = -1;
2362 2363 2364 2365 2366
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2367
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2368
                          managed_system, vios_id);
2369
    virBufferAsprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
2370 2371
    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2372
    virBufferAddLit(&buf, "|grep -c '^.*$'");
E
Eric Blake 已提交
2373 2374
    if (phypExecInt(session, &buf, conn, &nvolumes) < 0)
        return -1;
2375 2376

    /* We need to remove 2 line from the header text output */
E
Eric Blake 已提交
2377
    return nvolumes - 2;
2378 2379 2380
}

static int
2381
phypStoragePoolDestroy(virStoragePoolPtr pool)
2382
{
2383
    int result = -1;
2384 2385
    virConnectPtr conn = pool->conn;
    phyp_driverPtr phyp_driver = conn->privateData;
2386
    LIBSSH2_SESSION *session = phyp_driver->session;
2387 2388 2389 2390 2391 2392 2393 2394
    int vios_id = phyp_driver->vios_id;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2395
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2396 2397
                          managed_system, vios_id);

2398
    virBufferAsprintf(&buf, "rmsp %s", pool->name);
2399 2400 2401

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2402
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2403 2404

    if (exit_status < 0) {
2405
        VIR_ERROR(_("Unable to destroy Storage Pool: %s"), NULLSTR(ret));
2406
        goto cleanup;
2407 2408
    }

2409
    result = 0;
2410

2411
 cleanup:
2412
    VIR_FREE(ret);
2413 2414

    return result;
2415 2416 2417 2418 2419
}

static int
phypBuildStoragePool(virConnectPtr conn, virStoragePoolDefPtr def)
{
2420
    int result = -1;
2421
    phyp_driverPtr phyp_driver = conn->privateData;
2422
    LIBSSH2_SESSION *session = phyp_driver->session;
2423 2424 2425 2426 2427 2428 2429 2430
    virStoragePoolSource source = def->source;
    int vios_id = phyp_driver->vios_id;
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

2431
    if (source.adapter.type != VIR_STORAGE_ADAPTER_TYPE_SCSI_HOST) {
2432 2433 2434 2435 2436
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Only 'scsi_host' adapter is supported"));
        goto cleanup;
    }

2437
    if (system_type == HMC)
2438
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2439 2440
                          managed_system, vios_id);

2441
    virBufferAsprintf(&buf, "mksp -f %schild %s", def->name,
2442
                      source.adapter.data.scsi_host.name);
2443 2444 2445

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2446
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2447 2448

    if (exit_status < 0) {
2449
        VIR_ERROR(_("Unable to create Storage Pool: %s"), NULLSTR(ret));
2450
        goto cleanup;
2451 2452
    }

2453
    result = 0;
2454

2455
 cleanup:
2456
    VIR_FREE(ret);
2457 2458

    return result;
2459 2460 2461 2462

}

static int
2463
phypConnectNumOfStoragePools(virConnectPtr conn)
2464 2465
{
    phyp_driverPtr phyp_driver = conn->privateData;
2466
    LIBSSH2_SESSION *session = phyp_driver->session;
2467
    int system_type = phyp_driver->system_type;
2468
    int nsp = -1;
2469 2470 2471 2472 2473
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2474
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2475 2476
                          managed_system, vios_id);

2477
    virBufferAddLit(&buf, "lsvg");
2478 2479 2480 2481

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');

2482
    virBufferAddLit(&buf, "|grep -c '^.*$'");
E
Eric Blake 已提交
2483
    phypExecInt(session, &buf, conn, &nsp);
2484
    return nsp;
2485 2486 2487
}

static int
2488
phypConnectListStoragePools(virConnectPtr conn, char **const pools, int npools)
2489
{
2490
    bool success = false;
2491
    phyp_driverPtr phyp_driver = conn->privateData;
2492
    LIBSSH2_SESSION *session = phyp_driver->session;
2493 2494 2495 2496 2497
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    int got = 0;
2498
    size_t i;
2499 2500
    char *ret = NULL;
    char *storage_pools = NULL;
E
Eric Blake 已提交
2501
    char *char_ptr = NULL;
2502 2503 2504
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2505
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2506 2507
                          managed_system, vios_id);

2508
    virBufferAddLit(&buf, "lsvg");
2509 2510 2511

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2512
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2513 2514

    /* I need to parse the textual return in order to get the storage pools */
2515
    if (exit_status < 0 || ret == NULL) {
2516
        goto cleanup;
2517
    } else {
2518 2519 2520
        storage_pools = ret;

        while (got < npools) {
E
Eric Blake 已提交
2521
            char_ptr = strchr(storage_pools, '\n');
2522

E
Eric Blake 已提交
2523 2524
            if (char_ptr) {
                *char_ptr = '\0';
2525
                if (VIR_STRDUP(pools[got++], storage_pools) < 0)
2526
                    goto cleanup;
E
Eric Blake 已提交
2527 2528
                char_ptr++;
                storage_pools = char_ptr;
2529
            } else {
2530
                break;
2531
            }
2532 2533 2534
        }
    }

2535 2536
    success = true;

2537
 cleanup:
2538 2539 2540 2541 2542 2543
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(pools[i]);

        got = -1;
    }
2544
    VIR_FREE(ret);
2545
    return got;
2546 2547 2548
}

static virStoragePoolPtr
2549 2550
phypStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid)
2551 2552 2553 2554 2555
{
    virStoragePoolPtr sp = NULL;
    int npools = 0;
    int gotpools = 0;
    char **pools = NULL;
2556
    size_t i = 0;
2557 2558
    unsigned char *local_uuid = NULL;

2559
    if (VIR_ALLOC_N(local_uuid, VIR_UUID_BUFLEN) < 0)
2560 2561
        goto err;

2562
    if ((npools = phypConnectNumOfStoragePools(conn)) == -1)
2563 2564
        goto err;

2565
    if (VIR_ALLOC_N(pools, npools) < 0)
2566 2567
        goto err;

2568
    if ((gotpools = phypConnectListStoragePools(conn, pools, npools)) == -1)
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
        goto err;

    if (gotpools != npools) {
        virReportOOMError();
        goto err;
    }

    for (i = 0; i < gotpools; i++) {
        if (phypGetStoragePoolUUID(conn, local_uuid, pools[i]) == -1)
            continue;

        if (!memcmp(local_uuid, uuid, VIR_UUID_BUFLEN)) {
2581
            sp = virGetStoragePool(conn, pools[i], uuid, NULL, NULL);
2582 2583 2584 2585 2586 2587 2588 2589 2590 2591
            VIR_FREE(local_uuid);
            VIR_FREE(pools);

            if (sp)
                return sp;
            else
                goto err;
        }
    }

2592
 err:
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
    VIR_FREE(local_uuid);
    VIR_FREE(pools);
    return NULL;
}

static virStoragePoolPtr
phypStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml, unsigned int flags)
{
    virCheckFlags(0, NULL);

    virStoragePoolDefPtr def = NULL;
2605
    virStoragePoolPtr dup_sp = NULL;
2606 2607 2608 2609 2610 2611
    virStoragePoolPtr sp = NULL;

    if (!(def = virStoragePoolDefParseString(xml)))
        goto err;

    /* checking if this name already exists on this system */
2612
    if ((dup_sp = phypStoragePoolLookupByName(conn, def->name)) != NULL) {
2613
        VIR_WARN("StoragePool name already exists.");
2614
        virObjectUnref(dup_sp);
2615 2616 2617 2618
        goto err;
    }

    /* checking if ID or UUID already exists on this system */
2619
    if ((dup_sp = phypStoragePoolLookupByUUID(conn, def->uuid)) != NULL) {
2620
        VIR_WARN("StoragePool uuid already exists.");
2621
        virObjectUnref(dup_sp);
2622 2623
        goto err;
    }
2624

2625
    if ((sp = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL)) == NULL)
2626 2627 2628 2629 2630 2631 2632
        goto err;

    if (phypBuildStoragePool(conn, def) == -1)
        goto err;

    return sp;

2633
 err:
2634
    virStoragePoolDefFree(def);
2635
    virObjectUnref(sp);
2636 2637 2638 2639
    return NULL;
}

static char *
2640
phypStoragePoolGetXMLDesc(virStoragePoolPtr pool, unsigned int flags)
2641 2642 2643 2644 2645 2646
{
    virCheckFlags(0, NULL);

    virStoragePoolDef def;
    memset(&def, 0, sizeof(virStoragePoolDef));

2647
    if (pool->name != NULL) {
2648
        def.name = pool->name;
2649
    } else {
2650
        VIR_ERROR(_("Unable to determine storage pool's name."));
2651 2652 2653
        goto err;
    }

2654
    if (memcpy(def.uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
2655
        VIR_ERROR(_("Unable to determine storage pool's uuid."));
2656 2657 2658 2659 2660
        goto err;
    }

    if ((def.capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2661
        VIR_ERROR(_("Unable to determine storage pools's size."));
2662 2663 2664
        goto err;
    }

J
Ján Tomko 已提交
2665
    /* Information not available */
2666 2667 2668 2669 2670 2671
    def.allocation = 0;
    def.available = 0;

    def.source.ndevice = 1;

    /*XXX source adapter not working properly, should show hdiskX */
2672
    if ((def.source.adapter.data.scsi_host.name =
2673
         phypGetStoragePoolDevice(pool->conn, pool->name)) == NULL) {
2674
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
2675 2676 2677 2678 2679
        goto err;
    }

    return virStoragePoolDefFormat(&def);

2680
 err:
2681
    return NULL;
2682 2683
}

E
Eduardo Otubo 已提交
2684 2685 2686 2687 2688 2689 2690
static int
phypInterfaceDestroy(virInterfacePtr iface,
                     unsigned int flags)
{
    virCheckFlags(0, -1);

    phyp_driverPtr phyp_driver = iface->conn->privateData;
2691
    LIBSSH2_SESSION *session = phyp_driver->session;
E
Eduardo Otubo 已提交
2692 2693 2694 2695 2696 2697 2698
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int exit_status = 0;
    int slot_num = 0;
    int lpar_id = 0;
    char *ret = NULL;
E
Eric Blake 已提交
2699
    int rv = -1;
E
Eduardo Otubo 已提交
2700 2701 2702 2703 2704

    /* Getting the remote slot number */

    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
2705
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2706

2707
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2708 2709 2710
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,slot_num|"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
2711
    if (phypExecInt(session, &buf, iface->conn, &slot_num) < 0)
E
Eric Blake 已提交
2712
        goto cleanup;
E
Eduardo Otubo 已提交
2713 2714 2715 2716

    /* Getting the remote slot number */
    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
2717
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2718

2719
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2720 2721 2722
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,lpar_id|"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
2723
    if (phypExecInt(session, &buf, iface->conn, &lpar_id) < 0)
E
Eric Blake 已提交
2724
        goto cleanup;
E
Eduardo Otubo 已提交
2725 2726 2727 2728

    /* excluding interface */
    virBufferAddLit(&buf, "chhwres ");
    if (system_type == HMC)
2729
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2730

2731
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2732 2733
                      " -r virtualio --rsubtype eth"
                      " --id %d -o r -s %d", lpar_id, slot_num);
2734 2735
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, iface->conn, false);
E
Eduardo Otubo 已提交
2736 2737

    if (exit_status < 0 || ret != NULL)
E
Eric Blake 已提交
2738
        goto cleanup;
E
Eduardo Otubo 已提交
2739

E
Eric Blake 已提交
2740
    rv = 0;
E
Eduardo Otubo 已提交
2741

2742
 cleanup:
E
Eduardo Otubo 已提交
2743
    VIR_FREE(ret);
E
Eric Blake 已提交
2744
    return rv;
E
Eduardo Otubo 已提交
2745 2746 2747 2748 2749 2750 2751 2752 2753
}

static virInterfacePtr
phypInterfaceDefineXML(virConnectPtr conn, const char *xml,
                       unsigned int flags)
{
    virCheckFlags(0, NULL);

    phyp_driverPtr phyp_driver = conn->privateData;
2754
    LIBSSH2_SESSION *session = phyp_driver->session;
E
Eduardo Otubo 已提交
2755 2756 2757 2758 2759 2760 2761 2762 2763
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int exit_status = 0;
    int slot = 0;
    char *ret = NULL;
    char name[PHYP_IFACENAME_SIZE];
    char mac[PHYP_MAC_SIZE];
    virInterfaceDefPtr def;
E
Eric Blake 已提交
2764
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2765 2766

    if (!(def = virInterfaceDefParseString(xml)))
E
Eric Blake 已提交
2767
        goto cleanup;
E
Eduardo Otubo 已提交
2768 2769 2770 2771

    /* Now need to get the next free slot number */
    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
2772
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2773

2774
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2775 2776 2777
                      " -r virtualio --rsubtype slot --level slot"
                      " -Fslot_num --filter lpar_names=%s"
                      " |sort|tail -n 1", def->name);
E
Eric Blake 已提交
2778
    if (phypExecInt(session, &buf, conn, &slot) < 0)
E
Eric Blake 已提交
2779
        goto cleanup;
E
Eduardo Otubo 已提交
2780 2781 2782 2783 2784 2785 2786

    /* The next free slot itself: */
    slot++;

    /* Now adding the new network interface */
    virBufferAddLit(&buf, "chhwres ");
    if (system_type == HMC)
2787
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2788

2789
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2790 2791 2792
                      " -r virtualio --rsubtype eth"
                      " -p %s -o a -s %d -a port_vlan_id=1,"
                      "ieee_virtual_eth=0", def->name, slot);
2793 2794
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2795 2796

    if (exit_status < 0 || ret != NULL)
E
Eric Blake 已提交
2797
        goto cleanup;
E
Eduardo Otubo 已提交
2798 2799 2800 2801 2802 2803 2804 2805 2806

    /* Need to sleep a little while to wait for the HMC to
     * complete the execution of the command.
     * */
    sleep(1);

    /* Getting the new interface name */
    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
2807
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2808

2809
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2810 2811 2812
                      " -r virtualio --rsubtype slot --level slot"
                      " |sed '/lpar_name=%s/!d; /slot_num=%d/!d; "
                      "s/^.*drc_name=//'", def->name, slot);
2813 2814
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2815 2816 2817 2818 2819

    if (exit_status < 0 || ret == NULL) {
        /* roll back and excluding interface if error*/
        virBufferAddLit(&buf, "chhwres ");
        if (system_type == HMC)
2820
            virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2821

2822
        virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2823 2824
                " -r virtualio --rsubtype eth"
                " -p %s -o r -s %d", def->name, slot);
2825 2826
        VIR_FREE(ret);
        ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eric Blake 已提交
2827
        goto cleanup;
E
Eduardo Otubo 已提交
2828 2829 2830 2831 2832 2833 2834
    }

    memcpy(name, ret, PHYP_IFACENAME_SIZE-1);

    /* Getting the new interface mac addr */
    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
2835
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2836

2837
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2838 2839 2840
                      "-r virtualio --rsubtype eth --level lpar "
                      " |sed '/lpar_name=%s/!d; /slot_num=%d/!d; "
                      "s/^.*mac_addr=//'", def->name, slot);
2841 2842
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2843 2844

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
2845
        goto cleanup;
E
Eduardo Otubo 已提交
2846 2847 2848

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

E
Eric Blake 已提交
2849
    result = virGetInterface(conn, name, mac);
E
Eduardo Otubo 已提交
2850

2851
 cleanup:
E
Eduardo Otubo 已提交
2852 2853
    VIR_FREE(ret);
    virInterfaceDefFree(def);
E
Eric Blake 已提交
2854
    return result;
E
Eduardo Otubo 已提交
2855 2856 2857 2858 2859 2860
}

static virInterfacePtr
phypInterfaceLookupByName(virConnectPtr conn, const char *name)
{
    phyp_driverPtr phyp_driver = conn->privateData;
2861
    LIBSSH2_SESSION *session = phyp_driver->session;
E
Eduardo Otubo 已提交
2862 2863 2864 2865 2866 2867 2868 2869
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int exit_status = 0;
    char *ret = NULL;
    int slot = 0;
    int lpar_id = 0;
    char mac[PHYP_MAC_SIZE];
2870
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2871 2872 2873 2874

    /*Getting the slot number for the interface */
    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
2875
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2876

2877
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2878 2879 2880
                      " -r virtualio --rsubtype slot --level slot "
                      " -F drc_name,slot_num |"
                      " sed -n '/%s/ s/^.*,//p'", name);
E
Eric Blake 已提交
2881
    if (phypExecInt(session, &buf, conn, &slot) < 0)
E
Eric Blake 已提交
2882
        goto cleanup;
E
Eduardo Otubo 已提交
2883 2884 2885 2886

    /*Getting the lpar_id for the interface */
    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
2887
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2888

2889
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2890 2891 2892
                      " -r virtualio --rsubtype slot --level slot "
                      " -F drc_name,lpar_id |"
                      " sed -n '/%s/ s/^.*,//p'", name);
E
Eric Blake 已提交
2893
    if (phypExecInt(session, &buf, conn, &lpar_id) < 0)
E
Eric Blake 已提交
2894
        goto cleanup;
E
Eduardo Otubo 已提交
2895 2896 2897 2898

    /*Getting the interface mac */
    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
2899
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2900

2901
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2902 2903 2904
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F lpar_id,slot_num,mac_addr|"
                      " sed -n '/%d,%d/ s/^.*,//p'", lpar_id, slot);
2905
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2906 2907

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
2908
        goto cleanup;
E
Eduardo Otubo 已提交
2909 2910 2911

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

2912
    result = virGetInterface(conn, name, ret);
E
Eduardo Otubo 已提交
2913

2914
 cleanup:
E
Eduardo Otubo 已提交
2915 2916 2917 2918 2919 2920 2921 2922
    VIR_FREE(ret);
    return result;
}

static int
phypInterfaceIsActive(virInterfacePtr iface)
{
    phyp_driverPtr phyp_driver = iface->conn->privateData;
2923
    LIBSSH2_SESSION *session = phyp_driver->session;
E
Eduardo Otubo 已提交
2924 2925 2926
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
E
Eric Blake 已提交
2927
    int state = -1;
E
Eduardo Otubo 已提交
2928 2929 2930

    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
2931
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2932

2933
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2934 2935 2936
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,state |"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
2937
    phypExecInt(session, &buf, iface->conn, &state);
E
Eduardo Otubo 已提交
2938 2939 2940 2941
    return state;
}

static int
2942
phypConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
E
Eduardo Otubo 已提交
2943 2944
{
    phyp_driverPtr phyp_driver = conn->privateData;
2945
    LIBSSH2_SESSION *session = phyp_driver->session;
E
Eduardo Otubo 已提交
2946 2947 2948 2949 2950
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    int got = 0;
2951
    size_t i;
E
Eduardo Otubo 已提交
2952 2953
    char *ret = NULL;
    char *networks = NULL;
E
Eric Blake 已提交
2954
    char *char_ptr = NULL;
E
Eduardo Otubo 已提交
2955
    virBuffer buf = VIR_BUFFER_INITIALIZER;
E
Eric Blake 已提交
2956
    bool success = false;
E
Eduardo Otubo 已提交
2957 2958 2959

    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
2960 2961
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r virtualio --rsubtype slot  --level slot|"
E
Eduardo Otubo 已提交
2962 2963
                      " sed '/eth/!d; /lpar_id=%d/d; s/^.*drc_name=//g'",
                      vios_id);
2964
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2965

E
Eric Blake 已提交
2966 2967
    /* I need to parse the textual return in order to get the network
     * interfaces */
E
Eduardo Otubo 已提交
2968
    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
2969
        goto cleanup;
E
Eduardo Otubo 已提交
2970 2971 2972 2973

    networks = ret;

    while (got < nnames) {
E
Eric Blake 已提交
2974
        char_ptr = strchr(networks, '\n');
E
Eduardo Otubo 已提交
2975

E
Eric Blake 已提交
2976 2977
        if (char_ptr) {
            *char_ptr = '\0';
2978
            if (VIR_STRDUP(names[got++], networks) < 0)
E
Eric Blake 已提交
2979
                goto cleanup;
E
Eric Blake 已提交
2980 2981
            char_ptr++;
            networks = char_ptr;
E
Eduardo Otubo 已提交
2982 2983 2984 2985 2986
        } else {
            break;
        }
    }

2987
 cleanup:
E
Eric Blake 已提交
2988 2989 2990 2991
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);
    }
E
Eduardo Otubo 已提交
2992 2993 2994 2995 2996
    VIR_FREE(ret);
    return got;
}

static int
2997
phypConnectNumOfInterfaces(virConnectPtr conn)
E
Eduardo Otubo 已提交
2998 2999
{
    phyp_driverPtr phyp_driver = conn->privateData;
3000
    LIBSSH2_SESSION *session = phyp_driver->session;
E
Eduardo Otubo 已提交
3001 3002 3003
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
E
Eric Blake 已提交
3004
    int nnets = -1;
E
Eduardo Otubo 已提交
3005 3006 3007 3008
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
3009
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
3010

3011
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
3012 3013
                      "-r virtualio --rsubtype eth --level lpar|"
                      "grep -v lpar_id=%d|grep -c lpar_name", vios_id);
E
Eric Blake 已提交
3014
    phypExecInt(session, &buf, conn, &nnets);
E
Eduardo Otubo 已提交
3015 3016 3017
    return nnets;
}

3018 3019
static int
phypGetLparState(virConnectPtr conn, unsigned int lpar_id)
3020 3021
{
    phyp_driverPtr phyp_driver = conn->privateData;
3022
    LIBSSH2_SESSION *session = phyp_driver->session;
3023 3024 3025 3026 3027 3028
    int system_type = phyp_driver->system_type;
    char *ret = NULL;
    int exit_status = 0;
    char *managed_system = phyp_driver->managed_system;
    int state = VIR_DOMAIN_NOSTATE;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3029

3030 3031
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
3032 3033
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F state --filter lpar_ids=%d", lpar_id);
3034
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
3035

3036 3037
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3038

3039 3040 3041 3042 3043 3044
    if (STREQ(ret, "Running"))
        state = VIR_DOMAIN_RUNNING;
    else if (STREQ(ret, "Not Activated"))
        state = VIR_DOMAIN_SHUTOFF;
    else if (STREQ(ret, "Shutting Down"))
        state = VIR_DOMAIN_SHUTDOWN;
3045

3046
 cleanup:
3047 3048
    VIR_FREE(ret);
    return state;
3049 3050
}

3051 3052 3053 3054
/* XXX - is this needed? */
static int phypDiskType(virConnectPtr, char *) ATTRIBUTE_UNUSED;
static int
phypDiskType(virConnectPtr conn, char *backing_device)
3055 3056
{
    phyp_driverPtr phyp_driver = conn->privateData;
3057
    LIBSSH2_SESSION *session = phyp_driver->session;
3058 3059 3060 3061 3062 3063 3064
    int system_type = phyp_driver->system_type;
    char *ret = NULL;
    int exit_status = 0;
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    int disk_type = -1;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3065

3066 3067
    virBufferAddLit(&buf, "viosvrcmd");
    if (system_type == HMC)
3068 3069
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -p %d -c \"lssp -field name type "
E
Eric Blake 已提交
3070
                      "-fmt , -all|sed -n '/%s/ {\n s/^.*,//\n p\n}'\"",
3071
                      vios_id, backing_device);
3072
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
3073

3074 3075
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3076

3077
    if (STREQ(ret, "LVPOOL"))
E
Eric Blake 已提交
3078
        disk_type = VIR_STORAGE_TYPE_BLOCK;
3079
    else if (STREQ(ret, "FBPOOL"))
E
Eric Blake 已提交
3080
        disk_type = VIR_STORAGE_TYPE_FILE;
3081

3082
 cleanup:
3083 3084 3085
    VIR_FREE(ret);
    return disk_type;
}
3086

3087
static int
3088
phypConnectNumOfDefinedDomains(virConnectPtr conn)
3089
{
3090
    return phypConnectNumOfDomainsGeneric(conn, 1);
3091
}
3092

3093
static int
3094
phypConnectNumOfDomains(virConnectPtr conn)
3095
{
3096
    return phypConnectNumOfDomainsGeneric(conn, 0);
3097 3098
}

3099
static int
3100
phypConnectListDomains(virConnectPtr conn, int *ids, int nids)
3101
{
3102
    return phypConnectListDomainsGeneric(conn, ids, nids, 0);
3103
}
3104

3105
static int
3106
phypConnectListDefinedDomains(virConnectPtr conn, char **const names, int nnames)
3107
{
3108
    bool success = false;
3109
    phyp_driverPtr phyp_driver = conn->privateData;
3110
    LIBSSH2_SESSION *session = phyp_driver->session;
3111 3112 3113 3114
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
    int got = 0;
3115
    size_t i;
3116 3117
    char *ret = NULL;
    char *domains = NULL;
E
Eric Blake 已提交
3118
    char *char_ptr = NULL;
3119
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3120

3121 3122
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
3123
        virBufferAsprintf(&buf, " -m %s", managed_system);
3124
    virBufferAddLit(&buf, " -F name,state"
E
Eric Blake 已提交
3125
                      "|sed -n '/Not Activated/ {\n s/,.*$//\n p\n}'");
3126
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
3127

3128
    /* I need to parse the textual return in order to get the domains */
3129
    if (exit_status < 0 || ret == NULL) {
3130
        goto cleanup;
3131
    } else {
3132
        domains = ret;
3133

3134
        while (got < nnames) {
E
Eric Blake 已提交
3135
            char_ptr = strchr(domains, '\n');
3136

E
Eric Blake 已提交
3137 3138
            if (char_ptr) {
                *char_ptr = '\0';
3139
                if (VIR_STRDUP(names[got++], domains) < 0)
3140
                    goto cleanup;
E
Eric Blake 已提交
3141 3142
                char_ptr++;
                domains = char_ptr;
3143
            } else {
3144
                break;
3145
            }
3146
        }
3147 3148
    }

3149 3150
    success = true;

3151
 cleanup:
3152 3153 3154 3155 3156 3157
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);

        got = -1;
    }
3158
    VIR_FREE(ret);
3159
    return got;
3160 3161
}

3162 3163
static virDomainPtr
phypDomainLookupByName(virConnectPtr conn, const char *lpar_name)
3164
{
3165
    phyp_driverPtr phyp_driver = conn->privateData;
3166
    LIBSSH2_SESSION *session = phyp_driver->session;
3167 3168 3169 3170
    virDomainPtr dom = NULL;
    int lpar_id = 0;
    char *managed_system = phyp_driver->managed_system;
    unsigned char lpar_uuid[VIR_UUID_BUFLEN];
3171

3172 3173 3174
    lpar_id = phypGetLparID(session, managed_system, lpar_name, conn);
    if (lpar_id == -1)
        return NULL;
3175

3176 3177
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
        return NULL;
3178

3179
    dom = virGetDomain(conn, lpar_name, lpar_uuid, lpar_id);
3180 3181

    return dom;
3182 3183
}

3184 3185
static virDomainPtr
phypDomainLookupByID(virConnectPtr conn, int lpar_id)
3186
{
3187
    phyp_driverPtr phyp_driver = conn->privateData;
3188
    LIBSSH2_SESSION *session = phyp_driver->session;
3189 3190 3191
    virDomainPtr dom = NULL;
    char *managed_system = phyp_driver->managed_system;
    unsigned char lpar_uuid[VIR_UUID_BUFLEN];
E
Eduardo Otubo 已提交
3192

3193 3194
    char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
                                      conn);
3195

3196
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
3197
        goto cleanup;
3198

3199
    dom = virGetDomain(conn, lpar_name, lpar_uuid, lpar_id);
3200

3201
 cleanup:
3202
    VIR_FREE(lpar_name);
3203

3204
    return dom;
3205 3206
}

3207
static char *
3208
phypDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
3209
{
3210
    phyp_driverPtr phyp_driver = dom->conn->privateData;
3211
    LIBSSH2_SESSION *session = phyp_driver->session;
3212 3213
    virDomainDef def;
    char *managed_system = phyp_driver->managed_system;
3214
    unsigned long long memory;
3215
    unsigned int vcpus;
E
Eduardo Otubo 已提交
3216

3217
    virCheckFlags(VIR_DOMAIN_XML_COMMON_FLAGS, NULL);
3218

3219
    memset(&def, 0, sizeof(virDomainDef));
E
Eduardo Otubo 已提交
3220

3221 3222 3223 3224 3225 3226 3227
    def.virtType = VIR_DOMAIN_VIRT_PHYP;
    def.id = dom->id;

    char *lpar_name = phypGetLparNAME(session, managed_system, def.id,
                                      dom->conn);

    if (lpar_name == NULL) {
3228
        VIR_ERROR(_("Unable to determine domain's name."));
3229
        goto err;
E
Eduardo Otubo 已提交
3230 3231
    }

3232
    if (phypGetLparUUID(def.uuid, dom->id, dom->conn) == -1) {
3233
        VIR_ERROR(_("Unable to generate random uuid."));
E
Eduardo Otubo 已提交
3234 3235
        goto err;
    }
3236

3237
    if ((memory = phypGetLparMem(dom->conn, managed_system, dom->id, 0)) == 0) {
3238
        VIR_ERROR(_("Unable to determine domain's max memory."));
3239 3240
        goto err;
    }
3241

3242
    virDomainDefSetMemoryTotal(&def, memory);
3243

3244
    if ((def.mem.cur_balloon =
3245
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0) {
3246
        VIR_ERROR(_("Unable to determine domain's memory."));
3247 3248
        goto err;
    }
3249

3250
    if ((vcpus = phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0) {
3251
        VIR_ERROR(_("Unable to determine domain's CPU."));
3252
        goto err;
3253
    }
3254

3255
    if (virDomainDefSetVcpusMax(&def, vcpus, phyp_driver->xmlopt) < 0)
3256 3257
        goto err;

3258 3259
    if (virDomainDefSetVcpus(&def, vcpus) < 0)
        goto err;
3260

3261
    return virDomainDefFormat(&def, phyp_driver->caps,
3262
                              virDomainDefFormatConvertXMLFlags(flags));
3263

3264
 err:
3265 3266
    return NULL;
}
3267

3268 3269 3270
static int
phypDomainResume(virDomainPtr dom)
{
3271
    int result = -1;
3272
    phyp_driverPtr phyp_driver = dom->conn->privateData;
3273
    LIBSSH2_SESSION *session = phyp_driver->session;
3274 3275 3276 3277 3278
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3279

3280 3281
    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
3282 3283
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar -o on --id %d -f %s",
3284
                      dom->id, dom->name);
3285
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3286

3287
    if (exit_status < 0)
3288
        goto cleanup;
3289

3290
    result = 0;
3291

3292
 cleanup:
3293
    VIR_FREE(ret);
3294 3295

    return result;
3296 3297
}

3298
static int
E
Eric Blake 已提交
3299
phypDomainReboot(virDomainPtr dom, unsigned int flags)
3300 3301 3302 3303
{
    int result = -1;
    virConnectPtr conn = dom->conn;
    phyp_driverPtr phyp_driver = conn->privateData;
3304
    LIBSSH2_SESSION *session = phyp_driver->session;
3305 3306 3307 3308 3309 3310
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

E
Eric Blake 已提交
3311 3312
    virCheckFlags(0, -1);

3313 3314
    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
3315 3316
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
3317 3318 3319 3320 3321 3322 3323 3324 3325
                      " -r lpar -o shutdown --id %d --immed --restart",
                      dom->id);
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);

    if (exit_status < 0)
        goto cleanup;

    result = 0;

3326
 cleanup:
3327 3328 3329 3330 3331
    VIR_FREE(ret);

    return result;
}

3332 3333
static int
phypDomainShutdown(virDomainPtr dom)
3334
{
3335
    int result = -1;
3336 3337
    virConnectPtr conn = dom->conn;
    phyp_driverPtr phyp_driver = conn->privateData;
3338
    LIBSSH2_SESSION *session = phyp_driver->session;
3339 3340 3341 3342 3343 3344 3345 3346
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
3347 3348
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar -o shutdown --id %d", dom->id);
3349
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3350 3351

    if (exit_status < 0)
3352
        goto cleanup;
3353

3354
    result = 0;
3355

3356
 cleanup:
3357
    VIR_FREE(ret);
3358 3359

    return result;
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
}

static int
phypDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
{
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    char *managed_system = phyp_driver->managed_system;

    info->state = phypGetLparState(dom->conn, dom->id);

    if ((info->maxMem =
         phypGetLparMem(dom->conn, managed_system, dom->id, 0)) == 0)
3372
        VIR_WARN("Unable to determine domain's max memory.");
3373 3374 3375

    if ((info->memory =
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0)
3376
        VIR_WARN("Unable to determine domain's memory.");
3377 3378 3379

    if ((info->nrVirtCpu =
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
3380
        VIR_WARN("Unable to determine domain's CPU.");
3381 3382 3383 3384

    return 0;
}

3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399
static int
phypDomainGetState(virDomainPtr dom,
                   int *state,
                   int *reason,
                   unsigned int flags)
{
    virCheckFlags(0, -1);

    *state = phypGetLparState(dom->conn, dom->id);
    if (reason)
        *reason = 0;

    return 0;
}

3400
static int
3401 3402
phypDomainDestroyFlags(virDomainPtr dom,
                       unsigned int flags)
3403
{
3404
    int result = -1;
3405
    phyp_driverPtr phyp_driver = dom->conn->privateData;
3406
    LIBSSH2_SESSION *session = phyp_driver->session;
3407 3408 3409 3410 3411 3412
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

3413 3414
    virCheckFlags(0, -1);

3415 3416
    virBufferAddLit(&buf, "rmsyscfg");
    if (system_type == HMC)
3417 3418
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar --id %d", dom->id);
3419
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3420 3421

    if (exit_status < 0)
3422
        goto cleanup;
3423 3424

    if (phypUUIDTable_RemLpar(dom->conn, dom->id) == -1)
3425
        goto cleanup;
3426

3427
    dom->id = -1;
3428
    result = 0;
3429

3430
 cleanup:
3431 3432
    VIR_FREE(ret);

3433
    return result;
3434
}
3435

3436 3437 3438 3439 3440 3441
static int
phypDomainDestroy(virDomainPtr dom)
{
    return phypDomainDestroyFlags(dom, 0);
}

3442 3443
static int
phypBuildLpar(virConnectPtr conn, virDomainDefPtr def)
3444
{
3445
    int result = -1;
3446
    phyp_driverPtr phyp_driver = conn->privateData;
3447
    LIBSSH2_SESSION *session = phyp_driver->session;
3448 3449 3450 3451 3452
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3453

3454
    if (!def->mem.cur_balloon) {
3455
        virReportError(VIR_ERR_XML_ERROR, "%s",
3456 3457
                       _("Field <currentMemory> on the domain XML file is "
                         "missing or has invalid value"));
3458
        goto cleanup;
3459 3460
    }

3461
    if (!virDomainDefGetMemoryInitial(def)) {
3462
        virReportError(VIR_ERR_XML_ERROR, "%s",
3463 3464
                       _("Field <memory> on the domain XML file is missing or "
                         "has invalid value"));
3465
        goto cleanup;
3466 3467
    }

3468
    if (def->ndisks < 1) {
3469 3470
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Domain XML must contain at least one <disk> element."));
3471
        goto cleanup;
3472 3473
    }

3474
    if (!virDomainDiskGetSource(def->disks[0])) {
3475 3476 3477
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Field <src> under <disk> on the domain XML file is "
                         "missing."));
3478
        goto cleanup;
3479 3480
    }

3481 3482
    virBufferAddLit(&buf, "mksyscfg");
    if (system_type == HMC)
3483
        virBufferAsprintf(&buf, " -m %s", managed_system);
3484
    virBufferAsprintf(&buf, " -r lpar -p %s -i min_mem=%lld,desired_mem=%lld,"
3485
                      "max_mem=%lld,desired_procs=%u,virtual_scsi_adapters=%s",
3486
                      def->name, def->mem.cur_balloon,
3487 3488
                      def->mem.cur_balloon,
                      virDomainDefGetMemoryInitial(def),
3489 3490
                      virDomainDefGetVcpus(def),
                      virDomainDiskGetSource(def->disks[0]));
3491
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
3492

3493
    if (exit_status < 0) {
3494
        VIR_ERROR(_("Unable to create LPAR. Reason: '%s'"), NULLSTR(ret));
3495
        goto cleanup;
3496
    }
3497

3498
    if (phypUUIDTable_AddLpar(conn, def->uuid, def->id) == -1) {
3499
        VIR_ERROR(_("Unable to add LPAR to the table"));
3500
        goto cleanup;
3501
    }
3502

3503
    result = 0;
3504

3505
 cleanup:
3506
    VIR_FREE(ret);
3507 3508

    return result;
3509
}
3510

3511
static virDomainPtr
3512 3513
phypDomainCreateXML(virConnectPtr conn,
                    const char *xml, unsigned int flags)
3514
{
E
Eduardo Otubo 已提交
3515
    virCheckFlags(0, NULL);
3516

3517 3518
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = phyp_driver->session;
3519 3520 3521 3522
    virDomainDefPtr def = NULL;
    virDomainPtr dom = NULL;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    lparPtr *lpars = uuid_table->lpars;
3523
    size_t i = 0;
3524
    char *managed_system = phyp_driver->managed_system;
3525
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
3526

3527 3528 3529
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
3530
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
3531

3532 3533
    if (!(def = virDomainDefParseString(xml, phyp_driver->caps,
                                        phyp_driver->xmlopt,
3534
                                        NULL,
3535
                                        parse_flags)))
3536 3537 3538
        goto err;

    /* checking if this name already exists on this system */
3539
    if (phypGetLparID(session, managed_system, def->name, conn) != -1) {
3540
        VIR_WARN("LPAR name already exists.");
3541 3542 3543 3544 3545 3546
        goto err;
    }

    /* checking if ID or UUID already exists on this system */
    for (i = 0; i < uuid_table->nlpars; i++) {
        if (lpars[i]->id == def->id || lpars[i]->uuid == def->uuid) {
3547
            VIR_WARN("LPAR ID or UUID already exists.");
3548 3549 3550 3551
            goto err;
        }
    }

3552
    if ((dom = virGetDomain(conn, def->name, def->uuid, def->id)) == NULL)
3553 3554 3555 3556 3557 3558 3559 3560 3561 3562
        goto err;

    if (phypBuildLpar(conn, def) == -1)
        goto err;

    if (phypDomainResume(dom) == -1)
        goto err;

    return dom;

3563
 err:
3564
    virDomainDefFree(def);
3565
    virObjectUnref(dom);
3566 3567 3568 3569 3570 3571 3572 3573
    return NULL;
}

static char *
phypConnectGetCapabilities(virConnectPtr conn)
{
    phyp_driverPtr phyp_driver = conn->privateData;

3574
    return virCapabilitiesFormatXML(phyp_driver->caps);
3575 3576 3577
}

static int
3578 3579
phypDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                        unsigned int flags)
3580 3581
{
    phyp_driverPtr phyp_driver = dom->conn->privateData;
3582
    LIBSSH2_SESSION *session = phyp_driver->session;
3583 3584 3585 3586 3587 3588 3589 3590 3591
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
    char *ret = NULL;
    char operation;
    unsigned long ncpus = 0;
    unsigned int amount = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

3592
    if (flags != VIR_DOMAIN_VCPU_LIVE) {
3593
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
3594 3595 3596
        return -1;
    }

3597 3598 3599
    if ((ncpus = phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
        return 0;

3600
    if (nvcpus > phypDomainGetMaxVcpus(dom)) {
3601
        VIR_ERROR(_("You are trying to set a number of CPUs bigger than "
3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
                     "the max possible."));
        return 0;
    }

    if (ncpus > nvcpus) {
        operation = 'r';
        amount = nvcpus - ncpus;
    } else if (ncpus < nvcpus) {
        operation = 'a';
        amount = nvcpus - ncpus;
3612
    } else {
3613
        return 0;
3614
    }
3615 3616 3617

    virBufferAddLit(&buf, "chhwres -r proc");
    if (system_type == HMC)
3618 3619
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --id %d -o %c --procunits %d 2>&1 |sed "
3620 3621
                      "-e 's/^.*\\([0-9][0-9]*.[0-9][0-9]*\\).*$/\\1/'",
                      dom->id, operation, amount);
3622
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3623 3624

    if (exit_status < 0) {
3625
        VIR_ERROR(_
3626 3627 3628 3629 3630 3631
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    " Contact your support to enable this feature."));
    }

    VIR_FREE(ret);
    return 0;
3632 3633

}
3634

3635
static int
3636
phypDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
3637 3638 3639 3640
{
    return phypDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_VCPU_LIVE);
}

3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665
static int
phypDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    LIBSSH2_SESSION *session = phyp_driver->session;
    char *managed_system = phyp_driver->managed_system;
    char *lpar_name = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    lpar_name = phypGetLparNAME(session, managed_system, dom->id, dom->conn);

    if (lpar_name == NULL) {
        VIR_ERROR(_("Unable to determine domain's name."));
        goto cleanup;
    }

    ret = 0;

 cleanup:
    VIR_FREE(lpar_name);
    return ret;
}

3666
static virHypervisorDriver phypHypervisorDriver = {
3667
    .name = "PHYP",
3668 3669
    .connectOpen = phypConnectOpen, /* 0.7.0 */
    .connectClose = phypConnectClose, /* 0.7.0 */
3670
    .connectGetCapabilities = phypConnectGetCapabilities, /* 0.7.3 */
3671 3672 3673
    .connectListDomains = phypConnectListDomains, /* 0.7.0 */
    .connectNumOfDomains = phypConnectNumOfDomains, /* 0.7.0 */
    .domainCreateXML = phypDomainCreateXML, /* 0.7.3 */
3674 3675 3676 3677 3678 3679
    .domainLookupByID = phypDomainLookupByID, /* 0.7.0 */
    .domainLookupByName = phypDomainLookupByName, /* 0.7.0 */
    .domainResume = phypDomainResume, /* 0.7.0 */
    .domainShutdown = phypDomainShutdown, /* 0.7.0 */
    .domainReboot = phypDomainReboot, /* 0.9.1 */
    .domainDestroy = phypDomainDestroy, /* 0.7.3 */
3680
    .domainDestroyFlags = phypDomainDestroyFlags, /* 0.9.4 */
3681 3682
    .domainGetInfo = phypDomainGetInfo, /* 0.7.0 */
    .domainGetState = phypDomainGetState, /* 0.9.2 */
3683
    .domainSetVcpus = phypDomainSetVcpus, /* 0.7.3 */
3684 3685
    .domainSetVcpusFlags = phypDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = phypDomainGetVcpusFlags, /* 0.8.5 */
3686
    .domainGetMaxVcpus = phypDomainGetMaxVcpus, /* 0.7.3 */
3687
    .domainGetXMLDesc = phypDomainGetXMLDesc, /* 0.7.0 */
3688 3689 3690 3691 3692 3693 3694
    .connectListDefinedDomains = phypConnectListDefinedDomains, /* 0.7.0 */
    .connectNumOfDefinedDomains = phypConnectNumOfDefinedDomains, /* 0.7.0 */
    .domainAttachDevice = phypDomainAttachDevice, /* 0.8.2 */
    .connectIsEncrypted = phypConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = phypConnectIsSecure, /* 0.7.3 */
    .domainIsUpdated = phypDomainIsUpdated, /* 0.8.6 */
    .connectIsAlive = phypConnectIsAlive, /* 0.9.8 */
3695
    .domainHasManagedSaveImage = phypDomainHasManagedSaveImage, /* 1.2.13 */
3696 3697
};

3698
static virStorageDriver phypStorageDriver = {
3699 3700
    .connectNumOfStoragePools = phypConnectNumOfStoragePools, /* 0.8.2 */
    .connectListStoragePools = phypConnectListStoragePools, /* 0.8.2 */
3701
    .storagePoolLookupByName = phypStoragePoolLookupByName, /* 0.8.2 */
3702
    .storagePoolLookupByUUID = phypStoragePoolLookupByUUID, /* 0.8.2 */
3703
    .storagePoolCreateXML = phypStoragePoolCreateXML, /* 0.8.2 */
3704 3705
    .storagePoolDestroy = phypStoragePoolDestroy, /* 0.8.2 */
    .storagePoolGetXMLDesc = phypStoragePoolGetXMLDesc, /* 0.8.2 */
3706 3707 3708
    .storagePoolNumOfVolumes = phypStoragePoolNumOfVolumes, /* 0.8.2 */
    .storagePoolListVolumes = phypStoragePoolListVolumes, /* 0.8.2 */

3709 3710
    .storageVolLookupByName = phypStorageVolLookupByName, /* 0.8.2 */
    .storageVolLookupByPath = phypStorageVolLookupByPath, /* 0.8.2 */
3711
    .storageVolCreateXML = phypStorageVolCreateXML, /* 0.8.2 */
3712 3713
    .storageVolGetXMLDesc = phypStorageVolGetXMLDesc, /* 0.8.2 */
    .storageVolGetPath = phypStorageVolGetPath, /* 0.8.2 */
3714 3715
};

E
Eduardo Otubo 已提交
3716
static virInterfaceDriver phypInterfaceDriver = {
3717 3718
    .connectNumOfInterfaces = phypConnectNumOfInterfaces, /* 0.9.1 */
    .connectListInterfaces = phypConnectListInterfaces, /* 0.9.1 */
3719 3720 3721 3722
    .interfaceLookupByName = phypInterfaceLookupByName, /* 0.9.1 */
    .interfaceDefineXML = phypInterfaceDefineXML, /* 0.9.1 */
    .interfaceDestroy = phypInterfaceDestroy, /* 0.9.1 */
    .interfaceIsActive = phypInterfaceIsActive /* 0.9.1 */
3723 3724
};

3725
static virConnectDriver phypConnectDriver = {
3726
    .remoteOnly = true,
3727
    .uriSchemes = (const char *[]){ "phyp", NULL },
3728 3729 3730 3731 3732
    .hypervisorDriver = &phypHypervisorDriver,
    .interfaceDriver = &phypInterfaceDriver,
    .storageDriver = &phypStorageDriver,
};

3733 3734 3735
int
phypRegister(void)
{
3736 3737
    return virRegisterConnectDriver(&phypConnectDriver,
                                    false);
3738
}