phyp_driver.c 108.6 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 293 294 295 296

static int phypDefaultConsoleType(const char *ostype ATTRIBUTE_UNUSED)
{
    return VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
}


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

304
    uname(&utsname);
305

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

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

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

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

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

334 335
    caps->defaultConsoleTargetType = phypDefaultConsoleType;

336
    return caps;
337

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

563 564 565 566 567 568 569 570
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
    virBufferFreeAndReset(&username);
571 572
    return 0;

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

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

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

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

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

605
    return 0;
606

607
err:
608
    return -1;
609 610
}

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

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

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

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

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

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

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

640
    return 0;
641

642
err:
643
    return -1;
644 645
}

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

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

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

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

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

688
    VIR_FORCE_CLOSE(fd);
689
    return 0;
690

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

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

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

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

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

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

736 737 738 739 740 741 742 743 744
        if (!channel) {
            if (libssh2_session_last_errno(session) !=
                LIBSSH2_ERROR_EAGAIN) {
                goto err;;
            } else {
                waitsocket(sock, session);
            }
        }
    } while (!channel);
745

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

928 929
    return false;
}
930

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
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;
959 960
}

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

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

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

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

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

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

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

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

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

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

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

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

1044
connected:
1045

1046
    (*internal_socket) = sock;
1047

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return VIR_DRV_OPEN_SUCCESS;

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

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

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

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


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

1285 1286 1287 1288 1289 1290

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

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

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;
}


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

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

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

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

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

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

1356 1357 1358 1359 1360 1361 1362 1363 1364

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

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

1378
    return -1;
1379 1380
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        backing_device = strdup(char_ptr);

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

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

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

1542
cleanup:
1543
    VIR_FREE(ret);
1544

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

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

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

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

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

    virBufferAddLit(&buf, "lssyscfg");

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

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

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

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

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

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

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

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

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

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

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

1685
    result = 0;
1686

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

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

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

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

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

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

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


static int
phypAttachDevice(virDomainPtr domain, const char *xml)
{
1729
    int result = -1;
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
    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 已提交
1748 1749 1750 1751 1752
    if (VIR_ALLOC(def) < 0) {
        virReportOOMError();
        goto cleanup;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1874
    result = 0;
1875

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

    return result;
1886 1887
}

1888 1889
static char *
phypVolumeGetKey(virConnectPtr conn, const char *name)
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
1902
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1903 1904
                          managed_system, vios_id);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2004 2005
    key = phypVolumeGetKey(conn, lvname);

2006
cleanup:
2007 2008
    VIR_FREE(ret);

2009
    return key;
2010 2011 2012 2013 2014
}

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

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

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

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

    VIR_FREE(key);

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

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

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

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

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

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

    if ((spdef->capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2063
        VIR_ERROR(_("Unable to determine storage pools's size."));
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
        goto err;
    }

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

    spdef->source.ndevice = 1;

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

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

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

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

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

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

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

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

2116 2117
    VIR_FREE(key);

2118 2119
    return vol;

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

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

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

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

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

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

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

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

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

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

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

2189
    key = phypVolumeGetKey(conn, volname);
2190

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

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

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

    return vol;
2201 2202 2203 2204 2205 2206
}

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

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

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

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

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

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

2236
    result = 0;
2237

2238
cleanup:
2239
    VIR_FREE(ret);
2240 2241

    return result;
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
}

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

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

    return virGetStoragePool(conn, name, uuid);
}

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

2263 2264 2265
    virCheckFlags(0, NULL);

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

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

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

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

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

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

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

    pool.source.ndevice = 1;

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

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

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

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

    voldef.type = VIR_STORAGE_POOL_LOGICAL;

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

    VIR_FREE(voldef.key);

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

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

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

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

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

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

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

2367
    pv = phypVolumeGetPhysicalVolumeByStoragePool(vol, ret);
2368

2369 2370
    if (!pv)
        goto cleanup;
2371

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

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

    return path;
2382 2383 2384 2385 2386 2387
}

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

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

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

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

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

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

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

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

2438 2439
    success = true;

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

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

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

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

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

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

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

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

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

2508
    result = 0;
2509

2510
cleanup:
2511
    VIR_FREE(ret);
2512 2513

    return result;
2514 2515 2516 2517 2518
}

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

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

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

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

2547
    result = 0;
2548

2549
cleanup:
2550
    VIR_FREE(ret);
2551 2552

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

}

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

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

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

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

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

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

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

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

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

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

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

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

