phyp_driver.c 109.8 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2013 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
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
36 37 38 39 40
#include <libssh2.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
41 42
#include <fcntl.h>
#include <domain_event.h>
43 44

#include "internal.h"
45
#include "virauth.h"
46
#include "datatypes.h"
47
#include "virbuffer.h"
48
#include "viralloc.h"
49
#include "virlog.h"
50
#include "driver.h"
51
#include "virerror.h"
52
#include "viruuid.h"
53
#include "domain_conf.h"
54
#include "storage_conf.h"
55
#include "nodeinfo.h"
E
Eric Blake 已提交
56
#include "virfile.h"
E
Eduardo Otubo 已提交
57
#include "interface_conf.h"
58
#include "phyp_driver.h"
59
#include "virstring.h"
60 61 62 63 64 65 66

#define VIR_FROM_THIS VIR_FROM_PHYP

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

67 68
static unsigned const int HMC = 0;
static unsigned const int IVM = 127;
E
Eduardo Otubo 已提交
69 70
static unsigned const int PHYP_IFACENAME_SIZE = 24;
static unsigned const int PHYP_MAC_SIZE= 12;
71

72 73 74 75 76 77 78 79
static int
waitsocket(int socket_fd, LIBSSH2_SESSION * session)
{
    struct timeval timeout;
    fd_set fd;
    fd_set *writefd = NULL;
    fd_set *readfd = NULL;
    int dir;
80

81 82
    timeout.tv_sec = 0;
    timeout.tv_usec = 1000;
83

84
    FD_ZERO(&fd);
85

86
    FD_SET(socket_fd, &fd);
87

88 89
    /* now make sure we wait in the correct direction */
    dir = libssh2_session_block_directions(session);
90

91 92
    if (dir & LIBSSH2_SESSION_BLOCK_INBOUND)
        readfd = &fd;
93

94 95
    if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
        writefd = &fd;
96

97
    return select(socket_fd + 1, readfd, writefd, NULL, &timeout);
98
}
99

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

119 120 121 122 123
    if (VIR_ALLOC_N(buffer, buffer_size) < 0) {
        virReportOOMError();
        return NULL;
    }

124 125 126 127
    /* 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) {
128 129 130 131 132
        if (waitsocket(sock, session) < 0 && errno != EINTR) {
            virReportSystemError(errno, "%s",
                                 _("unable to wait on libssh2 socket"));
            goto err;
        }
133 134
    }

135 136
    if (channel == NULL) {
        goto err;
137
    }
138

139 140
    while ((rc = libssh2_channel_exec(channel, cmd)) ==
           LIBSSH2_ERROR_EAGAIN) {
141 142 143 144 145
        if (waitsocket(sock, session) < 0 && errno != EINTR) {
            virReportSystemError(errno, "%s",
                                 _("unable to wait on libssh2 socket"));
            goto err;
        }
146
    }
147

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

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

163 164 165
        /* this is due to blocking that would occur otherwise so we loop on
         * this condition */
        if (rc == LIBSSH2_ERROR_EAGAIN) {
166 167 168 169 170
            if (waitsocket(sock, session) < 0 && errno != EINTR) {
                virReportSystemError(errno, "%s",
                                     _("unable to wait on libssh2 socket"));
                goto err;
            }
171 172 173
        } else {
            break;
        }
E
Eduardo Otubo 已提交
174 175
    }

176
    exitcode = 127;
177

178
    while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) {
179 180 181 182 183
        if (waitsocket(sock, session) < 0 && errno != EINTR) {
            virReportSystemError(errno, "%s",
                                 _("unable to wait on libssh2 socket"));
            goto err;
        }
184 185
    }

186 187
    if (rc == 0) {
        exitcode = libssh2_channel_get_exit_status(channel);
188 189
    }

190 191 192
    (*exit_status) = exitcode;
    libssh2_channel_free(channel);
    channel = NULL;
193 194
    VIR_FREE(buffer);

195 196 197 198 199 200
    if (virBufferError(&tex_ret)) {
        virBufferFreeAndReset(&tex_ret);
        virReportOOMError();
        return NULL;
    }
    return virBufferContentAndReset(&tex_ret);
201 202 203 204 205 206

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

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/* 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 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/* 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;
}

259
static int
260
phypGetSystemType(virConnectPtr conn)
261 262
{
    ConnectionData *connection_data = conn->networkPrivateData;
263
    LIBSSH2_SESSION *session = connection_data->session;
264 265 266
    char *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
267

268 269
    if (virAsprintf(&cmd, "lshmc -V") < 0) {
        virReportOOMError();
270
        return -1;
271 272
    }
    ret = phypExec(session, cmd, &exit_status, conn);
273

274 275 276
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return exit_status;
277 278
}

279
static int
280
phypGetVIOSPartitionID(virConnectPtr conn)
281
{
282 283 284 285 286 287 288
    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;
289

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

299

300 301 302 303 304
static virCapsPtr
phypCapsInit(void)
{
    virCapsPtr caps;
    virCapsGuestPtr guest;
305

306 307
    if ((caps = virCapabilitiesNew(virArchFromHost(),
                                   0, 0)) == NULL)
308
        goto no_memory;
309

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

320 321
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "linux",
322
                                         caps->host.arch,
323 324
                                         NULL, NULL, 0, NULL)) == NULL)
        goto no_memory;
325

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

330
    return caps;
331

332
no_memory:
333
    virObjectUnref(caps);
334 335
    return NULL;
}
336

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

490 491
    if (virAsprintf(&remote_file, "/home/%s/libvirt_uuid_table",
                    NULLSTR(conn->uri->user)) < 0) {
E
Eduardo Otubo 已提交
492
        virReportOOMError();
493
        goto cleanup;
494 495
    }

496
    if (stat(local_file, &local_fileinfo) == -1) {
497
        VIR_WARN("Unable to stat local file.");
498
        goto cleanup;
499
    }
500

501
    if (!(f = fopen(local_file, "rb"))) {
502
        VIR_WARN("Unable to open local file.");
503
        goto cleanup;
504
    }
505

506 507 508 509 510
    do {
        channel =
            libssh2_scp_send(session, remote_file,
                             0x1FF & local_fileinfo.st_mode,
                             (unsigned long) local_fileinfo.st_size);
511

512 513
        if ((!channel) && (libssh2_session_last_errno(session) !=
                           LIBSSH2_ERROR_EAGAIN))
514
            goto cleanup;
515
    } while (!channel);
516

517
    do {
518
        nread = fread(buffer, 1, sizeof(buffer), f);
519
        if (nread <= 0) {
520
            if (feof(f)) {
521 522 523 524
                /* end of file */
                break;
            } else {
                VIR_ERROR(_("Failed to read from %s"), local_file);
525
                goto cleanup;
526 527 528 529
            }
        }
        ptr = buffer;
        sent = 0;
530

531 532 533 534 535 536 537 538 539 540 541 542 543
        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);
544

545
    ret = 0;
546

547
cleanup:
548 549 550 551 552 553 554
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
555 556
    VIR_FORCE_FCLOSE(f);
    return ret;
557 558 559
}

static int
560
phypUUIDTable_RemLpar(virConnectPtr conn, int id)
561
{
E
Eduardo Otubo 已提交
562
    phyp_driverPtr phyp_driver = conn->privateData;
563 564
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    unsigned int i = 0;
E
Eduardo Otubo 已提交
565

566 567 568 569 570
    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);
        }
571 572
    }

573
    if (phypUUIDTable_WriteFile(conn) == -1)
574 575
        goto err;

576
    if (phypUUIDTable_Push(conn) == -1)
577 578
        goto err;

579
    return 0;
580

581
err:
582
    return -1;
583 584
}

585 586
static int
phypUUIDTable_AddLpar(virConnectPtr conn, unsigned char *uuid, int id)
587
{
E
Eduardo Otubo 已提交
588
    phyp_driverPtr phyp_driver = conn->privateData;
589
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
E
Eduardo Otubo 已提交
590

591 592 593 594 595
    uuid_table->nlpars++;
    unsigned int i = uuid_table->nlpars;
    i--;

    if (VIR_REALLOC_N(uuid_table->lpars, uuid_table->nlpars) < 0) {
596
        virReportOOMError();
597
        goto err;
598 599
    }

600 601
    if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
        virReportOOMError();
602
        goto err;
603
    }
604

605
    uuid_table->lpars[i]->id = id;
606
    memcpy(uuid_table->lpars[i]->uuid, uuid, VIR_UUID_BUFLEN);
607

608 609
    if (phypUUIDTable_WriteFile(conn) == -1)
        goto err;
610

611
    if (phypUUIDTable_Push(conn) == -1)
612 613
        goto err;

614
    return 0;
615

616
err:
617
    return -1;
618 619
}

