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

#include <config.h>

#include <sys/types.h>
28
#include <sys/stat.h>
29 30 31 32 33 34 35 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
#include "virutil.h"
48
#include "datatypes.h"
49
#include "virbuffer.h"
50
#include "viralloc.h"
51
#include "virlog.h"
52 53
#include "driver.h"
#include "libvirt/libvirt.h"
54
#include "virerror.h"
55
#include "viruuid.h"
56
#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
static int
waitsocket(int socket_fd, LIBSSH2_SESSION * session)
{
    struct timeval timeout;
    fd_set fd;
    fd_set *writefd = NULL;
    fd_set *readfd = NULL;
    int dir;
83

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

87
    FD_ZERO(&fd);
88

89
    FD_SET(socket_fd, &fd);
90

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

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

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

100
    return select(socket_fd + 1, readfd, writefd, NULL, &timeout);
101
}
102

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

122 123 124 125 126
    if (VIR_ALLOC_N(buffer, buffer_size) < 0) {
        virReportOOMError();
        return NULL;
    }

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

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

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

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

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

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

179
    exitcode = 127;
180

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

189 190
    if (rc == 0) {
        exitcode = libssh2_channel_get_exit_status(channel);
191 192
    }

193 194 195
    (*exit_status) = exitcode;
    libssh2_channel_free(channel);
    channel = NULL;
196 197
    VIR_FREE(buffer);

198 199 200 201 202 203
    if (virBufferError(&tex_ret)) {
        virBufferFreeAndReset(&tex_ret);
        virReportOOMError();
        return NULL;
    }
    return virBufferContentAndReset(&tex_ret);
204 205 206 207 208 209

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

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

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

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

277 278 279
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return exit_status;
280 281
}

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

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

302

303
static int phypDefaultConsoleType(const char *ostype ATTRIBUTE_UNUSED,
304
                                  virArch arch ATTRIBUTE_UNUSED)
305 306 307 308 309
{
    return VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
}


310 311 312 313 314
static virCapsPtr
phypCapsInit(void)
{
    virCapsPtr caps;
    virCapsGuestPtr guest;
315

316 317
    if ((caps = virCapabilitiesNew(virArchFromHost(),
                                   0, 0)) == NULL)
318
        goto no_memory;
319

320 321 322 323 324 325
    /* 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);
326
        VIR_WARN
327
            ("Failed to query host NUMA topology, disabling NUMA capabilities");
328 329
    }

330 331 332
    /* XXX shouldn't 'borrow' KVM's prefix */
    virCapabilitiesSetMacPrefix(caps, (unsigned char[]) {
                                0x52, 0x54, 0x00});
333

334 335
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "linux",
336
                                         caps->host.arch,
337 338
                                         NULL, NULL, 0, NULL)) == NULL)
        goto no_memory;
339

340 341 342
    if (virCapabilitiesAddGuestDomain(guest,
                                      "phyp", NULL, NULL, 0, NULL) == NULL)
        goto no_memory;
343

344 345
    caps->defaultConsoleTargetType = phypDefaultConsoleType;

346
    return caps;
347

348
no_memory:
349 350 351
    virCapabilitiesFree(caps);
    return NULL;
}
352

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
/* 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;
368
    int ndom = -1;
369 370 371
    char *managed_system = phyp_driver->managed_system;
    const char *state;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
372

373 374 375 376 377 378 379
    if (type == 0)
        state = "|grep Running";
    else if (type == 1) {
        if (system_type == HMC) {
            state = "|grep \"Not Activated\"";
        } else {
            state = "|grep \"Open Firmware\"";
380
        }
381 382
    } else
        state = " ";
383

384 385
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
386 387
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F lpar_id,state %s |grep -c '^[0-9][0-9]*'",
388
                      state);
E
Eric Blake 已提交
389
    phypExecInt(session, &buf, conn, &ndom);
390
    return ndom;
391 392
}

393 394 395 396 397 398 399 400 401 402
/* 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)
403
{
404
    ConnectionData *connection_data = conn->networkPrivateData;
E
Eduardo Otubo 已提交
405
    phyp_driverPtr phyp_driver = conn->privateData;
406
    LIBSSH2_SESSION *session = connection_data->session;
E
Eduardo Otubo 已提交
407
    int system_type = phyp_driver->system_type;
408
    char *managed_system = phyp_driver->managed_system;
409
    int exit_status = 0;
410
    int got = -1;
411
    char *ret = NULL;
412
    char *line, *next_line;
413
    const char *state;
E
Eduardo Otubo 已提交
414 415
    virBuffer buf = VIR_BUFFER_INITIALIZER;

416 417 418 419 420
    if (type == 0)
        state = "|grep Running";
    else
        state = " ";

E
Eduardo Otubo 已提交
421 422
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
423 424
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F lpar_id,state %s | sed -e 's/,.*$//'",
425
                      state);
426
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
427

428
    if (exit_status < 0 || ret == NULL)
429
        goto cleanup;
430 431 432 433 434 435 436 437

    /* 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;
438
            goto cleanup;
439
        }
440 441 442 443
        got++;
        line = next_line;
        while (*line == '\n')
            line++; /* skip \n */
444
    }
445

446
cleanup:
447
    VIR_FREE(ret);
448
    return got;
449 450
}

451 452
static int
phypUUIDTable_WriteFile(virConnectPtr conn)
453
{
454 455
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
456
    unsigned int i = 0;
457 458 459 460 461
    int fd = -1;
    char local_file[] = "./uuid_table";

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

463
    for (i = 0; i < uuid_table->nlpars; i++) {
464 465 466
        if (safewrite(fd, &uuid_table->lpars[i]->id,
                      sizeof(uuid_table->lpars[i]->id)) !=
            sizeof(uuid_table->lpars[i]->id)) {
467
            VIR_ERROR(_("Unable to write information to local file."));
468 469 470 471 472
            goto err;
        }

        if (safewrite(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN) !=
            VIR_UUID_BUFLEN) {
473
            VIR_ERROR(_("Unable to write information to local file."));
474
            goto err;
475 476 477
        }
    }

478 479 480 481 482
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno, _("Could not close %s"),
                             local_file);
        goto err;
    }
483 484
    return 0;

485
err:
486
    VIR_FORCE_CLOSE(fd);
487 488 489
    return -1;
}

490 491
static int
phypUUIDTable_Push(virConnectPtr conn)
492 493
{
    ConnectionData *connection_data = conn->networkPrivateData;
494
    LIBSSH2_SESSION *session = connection_data->session;
495 496 497 498
    LIBSSH2_CHANNEL *channel = NULL;
    struct stat local_fileinfo;
    char buffer[1024];
    int rc = 0;
499
    FILE *f = NULL;
500 501 502 503
    size_t nread, sent;
    char *ptr;
    char local_file[] = "./uuid_table";
    char *remote_file = NULL;
504
    int ret = -1;
505

506 507
    if (virAsprintf(&remote_file, "/home/%s/libvirt_uuid_table",
                    NULLSTR(conn->uri->user)) < 0) {
E
Eduardo Otubo 已提交
508
        virReportOOMError();
509
        goto cleanup;
510 511
    }

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

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

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

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

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

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

561
    ret = 0;
562

563
cleanup:
564 565 566 567 568 569 570
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
571 572
    VIR_FORCE_FCLOSE(f);
    return ret;
573 574 575
}

static int
576
phypUUIDTable_RemLpar(virConnectPtr conn, int id)
577
{
E
Eduardo Otubo 已提交
578
    phyp_driverPtr phyp_driver = conn->privateData;
579 580
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    unsigned int i = 0;
E
Eduardo Otubo 已提交
581

582 583 584 585 586
    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);
        }
587 588
    }

589
    if (phypUUIDTable_WriteFile(conn) == -1)
590 591
        goto err;

592
    if (phypUUIDTable_Push(conn) == -1)
593 594
        goto err;

595
    return 0;
596

597
err:
598
    return -1;
599 600
}

601 602
static int
phypUUIDTable_AddLpar(virConnectPtr conn, unsigned char *uuid, int id)
603
{
E
Eduardo Otubo 已提交
604
    phyp_driverPtr phyp_driver = conn->privateData;
605
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
E
Eduardo Otubo 已提交
606

607 608 609 610 611
    uuid_table->nlpars++;
    unsigned int i = uuid_table->nlpars;
    i--;

    if (VIR_REALLOC_N(uuid_table->lpars, uuid_table->nlpars) < 0) {
612
        virReportOOMError();
613
        goto err;
614 615
    }

616 617
    if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
        virReportOOMError();
618
        goto err;
619
    }
620

621
    uuid_table->lpars[i]->id = id;
622
    memcpy(uuid_table->lpars[i]->uuid, uuid, VIR_UUID_BUFLEN);
623

