phyp_driver.c 112.1 KB
Newer Older
1
/*
E
Eric Blake 已提交
2
 * Copyright (C) 2010-2011 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * Copyright IBM Corp. 2009
 *
 * phyp_driver.c: ssh layer to access Power Hypervisors
 *
 * Authors:
 *  Eduardo Otubo <otubo at linux.vnet.ibm.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <config.h>

#include <sys/types.h>
28
#include <sys/stat.h>
29 30 31 32 33 34 35 36
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
37 38 39 40 41
#include <libssh2.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
42 43 44
#include <fcntl.h>
#include <sys/utsname.h>
#include <domain_event.h>
45 46

#include "internal.h"
47
#include "authhelper.h"
48 49 50 51 52 53 54 55 56 57
#include "util.h"
#include "datatypes.h"
#include "buf.h"
#include "memory.h"
#include "logging.h"
#include "driver.h"
#include "libvirt/libvirt.h"
#include "virterror_internal.h"
#include "uuid.h"
#include "domain_conf.h"
58
#include "storage_conf.h"
59
#include "nodeinfo.h"
60
#include "files.h"
E
Eduardo Otubo 已提交
61
#include "interface_conf.h"
62 63 64 65 66

#include "phyp_driver.h"

#define VIR_FROM_THIS VIR_FROM_PHYP

67
#define PHYP_ERROR(code, ...)                                                 \
68
    virReportErrorHelper(VIR_FROM_PHYP, code, __FILE__, __FUNCTION__,         \
69
                         __LINE__, __VA_ARGS__)
70

71 72 73 74
/*
 * URI: phyp://user@[hmc|ivm]/managed_system
 * */

75 76
static unsigned const int HMC = 0;
static unsigned const int IVM = 127;
E
Eduardo Otubo 已提交
77 78
static unsigned const int PHYP_IFACENAME_SIZE = 24;
static unsigned const int PHYP_MAC_SIZE= 12;
79

80 81 82 83 84 85 86 87 88
static int
waitsocket(int socket_fd, LIBSSH2_SESSION * session)
{
    struct timeval timeout;
    int rc;
    fd_set fd;
    fd_set *writefd = NULL;
    fd_set *readfd = NULL;
    int dir;
89

90 91
    timeout.tv_sec = 0;
    timeout.tv_usec = 1000;
92

93
    FD_ZERO(&fd);
94

95
    FD_SET(socket_fd, &fd);
96

97 98
    /* now make sure we wait in the correct direction */
    dir = libssh2_session_block_directions(session);
99

100 101
    if (dir & LIBSSH2_SESSION_BLOCK_INBOUND)
        readfd = &fd;
102

103 104
    if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
        writefd = &fd;
105

106
    rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
107

108 109
    return rc;
}
110

111 112
/* this function is the layer that manipulates the ssh channel itself
 * and executes the commands on the remote machine */
113 114 115
static char *phypExec(LIBSSH2_SESSION *, const char *, int *, virConnectPtr)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
    ATTRIBUTE_NONNULL(4);
116
static char *
117
phypExec(LIBSSH2_SESSION *session, const char *cmd, int *exit_status,
118 119 120 121 122
         virConnectPtr conn)
{
    LIBSSH2_CHANNEL *channel;
    ConnectionData *connection_data = conn->networkPrivateData;
    virBuffer tex_ret = VIR_BUFFER_INITIALIZER;
123 124
    char *buffer = NULL;
    size_t buffer_size = 16384;
125 126 127 128
    int exitcode;
    int bytecount = 0;
    int sock = connection_data->sock;
    int rc = 0;
129

130 131 132 133 134
    if (VIR_ALLOC_N(buffer, buffer_size) < 0) {
        virReportOOMError();
        return NULL;
    }

135 136 137 138 139
    /* 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) {
        waitsocket(sock, session);
140 141
    }

142 143
    if (channel == NULL) {
        goto err;
144
    }
145

146 147 148
    while ((rc = libssh2_channel_exec(channel, cmd)) ==
           LIBSSH2_ERROR_EAGAIN) {
        waitsocket(sock, session);
149
    }
150

151 152 153
    if (rc != 0) {
        goto err;
    }
154

155 156 157
    for (;;) {
        /* loop until we block */
        do {
158
            rc = libssh2_channel_read(channel, buffer, buffer_size);
159 160
            if (rc > 0) {
                bytecount += rc;
161
                virBufferAdd(&tex_ret, buffer, -1);
162 163 164
            }
        }
        while (rc > 0);
165

166 167 168 169 170 171 172
        /* this is due to blocking that would occur otherwise so we loop on
         * this condition */
        if (rc == LIBSSH2_ERROR_EAGAIN) {
            waitsocket(sock, session);
        } else {
            break;
        }
E
Eduardo Otubo 已提交
173 174
    }

175
    exitcode = 127;
176

177 178
    while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) {
        waitsocket(sock, session);
179 180
    }

181 182
    if (rc == 0) {
        exitcode = libssh2_channel_get_exit_status(channel);
183 184
    }

185 186 187
    (*exit_status) = exitcode;
    libssh2_channel_free(channel);
    channel = NULL;
188 189
    VIR_FREE(buffer);

190 191 192 193 194 195
    if (virBufferError(&tex_ret)) {
        virBufferFreeAndReset(&tex_ret);
        virReportOOMError();
        return NULL;
    }
    return virBufferContentAndReset(&tex_ret);
196 197 198 199 200 201

err:
    (*exit_status) = SSH_CMD_ERR;
    virBufferFreeAndReset(&tex_ret);
    VIR_FREE(buffer);
    return NULL;
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
/* 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;

    if (virBufferError(buf)) {
        virBufferFreeAndReset(buf);
        virReportOOMError();
        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 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
/* 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;
}

254
static int
255
phypGetSystemType(virConnectPtr conn)
256 257
{
    ConnectionData *connection_data = conn->networkPrivateData;
258
    LIBSSH2_SESSION *session = connection_data->session;
259 260 261
    char *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
262

263 264
    if (virAsprintf(&cmd, "lshmc -V") < 0) {
        virReportOOMError();
265
        return -1;
266 267
    }
    ret = phypExec(session, cmd, &exit_status, conn);
268

269 270 271
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return exit_status;
272 273
}

274
static int
275
phypGetVIOSPartitionID(virConnectPtr conn)
276
{
277 278 279 280 281 282 283
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
    int id = -1;
    char *managed_system = phyp_driver->managed_system;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
284

285 286
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
287
        virBufferAsprintf(&buf, " -m %s", managed_system);
E
Eric Blake 已提交
288 289
    virBufferAddLit(&buf, " -r lpar -F lpar_id,lpar_env"
                    "|sed -n '/vioserver/ {\n s/,.*$//\n p\n}'");
E
Eric Blake 已提交
290
    phypExecInt(session, &buf, conn, &id);
291
    return id;
292
}
293

294 295 296 297 298 299
static virCapsPtr
phypCapsInit(void)
{
    struct utsname utsname;
    virCapsPtr caps;
    virCapsGuestPtr guest;
300

301
    uname(&utsname);
302

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

306 307 308 309 310 311
    /* Some machines have problematic NUMA toplogy causing
     * unexpected failures. We don't want to break the QEMU
     * driver in this scenario, so log errors & carry on
     */
    if (nodeCapsInitNUMA(caps) < 0) {
        virCapabilitiesFreeNUMAInfo(caps);
312
        VIR_WARN
313
            ("Failed to query host NUMA topology, disabling NUMA capabilities");
314 315
    }

316 317 318
    /* XXX shouldn't 'borrow' KVM's prefix */
    virCapabilitiesSetMacPrefix(caps, (unsigned char[]) {
                                0x52, 0x54, 0x00});
319

320 321 322 323 324 325
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "linux",
                                         utsname.machine,
                                         sizeof(int) == 4 ? 32 : 8,
                                         NULL, NULL, 0, NULL)) == NULL)
        goto no_memory;
326

327 328 329
    if (virCapabilitiesAddGuestDomain(guest,
                                      "phyp", NULL, NULL, 0, NULL) == NULL)
        goto no_memory;
330

331
    return caps;
332

333
no_memory:
334 335 336
    virCapabilitiesFree(caps);
    return NULL;
}
337

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
/* 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
phypNumDomainsGeneric(virConnectPtr conn, unsigned int type)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
353
    int ndom = -1;
354 355 356
    char *managed_system = phyp_driver->managed_system;
    const char *state;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
357

358 359 360 361 362 363 364
    if (type == 0)
        state = "|grep Running";
    else if (type == 1) {
        if (system_type == HMC) {
            state = "|grep \"Not Activated\"";
        } else {
            state = "|grep \"Open Firmware\"";
365
        }
366 367
    } else
        state = " ";
368

369 370
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
371 372
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F lpar_id,state %s |grep -c '^[0-9][0-9]*'",
373
                      state);
E
Eric Blake 已提交
374
    phypExecInt(session, &buf, conn, &ndom);
375
    return ndom;
376 377
}

378 379 380 381 382 383 384 385 386 387
/* 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
phypListDomainsGeneric(virConnectPtr conn, int *ids, int nids,
                       unsigned int type)
388
{
389
    ConnectionData *connection_data = conn->networkPrivateData;
E
Eduardo Otubo 已提交
390
    phyp_driverPtr phyp_driver = conn->privateData;
391
    LIBSSH2_SESSION *session = connection_data->session;
E
Eduardo Otubo 已提交
392
    int system_type = phyp_driver->system_type;
393
    char *managed_system = phyp_driver->managed_system;
394
    int exit_status = 0;
395
    int got = -1;
396
    char *ret = NULL;
397
    char *line, *next_line;
398
    const char *state;
E
Eduardo Otubo 已提交
399 400
    virBuffer buf = VIR_BUFFER_INITIALIZER;

401 402 403 404 405
    if (type == 0)
        state = "|grep Running";
    else
        state = " ";

E
Eduardo Otubo 已提交
406 407
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
408 409
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F lpar_id,state %s | sed -e 's/,.*$//'",
410
                      state);
411
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
412

413
    if (exit_status < 0 || ret == NULL)
414
        goto cleanup;
415 416 417 418 419 420 421 422

    /* 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;
423
            goto cleanup;
424
        }
425 426 427 428
        got++;
        line = next_line;
        while (*line == '\n')
            line++; /* skip \n */
429
    }
430

431
cleanup:
432
    VIR_FREE(ret);
433
    return got;
434 435
}

436 437
static int
phypUUIDTable_WriteFile(virConnectPtr conn)
438
{
439 440
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
441
    unsigned int i = 0;
442 443 444 445 446
    int fd = -1;
    char local_file[] = "./uuid_table";

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

448
    for (i = 0; i < uuid_table->nlpars; i++) {
449 450 451
        if (safewrite(fd, &uuid_table->lpars[i]->id,
                      sizeof(uuid_table->lpars[i]->id)) !=
            sizeof(uuid_table->lpars[i]->id)) {
452
            VIR_ERROR(_("Unable to write information to local file."));
453 454 455 456 457
            goto err;
        }

        if (safewrite(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN) !=
            VIR_UUID_BUFLEN) {
458
            VIR_ERROR(_("Unable to write information to local file."));
459
            goto err;
460 461 462
        }
    }

463 464 465 466 467
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno, _("Could not close %s"),
                             local_file);
        goto err;
    }
468 469
    return 0;

470
err:
471
    VIR_FORCE_CLOSE(fd);
472 473 474
    return -1;
}

475 476
static int
phypUUIDTable_Push(virConnectPtr conn)
477 478
{
    ConnectionData *connection_data = conn->networkPrivateData;
479
    LIBSSH2_SESSION *session = connection_data->session;
480 481 482 483 484 485 486 487 488 489
    LIBSSH2_CHANNEL *channel = NULL;
    virBuffer username = VIR_BUFFER_INITIALIZER;
    struct stat local_fileinfo;
    char buffer[1024];
    int rc = 0;
    FILE *fd;
    size_t nread, sent;
    char *ptr;
    char local_file[] = "./uuid_table";
    char *remote_file = NULL;
490

491
    if (conn->uri->user != NULL) {
492
        virBufferAdd(&username, conn->uri->user, -1);
E
Eduardo Otubo 已提交
493

494 495 496 497 498 499 500 501 502 503 504
        if (virBufferError(&username)) {
            virBufferFreeAndReset(&username);
            virReportOOMError();
            goto err;
        }
    }

    if (virAsprintf
        (&remote_file, "/home/%s/libvirt_uuid_table",
         virBufferContentAndReset(&username))
        < 0) {
E
Eduardo Otubo 已提交
505
        virReportOOMError();
506
        goto err;
507 508
    }

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

514
    if (!(fd = fopen(local_file, "rb"))) {
515
        VIR_WARN("Unable to open local file.");
516
        goto err;
517
    }
518

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

525 526 527 528
        if ((!channel) && (libssh2_session_last_errno(session) !=
                           LIBSSH2_ERROR_EAGAIN))
            goto err;
    } while (!channel);