620 621
static int
phypUUIDTable_ReadFile(virConnectPtr conn)
622
{
E
Eduardo Otubo 已提交
623
    phyp_driverPtr phyp_driver = conn->privateData;
624 625 626 627 628 629
    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;
630

631
    if ((fd = open(local_file, O_RDONLY)) == -1) {
632
        VIR_WARN("Unable to read information from local file.");
633
        goto err;
634 635
    }

636 637 638
    /* 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++) {
639

640 641 642 643 644 645 646 647
            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 {
648
                VIR_WARN
649
                    ("Unable to read from information from local file.");
650 651
                goto err;
            }
652

653 654
            rc = read(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN);
            if (rc != VIR_UUID_BUFLEN) {
655
                VIR_WARN("Unable to read information from local file.");
656 657
                goto err;
            }
658
        }
659 660
    } else
        virReportOOMError();
661

662
    VIR_FORCE_CLOSE(fd);
663
    return 0;
664

665
err:
666
    VIR_FORCE_CLOSE(fd);
667
    return -1;
668 669
}

670 671
static int
phypUUIDTable_Pull(virConnectPtr conn)
672 673
{
    ConnectionData *connection_data = conn->networkPrivateData;
674
    LIBSSH2_SESSION *session = connection_data->session;
675 676 677 678
    LIBSSH2_CHANNEL *channel = NULL;
    struct stat fileinfo;
    char buffer[1024];
    int rc = 0;
679
    int fd = -1;
680 681 682 683 684 685
    int got = 0;
    int amount = 0;
    int total = 0;
    int sock = 0;
    char local_file[] = "./uuid_table";
    char *remote_file = NULL;
686
    int ret = -1;
E
Eduardo Otubo 已提交
687

688 689
    if (virAsprintf(&remote_file, "/home/%s/libvirt_uuid_table",
                    NULLSTR(conn->uri->user)) < 0) {
690
        virReportOOMError();
691
        goto cleanup;
692
    }
693

694 695 696
    /* Trying to stat the remote file. */
    do {
        channel = libssh2_scp_recv(session, remote_file, &fileinfo);
697

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

712 713
    /* Creating a new data base based on remote file */
    if ((fd = creat(local_file, 0755)) == -1)
714
        goto cleanup;
715

716 717 718 719
    /* Request a file via SCP */
    while (got < fileinfo.st_size) {
        do {
            amount = sizeof(buffer);
720

721 722 723
            if ((fileinfo.st_size - got) < amount) {
                amount = fileinfo.st_size - got;
            }
E
Eduardo Otubo 已提交
724

725 726 727
            rc = libssh2_channel_read(channel, buffer, amount);
            if (rc > 0) {
                if (safewrite(fd, buffer, rc) != rc)
728
                    VIR_WARN
729
                        ("Unable to write information to local file.");
730

731 732 733 734
                got += rc;
                total += rc;
            }
        } while (rc > 0);
735

736 737 738 739
        if ((rc == LIBSSH2_ERROR_EAGAIN)
            && (got < fileinfo.st_size)) {
            /* this is due to blocking that would occur otherwise
             * so we loop on this condition */
740

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

757
    ret = 0;
758

759
cleanup:
760 761 762 763 764 765 766
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
767 768
    VIR_FORCE_CLOSE(fd);
    return ret;
769 770
}

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

783
    if ((nids_numdomains = phypConnectNumOfDomainsGeneric(conn, 2)) < 0)
E
Eric Blake 已提交
784
        goto cleanup;
785 786

    if (VIR_ALLOC_N(ids, nids_numdomains) < 0) {
787
        virReportOOMError();
E
Eric Blake 已提交
788
        goto cleanup;
789 790
    }

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

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

805 806 807
    phyp_driver = conn->privateData;
    uuid_table = phyp_driver->uuid_table;
    uuid_table->nlpars = nids_listdomains;
808

809 810 811
    /* 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 已提交
812
        table_created = true;
813 814 815 816
        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 已提交
817
                    goto cleanup;
818 819
                }
                uuid_table->lpars[i]->id = ids[i];
820

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

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

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

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

E
Eric Blake 已提交
842 843 844 845 846 847 848
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);
    }
849
    VIR_FREE(ids);
E
Eric Blake 已提交
850
    return ret;
851 852
}

853 854
static void
phypUUIDTable_Free(uuid_tablePtr uuid_table)
855
{
856
    int i;
857

858 859 860 861 862 863 864 865
    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);
866 867
}

868 869 870 871 872 873 874 875
#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)
876
{
877
    size_t len = strlen(src);
878 879
    size_t i = 0;

880
    if (len == 0)
881
        return false;
882

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

892 893
    return false;
}
894

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
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;
923 924
}

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

943 944
    if (userhome == NULL)
        goto err;
E
Eduardo Otubo 已提交
945

946
    if (virAsprintf(&pubkey, "%s/.ssh/id_rsa.pub", userhome) < 0) {
947
        virReportOOMError();
948
        goto err;
949 950
    }

951 952
    if (virAsprintf(&pvtkey, "%s/.ssh/id_rsa", userhome) < 0) {
        virReportOOMError();
953 954 955
        goto err;
    }

956 957
    if (conn->uri->user != NULL) {
        username = strdup(conn->uri->user);
958

959 960 961 962 963 964
        if (username == NULL) {
            virReportOOMError();
            goto err;
        }
    } else {
        if (auth == NULL || auth->cb == NULL) {
965 966
            virReportError(VIR_ERR_AUTH_FAILED,
                           "%s", _("No authentication callback provided."));
967 968
            goto err;
        }
969

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

972
        if (username == NULL) {
973 974
            virReportError(VIR_ERR_AUTH_FAILED, "%s",
                           _("Username request failed"));
975 976 977
            goto err;
        }
    }
978

979 980 981 982
    memset(&hints, 0, sizeof(hints));
    hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
983

984 985
    ret = getaddrinfo(hostname, "22", &hints, &ai);
    if (ret != 0) {
986 987
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Error while getting %s address info"), hostname);
988 989
        goto err;
    }
990

991 992 993 994 995 996 997
    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;
            }
998
            VIR_FORCE_CLOSE(sock);
999 1000 1001
        }
        cur = cur->ai_next;
    }
1002

1003 1004
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Failed to connect to %s"), hostname);
1005 1006
    freeaddrinfo(ai);
    goto err;
1007

1008
connected:
1009

1010
    (*internal_socket) = sock;
1011

1012 1013 1014
    /* Create a session instance */
    session = libssh2_session_init();
    if (!session)
1015 1016
        goto err;

1017 1018
    /* tell libssh2 we want it all done non-blocking */
    libssh2_session_set_blocking(session, 0);
1019

1020 1021 1022
    while ((rc = libssh2_session_startup(session, sock)) ==
           LIBSSH2_ERROR_EAGAIN) ;
    if (rc) {
1023 1024
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Failure establishing SSH session."));
1025 1026
        goto disconnect;
    }
1027

1028 1029 1030 1031 1032
    /* Trying authentication by pubkey */
    if (stat(pvtkey, &pvt_stat) || stat(pubkey, &pub_stat)) {
        rc = LIBSSH2_ERROR_SOCKET_NONE;
        goto keyboard_interactive;
    }
1033

1034 1035 1036 1037 1038 1039
    while ((rc =
            libssh2_userauth_publickey_fromfile(session, username,
                                                pubkey,
                                                pvtkey,
                                                NULL)) ==
           LIBSSH2_ERROR_EAGAIN) ;
1040

1041
keyboard_interactive:
1042 1043 1044 1045
    if (rc == LIBSSH2_ERROR_SOCKET_NONE
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED) {
        if (auth == NULL || auth->cb == NULL) {
1046 1047
            virReportError(VIR_ERR_AUTH_FAILED,
                           "%s", _("No authentication callback provided."));
1048 1049
            goto disconnect;
        }
1050

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

1053
        if (password == NULL) {
1054 1055
            virReportError(VIR_ERR_AUTH_FAILED, "%s",
                           _("Password request failed"));
1056 1057
            goto disconnect;
        }
1058

1059 1060 1061 1062
        while ((rc =
                libssh2_userauth_password(session, username,
                                          password)) ==
               LIBSSH2_ERROR_EAGAIN) ;
1063

1064
        if (rc) {
1065 1066
            virReportError(VIR_ERR_AUTH_FAILED,
                           "%s", _("Authentication failed"));
1067 1068 1069
            goto disconnect;
        } else
            goto exit;
1070

1071 1072
    } else if (rc == LIBSSH2_ERROR_NONE) {
        goto exit;
1073

1074 1075
    } else if (rc == LIBSSH2_ERROR_ALLOC || rc == LIBSSH2_ERROR_SOCKET_SEND
               || rc == LIBSSH2_ERROR_SOCKET_TIMEOUT) {
1076 1077 1078
        goto err;
    }

1079
disconnect:
1080 1081
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1082
err:
1083
    VIR_FORCE_CLOSE(sock);
1084 1085 1086 1087 1088
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
1089
    return NULL;
1090

1091
exit:
1092 1093 1094 1095 1096 1097
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
    return session;
1098 1099
}

1100
static virDrvOpenStatus
1101 1102
phypConnectOpen(virConnectPtr conn,
                virConnectAuthPtr auth, unsigned int flags)
1103 1104 1105 1106 1107 1108 1109 1110
{
    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 已提交
1111

E
Eric Blake 已提交
1112 1113
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1114 1115 1116 1117 1118 1119 1120
    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) {
1121 1122
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing server name in phyp:// URI"));
1123 1124 1125 1126
        return VIR_DRV_OPEN_ERROR;
    }

    if (VIR_ALLOC(phyp_driver) < 0) {
1127
        virReportOOMError();
1128
        goto failure;
1129 1130
    }

1131 1132 1133 1134
    if (VIR_ALLOC(uuid_table) < 0) {
        virReportOOMError();
        goto failure;
    }
1135

1136 1137 1138 1139
    if (VIR_ALLOC(connection_data) < 0) {
        virReportOOMError();
        goto failure;
    }
1140
    connection_data->sock = -1;
1141

1142 1143 1144 1145 1146 1147
    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 已提交
1148

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
        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';

1162
        if (contains_specialcharacters(conn->uri->path)) {
1163 1164 1165
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s",
                           _("Error parsing 'path'. Invalid characters."));
1166 1167 1168 1169 1170
            goto failure;
        }
    }

    if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) {
1171 1172
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Error while opening SSH session."));
1173 1174 1175 1176
        goto failure;
    }

    connection_data->session = session;
