phyp_driver.c 108.7 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2012 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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
21
 * License along with this library.  If not, see
O
Osier Yang 已提交
22
 * <http://www.gnu.org/licenses/>.
23 24 25 26 27
 */

#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 "virauth.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"
E
Eric Blake 已提交
60
#include "virfile.h"
E
Eduardo Otubo 已提交
61
#include "interface_conf.h"
62 63 64 65 66 67 68 69 70

#include "phyp_driver.h"

#define VIR_FROM_THIS VIR_FROM_PHYP

/*
 * URI: phyp://user@[hmc|ivm]/managed_system
 * */

71 72
static unsigned const int HMC = 0;
static unsigned const int IVM = 127;
E
Eduardo Otubo 已提交
73 74
static unsigned const int PHYP_IFACENAME_SIZE = 24;
static unsigned const int PHYP_MAC_SIZE= 12;
75

76 77 78 79 80 81 82 83 84
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;
85

86 87
    timeout.tv_sec = 0;
    timeout.tv_usec = 1000;
88

89
    FD_ZERO(&fd);
90

91
    FD_SET(socket_fd, &fd);
92

93 94
    /* now make sure we wait in the correct direction */
    dir = libssh2_session_block_directions(session);
95

96 97
    if (dir & LIBSSH2_SESSION_BLOCK_INBOUND)
        readfd = &fd;
98

99 100
    if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
        writefd = &fd;
101

102
    rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
103

104 105
    return rc;
}
106

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

126 127 128 129 130
    if (VIR_ALLOC_N(buffer, buffer_size) < 0) {
        virReportOOMError();
        return NULL;
    }

131 132 133 134 135
    /* 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);
136 137
    }

138 139
    if (channel == NULL) {
        goto err;
140
    }
141

142 143 144
    while ((rc = libssh2_channel_exec(channel, cmd)) ==
           LIBSSH2_ERROR_EAGAIN) {
        waitsocket(sock, session);
145
    }
146

147 148 149
    if (rc != 0) {
        goto err;
    }
150

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

162 163 164 165 166 167 168
        /* 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 已提交
169 170
    }

171
    exitcode = 127;
172

173 174
    while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) {
        waitsocket(sock, session);
175 176
    }

177 178
    if (rc == 0) {
        exitcode = libssh2_channel_get_exit_status(channel);
179 180
    }

181 182 183
    (*exit_status) = exitcode;
    libssh2_channel_free(channel);
    channel = NULL;
184 185
    VIR_FREE(buffer);

186 187 188 189 190 191
    if (virBufferError(&tex_ret)) {
        virBufferFreeAndReset(&tex_ret);
        virReportOOMError();
        return NULL;
    }
    return virBufferContentAndReset(&tex_ret);
192 193 194 195 196 197

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

200 201 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
/* 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 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
/* 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;
}

250
static int
251
phypGetSystemType(virConnectPtr conn)
252 253
{
    ConnectionData *connection_data = conn->networkPrivateData;
254
    LIBSSH2_SESSION *session = connection_data->session;
255 256 257
    char *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
258

259 260
    if (virAsprintf(&cmd, "lshmc -V") < 0) {
        virReportOOMError();
261
        return -1;
262 263
    }
    ret = phypExec(session, cmd, &exit_status, conn);
264

265 266 267
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return exit_status;
268 269
}

270
static int
271
phypGetVIOSPartitionID(virConnectPtr conn)
272
{
273 274 275 276 277 278 279
    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;
280

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

290

291 292
static int phypDefaultConsoleType(const char *ostype ATTRIBUTE_UNUSED,
                                  const char *arch ATTRIBUTE_UNUSED)
293 294 295 296 297
{
    return VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
}


298 299 300 301 302 303
static virCapsPtr
phypCapsInit(void)
{
    struct utsname utsname;
    virCapsPtr caps;
    virCapsGuestPtr guest;
304

305
    uname(&utsname);
306

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

310 311 312 313 314 315
    /* 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);
316
        VIR_WARN
317
            ("Failed to query host NUMA topology, disabling NUMA capabilities");
318 319
    }

320 321 322
    /* XXX shouldn't 'borrow' KVM's prefix */
    virCapabilitiesSetMacPrefix(caps, (unsigned char[]) {
                                0x52, 0x54, 0x00});
323

324 325 326 327 328 329
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "linux",
                                         utsname.machine,
                                         sizeof(int) == 4 ? 32 : 8,
                                         NULL, NULL, 0, NULL)) == NULL)
        goto no_memory;
330

331 332 333
    if (virCapabilitiesAddGuestDomain(guest,
                                      "phyp", NULL, NULL, 0, NULL) == NULL)
        goto no_memory;
334

335 336
    caps->defaultConsoleTargetType = phypDefaultConsoleType;

337
    return caps;
338

339
no_memory:
340 341 342
    virCapabilitiesFree(caps);
    return NULL;
}
343

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
/* 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;
359
    int ndom = -1;
360 361 362
    char *managed_system = phyp_driver->managed_system;
    const char *state;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
363

364 365 366 367 368 369 370
    if (type == 0)
        state = "|grep Running";
    else if (type == 1) {
        if (system_type == HMC) {
            state = "|grep \"Not Activated\"";
        } else {
            state = "|grep \"Open Firmware\"";
371
        }
372 373
    } else
        state = " ";
374

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

384 385 386 387 388 389 390 391 392 393
/* 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)
394
{
395
    ConnectionData *connection_data = conn->networkPrivateData;
E
Eduardo Otubo 已提交
396
    phyp_driverPtr phyp_driver = conn->privateData;
397
    LIBSSH2_SESSION *session = connection_data->session;
E
Eduardo Otubo 已提交
398
    int system_type = phyp_driver->system_type;
399
    char *managed_system = phyp_driver->managed_system;
400
    int exit_status = 0;
401
    int got = -1;
402
    char *ret = NULL;
403
    char *line, *next_line;
404
    const char *state;
E
Eduardo Otubo 已提交
405 406
    virBuffer buf = VIR_BUFFER_INITIALIZER;

407 408 409 410 411
    if (type == 0)
        state = "|grep Running";
    else
        state = " ";

E
Eduardo Otubo 已提交
412 413
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
414 415
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F lpar_id,state %s | sed -e 's/,.*$//'",
416
                      state);
417
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
418

419
    if (exit_status < 0 || ret == NULL)
420
        goto cleanup;
421 422 423 424 425 426 427 428

    /* 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;
429
            goto cleanup;
430
        }
431 432 433 434
        got++;
        line = next_line;
        while (*line == '\n')
            line++; /* skip \n */
435
    }
436

437
cleanup:
438
    VIR_FREE(ret);
439
    return got;
440 441
}

442 443
static int
phypUUIDTable_WriteFile(virConnectPtr conn)
444
{
445 446
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
447
    unsigned int i = 0;
448 449 450 451 452
    int fd = -1;
    char local_file[] = "./uuid_table";

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

454
    for (i = 0; i < uuid_table->nlpars; i++) {
455 456 457
        if (safewrite(fd, &uuid_table->lpars[i]->id,
                      sizeof(uuid_table->lpars[i]->id)) !=
            sizeof(uuid_table->lpars[i]->id)) {
458
            VIR_ERROR(_("Unable to write information to local file."));
459 460 461 462 463
            goto err;
        }

        if (safewrite(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN) !=
            VIR_UUID_BUFLEN) {
464
            VIR_ERROR(_("Unable to write information to local file."));
465
            goto err;
466 467 468
        }
    }

469 470 471 472 473
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno, _("Could not close %s"),
                             local_file);
        goto err;
    }
474 475
    return 0;

476
err:
477
    VIR_FORCE_CLOSE(fd);
478 479 480
    return -1;
}

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

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

500 501 502 503 504 505 506 507 508 509 510
        if (virBufferError(&username)) {
            virBufferFreeAndReset(&username);
            virReportOOMError();
            goto err;
        }
    }

    if (virAsprintf
        (&remote_file, "/home/%s/libvirt_uuid_table",
         virBufferContentAndReset(&username))
        < 0) {
E
Eduardo Otubo 已提交
511
        virReportOOMError();
512
        goto err;
513 514
    }

515
    if (stat(local_file, &local_fileinfo) == -1) {
516
        VIR_WARN("Unable to stat local file.");
517 518
        goto err;
    }
519

520
    if (!(fd = fopen(local_file, "rb"))) {
521
        VIR_WARN("Unable to open local file.");
522
        goto err;
523
    }
524

525 526 527 528 529
    do {
        channel =
            libssh2_scp_send(session, remote_file,
                             0x1FF & local_fileinfo.st_mode,
                             (unsigned long) local_fileinfo.st_size);
530

531 532 533 534
        if ((!channel) && (libssh2_session_last_errno(session) !=
                           LIBSSH2_ERROR_EAGAIN))
            goto err;
    } while (!channel);
535

536 537 538 539 540 541 542 543 544 545 546 547 548
    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;
549

550 551 552 553 554 555 556 557 558 559 560 561 562
        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);
563

564 565 566 567 568 569 570 571
    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);
572 573
    return 0;

574
err:
575 576 577 578 579 580 581
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
582
    VIR_FORCE_FCLOSE(fd);
583
    return -1;
584 585 586
}

static int
587
phypUUIDTable_RemLpar(virConnectPtr conn, int id)
588
{
E
Eduardo Otubo 已提交
589
    phyp_driverPtr phyp_driver = conn->privateData;
590 591
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    unsigned int i = 0;
E
Eduardo Otubo 已提交
592

593 594 595 596 597
    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);
        }
598 599
    }

600
    if (phypUUIDTable_WriteFile(conn) == -1)
601 602
        goto err;

603
    if (phypUUIDTable_Push(conn) == -1)
604 605
        goto err;

606
    return 0;
607

608
err:
609
    return -1;
610 611
}

612 613
static int
phypUUIDTable_AddLpar(virConnectPtr conn, unsigned char *uuid, int id)
614
{
E
Eduardo Otubo 已提交
615
    phyp_driverPtr phyp_driver = conn->privateData;
616
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
E
Eduardo Otubo 已提交
617

618 619 620 621 622
    uuid_table->nlpars++;
    unsigned int i = uuid_table->nlpars;
    i--;

    if (VIR_REALLOC_N(uuid_table->lpars, uuid_table->nlpars) < 0) {
623
        virReportOOMError();
624
        goto err;
625 626
    }

627 628
    if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
        virReportOOMError();
629
        goto err;
630
    }
