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

#include <config.h>

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

#include "internal.h"
46
#include "virauth.h"
47 48
#include "util.h"
#include "datatypes.h"
49
#include "virbuffer.h"
50
#include "memory.h"
51
#include "virlog.h"
52 53 54 55 56
#include "driver.h"
#include "libvirt/libvirt.h"
#include "virterror_internal.h"
#include "uuid.h"
#include "domain_conf.h"
57
#include "storage_conf.h"
58
#include "nodeinfo.h"
E
Eric Blake 已提交
59
#include "virfile.h"
E
Eduardo Otubo 已提交
60
#include "interface_conf.h"
61 62 63 64 65 66 67 68 69

#include "phyp_driver.h"

#define VIR_FROM_THIS VIR_FROM_PHYP

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

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

75 76 77 78 79 80 81 82 83
static int
waitsocket(int socket_fd, LIBSSH2_SESSION * session)
{
    struct timeval timeout;
    int rc;
    fd_set fd;
    fd_set *writefd = NULL;
    fd_set *readfd = NULL;
    int dir;
84

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

88
    FD_ZERO(&fd);
89

90
    FD_SET(socket_fd, &fd);
91

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

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

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

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

103 104
    return rc;
}
105

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

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

130 131 132 133 134
    /* Exec non-blocking on the remove host */
    while ((channel = libssh2_channel_open_session(session)) == NULL &&
           libssh2_session_last_error(session, NULL, NULL, 0) ==
           LIBSSH2_ERROR_EAGAIN) {
        waitsocket(sock, session);
135 136
    }

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

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

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

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

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

170
    exitcode = 127;
171

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

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

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

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

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

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/* 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 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/* 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;
}

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

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

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

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

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

289

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


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

303 304
    if ((caps = virCapabilitiesNew(virArchFromHost(),
                                   0, 0)) == NULL)
305
        goto no_memory;
306

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

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

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

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

331 332
    caps->defaultConsoleTargetType = phypDefaultConsoleType;

333
    return caps;
334

335
no_memory:
336 337 338
    virCapabilitiesFree(caps);
    return NULL;
}
339

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
/* This is a generic function that won't be used directly by
 * libvirt api. The function returns the number of domains
 * in different states: Running, Not Activated and all:
 *
 * type: 0 - Running
 *       1 - Not Activated
 *       * - All
 * */
static int
phypNumDomainsGeneric(virConnectPtr conn, unsigned int type)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
355
    int ndom = -1;
356 357 358
    char *managed_system = phyp_driver->managed_system;
    const char *state;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
359

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

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

380 381 382 383 384 385 386 387 388 389
/* This is a generic function that won't be used directly by
 * libvirt api. The function returns the ids of domains
 * in different states: Running, and all:
 *
 * type: 0 - Running
 *       1 - all
 * */
static int
phypListDomainsGeneric(virConnectPtr conn, int *ids, int nids,
                       unsigned int type)
390
{
391
    ConnectionData *connection_data = conn->networkPrivateData;
E
Eduardo Otubo 已提交
392
    phyp_driverPtr phyp_driver = conn->privateData;
393
    LIBSSH2_SESSION *session = connection_data->session;
E
Eduardo Otubo 已提交
394
    int system_type = phyp_driver->system_type;
395
    char *managed_system = phyp_driver->managed_system;
396
    int exit_status = 0;
397
    int got = -1;
398
    char *ret = NULL;
399
    char *line, *next_line;
400
    const char *state;
E
Eduardo Otubo 已提交
401 402
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

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

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

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

433
cleanup:
434
    VIR_FREE(ret);
435
    return got;
436 437
}

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

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

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

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

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

472
err:
473
    VIR_FORCE_CLOSE(fd);
474 475 476
    return -1;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

589 590 591 592 593
    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);
        }
594 595
    }

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

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

602
    return 0;
603

604
err:
605
    return -1;
606 607
}

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

614 615 616 617 618
    uuid_table->nlpars++;
    unsigned int i = uuid_table->nlpars;
    i--;

    if (VIR_REALLOC_N(uuid_table->lpars, uuid_table->nlpars) < 0) {
619
        virReportOOMError();
620
        goto err;
621 622
    }