1177
    connection_data->sock = internal_socket;
1178 1179 1180 1181 1182 1183 1184 1185 1186

    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) {
1187
        virReportOOMError();
1188
        goto failure;
1189 1190
    }

1191
    if (!(phyp_driver->xmlopt = virDomainXMLOptionNew(NULL, NULL, NULL)))
1192 1193
        goto failure;

1194 1195
    conn->privateData = phyp_driver;
    conn->networkPrivateData = connection_data;
1196

1197 1198
    if ((phyp_driver->system_type = phypGetSystemType(conn)) == -1)
        goto failure;
1199

1200 1201
    if (phypUUIDTable_Init(conn) == -1)
        goto failure;
1202

1203 1204 1205 1206 1207 1208 1209
    if (phyp_driver->system_type == HMC) {
        if ((phyp_driver->vios_id = phypGetVIOSPartitionID(conn)) == -1)
            goto failure;
    }

    return VIR_DRV_OPEN_SUCCESS;

1210
failure:
1211
    if (phyp_driver != NULL) {
1212
        virObjectUnref(phyp_driver->caps);
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
        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);
    }

1224 1225
    if (connection_data)
        VIR_FORCE_CLOSE(connection_data->sock);
1226 1227 1228
    VIR_FREE(connection_data);

    return VIR_DRV_OPEN_ERROR;
1229 1230 1231
}

static int
1232
phypConnectClose(virConnectPtr conn)
1233
{
1234 1235 1236
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
1237

1238 1239
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1240

1241
    virObjectUnref(phyp_driver->caps);
1242
    virObjectUnref(phyp_driver->xmlopt);
1243 1244 1245
    phypUUIDTable_Free(phyp_driver->uuid_table);
    VIR_FREE(phyp_driver->managed_system);
    VIR_FREE(phyp_driver);
1246 1247

    VIR_FORCE_CLOSE(connection_data->sock);
1248 1249 1250
    VIR_FREE(connection_data);
    return 0;
}
1251 1252


1253
static int
1254
phypConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1255 1256 1257 1258
{
    /* Phyp uses an SSH tunnel, so is always encrypted */
    return 1;
}
1259

1260 1261

static int
1262
phypConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1263 1264 1265
{
    /* Phyp uses an SSH tunnel, so is always secure */
    return 1;
1266 1267
}

1268 1269

static int
1270
phypConnectIsAlive(virConnectPtr conn)
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
{
    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;
}


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

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

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

1309 1310 1311 1312
/* return the lpar name given a lpar_id and a managed system name */
static char *
phypGetLparNAME(LIBSSH2_SESSION * session, const char *managed_system,
                unsigned int lpar_id, virConnectPtr conn)
1313 1314
{
    phyp_driverPtr phyp_driver = conn->privateData;
1315 1316 1317 1318
    int system_type = phyp_driver->system_type;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1319

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

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

1331 1332 1333 1334 1335 1336 1337 1338 1339

/* Search into the uuid_table for a lpar_uuid given a lpar_id
 * and a managed system name
 *
 * return:  0 - record found
 *         -1 - not found
 * */
static int
phypGetLparUUID(unsigned char *uuid, int lpar_id, virConnectPtr conn)
1340 1341
{
    phyp_driverPtr phyp_driver = conn->privateData;
1342 1343 1344
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    lparPtr *lpars = uuid_table->lpars;
    unsigned int i = 0;
1345

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

1353
    return -1;
1354 1355
}

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

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

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

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

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

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

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

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

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

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

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

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

1453 1454 1455 1456 1457 1458
/* XXX - is this needed? */
static char *phypGetBackingDevice(virConnectPtr, const char *, char *)
    ATTRIBUTE_UNUSED;
static char *
phypGetBackingDevice(virConnectPtr conn, const char *managed_system,
                     char *lpar_name)
1459
{
1460 1461
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
1462
    phyp_driverPtr phyp_driver = conn->privateData;
1463 1464 1465 1466 1467 1468 1469
    int system_type = phyp_driver->system_type;
    char *ret = NULL;
    int remote_slot = 0;
    int exit_status = 0;
    char *char_ptr;
    char *backing_device = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1470

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

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

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

1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
    /* here is a little trick to deal returns of this kind:
     *
     * 0x8100000000000000//lv01
     *
     * the information we really need is only lv01, so we
     * need to skip a lot of things on the string.
     * */
    char_ptr = strchr(ret, '/');

    if (char_ptr) {
        char_ptr++;
        if (char_ptr[0] == '/')
            char_ptr++;
        else
1499
            goto cleanup;
1500 1501 1502 1503 1504

        backing_device = strdup(char_ptr);

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

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

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

1517
cleanup:
1518
    VIR_FREE(ret);
1519

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

static char *
phypGetLparProfile(virConnectPtr conn, int lpar_id)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
1537 1538
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1539 1540
                      " -r prof --filter lpar_ids=%d -F name|head -n 1",
                      lpar_id);
1541
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1542

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

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

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

    virBufferAddLit(&buf, "lssyscfg");

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

1571
    virBufferAsprintf(&buf, " -r prof --filter "
1572 1573 1574 1575 1576
                      "profile_names=%s -F virtual_eth_adapters,"
                      "virtual_opti_pool_id,virtual_scsi_adapters,"
                      "virtual_serial_adapters|sed -e 's/\"//g' -e "
                      "'s/,/\\n/g'|sed -e 's/\\(^[0-9][0-9]\\*\\).*$/\\1/'"
                      "|sort|tail -n 1", profile);
E
Eric Blake 已提交
1577 1578 1579
    if (phypExecInt(session, &buf, conn, &slot) < 0)
        return -1;
    return slot + 1;
1580 1581 1582 1583 1584
}

static int
phypCreateServerSCSIAdapter(virConnectPtr conn)
{
1585
    int result = -1;
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    char *profile = NULL;
    int slot = 0;
    char *vios_name = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!
        (vios_name =
         phypGetLparNAME(session, managed_system, vios_id, conn))) {
1602
        VIR_ERROR(_("Unable to get VIOS name"));
1603
        goto cleanup;
1604 1605 1606
    }

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

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

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

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

    /* Here I change the VIOS configuration to append the new adapter
     * with the free slot I got with phypGetVIOSNextSlotNumber.
     * */
    virBufferAddLit(&buf, "chsyscfg");
    if (system_type == HMC)
1635 1636
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r prof -i 'name=%s,lpar_id=%d,"
1637 1638
                      "\"virtual_scsi_adapters=%s,%d/server/any/any/1\"'",
                      vios_name, vios_id, ret, slot);
1639
    VIR_FREE(ret);
1640
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1641 1642

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

    /* Finally I add the new scsi adapter to VIOS using the same slot
     * I used in the VIOS configuration.
     * */
    virBufferAddLit(&buf, "chhwres -r virtualio --rsubtype scsi");
    if (system_type == HMC)
1650 1651
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1652 1653
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      vios_name, slot);
1654
    VIR_FREE(ret);
1655
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1656 1657

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

1660
    result = 0;
1661

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

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

static char *
phypGetVIOSFreeSCSIAdapter(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
1684
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1685 1686
                          managed_system, vios_id);

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

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

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

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