624 625
    if (phypUUIDTable_WriteFile(conn) == -1)
        goto err;
626

627
    if (phypUUIDTable_Push(conn) == -1)
628 629
        goto err;

630
    return 0;
631

632
err:
633
    return -1;
634 635
}

636 637
static int
phypUUIDTable_ReadFile(virConnectPtr conn)
638
{
E
Eduardo Otubo 已提交
639
    phyp_driverPtr phyp_driver = conn->privateData;
640 641 642 643 644 645
    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;
646

647
    if ((fd = open(local_file, O_RDONLY)) == -1) {
648
        VIR_WARN("Unable to read information from local file.");
649
        goto err;
650 651
    }

652 653 654
    /* 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++) {
655

656 657 658 659 660 661 662 663
            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 {
664
                VIR_WARN
665
                    ("Unable to read from information from local file.");
666 667
                goto err;
            }
668

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

678
    VIR_FORCE_CLOSE(fd);
679
    return 0;
680

681
err:
682
    VIR_FORCE_CLOSE(fd);
683
    return -1;
684 685
}

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

704 705
    if (virAsprintf(&remote_file, "/home/%s/libvirt_uuid_table",
                    NULLSTR(conn->uri->user)) < 0) {
706
        virReportOOMError();
707
        goto cleanup;
708
    }
709

710 711 712
    /* Trying to stat the remote file. */
    do {
        channel = libssh2_scp_recv(session, remote_file, &fileinfo);
713

714 715 716
        if (!channel) {
            if (libssh2_session_last_errno(session) !=
                LIBSSH2_ERROR_EAGAIN) {
717
                goto cleanup;
718
            } else {
719 720 721
                if (waitsocket(sock, session) < 0 && errno != EINTR) {
                    virReportSystemError(errno, "%s",
                                         _("unable to wait on libssh2 socket"));
722
                    goto cleanup;
723
                }
724 725 726
            }
        }
    } while (!channel);
727

728 729
    /* Creating a new data base based on remote file */
    if ((fd = creat(local_file, 0755)) == -1)
730
        goto cleanup;
731

732 733 734 735
    /* Request a file via SCP */
    while (got < fileinfo.st_size) {
        do {
            amount = sizeof(buffer);
736

737 738 739
            if ((fileinfo.st_size - got) < amount) {
                amount = fileinfo.st_size - got;
            }
E
Eduardo Otubo 已提交
740

741 742 743
            rc = libssh2_channel_read(channel, buffer, amount);
            if (rc > 0) {
                if (safewrite(fd, buffer, rc) != rc)
744
                    VIR_WARN
745
                        ("Unable to write information to local file.");
746

747 748 749 750
                got += rc;
                total += rc;
            }
        } while (rc > 0);
751

752 753 754 755
        if ((rc == LIBSSH2_ERROR_EAGAIN)
            && (got < fileinfo.st_size)) {
            /* this is due to blocking that would occur otherwise
             * so we loop on this condition */
756

757 758 759 760
            /* now we wait */
            if (waitsocket(sock, session) < 0 && errno != EINTR) {
                virReportSystemError(errno, "%s",
                                     _("unable to wait on libssh2 socket"));
761
                goto cleanup;
762
            }
763 764 765 766
            continue;
        }
        break;
    }
767 768 769
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno, _("Could not close %s"),
                             local_file);
770
        goto cleanup;
771
    }
772

773
    ret = 0;
774

775
cleanup:
776 777 778 779 780 781 782
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
783 784
    VIR_FORCE_CLOSE(fd);
    return ret;
785 786
}

787 788
static int
phypUUIDTable_Init(virConnectPtr conn)
789
{
E
Eric Blake 已提交
790
    uuid_tablePtr uuid_table = NULL;
791 792 793 794 795
    phyp_driverPtr phyp_driver;
    int nids_numdomains = 0;
    int nids_listdomains = 0;
    int *ids = NULL;
    unsigned int i = 0;
E
Eric Blake 已提交
796 797
    int ret = -1;
    bool table_created = false;
E
Eduardo Otubo 已提交
798

799
    if ((nids_numdomains = phypNumDomainsGeneric(conn, 2)) < 0)
E
Eric Blake 已提交
800
        goto cleanup;
801 802

    if (VIR_ALLOC_N(ids, nids_numdomains) < 0) {
803
        virReportOOMError();
E
Eric Blake 已提交
804
        goto cleanup;
805 806
    }

807 808
    if ((nids_listdomains =
         phypListDomainsGeneric(conn, ids, nids_numdomains, 1)) < 0)
E
Eric Blake 已提交
809
        goto cleanup;
810

811
    /* exit early if there are no domains */
E
Eric Blake 已提交
812 813 814 815 816
    if (nids_numdomains == 0 && nids_listdomains == 0) {
        ret = 0;
        goto cleanup;
    }
    if (nids_numdomains != nids_listdomains) {
817
        VIR_ERROR(_("Unable to determine number of domains."));
E
Eric Blake 已提交
818
        goto cleanup;
819
    }
820

821 822 823
    phyp_driver = conn->privateData;
    uuid_table = phyp_driver->uuid_table;
    uuid_table->nlpars = nids_listdomains;
824

825 826 827
    /* 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 已提交
828
        table_created = true;
829 830 831 832
        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 已提交
833
                    goto cleanup;
834 835
                }
                uuid_table->lpars[i]->id = ids[i];
836

837 838 839 840
                if (virUUIDGenerate(uuid_table->lpars[i]->uuid) < 0)
                    VIR_WARN("Unable to generate UUID for domain %d",
                             ids[i]);
            }
E
Eduardo Otubo 已提交
841
        } else {
842
            virReportOOMError();
E
Eric Blake 已提交
843
            goto cleanup;
E
Eduardo Otubo 已提交
844
        }
845

846
        if (phypUUIDTable_WriteFile(conn) == -1)
E
Eric Blake 已提交
847
            goto cleanup;
848

849
        if (phypUUIDTable_Push(conn) == -1)
E
Eric Blake 已提交
850
            goto cleanup;
851 852
    } else {
        if (phypUUIDTable_ReadFile(conn) == -1)
E
Eric Blake 已提交
853
            goto cleanup;
854
    }
855

E
Eric Blake 已提交
856
    ret = 0;
857

E
Eric Blake 已提交
858 859 860 861 862 863 864
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);
    }
865
    VIR_FREE(ids);
E
Eric Blake 已提交
866
    return ret;
867 868
}

869 870
static void
phypUUIDTable_Free(uuid_tablePtr uuid_table)
871
{
872
    int i;
873

874 875 876 877 878 879 880 881
    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);
882 883
}

884 885 886 887 888 889 890 891
#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)
892
{
893
    size_t len = strlen(src);
894 895
    size_t i = 0;

896
    if (len == 0)
897
        return false;
898

899 900
    for (i = 0; i < len; i++) {
        switch (src[i]) {
901 902 903 904
        SPECIALCHARACTER_CASES
            return true;
        default:
            continue;
905 906 907
        }
    }

908 909
    return false;
}
910

911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
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;
939 940
}

941 942 943
static LIBSSH2_SESSION *
openSSHSession(virConnectPtr conn, virConnectAuthPtr auth,
               int *internal_socket)
944
{
945 946 947 948
    LIBSSH2_SESSION *session;
    const char *hostname = conn->uri->server;
    char *username = NULL;
    char *password = NULL;
949
    int sock = -1;
950 951 952 953 954 955
    int rc;
    struct addrinfo *ai = NULL, *cur;
    struct addrinfo hints;
    int ret;
    char *pubkey = NULL;
    char *pvtkey = NULL;
956
    char *userhome = virGetUserDirectory();
957
    struct stat pvt_stat, pub_stat;
958

959 960
    if (userhome == NULL)
        goto err;
E
Eduardo Otubo 已提交
961

962
    if (virAsprintf(&pubkey, "%s/.ssh/id_rsa.pub", userhome) < 0) {
963
        virReportOOMError();
964
        goto err;
965 966
    }

967 968
    if (virAsprintf(&pvtkey, "%s/.ssh/id_rsa", userhome) < 0) {
        virReportOOMError();
969 970 971
        goto err;
    }

972 973
    if (conn->uri->user != NULL) {
        username = strdup(conn->uri->user);
974

975 976 977 978 979 980
        if (username == NULL) {
            virReportOOMError();
            goto err;
        }
    } else {
        if (auth == NULL || auth->cb == NULL) {
981 982
            virReportError(VIR_ERR_AUTH_FAILED,
                           "%s", _("No authentication callback provided."));
983 984
            goto err;
        }
985

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

988
        if (username == NULL) {
989 990
            virReportError(VIR_ERR_AUTH_FAILED, "%s",
                           _("Username request failed"));
991 992 993
            goto err;
        }
    }
994

995 996 997 998
    memset(&hints, 0, sizeof(hints));
    hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
999

1000 1001
    ret = getaddrinfo(hostname, "22", &hints, &ai);
    if (ret != 0) {
1002 1003
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Error while getting %s address info"), hostname);
1004 1005
        goto err;
    }
1006

1007 1008 1009 1010 1011 1012 1013
    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;
            }
1014
            VIR_FORCE_CLOSE(sock);
1015 1016 1017
        }
        cur = cur->ai_next;
    }
1018

1019 1020
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Failed to connect to %s"), hostname);
1021 1022
    freeaddrinfo(ai);
    goto err;
1023

1024
connected:
1025

1026
    (*internal_socket) = sock;
1027

1028 1029 1030
    /* Create a session instance */
    session = libssh2_session_init();
    if (!session)
1031 1032
        goto err;

1033 1034
    /* tell libssh2 we want it all done non-blocking */
    libssh2_session_set_blocking(session, 0);
1035

1036 1037 1038
    while ((rc = libssh2_session_startup(session, sock)) ==
           LIBSSH2_ERROR_EAGAIN) ;
    if (rc) {
1039 1040
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Failure establishing SSH session."));
1041 1042
        goto disconnect;
    }
1043

1044 1045 1046 1047 1048
    /* Trying authentication by pubkey */
    if (stat(pvtkey, &pvt_stat) || stat(pubkey, &pub_stat)) {
        rc = LIBSSH2_ERROR_SOCKET_NONE;
        goto keyboard_interactive;
    }
1049

1050 1051 1052 1053 1054 1055
    while ((rc =
            libssh2_userauth_publickey_fromfile(session, username,
                                                pubkey,
                                                pvtkey,
                                                NULL)) ==
           LIBSSH2_ERROR_EAGAIN) ;
1056

1057
keyboard_interactive:
1058 1059 1060 1061
    if (rc == LIBSSH2_ERROR_SOCKET_NONE
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED) {
        if (auth == NULL || auth->cb == NULL) {
1062 1063
            virReportError(VIR_ERR_AUTH_FAILED,
                           "%s", _("No authentication callback provided."));
1064 1065
            goto disconnect;
        }
1066

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

1069
        if (password == NULL) {
1070 1071
            virReportError(VIR_ERR_AUTH_FAILED, "%s",
                           _("Password request failed"));
1072 1073
            goto disconnect;
        }
1074

1075 1076 1077 1078
        while ((rc =
                libssh2_userauth_password(session, username,
                                          password)) ==
               LIBSSH2_ERROR_EAGAIN) ;
1079

1080
        if (rc) {
1081 1082
            virReportError(VIR_ERR_AUTH_FAILED,
                           "%s", _("Authentication failed"));
1083 1084 1085
            goto disconnect;
        } else
            goto exit;
1086

1087 1088
    } else if (rc == LIBSSH2_ERROR_NONE) {
        goto exit;
1089

1090 1091
    } else if (rc == LIBSSH2_ERROR_ALLOC || rc == LIBSSH2_ERROR_SOCKET_SEND
               || rc == LIBSSH2_ERROR_SOCKET_TIMEOUT) {
1092 1093 1094
        goto err;
    }

1095
disconnect:
1096 1097
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1098
err:
1099
    VIR_FORCE_CLOSE(sock);
1100 1101 1102 1103 1104
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
1105
    return NULL;
1106

1107
exit:
1108 1109 1110 1111 1112 1113
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
    return session;
1114 1115
}