623 624
    if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
        virReportOOMError();
625
        goto err;
626
    }
627

628
    uuid_table->lpars[i]->id = id;
629
    memcpy(uuid_table->lpars[i]->uuid, uuid, VIR_UUID_BUFLEN);
630

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

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

637
    return 0;
638

639
err:
640
    return -1;
641 642
}

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

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

659 660 661
    /* 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++) {
662

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

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

685
    VIR_FORCE_CLOSE(fd);
686
    return 0;
687

688
err:
689
    VIR_FORCE_CLOSE(fd);
690
    return -1;
691 692
}

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

711
    if (conn->uri->user != NULL) {
712
        virBufferAdd(&username, conn->uri->user, -1);
713

714 715 716 717 718 719
        if (virBufferError(&username)) {
            virBufferFreeAndReset(&username);
            virReportOOMError();
            goto err;
        }
    }
720

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

729 730 731
    /* Trying to stat the remote file. */
    do {
        channel = libssh2_scp_recv(session, remote_file, &fileinfo);
732

733 734 735
        if (!channel) {
            if (libssh2_session_last_errno(session) !=
                LIBSSH2_ERROR_EAGAIN) {
E
Eric Blake 已提交
736
                goto err;
737 738 739 740 741
            } else {
                waitsocket(sock, session);
            }
        }
    } while (!channel);
742

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

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

752 753 754
            if ((fileinfo.st_size - got) < amount) {
                amount = fileinfo.st_size - got;
            }
E
Eduardo Otubo 已提交
755

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

762 763 764 765
                got += rc;
                total += rc;
            }
        } while (rc > 0);
766

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

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

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

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

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

816
    if ((nids_numdomains = phypNumDomainsGeneric(conn, 2)) < 0)
E
Eric Blake 已提交
817
        goto cleanup;
818 819

    if (VIR_ALLOC_N(ids, nids_numdomains) < 0) {
820
        virReportOOMError();
E
Eric Blake 已提交
821
        goto cleanup;
822 823
    }

824 825
    if ((nids_listdomains =
         phypListDomainsGeneric(conn, ids, nids_numdomains, 1)) < 0)
E
Eric Blake 已提交
826
        goto cleanup;
827

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

838 839 840
    phyp_driver = conn->privateData;
    uuid_table = phyp_driver->uuid_table;
    uuid_table->nlpars = nids_listdomains;
841

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

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

863
        if (phypUUIDTable_WriteFile(conn) == -1)
E
Eric Blake 已提交
864
            goto cleanup;
865

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

E
Eric Blake 已提交
873
    ret = 0;
874

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

886 887
static void
phypUUIDTable_Free(uuid_tablePtr uuid_table)
888
{
889
    int i;
890

891 892 893 894 895 896 897 898
    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);
899 900
}

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

913
    if (len == 0)
914
        return false;
915

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

925 926
    return false;
}
927

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

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

976 977
    if (userhome == NULL)
        goto err;
E
Eduardo Otubo 已提交
978

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

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

989 990
    if (conn->uri->user != NULL) {
        username = strdup(conn->uri->user);
991

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

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

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

1012 1013 1014 1015
    memset(&hints, 0, sizeof(hints));
    hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
1016

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

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

1036 1037
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Failed to connect to %s"), hostname);
1038 1039
    freeaddrinfo(ai);
    goto err;
1040

1041
connected:
1042

1043
    (*internal_socket) = sock;
1044

1045 1046 1047
    /* Create a session instance */
    session = libssh2_session_init();
    if (!session)
1048 1049
        goto err;

1050 1051
    /* tell libssh2 we want it all done non-blocking */
    libssh2_session_set_blocking(session, 0);
1052

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

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

1067 1068 1069 1070 1071 1072
    while ((rc =
            libssh2_userauth_publickey_fromfile(session, username,
                                                pubkey,
                                                pvtkey,
                                                NULL)) ==
           LIBSSH2_ERROR_EAGAIN) ;
1073

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

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

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

1092 1093 1094 1095
        while ((rc =
                libssh2_userauth_password(session, username,
                                          password)) ==
               LIBSSH2_ERROR_EAGAIN) ;
1096

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

1104 1105
    } else if (rc == LIBSSH2_ERROR_NONE) {
        goto exit;
1106

1107 1108
    } else if (rc == LIBSSH2_ERROR_ALLOC || rc == LIBSSH2_ERROR_SOCKET_SEND
               || rc == LIBSSH2_ERROR_SOCKET_TIMEOUT) {
1109 1110 1111
        goto err;
    }

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