static int
1702
phypDomainAttachDevice(virDomainPtr domain, const char *xml)
1703
{
1704
    int result = -1;
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
    virConnectPtr conn = domain->conn;
    ConnectionData *connection_data = domain->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = domain->conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    char *ret = NULL;
    char *scsi_adapter = NULL;
    int slot = 0;
    char *vios_name = NULL;
    char *profile = NULL;
    virDomainDeviceDefPtr dev = NULL;
    virDomainDefPtr def = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *domain_name = NULL;

E
Eric Blake 已提交
1723 1724 1725 1726 1727
    if (VIR_ALLOC(def) < 0) {
        virReportOOMError();
        goto cleanup;
    }

1728
    domain_name = escape_specialcharacters(domain->name);
1729

1730
    if (domain_name == NULL) {
1731
        goto cleanup;
1732 1733 1734 1735 1736 1737
    }

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

    if (def->os.type == NULL) {
        virReportOOMError();
1738
        goto cleanup;
1739 1740
    }

1741 1742
    dev = virDomainDeviceDefParse(xml, def, phyp_driver->caps, NULL,
                                  VIR_DOMAIN_XML_INACTIVE);
1743
    if (!dev) {
1744
        goto cleanup;
1745 1746 1747 1748 1749
    }

    if (!
        (vios_name =
         phypGetLparNAME(session, managed_system, vios_id, conn))) {
1750
        VIR_ERROR(_("Unable to get VIOS name"));
1751
        goto cleanup;
1752 1753 1754 1755 1756 1757 1758 1759
    }

    /* 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) {
1760
            VIR_ERROR(_("Unable to create new virtual adapter"));
1761
            goto cleanup;
1762 1763
        } else {
            if (!(scsi_adapter = phypGetVIOSFreeSCSIAdapter(conn))) {
1764
                VIR_ERROR(_("Unable to create new virtual adapter"));
1765
                goto cleanup;
1766 1767 1768 1769 1770
            }
        }
    }

    if (system_type == HMC)
1771
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1772 1773
                          managed_system, vios_id);

1774
    virBufferAsprintf(&buf, "mkvdev -vdev %s -vadapter %s",
1775 1776 1777 1778
                      dev->data.disk->src, scsi_adapter);

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1779
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1780 1781

    if (exit_status < 0 || ret == NULL)
1782
        goto cleanup;
1783 1784

    if (!(profile = phypGetLparProfile(conn, domain->id))) {
1785
        VIR_ERROR(_("Unable to get VIOS profile name."));
1786
        goto cleanup;
1787 1788 1789 1790 1791 1792
    }

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

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

    if (exit_status < 0 || ret == NULL)
1814
        goto cleanup;
1815 1816 1817 1818 1819 1820

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

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

    if (exit_status < 0 || ret == NULL) {
1843
        VIR_ERROR(_
1844 1845
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    "Contact your support to enable this feature."));
1846
        goto cleanup;
1847 1848
    }

1849
    result = 0;
1850

1851
cleanup:
1852
    VIR_FREE(ret);
1853 1854
    virDomainDeviceDefFree(dev);
    virDomainDefFree(def);
1855 1856
    VIR_FREE(vios_name);
    VIR_FREE(scsi_adapter);
1857 1858 1859 1860
    VIR_FREE(profile);
    VIR_FREE(domain_name);

    return result;
1861 1862
}

1863
static char *
1864
phypStorageVolGetKey(virConnectPtr conn, const char *name)
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
{
    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)
1877
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1878 1879
                          managed_system, vios_id);

1880
    virBufferAsprintf(&buf, "lslv %s -field lvid", name);
1881 1882 1883 1884

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

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

1888
    if (exit_status < 0)
1889 1890
        VIR_FREE(ret);
    return ret;
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
}

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

1910
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field name", name);
1911 1912 1913 1914

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

1915
    virBufferAddLit(&buf, "|sed '1d; s/ //g'");
1916
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1917

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

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;
1932
    int sp_size = -1;
1933 1934 1935
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
1936
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1937 1938
                          managed_system, vios_id);

1939
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field size", name);
1940 1941 1942 1943

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

1944
    virBufferAddLit(&buf, "|sed '1d; s/ //g'");
E
Eric Blake 已提交
1945
    phypExecInt(session, &buf, conn, &sp_size);
1946
    return sp_size;
1947 1948
}

1949
static char *
1950
phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
1951
                unsigned int capacity)
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
{
    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;
1962
    char *key = NULL;
1963 1964

    if (system_type == HMC)
1965
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1966 1967
                          managed_system, vios_id);

1968
    virBufferAsprintf(&buf, "mklv -lv %s %s %d", lvname, spname, capacity);
1969 1970 1971

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1972
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1973 1974

    if (exit_status < 0) {
1975
        VIR_ERROR(_("Unable to create Volume: %s"), NULLSTR(ret));
1976
        goto cleanup;
1977 1978
    }

1979
    key = phypStorageVolGetKey(conn, lvname);
1980

1981
cleanup:
1982 1983
    VIR_FREE(ret);

1984
    return key;
1985 1986 1987
}

static virStorageVolPtr
1988
phypStorageVolLookupByName(virStoragePoolPtr pool, const char *volname)
1989
{
1990 1991
    char *key;
    virStorageVolPtr vol;
1992

1993
    key = phypStorageVolGetKey(pool->conn, volname);
1994

1995
    if (key == NULL)
1996 1997
        return NULL;

1998
    vol = virGetStorageVol(pool->conn, pool->name, volname, key, NULL, NULL);
1999 2000 2001 2002

    VIR_FREE(key);

    return vol;
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
}

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

    virStorageVolDefPtr voldef = NULL;
    virStoragePoolDefPtr spdef = NULL;
    virStorageVolPtr vol = NULL;
2014
    virStorageVolPtr dup_vol = NULL;
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
    char *key = NULL;

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

    /* Filling spdef manually
     * */
    if (pool->name != NULL) {
        spdef->name = pool->name;
    } else {
2027
        VIR_ERROR(_("Unable to determine storage pool's name."));
2028 2029 2030 2031
        goto err;
    }

    if (memcpy(spdef->uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
2032
        VIR_ERROR(_("Unable to determine storage pool's uuid."));
2033 2034 2035 2036 2037
        goto err;
    }

    if ((spdef->capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2038
        VIR_ERROR(_("Unable to determine storage pools's size."));
2039 2040 2041
        goto err;
    }

J
Ján Tomko 已提交
2042
    /* Information not available */
2043 2044 2045 2046 2047 2048
    spdef->allocation = 0;
    spdef->available = 0;

    spdef->source.ndevice = 1;

    /*XXX source adapter not working properly, should show hdiskX */
2049
    if ((spdef->source.adapter.data.name =
2050
         phypGetStoragePoolDevice(pool->conn, pool->name)) == NULL) {
2051
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
2052 2053 2054 2055
        goto err;
    }

    if ((voldef = virStorageVolDefParseString(spdef, xml)) == NULL) {
2056
        VIR_ERROR(_("Error parsing volume XML."));
2057 2058 2059 2060
        goto err;
    }

    /* checking if this name already exists on this system */
2061
    if ((dup_vol = phypStorageVolLookupByName(pool, voldef->name)) != NULL) {
2062
        VIR_ERROR(_("StoragePool name already exists."));
2063
        virObjectUnref(dup_vol);
2064 2065 2066 2067 2068 2069 2070
        goto err;
    }

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

    if (voldef->capacity) {
2076
        VIR_ERROR(_("Capacity cannot be empty."));
2077 2078 2079
        goto err;
    }

2080 2081 2082 2083
    key = phypBuildVolume(pool->conn, voldef->name, spdef->name,
                          voldef->capacity);

    if (key == NULL)
2084 2085 2086 2087
        goto err;

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

2091 2092
    VIR_FREE(key);

2093 2094
    return vol;

2095
err:
2096
    VIR_FREE(key);
2097 2098
    virStorageVolDefFree(voldef);
    virStoragePoolDefFree(spdef);
2099
    virObjectUnref(vol);
2100 2101 2102 2103
    return NULL;
}

static char *
2104
phypStorageVolGetPhysicalVolumeByStoragePool(virStorageVolPtr vol, char *sp)
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
{
    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)
2118
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2119 2120
                          managed_system, vios_id);

2121
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field pvname", sp);
2122 2123 2124 2125

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

2126
    virBufferAddLit(&buf, "|sed 1d");
2127
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2128

2129
    if (exit_status < 0)
2130 2131
        VIR_FREE(ret);
    return ret;
2132 2133 2134
}

static virStorageVolPtr
2135
phypStorageVolLookupByPath(virConnectPtr conn, const char *volname)
2136 2137 2138 2139 2140 2141 2142 2143
{
    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;
2144
    char *ret = NULL;
2145 2146
    char *key = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2147
    virStorageVolPtr vol = NULL;
2148 2149

    if (system_type == HMC)
2150
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2151 2152
                          managed_system, vios_id);

2153
    virBufferAsprintf(&buf, "lslv %s -field vgname", volname);
2154 2155 2156 2157

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

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

2161
    if (exit_status < 0 || ret == NULL)
2162
        goto cleanup;
2163

2164
    key = phypStorageVolGetKey(conn, volname);
2165

2166
    if (key == NULL)
2167
        goto cleanup;
2168

2169
    vol = virGetStorageVol(conn, ret, volname, key, NULL, NULL);
2170

2171
cleanup:
2172
    VIR_FREE(ret);
2173 2174 2175
    VIR_FREE(key);

    return vol;
2176 2177 2178 2179 2180 2181
}

static int
phypGetStoragePoolUUID(virConnectPtr conn, unsigned char *uuid,
                       const char *name)
{
2182
    int result = -1;
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
    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)
2194
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2195 2196
                          managed_system, vios_id);

2197
    virBufferAsprintf(&buf, "lsdev -dev %s -attr vgserial_id", name);
2198 2199 2200 2201

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

2202
    virBufferAddLit(&buf, "|sed '1,2d'");
2203
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2204 2205

    if (exit_status < 0 || ret == NULL)
2206
        goto cleanup;
2207

2208
    if (memcpy(uuid, ret, VIR_UUID_BUFLEN) == NULL)
2209
        goto cleanup;
2210

2211
    result = 0;
2212

2213
cleanup:
2214
    VIR_FREE(ret);
2215 2216

    return result;
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
}

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

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

2227
    return virGetStoragePool(conn, name, uuid, NULL, NULL);
2228 2229 2230
}