1116 1117
static virDrvOpenStatus
phypOpen(virConnectPtr conn,
E
Eric Blake 已提交
1118
         virConnectAuthPtr auth, unsigned int flags)
1119 1120 1121 1122 1123 1124 1125 1126
{
    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 已提交
1127

E
Eric Blake 已提交
1128 1129
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1130 1131 1132 1133 1134 1135 1136
    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) {
1137 1138
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing server name in phyp:// URI"));
1139 1140 1141 1142
        return VIR_DRV_OPEN_ERROR;
    }

    if (VIR_ALLOC(phyp_driver) < 0) {
1143
        virReportOOMError();
1144
        goto failure;
1145 1146
    }

1147 1148 1149 1150
    if (VIR_ALLOC(uuid_table) < 0) {
        virReportOOMError();
        goto failure;
    }
1151

1152 1153 1154 1155
    if (VIR_ALLOC(connection_data) < 0) {
        virReportOOMError();
        goto failure;
    }
1156
    connection_data->sock = -1;
1157

1158 1159 1160 1161 1162 1163
    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 已提交
1164

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
        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';

1178
        if (contains_specialcharacters(conn->uri->path)) {
1179 1180 1181
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s",
                           _("Error parsing 'path'. Invalid characters."));
1182 1183 1184 1185 1186
            goto failure;
        }
    }

    if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) {
1187 1188
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Error while opening SSH session."));
1189 1190 1191 1192
        goto failure;
    }

    connection_data->session = session;
1193
    connection_data->sock = internal_socket;
1194 1195 1196 1197 1198 1199 1200 1201 1202

    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) {
1203
        virReportOOMError();
1204
        goto failure;
1205 1206
    }

1207 1208
    conn->privateData = phyp_driver;
    conn->networkPrivateData = connection_data;
1209

1210 1211
    if ((phyp_driver->system_type = phypGetSystemType(conn)) == -1)
        goto failure;
1212

1213 1214
    if (phypUUIDTable_Init(conn) == -1)
        goto failure;
1215

1216 1217 1218 1219 1220 1221 1222
    if (phyp_driver->system_type == HMC) {
        if ((phyp_driver->vios_id = phypGetVIOSPartitionID(conn)) == -1)
            goto failure;
    }

    return VIR_DRV_OPEN_SUCCESS;

1223
failure:
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
    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);
    }

1237 1238
    if (connection_data)
        VIR_FORCE_CLOSE(connection_data->sock);
1239 1240 1241
    VIR_FREE(connection_data);

    return VIR_DRV_OPEN_ERROR;
1242 1243 1244
}

static int
1245
phypClose(virConnectPtr conn)
1246
{
1247 1248 1249
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
1250

1251 1252
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
1253

1254 1255 1256 1257
    virCapabilitiesFree(phyp_driver->caps);
    phypUUIDTable_Free(phyp_driver->uuid_table);
    VIR_FREE(phyp_driver->managed_system);
    VIR_FREE(phyp_driver);
1258 1259

    VIR_FORCE_CLOSE(connection_data->sock);
1260 1261 1262
    VIR_FREE(connection_data);
    return 0;
}
1263 1264


1265 1266 1267 1268 1269 1270
static int
phypIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Phyp uses an SSH tunnel, so is always encrypted */
    return 1;
}
1271

1272 1273 1274 1275 1276 1277

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

1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296

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


1297 1298 1299 1300 1301
static int
phypIsUpdated(virDomainPtr conn ATTRIBUTE_UNUSED)
{
    return 0;
}
1302 1303

/* return the lpar_id given a name and a managed system name */
1304
static int
1305 1306
phypGetLparID(LIBSSH2_SESSION * session, const char *managed_system,
              const char *name, virConnectPtr conn)
1307
{
1308
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1309
    int system_type = phyp_driver->system_type;
1310
    int lpar_id = -1;
E
Eduardo Otubo 已提交
1311 1312
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1313
    virBufferAddLit(&buf, "lssyscfg -r lpar");
E
Eduardo Otubo 已提交
1314
    if (system_type == HMC)
1315 1316
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --filter lpar_names=%s -F lpar_id", name);
E
Eric Blake 已提交
1317
    phypExecInt(session, &buf, conn, &lpar_id);
1318
    return lpar_id;
1319 1320
}

1321 1322 1323 1324
/* 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)
1325 1326
{
    phyp_driverPtr phyp_driver = conn->privateData;
1327 1328 1329 1330
    int system_type = phyp_driver->system_type;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1331

1332 1333
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
1334 1335
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " --filter lpar_ids=%d -F name", lpar_id);
1336
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1337

1338
    if (exit_status < 0)
1339 1340
        VIR_FREE(ret);
    return ret;
1341 1342
}

1343 1344 1345 1346 1347 1348 1349 1350 1351

/* 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)
1352 1353
{
    phyp_driverPtr phyp_driver = conn->privateData;
1354 1355 1356
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    lparPtr *lpars = uuid_table->lpars;
    unsigned int i = 0;
1357

1358 1359
    for (i = 0; i < uuid_table->nlpars; i++) {
        if (lpars[i]->id == lpar_id) {
1360
            memcpy(uuid, lpars[i]->uuid, VIR_UUID_BUFLEN);
1361 1362 1363
            return 0;
        }
    }
1364

1365
    return -1;
1366 1367
}

1368 1369 1370 1371 1372 1373 1374 1375
/*
 * type:
 * 0 - maxmem
 * 1 - memory
 * */