529

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

544 545 546 547 548 549 550 551 552 553 554 555 556
        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);
557

558 559 560 561 562 563 564 565
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
    virBufferFreeAndReset(&username);
566 567
    return 0;

568
err:
569 570 571 572 573 574 575 576
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
    return -1;
577 578 579
}

static int
580
phypUUIDTable_RemLpar(virConnectPtr conn, int id)
581
{
E
Eduardo Otubo 已提交
582
    phyp_driverPtr phyp_driver = conn->privateData;
583 584
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    unsigned int i = 0;
E
Eduardo Otubo 已提交
585

586 587 588 589 590
    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);
        }
591 592
    }

593
    if (phypUUIDTable_WriteFile(conn) == -1)
594 595
        goto err;

596
    if (phypUUIDTable_Push(conn) == -1)
597 598
        goto err;

599
    return 0;
600

601
err:
602
    return -1;
603 604
}

605 606
static int
phypUUIDTable_AddLpar(virConnectPtr conn, unsigned char *uuid, int id)
607
{
E
Eduardo Otubo 已提交
608
    phyp_driverPtr phyp_driver = conn->privateData;
609
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
E
Eduardo Otubo 已提交
610

611 612 613 614 615
    uuid_table->nlpars++;
    unsigned int i = uuid_table->nlpars;
    i--;

    if (VIR_REALLOC_N(uuid_table->lpars, uuid_table->nlpars) < 0) {
616
        virReportOOMError();
617
        goto err;
618 619
    }

620 621
    if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
        virReportOOMError();
622
        goto err;
623
    }
624

625
    uuid_table->lpars[i]->id = id;
626
    memcpy(uuid_table->lpars[i]->uuid, uuid, VIR_UUID_BUFLEN);
627

628 629
    if (phypUUIDTable_WriteFile(conn) == -1)
        goto err;
630

631
    if (phypUUIDTable_Push(conn) == -1)
632 633
        goto err;

634
    return 0;
635

636
err:
637
    return -1;
638 639
}

640 641
static int
phypUUIDTable_ReadFile(virConnectPtr conn)
642
{
E
Eduardo Otubo 已提交
643
    phyp_driverPtr phyp_driver = conn->privateData;
644 645 646 647 648 649
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    unsigned int i = 0;
    int fd = -1;
    char local_file[] = "./uuid_table";
    int rc = 0;
    int id;
650

651
    if ((fd = open(local_file, O_RDONLY)) == -1) {
652
        VIR_WARN("Unable to write information to local file.");
653
        goto err;
654 655
    }

656 657 658
    /* 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++) {
659

660 661 662 663 664 665 666 667
            rc = read(fd, &id, sizeof(int));
            if (rc == sizeof(int)) {
                if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
                    virReportOOMError();
                    goto err;
                }
                uuid_table->lpars[i]->id = id;
            } else {
668
                VIR_WARN
669 670 671
                    ("Unable to read from information to local file.");
                goto err;
            }
672

673 674
            rc = read(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN);
            if (rc != VIR_UUID_BUFLEN) {
675
                VIR_WARN("Unable to read information to local file.");
676 677
                goto err;
            }
678
        }
679 680
    } else
        virReportOOMError();
681

682
    VIR_FORCE_CLOSE(fd);
683
    return 0;
684

685
err:
686
    VIR_FORCE_CLOSE(fd);
687
    return -1;
688 689
}

690 691
static int
phypUUIDTable_Pull(virConnectPtr conn)
692 693
{
    ConnectionData *connection_data = conn->networkPrivateData;
694
    LIBSSH2_SESSION *session = connection_data->session;
695 696 697 698 699 700 701 702 703 704 705 706
    LIBSSH2_CHANNEL *channel = NULL;
    virBuffer username = VIR_BUFFER_INITIALIZER;
    struct stat fileinfo;
    char buffer[1024];
    int rc = 0;
    int fd;
    int got = 0;
    int amount = 0;
    int total = 0;
    int sock = 0;
    char local_file[] = "./uuid_table";
    char *remote_file = NULL;
E
Eduardo Otubo 已提交
707

708
    if (conn->uri->user != NULL) {
709
        virBufferAdd(&username, conn->uri->user, -1);
710

711 712 713 714 715 716
        if (virBufferError(&username)) {
            virBufferFreeAndReset(&username);
            virReportOOMError();
            goto err;
        }
    }
717

718 719 720 721 722 723 724
    if (virAsprintf
        (&remote_file, "/home/%s/libvirt_uuid_table",
         virBufferContentAndReset(&username))
        < 0) {
        virReportOOMError();
        goto err;
    }
725

726 727 728
    /* Trying to stat the remote file. */
    do {
        channel = libssh2_scp_recv(session, remote_file, &fileinfo);
729

730 731 732 733 734 735 736 737 738
        if (!channel) {
            if (libssh2_session_last_errno(session) !=
                LIBSSH2_ERROR_EAGAIN) {
                goto err;;
            } else {
                waitsocket(sock, session);
            }
        }
    } while (!channel);
739

740 741 742
    /* Creating a new data base based on remote file */
    if ((fd = creat(local_file, 0755)) == -1)
        goto err;
743

744 745 746 747
    /* Request a file via SCP */
    while (got < fileinfo.st_size) {
        do {
            amount = sizeof(buffer);
748

749 750 751
            if ((fileinfo.st_size - got) < amount) {
                amount = fileinfo.st_size - got;
            }
E
Eduardo Otubo 已提交
752

753 754 755
            rc = libssh2_channel_read(channel, buffer, amount);
            if (rc > 0) {
                if (safewrite(fd, buffer, rc) != rc)
756
                    VIR_WARN
757
                        ("Unable to write information to local file.");
758

759 760 761 762
                got += rc;
                total += rc;
            }
        } while (rc > 0);
763

764 765 766 767
        if ((rc == LIBSSH2_ERROR_EAGAIN)
            && (got < fileinfo.st_size)) {
            /* this is due to blocking that would occur otherwise
             * so we loop on this condition */
768

769 770 771 772 773
            waitsocket(sock, session);  /* now we wait */
            continue;
        }
        break;
    }
774 775 776 777 778
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno, _("Could not close %s"),
                             local_file);
        goto err;
    }
779

780 781 782 783 784 785 786 787 788
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
    virBufferFreeAndReset(&username);
    return 0;
789

790
err:
791 792 793 794 795 796 797
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
798 799 800
    return -1;
}

801 802
static int
phypUUIDTable_Init(virConnectPtr conn)
803
{
E
Eric Blake 已提交
804
    uuid_tablePtr uuid_table = NULL;
805 806 807 808 809
    phyp_driverPtr phyp_driver;
    int nids_numdomains = 0;
    int nids_listdomains = 0;
    int *ids = NULL;
    unsigned int i = 0;
E
Eric Blake 已提交
810 811
    int ret = -1;
    bool table_created = false;
E
Eduardo Otubo 已提交
812

813
    if ((nids_numdomains = phypNumDomainsGeneric(conn, 2)) < 0)
E
Eric Blake 已提交
814
        goto cleanup;
815 816

    if (VIR_ALLOC_N(ids, nids_numdomains) < 0) {
817
        virReportOOMError();
E
Eric Blake 已提交
818
        goto cleanup;
819 820
    }

821 822
    if ((nids_listdomains =
         phypListDomainsGeneric(conn, ids, nids_numdomains, 1)) < 0)
E
Eric Blake 已提交
823
        goto cleanup;
824

825
    /* exit early if there are no domains */
E
Eric Blake 已提交
826 827 828 829 830
    if (nids_numdomains == 0 && nids_listdomains == 0) {
        ret = 0;
        goto cleanup;
    }
    if (nids_numdomains != nids_listdomains) {
831
        VIR_ERROR(_("Unable to determine number of domains."));
E
Eric Blake 已提交
832
        goto cleanup;
833
    }
834

835 836 837
    phyp_driver = conn->privateData;
    uuid_table = phyp_driver->uuid_table;
    uuid_table->nlpars = nids_listdomains;
838

839 840 841
    /* 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 已提交
842
        table_created = true;
843 844 845 846
        if (VIR_ALLOC_N(uuid_table->lpars, uuid_table->nlpars) >= 0) {
            for (i = 0; i < uuid_table->nlpars; i++) {
                if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
                    virReportOOMError();
E
Eric Blake 已提交
847
                    goto cleanup;
848 849
                }
                uuid_table->lpars[i]->id = ids[i];
850

851 852 853 854
                if (virUUIDGenerate(uuid_table->lpars[i]->uuid) < 0)
                    VIR_WARN("Unable to generate UUID for domain %d",
                             ids[i]);
            }
E
Eduardo Otubo 已提交
855
        } else {
856
            virReportOOMError();
E
Eric Blake 已提交
857
            goto cleanup;
E
Eduardo Otubo 已提交
858
        }
859

860
        if (phypUUIDTable_WriteFile(conn) == -1)
E
Eric Blake 已提交
861
            goto cleanup;
862

863
        if (phypUUIDTable_Push(conn) == -1)
E
Eric Blake 已提交
864
            goto cleanup;
865 866
    } else {
        if (phypUUIDTable_ReadFile(conn) == -1)
E
Eric Blake 已提交
867
            goto cleanup;
868
    }
869

E
Eric Blake 已提交
870
    ret = 0;
871

E
Eric Blake 已提交
872 873 874 875 876 877 878
cleanup:
    if (ret < 0 && table_created) {
        for (i = 0; i < uuid_table->nlpars; i++) {
            VIR_FREE(uuid_table->lpars[i]);
        }
        VIR_FREE(uuid_table->lpars);
    }
879
    VIR_FREE(ids);
E
Eric Blake 已提交
880
    return ret;
881 882
}

883 884
static void
phypUUIDTable_Free(uuid_tablePtr uuid_table)
885
{
886
    int i;
887

888 889 890 891 892 893 894 895
    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);
896 897
}

898 899 900 901 902 903 904 905
#define SPECIALCHARACTER_CASES                                                \
    case '&': case ';': case '`': case '@': case '"': case '|': case '*':     \
    case '?': case '~': case '<': case '>': case '^': case '(': case ')':     \
    case '[': case ']': case '{': case '}': case '$': case '%': case '#':     \
    case '\\': case '\n': case '\r': case '\t':

static bool
contains_specialcharacters(const char *src)
906
{
907
    size_t len = strlen(src);
908 909
    size_t i = 0;

910
    if (len == 0)
911
        return false;
912

913 914
    for (i = 0; i < len; i++) {
        switch (src[i]) {
915 916 917 918
        SPECIALCHARACTER_CASES
            return true;
        default:
            continue;
919 920 921
        }
    }

922 923
    return false;
}
924

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
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;

    if (VIR_ALLOC_N(dst, len + 1) < 0) {
        virReportOOMError();
        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;
953 954
}

955 956 957
static LIBSSH2_SESSION *
openSSHSession(virConnectPtr conn, virConnectAuthPtr auth,
               int *internal_socket)
958
{
959 960 961 962 963 964 965 966 967 968 969 970 971
    LIBSSH2_SESSION *session;
    const char *hostname = conn->uri->server;
    char *username = NULL;
    char *password = NULL;
    int sock;
    int rc;
    struct addrinfo *ai = NULL, *cur;
    struct addrinfo hints;
    int ret;
    char *pubkey = NULL;
    char *pvtkey = NULL;
    char *userhome = virGetUserDirectory(geteuid());
    struct stat pvt_stat, pub_stat;
972

973 974
    if (userhome == NULL)
        goto err;
E
Eduardo Otubo 已提交
975

976
    if (virAsprintf(&pubkey, "%s/.ssh/id_rsa.pub", userhome) < 0) {
977
        virReportOOMError();
978
        goto err;
979 980
    }

981 982
    if (virAsprintf(&pvtkey, "%s/.ssh/id_rsa", userhome) < 0) {
        virReportOOMError();
983 984 985
        goto err;
    }

986 987
    if (conn->uri->user != NULL) {
        username = strdup(conn->uri->user);
988

989 990 991 992 993 994 995 996 997 998
        if (username == NULL) {
            virReportOOMError();
            goto err;
        }
    } else {
        if (auth == NULL || auth->cb == NULL) {
            PHYP_ERROR(VIR_ERR_AUTH_FAILED,
                       "%s", _("No authentication callback provided."));
            goto err;
        }
999

1000
        username = virRequestUsername(auth, NULL, conn->uri->server);
1001

1002 1003 1004 1005 1006 1007
        if (username == NULL) {
            PHYP_ERROR(VIR_ERR_AUTH_FAILED, "%s",
                       _("Username request failed"));
            goto err;
        }
    }
1008

1009 1010 1011 1012
    memset(&hints, 0, sizeof(hints));
    hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
1013

1014 1015 1016 1017 1018 1019
    ret = getaddrinfo(hostname, "22", &hints, &ai);
    if (ret != 0) {
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
                   _("Error while getting %s address info"), hostname);
        goto err;
    }
1020

1021 1022 1023 1024 1025 1026 1027
    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) {
                goto connected;
            }
1028
            VIR_FORCE_CLOSE(sock);
1029 1030 1031
        }
        cur = cur->ai_next;
    }
1032

1033 1034 1035 1036
    PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
               _("Failed to connect to %s"), hostname);
    freeaddrinfo(ai);
    goto err;
1037

1038
connected:
1039

1040
    (*internal_socket) = sock;
1041

1042 1043 1044
    /* Create a session instance */
    session = libssh2_session_init();
    if (!session)
1045 1046
        goto err;

1047 1048
    /* tell libssh2 we want it all done non-blocking */
    libssh2_session_set_blocking(session, 0);
1049

1050 1051 1052 1053 1054 1055 1056
    while ((rc = libssh2_session_startup(session, sock)) ==
           LIBSSH2_ERROR_EAGAIN) ;
    if (rc) {
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
                   "%s", _("Failure establishing SSH session."));
        goto disconnect;
    }