2632 2633
    success = true;

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

        got = -1;
    }
2641
    VIR_FREE(ret);
2642
    return got;
2643 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 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
}

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

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

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

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

    if ((gotpools = phypListStoragePools(conn, pools, npools)) == -1) {
        virReportOOMError();
        goto err;
    }

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

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

        if (!memcmp(local_uuid, uuid, VIR_UUID_BUFLEN)) {
            sp = virGetStoragePool(conn, pools[i], uuid);
            VIR_FREE(local_uuid);
            VIR_FREE(pools);

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

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

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

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

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

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

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

    return sp;

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

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

    if ((def.capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2766
        VIR_ERROR(_("Unable to determine storage pools's size."));
2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
        goto err;
    }

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

    def.source.ndevice = 1;

    /*XXX source adapter not working properly, should show hdiskX */
    if ((def.source.adapter =
         phypGetStoragePoolDevice(pool->conn, pool->name)) == NULL) {
2779
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
2780 2781 2782 2783 2784
        goto err;
    }

    return virStoragePoolDefFormat(&def);

2785
err:
2786
    return NULL;
2787 2788
}

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

    /* Getting the remote slot number */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2896
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2897 2898 2899
                      " -r virtualio --rsubtype eth"
                      " -p %s -o a -s %d -a port_vlan_id=1,"
                      "ieee_virtual_eth=0", def->name, slot);
2900 2901
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2902 2903

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

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

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

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

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

    memcpy(name, ret, PHYP_IFACENAME_SIZE-1);

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

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

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

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

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

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

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

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

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

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

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

3009
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
3010 3011 3012
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F lpar_id,slot_num,mac_addr|"
                      " sed -n '/%d,%d/ s/^.*,//p'", lpar_id, slot);
3013
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
3014 3015

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

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

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

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

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

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

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

    networks = ret;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3266 3267
    success = true;

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

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

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

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

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

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

    if (dom)
        dom->id = lpar_id;

    return dom;
3303 3304
}

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

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

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

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

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

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

3330
cleanup:
3331
    VIR_FREE(lpar_name);
3332

3333
    return dom;
3334 3335
}

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

3345 3346
    /* Flags checked by virDomainDefFormat */

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

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

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

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

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

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

3383
    return virDomainDefFormat(&def, flags);
3384

3385
err:
3386 3387
    return NULL;
}
3388

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

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

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

3412
    result = 0;
3413

3414
cleanup:
3415
    VIR_FREE(ret);
3416 3417

    return result;
3418 3419
}

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

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

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

    if (exit_status < 0)
3476
        goto cleanup;
3477

3478
    result = 0;
3479

3480
cleanup:
3481
    VIR_FREE(ret);
3482 3483

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

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

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

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

    return 0;
}

3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523
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;
}

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

3538 3539
    virCheckFlags(0, -1);

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

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

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

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

3555
cleanup:
3556 3557
    VIR_FREE(ret);

3558
    return result;
3559
}
3560

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

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

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

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

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

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

3607 3608
    virBufferAddLit(&buf, "mksyscfg");
    if (system_type == HMC)
3609
        virBufferAsprintf(&buf, " -m %s", managed_system);
3610 3611 3612 3613
    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,
3614
                      (int) def->vcpus, def->disks[0]->src);
3615
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
3616

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

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

3627
    result = 0;
3628

3629
cleanup:
3630
    VIR_FREE(ret);
3631 3632

    return result;
3633
}
3634

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

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

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

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

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

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

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

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

    VIR_FREE(ret);
    return 0;
3756 3757

}
3758

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

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

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

3775 3776 3777 3778
    return VIR_DRV_OPEN_SUCCESS;
}

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

3784
static virDriver phypDriver = {
3785 3786
    .no = VIR_DRV_PHYP,
    .name = "PHYP",
3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798
    .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 */
3799
    .domainDestroyFlags = phypDomainDestroyFlags, /* 0.9.4 */
3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812
    .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 */
3813
    .isAlive = phypIsAlive, /* 0.9.8 */
3814 3815
};

3816 3817
static virStorageDriver phypStorageDriver = {
    .name = "PHYP",
3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835
    .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 */
3836 3837
};

E
Eduardo Otubo 已提交
3838
static virInterfaceDriver phypInterfaceDriver = {
3839
    .name = "PHYP",
3840 3841 3842 3843 3844 3845 3846 3847
    .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 */
3848 3849
};

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

3860 3861
    return 0;
}