631

632
    uuid_table->lpars[i]->id = id;
633
    memcpy(uuid_table->lpars[i]->uuid, uuid, VIR_UUID_BUFLEN);
634

635 636
    if (phypUUIDTable_WriteFile(conn) == -1)
        goto err;
637

638
    if (phypUUIDTable_Push(conn) == -1)
639 640
        goto err;

641
    return 0;
642

643
err:
644
    return -1;
645 646
}

647 648
static int
phypUUIDTable_ReadFile(virConnectPtr conn)
649
{
E
Eduardo Otubo 已提交
650
    phyp_driverPtr phyp_driver = conn->privateData;
651 652 653 654 655 656
    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;
657

658
    if ((fd = open(local_file, O_RDONLY)) == -1) {
659
        VIR_WARN("Unable to write information to local file.");
660
        goto err;
661 662
    }

663 664 665
    /* 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++) {
666

667 668 669 670 671 672 673 674
            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 {
675
                VIR_WARN
676 677 678
                    ("Unable to read from information to local file.");
                goto err;
            }
679

680 681
            rc = read(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN);
            if (rc != VIR_UUID_BUFLEN) {
682
                VIR_WARN("Unable to read information to local file.");
683 684
                goto err;
            }
685
        }
686 687
    } else
        virReportOOMError();
688

689
    VIR_FORCE_CLOSE(fd);
690
    return 0;
691

692
err:
693
    VIR_FORCE_CLOSE(fd);
694
    return -1;
695 696
}

697 698
static int
phypUUIDTable_Pull(virConnectPtr conn)
699 700
{
    ConnectionData *connection_data = conn->networkPrivateData;
701
    LIBSSH2_SESSION *session = connection_data->session;
702 703 704 705 706 707 708 709 710 711 712 713
    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 已提交
714

715
    if (conn->uri->user != NULL) {
716
        virBufferAdd(&username, conn->uri->user, -1);
717

718 719 720 721 722 723
        if (virBufferError(&username)) {
            virBufferFreeAndReset(&username);
            virReportOOMError();
            goto err;
        }
    }
724

725 726 727 728 729 730 731
    if (virAsprintf
        (&remote_file, "/home/%s/libvirt_uuid_table",
         virBufferContentAndReset(&username))
        < 0) {
        virReportOOMError();
        goto err;
    }
732

733 734 735
    /* Trying to stat the remote file. */
    do {
        channel = libssh2_scp_recv(session, remote_file, &fileinfo);
736

737 738 739
        if (!channel) {
            if (libssh2_session_last_errno(session) !=
                LIBSSH2_ERROR_EAGAIN) {
E
Eric Blake 已提交
740
                goto err;
741 742 743 744 745
            } else {
                waitsocket(sock, session);
            }
        }
    } while (!channel);
746

747 748 749
    /* Creating a new data base based on remote file */
    if ((fd = creat(local_file, 0755)) == -1)
        goto err;
750

751 752 753 754
    /* Request a file via SCP */
    while (got < fileinfo.st_size) {
        do {
            amount = sizeof(buffer);
755

756 757 758
            if ((fileinfo.st_size - got) < amount) {
                amount = fileinfo.st_size - got;
            }
E
Eduardo Otubo 已提交
759

760 761 762
            rc = libssh2_channel_read(channel, buffer, amount);
            if (rc > 0) {
                if (safewrite(fd, buffer, rc) != rc)
763
                    VIR_WARN
764
                        ("Unable to write information to local file.");
765

766 767 768 769
                got += rc;
                total += rc;
            }
        } while (rc > 0);
770

771 772 773 774
        if ((rc == LIBSSH2_ERROR_EAGAIN)
            && (got < fileinfo.st_size)) {
            /* this is due to blocking that would occur otherwise
             * so we loop on this condition */
775

776 777 778 779 780
            waitsocket(sock, session);  /* now we wait */
            continue;
        }
        break;
    }
781 782 783 784 785
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno, _("Could not close %s"),
                             local_file);
        goto err;
    }
786

787 788 789 790 791 792 793 794 795
    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;
796

797
err:
798 799 800 801 802 803 804
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
805 806 807
    return -1;
}

808 809
static int
phypUUIDTable_Init(virConnectPtr conn)
810
{
E
Eric Blake 已提交
811
    uuid_tablePtr uuid_table = NULL;
812 813 814 815 816
    phyp_driverPtr phyp_driver;
    int nids_numdomains = 0;
    int nids_listdomains = 0;
    int *ids = NULL;
    unsigned int i = 0;
E
Eric Blake 已提交
817 818
    int ret = -1;
    bool table_created = false;
E
Eduardo Otubo 已提交
819

820
    if ((nids_numdomains = phypNumDomainsGeneric(conn, 2)) < 0)
E
Eric Blake 已提交
821
        goto cleanup;
822 823

    if (VIR_ALLOC_N(ids, nids_numdomains) < 0) {
824
        virReportOOMError();
E
Eric Blake 已提交
825
        goto cleanup;
826 827
    }

828 829
    if ((nids_listdomains =
         phypListDomainsGeneric(conn, ids, nids_numdomains, 1)) < 0)
E
Eric Blake 已提交
830
        goto cleanup;
831

832
    /* exit early if there are no domains */
E
Eric Blake 已提交
833 834 835 836 837
    if (nids_numdomains == 0 && nids_listdomains == 0) {
        ret = 0;
        goto cleanup;
    }
    if (nids_numdomains != nids_listdomains) {
838
        VIR_ERROR(_("Unable to determine number of domains."));
E
Eric Blake 已提交
839
        goto cleanup;
840
    }
841

842 843 844
    phyp_driver = conn->privateData;
    uuid_table = phyp_driver->uuid_table;
    uuid_table->nlpars = nids_listdomains;
845

846 847 848
    /* 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 已提交
849
        table_created = true;
850 851 852 853
        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 已提交
854
                    goto cleanup;
855 856
                }
                uuid_table->lpars[i]->id = ids[i];
857

858 859 860 861
                if (virUUIDGenerate(uuid_table->lpars[i]->uuid) < 0)
                    VIR_WARN("Unable to generate UUID for domain %d",
                             ids[i]);
            }
E
Eduardo Otubo 已提交
862
        } else {
863
            virReportOOMError();
E
Eric Blake 已提交
864
            goto cleanup;
E
Eduardo Otubo 已提交
865
        }
866

867
        if (phypUUIDTable_WriteFile(conn) == -1)
E
Eric Blake 已提交
868
            goto cleanup;
869

870
        if (phypUUIDTable_Push(conn) == -1)
E
Eric Blake 已提交
871
            goto cleanup;
872 873
    } else {
        if (phypUUIDTable_ReadFile(conn) == -1)
E
Eric Blake 已提交
874
            goto cleanup;
875
    }
876

E
Eric Blake 已提交
877
    ret = 0;
878

E
Eric Blake 已提交
879 880 881 882 883 884 885
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);
    }
886
    VIR_FREE(ids);
E
Eric Blake 已提交
887
    return ret;
888 889
}

890 891
static void
phypUUIDTable_Free(uuid_tablePtr uuid_table)
892
{
893
    int i;
894

895 896 897 898 899 900 901 902
    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);
903 904
}

905 906 907 908 909 910 911 912
#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)
913
{
914
    size_t len = strlen(src);
915 916
    size_t i = 0;

917
    if (len == 0)
918
        return false;
919

920 921
    for (i = 0; i < len; i++) {
        switch (src[i]) {
922 923 924 925
        SPECIALCHARACTER_CASES
            return true;
        default:
            continue;
926 927 928
        }
    }

929 930
    return false;
}
931

932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
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;
960 961
}

962 963 964
static LIBSSH2_SESSION *
openSSHSession(virConnectPtr conn, virConnectAuthPtr auth,
               int *internal_socket)
965
{
966 967 968 969 970 971 972 973 974 975 976
    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;
977
    char *userhome = virGetUserDirectory();
978
    struct stat pvt_stat, pub_stat;
979

980 981
    if (userhome == NULL)
        goto err;
E
Eduardo Otubo 已提交
982

983
    if (virAsprintf(&pubkey, "%s/.ssh/id_rsa.pub", userhome) < 0) {
984
        virReportOOMError();
985
        goto err;
986 987
    }

988 989
    if (virAsprintf(&pvtkey, "%s/.ssh/id_rsa", userhome) < 0) {
        virReportOOMError();
990 991 992
        goto err;
    }

993 994
    if (conn->uri->user != NULL) {
        username = strdup(conn->uri->user);
995

996 997 998 999 1000 1001
        if (username == NULL) {
            virReportOOMError();
            goto err;
        }
    } else {
        if (auth == NULL || auth->cb == NULL) {
1002 1003
            virReportError(VIR_ERR_AUTH_FAILED,
                           "%s", _("No authentication callback provided."));
1004 1005
            goto err;
        }
1006

1007
        username = virAuthGetUsername(conn, auth, "ssh", NULL, conn->uri->server);
1008

1009
        if (username == NULL) {
1010 1011
            virReportError(VIR_ERR_AUTH_FAILED, "%s",
                           _("Username request failed"));
1012 1013 1014
            goto err;
        }
    }
1015

1016 1017 1018 1019
    memset(&hints, 0, sizeof(hints));
    hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
1020

1021 1022
    ret = getaddrinfo(hostname, "22", &hints, &ai);
    if (ret != 0) {
1023 1024
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Error while getting %s address info"), hostname);
1025 1026
        goto err;
    }
1027

1028 1029 1030 1031 1032 1033 1034
    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;
            }
1035
            VIR_FORCE_CLOSE(sock);
1036 1037 1038
        }
        cur = cur->ai_next;
    }
1039

1040 1041
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Failed to connect to %s"), hostname);
1042 1043
    freeaddrinfo(ai);
    goto err;
1044

1045
connected:
1046

1047
    (*internal_socket) = sock;
1048

1049 1050 1051
    /* Create a session instance */
    session = libssh2_session_init();
    if (!session)
1052 1053
        goto err;

1054 1055
    /* tell libssh2 we want it all done non-blocking */
    libssh2_session_set_blocking(session, 0);
1056

1057 1058 1059
    while ((rc = libssh2_session_startup(session, sock)) ==
           LIBSSH2_ERROR_EAGAIN) ;
    if (rc) {
1060 1061
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Failure establishing SSH session."));
1062 1063
        goto disconnect;
    }
1064

1065 1066 1067 1068 1069
    /* Trying authentication by pubkey */
    if (stat(pvtkey, &pvt_stat) || stat(pubkey, &pub_stat)) {
        rc = LIBSSH2_ERROR_SOCKET_NONE;
        goto keyboard_interactive;
    }
1070

1071 1072 1073 1074 1075 1076
    while ((rc =
            libssh2_userauth_publickey_fromfile(session, username,
                                                pubkey,
                                                pvtkey,
                                                NULL)) ==
           LIBSSH2_ERROR_EAGAIN) ;
1077

1078
keyboard_interactive:
1079 1080 1081 1082
    if (rc == LIBSSH2_ERROR_SOCKET_NONE
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED) {
        if (auth == NULL || auth->cb == NULL) {
1083 1084
            virReportError(VIR_ERR_AUTH_FAILED,
                           "%s", _("No authentication callback provided."));
1085 1086
            goto disconnect;
        }
1087

1088
        password = virAuthGetPassword(conn, auth, "ssh", username, conn->uri->server);
1089

1090
        if (password == NULL) {
1091 1092
            virReportError(VIR_ERR_AUTH_FAILED, "%s",
                           _("Password request failed"));
1093 1094
            goto disconnect;
        }
1095

1096 1097 1098 1099
        while ((rc =
                libssh2_userauth_password(session, username,
                                          password)) ==
               LIBSSH2_ERROR_EAGAIN) ;
1100

1101
        if (rc) {
1102 1103
            virReportError(VIR_ERR_AUTH_FAILED,
                           "%s", _("Authentication failed"));
1104 1105 1106
            goto disconnect;
        } else
            goto exit;
1107

1108 1109
    } else if (rc == LIBSSH2_ERROR_NONE) {
        goto exit;
1110

1111 1112
    } else if (rc == LIBSSH2_ERROR_ALLOC || rc == LIBSSH2_ERROR_SOCKET_SEND
               || rc == LIBSSH2_ERROR_SOCKET_TIMEOUT) {
1113 1114 1115
        goto err;
    }

1116
disconnect:
1117 1118
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1119
err:
1120 1121 1122 1123 1124
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
1125
    return NULL;
1126

1127
exit:
1128 1129 1130 1131 1132 1133
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
    return session;
1134 1135
}