1057

1058 1059 1060 1061 1062
    /* Trying authentication by pubkey */
    if (stat(pvtkey, &pvt_stat) || stat(pubkey, &pub_stat)) {
        rc = LIBSSH2_ERROR_SOCKET_NONE;
        goto keyboard_interactive;
    }
1063

1064 1065 1066 1067 1068 1069
    while ((rc =
            libssh2_userauth_publickey_fromfile(session, username,
                                                pubkey,
                                                pvtkey,
                                                NULL)) ==
           LIBSSH2_ERROR_EAGAIN) ;
1070

1071
keyboard_interactive:
1072 1073 1074 1075 1076 1077 1078 1079
    if (rc == LIBSSH2_ERROR_SOCKET_NONE
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED) {
        if (auth == NULL || auth->cb == NULL) {
            PHYP_ERROR(VIR_ERR_AUTH_FAILED,
                       "%s", _("No authentication callback provided."));
            goto disconnect;
        }
1080

1081
        password = virRequestPassword(auth, username, conn->uri->server);
1082

1083 1084 1085 1086 1087
        if (password == NULL) {
            PHYP_ERROR(VIR_ERR_AUTH_FAILED, "%s",
                       _("Password request failed"));
            goto disconnect;
        }
1088

1089 1090 1091 1092
        while ((rc =
                libssh2_userauth_password(session, username,
                                          password)) ==
               LIBSSH2_ERROR_EAGAIN) ;
1093

1094 1095 1096 1097 1098 1099
        if (rc) {
            PHYP_ERROR(VIR_ERR_AUTH_FAILED,
                       "%s", _("Authentication failed"));
            goto disconnect;
        } else
            goto exit;
1100

1101 1102
    } else if (rc == LIBSSH2_ERROR_NONE) {
        goto exit;
1103

1104 1105
    } else if (rc == LIBSSH2_ERROR_ALLOC || rc == LIBSSH2_ERROR_SOCKET_SEND
               || rc == LIBSSH2_ERROR_SOCKET_TIMEOUT) {
1106 1107 1108
        goto err;
    }

1109
disconnect:
1110 1111
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1112
err:
1113 1114 1115 1116 1117
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
1118
    return NULL;
1119

1120
exit:
1121 1122 1123 1124 1125 1126
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
    return session;
1127 1128
}

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
static virDrvOpenStatus
phypOpen(virConnectPtr conn,
         virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
{
    LIBSSH2_SESSION *session = NULL;
    ConnectionData *connection_data = NULL;
    int internal_socket;
    uuid_tablePtr uuid_table = NULL;
    phyp_driverPtr phyp_driver = NULL;
    char *char_ptr;
    char *managed_system = NULL;
E
Eduardo Otubo 已提交
1140

1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    if (!conn || !conn->uri)
        return VIR_DRV_OPEN_DECLINED;

    if (conn->uri->scheme == NULL || STRNEQ(conn->uri->scheme, "phyp"))
        return VIR_DRV_OPEN_DECLINED;

    if (conn->uri->server == NULL) {
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
                   "%s", _("Missing server name in phyp:// URI"));
        return VIR_DRV_OPEN_ERROR;
    }

    if (VIR_ALLOC(phyp_driver) < 0) {
1154
        virReportOOMError();
1155
        goto failure;
1156 1157
    }

1158 1159 1160 1161
    if (VIR_ALLOC(uuid_table) < 0) {
        virReportOOMError();
        goto failure;
    }
1162

1163 1164 1165 1166
    if (VIR_ALLOC(connection_data) < 0) {
        virReportOOMError();
        goto failure;
    }
1167

1168 1169 1170 1171 1172 1173
    if (conn->uri->path) {
        /* need to shift one byte in order to remove the first "/" of URI component */
        if (conn->uri->path[0] == '/')
            managed_system = strdup(conn->uri->path + 1);
        else
            managed_system = strdup(conn->uri->path);
E
Eduardo Otubo 已提交
1174

1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
        if (!managed_system) {
            virReportOOMError();
            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';

1188
        if (contains_specialcharacters(conn->uri->path)) {
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
            PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
                       "%s",
                       _("Error parsing 'path'. Invalid characters."));
            goto failure;
        }
    }

    if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) {
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
                   "%s", _("Error while opening SSH session."));
        goto failure;
    }

    connection_data->session = session;

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

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

    phyp_driver->uuid_table = uuid_table;
    if ((phyp_driver->caps = phypCapsInit()) == NULL) {
1212
        virReportOOMError();
1213
        goto failure;
1214 1215
    }

1216 1217
    conn->privateData = phyp_driver;
    conn->networkPrivateData = connection_data;
1218

1219 1220
    if ((phyp_driver->system_type = phypGetSystemType(conn)) == -1)
        goto failure;
1221

1222 1223
    if (phypUUIDTable_Init(conn) == -1)
        goto failure;
1224

1225 1226 1227 1228 1229 1230 1231
    if (phyp_driver->system_type == HMC) {
        if ((phyp_driver->vios_id = phypGetVIOSPartitionID(conn)) == -1)
            goto failure;
    }

    return VIR_DRV_OPEN_SUCCESS;

1232
failure:
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
    if (phyp_driver != NULL) {
        virCapabilitiesFree(phyp_driver->caps);
        VIR_FREE(phyp_driver->managed_system);
        VIR_FREE(phyp_driver);
    }

    phypUUIDTable_Free(uuid_table);

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

    VIR_FREE(connection_data);

    return VIR_DRV_OPEN_ERROR;
1249 1250 1251
}

static int
1252
phypClose(virConnectPtr conn)
1253
{
1254 1255 1256
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
1257

1258 1259
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1260

1261 1262 1263 1264 1265 1266 1267
    virCapabilitiesFree(phyp_driver->caps);
    phypUUIDTable_Free(phyp_driver->uuid_table);
    VIR_FREE(phyp_driver->managed_system);
    VIR_FREE(phyp_driver);
    VIR_FREE(connection_data);
    return 0;
}
1268 1269


1270 1271 1272 1273 1274 1275
static int
phypIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Phyp uses an SSH tunnel, so is always encrypted */
    return 1;
}
1276

1277 1278 1279 1280 1281 1282

static int
phypIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Phyp uses an SSH tunnel, so is always secure */
    return 1;
1283 1284
}

1285 1286 1287 1288 1289
static int
phypIsUpdated(virDomainPtr conn ATTRIBUTE_UNUSED)
{
    return 0;
}
1290 1291

/* return the lpar_id given a name and a managed system name */
1292
static int
1293 1294
phypGetLparID(LIBSSH2_SESSION * session, const char *managed_system,
              const char *name, virConnectPtr conn)
1295
{
1296
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1297
    int system_type = phyp_driver->system_type;
1298
    int lpar_id = -1;
E
Eduardo Otubo 已提交
1299 1300
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1301
    virBufferAddLit(&buf, "lssyscfg -r lpar");
E
Eduardo Otubo 已提交
1302
    if (system_type == HMC)
1303 1304
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --filter lpar_names=%s -F lpar_id", name);
E
Eric Blake 已提交
1305
    phypExecInt(session, &buf, conn, &lpar_id);
1306
    return lpar_id;
1307 1308
}

1309 1310 1311 1312
/* 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)
1313 1314
{
    phyp_driverPtr phyp_driver = conn->privateData;
1315 1316 1317 1318
    int system_type = phyp_driver->system_type;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1319

1320 1321
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
1322 1323
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --filter lpar_ids=%d -F name", lpar_id);
1324
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1325

1326
    if (exit_status < 0)
1327 1328
        VIR_FREE(ret);
    return ret;
1329 1330
}

1331 1332 1333 1334 1335 1336 1337 1338 1339

/* 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)
1340 1341
{
    phyp_driverPtr phyp_driver = conn->privateData;
1342 1343 1344
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    lparPtr *lpars = uuid_table->lpars;
    unsigned int i = 0;
1345

1346 1347
    for (i = 0; i < uuid_table->nlpars; i++) {
        if (lpars[i]->id == lpar_id) {
1348
            memcpy(uuid, lpars[i]->uuid, VIR_UUID_BUFLEN);
1349 1350 1351
            return 0;
        }
    }
1352

1353
    return -1;
1354 1355
}

1356 1357 1358 1359 1360 1361 1362 1363
/*
 * type:
 * 0 - maxmem
 * 1 - memory
 * */
static unsigned long
phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id,
               int type)
1364
{
1365 1366 1367 1368 1369 1370
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
    phyp_driverPtr phyp_driver = conn->privateData;
    int system_type = phyp_driver->system_type;
    int memory = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1371

1372 1373
    if (type != 1 && type != 0)
        return 0;
1374

1375 1376
    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
1377 1378
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1379 1380
                      " -r mem --level lpar -F %s --filter lpar_ids=%d",
                      type ? "curr_mem" : "curr_max_mem", lpar_id);
E
Eric Blake 已提交
1381
    phypExecInt(session, &buf, conn, &memory);
1382
    return memory;
1383 1384
}

1385 1386 1387
static unsigned long
phypGetLparCPUGeneric(virConnectPtr conn, const char *managed_system,
                      int lpar_id, int type)
1388
{
1389
    ConnectionData *connection_data = conn->networkPrivateData;
1390
    LIBSSH2_SESSION *session = connection_data->session;
1391
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1392
    int system_type = phyp_driver->system_type;
1393
    int vcpus = 0;
E
Eduardo Otubo 已提交
1394
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1395

1396
    virBufferAddLit(&buf, "lshwres");
E
Eduardo Otubo 已提交
1397
    if (system_type == HMC)
1398 1399
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1400 1401
                      " -r proc --level lpar -F %s --filter lpar_ids=%d",
                      type ? "curr_max_procs" : "curr_procs", lpar_id);
E
Eric Blake 已提交
1402
    phypExecInt(session, &buf, conn, &vcpus);
1403
    return vcpus;
1404
}
1405

1406 1407 1408 1409
static unsigned long
phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id)
{
    return phypGetLparCPUGeneric(conn, managed_system, lpar_id, 0);
1410 1411
}