1123
exit:
1124 1125 1126 1127 1128 1129
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
    return session;
1130 1131
}

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

E
Eric Blake 已提交
1144 1145
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

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

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

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

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

1173 1174 1175 1176 1177 1178
    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 已提交
1179

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
        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';

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

    if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) {
1202 1203
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Error while opening SSH session."));
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
        goto failure;
    }

    connection_data->session = session;

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

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

    phyp_driver->uuid_table = uuid_table;
    if ((phyp_driver->caps = phypCapsInit()) == NULL) {
1217
        virReportOOMError();
1218
        goto failure;
1219 1220
    }

1221 1222
    conn->privateData = phyp_driver;
    conn->networkPrivateData = connection_data;
1223

1224 1225
    if ((phyp_driver->system_type = phypGetSystemType(conn)) == -1)
        goto failure;
1226

1227 1228
    if (phypUUIDTable_Init(conn) == -1)
        goto failure;
1229

1230 1231 1232 1233 1234 1235 1236
    if (phyp_driver->system_type == HMC) {
        if ((phyp_driver->vios_id = phypGetVIOSPartitionID(conn)) == -1)
            goto failure;
    }

    return VIR_DRV_OPEN_SUCCESS;

1237
failure:
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    if (phyp_driver != NULL) {
        virCapabilitiesFree(phyp_driver->caps);
        VIR_FREE(phyp_driver->managed_system);
        VIR_FREE(phyp_driver);
    }

    phypUUIDTable_Free(uuid_table);

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

    VIR_FREE(connection_data);

    return VIR_DRV_OPEN_ERROR;
1254 1255 1256
}

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

1263 1264
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1265

1266 1267 1268 1269 1270 1271 1272
    virCapabilitiesFree(phyp_driver->caps);
    phypUUIDTable_Free(phyp_driver->uuid_table);
    VIR_FREE(phyp_driver->managed_system);
    VIR_FREE(phyp_driver);
    VIR_FREE(connection_data);
    return 0;
}
1273 1274


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

1282 1283 1284 1285 1286 1287

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

1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306

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

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


1307 1308 1309 1310 1311
static int
phypIsUpdated(virDomainPtr conn ATTRIBUTE_UNUSED)
{
    return 0;
}
1312 1313

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

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

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

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

1348
    if (exit_status < 0)
1349 1350
        VIR_FREE(ret);
    return ret;
1351 1352
}

1353 1354 1355 1356 1357 1358 1359 1360 1361

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

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

1375
    return -1;
1376 1377
}

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

1394 1395
    if (type != 1 && type != 0)
        return 0;
1396

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

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

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

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

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

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

1445 1446 1447
    return phypGetLparCPUGeneric(dom->conn, managed_system, dom->id, 1);
}

1448 1449 1450 1451 1452 1453 1454
static int
phypGetLparCPUMAX(virDomainPtr dom)
{
    return phypDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                         VIR_DOMAIN_VCPU_MAXIMUM));
}

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

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

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

1493 1494 1495 1496 1497 1498
    if ((remote_slot =
         phypGetRemoteSlot(conn, managed_system, lpar_name)) == -1)
        return NULL;

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

1504
    if (exit_status < 0 || ret == NULL)
1505
        goto cleanup;
1506

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

        backing_device = strdup(char_ptr);

        if (backing_device == NULL) {
            virReportOOMError();
1527
            goto cleanup;
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
        }
    } else {
        backing_device = ret;
        ret = NULL;
    }

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

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