static unsigned long
phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id,
               int type)
1376
{
1377 1378 1379 1380 1381 1382
    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;
1383

1384 1385
    if (type != 1 && type != 0)
        return 0;
1386

1387 1388
    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
1389 1390
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1391 1392
                      " -r mem --level lpar -F %s --filter lpar_ids=%d",
                      type ? "curr_mem" : "curr_max_mem", lpar_id);
E
Eric Blake 已提交
1393
    phypExecInt(session, &buf, conn, &memory);
1394
    return memory;
1395 1396
}

1397 1398 1399
static unsigned long
phypGetLparCPUGeneric(virConnectPtr conn, const char *managed_system,
                      int lpar_id, int type)
1400
{
1401
    ConnectionData *connection_data = conn->networkPrivateData;
1402
    LIBSSH2_SESSION *session = connection_data->session;
1403
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1404
    int system_type = phyp_driver->system_type;
1405
    int vcpus = 0;
E
Eduardo Otubo 已提交
1406
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1407

1408
    virBufferAddLit(&buf, "lshwres");
E
Eduardo Otubo 已提交
1409
    if (system_type == HMC)
1410 1411
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1412 1413
                      " -r proc --level lpar -F %s --filter lpar_ids=%d",
                      type ? "curr_max_procs" : "curr_procs", lpar_id);
E
Eric Blake 已提交
1414
    phypExecInt(session, &buf, conn, &vcpus);
1415
    return vcpus;
1416
}
1417

1418 1419 1420 1421
static unsigned long
phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id)
{
    return phypGetLparCPUGeneric(conn, managed_system, lpar_id, 0);
1422 1423
}

1424
static int
1425
phypDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
1426 1427 1428
{
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    char *managed_system = phyp_driver->managed_system;
1429

1430
    if (flags != (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_MAXIMUM)) {
1431
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
1432 1433 1434
        return -1;
    }

1435 1436 1437
    return phypGetLparCPUGeneric(dom->conn, managed_system, dom->id, 1);
}

1438 1439 1440 1441 1442 1443 1444
static int
phypGetLparCPUMAX(virDomainPtr dom)
{
    return phypDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                         VIR_DOMAIN_VCPU_MAXIMUM));
}

1445 1446 1447
static int
phypGetRemoteSlot(virConnectPtr conn, const char *managed_system,
                  const char *lpar_name)
1448
{
1449 1450
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
1451
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1452
    int system_type = phyp_driver->system_type;
1453
    int remote_slot = -1;
E
Eduardo Otubo 已提交
1454 1455
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1456
    virBufferAddLit(&buf, "lshwres");
E
Eduardo Otubo 已提交
1457
    if (system_type == HMC)
1458 1459
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r virtualio --rsubtype scsi -F "
1460
                      "remote_slot_num --filter lpar_names=%s", lpar_name);
E
Eric Blake 已提交
1461
    phypExecInt(session, &buf, conn, &remote_slot);
1462
    return remote_slot;
1463 1464
}

1465 1466 1467 1468 1469 1470
/* 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)
1471
{
1472 1473
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
1474
    phyp_driverPtr phyp_driver = conn->privateData;
1475 1476 1477 1478 1479 1480 1481
    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;
1482

1483 1484 1485 1486 1487 1488
    if ((remote_slot =
         phypGetRemoteSlot(conn, managed_system, lpar_name)) == -1)
        return NULL;

    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
1489 1490
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r virtualio --rsubtype scsi -F "
1491
                      "backing_devices --filter slots=%d", remote_slot);
1492
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1493

1494
    if (exit_status < 0 || ret == NULL)
1495
        goto cleanup;
1496

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
    /* 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
1511
            goto cleanup;
1512 1513 1514 1515 1516

        backing_device = strdup(char_ptr);

        if (backing_device == NULL) {
            virReportOOMError();
1517
            goto cleanup;
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
        }
    } else {
        backing_device = ret;
        ret = NULL;
    }

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

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

1529
cleanup:
1530
    VIR_FREE(ret);
1531

1532
    return backing_device;
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
}

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)
1549 1550
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1551 1552
                      " -r prof --filter lpar_ids=%d -F name|head -n 1",
                      lpar_id);
1553
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1554

1555
    if (exit_status < 0)
1556 1557
        VIR_FREE(ret);
    return ret;
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
}

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;
1570
    int slot = -1;
1571 1572 1573
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!(profile = phypGetLparProfile(conn, vios_id))) {
1574
        VIR_ERROR(_("Unable to get VIOS profile name."));
1575
        return -1;
1576 1577 1578 1579 1580
    }

    virBufferAddLit(&buf, "lssyscfg");

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

1583
    virBufferAsprintf(&buf, " -r prof --filter "
1584 1585 1586 1587 1588
                      "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 已提交
1589 1590 1591
    if (phypExecInt(session, &buf, conn, &slot) < 0)
        return -1;
    return slot + 1;
1592 1593 1594 1595 1596
}

static int
phypCreateServerSCSIAdapter(virConnectPtr conn)
{
1597
    int result = -1;
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
    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))) {
1614
        VIR_ERROR(_("Unable to get VIOS name"));
1615
        goto cleanup;
1616 1617 1618
    }

    if (!(profile = phypGetLparProfile(conn, vios_id))) {
1619
        VIR_ERROR(_("Unable to get VIOS profile name."));
1620
        goto cleanup;
1621 1622 1623
    }

    if ((slot = phypGetVIOSNextSlotNumber(conn)) == -1) {
1624
        VIR_ERROR(_("Unable to get free slot number"));
1625
        goto cleanup;
1626 1627 1628 1629 1630 1631 1632
    }

    /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
     * be appended to this list
     * */
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
1633 1634
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r prof --filter lpar_ids=%d,profile_names=%s"
1635 1636
                      " -F virtual_scsi_adapters|sed -e s/\\\"//g",
                      vios_id, profile);
1637
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1638 1639

    if (exit_status < 0 || ret == NULL)
1640
        goto cleanup;
1641 1642 1643 1644 1645 1646

    /* 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)
1647 1648
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r prof -i 'name=%s,lpar_id=%d,"
1649 1650
                      "\"virtual_scsi_adapters=%s,%d/server/any/any/1\"'",
                      vios_name, vios_id, ret, slot);
1651
    VIR_FREE(ret);
1652
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1653 1654

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

    /* 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)
1662 1663
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1664 1665
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      vios_name, slot);
1666
    VIR_FREE(ret);
1667
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1668 1669

    if (exit_status < 0 || ret == NULL)
1670
        goto cleanup;
1671

1672
    result = 0;
1673

1674
cleanup:
1675 1676 1677
    VIR_FREE(profile);
    VIR_FREE(vios_name);
    VIR_FREE(ret);
1678 1679

    return result;
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
}

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

1699
    virBufferAsprintf(&buf, "lsmap -all -field svsa backing -fmt , ");
1700 1701 1702 1703

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

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

1707
    if (exit_status < 0)
1708 1709
        VIR_FREE(ret);
    return ret;
1710 1711 1712 1713 1714 1715
}


static int
phypAttachDevice(virDomainPtr domain, const char *xml)
{
1716
    int result = -1;
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
    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 已提交
1735 1736 1737 1738 1739
    if (VIR_ALLOC(def) < 0) {
        virReportOOMError();
        goto cleanup;
    }

1740
    domain_name = escape_specialcharacters(domain->name);
1741

1742
    if (domain_name == NULL) {
1743
        goto cleanup;
1744 1745 1746 1747 1748 1749
    }

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

    if (def->os.type == NULL) {
        virReportOOMError();
1750
        goto cleanup;
1751 1752 1753 1754 1755
    }

    dev = virDomainDeviceDefParse(phyp_driver->caps, def, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
    if (!dev) {
1756
        goto cleanup;
1757 1758 1759 1760 1761
    }

    if (!
        (vios_name =
         phypGetLparNAME(session, managed_system, vios_id, conn))) {
1762
        VIR_ERROR(_("Unable to get VIOS name"));
1763
        goto cleanup;
1764 1765 1766 1767 1768 1769 1770 1771
    }

    /* 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) {
1772
            VIR_ERROR(_("Unable to create new virtual adapter"));
1773
            goto cleanup;
1774 1775
        } else {
            if (!(scsi_adapter = phypGetVIOSFreeSCSIAdapter(conn))) {
1776
                VIR_ERROR(_("Unable to create new virtual adapter"));
1777
                goto cleanup;
1778 1779 1780 1781 1782
            }
        }
    }

    if (system_type == HMC)
1783
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1784 1785
                          managed_system, vios_id);

1786
    virBufferAsprintf(&buf, "mkvdev -vdev %s -vadapter %s",
1787 1788 1789 1790
                      dev->data.disk->src, scsi_adapter);

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1791
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1792 1793

    if (exit_status < 0 || ret == NULL)
1794
        goto cleanup;
1795 1796

    if (!(profile = phypGetLparProfile(conn, domain->id))) {
1797
        VIR_ERROR(_("Unable to get VIOS profile name."));
1798
        goto cleanup;
1799 1800 1801 1802 1803 1804
    }

    /* Let's get the slot number for the adapter we just created
     * */
    virBufferAddLit(&buf, "lshwres -r virtualio --rsubtype scsi");
    if (system_type == HMC)