1136 1137
static virDrvOpenStatus
phypOpen(virConnectPtr conn,
E
Eric Blake 已提交
1138
         virConnectAuthPtr auth, unsigned int flags)
1139 1140 1141 1142 1143 1144 1145 1146
{
    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 已提交
1147

E
Eric Blake 已提交
1148 1149
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1150 1151 1152 1153 1154 1155 1156
    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) {
1157 1158
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing server name in phyp:// URI"));
1159 1160 1161 1162
        return VIR_DRV_OPEN_ERROR;
    }

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

1167 1168 1169 1170
    if (VIR_ALLOC(uuid_table) < 0) {
        virReportOOMError();
        goto failure;
    }
1171

1172 1173 1174 1175
    if (VIR_ALLOC(connection_data) < 0) {
        virReportOOMError();
        goto failure;
    }
1176

1177 1178 1179 1180 1181 1182
    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 已提交
1183

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
        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';

1197
        if (contains_specialcharacters(conn->uri->path)) {
1198 1199 1200
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s",
                           _("Error parsing 'path'. Invalid characters."));
1201 1202 1203 1204 1205
            goto failure;
        }
    }

    if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) {
1206 1207
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Error while opening SSH session."));
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
        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) {
1221
        virReportOOMError();
1222
        goto failure;
1223 1224
    }

1225 1226
    conn->privateData = phyp_driver;
    conn->networkPrivateData = connection_data;
1227

1228 1229
    if ((phyp_driver->system_type = phypGetSystemType(conn)) == -1)
        goto failure;
1230

1231 1232
    if (phypUUIDTable_Init(conn) == -1)
        goto failure;
1233

1234 1235 1236 1237 1238 1239 1240
    if (phyp_driver->system_type == HMC) {
        if ((phyp_driver->vios_id = phypGetVIOSPartitionID(conn)) == -1)
            goto failure;
    }

    return VIR_DRV_OPEN_SUCCESS;

1241
failure:
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
    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;
1258 1259 1260
}

static int
1261
phypClose(virConnectPtr conn)
1262
{
1263 1264 1265
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
1266

1267 1268
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1269

1270 1271 1272 1273 1274 1275 1276
    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;
}
1277 1278


1279 1280 1281 1282 1283 1284
static int
phypIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Phyp uses an SSH tunnel, so is always encrypted */
    return 1;
}
1285

1286 1287 1288 1289 1290 1291

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

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

static int
phypIsAlive(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;

    /* XXX we should be able to do something better but this is simple, safe,
     * and good enough for now. In worst case, the function will return true
     * even though the connection is not alive.
     */
    if (connection_data && connection_data->session)
        return 1;
    else
        return 0;
}


1311 1312 1313 1314 1315
static int
phypIsUpdated(virDomainPtr conn ATTRIBUTE_UNUSED)
{
    return 0;
}
1316 1317

/* return the lpar_id given a name and a managed system name */
1318
static int
1319 1320
phypGetLparID(LIBSSH2_SESSION * session, const char *managed_system,
              const char *name, virConnectPtr conn)
1321
{
1322
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1323
    int system_type = phyp_driver->system_type;
1324
    int lpar_id = -1;
E
Eduardo Otubo 已提交
1325 1326
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1327
    virBufferAddLit(&buf, "lssyscfg -r lpar");
E
Eduardo Otubo 已提交
1328
    if (system_type == HMC)
1329 1330
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --filter lpar_names=%s -F lpar_id", name);
E
Eric Blake 已提交
1331
    phypExecInt(session, &buf, conn, &lpar_id);
1332
    return lpar_id;
1333 1334
}

1335 1336 1337 1338
/* 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)
1339 1340
{
    phyp_driverPtr phyp_driver = conn->privateData;
1341 1342 1343 1344
    int system_type = phyp_driver->system_type;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1345

1346 1347
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
1348 1349
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --filter lpar_ids=%d -F name", lpar_id);
1350
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1351

1352
    if (exit_status < 0)
1353 1354
        VIR_FREE(ret);
    return ret;
1355 1356
}

1357 1358 1359 1360 1361 1362 1363 1364 1365

/* 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)
1366 1367
{
    phyp_driverPtr phyp_driver = conn->privateData;
1368 1369 1370
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    lparPtr *lpars = uuid_table->lpars;
    unsigned int i = 0;
1371

1372 1373
    for (i = 0; i < uuid_table->nlpars; i++) {
        if (lpars[i]->id == lpar_id) {
1374
            memcpy(uuid, lpars[i]->uuid, VIR_UUID_BUFLEN);
1375 1376 1377
            return 0;
        }
    }
1378

1379
    return -1;
1380 1381
}

1382 1383 1384 1385 1386 1387 1388 1389
/*
 * type:
 * 0 - maxmem
 * 1 - memory
 * */
static unsigned long
phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id,
               int type)
1390
{
1391 1392 1393 1394 1395 1396
    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;
1397

1398 1399
    if (type != 1 && type != 0)
        return 0;
1400

1401 1402
    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
1403 1404
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1405 1406
                      " -r mem --level lpar -F %s --filter lpar_ids=%d",
                      type ? "curr_mem" : "curr_max_mem", lpar_id);
E
Eric Blake 已提交
1407
    phypExecInt(session, &buf, conn, &memory);
1408
    return memory;
1409 1410
}

1411 1412 1413
static unsigned long
phypGetLparCPUGeneric(virConnectPtr conn, const char *managed_system,
                      int lpar_id, int type)
1414
{
1415
    ConnectionData *connection_data = conn->networkPrivateData;
1416
    LIBSSH2_SESSION *session = connection_data->session;
1417
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1418
    int system_type = phyp_driver->system_type;
1419
    int vcpus = 0;
E
Eduardo Otubo 已提交
1420
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1421

1422
    virBufferAddLit(&buf, "lshwres");
E
Eduardo Otubo 已提交
1423
    if (system_type == HMC)
1424 1425
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1426 1427
                      " -r proc --level lpar -F %s --filter lpar_ids=%d",
                      type ? "curr_max_procs" : "curr_procs", lpar_id);
E
Eric Blake 已提交
1428
    phypExecInt(session, &buf, conn, &vcpus);
1429
    return vcpus;
1430
}
1431

1432 1433 1434 1435
static unsigned long
phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id)
{
    return phypGetLparCPUGeneric(conn, managed_system, lpar_id, 0);
1436 1437
}

1438
static int
1439
phypDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
1440 1441 1442
{
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    char *managed_system = phyp_driver->managed_system;
1443

1444
    if (flags != (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_MAXIMUM)) {
1445
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
1446 1447 1448
        return -1;
    }

1449 1450 1451
    return phypGetLparCPUGeneric(dom->conn, managed_system, dom->id, 1);
}

1452 1453 1454 1455 1456 1457 1458
static int
phypGetLparCPUMAX(virDomainPtr dom)
{
    return phypDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                         VIR_DOMAIN_VCPU_MAXIMUM));
}

1459 1460 1461
static int
phypGetRemoteSlot(virConnectPtr conn, const char *managed_system,
                  const char *lpar_name)
1462
{
1463 1464
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
1465
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1466
    int system_type = phyp_driver->system_type;
1467
    int remote_slot = -1;
E
Eduardo Otubo 已提交
1468 1469
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1470
    virBufferAddLit(&buf, "lshwres");
E
Eduardo Otubo 已提交
1471
    if (system_type == HMC)
1472 1473
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r virtualio --rsubtype scsi -F "
1474
                      "remote_slot_num --filter lpar_names=%s", lpar_name);
E
Eric Blake 已提交
1475
    phypExecInt(session, &buf, conn, &remote_slot);
1476
    return remote_slot;
1477 1478
}