1539
cleanup:
1540
    VIR_FREE(ret);
1541

1542
    return backing_device;
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
}

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

1565
    if (exit_status < 0)
1566 1567
        VIR_FREE(ret);
    return ret;
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
}

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;
1580
    int slot = -1;
1581 1582 1583
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

    virBufferAddLit(&buf, "lssyscfg");

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

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

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

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

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

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

    if (exit_status < 0 || ret == NULL)
1650
        goto cleanup;
1651 1652 1653 1654 1655 1656

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

    if (exit_status < 0 || ret == NULL)
1665
        goto cleanup;
1666 1667 1668 1669 1670 1671

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

    if (exit_status < 0 || ret == NULL)
1680
        goto cleanup;
1681

1682
    result = 0;
1683

1684
cleanup:
1685 1686 1687
    VIR_FREE(profile);
    VIR_FREE(vios_name);
    VIR_FREE(ret);
1688 1689

    return result;
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
}

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

1709
    virBufferAsprintf(&buf, "lsmap -all -field svsa backing -fmt , ");
1710 1711 1712 1713

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

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

1717
    if (exit_status < 0)
1718 1719
        VIR_FREE(ret);
    return ret;
1720 1721 1722 1723 1724 1725
}


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

1750
    domain_name = escape_specialcharacters(domain->name);
1751

1752
    if (domain_name == NULL) {
1753
        goto cleanup;
1754 1755 1756 1757 1758 1759
    }

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

    if (def->os.type == NULL) {
        virReportOOMError();
1760
        goto cleanup;
1761 1762 1763 1764 1765
    }

    dev = virDomainDeviceDefParse(phyp_driver->caps, def, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
    if (!dev) {
1766
        goto cleanup;
1767 1768 1769 1770 1771
    }

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

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

    if (system_type == HMC)
1793
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1794 1795
                          managed_system, vios_id);

1796
    virBufferAsprintf(&buf, "mkvdev -vdev %s -vadapter %s",
1797 1798 1799 1800
                      dev->data.disk->src, scsi_adapter);

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1801
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1802 1803

    if (exit_status < 0 || ret == NULL)
1804
        goto cleanup;
1805 1806

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

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

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

    if (exit_status < 0 || ret == NULL)
1836
        goto cleanup;
1837 1838 1839 1840 1841 1842

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

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

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

1871
    result = 0;
1872

1873
cleanup:
1874
    VIR_FREE(ret);
1875 1876
    virDomainDeviceDefFree(dev);
    virDomainDefFree(def);
1877 1878
    VIR_FREE(vios_name);
    VIR_FREE(scsi_adapter);
1879 1880 1881 1882
    VIR_FREE(profile);
    VIR_FREE(domain_name);

    return result;
1883 1884
}

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

1902
    virBufferAsprintf(&buf, "lslv %s -field lvid", name);
1903 1904 1905 1906

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

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

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

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

1932
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field name", name);
1933 1934 1935 1936

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

1937
    virBufferAsprintf(&buf, "|sed '1d; s/ //g'");
1938
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1939

1940
    if (exit_status < 0)
1941 1942
        VIR_FREE(ret);
    return ret;
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
}

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;
1954
    int sp_size = -1;
1955 1956 1957
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

1961
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field size", name);
1962 1963 1964 1965

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

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

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

    if (system_type == HMC)
1987
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1988 1989
                          managed_system, vios_id);

1990
    virBufferAsprintf(&buf, "mklv -lv %s %s %d", lvname, spname, capacity);
1991 1992 1993

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1994
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1995 1996

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

2001 2002
    key = phypVolumeGetKey(conn, lvname);

2003
cleanup:
2004 2005
    VIR_FREE(ret);

2006
    return key;
2007 2008 2009 2010 2011
}

static virStorageVolPtr
phypVolumeLookupByName(virStoragePoolPtr pool, const char *volname)
{
2012 2013
    char *key;
    virStorageVolPtr vol;
2014

2015
    key = phypVolumeGetKey(pool->conn, volname);
2016

2017
    if (key == NULL)
2018 2019
        return NULL;

2020
    vol = virGetStorageVol(pool->conn, pool->name, volname, key, NULL, NULL);
2021 2022 2023 2024

    VIR_FREE(key);

    return vol;
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
}

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

    virStorageVolDefPtr voldef = NULL;
    virStoragePoolDefPtr spdef = NULL;
    virStorageVolPtr vol = NULL;