1805 1806
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1807 1808
                      " slot_num,backing_device|grep %s|cut -d, -f1",
                      dev->data.disk->src);
E
Eric Blake 已提交
1809
    if (phypExecInt(session, &buf, conn, &slot) < 0)
1810
        goto cleanup;
1811 1812 1813 1814 1815 1816

    /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
     * be appended to this list
     * */
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
1817 1818
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1819 1820 1821
                      " -r prof --filter lpar_ids=%d,profile_names=%s"
                      " -F virtual_scsi_adapters|sed -e 's/\"//g'",
                      vios_id, profile);
1822
    VIR_FREE(ret);
1823
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1824 1825

    if (exit_status < 0 || ret == NULL)
1826
        goto cleanup;
1827 1828 1829 1830 1831 1832

    /* 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)
1833 1834
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1835 1836 1837 1838
                      " -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 已提交
1839
    if (phypExecInt(session, &buf, conn, &slot) < 0)
1840
        goto cleanup;
1841 1842 1843 1844 1845 1846

    /* 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)
1847 1848
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf,
1849 1850
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      domain_name, slot);
1851
    VIR_FREE(ret);
1852
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1853 1854

    if (exit_status < 0 || ret == NULL) {
1855
        VIR_ERROR(_
1856 1857
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    "Contact your support to enable this feature."));
1858
        goto cleanup;
1859 1860
    }

1861
    result = 0;
1862

1863
cleanup:
1864
    VIR_FREE(ret);
1865 1866
    virDomainDeviceDefFree(dev);
    virDomainDefFree(def);
1867 1868
    VIR_FREE(vios_name);
    VIR_FREE(scsi_adapter);
1869 1870 1871 1872
    VIR_FREE(profile);
    VIR_FREE(domain_name);

    return result;
1873 1874
}

1875 1876
static char *
phypVolumeGetKey(virConnectPtr conn, const char *name)
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
{
    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)
1889
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1890 1891
                          managed_system, vios_id);

1892
    virBufferAsprintf(&buf, "lslv %s -field lvid", name);
1893 1894 1895 1896

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

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

1900
    if (exit_status < 0)
1901 1902
        VIR_FREE(ret);
    return ret;
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
}

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

1922
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field name", name);
1923 1924 1925 1926

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

1927
    virBufferAsprintf(&buf, "|sed '1d; s/ //g'");
1928
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
1929

1930
    if (exit_status < 0)
1931 1932
        VIR_FREE(ret);
    return ret;
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
}

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;
1944
    int sp_size = -1;
1945 1946 1947
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
1948
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1949 1950
                          managed_system, vios_id);

1951
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field size", name);
1952 1953 1954 1955

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

1956
    virBufferAsprintf(&buf, "|sed '1d; s/ //g'");
E
Eric Blake 已提交
1957
    phypExecInt(session, &buf, conn, &sp_size);
1958
    return sp_size;
1959 1960
}

1961
static char *
1962
phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
1963
                unsigned int capacity)
1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
{
    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;
1974
    char *key = NULL;
1975 1976

    if (system_type == HMC)
1977
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
1978 1979
                          managed_system, vios_id);

1980
    virBufferAsprintf(&buf, "mklv -lv %s %s %d", lvname, spname, capacity);
1981 1982 1983

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
1984
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
1985 1986

    if (exit_status < 0) {
1987
        VIR_ERROR(_("Unable to create Volume: %s"), NULLSTR(ret));
1988
        goto cleanup;
1989 1990
    }

1991 1992
    key = phypVolumeGetKey(conn, lvname);

1993
cleanup:
1994 1995
    VIR_FREE(ret);

1996
    return key;
1997 1998 1999 2000 2001
}

static virStorageVolPtr
phypVolumeLookupByName(virStoragePoolPtr pool, const char *volname)
{
2002 2003
    char *key;
    virStorageVolPtr vol;
2004

2005
    key = phypVolumeGetKey(pool->conn, volname);
2006

2007
    if (key == NULL)
2008 2009
        return NULL;

2010
    vol = virGetStorageVol(pool->conn, pool->name, volname, key, NULL, NULL);
2011 2012 2013 2014

    VIR_FREE(key);

    return vol;
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
}

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

    virStorageVolDefPtr voldef = NULL;
    virStoragePoolDefPtr spdef = NULL;
    virStorageVolPtr vol = NULL;
2026
    virStorageVolPtr dup_vol = NULL;
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
    char *key = NULL;

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

    /* Filling spdef manually
     * */
    if (pool->name != NULL) {
        spdef->name = pool->name;
    } else {
2039
        VIR_ERROR(_("Unable to determine storage pool's name."));
2040 2041 2042 2043
        goto err;
    }

    if (memcpy(spdef->uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
2044
        VIR_ERROR(_("Unable to determine storage pool's uuid."));
2045 2046 2047 2048 2049
        goto err;
    }

    if ((spdef->capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2050
        VIR_ERROR(_("Unable to determine storage pools's size."));
2051 2052 2053
        goto err;
    }

J
Ján Tomko 已提交
2054
    /* Information not available */
2055 2056 2057 2058 2059 2060 2061 2062
    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) {
2063
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
2064 2065 2066 2067
        goto err;
    }

    if ((voldef = virStorageVolDefParseString(spdef, xml)) == NULL) {
2068
        VIR_ERROR(_("Error parsing volume XML."));
2069 2070 2071 2072
        goto err;
    }

    /* checking if this name already exists on this system */
2073
    if ((dup_vol = phypVolumeLookupByName(pool, voldef->name)) != NULL) {
2074
        VIR_ERROR(_("StoragePool name already exists."));
2075
        virObjectUnref(dup_vol);
2076 2077 2078 2079 2080 2081 2082
        goto err;
    }

    /* The key must be NULL, the Power Hypervisor creates a key
     * in the moment you create the volume.
     * */
    if (voldef->key) {
2083
        VIR_ERROR(_("Key must be empty, Power Hypervisor will create one for you."));
2084 2085 2086 2087
        goto err;
    }

    if (voldef->capacity) {
2088
        VIR_ERROR(_("Capacity cannot be empty."));
2089 2090 2091
        goto err;
    }

2092 2093 2094 2095
    key = phypBuildVolume(pool->conn, voldef->name, spdef->name,
                          voldef->capacity);

    if (key == NULL)
2096 2097 2098 2099
        goto err;

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

2103 2104
    VIR_FREE(key);

2105 2106
    return vol;

2107
err:
2108
    VIR_FREE(key);
2109 2110
    virStorageVolDefFree(voldef);
    virStoragePoolDefFree(spdef);
2111
    virObjectUnref(vol);
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
    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)
2130
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2131 2132
                          managed_system, vios_id);

2133
    virBufferAsprintf(&buf, "lssp -detail -sp %s -field pvname", sp);
2134 2135 2136 2137

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

2138
    virBufferAsprintf(&buf, "|sed 1d");
2139
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2140

2141
    if (exit_status < 0)
2142 2143
        VIR_FREE(ret);
    return ret;
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
}

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;
2156
    char *ret = NULL;
2157 2158
    char *key = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2159
    virStorageVolPtr vol = NULL;
2160 2161

    if (system_type == HMC)
2162
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2163 2164
                          managed_system, vios_id);

2165
    virBufferAsprintf(&buf, "lslv %s -field vgname", volname);
2166 2167 2168 2169

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

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

2173
    if (exit_status < 0 || ret == NULL)
2174
        goto cleanup;
2175

2176
    key = phypVolumeGetKey(conn, volname);
2177

2178
    if (key == NULL)
2179
        goto cleanup;
2180

2181
    vol = virGetStorageVol(conn, ret, volname, key, NULL, NULL);
2182

2183
cleanup:
2184
    VIR_FREE(ret);
2185 2186 2187
    VIR_FREE(key);

    return vol;
2188 2189 2190 2191 2192 2193
}

static int
phypGetStoragePoolUUID(virConnectPtr conn, unsigned char *uuid,
                       const char *name)
{
2194
    int result = -1;
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
    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)
2206
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2207 2208
                          managed_system, vios_id);

2209
    virBufferAsprintf(&buf, "lsdev -dev %s -attr vgserial_id", name);