1412
static int
1413
phypDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
1414 1415 1416
{
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    char *managed_system = phyp_driver->managed_system;
1417

1418 1419 1420 1421 1422
    if (flags != (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_MAXIMUM)) {
        PHYP_ERROR(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
        return -1;
    }

1423 1424 1425
    return phypGetLparCPUGeneric(dom->conn, managed_system, dom->id, 1);
}

1426 1427 1428 1429 1430 1431 1432
static int
phypGetLparCPUMAX(virDomainPtr dom)
{
    return phypDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                         VIR_DOMAIN_VCPU_MAXIMUM));
}

1433 1434 1435
static int
phypGetRemoteSlot(virConnectPtr conn, const char *managed_system,
                  const char *lpar_name)
1436
{
1437 1438
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
1439
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1440
    int system_type = phyp_driver->system_type;
1441
    int remote_slot = -1;
E
Eduardo Otubo 已提交
1442 1443
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1444
    virBufferAddLit(&buf, "lshwres");
E
Eduardo Otubo 已提交
1445
    if (system_type == HMC)
1446 1447
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r virtualio --rsubtype scsi -F "
1448
                      "remote_slot_num --filter lpar_names=%s", lpar_name);
E
Eric Blake 已提交
1449
    phypExecInt(session, &buf, conn, &remote_slot);
1450
    return remote_slot;
1451 1452
}

1453 1454 1455 1456 1457 1458
/* 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)
1459
{
1460 1461
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
1462
    phyp_driverPtr phyp_driver = conn->privateData;
1463 1464 1465 1466 1467 1468 1469
    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;
1470

1471 1472 1473 1474 1475 1476
    if ((remote_slot =
         phypGetRemoteSlot(conn, managed_system, lpar_name)) == -1)
        return NULL;

    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
1477 1478
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r virtualio --rsubtype scsi -F "
1479
                      "backing_devices --filter slots=%d", remote_slot);
1480
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1481

1482
    if (exit_status < 0 || ret == NULL)
1483
        goto cleanup;
1484

1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
    /* 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
1499
            goto cleanup;
1500 1501 1502 1503 1504

        backing_device = strdup(char_ptr);

        if (backing_device == NULL) {
            virReportOOMError();
1505
            goto cleanup;
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
        }
    } else {
        backing_device = ret;
        ret = NULL;
    }

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

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

1517
cleanup:
1518
    VIR_FREE(ret);
1519

1520
    return backing_device;
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
}

static char *
phypGetLparProfile(virConnectPtr conn, int lpar_id)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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)
1537 1538
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1539 1540
                      " -r prof --filter lpar_ids=%d -F name|head -n 1",
                      lpar_id);
1541
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1542

1543
    if (exit_status < 0)
1544 1545
        VIR_FREE(ret);
    return ret;
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
}

static int
phypGetVIOSNextSlotNumber(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    char *profile = NULL;
1558
    int slot = -1;
1559 1560 1561
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!(profile = phypGetLparProfile(conn, vios_id))) {
1562
        VIR_ERROR(_("Unable to get VIOS profile name."));
1563
        return -1;
1564 1565 1566 1567 1568
    }

    virBufferAddLit(&buf, "lssyscfg");

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

1571
    virBufferAsprintf(&buf, " -r prof --filter "
1572 1573 1574 1575 1576
                      "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 已提交
1577 1578 1579
    if (phypExecInt(session, &buf, conn, &slot) < 0)
        return -1;
    return slot + 1;
1580 1581 1582 1583 1584
}

static int
phypCreateServerSCSIAdapter(virConnectPtr conn)
{
1585
    int result = -1;
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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))) {
1602
        VIR_ERROR(_("Unable to get VIOS name"));
1603
        goto cleanup;
1604 1605 1606
    }

    if (!(profile = phypGetLparProfile(conn, vios_id))) {
1607
        VIR_ERROR(_("Unable to get VIOS profile name."));
1608
        goto cleanup;
1609 1610 1611
    }

    if ((slot = phypGetVIOSNextSlotNumber(conn)) == -1) {
1612
        VIR_ERROR(_("Unable to get free slot number"));
1613
        goto cleanup;
1614 1615 1616 1617 1618 1619 1620
    }

    /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
     * be appended to this list
     * */
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
1621 1622
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r prof --filter lpar_ids=%d,profile_names=%s"
1623 1624
                      " -F virtual_scsi_adapters|sed -e s/\\\"//g",
                      vios_id, profile);
1625
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1626 1627

    if (exit_status < 0 || ret == NULL)
1628
        goto cleanup;
1629 1630 1631 1632 1633 1634

    /* 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)
1635 1636
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r prof -i 'name=%s,lpar_id=%d,"
1637 1638
                      "\"virtual_scsi_adapters=%s,%d/server/any/any/1\"'",
                      vios_name, vios_id, ret, slot);
1639
    VIR_FREE(ret);
1640
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1641 1642

    if (exit_status < 0 || ret == NULL)
1643
        goto cleanup;
1644 1645 1646 1647 1648 1649

    /* 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)
1650 1651
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1652 1653
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      vios_name, slot);
1654
    VIR_FREE(ret);
1655
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1656 1657

    if (exit_status < 0 || ret == NULL)
1658
        goto cleanup;
1659

1660
    result = 0;
1661

1662
cleanup:
1663 1664 1665
    VIR_FREE(profile);
    VIR_FREE(vios_name);
    VIR_FREE(ret);
1666 1667

    return result;
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
}

static char *
phypGetVIOSFreeSCSIAdapter(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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)
1684
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1685 1686
                          managed_system, vios_id);

1687
    virBufferAsprintf(&buf, "lsmap -all -field svsa backing -fmt , ");
1688 1689 1690 1691

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

1692
    virBufferAsprintf(&buf, "|sed '/,[^.*]/d; s/,//g; q'");
1693
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1694

1695
    if (exit_status < 0)
1696 1697
        VIR_FREE(ret);
    return ret;
1698 1699 1700 1701 1702 1703
}


static int
phypAttachDevice(virDomainPtr domain, const char *xml)
{
1704
    int result = -1;
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
    virConnectPtr conn = domain->conn;
    ConnectionData *connection_data = domain->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = domain->conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;

1723
    domain_name = escape_specialcharacters(domain->name);
1724

1725
    if (domain_name == NULL) {
1726
        goto cleanup;
1727 1728 1729 1730 1731 1732
    }

    def->os.type = strdup("aix");

    if (def->os.type == NULL) {
        virReportOOMError();
1733
        goto cleanup;
1734 1735 1736 1737 1738
    }

    dev = virDomainDeviceDefParse(phyp_driver->caps, def, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
    if (!dev) {
1739
        goto cleanup;
1740 1741 1742 1743 1744
    }

    if (!
        (vios_name =
         phypGetLparNAME(session, managed_system, vios_id, conn))) {
1745
        VIR_ERROR(_("Unable to get VIOS name"));
1746
        goto cleanup;
1747 1748 1749 1750 1751 1752 1753 1754
    }

    /* 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) {
1755
            VIR_ERROR(_("Unable to create new virtual adapter"));
1756
            goto cleanup;
1757 1758
        } else {
            if (!(scsi_adapter = phypGetVIOSFreeSCSIAdapter(conn))) {
1759
                VIR_ERROR(_("Unable to create new virtual adapter"));
1760
                goto cleanup;
1761 1762 1763 1764 1765
            }
        }
    }

    if (system_type == HMC)
1766
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1767 1768
                          managed_system, vios_id);

1769
    virBufferAsprintf(&buf, "mkvdev -vdev %s -vadapter %s",
1770 1771 1772 1773
                      dev->data.disk->src, scsi_adapter);

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1774
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1775 1776

    if (exit_status < 0 || ret == NULL)
1777
        goto cleanup;
1778 1779

    if (!(profile = phypGetLparProfile(conn, domain->id))) {
1780
        VIR_ERROR(_("Unable to get VIOS profile name."));
1781
        goto cleanup;
1782 1783 1784 1785 1786 1787
    }

    /* Let's get the slot number for the adapter we just created
     * */
    virBufferAddLit(&buf, "lshwres -r virtualio --rsubtype scsi");
    if (system_type == HMC)
1788 1789
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1790 1791
                      " slot_num,backing_device|grep %s|cut -d, -f1",
                      dev->data.disk->src);
E
Eric Blake 已提交
1792
    if (phypExecInt(session, &buf, conn, &slot) < 0)
1793
        goto cleanup;
1794 1795 1796 1797 1798 1799

    /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
     * be appended to this list
     * */
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
1800 1801
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1802 1803 1804
                      " -r prof --filter lpar_ids=%d,profile_names=%s"
                      " -F virtual_scsi_adapters|sed -e 's/\"//g'",
                      vios_id, profile);
1805
    VIR_FREE(ret);
1806
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1807 1808

    if (exit_status < 0 || ret == NULL)
1809
        goto cleanup;
1810 1811 1812 1813 1814 1815

    /* 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)
1816 1817
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1818 1819 1820 1821
                      " -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 已提交
1822
    if (phypExecInt(session, &buf, conn, &slot) < 0)
1823
        goto cleanup;
1824 1825 1826 1827 1828 1829

    /* 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)
1830 1831
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1832 1833
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      domain_name, slot);
1834
    VIR_FREE(ret);
1835
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1836 1837

    if (exit_status < 0 || ret == NULL) {
1838
        VIR_ERROR(_
1839 1840
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    "Contact your support to enable this feature."));
1841
        goto cleanup;
1842 1843
    }

1844
    result = 0;
1845

1846
cleanup:
1847
    VIR_FREE(ret);
1848 1849
    virDomainDeviceDefFree(dev);
    virDomainDefFree(def);
1850 1851
    VIR_FREE(vios_name);
    VIR_FREE(scsi_adapter);
1852 1853 1854 1855
    VIR_FREE(profile);
    VIR_FREE(domain_name);

    return result;
1856 1857
}

1858 1859
static char *
phypVolumeGetKey(virConnectPtr conn, const char *name)
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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)
1872
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1873 1874
                          managed_system, vios_id);

1875
    virBufferAsprintf(&buf, "lslv %s -field lvid", name);
1876 1877 1878 1879

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

1880
    virBufferAsprintf(&buf, "|sed -e 's/^LV IDENTIFIER://' -e 's/ //g'");
1881
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1882

1883
    if (exit_status < 0)
1884 1885
        VIR_FREE(ret);
    return ret;
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
}

static char *
phypGetStoragePoolDevice(virConnectPtr conn, char *name)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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)
1902
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1903 1904
                          managed_system, vios_id);

1905
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field name", name);
1906 1907 1908 1909

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

1910
    virBufferAsprintf(&buf, "|sed '1d; s/ //g'");
1911
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1912

1913
    if (exit_status < 0)
1914 1915
        VIR_FREE(ret);
    return ret;
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
}

static unsigned long int
phypGetStoragePoolSize(virConnectPtr conn, char *name)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
1927
    int sp_size = -1;
1928 1929 1930
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
1931
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1932 1933
                          managed_system, vios_id);

1934
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field size", name);
1935 1936 1937 1938

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

1939
    virBufferAsprintf(&buf, "|sed '1d; s/ //g'");
E
Eric Blake 已提交
1940
    phypExecInt(session, &buf, conn, &sp_size);
1941
    return sp_size;
1942 1943
}

1944
static char *
1945
phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
1946
                unsigned int capacity)
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;
1957
    char *key = NULL;
1958 1959

    if (system_type == HMC)
1960
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1961 1962
                          managed_system, vios_id);

1963
    virBufferAsprintf(&buf, "mklv -lv %s %s %d", lvname, spname, capacity);
1964 1965 1966

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1967
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1968 1969

    if (exit_status < 0) {
1970
        VIR_ERROR(_("Unable to create Volume: %s"), NULLSTR(ret));
1971
        goto cleanup;
1972 1973
    }

1974 1975
    key = phypVolumeGetKey(conn, lvname);

1976
cleanup:
1977 1978
    VIR_FREE(ret);

1979
    return key;
1980 1981 1982 1983 1984
}

static virStorageVolPtr
phypVolumeLookupByName(virStoragePoolPtr pool, const char *volname)
{
1985 1986
    char *key;
    virStorageVolPtr vol;
1987

1988
    key = phypVolumeGetKey(pool->conn, volname);
1989

1990
    if (key == NULL)
1991 1992
        return NULL;

1993 1994 1995 1996 1997
    vol = virGetStorageVol(pool->conn, pool->name, volname, key);

    VIR_FREE(key);

    return vol;
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
}

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

    virStorageVolDefPtr voldef = NULL;
    virStoragePoolDefPtr spdef = NULL;
    virStorageVolPtr vol = NULL;
    char *key = NULL;

    if (VIR_ALLOC(spdef) < 0) {
        virReportOOMError();
        return NULL;
    }

    /* Filling spdef manually
     * */
    if (pool->name != NULL) {
        spdef->name = pool->name;
    } else {
2021
        VIR_ERROR(_("Unable to determine storage pool's name."));
2022 2023 2024 2025
        goto err;
    }

    if (memcpy(spdef->uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
2026
        VIR_ERROR(_("Unable to determine storage pool's uuid."));
2027 2028 2029 2030 2031
        goto err;
    }

    if ((spdef->capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2032
        VIR_ERROR(_("Unable to determine storage pools's size."));
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
        goto err;
    }

    /* Information not avaliable */
    spdef->allocation = 0;
    spdef->available = 0;

    spdef->source.ndevice = 1;

    /*XXX source adapter not working properly, should show hdiskX */
    if ((spdef->source.adapter =
         phypGetStoragePoolDevice(pool->conn, pool->name)) == NULL) {
2045
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
2046 2047 2048 2049
        goto err;
    }

    if ((voldef = virStorageVolDefParseString(spdef, xml)) == NULL) {
2050
        VIR_ERROR(_("Error parsing volume XML."));
2051 2052 2053 2054 2055
        goto err;
    }

    /* checking if this name already exists on this system */
    if (phypVolumeLookupByName(pool, voldef->name) != NULL) {
2056
        VIR_ERROR(_("StoragePool name already exists."));
2057 2058 2059 2060 2061 2062 2063
        goto err;
    }

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

    if (voldef->capacity) {
2069
        VIR_ERROR(_("Capacity cannot be empty."));
2070 2071 2072
        goto err;
    }

2073 2074 2075 2076
    key = phypBuildVolume(pool->conn, voldef->name, spdef->name,
                          voldef->capacity);

    if (key == NULL)
2077 2078 2079 2080 2081 2082 2083
        goto err;

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

2084 2085
    VIR_FREE(key);

2086 2087
    return vol;

2088
err:
2089
    VIR_FREE(key);
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
    virStorageVolDefFree(voldef);
    virStoragePoolDefFree(spdef);
    if (vol)
        virUnrefStorageVol(vol);
    return NULL;
}

static char *
phypVolumeGetPhysicalVolumeByStoragePool(virStorageVolPtr vol, char *sp)
{
    virConnectPtr conn = vol->conn;
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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)
2112
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2113 2114
                          managed_system, vios_id);