2036
    virStorageVolPtr dup_vol = NULL;
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
    char *key = NULL;

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

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

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

    if ((spdef->capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2060
        VIR_ERROR(_("Unable to determine storage pools's size."));
2061 2062 2063
        goto err;
    }

J
Ján Tomko 已提交
2064
    /* Information not available */
2065 2066 2067 2068 2069 2070 2071 2072
    spdef->allocation = 0;
    spdef->available = 0;

    spdef->source.ndevice = 1;

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

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

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

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

    if (voldef->capacity) {
2098
        VIR_ERROR(_("Capacity cannot be empty."));
2099 2100 2101
        goto err;
    }

2102 2103 2104 2105
    key = phypBuildVolume(pool->conn, voldef->name, spdef->name,
                          voldef->capacity);

    if (key == NULL)
2106 2107 2108 2109
        goto err;

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

2113 2114
    VIR_FREE(key);

2115 2116
    return vol;

2117
err:
2118
    VIR_FREE(key);
2119 2120
    virStorageVolDefFree(voldef);
    virStoragePoolDefFree(spdef);
2121
    virObjectUnref(vol);
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
    return NULL;
}

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

    if (system_type == HMC)
2140
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2141 2142
                          managed_system, vios_id);

2143
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field pvname", sp);
2144 2145 2146 2147

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

2148
    virBufferAsprintf(&buf, "|sed 1d");
2149
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2150

2151
    if (exit_status < 0)
2152 2153
        VIR_FREE(ret);
    return ret;
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
}

static virStorageVolPtr
phypVolumeLookupByPath(virConnectPtr conn, const char *volname)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
2166
    char *ret = NULL;
2167 2168
    char *key = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2169
    virStorageVolPtr vol = NULL;
2170 2171

    if (system_type == HMC)
2172
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2173 2174
                          managed_system, vios_id);

2175
    virBufferAsprintf(&buf, "lslv %s -field vgname", volname);
2176 2177 2178 2179

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

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

2183
    if (exit_status < 0 || ret == NULL)
2184
        goto cleanup;
2185

2186
    key = phypVolumeGetKey(conn, volname);
2187

2188
    if (key == NULL)
2189
        goto cleanup;
2190

2191
    vol = virGetStorageVol(conn, ret, volname, key, NULL, NULL);
2192

2193
cleanup:
2194
    VIR_FREE(ret);
2195 2196 2197
    VIR_FREE(key);

    return vol;
2198 2199 2200 2201 2202 2203
}

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

2219
    virBufferAsprintf(&buf, "lsdev -dev %s -attr vgserial_id", name);
2220 2221 2222 2223

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

2224
    virBufferAsprintf(&buf, "|sed '1,2d'");
2225
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2226 2227

    if (exit_status < 0 || ret == NULL)
2228
        goto cleanup;
2229

2230
    if (memcpy(uuid, ret, VIR_UUID_BUFLEN) == NULL)
2231
        goto cleanup;
2232

2233
    result = 0;
2234

2235
cleanup:
2236
    VIR_FREE(ret);
2237 2238

    return result;
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
}

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

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

2249
    return virGetStoragePool(conn, name, uuid, NULL, NULL);
2250 2251 2252 2253 2254
}

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

2260 2261 2262
    virCheckFlags(0, NULL);

    memset(&voldef, 0, sizeof(virStorageVolDef));
2263
    memset(&pool, 0, sizeof(virStoragePoolDef));
2264

2265
    sp = phypStoragePoolLookupByName(vol->conn, vol->pool);
2266 2267

    if (!sp)
2268
        goto cleanup;
2269 2270 2271 2272

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

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

    if ((pool.capacity = phypGetStoragePoolSize(sp->conn, sp->name)) == -1) {
2283
        VIR_ERROR(_("Unable to determine storage sps's size."));
2284
        goto cleanup;
2285 2286
    }