2210 2211 2212 2213

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

2214
    virBufferAsprintf(&buf, "|sed '1,2d'");
2215
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2216 2217

    if (exit_status < 0 || ret == NULL)
2218
        goto cleanup;
2219

2220
    if (memcpy(uuid, ret, VIR_UUID_BUFLEN) == NULL)
2221
        goto cleanup;
2222

2223
    result = 0;
2224

2225
cleanup:
2226
    VIR_FREE(ret);
2227 2228

    return result;
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
}

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

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

2239
    return virGetStoragePool(conn, name, uuid, NULL, NULL);
2240 2241 2242 2243 2244
}

static char *
phypVolumeGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
{
2245 2246 2247
    virStorageVolDef voldef;
    virStoragePoolDef pool;
    virStoragePoolPtr sp;
2248
    char *xml = NULL;
2249

2250 2251 2252
    virCheckFlags(0, NULL);

    memset(&voldef, 0, sizeof(virStorageVolDef));
2253
    memset(&pool, 0, sizeof(virStoragePoolDef));
2254

2255
    sp = phypStoragePoolLookupByName(vol->conn, vol->pool);
2256 2257

    if (!sp)
2258
        goto cleanup;
2259 2260 2261 2262

    if (sp->name != NULL) {
        pool.name = sp->name;
    } else {
2263
        VIR_ERROR(_("Unable to determine storage sp's name."));
2264
        goto cleanup;
2265 2266
    }

2267
    if (memcpy(pool.uuid, sp->uuid, VIR_UUID_BUFLEN) == NULL) {
2268
        VIR_ERROR(_("Unable to determine storage sp's uuid."));
2269
        goto cleanup;
2270 2271 2272
    }

    if ((pool.capacity = phypGetStoragePoolSize(sp->conn, sp->name)) == -1) {
2273
        VIR_ERROR(_("Unable to determine storage sps's size."));
2274
        goto cleanup;
2275 2276
    }

J
Ján Tomko 已提交
2277
    /* Information not available */
2278 2279 2280 2281 2282 2283 2284
    pool.allocation = 0;
    pool.available = 0;

    pool.source.ndevice = 1;

    if ((pool.source.adapter =
         phypGetStoragePoolDevice(sp->conn, sp->name)) == NULL) {
2285
        VIR_ERROR(_("Unable to determine storage sps's source adapter."));
2286
        goto cleanup;
2287 2288 2289 2290 2291
    }

    if (vol->name != NULL)
        voldef.name = vol->name;
    else {
2292
        VIR_ERROR(_("Unable to determine storage pool's name."));
2293
        goto cleanup;
2294 2295
    }

2296 2297 2298 2299
    voldef.key = strdup(vol->key);

    if (voldef.key == NULL) {
        virReportOOMError();
2300
        goto cleanup;
2301 2302 2303 2304
    }

    voldef.type = VIR_STORAGE_POOL_LOGICAL;

2305 2306 2307 2308
    xml = virStorageVolDefFormat(&pool, &voldef);

    VIR_FREE(voldef.key);

2309
cleanup:
2310
    virObjectUnref(sp);
2311
    return xml;
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
}

/* 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;
2333
    char *ret = NULL;
2334 2335
    char *path = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2336
    char *pv;
2337 2338

    if (system_type == HMC)
2339
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2340 2341
                          managed_system, vios_id);

2342
    virBufferAsprintf(&buf, "lslv %s -field vgname", vol->name);
2343 2344 2345 2346

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

2347
    virBufferAsprintf(&buf,
2348
                      "|sed -e 's/^VOLUME GROUP://g' -e 's/ //g'");
2349
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
2350

2351
    if (exit_status < 0 || ret == NULL)
2352
        goto cleanup;
2353

2354
    pv = phypVolumeGetPhysicalVolumeByStoragePool(vol, ret);
2355

2356 2357
    if (!pv)
        goto cleanup;
2358

2359
    if (virAsprintf(&path, "/%s/%s/%s", pv, ret, vol->name) < 0) {
2360 2361 2362
        virReportOOMError();
        goto cleanup;
    }
2363

2364
cleanup:
2365
    VIR_FREE(ret);
2366
    VIR_FREE(path);
2367 2368

    return path;
2369 2370 2371 2372 2373 2374
}

static int
phypStoragePoolListVolumes(virStoragePoolPtr pool, char **const volumes,
                           int nvolumes)
{
2375
    bool success = false;
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
    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 已提交
2388
    char *char_ptr = NULL;
2389 2390 2391
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2392
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2393 2394
                          managed_system, vios_id);

2395
    virBufferAsprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
2396 2397 2398 2399

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

2400
    virBufferAsprintf(&buf, "|sed '1,2d'");
2401
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2402 2403 2404

    /* I need to parse the textual return in order to get the volumes */
    if (exit_status < 0 || ret == NULL)
2405
        goto cleanup;
2406 2407 2408 2409
    else {
        volumes_list = ret;

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

E
Eric Blake 已提交
2412 2413
            if (char_ptr) {
                *char_ptr = '\0';
2414 2415
                if ((volumes[got++] = strdup(volumes_list)) == NULL) {
                    virReportOOMError();
2416
                    goto cleanup;
2417
                }
E
Eric Blake 已提交
2418 2419
                char_ptr++;
                volumes_list = char_ptr;
2420 2421 2422 2423 2424
            } else
                break;
        }
    }

2425 2426
    success = true;

2427
cleanup:
2428 2429 2430 2431 2432 2433
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(volumes[i]);

        got = -1;
    }
2434
    VIR_FREE(ret);
2435
    return got;
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
}

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;
2446
    int nvolumes = -1;
2447 2448 2449 2450 2451
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2452
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2453
                          managed_system, vios_id);
2454
    virBufferAsprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
2455 2456
    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2457
    virBufferAsprintf(&buf, "|grep -c '^.*$'");
E
Eric Blake 已提交
2458 2459
    if (phypExecInt(session, &buf, conn, &nvolumes) < 0)
        return -1;
2460 2461

    /* We need to remove 2 line from the header text output */
E
Eric Blake 已提交
2462
    return nvolumes - 2;
2463 2464 2465 2466 2467
}

static int
phypDestroyStoragePool(virStoragePoolPtr pool)
{
2468
    int result = -1;
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
    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)
2481
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2482 2483
                          managed_system, vios_id);

2484
    virBufferAsprintf(&buf, "rmsp %s", pool->name);
2485 2486 2487

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2488
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2489 2490

    if (exit_status < 0) {
2491
        VIR_ERROR(_("Unable to destroy Storage Pool: %s"), NULLSTR(ret));
2492
        goto cleanup;
2493 2494
    }

2495
    result = 0;
2496

2497
cleanup:
2498
    VIR_FREE(ret);
2499 2500

    return result;
2501 2502 2503 2504 2505
}

static int
phypBuildStoragePool(virConnectPtr conn, virStoragePoolDefPtr def)
{
2506
    int result = -1;
2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
    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)
2519
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2520 2521
                          managed_system, vios_id);

2522
    virBufferAsprintf(&buf, "mksp -f %schild %s", def->name,
2523 2524 2525 2526
                      source.adapter);

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2527
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2528 2529

    if (exit_status < 0) {
2530
        VIR_ERROR(_("Unable to create Storage Pool: %s"), NULLSTR(ret));
2531
        goto cleanup;
2532 2533
    }

2534
    result = 0;
2535

2536
cleanup:
2537
    VIR_FREE(ret);
2538 2539

    return result;
2540 2541 2542 2543 2544 2545 2546 2547 2548 2549

}

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;
2550
    int nsp = -1;
2551 2552 2553 2554 2555
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2556
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2557 2558
                          managed_system, vios_id);

2559
    virBufferAsprintf(&buf, "lsvg");
2560 2561 2562 2563

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

2564
    virBufferAsprintf(&buf, "|grep -c '^.*$'");
E
Eric Blake 已提交
2565
    phypExecInt(session, &buf, conn, &nsp);
2566
    return nsp;
2567 2568 2569 2570 2571
}

static int
phypListStoragePools(virConnectPtr conn, char **const pools, int npools)
{
2572
    bool success = false;
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
    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 已提交
2584
    char *char_ptr = NULL;
2585 2586 2587
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
2588
        virBufferAsprintf(&buf, "viosvrcmd -m %s --id %d -c '",
2589 2590
                          managed_system, vios_id);

2591
    virBufferAsprintf(&buf, "lsvg");
2592 2593 2594

    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
2595
    ret = phypExecBuffer(session, &buf, &exit_status, conn, false);
2596 2597 2598

    /* I need to parse the textual return in order to get the storage pools */
    if (exit_status < 0 || ret == NULL)
2599
        goto cleanup;
2600 2601 2602 2603
    else {
        storage_pools = ret;

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

E
Eric Blake 已提交
2606 2607
            if (char_ptr) {
                *char_ptr = '\0';
2608 2609
                if ((pools[got++] = strdup(storage_pools)) == NULL) {
                    virReportOOMError();
2610
                    goto cleanup;
2611
                }
E
Eric Blake 已提交
2612 2613
                char_ptr++;
                storage_pools = char_ptr;
2614 2615 2616 2617 2618
            } else
                break;
        }
    }