1479 1480 1481 1482 1483 1484
/* 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)
1485
{
1486 1487
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
1488
    phyp_driverPtr phyp_driver = conn->privateData;
1489 1490 1491 1492 1493 1494 1495
    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;
1496

1497 1498 1499 1500 1501 1502
    if ((remote_slot =
         phypGetRemoteSlot(conn, managed_system, lpar_name)) == -1)
        return NULL;

    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
1503 1504
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r virtualio --rsubtype scsi -F "
1505
                      "backing_devices --filter slots=%d", remote_slot);
1506
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1507

1508
    if (exit_status < 0 || ret == NULL)
1509
        goto cleanup;
1510

1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
    /* 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
1525
            goto cleanup;
1526 1527 1528 1529 1530

        backing_device = strdup(char_ptr);

        if (backing_device == NULL) {
            virReportOOMError();
1531
            goto cleanup;
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
        }
    } else {
        backing_device = ret;
        ret = NULL;
    }

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

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

1543
cleanup:
1544
    VIR_FREE(ret);
1545

1546
    return backing_device;
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
}

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)
1563 1564
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1565 1566
                      " -r prof --filter lpar_ids=%d -F name|head -n 1",
                      lpar_id);
1567
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1568

1569
    if (exit_status < 0)
1570 1571
        VIR_FREE(ret);
    return ret;
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
}

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;
1584
    int slot = -1;
1585 1586 1587
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!(profile = phypGetLparProfile(conn, vios_id))) {
1588
        VIR_ERROR(_("Unable to get VIOS profile name."));
1589
        return -1;
1590 1591 1592 1593 1594
    }

    virBufferAddLit(&buf, "lssyscfg");

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

1597
    virBufferAsprintf(&buf, " -r prof --filter "
1598 1599 1600 1601 1602
                      "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 已提交
1603 1604 1605
    if (phypExecInt(session, &buf, conn, &slot) < 0)
        return -1;
    return slot + 1;
1606 1607 1608 1609 1610
}

static int
phypCreateServerSCSIAdapter(virConnectPtr conn)
{
1611
    int result = -1;
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
    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))) {
1628
        VIR_ERROR(_("Unable to get VIOS name"));
1629
        goto cleanup;
1630 1631 1632
    }

    if (!(profile = phypGetLparProfile(conn, vios_id))) {
1633
        VIR_ERROR(_("Unable to get VIOS profile name."));
1634
        goto cleanup;
1635 1636 1637
    }

    if ((slot = phypGetVIOSNextSlotNumber(conn)) == -1) {
1638
        VIR_ERROR(_("Unable to get free slot number"));
1639
        goto cleanup;
1640 1641 1642 1643 1644 1645 1646
    }

    /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
     * be appended to this list
     * */
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
1647 1648
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r prof --filter lpar_ids=%d,profile_names=%s"
1649 1650
                      " -F virtual_scsi_adapters|sed -e s/\\\"//g",
                      vios_id, profile);
1651
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1652 1653

    if (exit_status < 0 || ret == NULL)
1654
        goto cleanup;
1655 1656 1657 1658 1659 1660

    /* 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)
1661 1662
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r prof -i 'name=%s,lpar_id=%d,"
1663 1664
                      "\"virtual_scsi_adapters=%s,%d/server/any/any/1\"'",
                      vios_name, vios_id, ret, slot);
1665
    VIR_FREE(ret);
1666
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1667 1668

    if (exit_status < 0 || ret == NULL)
1669
        goto cleanup;
1670 1671 1672 1673 1674 1675

    /* 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)
1676 1677
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1678 1679
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      vios_name, slot);
1680
    VIR_FREE(ret);
1681
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1682 1683

    if (exit_status < 0 || ret == NULL)
1684
        goto cleanup;
1685

1686
    result = 0;
1687

1688
cleanup:
1689 1690 1691
    VIR_FREE(profile);
    VIR_FREE(vios_name);
    VIR_FREE(ret);
1692 1693

    return result;
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
}

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)
1710
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1711 1712
                          managed_system, vios_id);

1713
    virBufferAsprintf(&buf, "lsmap -all -field svsa backing -fmt , ");
1714 1715 1716 1717

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

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

1721
    if (exit_status < 0)
1722 1723
        VIR_FREE(ret);
    return ret;
1724 1725 1726 1727 1728 1729
}


static int
phypAttachDevice(virDomainPtr domain, const char *xml)
{
1730
    int result = -1;
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
    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;

E
Eric Blake 已提交
1749 1750 1751 1752 1753
    if (VIR_ALLOC(def) < 0) {
        virReportOOMError();
        goto cleanup;
    }

1754
    domain_name = escape_specialcharacters(domain->name);
1755

1756
    if (domain_name == NULL) {
1757
        goto cleanup;
1758 1759 1760 1761 1762 1763
    }

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

    if (def->os.type == NULL) {
        virReportOOMError();
1764
        goto cleanup;
1765 1766 1767 1768 1769
    }

    dev = virDomainDeviceDefParse(phyp_driver->caps, def, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
    if (!dev) {
1770
        goto cleanup;
1771 1772 1773 1774 1775
    }

    if (!
        (vios_name =
         phypGetLparNAME(session, managed_system, vios_id, conn))) {
1776
        VIR_ERROR(_("Unable to get VIOS name"));
1777
        goto cleanup;
1778 1779 1780 1781 1782 1783 1784 1785
    }

    /* 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) {
1786
            VIR_ERROR(_("Unable to create new virtual adapter"));
1787
            goto cleanup;
1788 1789
        } else {
            if (!(scsi_adapter = phypGetVIOSFreeSCSIAdapter(conn))) {
1790
                VIR_ERROR(_("Unable to create new virtual adapter"));
1791
                goto cleanup;
1792 1793 1794 1795 1796
            }
        }
    }

    if (system_type == HMC)
1797
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1798 1799
                          managed_system, vios_id);

1800
    virBufferAsprintf(&buf, "mkvdev -vdev %s -vadapter %s",
1801 1802 1803 1804
                      dev->data.disk->src, scsi_adapter);

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1805
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1806 1807

    if (exit_status < 0 || ret == NULL)
1808
        goto cleanup;
1809 1810

    if (!(profile = phypGetLparProfile(conn, domain->id))) {
1811
        VIR_ERROR(_("Unable to get VIOS profile name."));
1812
        goto cleanup;
1813 1814 1815 1816 1817 1818
    }

    /* Let's get the slot number for the adapter we just created
     * */
    virBufferAddLit(&buf, "lshwres -r virtualio --rsubtype scsi");
    if (system_type == HMC)
1819 1820
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1821 1822
                      " slot_num,backing_device|grep %s|cut -d, -f1",
                      dev->data.disk->src);
E
Eric Blake 已提交
1823
    if (phypExecInt(session, &buf, conn, &slot) < 0)
1824
        goto cleanup;
1825 1826 1827 1828 1829 1830

    /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
     * be appended to this list
     * */
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
1831 1832
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1833 1834 1835
                      " -r prof --filter lpar_ids=%d,profile_names=%s"
                      " -F virtual_scsi_adapters|sed -e 's/\"//g'",
                      vios_id, profile);
1836
    VIR_FREE(ret);
1837
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1838 1839

    if (exit_status < 0 || ret == NULL)
1840
        goto cleanup;
1841 1842 1843 1844 1845 1846

    /* 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)
1847 1848
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1849 1850 1851 1852
                      " -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 已提交
1853
    if (phypExecInt(session, &buf, conn, &slot) < 0)
1854
        goto cleanup;
1855 1856 1857 1858 1859 1860

    /* 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)
1861 1862
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1863 1864
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      domain_name, slot);
1865
    VIR_FREE(ret);
1866
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1867 1868

    if (exit_status < 0 || ret == NULL) {
1869
        VIR_ERROR(_
1870 1871
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    "Contact your support to enable this feature."));
1872
        goto cleanup;
1873 1874
    }

1875
    result = 0;
1876

1877
cleanup:
1878
    VIR_FREE(ret);
1879 1880
    virDomainDeviceDefFree(dev);
    virDomainDefFree(def);
1881 1882
    VIR_FREE(vios_name);
    VIR_FREE(scsi_adapter);
1883 1884 1885 1886
    VIR_FREE(profile);
    VIR_FREE(domain_name);

    return result;
1887 1888
}

1889 1890
static char *
phypVolumeGetKey(virConnectPtr conn, const char *name)
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
{
    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)
1903
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1904 1905
                          managed_system, vios_id);

1906
    virBufferAsprintf(&buf, "lslv %s -field lvid", name);
1907 1908 1909 1910

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

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

1914
    if (exit_status < 0)
1915 1916
        VIR_FREE(ret);
    return ret;
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
}

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)
1933
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1934 1935
                          managed_system, vios_id);

1936
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field name", name);
1937 1938 1939 1940

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

1941
    virBufferAsprintf(&buf, "|sed '1d; s/ //g'");
1942
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1943

1944
    if (exit_status < 0)
1945 1946
        VIR_FREE(ret);
    return ret;
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
}

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;
1958
    int sp_size = -1;
1959 1960 1961
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

1965
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field size", name);
1966 1967 1968 1969

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

1970
    virBufferAsprintf(&buf, "|sed '1d; s/ //g'");
E
Eric Blake 已提交
1971
    phypExecInt(session, &buf, conn, &sp_size);
1972
    return sp_size;
1973 1974
}

1975
static char *
1976
phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
1977
                unsigned int capacity)
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
{
    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;
1988
    char *key = NULL;
1989 1990

    if (system_type == HMC)
1991
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1992 1993
                          managed_system, vios_id);

1994
    virBufferAsprintf(&buf, "mklv -lv %s %s %d", lvname, spname, capacity);
1995 1996 1997

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1998
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1999 2000

    if (exit_status < 0) {
2001
        VIR_ERROR(_("Unable to create Volume: %s"), NULLSTR(ret));
2002
        goto cleanup;
2003 2004
    }

2005 2006
    key = phypVolumeGetKey(conn, lvname);

2007
cleanup:
2008 2009
    VIR_FREE(ret);

2010
    return key;
2011 2012 2013 2014 2015
}

static virStorageVolPtr
phypVolumeLookupByName(virStoragePoolPtr pool, const char *volname)
{
2016 2017
    char *key;
    virStorageVolPtr vol;
2018

2019
    key = phypVolumeGetKey(pool->conn, volname);
2020

2021
    if (key == NULL)
2022 2023
        return NULL;

2024
    vol = virGetStorageVol(pool->conn, pool->name, volname, key, NULL, NULL);
2025 2026 2027 2028

    VIR_FREE(key);

    return vol;
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
}

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

    virStorageVolDefPtr voldef = NULL;
    virStoragePoolDefPtr spdef = NULL;
    virStorageVolPtr vol = NULL;
2040
    virStorageVolPtr dup_vol = NULL;
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
    char *key = NULL;

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

    /* Filling spdef manually
     * */
    if (pool->name != NULL) {
        spdef->name = pool->name;
    } else {
2053
        VIR_ERROR(_("Unable to determine storage pool's name."));
2054 2055 2056 2057
        goto err;
    }

    if (memcpy(spdef->uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
2058
        VIR_ERROR(_("Unable to determine storage pool's uuid."));
2059 2060 2061 2062 2063
        goto err;
    }

    if ((spdef->capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2064
        VIR_ERROR(_("Unable to determine storage pools's size."));
2065 2066 2067
        goto err;
    }

J
Ján Tomko 已提交
2068
    /* Information not available */
2069 2070 2071 2072 2073 2074 2075 2076
    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) {
2077
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
2078 2079 2080 2081
        goto err;
    }

    if ((voldef = virStorageVolDefParseString(spdef, xml)) == NULL) {
2082
        VIR_ERROR(_("Error parsing volume XML."));
2083 2084 2085 2086
        goto err;
    }

    /* checking if this name already exists on this system */
2087
    if ((dup_vol = phypVolumeLookupByName(pool, voldef->name)) != NULL) {
2088
        VIR_ERROR(_("StoragePool name already exists."));
2089
        virObjectUnref(dup_vol);
2090 2091 2092 2093 2094 2095 2096
        goto err;
    }

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

    if (voldef->capacity) {
2102
        VIR_ERROR(_("Capacity cannot be empty."));
2103 2104 2105
        goto err;
    }

2106 2107 2108 2109
    key = phypBuildVolume(pool->conn, voldef->name, spdef->name,
                          voldef->capacity);

    if (key == NULL)
2110 2111 2112 2113
        goto err;

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

2117 2118
    VIR_FREE(key);

2119 2120
    return vol;

2121
err:
2122
    VIR_FREE(key);
2123 2124
    virStorageVolDefFree(voldef);
    virStoragePoolDefFree(spdef);
2125
    virObjectUnref(vol);
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
    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)