static char *
2231
phypStorageVolGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
2232
{
2233 2234 2235
    virStorageVolDef voldef;
    virStoragePoolDef pool;
    virStoragePoolPtr sp;
2236
    char *xml = NULL;
2237

2238 2239 2240
    virCheckFlags(0, NULL);

    memset(&voldef, 0, sizeof(virStorageVolDef));
2241
    memset(&pool, 0, sizeof(virStoragePoolDef));
2242

2243
    sp = phypStoragePoolLookupByName(vol->conn, vol->pool);
2244 2245

    if (!sp)
2246
        goto cleanup;
2247 2248 2249 2250

    if (sp->name != NULL) {
        pool.name = sp->name;
    } else {
2251
        VIR_ERROR(_("Unable to determine storage sp's name."));
2252
        goto cleanup;
2253 2254
    }

2255
    if (memcpy(pool.uuid, sp->uuid, VIR_UUID_BUFLEN) == NULL) {
2256
        VIR_ERROR(_("Unable to determine storage sp's uuid."));
2257
        goto cleanup;
2258 2259 2260
    }

    if ((pool.capacity = phypGetStoragePoolSize(sp->conn, sp->name)) == -1) {
2261
        VIR_ERROR(_("Unable to determine storage sps's size."));
2262
        goto cleanup;
2263 2264
    }

J
Ján Tomko 已提交
2265
    /* Information not available */
2266 2267 2268 2269 2270
    pool.allocation = 0;
    pool.available = 0;

    pool.source.ndevice = 1;

2271
    if ((pool.source.adapter.data.name =
2272
         phypGetStoragePoolDevice(sp->conn, sp->name)) == NULL) {
2273
        VIR_ERROR(_("Unable to determine storage sps's source adapter."));
2274
        goto cleanup;
2275 2276 2277 2278 2279
    }

    if (vol->name != NULL)
        voldef.name = vol->name;
    else {
2280
        VIR_ERROR(_("Unable to determine storage pool's name."));
2281
        goto cleanup;
2282 2283
    }

2284 2285 2286 2287
    voldef.key = strdup(vol->key);

    if (voldef.key == NULL) {
        virReportOOMError();
2288
        goto cleanup;
2289 2290 2291 2292
    }

    voldef.type = VIR_STORAGE_POOL_LOGICAL;

2293 2294 2295 2296
    xml = virStorageVolDefFormat(&pool, &voldef);

    VIR_FREE(voldef.key);

2297
cleanup:
2298
    virObjectUnref(sp);
2299
    return xml;
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
}

/* 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 *
2311
phypStorageVolGetPath(virStorageVolPtr vol)
2312 2313 2314 2315 2316 2317 2318 2319 2320
{
    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;
2321
    char *ret = NULL;
2322 2323
    char *path = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2324
    char *pv;
2325 2326

    if (system_type == HMC)
2327
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2328 2329
                          managed_system, vios_id);

2330
    virBufferAsprintf(&buf, "lslv %s -field vgname", vol->name);
2331 2332 2333 2334

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

2335
    virBufferAsprintf(&buf,
2336
                      "|sed -e 's/^VOLUME GROUP://g' -e 's/ //g'");
2337
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2338

2339
    if (exit_status < 0 || ret == NULL)
2340
        goto cleanup;
2341

2342
    pv = phypStorageVolGetPhysicalVolumeByStoragePool(vol, ret);
2343

2344 2345
    if (!pv)
        goto cleanup;
2346

2347
    if (virAsprintf(&path, "/%s/%s/%s", pv, ret, vol->name) < 0) {
2348 2349 2350
        virReportOOMError();
        goto cleanup;
    }
2351

2352
cleanup:
2353
    VIR_FREE(ret);
2354
    VIR_FREE(path);
2355 2356

    return path;
2357 2358 2359 2360 2361 2362
}

static int
phypStoragePoolListVolumes(virStoragePoolPtr pool, char **const volumes,
                           int nvolumes)
{
2363
    bool success = false;
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
    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 已提交
2376
    char *char_ptr = NULL;
2377 2378 2379
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2380
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2381 2382
                          managed_system, vios_id);

2383
    virBufferAsprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
2384 2385 2386 2387

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

2388
    virBufferAddLit(&buf, "|sed '1,2d'");
2389
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2390 2391 2392

    /* I need to parse the textual return in order to get the volumes */
    if (exit_status < 0 || ret == NULL)
2393
        goto cleanup;
2394 2395 2396 2397
    else {
        volumes_list = ret;

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

E
Eric Blake 已提交
2400 2401
            if (char_ptr) {
                *char_ptr = '\0';
2402 2403
                if ((volumes[got++] = strdup(volumes_list)) == NULL) {
                    virReportOOMError();
2404
                    goto cleanup;
2405
                }
E
Eric Blake 已提交
2406 2407
                char_ptr++;
                volumes_list = char_ptr;
2408 2409 2410 2411 2412
            } else
                break;
        }
    }

2413 2414
    success = true;

2415
cleanup:
2416 2417 2418 2419 2420 2421
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(volumes[i]);

        got = -1;
    }
2422
    VIR_FREE(ret);
2423
    return got;
2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
}

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;
2434
    int nvolumes = -1;
2435 2436 2437 2438 2439
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

    /* We need to remove 2 line from the header text output */
E
Eric Blake 已提交
2450
    return nvolumes - 2;
2451 2452 2453
}

static int
2454
phypStoragePoolDestroy(virStoragePoolPtr pool)
2455
{
2456
    int result = -1;
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
    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)
2469
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2470 2471
                          managed_system, vios_id);

2472
    virBufferAsprintf(&buf, "rmsp %s", pool->name);
2473 2474 2475

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2476
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2477 2478

    if (exit_status < 0) {
2479
        VIR_ERROR(_("Unable to destroy Storage Pool: %s"), NULLSTR(ret));
2480
        goto cleanup;
2481 2482
    }

2483
    result = 0;
2484

2485
cleanup:
2486
    VIR_FREE(ret);
2487 2488

    return result;
2489 2490 2491 2492 2493
}

static int
phypBuildStoragePool(virConnectPtr conn, virStoragePoolDefPtr def)
{
2494
    int result = -1;
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505
    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;

2506 2507 2508 2509 2510 2511 2512
    if (source.adapter.type !=
        VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Only 'scsi_host' adapter is supported"));
        goto cleanup;
    }

2513
    if (system_type == HMC)
2514
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2515 2516
                          managed_system, vios_id);

2517
    virBufferAsprintf(&buf, "mksp -f %schild %s", def->name,
2518
                      source.adapter.data.name);
2519 2520 2521

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2522
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2523 2524

    if (exit_status < 0) {
2525
        VIR_ERROR(_("Unable to create Storage Pool: %s"), NULLSTR(ret));
2526
        goto cleanup;
2527 2528
    }

2529
    result = 0;
2530

2531
cleanup:
2532
    VIR_FREE(ret);
2533 2534

    return result;
2535 2536 2537 2538

}

static int
2539
phypConnectNumOfStoragePools(virConnectPtr conn)
2540 2541 2542 2543 2544
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
2545
    int nsp = -1;
2546 2547 2548 2549 2550
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2551
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2552 2553
                          managed_system, vios_id);

2554
    virBufferAddLit(&buf, "lsvg");
2555 2556 2557 2558

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

2559
    virBufferAddLit(&buf, "|grep -c '^.*$'");
E
Eric Blake 已提交
2560
    phypExecInt(session, &buf, conn, &nsp);
2561
    return nsp;
2562 2563 2564
}

static int
2565
phypConnectListStoragePools(virConnectPtr conn, char **const pools, int npools)
2566
{
2567
    bool success = false;
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
    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 已提交
2579
    char *char_ptr = NULL;
2580 2581 2582
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2583
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2584 2585
                          managed_system, vios_id);

2586
    virBufferAddLit(&buf, "lsvg");
2587 2588 2589

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2590
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2591 2592 2593

    /* I need to parse the textual return in order to get the storage pools */
    if (exit_status < 0 || ret == NULL)
2594
        goto cleanup;
2595 2596 2597 2598
    else {
        storage_pools = ret;

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

E
Eric Blake 已提交
2601 2602
            if (char_ptr) {
                *char_ptr = '\0';
2603 2604
                if ((pools[got++] = strdup(storage_pools)) == NULL) {
                    virReportOOMError();
2605
                    goto cleanup;
2606
                }
E
Eric Blake 已提交
2607 2608
                char_ptr++;
                storage_pools = char_ptr;
2609 2610 2611 2612 2613
            } else
                break;
        }
    }

2614 2615
    success = true;

2616
cleanup:
2617 2618 2619 2620 2621 2622
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(pools[i]);

        got = -1;
    }
2623
    VIR_FREE(ret);
2624
    return got;
2625 2626 2627
}

static virStoragePoolPtr
2628 2629
phypStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid)
2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
{
    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;
    }

2643
    if ((npools = phypConnectNumOfStoragePools(conn)) == -1) {
2644 2645 2646 2647 2648 2649 2650 2651 2652
        virReportOOMError();
        goto err;
    }

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

2653
    if ((gotpools = phypConnectListStoragePools(conn, pools, npools)) == -1) {
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667
        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)) {
2668
            sp = virGetStoragePool(conn, pools[i], uuid, NULL, NULL);
2669 2670 2671 2672 2673 2674 2675 2676 2677 2678
            VIR_FREE(local_uuid);
            VIR_FREE(pools);

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

2679
err:
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
    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;
2692
    virStoragePoolPtr dup_sp = NULL;
2693 2694 2695 2696 2697 2698
    virStoragePoolPtr sp = NULL;

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

    /* checking if this name already exists on this system */
2699
    if ((dup_sp = phypStoragePoolLookupByName(conn, def->name)) != NULL) {
2700
        VIR_WARN("StoragePool name already exists.");
2701
        virObjectUnref(dup_sp);
2702 2703 2704 2705
        goto err;
    }

    /* checking if ID or UUID already exists on this system */
2706
    if ((dup_sp = phypStoragePoolLookupByUUID(conn, def->uuid)) != NULL) {
2707
        VIR_WARN("StoragePool uuid already exists.");
2708
        virObjectUnref(dup_sp);
2709 2710
        goto err;
    }
2711

2712
    if ((sp = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL)) == NULL)