J
Ján Tomko 已提交
2287
    /* Information not available */
2288 2289 2290 2291 2292 2293 2294
    pool.allocation = 0;
    pool.available = 0;

    pool.source.ndevice = 1;

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

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

2306 2307 2308 2309
    voldef.key = strdup(vol->key);

    if (voldef.key == NULL) {
        virReportOOMError();
2310
        goto cleanup;
2311 2312 2313 2314
    }

    voldef.type = VIR_STORAGE_POOL_LOGICAL;

2315 2316 2317 2318
    xml = virStorageVolDefFormat(&pool, &voldef);

    VIR_FREE(voldef.key);

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

/* The Volume Group path here will be treated as suggested in the
 * email on the libvirt mailling list. As soon as I can't get the
 * path for every volume, the path will be a representation in
 * the form:
 *
 * /physical_volume/storage_pool/logical_volume
 *
 * */
static char *
phypVolumeGetPath(virStorageVolPtr vol)
{
    virConnectPtr conn = vol->conn;
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
2343
    char *ret = NULL;
2344 2345
    char *path = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2346
    char *pv;
2347 2348

    if (system_type == HMC)
2349
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2350 2351
                          managed_system, vios_id);

2352
    virBufferAsprintf(&buf, "lslv %s -field vgname", vol->name);
2353 2354 2355 2356

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

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

2361
    if (exit_status < 0 || ret == NULL)
2362
        goto cleanup;
2363

2364
    pv = phypVolumeGetPhysicalVolumeByStoragePool(vol, ret);
2365

2366 2367
    if (!pv)
        goto cleanup;
2368

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

2374
cleanup:
2375
    VIR_FREE(ret);
2376
    VIR_FREE(path);
2377 2378

    return path;
2379 2380 2381 2382 2383 2384
}

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

    if (system_type == HMC)
2402
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2403 2404
                          managed_system, vios_id);

2405
    virBufferAsprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
2406 2407 2408 2409

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

2410
    virBufferAsprintf(&buf, "|sed '1,2d'");
2411
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2412 2413 2414

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

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

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

2435 2436
    success = true;

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

        got = -1;
    }
2444
    VIR_FREE(ret);
2445
    return got;
2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
}

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

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

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

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

2494
    virBufferAsprintf(&buf, "rmsp %s", pool->name);
2495 2496 2497

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2498
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2499 2500

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

2505
    result = 0;
2506

2507
cleanup:
2508
    VIR_FREE(ret);
2509 2510

    return result;
2511 2512 2513 2514 2515
}

static int
phypBuildStoragePool(virConnectPtr conn, virStoragePoolDefPtr def)
{
2516
    int result = -1;
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    virStoragePoolSource source = def->source;
    int vios_id = phyp_driver->vios_id;
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2529
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2530 2531
                          managed_system, vios_id);

2532
    virBufferAsprintf(&buf, "mksp -f %schild %s", def->name,
2533 2534 2535 2536
                      source.adapter);

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2537
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2538 2539

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

2544
    result = 0;
2545

2546
cleanup:
2547
    VIR_FREE(ret);
2548 2549

    return result;
2550 2551 2552 2553 2554 2555 2556 2557 2558 2559

}

static int
phypNumOfStoragePools(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
2560
    int nsp = -1;
2561 2562 2563 2564 2565
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2566
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2567 2568
                          managed_system, vios_id);

2569
    virBufferAsprintf(&buf, "lsvg");
2570 2571 2572 2573

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

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

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

    if (system_type == HMC)
2598
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2599 2600
                          managed_system, vios_id);

2601
    virBufferAsprintf(&buf, "lsvg");
2602 2603 2604

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2605
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2606 2607 2608

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

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

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

2629 2630
    success = true;

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

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

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

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

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

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

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

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

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

        if (!memcmp(local_uuid, uuid, VIR_UUID_BUFLEN)) {
2683
            sp = virGetStoragePool(conn, pools[i], uuid, NULL, NULL);
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693
            VIR_FREE(local_uuid);
            VIR_FREE(pools);

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

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

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

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

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

2727
    if ((sp = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL)) == NULL)