2115
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field pvname", sp);
2116 2117 2118 2119

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

2120
    virBufferAsprintf(&buf, "|sed 1d");
2121
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2122

2123
    if (exit_status < 0)
2124 2125
        VIR_FREE(ret);
    return ret;
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
}

static virStorageVolPtr
phypVolumeLookupByPath(virConnectPtr conn, const char *volname)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;
2138
    char *ret = NULL;
2139 2140
    char *key = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2141
    virStorageVolPtr vol = NULL;
2142 2143

    if (system_type == HMC)
2144
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2145 2146
                          managed_system, vios_id);

2147
    virBufferAsprintf(&buf, "lslv %s -field vgname", volname);
2148 2149 2150 2151

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

2152
    virBufferAsprintf(&buf, "|sed -e 's/^VOLUME GROUP://g' -e 's/ //g'");
2153
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2154

2155
    if (exit_status < 0 || ret == NULL)
2156
        goto cleanup;
2157

2158
    key = phypVolumeGetKey(conn, volname);
2159

2160
    if (key == NULL)
2161
        goto cleanup;
2162

2163
    vol = virGetStorageVol(conn, ret, volname, key);
2164

2165
cleanup:
2166
    VIR_FREE(ret);
2167 2168 2169
    VIR_FREE(key);

    return vol;
2170 2171 2172 2173 2174 2175
}

static int
phypGetStoragePoolUUID(virConnectPtr conn, unsigned char *uuid,
                       const char *name)
{
2176
    int result = -1;
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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)
2188
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2189 2190
                          managed_system, vios_id);

2191
    virBufferAsprintf(&buf, "lsdev -dev %s -attr vgserial_id", name);
2192 2193 2194 2195

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

2196
    virBufferAsprintf(&buf, "|sed '1,2d'");
2197
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2198 2199

    if (exit_status < 0 || ret == NULL)
2200
        goto cleanup;
2201

2202
    if (memcpy(uuid, ret, VIR_UUID_BUFLEN) == NULL)
2203
        goto cleanup;
2204

2205
    result = 0;
2206

2207
cleanup:
2208
    VIR_FREE(ret);
2209 2210

    return result;
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
}

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

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

    return virGetStoragePool(conn, name, uuid);
}

static char *
phypVolumeGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
{
2227 2228 2229
    virStorageVolDef voldef;
    virStoragePoolDef pool;
    virStoragePoolPtr sp;
2230 2231
    char *xml;

2232 2233 2234
    virCheckFlags(0, NULL);

    memset(&voldef, 0, sizeof(virStorageVolDef));
2235
    memset(&pool, 0, sizeof(virStoragePoolDef));
2236

2237
    sp = phypStoragePoolLookupByName(vol->conn, vol->pool);
2238 2239 2240 2241 2242 2243 2244

    if (!sp)
        goto err;

    if (sp->name != NULL) {
        pool.name = sp->name;
    } else {
2245
        VIR_ERROR(_("Unable to determine storage sp's name."));
2246 2247 2248
        goto err;
    }

2249
    if (memcpy(pool.uuid, sp->uuid, VIR_UUID_BUFLEN) == NULL) {
2250
        VIR_ERROR(_("Unable to determine storage sp's uuid."));
2251 2252 2253 2254
        goto err;
    }

    if ((pool.capacity = phypGetStoragePoolSize(sp->conn, sp->name)) == -1) {
2255
        VIR_ERROR(_("Unable to determine storage sps's size."));
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
        goto err;
    }

    /* Information not avaliable */
    pool.allocation = 0;
    pool.available = 0;

    pool.source.ndevice = 1;

    if ((pool.source.adapter =
         phypGetStoragePoolDevice(sp->conn, sp->name)) == NULL) {
2267
        VIR_ERROR(_("Unable to determine storage sps's source adapter."));
2268 2269 2270 2271 2272 2273
        goto err;
    }

    if (vol->name != NULL)
        voldef.name = vol->name;
    else {
2274
        VIR_ERROR(_("Unable to determine storage pool's name."));
2275 2276 2277
        goto err;
    }

2278 2279 2280 2281
    voldef.key = strdup(vol->key);

    if (voldef.key == NULL) {
        virReportOOMError();
2282 2283 2284 2285 2286
        goto err;
    }

    voldef.type = VIR_STORAGE_POOL_LOGICAL;

2287 2288 2289 2290 2291
    xml = virStorageVolDefFormat(&pool, &voldef);

    VIR_FREE(voldef.key);

    return xml;
2292

2293
err:
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
    return NULL;
}

/* 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 *
phypVolumeGetPath(virStorageVolPtr vol)
{
    virConnectPtr conn = vol->conn;
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;
2316
    char *ret = NULL;
2317 2318
    char *path = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2319
    char *pv;
2320 2321

    if (system_type == HMC)
2322
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2323 2324
                          managed_system, vios_id);

2325
    virBufferAsprintf(&buf, "lslv %s -field vgname", vol->name);
2326 2327 2328 2329

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

2330
    virBufferAsprintf(&buf,
2331
                      "|sed -e 's/^VOLUME GROUP://g' -e 's/ //g'");
2332
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2333

2334
    if (exit_status < 0 || ret == NULL)
2335
        goto cleanup;
2336

2337
    pv = phypVolumeGetPhysicalVolumeByStoragePool(vol, ret);
2338

2339 2340
    if (!pv)
        goto cleanup;
2341

2342
    if (virAsprintf(&path, "/%s/%s/%s", pv, ret, vol->name) < 0) {
2343 2344 2345
        virReportOOMError();
        goto cleanup;
    }
2346

2347
cleanup:
2348
    VIR_FREE(ret);
2349
    VIR_FREE(path);
2350 2351

    return path;
2352 2353 2354 2355 2356 2357
}

static int
phypStoragePoolListVolumes(virStoragePoolPtr pool, char **const volumes,
                           int nvolumes)
{
2358
    bool success = false;
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
    virConnectPtr conn = pool->conn;
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;
    int i;
    char *ret = NULL;
    char *volumes_list = NULL;
E
Eric Blake 已提交
2371
    char *char_ptr = NULL;
2372 2373 2374
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2375
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2376 2377
                          managed_system, vios_id);

2378
    virBufferAsprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
2379 2380 2381 2382

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

2383
    virBufferAsprintf(&buf, "|sed '1,2d'");
2384
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2385 2386 2387

    /* I need to parse the textual return in order to get the volumes */
    if (exit_status < 0 || ret == NULL)
2388
        goto cleanup;
2389 2390 2391 2392
    else {
        volumes_list = ret;

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

E
Eric Blake 已提交
2395 2396
            if (char_ptr) {
                *char_ptr = '\0';
2397 2398
                if ((volumes[got++] = strdup(volumes_list)) == NULL) {
                    virReportOOMError();
2399
                    goto cleanup;
2400
                }
E
Eric Blake 已提交
2401 2402
                char_ptr++;
                volumes_list = char_ptr;
2403 2404 2405 2406 2407
            } else
                break;
        }
    }

2408 2409
    success = true;

2410
cleanup:
2411 2412 2413 2414 2415 2416
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(volumes[i]);

        got = -1;
    }
2417
    VIR_FREE(ret);
2418
    return got;
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
}

static int
phypStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
    virConnectPtr conn = pool->conn;
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
2429
    int nvolumes = -1;
2430 2431 2432 2433 2434
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2435
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2436
                          managed_system, vios_id);
2437
    virBufferAsprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
2438 2439
    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2440
    virBufferAsprintf(&buf, "|grep -c '^.*$'");
E
Eric Blake 已提交
2441 2442
    if (phypExecInt(session, &buf, conn, &nvolumes) < 0)
        return -1;
2443 2444

    /* We need to remove 2 line from the header text output */
E
Eric Blake 已提交
2445
    return nvolumes - 2;
2446 2447 2448 2449 2450
}

static int
phypDestroyStoragePool(virStoragePoolPtr pool)
{
2451
    int result = -1;
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
    virConnectPtr conn = pool->conn;
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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)
2464
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2465 2466
                          managed_system, vios_id);

2467
    virBufferAsprintf(&buf, "rmsp %s", pool->name);
2468 2469 2470

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2471
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2472 2473

    if (exit_status < 0) {
2474
        VIR_ERROR(_("Unable to destroy Storage Pool: %s"), NULLSTR(ret));
2475
        goto cleanup;
2476 2477
    }

2478
    result = 0;
2479

2480
cleanup:
2481
    VIR_FREE(ret);
2482 2483

    return result;
2484 2485 2486 2487 2488
}

static int
phypBuildStoragePool(virConnectPtr conn, virStoragePoolDefPtr def)
{
2489
    int result = -1;
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;

    if (system_type == HMC)
2502
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2503 2504
                          managed_system, vios_id);

2505
    virBufferAsprintf(&buf, "mksp -f %schild %s", def->name,
2506 2507 2508 2509
                      source.adapter);

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

    if (exit_status < 0) {
2513
        VIR_ERROR(_("Unable to create Storage Pool: %s"), NULLSTR(ret));
2514
        goto cleanup;
2515 2516
    }

2517
    result = 0;
2518

2519
cleanup:
2520
    VIR_FREE(ret);
2521 2522

    return result;
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532

}

static int
phypNumOfStoragePools(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
2533
    int nsp = -1;
2534 2535 2536 2537 2538
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2539
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2540 2541
                          managed_system, vios_id);

2542
    virBufferAsprintf(&buf, "lsvg");
2543 2544 2545 2546

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

2547
    virBufferAsprintf(&buf, "|grep -c '^.*$'");
E
Eric Blake 已提交
2548
    phypExecInt(session, &buf, conn, &nsp);
2549
    return nsp;
2550 2551 2552 2553 2554
}

static int
phypListStoragePools(virConnectPtr conn, char **const pools, int npools)
{
2555
    bool success = false;
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;
    int i;
    char *ret = NULL;
    char *storage_pools = NULL;
E
Eric Blake 已提交
2567
    char *char_ptr = NULL;
2568 2569 2570
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2571
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2572 2573
                          managed_system, vios_id);