2713 2714 2715 2716 2717 2718 2719
        goto err;

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

    return sp;

2720
err:
2721
    virStoragePoolDefFree(def);
2722
    virObjectUnref(sp);
2723 2724 2725 2726
    return NULL;
}

static char *
2727
phypStoragePoolGetXMLDesc(virStoragePoolPtr pool, unsigned int flags)
2728 2729 2730 2731 2732 2733 2734 2735 2736
{
    virCheckFlags(0, NULL);

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

    if (pool->name != NULL)
        def.name = pool->name;
    else {
2737
        VIR_ERROR(_("Unable to determine storage pool's name."));
2738 2739 2740
        goto err;
    }

2741
    if (memcpy(def.uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
2742
        VIR_ERROR(_("Unable to determine storage pool's uuid."));
2743 2744 2745 2746 2747
        goto err;
    }

    if ((def.capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2748
        VIR_ERROR(_("Unable to determine storage pools's size."));
2749 2750 2751
        goto err;
    }

J
Ján Tomko 已提交
2752
    /* Information not available */
2753 2754 2755 2756 2757 2758
    def.allocation = 0;
    def.available = 0;

    def.source.ndevice = 1;

    /*XXX source adapter not working properly, should show hdiskX */
2759
    if ((def.source.adapter.data.name =
2760
         phypGetStoragePoolDevice(pool->conn, pool->name)) == NULL) {
2761
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
2762 2763 2764 2765 2766
        goto err;
    }

    return virStoragePoolDefFormat(&def);

2767
err:
2768
    return NULL;
2769 2770
}

E
Eduardo Otubo 已提交
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
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 已提交
2787
    int rv = -1;
E
Eduardo Otubo 已提交
2788 2789 2790 2791 2792

    /* Getting the remote slot number */

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

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

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

2807
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2808 2809 2810
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,lpar_id|"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
2811
    if (phypExecInt(session, &buf, iface->conn, &lpar_id) < 0)
E
Eric Blake 已提交
2812
        goto cleanup;
E
Eduardo Otubo 已提交
2813 2814 2815 2816

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

2819
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2820 2821
                      " -r virtualio --rsubtype eth"
                      " --id %d -o r -s %d", lpar_id, slot_num);
2822 2823
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, iface->conn, false);
E
Eduardo Otubo 已提交
2824 2825

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

E
Eric Blake 已提交
2828
    rv = 0;
E
Eduardo Otubo 已提交
2829

E
Eric Blake 已提交
2830
cleanup:
E
Eduardo Otubo 已提交
2831
    VIR_FREE(ret);
E
Eric Blake 已提交
2832
    return rv;
E
Eduardo Otubo 已提交
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852
}

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 已提交
2853
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2854 2855

    if (!(def = virInterfaceDefParseString(xml)))
E
Eric Blake 已提交
2856
        goto cleanup;
E
Eduardo Otubo 已提交
2857 2858 2859 2860

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

2863
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2864 2865 2866
                      " -r virtualio --rsubtype slot --level slot"
                      " -Fslot_num --filter lpar_names=%s"
                      " |sort|tail -n 1", def->name);
E
Eric Blake 已提交
2867
    if (phypExecInt(session, &buf, conn, &slot) < 0)
E
Eric Blake 已提交
2868
        goto cleanup;
E
Eduardo Otubo 已提交
2869 2870 2871 2872 2873 2874 2875

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

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

2878
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2879 2880 2881
                      " -r virtualio --rsubtype eth"
                      " -p %s -o a -s %d -a port_vlan_id=1,"
                      "ieee_virtual_eth=0", def->name, slot);
2882 2883
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2884 2885

    if (exit_status < 0 || ret != NULL)
E
Eric Blake 已提交
2886
        goto cleanup;
E
Eduardo Otubo 已提交
2887 2888 2889 2890 2891 2892 2893 2894 2895

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

2898
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2899 2900 2901
                      " -r virtualio --rsubtype slot --level slot"
                      " |sed '/lpar_name=%s/!d; /slot_num=%d/!d; "
                      "s/^.*drc_name=//'", def->name, slot);
2902 2903
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2904 2905 2906 2907 2908

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

2911
        virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2912 2913
                " -r virtualio --rsubtype eth"
                " -p %s -o r -s %d", def->name, slot);
2914 2915
        VIR_FREE(ret);
        ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eric Blake 已提交
2916
        goto cleanup;
E
Eduardo Otubo 已提交
2917 2918 2919 2920 2921 2922 2923
    }

    memcpy(name, ret, PHYP_IFACENAME_SIZE-1);

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

2926
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2927 2928 2929
                      "-r virtualio --rsubtype eth --level lpar "
                      " |sed '/lpar_name=%s/!d; /slot_num=%d/!d; "
                      "s/^.*mac_addr=//'", def->name, slot);
2930 2931
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2932 2933

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
2934
        goto cleanup;
E
Eduardo Otubo 已提交
2935 2936 2937

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

E
Eric Blake 已提交
2940
cleanup:
E
Eduardo Otubo 已提交
2941 2942
    VIR_FREE(ret);
    virInterfaceDefFree(def);
E
Eric Blake 已提交
2943
    return result;
E
Eduardo Otubo 已提交
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
}

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];
2960
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2961 2962 2963 2964

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

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

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

2979
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2980 2981 2982
                      " -r virtualio --rsubtype slot --level slot "
                      " -F drc_name,lpar_id |"
                      " sed -n '/%s/ s/^.*,//p'", name);
E
Eric Blake 已提交
2983
    if (phypExecInt(session, &buf, conn, &lpar_id) < 0)
E
Eric Blake 已提交
2984
        goto cleanup;
E
Eduardo Otubo 已提交
2985 2986 2987 2988

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

2991
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2992 2993 2994
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F lpar_id,slot_num,mac_addr|"
                      " sed -n '/%d,%d/ s/^.*,//p'", lpar_id, slot);
2995
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
E
Eduardo Otubo 已提交
2996 2997

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
2998
        goto cleanup;
E
Eduardo Otubo 已提交
2999 3000 3001

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

E
Eric Blake 已提交
3004
cleanup:
E
Eduardo Otubo 已提交
3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
    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 已提交
3018
    int state = -1;
E
Eduardo Otubo 已提交
3019 3020 3021

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

3024
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
3025 3026 3027
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,state |"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
3028
    phypExecInt(session, &buf, iface->conn, &state);
E
Eduardo Otubo 已提交
3029 3030 3031 3032
    return state;
}

static int
3033
phypConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
E
Eduardo Otubo 已提交
3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045
{
    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 已提交
3046
    char *char_ptr = NULL;
E
Eduardo Otubo 已提交
3047
    virBuffer buf = VIR_BUFFER_INITIALIZER;
E
Eric Blake 已提交
3048
    bool success = false;
E
Eduardo Otubo 已提交
3049 3050 3051

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

E
Eric Blake 已提交
3058 3059
    /* I need to parse the textual return in order to get the network
     * interfaces */
E
Eduardo Otubo 已提交
3060
    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3061
        goto cleanup;
E
Eduardo Otubo 已提交
3062 3063 3064 3065

    networks = ret;

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

E
Eric Blake 已提交
3068 3069
        if (char_ptr) {
            *char_ptr = '\0';
E
Eduardo Otubo 已提交
3070 3071
            if ((names[got++] = strdup(networks)) == NULL) {
                virReportOOMError();
E
Eric Blake 已提交
3072
                goto cleanup;
E
Eduardo Otubo 已提交
3073
            }
E
Eric Blake 已提交
3074 3075
            char_ptr++;
            networks = char_ptr;
E
Eduardo Otubo 已提交
3076 3077 3078 3079 3080
        } else {
            break;
        }
    }

E
Eric Blake 已提交
3081 3082 3083 3084 3085
cleanup:
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);
    }
E
Eduardo Otubo 已提交
3086 3087 3088 3089 3090
    VIR_FREE(ret);
    return got;
}

static int
3091
phypConnectNumOfInterfaces(virConnectPtr conn)
E
Eduardo Otubo 已提交
3092 3093 3094 3095 3096 3097 3098
{
    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 已提交
3099
    int nnets = -1;
E
Eduardo Otubo 已提交
3100 3101 3102 3103
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

3106
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
3107 3108
                      "-r virtualio --rsubtype eth --level lpar|"
                      "grep -v lpar_id=%d|grep -c lpar_name", vios_id);
E
Eric Blake 已提交
3109
    phypExecInt(session, &buf, conn, &nnets);
E
Eduardo Otubo 已提交
3110 3111 3112
    return nnets;
}