2728 2729 2730 2731 2732 2733 2734
        goto err;

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

    return sp;

2735
err:
2736
    virStoragePoolDefFree(def);
2737
    virObjectUnref(sp);
2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751
    return NULL;
}

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

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

    if (pool->name != NULL)
        def.name = pool->name;
    else {
2752
        VIR_ERROR(_("Unable to determine storage pool's name."));
2753 2754 2755
        goto err;
    }

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

    if ((def.capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2763
        VIR_ERROR(_("Unable to determine storage pools's size."));
2764 2765 2766
        goto err;
    }

J
Ján Tomko 已提交
2767
    /* Information not available */
2768 2769 2770 2771 2772 2773 2774 2775
    def.allocation = 0;
    def.available = 0;

    def.source.ndevice = 1;

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

    return virStoragePoolDefFormat(&def);

2782
err:
2783
    return NULL;
2784 2785
}

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

    /* Getting the remote slot number */

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

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

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

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

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

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

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

E
Eric Blake 已提交
2843
    rv = 0;
E
Eduardo Otubo 已提交
2844

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

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 已提交
2868
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2869 2870

    if (!(def = virInterfaceDefParseString(xml)))
E
Eric Blake 已提交
2871
        goto cleanup;
E
Eduardo Otubo 已提交
2872 2873 2874 2875

    /* Now need to get the next free slot number */
    virBufferAddLit(&buf, "lshwres ");
    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 slot --level slot"
                      " -Fslot_num --filter lpar_names=%s"
                      " |sort|tail -n 1", def->name);
E
Eric Blake 已提交
2882
    if (phypExecInt(session, &buf, conn, &slot) < 0)
E
Eric Blake 已提交
2883
        goto cleanup;
E
Eduardo Otubo 已提交
2884 2885 2886 2887 2888 2889 2890

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

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

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

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

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

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

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

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

    memcpy(name, ret, PHYP_IFACENAME_SIZE-1);

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

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

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
2949
        goto cleanup;
E
Eduardo Otubo 已提交
2950 2951 2952

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

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

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];
2975
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2976 2977 2978 2979

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

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

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

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

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

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

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3013
        goto cleanup;
E
Eduardo Otubo 已提交
3014 3015 3016

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

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

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

3039
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
3040 3041 3042
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,state |"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
3043
    phypExecInt(session, &buf, iface->conn, &state);
E
Eduardo Otubo 已提交
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060
    return state;
}

static int
phypListInterfaces(virConnectPtr conn, char **const names, int nnames)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    int exit_status = 0;
    int got = 0;
    int i;
    char *ret = NULL;
    char *networks = NULL;
E
Eric Blake 已提交
3061
    char *char_ptr = NULL;
E
Eduardo Otubo 已提交
3062
    virBuffer buf = VIR_BUFFER_INITIALIZER;
E
Eric Blake 已提交
3063
    bool success = false;
E
Eduardo Otubo 已提交
3064 3065 3066

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

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

    networks = ret;

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

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

E
Eric Blake 已提交
3096 3097 3098 3099 3100
cleanup:
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);
    }
E
Eduardo Otubo 已提交
3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
    VIR_FREE(ret);
    return got;
}

static int
phypNumOfInterfaces(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int system_type = phyp_driver->system_type;
    int vios_id = phyp_driver->vios_id;
E
Eric Blake 已提交
3114
    int nnets = -1;
E
Eduardo Otubo 已提交
3115 3116 3117 3118
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

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

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

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

3147 3148
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3149

3150 3151 3152 3153 3154 3155
    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;
3156

3157
cleanup:
3158 3159
    VIR_FREE(ret);
    return state;
3160 3161
}

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

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

3186 3187
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3188

3189 3190 3191 3192
    if (STREQ(ret, "LVPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_BLOCK;
    else if (STREQ(ret, "FBPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_FILE;
3193

3194
cleanup:
3195 3196 3197
    VIR_FREE(ret);
    return disk_type;
}
3198

3199 3200 3201 3202 3203
static int
phypNumDefinedDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 1);
}
3204

3205 3206 3207 3208
static int
phypNumDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 0);
3209 3210
}