2574
    virBufferAsprintf(&buf, "lsvg");
2575 2576 2577

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2578
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2579 2580 2581

    /* I need to parse the textual return in order to get the storage pools */
    if (exit_status < 0 || ret == NULL)
2582
        goto cleanup;
2583 2584 2585 2586
    else {
        storage_pools = ret;

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

E
Eric Blake 已提交
2589 2590
            if (char_ptr) {
                *char_ptr = '\0';
2591 2592
                if ((pools[got++] = strdup(storage_pools)) == NULL) {
                    virReportOOMError();
2593
                    goto cleanup;
2594
                }
E
Eric Blake 已提交
2595 2596
                char_ptr++;
                storage_pools = char_ptr;
2597 2598 2599 2600 2601
            } else
                break;
        }
    }

2602 2603
    success = true;

2604
cleanup:
2605 2606 2607 2608 2609 2610
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(pools[i]);

        got = -1;
    }
2611
    VIR_FREE(ret);
2612
    return got;
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
}

static virStoragePoolPtr
phypGetStoragePoolLookUpByUUID(virConnectPtr conn,
                               const unsigned char *uuid)
{
    virStoragePoolPtr sp = NULL;
    int npools = 0;
    int gotpools = 0;
    char **pools = NULL;
    unsigned int i = 0;
    unsigned char *local_uuid = NULL;

    if (VIR_ALLOC_N(local_uuid, VIR_UUID_BUFLEN) < 0) {
        virReportOOMError();
        goto err;
    }

    if ((npools = phypNumOfStoragePools(conn)) == -1) {
        virReportOOMError();
        goto err;
    }

    if (VIR_ALLOC_N(pools, npools) < 0) {
        virReportOOMError();
        goto err;
    }

    if ((gotpools = phypListStoragePools(conn, pools, npools)) == -1) {
        virReportOOMError();
        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)) {
            sp = virGetStoragePool(conn, pools[i], uuid);
            VIR_FREE(local_uuid);
            VIR_FREE(pools);

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

2667
err:
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
    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;
    virStoragePoolPtr sp = NULL;

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

    /* checking if this name already exists on this system */
    if (phypStoragePoolLookupByName(conn, def->name) != NULL) {
2687
        VIR_WARN("StoragePool name already exists.");
2688 2689 2690 2691 2692
        goto err;
    }

    /* checking if ID or UUID already exists on this system */
    if (phypGetStoragePoolLookUpByUUID(conn, def->uuid) != NULL) {
2693
        VIR_WARN("StoragePool uuid already exists.");
2694 2695
        goto err;
    }
2696

2697 2698 2699 2700 2701 2702 2703 2704
    if ((sp = virGetStoragePool(conn, def->name, def->uuid)) == NULL)
        goto err;

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

    return sp;

2705
err:
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
    virStoragePoolDefFree(def);
    if (sp)
        virUnrefStoragePool(sp);
    return NULL;
}

static char *
phypGetStoragePoolXMLDesc(virStoragePoolPtr pool, unsigned int flags)
{
    virCheckFlags(0, NULL);

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

    if (pool->name != NULL)
        def.name = pool->name;
    else {
2723
        VIR_ERROR(_("Unable to determine storage pool's name."));
2724 2725 2726
        goto err;
    }

2727
    if (memcpy(def.uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
2728
        VIR_ERROR(_("Unable to determine storage pool's uuid."));
2729 2730 2731 2732 2733
        goto err;
    }

    if ((def.capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2734
        VIR_ERROR(_("Unable to determine storage pools's size."));
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746
        goto err;
    }

    /* Information not avaliable */
    def.allocation = 0;
    def.available = 0;

    def.source.ndevice = 1;

    /*XXX source adapter not working properly, should show hdiskX */
    if ((def.source.adapter =
         phypGetStoragePoolDevice(pool->conn, pool->name)) == NULL) {
2747
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
2748 2749 2750 2751 2752
        goto err;
    }

    return virStoragePoolDefFormat(&def);

2753
err:
2754
    return NULL;
2755 2756
}

E
Eduardo Otubo 已提交
2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
static int
phypInterfaceDestroy(virInterfacePtr iface,
                     unsigned int flags)
{
    virCheckFlags(0, -1);

    ConnectionData *connection_data = iface->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = iface->conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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 已提交
2773
    int rv = -1;
E
Eduardo Otubo 已提交
2774 2775 2776 2777 2778

    /* Getting the remote slot number */

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

2781
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2782 2783 2784
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,slot_num|"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
2785
    if (phypExecInt(session, &buf, iface->conn, &slot_num) < 0)
E
Eric Blake 已提交
2786
        goto cleanup;
E
Eduardo Otubo 已提交
2787 2788 2789 2790

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

2793
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2794 2795 2796
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,lpar_id|"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
2797
    if (phypExecInt(session, &buf, iface->conn, &lpar_id) < 0)
E
Eric Blake 已提交
2798
        goto cleanup;
E
Eduardo Otubo 已提交
2799 2800 2801 2802

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

2805
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2806 2807
                      " -r virtualio --rsubtype eth"
                      " --id %d -o r -s %d", lpar_id, slot_num);
2808 2809
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, iface->conn, false);
E
Eduardo Otubo 已提交
2810 2811

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

E
Eric Blake 已提交
2814
    rv = 0;
E
Eduardo Otubo 已提交
2815

E
Eric Blake 已提交
2816
cleanup:
E
Eduardo Otubo 已提交
2817
    VIR_FREE(ret);
E
Eric Blake 已提交
2818
    return rv;
E
Eduardo Otubo 已提交
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
}

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

    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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 已提交
2839
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2840 2841

    if (!(def = virInterfaceDefParseString(xml)))
E
Eric Blake 已提交
2842
        goto cleanup;
E
Eduardo Otubo 已提交
2843 2844 2845 2846

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

2849
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2850 2851 2852
                      " -r virtualio --rsubtype slot --level slot"
                      " -Fslot_num --filter lpar_names=%s"
                      " |sort|tail -n 1", def->name);
E
Eric Blake 已提交
2853
    if (phypExecInt(session, &buf, conn, &slot) < 0)
E
Eric Blake 已提交
2854
        goto cleanup;
E
Eduardo Otubo 已提交
2855 2856 2857 2858 2859 2860 2861

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

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

2864
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2865 2866 2867
                      " -r virtualio --rsubtype eth"
                      " -p %s -o a -s %d -a port_vlan_id=1,"
                      "ieee_virtual_eth=0", def->name, slot);
2868 2869
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2870 2871

    if (exit_status < 0 || ret != NULL)
E
Eric Blake 已提交
2872
        goto cleanup;
E
Eduardo Otubo 已提交
2873 2874 2875 2876 2877 2878 2879 2880 2881

    /* 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)
2882
        virBufferAsprintf(&buf, "-m %s ", managed_system);
E
Eduardo Otubo 已提交
2883

2884
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2885 2886 2887
                      " -r virtualio --rsubtype slot --level slot"
                      " |sed '/lpar_name=%s/!d; /slot_num=%d/!d; "
                      "s/^.*drc_name=//'", def->name, slot);
2888 2889
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2890 2891 2892 2893 2894

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

2897
        virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2898 2899
                " -r virtualio --rsubtype eth"
                " -p %s -o r -s %d", def->name, slot);
2900 2901
        VIR_FREE(ret);
        ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eric Blake 已提交
2902
        goto cleanup;
E
Eduardo Otubo 已提交
2903 2904 2905 2906 2907 2908 2909
    }

    memcpy(name, ret, PHYP_IFACENAME_SIZE-1);

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

2912
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2913 2914 2915
                      "-r virtualio --rsubtype eth --level lpar "
                      " |sed '/lpar_name=%s/!d; /slot_num=%d/!d; "
                      "s/^.*mac_addr=//'", def->name, slot);
2916 2917
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2918 2919

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
2920
        goto cleanup;
E
Eduardo Otubo 已提交
2921 2922 2923

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

E
Eric Blake 已提交
2926
cleanup:
E
Eduardo Otubo 已提交
2927 2928
    VIR_FREE(ret);
    virInterfaceDefFree(def);
E
Eric Blake 已提交
2929
    return result;
E
Eduardo Otubo 已提交
2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945
}

static virInterfacePtr
phypInterfaceLookupByName(virConnectPtr conn, const char *name)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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];
2946
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2947 2948 2949 2950

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

2953
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2954 2955 2956
                      " -r virtualio --rsubtype slot --level slot "
                      " -F drc_name,slot_num |"
                      " sed -n '/%s/ s/^.*,//p'", name);
E
Eric Blake 已提交
2957
    if (phypExecInt(session, &buf, conn, &slot) < 0)
E
Eric Blake 已提交
2958
        goto cleanup;
E
Eduardo Otubo 已提交
2959 2960 2961 2962

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

2965
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2966 2967 2968
                      " -r virtualio --rsubtype slot --level slot "
                      " -F drc_name,lpar_id |"
                      " sed -n '/%s/ s/^.*,//p'", name);
E
Eric Blake 已提交
2969
    if (phypExecInt(session, &buf, conn, &lpar_id) < 0)
E
Eric Blake 已提交
2970
        goto cleanup;
E
Eduardo Otubo 已提交
2971 2972 2973 2974

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

2977
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2978 2979 2980
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F lpar_id,slot_num,mac_addr|"
                      " sed -n '/%d,%d/ s/^.*,//p'", lpar_id, slot);
2981
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2982 2983

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
2984
        goto cleanup;
E
Eduardo Otubo 已提交
2985 2986 2987

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

E
Eric Blake 已提交
2990
cleanup:
E
Eduardo Otubo 已提交
2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003
    VIR_FREE(ret);
    return result;
}

static int
phypInterfaceIsActive(virInterfacePtr iface)
{
    ConnectionData *connection_data = iface->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = iface->conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
E
Eric Blake 已提交
3004
    int state = -1;
E
Eduardo Otubo 已提交
3005 3006 3007

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

3010
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
3011 3012 3013
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,state |"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
3014
    phypExecInt(session, &buf, iface->conn, &state);
E
Eduardo Otubo 已提交
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
    return state;
}

static int
phypListInterfaces(virConnectPtr conn, char **const names, int nnames)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;
    int i;
    char *ret = NULL;
    char *networks = NULL;
E
Eric Blake 已提交
3032
    char *char_ptr = NULL;
E
Eduardo Otubo 已提交
3033
    virBuffer buf = VIR_BUFFER_INITIALIZER;
E
Eric Blake 已提交
3034
    bool success = false;
E
Eduardo Otubo 已提交
3035 3036 3037

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

E
Eric Blake 已提交
3044 3045
    /* I need to parse the textual return in order to get the network
     * interfaces */
E
Eduardo Otubo 已提交
3046
    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3047
        goto cleanup;
E
Eduardo Otubo 已提交
3048 3049 3050 3051

    networks = ret;

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

E
Eric Blake 已提交
3054 3055
        if (char_ptr) {
            *char_ptr = '\0';
E
Eduardo Otubo 已提交
3056 3057
            if ((names[got++] = strdup(networks)) == NULL) {
                virReportOOMError();
E
Eric Blake 已提交
3058
                goto cleanup;
E
Eduardo Otubo 已提交
3059
            }
E
Eric Blake 已提交
3060 3061
            char_ptr++;
            networks = char_ptr;
E
Eduardo Otubo 已提交
3062 3063 3064 3065 3066
        } else {
            break;
        }
    }

E
Eric Blake 已提交
3067 3068 3069 3070 3071
cleanup:
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);
    }
E
Eduardo Otubo 已提交
3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084
    VIR_FREE(ret);
    return got;
}

static int
phypNumOfInterfaces(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
E
Eric Blake 已提交
3085
    int nnets = -1;
E
Eduardo Otubo 已提交
3086 3087 3088 3089
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

3092
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
3093 3094
                      "-r virtualio --rsubtype eth --level lpar|"
                      "grep -v lpar_id=%d|grep -c lpar_name", vios_id);
E
Eric Blake 已提交
3095
    phypExecInt(session, &buf, conn, &nnets);
E
Eduardo Otubo 已提交
3096 3097 3098
    return nnets;
}