3113 3114
static int
phypGetLparState(virConnectPtr conn, unsigned int lpar_id)
3115
{
3116
    ConnectionData *connection_data = conn->networkPrivateData;
3117
    phyp_driverPtr phyp_driver = conn->privateData;
3118 3119 3120 3121 3122 3123 3124
    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;
3125

3126 3127
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
3128 3129
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F state --filter lpar_ids=%d", lpar_id);
3130
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
3131

3132 3133
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3134

3135 3136 3137 3138 3139 3140
    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;
3141

3142
cleanup:
3143 3144
    VIR_FREE(ret);
    return state;
3145 3146
}

3147 3148 3149 3150
/* XXX - is this needed? */
static int phypDiskType(virConnectPtr, char *) ATTRIBUTE_UNUSED;
static int
phypDiskType(virConnectPtr conn, char *backing_device)
3151 3152
{
    phyp_driverPtr phyp_driver = conn->privateData;
3153 3154 3155 3156 3157 3158 3159 3160 3161
    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;
3162

3163 3164
    virBufferAddLit(&buf, "viosvrcmd");
    if (system_type == HMC)
3165 3166
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -p %d -c \"lssp -field name type "
E
Eric Blake 已提交
3167
                      "-fmt , -all|sed -n '/%s/ {\n s/^.*,//\n p\n}'\"",
3168
                      vios_id, backing_device);
3169
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
3170

3171 3172
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3173

3174 3175 3176 3177
    if (STREQ(ret, "LVPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_BLOCK;
    else if (STREQ(ret, "FBPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_FILE;
3178

3179
cleanup:
3180 3181 3182
    VIR_FREE(ret);
    return disk_type;
}
3183

3184
static int
3185
phypConnectNumOfDefinedDomains(virConnectPtr conn)
3186
{
3187
    return phypConnectNumOfDomainsGeneric(conn, 1);
3188
}
3189

3190
static int
3191
phypConnectNumOfDomains(virConnectPtr conn)
3192
{
3193
    return phypConnectNumOfDomainsGeneric(conn, 0);
3194 3195
}

3196
static int
3197
phypConnectListDomains(virConnectPtr conn, int *ids, int nids)
3198
{
3199
    return phypConnectListDomainsGeneric(conn, ids, nids, 0);
3200
}
3201

3202
static int
3203
phypConnectListDefinedDomains(virConnectPtr conn, char **const names, int nnames)
3204
{
3205
    bool success = false;
3206 3207 3208 3209 3210 3211 3212 3213 3214 3215
    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 已提交
3216
    char *char_ptr = NULL;
3217
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3218

3219 3220
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
3221
        virBufferAsprintf(&buf, " -m %s", managed_system);
3222
    virBufferAddLit(&buf, " -F name,state"
E
Eric Blake 已提交
3223
                      "|sed -n '/Not Activated/ {\n s/,.*$//\n p\n}'");
3224
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
3225

3226 3227
    /* I need to parse the textual return in order to get the domains */
    if (exit_status < 0 || ret == NULL)
3228
        goto cleanup;
3229 3230
    else {
        domains = ret;
3231

3232
        while (got < nnames) {
E
Eric Blake 已提交
3233
            char_ptr = strchr(domains, '\n');
3234

E
Eric Blake 已提交
3235 3236
            if (char_ptr) {
                *char_ptr = '\0';
3237
                if ((names[got++] = strdup(domains)) == NULL) {
3238
                    virReportOOMError();
3239
                    goto cleanup;
3240
                }
E
Eric Blake 已提交
3241 3242
                char_ptr++;
                domains = char_ptr;
3243 3244
            } else
                break;
3245
        }
3246 3247
    }

3248 3249
    success = true;

3250
cleanup:
3251 3252 3253 3254 3255 3256
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);

        got = -1;
    }
3257
    VIR_FREE(ret);
3258
    return got;
3259 3260
}

3261 3262
static virDomainPtr
phypDomainLookupByName(virConnectPtr conn, const char *lpar_name)
3263
{
3264 3265 3266 3267 3268 3269 3270
    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];
3271

3272 3273 3274
    lpar_id = phypGetLparID(session, managed_system, lpar_name, conn);
    if (lpar_id == -1)
        return NULL;
3275

3276 3277
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
        return NULL;
3278

3279 3280 3281 3282 3283 3284
    dom = virGetDomain(conn, lpar_name, lpar_uuid);

    if (dom)
        dom->id = lpar_id;

    return dom;
3285 3286
}

3287 3288
static virDomainPtr
phypDomainLookupByID(virConnectPtr conn, int lpar_id)
3289 3290
{
    ConnectionData *connection_data = conn->networkPrivateData;
3291
    phyp_driverPtr phyp_driver = conn->privateData;
3292
    LIBSSH2_SESSION *session = connection_data->session;
3293 3294 3295
    virDomainPtr dom = NULL;
    char *managed_system = phyp_driver->managed_system;
    unsigned char lpar_uuid[VIR_UUID_BUFLEN];
E
Eduardo Otubo 已提交
3296

3297 3298
    char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
                                      conn);
3299

3300
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
3301
        goto cleanup;
3302

3303
    dom = virGetDomain(conn, lpar_name, lpar_uuid);
3304

3305 3306
    if (dom)
        dom->id = lpar_id;
3307

3308
cleanup:
3309
    VIR_FREE(lpar_name);
3310

3311
    return dom;
3312 3313
}

3314
static char *
3315
phypDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
3316
{
3317 3318
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = dom->conn->privateData;
3319
    LIBSSH2_SESSION *session = connection_data->session;
3320 3321
    virDomainDef def;
    char *managed_system = phyp_driver->managed_system;
E
Eduardo Otubo 已提交
3322

3323 3324
    /* Flags checked by virDomainDefFormat */

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

3327 3328 3329 3330 3331 3332 3333
    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) {
3334
        VIR_ERROR(_("Unable to determine domain's name."));
3335
        goto err;
E
Eduardo Otubo 已提交
3336 3337
    }

3338
    if (phypGetLparUUID(def.uuid, dom->id, dom->conn) == -1) {
3339
        VIR_ERROR(_("Unable to generate random uuid."));
E
Eduardo Otubo 已提交
3340 3341
        goto err;
    }
3342

3343
    if ((def.mem.max_balloon =
3344
         phypGetLparMem(dom->conn, managed_system, dom->id, 0)) == 0) {
3345
        VIR_ERROR(_("Unable to determine domain's max memory."));
3346 3347
        goto err;
    }
3348

3349
    if ((def.mem.cur_balloon =
3350
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0) {
3351
        VIR_ERROR(_("Unable to determine domain's memory."));
3352 3353
        goto err;
    }
3354

E
Eric Blake 已提交
3355
    if ((def.maxvcpus = def.vcpus =
3356
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0) {
3357
        VIR_ERROR(_("Unable to determine domain's CPU."));
3358
        goto err;
3359
    }
3360

3361
    return virDomainDefFormat(&def, flags);
3362

3363
err:
3364 3365
    return NULL;
}
3366

3367 3368 3369
static int
phypDomainResume(virDomainPtr dom)
{
3370
    int result = -1;
3371 3372 3373 3374 3375 3376 3377 3378
    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;
3379

3380 3381
    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
3382 3383
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar -o on --id %d -f %s",
3384
                      dom->id, dom->name);
3385
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3386

3387
    if (exit_status < 0)
3388
        goto cleanup;
3389

3390
    result = 0;
3391

3392
cleanup:
3393
    VIR_FREE(ret);
3394 3395

    return result;
3396 3397
}

3398
static int
E
Eric Blake 已提交
3399
phypDomainReboot(virDomainPtr dom, unsigned int flags)
3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411
{
    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 已提交
3412 3413
    virCheckFlags(0, -1);

3414 3415
    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
3416 3417
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432
                      " -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;
}

3433 3434
static int
phypDomainShutdown(virDomainPtr dom)
3435
{
3436
    int result = -1;
3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
    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)
3449 3450
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar -o shutdown --id %d", dom->id);
3451
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3452 3453

    if (exit_status < 0)
3454
        goto cleanup;
3455

3456
    result = 0;
3457

3458
cleanup:
3459
    VIR_FREE(ret);
3460 3461

    return result;
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473
}

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)
3474
        VIR_WARN("Unable to determine domain's max memory.");
3475 3476 3477

    if ((info->memory =
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0)
3478
        VIR_WARN("Unable to determine domain's memory.");
3479 3480 3481

    if ((info->nrVirtCpu =
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
3482
        VIR_WARN("Unable to determine domain's CPU.");
3483 3484 3485 3486

    return 0;
}

3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501
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;
}

3502
static int
3503 3504
phypDomainDestroyFlags(virDomainPtr dom,
                       unsigned int flags)
3505
{
3506
    int result = -1;
3507 3508 3509 3510 3511 3512 3513 3514 3515
    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;

3516 3517
    virCheckFlags(0, -1);

3518 3519
    virBufferAddLit(&buf, "rmsyscfg");
    if (system_type == HMC)
3520 3521
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar --id %d", dom->id);
3522
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3523 3524

    if (exit_status < 0)
3525
        goto cleanup;
3526 3527

    if (phypUUIDTable_RemLpar(dom->conn, dom->id) == -1)
3528
        goto cleanup;
3529

3530
    dom->id = -1;
3531
    result = 0;
3532

3533
cleanup:
3534 3535
    VIR_FREE(ret);

3536
    return result;
3537
}
3538

3539 3540 3541 3542 3543 3544
static int
phypDomainDestroy(virDomainPtr dom)
{
    return phypDomainDestroyFlags(dom, 0);
}

3545 3546
static int
phypBuildLpar(virConnectPtr conn, virDomainDefPtr def)
3547
{
3548
    int result = -1;
3549 3550 3551 3552 3553 3554 3555 3556
    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;
3557

3558
    if (!def->mem.cur_balloon) {
3559 3560 3561
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Field <memory> on the domain XML file is missing or has "
                         "invalid value."));
3562
        goto cleanup;
3563 3564
    }