3211 3212
static int
phypListDomains(virConnectPtr conn, int *ids, int nids)
3213
{
3214 3215
    return phypListDomainsGeneric(conn, ids, nids, 0);
}
3216

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

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

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

3247
        while (got < nnames) {
E
Eric Blake 已提交
3248
            char_ptr = strchr(domains, '\n');
3249

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

3263 3264
    success = true;

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

        got = -1;
    }
3272
    VIR_FREE(ret);
3273
    return got;
3274 3275
}

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

3287 3288 3289
    lpar_id = phypGetLparID(session, managed_system, lpar_name, conn);
    if (lpar_id == -1)
        return NULL;
3290

3291 3292
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
        return NULL;
3293

3294 3295 3296 3297 3298 3299
    dom = virGetDomain(conn, lpar_name, lpar_uuid);

    if (dom)
        dom->id = lpar_id;

    return dom;
3300 3301
}

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

3313 3314
    char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
                                      conn);
3315

3316
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
3317
        goto cleanup;
3318

3319
    if (exit_status < 0)
3320
        goto cleanup;
3321

3322
    dom = virGetDomain(conn, lpar_name, lpar_uuid);
3323

3324 3325
    if (dom)
        dom->id = lpar_id;
3326

3327
cleanup:
3328
    VIR_FREE(lpar_name);
3329

3330
    return dom;
3331 3332
}

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

3342 3343
    /* Flags checked by virDomainDefFormat */

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

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

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

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

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

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

3380
    return virDomainDefFormat(&def, flags);
3381

3382
err:
3383 3384
    return NULL;
}
3385

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

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

3406
    if (exit_status < 0)
3407
        goto cleanup;
3408

3409
    result = 0;
3410

3411
cleanup:
3412
    VIR_FREE(ret);
3413 3414

    return result;
3415 3416
}

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

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

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

    if (exit_status < 0)
3473
        goto cleanup;
3474

3475
    result = 0;
3476

3477
cleanup:
3478
    VIR_FREE(ret);
3479 3480

    return result;
3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492
}

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)
3493
        VIR_WARN("Unable to determine domain's max memory.");
3494 3495 3496

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

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

    return 0;
}

3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520
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;
}

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

3535 3536
    virCheckFlags(0, -1);

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

    if (exit_status < 0)
3544
        goto cleanup;
3545 3546

    if (phypUUIDTable_RemLpar(dom->conn, dom->id) == -1)
3547
        goto cleanup;
3548

3549
    dom->id = -1;
3550
    result = 0;
3551

3552
cleanup:
3553 3554
    VIR_FREE(ret);

3555
    return result;
3556
}
3557

3558 3559 3560 3561 3562 3563
static int
phypDomainDestroy(virDomainPtr dom)
{
    return phypDomainDestroyFlags(dom, 0);
}

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

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

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

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

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

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

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

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

3624
    result = 0;
3625

3626
cleanup:
3627
    VIR_FREE(ret);
3628 3629

    return result;
3630
}
3631

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

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

    virCheckFlags(0, NULL);

    if (!(def = virDomainDefParseString(phyp_driver->caps, xml,
M
Matthias Bolte 已提交
3651
                                        1 << VIR_DOMAIN_VIRT_PHYP,
3652 3653 3654 3655
                                        VIR_DOMAIN_XML_SECURE)))
        goto err;

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

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

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

3719 3720 3721 3722
    if ((ncpus = phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
        return 0;

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

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

    VIR_FREE(ret);
    return 0;
3753 3754

}
3755

3756 3757 3758 3759 3760 3761
static int
phypDomainSetCPU(virDomainPtr dom, unsigned int nvcpus)
{
    return phypDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_VCPU_LIVE);
}

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

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

3772 3773 3774 3775
    return VIR_DRV_OPEN_SUCCESS;
}

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

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

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

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

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

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

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

3857 3858
    return 0;
}