3099 3100
static int
phypGetLparState(virConnectPtr conn, unsigned int lpar_id)
3101
{
3102
    ConnectionData *connection_data = conn->networkPrivateData;
3103
    phyp_driverPtr phyp_driver = conn->privateData;
3104 3105 3106 3107 3108 3109 3110
    LIBSSH2_SESSION *session = connection_data->session;
    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;
3111

3112 3113
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
3114 3115
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F state --filter lpar_ids=%d", lpar_id);
3116
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
3117

3118 3119
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3120

3121 3122 3123 3124 3125 3126
    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;
3127

3128
cleanup:
3129 3130
    VIR_FREE(ret);
    return state;
3131 3132
}

3133 3134 3135 3136
/* XXX - is this needed? */
static int phypDiskType(virConnectPtr, char *) ATTRIBUTE_UNUSED;
static int
phypDiskType(virConnectPtr conn, char *backing_device)
3137 3138
{
    phyp_driverPtr phyp_driver = conn->privateData;
3139 3140 3141 3142 3143 3144 3145 3146 3147
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;
3148

3149 3150
    virBufferAddLit(&buf, "viosvrcmd");
    if (system_type == HMC)
3151 3152
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -p %d -c \"lssp -field name type "
E
Eric Blake 已提交
3153
                      "-fmt , -all|sed -n '/%s/ {\n s/^.*,//\n p\n}'\"",
3154
                      vios_id, backing_device);
3155
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
3156

3157 3158
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3159

3160 3161 3162 3163
    if (STREQ(ret, "LVPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_BLOCK;
    else if (STREQ(ret, "FBPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_FILE;
3164

3165
cleanup:
3166 3167 3168
    VIR_FREE(ret);
    return disk_type;
}
3169

3170 3171 3172 3173 3174
static int
phypNumDefinedDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 1);
}
3175

3176 3177 3178 3179
static int
phypNumDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 0);
3180 3181
}

3182 3183
static int
phypListDomains(virConnectPtr conn, int *ids, int nids)
3184
{
3185 3186
    return phypListDomainsGeneric(conn, ids, nids, 0);
}
3187

3188 3189 3190
static int
phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames)
{
3191
    bool success = false;
3192 3193 3194 3195 3196 3197 3198 3199 3200 3201
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
    int got = 0;
    int i;
    char *ret = NULL;
    char *domains = NULL;
E
Eric Blake 已提交
3202
    char *char_ptr = NULL;
3203
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3204

3205 3206
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
3207 3208
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F name,state"
E
Eric Blake 已提交
3209
                      "|sed -n '/Not Activated/ {\n s/,.*$//\n p\n}'");
3210
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
3211

3212 3213
    /* I need to parse the textual return in order to get the domains */
    if (exit_status < 0 || ret == NULL)
3214
        goto cleanup;
3215 3216
    else {
        domains = ret;
3217

3218
        while (got < nnames) {
E
Eric Blake 已提交
3219
            char_ptr = strchr(domains, '\n');
3220

E
Eric Blake 已提交
3221 3222
            if (char_ptr) {
                *char_ptr = '\0';
3223
                if ((names[got++] = strdup(domains)) == NULL) {
3224
                    virReportOOMError();
3225
                    goto cleanup;
3226
                }
E
Eric Blake 已提交
3227 3228
                char_ptr++;
                domains = char_ptr;
3229 3230
            } else
                break;
3231
        }
3232 3233
    }

3234 3235
    success = true;

3236
cleanup:
3237 3238 3239 3240 3241 3242
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);

        got = -1;
    }
3243
    VIR_FREE(ret);
3244
    return got;
3245 3246
}

3247 3248
static virDomainPtr
phypDomainLookupByName(virConnectPtr conn, const char *lpar_name)
3249
{
3250 3251 3252 3253 3254 3255 3256
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    virDomainPtr dom = NULL;
    int lpar_id = 0;
    char *managed_system = phyp_driver->managed_system;
    unsigned char lpar_uuid[VIR_UUID_BUFLEN];
3257

3258 3259 3260
    lpar_id = phypGetLparID(session, managed_system, lpar_name, conn);
    if (lpar_id == -1)
        return NULL;
3261

3262 3263
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
        return NULL;
3264

3265 3266 3267 3268 3269 3270
    dom = virGetDomain(conn, lpar_name, lpar_uuid);

    if (dom)
        dom->id = lpar_id;

    return dom;
3271 3272
}

3273 3274
static virDomainPtr
phypDomainLookupByID(virConnectPtr conn, int lpar_id)
3275 3276
{
    ConnectionData *connection_data = conn->networkPrivateData;
3277
    phyp_driverPtr phyp_driver = conn->privateData;
3278
    LIBSSH2_SESSION *session = connection_data->session;
3279 3280 3281 3282
    virDomainPtr dom = NULL;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
    unsigned char lpar_uuid[VIR_UUID_BUFLEN];
E
Eduardo Otubo 已提交
3283

3284 3285
    char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
                                      conn);
3286

3287
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
3288
        goto cleanup;
3289

3290
    if (exit_status < 0)
3291
        goto cleanup;
3292

3293
    dom = virGetDomain(conn, lpar_name, lpar_uuid);
3294

3295 3296
    if (dom)
        dom->id = lpar_id;
3297

3298
cleanup:
3299
    VIR_FREE(lpar_name);
3300

3301
    return dom;
3302 3303
}

3304
static char *
3305
phypDomainGetXMLDesc(virDomainPtr dom, int flags)
3306
{
3307 3308
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = dom->conn->privateData;
3309
    LIBSSH2_SESSION *session = connection_data->session;
3310 3311
    virDomainDef def;
    char *managed_system = phyp_driver->managed_system;
E
Eduardo Otubo 已提交
3312

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

3315 3316 3317 3318 3319 3320 3321
    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) {
3322
        VIR_ERROR(_("Unable to determine domain's name."));
3323
        goto err;
E
Eduardo Otubo 已提交
3324 3325
    }

3326
    if (phypGetLparUUID(def.uuid, dom->id, dom->conn) == -1) {
3327
        VIR_ERROR(_("Unable to generate random uuid."));
E
Eduardo Otubo 已提交
3328 3329
        goto err;
    }
3330

3331
    if ((def.mem.max_balloon =
3332
         phypGetLparMem(dom->conn, managed_system, dom->id, 0)) == 0) {
3333
        VIR_ERROR(_("Unable to determine domain's max memory."));
3334 3335
        goto err;
    }
3336

3337
    if ((def.mem.cur_balloon =
3338
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0) {
3339
        VIR_ERROR(_("Unable to determine domain's memory."));
3340 3341
        goto err;
    }
3342

E
Eric Blake 已提交
3343
    if ((def.maxvcpus = def.vcpus =
3344
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0) {
3345
        VIR_ERROR(_("Unable to determine domain's CPU."));
3346
        goto err;
3347
    }
3348

3349
    return virDomainDefFormat(&def, flags);
3350

3351
err:
3352 3353
    return NULL;
}
3354

3355 3356 3357
static int
phypDomainResume(virDomainPtr dom)
{
3358
    int result = -1;
3359 3360 3361 3362 3363 3364 3365 3366
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;
3367

3368 3369
    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
3370 3371
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar -o on --id %d -f %s",
3372
                      dom->id, dom->name);
3373
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3374

3375
    if (exit_status < 0)
3376
        goto cleanup;
3377

3378
    result = 0;
3379

3380
cleanup:
3381
    VIR_FREE(ret);
3382 3383

    return result;
3384 3385
}

3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401
static int
phypDomainReboot(virDomainPtr dom, unsigned int flags ATTRIBUTE_UNUSED)
{
    int result = -1;
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    virConnectPtr conn = dom->conn;
    LIBSSH2_SESSION *session = connection_data->session;
    phyp_driverPtr phyp_driver = conn->privateData;
    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)
3402 3403
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418
                      " -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;

  cleanup:
    VIR_FREE(ret);

    return result;
}

3419 3420
static int
phypDomainShutdown(virDomainPtr dom)
3421
{
3422
    int result = -1;
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    virConnectPtr conn = dom->conn;
    LIBSSH2_SESSION *session = connection_data->session;
    phyp_driverPtr phyp_driver = conn->privateData;
    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)
3435 3436
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar -o shutdown --id %d", dom->id);
3437
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3438 3439

    if (exit_status < 0)
3440
        goto cleanup;
3441

3442
    result = 0;
3443

3444
cleanup:
3445
    VIR_FREE(ret);
3446 3447

    return result;
3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459
}

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)
3460
        VIR_WARN("Unable to determine domain's max memory.");
3461 3462 3463

    if ((info->memory =
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0)
3464
        VIR_WARN("Unable to determine domain's memory.");
3465 3466 3467

    if ((info->nrVirtCpu =
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
3468
        VIR_WARN("Unable to determine domain's CPU.");
3469 3470 3471 3472 3473 3474 3475

    return 0;
}

static int
phypDomainDestroy(virDomainPtr dom)
{
3476
    int result = -1;
3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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, "rmsyscfg");
    if (system_type == HMC)
3488 3489
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar --id %d", dom->id);
3490
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3491 3492

    if (exit_status < 0)
3493
        goto cleanup;
3494 3495

    if (phypUUIDTable_RemLpar(dom->conn, dom->id) == -1)
3496
        goto cleanup;
3497

3498
    dom->id = -1;
3499
    result = 0;
3500

3501
cleanup:
3502 3503
    VIR_FREE(ret);

3504
    return result;
3505
}
3506

3507 3508
static int
phypBuildLpar(virConnectPtr conn, virDomainDefPtr def)
3509
{
3510
    int result = -1;
3511 3512 3513 3514 3515 3516 3517 3518
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;
3519

3520
    if (!def->mem.cur_balloon) {
3521 3522 3523
        PHYP_ERROR(VIR_ERR_XML_ERROR, "%s",
                _("Field <memory> on the domain XML file is missing or has "
                  "invalid value."));
3524
        goto cleanup;
3525 3526
    }

3527
    if (!def->mem.max_balloon) {
3528 3529 3530
        PHYP_ERROR(VIR_ERR_XML_ERROR, "%s",
                _("Field <currentMemory> on the domain XML file is missing or "
                  "has invalid value."));
3531
        goto cleanup;
3532 3533
    }

3534 3535
    if (def->ndisks < 1) {
        PHYP_ERROR(VIR_ERR_XML_ERROR, "%s",
3536
                   _("Domain XML must contain at least one <disk> element."));
3537
        goto cleanup;
3538 3539 3540
    }

    if (!def->disks[0]->src) {
3541 3542
        PHYP_ERROR(VIR_ERR_XML_ERROR, "%s",
                   _("Field <src> under <disk> on the domain XML file is "
3543
                     "missing."));
3544
        goto cleanup;
3545 3546
    }

3547 3548
    virBufferAddLit(&buf, "mksyscfg");
    if (system_type == HMC)
3549 3550
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar -p %s -i min_mem=%d,desired_mem=%d,"
3551
                      "max_mem=%d,desired_procs=%d,virtual_scsi_adapters=%s",
3552 3553 3554
                      def->name, (int) def->mem.cur_balloon,
                      (int) def->mem.cur_balloon, (int) def->mem.max_balloon,
                      (int) def->vcpus, def->disks[0]->src);
3555
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
3556

3557
    if (exit_status < 0) {
3558
        VIR_ERROR(_("Unable to create LPAR. Reason: '%s'"), NULLSTR(ret));
3559
        goto cleanup;
3560
    }
3561

3562
    if (phypUUIDTable_AddLpar(conn, def->uuid, def->id) == -1) {
3563
        VIR_ERROR(_("Unable to add LPAR to the table"));
3564
        goto cleanup;
3565
    }
3566

3567
    result = 0;
3568

3569
cleanup:
3570
    VIR_FREE(ret);
3571 3572

    return result;
3573
}
3574

3575 3576 3577 3578
static virDomainPtr
phypDomainCreateAndStart(virConnectPtr conn,
                         const char *xml, unsigned int flags)
{
E
Eduardo Otubo 已提交
3579
    virCheckFlags(0, NULL);
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597

    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
    virDomainDefPtr def = NULL;
    virDomainPtr dom = NULL;
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    lparPtr *lpars = uuid_table->lpars;
    unsigned int i = 0;
    char *managed_system = phyp_driver->managed_system;

    virCheckFlags(0, NULL);

    if (!(def = virDomainDefParseString(phyp_driver->caps, xml,
                                        VIR_DOMAIN_XML_SECURE)))
        goto err;

    /* checking if this name already exists on this system */
3598
    if (phypGetLparID(session, managed_system, def->name, conn) != -1) {
3599
        VIR_WARN("LPAR name already exists.");
3600 3601 3602 3603 3604 3605
        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) {
3606
            VIR_WARN("LPAR ID or UUID already exists.");
3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621
            goto err;
        }
    }

    if ((dom = virGetDomain(conn, def->name, def->uuid)) == NULL)
        goto err;

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

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

    return dom;