2144
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2145 2146
                          managed_system, vios_id);

2147
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field pvname", sp);
2148 2149 2150 2151

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

2152
    virBufferAsprintf(&buf, "|sed 1d");
2153
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2154

2155
    if (exit_status < 0)
2156 2157
        VIR_FREE(ret);
    return ret;
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
}

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;
2170
    char *ret = NULL;
2171 2172
    char *key = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2173
    virStorageVolPtr vol = NULL;
2174 2175

    if (system_type == HMC)
2176
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2177 2178
                          managed_system, vios_id);

2179
    virBufferAsprintf(&buf, "lslv %s -field vgname", volname);
2180 2181 2182 2183

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

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

2187
    if (exit_status < 0 || ret == NULL)
2188
        goto cleanup;
2189

2190
    key = phypVolumeGetKey(conn, volname);
2191

2192
    if (key == NULL)
2193
        goto cleanup;
2194

2195
    vol = virGetStorageVol(conn, ret, volname, key, NULL, NULL);
2196

2197
cleanup:
2198
    VIR_FREE(ret);
2199 2200 2201
    VIR_FREE(key);

    return vol;
2202 2203 2204 2205 2206 2207
}

static int
phypGetStoragePoolUUID(virConnectPtr conn, unsigned char *uuid,
                       const char *name)
{
2208
    int result = -1;
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
    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)
2220
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2221 2222
                          managed_system, vios_id);

2223
    virBufferAsprintf(&buf, "lsdev -dev %s -attr vgserial_id", name);
2224 2225 2226 2227

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

2228
    virBufferAsprintf(&buf, "|sed '1,2d'");
2229
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2230 2231

    if (exit_status < 0 || ret == NULL)
2232
        goto cleanup;
2233

2234
    if (memcpy(uuid, ret, VIR_UUID_BUFLEN) == NULL)
2235
        goto cleanup;
2236

2237
    result = 0;
2238

2239
cleanup:
2240
    VIR_FREE(ret);
2241 2242

    return result;
2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
}

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

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

2253
    return virGetStoragePool(conn, name, uuid, NULL, NULL);
2254 2255 2256 2257 2258
}

static char *
phypVolumeGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
{
2259 2260 2261
    virStorageVolDef voldef;
    virStoragePoolDef pool;
    virStoragePoolPtr sp;
2262
    char *xml = NULL;
2263

2264 2265 2266
    virCheckFlags(0, NULL);

    memset(&voldef, 0, sizeof(virStorageVolDef));
2267
    memset(&pool, 0, sizeof(virStoragePoolDef));
2268

2269
    sp = phypStoragePoolLookupByName(vol->conn, vol->pool);
2270 2271

    if (!sp)
2272
        goto cleanup;
2273 2274 2275 2276

    if (sp->name != NULL) {
        pool.name = sp->name;
    } else {
2277
        VIR_ERROR(_("Unable to determine storage sp's name."));
2278
        goto cleanup;
2279 2280
    }

2281
    if (memcpy(pool.uuid, sp->uuid, VIR_UUID_BUFLEN) == NULL) {
2282
        VIR_ERROR(_("Unable to determine storage sp's uuid."));
2283
        goto cleanup;
2284 2285 2286
    }

    if ((pool.capacity = phypGetStoragePoolSize(sp->conn, sp->name)) == -1) {
2287
        VIR_ERROR(_("Unable to determine storage sps's size."));
2288
        goto cleanup;
2289 2290
    }

J
Ján Tomko 已提交
2291
    /* Information not available */
2292 2293 2294 2295 2296 2297 2298
    pool.allocation = 0;
    pool.available = 0;

    pool.source.ndevice = 1;

    if ((pool.source.adapter =
         phypGetStoragePoolDevice(sp->conn, sp->name)) == NULL) {
2299
        VIR_ERROR(_("Unable to determine storage sps's source adapter."));
2300
        goto cleanup;
2301 2302 2303 2304 2305
    }

    if (vol->name != NULL)
        voldef.name = vol->name;
    else {
2306
        VIR_ERROR(_("Unable to determine storage pool's name."));
2307
        goto cleanup;
2308 2309
    }

2310 2311 2312 2313
    voldef.key = strdup(vol->key);

    if (voldef.key == NULL) {
        virReportOOMError();
2314
        goto cleanup;
2315 2316 2317 2318
    }

    voldef.type = VIR_STORAGE_POOL_LOGICAL;

2319 2320 2321 2322
    xml = virStorageVolDefFormat(&pool, &voldef);

    VIR_FREE(voldef.key);

2323
cleanup:
2324
    virObjectUnref(sp);
2325
    return xml;
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
}

/* 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;
2347
    char *ret = NULL;
2348 2349
    char *path = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2350
    char *pv;
2351 2352

    if (system_type == HMC)
2353
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2354 2355
                          managed_system, vios_id);

2356
    virBufferAsprintf(&buf, "lslv %s -field vgname", vol->name);
2357 2358 2359 2360

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

2361
    virBufferAsprintf(&buf,
2362
                      "|sed -e 's/^VOLUME GROUP://g' -e 's/ //g'");
2363
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2364

2365
    if (exit_status < 0 || ret == NULL)
2366
        goto cleanup;
2367

2368
    pv = phypVolumeGetPhysicalVolumeByStoragePool(vol, ret);
2369

2370 2371
    if (!pv)
        goto cleanup;
2372

2373
    if (virAsprintf(&path, "/%s/%s/%s", pv, ret, vol->name) < 0) {
2374 2375 2376
        virReportOOMError();
        goto cleanup;
    }
2377

2378
cleanup:
2379
    VIR_FREE(ret);
2380
    VIR_FREE(path);
2381 2382

    return path;
2383 2384 2385 2386 2387 2388
}

static int
phypStoragePoolListVolumes(virStoragePoolPtr pool, char **const volumes,
                           int nvolumes)
{
2389
    bool success = false;
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401
    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 已提交
2402
    char *char_ptr = NULL;
2403 2404 2405
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2406
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2407 2408
                          managed_system, vios_id);

2409
    virBufferAsprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
2410 2411 2412 2413

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

2414
    virBufferAsprintf(&buf, "|sed '1,2d'");
2415
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2416 2417 2418

    /* I need to parse the textual return in order to get the volumes */
    if (exit_status < 0 || ret == NULL)
2419
        goto cleanup;
2420 2421 2422 2423
    else {
        volumes_list = ret;

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

E
Eric Blake 已提交
2426 2427
            if (char_ptr) {
                *char_ptr = '\0';
2428 2429
                if ((volumes[got++] = strdup(volumes_list)) == NULL) {
                    virReportOOMError();
2430
                    goto cleanup;
2431
                }
E
Eric Blake 已提交
2432 2433
                char_ptr++;
                volumes_list = char_ptr;
2434 2435 2436 2437 2438
            } else
                break;
        }
    }

2439 2440
    success = true;

2441
cleanup:
2442 2443 2444 2445 2446 2447
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(volumes[i]);

        got = -1;
    }
2448
    VIR_FREE(ret);
2449
    return got;
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
}

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;
2460
    int nvolumes = -1;
2461 2462 2463 2464 2465
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2466
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2467
                          managed_system, vios_id);
2468
    virBufferAsprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
2469 2470
    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2471
    virBufferAsprintf(&buf, "|grep -c '^.*$'");
E
Eric Blake 已提交
2472 2473
    if (phypExecInt(session, &buf, conn, &nvolumes) < 0)
        return -1;
2474 2475

    /* We need to remove 2 line from the header text output */
E
Eric Blake 已提交
2476
    return nvolumes - 2;
2477 2478 2479 2480 2481
}

static int
phypDestroyStoragePool(virStoragePoolPtr pool)
{
2482
    int result = -1;
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
    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)
2495
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2496 2497
                          managed_system, vios_id);

2498
    virBufferAsprintf(&buf, "rmsp %s", pool->name);
2499 2500 2501

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2502
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2503 2504

    if (exit_status < 0) {
2505
        VIR_ERROR(_("Unable to destroy Storage Pool: %s"), NULLSTR(ret));
2506
        goto cleanup;
2507 2508
    }