2619 2620
    success = true;

2621
cleanup:
2622 2623 2624 2625 2626 2627
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(pools[i]);

        got = -1;
    }
2628
    VIR_FREE(ret);
2629
    return got;
2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672
}

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)) {
2673
            sp = virGetStoragePool(conn, pools[i], uuid, NULL, NULL);
2674 2675 2676 2677 2678 2679 2680 2681 2682 2683
            VIR_FREE(local_uuid);
            VIR_FREE(pools);

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

2684
err:
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
    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;
2697
    virStoragePoolPtr dup_sp = NULL;
2698 2699 2700 2701 2702 2703
    virStoragePoolPtr sp = NULL;

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

    /* checking if this name already exists on this system */
2704
    if ((dup_sp = phypStoragePoolLookupByName(conn, def->name)) != NULL) {
2705
        VIR_WARN("StoragePool name already exists.");
2706
        virObjectUnref(dup_sp);
2707 2708 2709 2710
        goto err;
    }

    /* checking if ID or UUID already exists on this system */
2711
    if ((dup_sp = phypGetStoragePoolLookUpByUUID(conn, def->uuid)) != NULL) {
2712
        VIR_WARN("StoragePool uuid already exists.");
2713
        virObjectUnref(dup_sp);
2714 2715
        goto err;
    }
2716

2717
    if ((sp = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL)) == NULL)
2718 2719 2720 2721 2722 2723 2724
        goto err;

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

    return sp;

2725
err:
2726
    virStoragePoolDefFree(def);
2727
    virObjectUnref(sp);
2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741
    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 {
2742
        VIR_ERROR(_("Unable to determine storage pool's name."));
2743 2744 2745
        goto err;
    }

2746
    if (memcpy(def.uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
2747
        VIR_ERROR(_("Unable to determine storage pool's uuid."));
2748 2749 2750 2751 2752
        goto err;
    }

    if ((def.capacity =
         phypGetStoragePoolSize(pool->conn, pool->name)) == -1) {
2753
        VIR_ERROR(_("Unable to determine storage pools's size."));
2754 2755 2756
        goto err;
    }

J
Ján Tomko 已提交
2757
    /* Information not available */
2758 2759 2760 2761 2762 2763 2764 2765
    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) {
2766
        VIR_ERROR(_("Unable to determine storage pools's source adapter."));
2767 2768 2769 2770 2771
        goto err;
    }

    return virStoragePoolDefFormat(&def);

2772
err:
2773
    return NULL;
2774 2775
}

E
Eduardo Otubo 已提交
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
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 已提交
2792
    int rv = -1;
E
Eduardo Otubo 已提交
2793 2794 2795 2796 2797

    /* Getting the remote slot number */

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

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

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

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

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

2824
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
2825 2826
                      " -r virtualio --rsubtype eth"
                      " --id %d -o r -s %d", lpar_id, slot_num);
2827 2828
    VIR_FREE(ret);
    ret = phypExecBuffer(session, &buf, &exit_status, iface->conn, false);
E
Eduardo Otubo 已提交
2829 2830

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

E
Eric Blake 已提交
2833
    rv = 0;
E
Eduardo Otubo 已提交
2834

E
Eric Blake 已提交
2835
cleanup:
E
Eduardo Otubo 已提交
2836
    VIR_FREE(ret);
E
Eric Blake 已提交
2837
    return rv;
E
Eduardo Otubo 已提交
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857
}

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 已提交
2858
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2859 2860

    if (!(def = virInterfaceDefParseString(xml)))
E
Eric Blake 已提交
2861
        goto cleanup;
E
Eduardo Otubo 已提交
2862 2863 2864 2865

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

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

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

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

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

    if (exit_status < 0 || ret != NULL)
E
Eric Blake 已提交
2891
        goto cleanup;
E
Eduardo Otubo 已提交
2892 2893 2894 2895 2896 2897 2898 2899 2900

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

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

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

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

    memcpy(name, ret, PHYP_IFACENAME_SIZE-1);

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

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

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
2939
        goto cleanup;
E
Eduardo Otubo 已提交
2940 2941 2942

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

E
Eric Blake 已提交
2945
cleanup:
E
Eduardo Otubo 已提交
2946 2947
    VIR_FREE(ret);
    virInterfaceDefFree(def);
E
Eric Blake 已提交
2948
    return result;
E
Eduardo Otubo 已提交
2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
}

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];
2965
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
2966 2967 2968 2969

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

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

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

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

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

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

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3003
        goto cleanup;
E
Eduardo Otubo 已提交
3004 3005 3006

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

E
Eric Blake 已提交
3009
cleanup:
E
Eduardo Otubo 已提交
3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
    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 已提交
3023
    int state = -1;
E
Eduardo Otubo 已提交
3024 3025 3026

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

3029
    virBufferAsprintf(&buf,
E
Eduardo Otubo 已提交
3030 3031 3032
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,state |"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);
E
Eric Blake 已提交
3033
    phypExecInt(session, &buf, iface->conn, &state);
E
Eduardo Otubo 已提交
3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050
    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 已提交
3051
    char *char_ptr = NULL;
E
Eduardo Otubo 已提交
3052
    virBuffer buf = VIR_BUFFER_INITIALIZER;
E
Eric Blake 已提交
3053
    bool success = false;
E
Eduardo Otubo 已提交
3054 3055 3056

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

E
Eric Blake 已提交
3063 3064
    /* I need to parse the textual return in order to get the network
     * interfaces */
E
Eduardo Otubo 已提交
3065
    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3066
        goto cleanup;
E
Eduardo Otubo 已提交
3067 3068 3069 3070

    networks = ret;

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

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

E
Eric Blake 已提交
3086 3087 3088 3089 3090
cleanup:
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);
    }
E
Eduardo Otubo 已提交
3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
    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 已提交
3104
    int nnets = -1;
E
Eduardo Otubo 已提交
3105 3106 3107 3108
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

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

3118 3119
static int
phypGetLparState(virConnectPtr conn, unsigned int lpar_id)
3120
{
3121
    ConnectionData *connection_data = conn->networkPrivateData;
3122
    phyp_driverPtr phyp_driver = conn->privateData;
3123 3124 3125 3126 3127 3128 3129
    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;
3130

3131 3132
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
3133 3134
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -F state --filter lpar_ids=%d", lpar_id);
3135
    ret = phypExecBuffer(session, &buf, &exit_status, conn, true);
3136

3137 3138
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3139

3140 3141 3142 3143 3144 3145
    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;
3146

3147
cleanup:
3148 3149
    VIR_FREE(ret);
    return state;
3150 3151
}

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

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

3176 3177
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3178

3179 3180 3181 3182
    if (STREQ(ret, "LVPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_BLOCK;
    else if (STREQ(ret, "FBPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_FILE;
3183

3184
cleanup:
3185 3186 3187
    VIR_FREE(ret);
    return disk_type;
}
3188

3189 3190 3191 3192 3193
static int
phypNumDefinedDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 1);
}
3194

3195 3196 3197 3198
static int
phypNumDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 0);
3199 3200
}

3201 3202
static int
phypListDomains(virConnectPtr conn, int *ids, int nids)
3203
{
3204 3205
    return phypListDomainsGeneric(conn, ids, nids, 0);
}
3206

3207 3208 3209
static int
phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames)
{
3210
    bool success = false;
3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
    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 已提交
3221
    char *char_ptr = NULL;
3222
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3223

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

3231 3232
    /* I need to parse the textual return in order to get the domains */
    if (exit_status < 0 || ret == NULL)
3233
        goto cleanup;
3234 3235
    else {
        domains = ret;
3236

3237
        while (got < nnames) {
E
Eric Blake 已提交
3238
            char_ptr = strchr(domains, '\n');
3239

E
Eric Blake 已提交
3240 3241
            if (char_ptr) {
                *char_ptr = '\0';
3242
                if ((names[got++] = strdup(domains)) == NULL) {
3243
                    virReportOOMError();
3244
                    goto cleanup;
3245
                }
E
Eric Blake 已提交
3246 3247
                char_ptr++;
                domains = char_ptr;
3248 3249
            } else
                break;
3250
        }
3251 3252
    }

3253 3254
    success = true;

3255
cleanup:
3256 3257 3258 3259 3260 3261
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);

        got = -1;
    }