3622
err:
3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641
    virDomainDefFree(def);
    if (dom)
        virUnrefDomain(dom);
    return NULL;
}

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

    if ((xml = virCapabilitiesFormatXML(phyp_driver->caps)) == NULL)
        virReportOOMError();

    return xml;
}

static int
3642 3643
phypDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                        unsigned int flags)
3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656
{
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    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;

3657 3658 3659 3660 3661
    if (flags != VIR_DOMAIN_VCPU_LIVE) {
        PHYP_ERROR(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
        return -1;
    }

3662 3663 3664 3665
    if ((ncpus = phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
        return 0;

    if (nvcpus > phypGetLparCPUMAX(dom)) {
3666
        VIR_ERROR(_("You are trying to set a number of CPUs bigger than "
3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681
                     "the max possible."));
        return 0;
    }

    if (ncpus > nvcpus) {
        operation = 'r';
        amount = nvcpus - ncpus;
    } else if (ncpus < nvcpus) {
        operation = 'a';
        amount = nvcpus - ncpus;
    } else
        return 0;

    virBufferAddLit(&buf, "chhwres -r proc");
    if (system_type == HMC)
3682 3683
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --id %d -o %c --procunits %d 2>&1 |sed "
3684 3685
                      "-e 's/^.*\\([0-9][0-9]*.[0-9][0-9]*\\).*$/\\1/'",
                      dom->id, operation, amount);
3686
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3687 3688

    if (exit_status < 0) {
3689
        VIR_ERROR(_
3690 3691 3692 3693 3694 3695
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    " Contact your support to enable this feature."));
    }

    VIR_FREE(ret);
    return 0;
3696 3697

}
3698

3699 3700 3701 3702 3703 3704
static int
phypDomainSetCPU(virDomainPtr dom, unsigned int nvcpus)
{
    return phypDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_VCPU_LIVE);
}

3705
static virDrvOpenStatus
3706 3707
phypVIOSDriverOpen(virConnectPtr conn,
                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
3708
                   int flags ATTRIBUTE_UNUSED)
3709
{
3710 3711 3712
    if (conn->driver->no != VIR_DRV_PHYP)
        return VIR_DRV_OPEN_DECLINED;

3713 3714 3715 3716
    return VIR_DRV_OPEN_SUCCESS;
}

static int
3717
phypVIOSDriverClose(virConnectPtr conn ATTRIBUTE_UNUSED)
3718 3719 3720 3721
{
    return 0;
}

3722 3723 3724 3725 3726 3727 3728 3729
static virDriver phypDriver = {
    VIR_DRV_PHYP, "PHYP", phypOpen,     /* open */
    phypClose,                  /* close */
    NULL,                       /* supports_feature */
    NULL,                       /* type */
    NULL,                       /* version */
    NULL,                       /* libvirtVersion (impl. in libvirt.c) */
    NULL,                       /* getHostname */
E
Eric Blake 已提交
3730
    NULL,                       /* getSysinfo */
3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
    NULL,                       /* getMaxVcpus */
    NULL,                       /* nodeGetInfo */
    phypConnectGetCapabilities, /* getCapabilities */
    phypListDomains,            /* listDomains */
    phypNumDomains,             /* numOfDomains */
    phypDomainCreateAndStart,   /* domainCreateXML */
    phypDomainLookupByID,       /* domainLookupByID */
    NULL,                       /* domainLookupByUUID */
    phypDomainLookupByName,     /* domainLookupByName */
    NULL,                       /* domainSuspend */
    phypDomainResume,           /* domainResume */
    phypDomainShutdown,         /* domainShutdown */
3743
    phypDomainReboot,           /* domainReboot */
3744 3745 3746 3747 3748
    phypDomainDestroy,          /* domainDestroy */
    NULL,                       /* domainGetOSType */
    NULL,                       /* domainGetMaxMemory */
    NULL,                       /* domainSetMaxMemory */
    NULL,                       /* domainSetMemory */
3749
    NULL,                       /* domainSetMemoryFlags */
3750 3751 3752 3753
    NULL,                       /* domainSetMemoryParameters */
    NULL,                       /* domainGetMemoryParameters */
    NULL,                       /* domainSetBlkioParameters */
    NULL,                       /* domainGetBlkioParameters */
3754 3755 3756 3757 3758
    phypDomainGetInfo,          /* domainGetInfo */
    NULL,                       /* domainSave */
    NULL,                       /* domainRestore */
    NULL,                       /* domainCoreDump */
    phypDomainSetCPU,           /* domainSetVcpus */
3759 3760
    phypDomainSetVcpusFlags,    /* domainSetVcpusFlags */
    phypDomainGetVcpusFlags,    /* domainGetVcpusFlags */
3761 3762 3763 3764 3765
    NULL,                       /* domainPinVcpu */
    NULL,                       /* domainGetVcpus */
    phypGetLparCPUMAX,          /* domainGetMaxVcpus */
    NULL,                       /* domainGetSecurityLabel */
    NULL,                       /* nodeGetSecurityModel */
3766
    phypDomainGetXMLDesc,       /* domainGetXMLDesc */
3767 3768 3769 3770 3771 3772 3773 3774
    NULL,                       /* domainXMLFromNative */
    NULL,                       /* domainXMLToNative */
    phypListDefinedDomains,     /* listDefinedDomains */
    phypNumDefinedDomains,      /* numOfDefinedDomains */
    NULL,                       /* domainCreate */
    NULL,                       /* domainCreateWithFlags */
    NULL,                       /* domainDefineXML */
    NULL,                       /* domainUndefine */
3775
    phypAttachDevice,           /* domainAttachDevice */
3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807
    NULL,                       /* domainAttachDeviceFlags */
    NULL,                       /* domainDetachDevice */
    NULL,                       /* domainDetachDeviceFlags */
    NULL,                       /* domainUpdateDeviceFlags */
    NULL,                       /* domainGetAutostart */
    NULL,                       /* domainSetAutostart */
    NULL,                       /* domainGetSchedulerType */
    NULL,                       /* domainGetSchedulerParameters */
    NULL,                       /* domainSetSchedulerParameters */
    NULL,                       /* domainMigratePrepare */
    NULL,                       /* domainMigratePerform */
    NULL,                       /* domainMigrateFinish */
    NULL,                       /* domainBlockStats */
    NULL,                       /* domainInterfaceStats */
    NULL,                       /* domainMemoryStats */
    NULL,                       /* domainBlockPeek */
    NULL,                       /* domainMemoryPeek */
    NULL,                       /* domainGetBlockInfo */
    NULL,                       /* nodeGetCellsFreeMemory */
    NULL,                       /* getFreeMemory */
    NULL,                       /* domainEventRegister */
    NULL,                       /* domainEventDeregister */
    NULL,                       /* domainMigratePrepare2 */
    NULL,                       /* domainMigrateFinish2 */
    NULL,                       /* nodeDeviceDettach */
    NULL,                       /* nodeDeviceReAttach */
    NULL,                       /* nodeDeviceReset */
    NULL,                       /* domainMigratePrepareTunnel */
    phypIsEncrypted,            /* isEncrypted */
    phypIsSecure,               /* isSecure */
    NULL,                       /* domainIsActive */
    NULL,                       /* domainIsPersistent */
3808
    phypIsUpdated,              /* domainIsUpdated */
3809 3810 3811 3812 3813
    NULL,                       /* cpuCompare */
    NULL,                       /* cpuBaseline */
    NULL,                       /* domainGetJobInfo */
    NULL,                       /* domainAbortJob */
    NULL,                       /* domainMigrateSetMaxDowntime */
3814
    NULL,                       /* domainMigrateSetMaxSpeed */
3815 3816 3817 3818 3819 3820
    NULL,                       /* domainEventRegisterAny */
    NULL,                       /* domainEventDeregisterAny */
    NULL,                       /* domainManagedSave */
    NULL,                       /* domainHasManagedSaveImage */
    NULL,                       /* domainManagedSaveRemove */
    NULL,                       /* domainSnapshotCreateXML */
3821
    NULL,                       /* domainSnapshotGetXMLDesc */
3822 3823 3824 3825 3826 3827 3828
    NULL,                       /* domainSnapshotNum */
    NULL,                       /* domainSnapshotListNames */
    NULL,                       /* domainSnapshotLookupByName */
    NULL,                       /* domainHasCurrentSnapshot */
    NULL,                       /* domainSnapshotCurrent */
    NULL,                       /* domainRevertToSnapshot */
    NULL,                       /* domainSnapshotDelete */
C
Chris Lalancette 已提交
3829
    NULL,                       /* qemuMonitorCommand */
3830 3831
    NULL,                       /* domainOpenConsole */
    NULL,                       /* domainInjectNMI */
3832 3833
};

3834 3835
static virStorageDriver phypStorageDriver = {
    .name = "PHYP",
3836 3837
    .open = phypVIOSDriverOpen,
    .close = phypVIOSDriverClose,
3838

3839 3840
    .numOfPools = phypNumOfStoragePools,
    .listPools = phypListStoragePools,
3841 3842 3843
    .numOfDefinedPools = NULL,
    .listDefinedPools = NULL,
    .findPoolSources = NULL,
3844 3845
    .poolLookupByName = phypStoragePoolLookupByName,
    .poolLookupByUUID = phypGetStoragePoolLookUpByUUID,
3846
    .poolLookupByVolume = NULL,
3847
    .poolCreateXML = phypStoragePoolCreateXML,
3848 3849 3850 3851
    .poolDefineXML = NULL,
    .poolBuild = NULL,
    .poolUndefine = NULL,
    .poolCreate = NULL,
3852
    .poolDestroy = phypDestroyStoragePool,
3853 3854 3855
    .poolDelete = NULL,
    .poolRefresh = NULL,
    .poolGetInfo = NULL,
3856
    .poolGetXMLDesc = phypGetStoragePoolXMLDesc,
3857 3858
    .poolGetAutostart = NULL,
    .poolSetAutostart = NULL,
3859 3860
    .poolNumOfVolumes = phypStoragePoolNumOfVolumes,
    .poolListVolumes = phypStoragePoolListVolumes,
3861

3862
    .volLookupByName = phypVolumeLookupByName,
3863
    .volLookupByKey = NULL,
3864 3865
    .volLookupByPath = phypVolumeLookupByPath,
    .volCreateXML = phypStorageVolCreateXML,
3866 3867 3868
    .volCreateXMLFrom = NULL,
    .volDelete = NULL,
    .volGetInfo = NULL,
3869 3870
    .volGetXMLDesc = phypVolumeGetXMLDesc,
    .volGetPath = phypVolumeGetPath,
3871 3872 3873 3874
    .poolIsActive = NULL,
    .poolIsPersistent = NULL
};

E
Eduardo Otubo 已提交
3875
static virInterfaceDriver phypInterfaceDriver = {
3876 3877 3878
    .name = "PHYP",
    .open = phypVIOSDriverOpen,
    .close = phypVIOSDriverClose,
E
Eduardo Otubo 已提交
3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890
    .numOfInterfaces = phypNumOfInterfaces,
    .listInterfaces = phypListInterfaces,
    .numOfDefinedInterfaces = NULL,
    .listDefinedInterfaces = NULL,
    .interfaceLookupByName = phypInterfaceLookupByName,
    .interfaceLookupByMACString = NULL,
    .interfaceGetXMLDesc = NULL,
    .interfaceDefineXML = phypInterfaceDefineXML,
    .interfaceUndefine = NULL,
    .interfaceCreate = NULL,
    .interfaceDestroy = phypInterfaceDestroy,
    .interfaceIsActive = phypInterfaceIsActive
3891 3892
};

3893 3894 3895
int
phypRegister(void)
{
3896 3897 3898 3899
    if (virRegisterDriver(&phypDriver) < 0)
        return -1;
    if (virRegisterStorageDriver(&phypStorageDriver) < 0)
        return -1;
E
Eduardo Otubo 已提交
3900
    if (virRegisterInterfaceDriver(&phypInterfaceDriver) < 0)
3901
        return -1;
3902

3903 3904
    return 0;
}