2509
    result = 0;
2510

2511
cleanup:
2512
    VIR_FREE(ret);
2513 2514

    return result;
2515 2516 2517 2518 2519
}

static int
phypBuildStoragePool(virConnectPtr conn, virStoragePoolDefPtr def)
{
2520
    int result = -1;
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
    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)
2533
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2534 2535
                          managed_system, vios_id);

2536
    virBufferAsprintf(&buf, "mksp -f %schild %s", def->name,
2537 2538 2539 2540
                      source.adapter);

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2541
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2542 2543

    if (exit_status < 0) {
2544
        VIR_ERROR(_("Unable to create Storage Pool: %s"), NULLSTR(ret));
2545
        goto cleanup;
2546 2547
    }

2548
    result = 0;
2549

2550
cleanup:
2551
    VIR_FREE(ret);
2552 2553

    return result;
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563

}

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;
2564
    int nsp = -1;
2565 2566 2567 2568 2569
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

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

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

2578
    virBufferAsprintf(&buf, "|grep -c '^.*$'");
E
Eric Blake 已提交
2579
    phypExecInt(session, &buf, conn, &nsp);
2580
    return nsp;
2581 2582 2583 2584 2585
}

static int
phypListStoragePools(virConnectPtr conn, char **const pools, int npools)
{
2586
    bool success = false;
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597
    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 已提交
2598
    char *char_ptr = NULL;
2599 2600 2601
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2602
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2603 2604
                          managed_system, vios_id);

2605
    virBufferAsprintf(&buf, "lsvg");
2606 2607 2608

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2609
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2610 2611 2612

    /* I need to parse the textual return in order to get the storage pools */
    if (exit_status < 0 || ret == NULL)
2613
        goto cleanup;
2614 2615 2616 2617
    else {
        storage_pools = ret;

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

E
Eric Blake 已提交
2620 2621
            if (char_ptr) {
                *char_ptr = '\0';
2622 2623
                if ((pools[got++] = strdup(storage_pools)) == NULL) {
                    virReportOOMError();
2624
                    goto cleanup;
2625
                }
E
Eric Blake 已提交
2626 2627
                char_ptr++;
                storage_pools = char_ptr;
2628 2629 2630 2631 2632
            } else
                break;
        }
    }

2633 2634
    success = true;

2635
cleanup:
2636 2637 2638 2639 2640 2641
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(pools[i]);

        got = -1;
    }
2642
    VIR_FREE(ret);
2643
    return got;
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
}

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)) {
2687
            sp = virGetStoragePool(conn, pools[i], uuid, NULL, NULL);
2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
            VIR_FREE(local_uuid);
            VIR_FREE(pools);

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

2698
err:
2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
    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;
2711
    virStoragePoolPtr dup_sp = NULL;
2712 2713 2714 2715 2716 2717
    virStoragePoolPtr sp = NULL;

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

    /* checking if this name already exists on this system */
2718
    if ((dup_sp = phypStoragePoolLookupByName(conn, def->name)) != NULL) {
2719
        VIR_WARN("StoragePool name already exists.");
2720
        virObjectUnref(dup_sp);
2721 2722 2723 2724
        goto err;
    }

    /* checking if ID or UUID already exists on this system */
2725
    if ((dup_sp = phypGetStoragePoolLookUpByUUID(conn, def->uuid)) != NULL) {
2726
        VIR_WARN("StoragePool uuid already exists.");
2727
        virObjectUnref(dup_sp);
2728 2729
        goto err;
    }
2730

2731
    if ((sp = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL)) == NULL)
2732 2733 2734 2735 2736 2737 2738
        goto err;

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

    return sp;

2739
err:
2740
    virStoragePoolDefFree(def);
2741
    virObjectUnref(sp);
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
    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 {
2756
        VIR_ERROR(_("Unable to determine storage pool's name."));
2757 2758 2759
        goto err;
    }

2760
    if (memcpy(def.uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
2761
        VIR_ERROR(_("Unable to determine storage pool's uuid."));
2762 2763 2764 2765 2766
        goto err;
    }

    if ((def.capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2767
        VIR_ERROR(_("Unable to determine storage pools's size."));
2768 2769 2770
        goto err;
    }

J
Ján Tomko 已提交
2771
    /* Information not available */
2772 2773 2774 2775 2776 2777 2778 2779
    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) {
2780
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
2781 2782 2783 2784 2785
        goto err;
    }

    return virStoragePoolDefFormat(&def);

2786
err:
2787
    return NULL;
2788 2789
}

E
Eduardo Otubo 已提交
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
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 已提交
2806
    int rv = -1;
E
Eduardo Otubo 已提交
2807 2808 2809 2810 2811

    /* Getting the remote slot number */

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

2814
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2815 2816 2817
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,slot_num|"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
2818
    if (phypExecInt(session, &buf, iface->conn, &slot_num) < 0)
E
Eric Blake 已提交
2819
        goto cleanup;
E
Eduardo Otubo 已提交
2820 2821 2822 2823

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

2826
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2827 2828 2829
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,lpar_id|"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
2830
    if (phypExecInt(session, &buf, iface->conn, &lpar_id) < 0)
E
Eric Blake 已提交
2831
        goto cleanup;
E
Eduardo Otubo 已提交
2832 2833 2834 2835

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

2838
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2839 2840
                      " -r virtualio --rsubtype eth"
                      " --id %d -o r -s %d", lpar_id, slot_num);
2841 2842
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, iface->conn, false);
E
Eduardo Otubo 已提交
2843 2844

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

E
Eric Blake 已提交
2847
    rv = 0;
E
Eduardo Otubo 已提交
2848

E
Eric Blake 已提交
2849
cleanup:
E
Eduardo Otubo 已提交
2850
    VIR_FREE(ret);
E
Eric Blake 已提交
2851
    return rv;
E
Eduardo Otubo 已提交
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
}

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 已提交
2872
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2873 2874

    if (!(def = virInterfaceDefParseString(xml)))
E
Eric Blake 已提交
2875
        goto cleanup;
E
Eduardo Otubo 已提交
2876 2877 2878 2879

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

2882
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2883 2884 2885
                      " -r virtualio --rsubtype slot --level slot"
                      " -Fslot_num --filter lpar_names=%s"
                      " |sort|tail -n 1", def->name);
E
Eric Blake 已提交
2886
    if (phypExecInt(session, &buf, conn, &slot) < 0)
E
Eric Blake 已提交
2887
        goto cleanup;
E
Eduardo Otubo 已提交
2888 2889 2890 2891 2892 2893 2894

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

    /* Now adding the new network interface */
    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 2900
                      " -r virtualio --rsubtype eth"
                      " -p %s -o a -s %d -a port_vlan_id=1,"
                      "ieee_virtual_eth=0", def->name, slot);
2901 2902
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2903 2904

    if (exit_status < 0 || ret != NULL)
E
Eric Blake 已提交
2905
        goto cleanup;
E
Eduardo Otubo 已提交
2906 2907 2908 2909 2910 2911 2912 2913 2914

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

2917
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2918 2919 2920
                      " -r virtualio --rsubtype slot --level slot"
                      " |sed '/lpar_name=%s/!d; /slot_num=%d/!d; "
                      "s/^.*drc_name=//'", def->name, slot);
2921 2922
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2923 2924 2925 2926 2927

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

2930
        virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2931 2932
                " -r virtualio --rsubtype eth"
                " -p %s -o r -s %d", def->name, slot);
2933 2934
        VIR_FREE(ret);
        ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eric Blake 已提交
2935
        goto cleanup;
E
Eduardo Otubo 已提交
2936 2937 2938 2939 2940 2941 2942
    }

    memcpy(name, ret, PHYP_IFACENAME_SIZE-1);

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

2945
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2946 2947 2948
                      "-r virtualio --rsubtype eth --level lpar "
                      " |sed '/lpar_name=%s/!d; /slot_num=%d/!d; "
                      "s/^.*mac_addr=//'", def->name, slot);
2949 2950
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2951 2952

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
2953
        goto cleanup;
E
Eduardo Otubo 已提交
2954 2955 2956

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

E
Eric Blake 已提交
2959
cleanup:
E
Eduardo Otubo 已提交
2960 2961
    VIR_FREE(ret);
    virInterfaceDefFree(def);
E
Eric Blake 已提交
2962
    return result;
E
Eduardo Otubo 已提交
2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978
}

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];
2979
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2980 2981 2982 2983

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

2986
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2987 2988 2989
                      " -r virtualio --rsubtype slot --level slot "
                      " -F drc_name,slot_num |"
                      " sed -n '/%s/ s/^.*,//p'", name);
E
Eric Blake 已提交
2990
    if (phypExecInt(session, &buf, conn, &slot) < 0)
E
Eric Blake 已提交
2991
        goto cleanup;
E
Eduardo Otubo 已提交
2992 2993 2994 2995

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

2998
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2999 3000 3001
                      " -r virtualio --rsubtype slot --level slot "
                      " -F drc_name,lpar_id |"
                      " sed -n '/%s/ s/^.*,//p'", name);
E
Eric Blake 已提交
3002
    if (phypExecInt(session, &buf, conn, &lpar_id) < 0)
E
Eric Blake 已提交
3003
        goto cleanup;
E
Eduardo Otubo 已提交
3004 3005 3006 3007

    /*Getting the interface mac */
    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 lpar_id,slot_num,mac_addr|"
                      " sed -n '/%d,%d/ s/^.*,//p'", lpar_id, slot);
3014
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
3015 3016

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3017
        goto cleanup;
E
Eduardo Otubo 已提交
3018 3019 3020

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

E
Eric Blake 已提交
3023
cleanup:
E
Eduardo Otubo 已提交
3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
    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 已提交
3037
    int state = -1;
E
Eduardo Otubo 已提交
3038 3039 3040

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

3043
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
3044 3045 3046
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,state |"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
3047
    phypExecInt(session, &buf, iface->conn, &state);
E
Eduardo Otubo 已提交
3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
    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 已提交
3065
    char *char_ptr = NULL;
E
Eduardo Otubo 已提交
3066
    virBuffer buf = VIR_BUFFER_INITIALIZER;
E
Eric Blake 已提交
3067
    bool success = false;
E
Eduardo Otubo 已提交
3068 3069 3070

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