3262
    VIR_FREE(ret);
3263
    return got;
3264 3265
}

3266 3267
static virDomainPtr
phypDomainLookupByName(virConnectPtr conn, const char *lpar_name)
3268
{
3269 3270 3271 3272 3273 3274 3275
    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];
3276

3277 3278 3279
    lpar_id = phypGetLparID(session, managed_system, lpar_name, conn);
    if (lpar_id == -1)
        return NULL;
3280

3281 3282
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
        return NULL;
3283

3284 3285 3286 3287 3288 3289
    dom = virGetDomain(conn, lpar_name, lpar_uuid);

    if (dom)
        dom->id = lpar_id;

    return dom;
3290 3291
}

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

3302 3303
    char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
                                      conn);
3304

3305
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
3306
        goto cleanup;
3307

3308
    dom = virGetDomain(conn, lpar_name, lpar_uuid);
3309

3310 3311
    if (dom)
        dom->id = lpar_id;
3312

3313
cleanup:
3314
    VIR_FREE(lpar_name);
3315

3316
    return dom;
3317 3318
}

3319
static char *
3320
phypDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
3321
{
3322 3323
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = dom->conn->privateData;
3324
    LIBSSH2_SESSION *session = connection_data->session;
3325 3326
    virDomainDef def;
    char *managed_system = phyp_driver->managed_system;
E
Eduardo Otubo 已提交
3327

3328 3329
    /* Flags checked by virDomainDefFormat */

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

3332 3333 3334 3335 3336 3337 3338
    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) {
3339
        VIR_ERROR(_("Unable to determine domain's name."));
3340
        goto err;
E
Eduardo Otubo 已提交
3341 3342
    }

3343
    if (phypGetLparUUID(def.uuid, dom->id, dom->conn) == -1) {
3344
        VIR_ERROR(_("Unable to generate random uuid."));
E
Eduardo Otubo 已提交
3345 3346
        goto err;
    }
3347

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

3354
    if ((def.mem.cur_balloon =
3355
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0) {
3356
        VIR_ERROR(_("Unable to determine domain's memory."));
3357 3358
        goto err;
    }
3359

E
Eric Blake 已提交
3360
    if ((def.maxvcpus = def.vcpus =
3361
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0) {
3362
        VIR_ERROR(_("Unable to determine domain's CPU."));
3363
        goto err;
3364
    }
3365

3366
    return virDomainDefFormat(&def, flags);
3367

3368
err:
3369 3370
    return NULL;
}
3371

3372 3373 3374
static int
phypDomainResume(virDomainPtr dom)
{
3375
    int result = -1;
3376 3377 3378 3379 3380 3381 3382 3383
    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;
3384

3385 3386
    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
3387 3388
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar -o on --id %d -f %s",
3389
                      dom->id, dom->name);
3390
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3391

3392
    if (exit_status < 0)
3393
        goto cleanup;
3394

3395
    result = 0;
3396

3397
cleanup:
3398
    VIR_FREE(ret);
3399 3400

    return result;
3401 3402
}

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

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

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

    if (exit_status < 0)
3459
        goto cleanup;
3460

3461
    result = 0;
3462

3463
cleanup:
3464
    VIR_FREE(ret);
3465 3466

    return result;
3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478
}

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)
3479
        VIR_WARN("Unable to determine domain's max memory.");
3480 3481 3482

    if ((info->memory =
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0)
3483
        VIR_WARN("Unable to determine domain's memory.");
3484 3485 3486

    if ((info->nrVirtCpu =
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
3487
        VIR_WARN("Unable to determine domain's CPU.");
3488 3489 3490 3491

    return 0;
}

3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506
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;
}

3507
static int
3508 3509
phypDomainDestroyFlags(virDomainPtr dom,
                       unsigned int flags)
3510
{
3511
    int result = -1;
3512 3513 3514 3515 3516 3517 3518 3519 3520
    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;

3521 3522
    virCheckFlags(0, -1);

3523 3524
    virBufferAddLit(&buf, "rmsyscfg");
    if (system_type == HMC)
3525 3526
        virBufferAsprintf(&buf, " -m %s", managed_system);
    virBufferAsprintf(&buf, " -r lpar --id %d", dom->id);
3527
    ret = phypExecBuffer(session, &buf, &exit_status, dom->conn, false);
3528 3529

    if (exit_status < 0)
3530
        goto cleanup;
3531 3532

    if (phypUUIDTable_RemLpar(dom->conn, dom->id) == -1)
3533
        goto cleanup;
3534

3535
    dom->id = -1;
3536
    result = 0;
3537

3538
cleanup:
3539 3540
    VIR_FREE(ret);

3541
    return result;
3542
}
3543

3544 3545 3546 3547 3548 3549
static int
phypDomainDestroy(virDomainPtr dom)
{
    return phypDomainDestroyFlags(dom, 0);
}

3550 3551
static int
phypBuildLpar(virConnectPtr conn, virDomainDefPtr def)
3552
{
3553
    int result = -1;
3554 3555 3556 3557 3558 3559 3560 3561
    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;
3562

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

3570
    if (!def->mem.max_balloon) {
3571 3572 3573
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Field <currentMemory> on the domain XML file is missing or "
                         "has invalid value."));
3574
        goto cleanup;
3575 3576
    }

3577
    if (def->ndisks < 1) {
3578 3579
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Domain XML must contain at least one <disk> element."));
3580
        goto cleanup;
3581 3582 3583
    }

    if (!def->disks[0]->src) {
3584 3585 3586
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Field <src> under <disk> on the domain XML file is "
                         "missing."));
3587
        goto cleanup;
3588 3589
    }

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

3600
    if (exit_status < 0) {
3601
        VIR_ERROR(_("Unable to create LPAR. Reason: '%s'"), NULLSTR(ret));
3602
        goto cleanup;
3603
    }
3604

3605
    if (phypUUIDTable_AddLpar(conn, def->uuid, def->id) == -1) {
3606
        VIR_ERROR(_("Unable to add LPAR to the table"));
3607
        goto cleanup;
3608
    }
3609

3610
    result = 0;
3611

3612
cleanup:
3613
    VIR_FREE(ret);
3614 3615

    return result;
3616
}
3617

3618 3619 3620 3621
static virDomainPtr
phypDomainCreateAndStart(virConnectPtr conn,
                         const char *xml, unsigned int flags)
{
E
Eduardo Otubo 已提交
3622
    virCheckFlags(0, NULL);
3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636

    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 已提交
3637
                                        1 << VIR_DOMAIN_VIRT_PHYP,
3638 3639 3640 3641
                                        VIR_DOMAIN_XML_SECURE)))
        goto err;

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

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

3700
    if (flags != VIR_DOMAIN_VCPU_LIVE) {
3701
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
3702 3703 3704
        return -1;
    }

3705 3706 3707 3708
    if ((ncpus = phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
        return 0;

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

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

    VIR_FREE(ret);
    return 0;
3739 3740

}
3741

3742 3743 3744 3745 3746 3747
static int
phypDomainSetCPU(virDomainPtr dom, unsigned int nvcpus)
{
    return phypDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_VCPU_LIVE);
}

3748
static virDrvOpenStatus
3749 3750
phypVIOSDriverOpen(virConnectPtr conn,
                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3751
                   unsigned int flags)
3752
{
E
Eric Blake 已提交
3753 3754
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

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

3758 3759 3760 3761
    return VIR_DRV_OPEN_SUCCESS;
}

static int
3762
phypVIOSDriverClose(virConnectPtr conn ATTRIBUTE_UNUSED)
3763 3764 3765 3766
{
    return 0;
}

3767
static virDriver phypDriver = {
3768 3769
    .no = VIR_DRV_PHYP,
    .name = "PHYP",
3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781
    .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 */
3782
    .domainDestroyFlags = phypDomainDestroyFlags, /* 0.9.4 */
3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795
    .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 */
3796
    .isAlive = phypIsAlive, /* 0.9.8 */
3797 3798
};

3799 3800
static virStorageDriver phypStorageDriver = {
    .name = "PHYP",
3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818
    .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 */
3819 3820
};

E
Eduardo Otubo 已提交
3821
static virInterfaceDriver phypInterfaceDriver = {
3822
    .name = "PHYP",
3823 3824 3825 3826 3827 3828 3829 3830
    .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 */
3831 3832
};

3833 3834 3835
int
phypRegister(void)
{
3836 3837 3838 3839
    if (virRegisterDriver(&phypDriver) < 0)
        return -1;
    if (virRegisterStorageDriver(&phypStorageDriver) < 0)
        return -1;
E
Eduardo Otubo 已提交
3840
    if (virRegisterInterfaceDriver(&phypInterfaceDriver) < 0)
3841
        return -1;
3842

3843 3844
    return 0;
}