3565
    if (!def->mem.max_balloon) {
3566 3567 3568
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Field <currentMemory> on the domain XML file is missing or "
                         "has invalid value."));
3569
        goto cleanup;
3570 3571
    }

3572
    if (def->ndisks < 1) {
3573 3574
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Domain XML must contain at least one <disk> element."));
3575
        goto cleanup;
3576 3577 3578
    }

    if (!def->disks[0]->src) {
3579 3580 3581
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Field <src> under <disk> on the domain XML file is "
                         "missing."));
3582
        goto cleanup;
3583 3584
    }

3585 3586
    virBufferAddLit(&buf, "mksyscfg");
    if (system_type == HMC)
3587
        virBufferAsprintf(&buf, " -m %s", managed_system);
3588 3589 3590 3591
    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,
3592
                      (int) def->vcpus, def->disks[0]->src);
3593
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
3594

3595
    if (exit_status < 0) {
3596
        VIR_ERROR(_("Unable to create LPAR. Reason: '%s'"), NULLSTR(ret));
3597
        goto cleanup;
3598
    }
3599

3600
    if (phypUUIDTable_AddLpar(conn, def->uuid, def->id) == -1) {
3601
        VIR_ERROR(_("Unable to add LPAR to the table"));
3602
        goto cleanup;
3603
    }
3604

3605
    result = 0;
3606

3607
cleanup:
3608
    VIR_FREE(ret);
3609 3610

    return result;
3611
}
3612

3613
static virDomainPtr
3614 3615
phypDomainCreateXML(virConnectPtr conn,
                    const char *xml, unsigned int flags)
3616
{
E
Eduardo Otubo 已提交
3617
    virCheckFlags(0, NULL);
3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630

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

3631 3632 3633
    if (!(def = virDomainDefParseString(xml, phyp_driver->caps,
                                        phyp_driver->xmlopt,
                                        1 << VIR_DOMAIN_VIRT_PHYP,
3634 3635 3636 3637
                                        VIR_DOMAIN_XML_SECURE)))
        goto err;

    /* checking if this name already exists on this system */
3638
    if (phypGetLparID(session, managed_system, def->name, conn) != -1) {
3639
        VIR_WARN("LPAR name already exists.");
3640 3641 3642 3643 3644 3645
        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) {
3646
            VIR_WARN("LPAR ID or UUID already exists.");
3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
            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;

3662
err:
3663
    virDomainDefFree(def);
3664
    virObjectUnref(dom);
3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680
    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
3681 3682
phypDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                        unsigned int flags)
3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695
{
    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;

3696
    if (flags != VIR_DOMAIN_VCPU_LIVE) {
3697
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
3698 3699 3700
        return -1;
    }

3701 3702 3703
    if ((ncpus = phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
        return 0;

3704
    if (nvcpus > phypDomainGetMaxVcpus(dom)) {
3705
        VIR_ERROR(_("You are trying to set a number of CPUs bigger than "
3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720
                     "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)
3721 3722
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --id %d -o %c --procunits %d 2>&1 |sed "
3723 3724
                      "-e 's/^.*\\([0-9][0-9]*.[0-9][0-9]*\\).*$/\\1/'",
                      dom->id, operation, amount);
3725
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3726 3727

    if (exit_status < 0) {
3728
        VIR_ERROR(_
3729 3730 3731 3732 3733 3734
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    " Contact your support to enable this feature."));
    }

    VIR_FREE(ret);
    return 0;
3735 3736

}
3737

3738
static int
3739
phypDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
3740 3741 3742 3743
{
    return phypDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_VCPU_LIVE);
}

3744
static virDrvOpenStatus
3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766
phypStorageOpen(virConnectPtr conn,
                virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

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

    return VIR_DRV_OPEN_SUCCESS;
}

static int
phypStorageClose(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 0;
}

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

3770 3771 3772
    if (conn->driver->no != VIR_DRV_PHYP)
        return VIR_DRV_OPEN_DECLINED;

3773 3774 3775 3776
    return VIR_DRV_OPEN_SUCCESS;
}

static int
3777
phypInterfaceClose(virConnectPtr conn ATTRIBUTE_UNUSED)
3778 3779 3780 3781
{
    return 0;
}

3782
static virDriver phypDriver = {
3783 3784
    .no = VIR_DRV_PHYP,
    .name = "PHYP",
3785 3786
    .connectOpen = phypConnectOpen, /* 0.7.0 */
    .connectClose = phypConnectClose, /* 0.7.0 */
3787
    .connectGetCapabilities = phypConnectGetCapabilities, /* 0.7.3 */
3788 3789 3790
    .connectListDomains = phypConnectListDomains, /* 0.7.0 */
    .connectNumOfDomains = phypConnectNumOfDomains, /* 0.7.0 */
    .domainCreateXML = phypDomainCreateXML, /* 0.7.3 */
3791 3792 3793 3794 3795 3796
    .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 */
3797
    .domainDestroyFlags = phypDomainDestroyFlags, /* 0.9.4 */
3798 3799
    .domainGetInfo = phypDomainGetInfo, /* 0.7.0 */
    .domainGetState = phypDomainGetState, /* 0.9.2 */
3800
    .domainSetVcpus = phypDomainSetVcpus, /* 0.7.3 */
3801 3802
    .domainSetVcpusFlags = phypDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = phypDomainGetVcpusFlags, /* 0.8.5 */
3803
    .domainGetMaxVcpus = phypDomainGetMaxVcpus, /* 0.7.3 */
3804
    .domainGetXMLDesc = phypDomainGetXMLDesc, /* 0.7.0 */
3805 3806 3807 3808 3809 3810 3811
    .connectListDefinedDomains = phypConnectListDefinedDomains, /* 0.7.0 */
    .connectNumOfDefinedDomains = phypConnectNumOfDefinedDomains, /* 0.7.0 */
    .domainAttachDevice = phypDomainAttachDevice, /* 0.8.2 */
    .connectIsEncrypted = phypConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = phypConnectIsSecure, /* 0.7.3 */
    .domainIsUpdated = phypDomainIsUpdated, /* 0.8.6 */
    .connectIsAlive = phypConnectIsAlive, /* 0.9.8 */
3812 3813
};

3814 3815
static virStorageDriver phypStorageDriver = {
    .name = "PHYP",
3816 3817
    .storageOpen = phypStorageOpen, /* 0.8.2 */
    .storageClose = phypStorageClose, /* 0.8.2 */
3818

3819 3820
    .connectNumOfStoragePools = phypConnectNumOfStoragePools, /* 0.8.2 */
    .connectListStoragePools = phypConnectListStoragePools, /* 0.8.2 */
3821
    .storagePoolLookupByName = phypStoragePoolLookupByName, /* 0.8.2 */
3822
    .storagePoolLookupByUUID = phypStoragePoolLookupByUUID, /* 0.8.2 */
3823
    .storagePoolCreateXML = phypStoragePoolCreateXML, /* 0.8.2 */
3824 3825
    .storagePoolDestroy = phypStoragePoolDestroy, /* 0.8.2 */
    .storagePoolGetXMLDesc = phypStoragePoolGetXMLDesc, /* 0.8.2 */
3826 3827 3828
    .storagePoolNumOfVolumes = phypStoragePoolNumOfVolumes, /* 0.8.2 */
    .storagePoolListVolumes = phypStoragePoolListVolumes, /* 0.8.2 */

3829 3830
    .storageVolLookupByName = phypStorageVolLookupByName, /* 0.8.2 */
    .storageVolLookupByPath = phypStorageVolLookupByPath, /* 0.8.2 */
3831
    .storageVolCreateXML = phypStorageVolCreateXML, /* 0.8.2 */
3832 3833
    .storageVolGetXMLDesc = phypStorageVolGetXMLDesc, /* 0.8.2 */
    .storageVolGetPath = phypStorageVolGetPath, /* 0.8.2 */
3834 3835
};

E
Eduardo Otubo 已提交
3836
static virInterfaceDriver phypInterfaceDriver = {
3837
    .name = "PHYP",
3838 3839 3840 3841
    .interfaceOpen = phypInterfaceOpen, /* 0.9.1 */
    .interfaceClose = phypInterfaceClose, /* 0.9.1 */
    .connectNumOfInterfaces = phypConnectNumOfInterfaces, /* 0.9.1 */
    .connectListInterfaces = phypConnectListInterfaces, /* 0.9.1 */
3842 3843 3844 3845
    .interfaceLookupByName = phypInterfaceLookupByName, /* 0.9.1 */
    .interfaceDefineXML = phypInterfaceDefineXML, /* 0.9.1 */
    .interfaceDestroy = phypInterfaceDestroy, /* 0.9.1 */
    .interfaceIsActive = phypInterfaceIsActive /* 0.9.1 */
3846 3847
};

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

3858 3859
    return 0;
}