E
Eric Blake 已提交
3077 3078
    /* I need to parse the textual return in order to get the network
     * interfaces */
E
Eduardo Otubo 已提交
3079
    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3080
        goto cleanup;
E
Eduardo Otubo 已提交
3081 3082 3083 3084

    networks = ret;

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

E
Eric Blake 已提交
3087 3088
        if (char_ptr) {
            *char_ptr = '\0';
E
Eduardo Otubo 已提交
3089 3090
            if ((names[got++] = strdup(networks)) == NULL) {
                virReportOOMError();
E
Eric Blake 已提交
3091
                goto cleanup;
E
Eduardo Otubo 已提交
3092
            }
E
Eric Blake 已提交
3093 3094
            char_ptr++;
            networks = char_ptr;
E
Eduardo Otubo 已提交
3095 3096 3097 3098 3099
        } else {
            break;
        }
    }

E
Eric Blake 已提交
3100 3101 3102 3103 3104
cleanup:
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);
    }
E
Eduardo Otubo 已提交
3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117
    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 已提交
3118
    int nnets = -1;
E
Eduardo Otubo 已提交
3119 3120 3121 3122
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

3125
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
3126 3127
                      "-r virtualio --rsubtype eth --level lpar|"
                      "grep -v lpar_id=%d|grep -c lpar_name", vios_id);
E
Eric Blake 已提交
3128
    phypExecInt(session, &buf, conn, &nnets);
E
Eduardo Otubo 已提交
3129 3130 3131
    return nnets;
}

3132 3133
static int
phypGetLparState(virConnectPtr conn, unsigned int lpar_id)
3134
{
3135
    ConnectionData *connection_data = conn->networkPrivateData;
3136
    phyp_driverPtr phyp_driver = conn->privateData;
3137 3138 3139 3140 3141 3142 3143
    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;
3144

3145 3146
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
3147 3148
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F state --filter lpar_ids=%d", lpar_id);
3149
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
3150

3151 3152
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3153

3154 3155 3156 3157 3158 3159
    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;
3160

3161
cleanup:
3162 3163
    VIR_FREE(ret);
    return state;
3164 3165
}

3166 3167 3168 3169
/* XXX - is this needed? */
static int phypDiskType(virConnectPtr, char *) ATTRIBUTE_UNUSED;
static int
phypDiskType(virConnectPtr conn, char *backing_device)
3170 3171
{
    phyp_driverPtr phyp_driver = conn->privateData;
3172 3173 3174 3175 3176 3177 3178 3179 3180
    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;
3181

3182 3183
    virBufferAddLit(&buf, "viosvrcmd");
    if (system_type == HMC)
3184 3185
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -p %d -c \"lssp -field name type "
E
Eric Blake 已提交
3186
                      "-fmt , -all|sed -n '/%s/ {\n s/^.*,//\n p\n}'\"",
3187
                      vios_id, backing_device);
3188
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
3189

3190 3191
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3192

3193 3194 3195 3196
    if (STREQ(ret, "LVPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_BLOCK;
    else if (STREQ(ret, "FBPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_FILE;
3197

3198
cleanup:
3199 3200 3201
    VIR_FREE(ret);
    return disk_type;
}
3202

3203 3204 3205 3206 3207
static int
phypNumDefinedDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 1);
}
3208

3209 3210 3211 3212
static int
phypNumDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 0);
3213 3214
}

3215 3216
static int
phypListDomains(virConnectPtr conn, int *ids, int nids)
3217
{
3218 3219
    return phypListDomainsGeneric(conn, ids, nids, 0);
}
3220

3221 3222 3223
static int
phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames)
{
3224
    bool success = false;
3225 3226 3227 3228 3229 3230 3231 3232 3233 3234
    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 已提交
3235
    char *char_ptr = NULL;
3236
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3237

3238 3239
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
3240 3241
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F name,state"
E
Eric Blake 已提交
3242
                      "|sed -n '/Not Activated/ {\n s/,.*$//\n p\n}'");
3243
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
3244

3245 3246
    /* I need to parse the textual return in order to get the domains */
    if (exit_status < 0 || ret == NULL)
3247
        goto cleanup;
3248 3249
    else {
        domains = ret;
3250

3251
        while (got < nnames) {
E
Eric Blake 已提交
3252
            char_ptr = strchr(domains, '\n');
3253

E
Eric Blake 已提交
3254 3255
            if (char_ptr) {
                *char_ptr = '\0';
3256
                if ((names[got++] = strdup(domains)) == NULL) {
3257
                    virReportOOMError();
3258
                    goto cleanup;
3259
                }
E
Eric Blake 已提交
3260 3261
                char_ptr++;
                domains = char_ptr;
3262 3263
            } else
                break;
3264
        }
3265 3266
    }

3267 3268
    success = true;

3269
cleanup:
3270 3271 3272 3273 3274 3275
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);

        got = -1;
    }
3276
    VIR_FREE(ret);
3277
    return got;
3278 3279
}

3280 3281
static virDomainPtr
phypDomainLookupByName(virConnectPtr conn, const char *lpar_name)
3282
{
3283 3284 3285 3286 3287 3288 3289
    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];
3290

3291 3292 3293
    lpar_id = phypGetLparID(session, managed_system, lpar_name, conn);
    if (lpar_id == -1)
        return NULL;
3294

3295 3296
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
        return NULL;
3297

3298 3299 3300 3301 3302 3303
    dom = virGetDomain(conn, lpar_name, lpar_uuid);

    if (dom)
        dom->id = lpar_id;

    return dom;
3304 3305
}

3306 3307
static virDomainPtr
phypDomainLookupByID(virConnectPtr conn, int lpar_id)
3308 3309
{
    ConnectionData *connection_data = conn->networkPrivateData;
3310
    phyp_driverPtr phyp_driver = conn->privateData;
3311
    LIBSSH2_SESSION *session = connection_data->session;
3312 3313 3314 3315
    virDomainPtr dom = NULL;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
    unsigned char lpar_uuid[VIR_UUID_BUFLEN];
E
Eduardo Otubo 已提交
3316

3317 3318
    char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
                                      conn);
3319

3320
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
3321
        goto cleanup;
3322

3323
    if (exit_status < 0)
3324
        goto cleanup;
3325

3326
    dom = virGetDomain(conn, lpar_name, lpar_uuid);
3327

3328 3329
    if (dom)
        dom->id = lpar_id;
3330

3331
cleanup:
3332
    VIR_FREE(lpar_name);
3333

3334
    return dom;
3335 3336
}

3337
static char *
3338
phypDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
3339
{
3340 3341
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = dom->conn->privateData;
3342
    LIBSSH2_SESSION *session = connection_data->session;
3343 3344
    virDomainDef def;
    char *managed_system = phyp_driver->managed_system;
E
Eduardo Otubo 已提交
3345

3346 3347
    /* Flags checked by virDomainDefFormat */

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

3350 3351 3352 3353 3354 3355 3356
    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) {
3357
        VIR_ERROR(_("Unable to determine domain's name."));
3358
        goto err;
E
Eduardo Otubo 已提交
3359 3360
    }

3361
    if (phypGetLparUUID(def.uuid, dom->id, dom->conn) == -1) {
3362
        VIR_ERROR(_("Unable to generate random uuid."));
E
Eduardo Otubo 已提交
3363 3364
        goto err;
    }
3365

3366
    if ((def.mem.max_balloon =
3367
         phypGetLparMem(dom->conn, managed_system, dom->id, 0)) == 0) {
3368
        VIR_ERROR(_("Unable to determine domain's max memory."));
3369 3370
        goto err;
    }
3371

3372
    if ((def.mem.cur_balloon =
3373
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0) {
3374
        VIR_ERROR(_("Unable to determine domain's memory."));
3375 3376
        goto err;
    }
3377

E
Eric Blake 已提交
3378
    if ((def.maxvcpus = def.vcpus =
3379
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0) {
3380
        VIR_ERROR(_("Unable to determine domain's CPU."));
3381
        goto err;
3382
    }
3383

3384
    return virDomainDefFormat(&def, flags);
3385

3386
err:
3387 3388
    return NULL;
}
3389

3390 3391 3392
static int
phypDomainResume(virDomainPtr dom)
{
3393
    int result = -1;
3394 3395 3396 3397 3398 3399 3400 3401
    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;
3402

3403 3404
    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
3405 3406
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar -o on --id %d -f %s",
3407
                      dom->id, dom->name);
3408
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3409

3410
    if (exit_status < 0)
3411
        goto cleanup;
3412

3413
    result = 0;
3414

3415
cleanup:
3416
    VIR_FREE(ret);
3417 3418

    return result;
3419 3420
}

3421
static int
E
Eric Blake 已提交
3422
phypDomainReboot(virDomainPtr dom, unsigned int flags)
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434
{
    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;

E
Eric Blake 已提交
3435 3436
    virCheckFlags(0, -1);

3437 3438
    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
3439 3440
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455
                      " -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;
}

3456 3457
static int
phypDomainShutdown(virDomainPtr dom)
3458
{
3459
    int result = -1;
3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
    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)
3472 3473
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar -o shutdown --id %d", dom->id);
3474
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3475 3476

    if (exit_status < 0)
3477
        goto cleanup;
3478

3479
    result = 0;
3480

3481
cleanup:
3482
    VIR_FREE(ret);
3483 3484

    return result;
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496
}

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)
3497
        VIR_WARN("Unable to determine domain's max memory.");
3498 3499 3500

    if ((info->memory =
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0)
3501
        VIR_WARN("Unable to determine domain's memory.");
3502 3503 3504

    if ((info->nrVirtCpu =
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
3505
        VIR_WARN("Unable to determine domain's CPU.");
3506 3507 3508 3509

    return 0;
}

3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524
static int
phypDomainGetState(virDomainPtr dom,
                   int *state,
                   int *reason,
                   unsigned int flags)
{
    virCheckFlags(0, -1);

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

    return 0;
}

3525
static int
3526 3527
phypDomainDestroyFlags(virDomainPtr dom,
                       unsigned int flags)
3528
{
3529
    int result = -1;
3530 3531 3532 3533 3534 3535 3536 3537 3538
    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;

3539 3540
    virCheckFlags(0, -1);

3541 3542
    virBufferAddLit(&buf, "rmsyscfg");
    if (system_type == HMC)
3543 3544
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar --id %d", dom->id);
3545
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3546 3547

    if (exit_status < 0)
3548
        goto cleanup;
3549 3550

    if (phypUUIDTable_RemLpar(dom->conn, dom->id) == -1)
3551
        goto cleanup;
3552

3553
    dom->id = -1;
3554
    result = 0;
3555

3556
cleanup:
3557 3558
    VIR_FREE(ret);

3559
    return result;
3560
}
3561

3562 3563 3564 3565 3566 3567
static int
phypDomainDestroy(virDomainPtr dom)
{
    return phypDomainDestroyFlags(dom, 0);
}

3568 3569
static int
phypBuildLpar(virConnectPtr conn, virDomainDefPtr def)
3570
{
3571
    int result = -1;
3572 3573 3574 3575 3576 3577 3578 3579
    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;
3580

3581
    if (!def->mem.cur_balloon) {
3582 3583 3584
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Field <memory> on the domain XML file is missing or has "
                         "invalid value."));
3585
        goto cleanup;
3586 3587
    }

3588
    if (!def->mem.max_balloon) {
3589 3590 3591
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Field <currentMemory> on the domain XML file is missing or "
                         "has invalid value."));
3592
        goto cleanup;
3593 3594
    }

3595
    if (def->ndisks < 1) {
3596 3597
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Domain XML must contain at least one <disk> element."));
3598
        goto cleanup;
3599 3600 3601
    }

    if (!def->disks[0]->src) {
3602 3603 3604
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Field <src> under <disk> on the domain XML file is "
                         "missing."));
3605
        goto cleanup;
3606 3607
    }

3608 3609
    virBufferAddLit(&buf, "mksyscfg");
    if (system_type == HMC)
3610
        virBufferAsprintf(&buf, " -m %s", managed_system);
3611 3612 3613 3614
    virBufferAsprintf(&buf, " -r lpar -p %s -i min_mem=%lld,desired_mem=%lld,"
                      "max_mem=%lld,desired_procs=%d,virtual_scsi_adapters=%s",
                      def->name, def->mem.cur_balloon,
                      def->mem.cur_balloon, def->mem.max_balloon,
3615
                      (int) def->vcpus, def->disks[0]->src);
3616
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
3617

3618
    if (exit_status < 0) {
3619
        VIR_ERROR(_("Unable to create LPAR. Reason: '%s'"), NULLSTR(ret));
3620
        goto cleanup;
3621
    }
3622

3623
    if (phypUUIDTable_AddLpar(conn, def->uuid, def->id) == -1) {
3624
        VIR_ERROR(_("Unable to add LPAR to the table"));
3625
        goto cleanup;
3626
    }
3627

3628
    result = 0;
3629

3630
cleanup:
3631
    VIR_FREE(ret);
3632 3633

    return result;
3634
}
3635

3636 3637 3638 3639
static virDomainPtr
phypDomainCreateAndStart(virConnectPtr conn,
                         const char *xml, unsigned int flags)
{
E
Eduardo Otubo 已提交
3640
    virCheckFlags(0, NULL);
3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654

    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,
M
Matthias Bolte 已提交
3655
                                        1 << VIR_DOMAIN_VIRT_PHYP,
3656 3657 3658 3659
                                        VIR_DOMAIN_XML_SECURE)))
        goto err;

    /* checking if this name already exists on this system */
3660
    if (phypGetLparID(session, managed_system, def->name, conn) != -1) {
3661
        VIR_WARN("LPAR name already exists.");
3662 3663 3664 3665 3666 3667
        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) {
3668
            VIR_WARN("LPAR ID or UUID already exists.");
3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683
            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;

3684
err:
3685
    virDomainDefFree(def);
3686
    virObjectUnref(dom);
3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702
    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
3703 3704
phypDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                        unsigned int flags)
3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717
{
    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;

3718
    if (flags != VIR_DOMAIN_VCPU_LIVE) {
3719
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
3720 3721 3722
        return -1;
    }

3723 3724 3725 3726
    if ((ncpus = phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
        return 0;

    if (nvcpus > phypGetLparCPUMAX(dom)) {
3727
        VIR_ERROR(_("You are trying to set a number of CPUs bigger than "
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
                     "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)
3743 3744
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --id %d -o %c --procunits %d 2>&1 |sed "
3745 3746
                      "-e 's/^.*\\([0-9][0-9]*.[0-9][0-9]*\\).*$/\\1/'",
                      dom->id, operation, amount);
3747
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3748 3749

    if (exit_status < 0) {
3750
        VIR_ERROR(_
3751 3752 3753 3754 3755 3756
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    " Contact your support to enable this feature."));
    }

    VIR_FREE(ret);
    return 0;
3757 3758

}
3759

3760 3761 3762 3763 3764 3765
static int
phypDomainSetCPU(virDomainPtr dom, unsigned int nvcpus)
{
    return phypDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_VCPU_LIVE);
}

3766
static virDrvOpenStatus
3767 3768
phypVIOSDriverOpen(virConnectPtr conn,
                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3769
                   unsigned int flags)
3770
{
E
Eric Blake 已提交
3771 3772
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3773 3774 3775
    if (conn->driver->no != VIR_DRV_PHYP)
        return VIR_DRV_OPEN_DECLINED;

3776 3777 3778 3779
    return VIR_DRV_OPEN_SUCCESS;
}

static int
3780
phypVIOSDriverClose(virConnectPtr conn ATTRIBUTE_UNUSED)
3781 3782 3783 3784
{
    return 0;
}

3785
static virDriver phypDriver = {
3786 3787
    .no = VIR_DRV_PHYP,
    .name = "PHYP",
3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799
    .open = phypOpen, /* 0.7.0 */
    .close = phypClose, /* 0.7.0 */
    .getCapabilities = phypConnectGetCapabilities, /* 0.7.3 */
    .listDomains = phypListDomains, /* 0.7.0 */
    .numOfDomains = phypNumDomains, /* 0.7.0 */
    .domainCreateXML = phypDomainCreateAndStart, /* 0.7.3 */
    .domainLookupByID = phypDomainLookupByID, /* 0.7.0 */
    .domainLookupByName = phypDomainLookupByName, /* 0.7.0 */
    .domainResume = phypDomainResume, /* 0.7.0 */
    .domainShutdown = phypDomainShutdown, /* 0.7.0 */
    .domainReboot = phypDomainReboot, /* 0.9.1 */
    .domainDestroy = phypDomainDestroy, /* 0.7.3 */
3800
    .domainDestroyFlags = phypDomainDestroyFlags, /* 0.9.4 */
3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813
    .domainGetInfo = phypDomainGetInfo, /* 0.7.0 */
    .domainGetState = phypDomainGetState, /* 0.9.2 */
    .domainSetVcpus = phypDomainSetCPU, /* 0.7.3 */
    .domainSetVcpusFlags = phypDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = phypDomainGetVcpusFlags, /* 0.8.5 */
    .domainGetMaxVcpus = phypGetLparCPUMAX, /* 0.7.3 */
    .domainGetXMLDesc = phypDomainGetXMLDesc, /* 0.7.0 */
    .listDefinedDomains = phypListDefinedDomains, /* 0.7.0 */
    .numOfDefinedDomains = phypNumDefinedDomains, /* 0.7.0 */
    .domainAttachDevice = phypAttachDevice, /* 0.8.2 */
    .isEncrypted = phypIsEncrypted, /* 0.7.3 */
    .isSecure = phypIsSecure, /* 0.7.3 */
    .domainIsUpdated = phypIsUpdated, /* 0.8.6 */
3814
    .isAlive = phypIsAlive, /* 0.9.8 */
3815 3816
};

3817 3818
static virStorageDriver phypStorageDriver = {
    .name = "PHYP",
3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836
    .open = phypVIOSDriverOpen, /* 0.8.2 */
    .close = phypVIOSDriverClose, /* 0.8.2 */

    .numOfPools = phypNumOfStoragePools, /* 0.8.2 */
    .listPools = phypListStoragePools, /* 0.8.2 */
    .poolLookupByName = phypStoragePoolLookupByName, /* 0.8.2 */
    .poolLookupByUUID = phypGetStoragePoolLookUpByUUID, /* 0.8.2 */
    .poolCreateXML = phypStoragePoolCreateXML, /* 0.8.2 */
    .poolDestroy = phypDestroyStoragePool, /* 0.8.2 */
    .poolGetXMLDesc = phypGetStoragePoolXMLDesc, /* 0.8.2 */
    .poolNumOfVolumes = phypStoragePoolNumOfVolumes, /* 0.8.2 */
    .poolListVolumes = phypStoragePoolListVolumes, /* 0.8.2 */

    .volLookupByName = phypVolumeLookupByName, /* 0.8.2 */
    .volLookupByPath = phypVolumeLookupByPath, /* 0.8.2 */
    .volCreateXML = phypStorageVolCreateXML, /* 0.8.2 */
    .volGetXMLDesc = phypVolumeGetXMLDesc, /* 0.8.2 */
    .volGetPath = phypVolumeGetPath, /* 0.8.2 */
3837 3838
};

E
Eduardo Otubo 已提交
3839
static virInterfaceDriver phypInterfaceDriver = {
3840
    .name = "PHYP",
3841 3842 3843 3844 3845 3846 3847 3848
    .open = phypVIOSDriverOpen, /* 0.9.1 */
    .close = phypVIOSDriverClose, /* 0.9.1 */
    .numOfInterfaces = phypNumOfInterfaces, /* 0.9.1 */
    .listInterfaces = phypListInterfaces, /* 0.9.1 */
    .interfaceLookupByName = phypInterfaceLookupByName, /* 0.9.1 */
    .interfaceDefineXML = phypInterfaceDefineXML, /* 0.9.1 */
    .interfaceDestroy = phypInterfaceDestroy, /* 0.9.1 */
    .interfaceIsActive = phypInterfaceIsActive /* 0.9.1 */
3849 3850
};

3851 3852 3853
int
phypRegister(void)
{
3854 3855 3856 3857
    if (virRegisterDriver(&phypDriver) < 0)
        return -1;
    if (virRegisterStorageDriver(&phypStorageDriver) < 0)
        return -1;
E
Eduardo Otubo 已提交
3858
    if (virRegisterInterfaceDriver(&phypInterfaceDriver) < 0)
3859
        return -1;
3860

3861 3862
    return 0;
}