phyp_driver.c 125.9 KB
Newer Older
1
/*
E
Eric Blake 已提交
2
 * Copyright (C) 2010-2011 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <config.h>

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

#include "internal.h"
47
#include "authhelper.h"
48 49 50 51 52 53 54 55 56 57
#include "util.h"
#include "datatypes.h"
#include "buf.h"
#include "memory.h"
#include "logging.h"
#include "driver.h"
#include "libvirt/libvirt.h"
#include "virterror_internal.h"
#include "uuid.h"
#include "domain_conf.h"
58
#include "storage_conf.h"
59
#include "nodeinfo.h"
60
#include "files.h"
E
Eduardo Otubo 已提交
61
#include "interface_conf.h"
62 63 64 65 66

#include "phyp_driver.h"

#define VIR_FROM_THIS VIR_FROM_PHYP

67 68
#define PHYP_ERROR(code, ...)                                                 \
    virReportErrorHelper(NULL, VIR_FROM_PHYP, code, __FILE__, __FUNCTION__,   \
69
                         __LINE__, __VA_ARGS__)
70

71 72 73 74
/*
 * URI: phyp://user@[hmc|ivm]/managed_system
 * */

75 76
static unsigned const int HMC = 0;
static unsigned const int IVM = 127;
E
Eduardo Otubo 已提交
77 78
static unsigned const int PHYP_IFACENAME_SIZE = 24;
static unsigned const int PHYP_MAC_SIZE= 12;
79

80 81 82 83 84 85 86 87 88
static int
waitsocket(int socket_fd, LIBSSH2_SESSION * session)
{
    struct timeval timeout;
    int rc;
    fd_set fd;
    fd_set *writefd = NULL;
    fd_set *readfd = NULL;
    int dir;
89

90 91
    timeout.tv_sec = 0;
    timeout.tv_usec = 1000;
92

93
    FD_ZERO(&fd);
94

95
    FD_SET(socket_fd, &fd);
96

97 98
    /* now make sure we wait in the correct direction */
    dir = libssh2_session_block_directions(session);
99

100 101
    if (dir & LIBSSH2_SESSION_BLOCK_INBOUND)
        readfd = &fd;
102

103 104
    if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
        writefd = &fd;
105

106
    rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
107

108 109
    return rc;
}
110

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

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

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

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

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

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

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

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

172
    exitcode = 127;
173

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

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

182 183 184 185
    (*exit_status) = exitcode;
    libssh2_channel_free(channel);
    channel = NULL;
    goto exit;
186

187 188 189
  err:
    (*exit_status) = SSH_CMD_ERR;
    virBufferFreeAndReset(&tex_ret);
190
    VIR_FREE(buffer);
191 192 193
    return NULL;

  exit:
194 195
    VIR_FREE(buffer);

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

static int
205
phypGetSystemType(virConnectPtr conn)
206 207
{
    ConnectionData *connection_data = conn->networkPrivateData;
208
    LIBSSH2_SESSION *session = connection_data->session;
209 210 211
    char *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
212

213 214
    if (virAsprintf(&cmd, "lshmc -V") < 0) {
        virReportOOMError();
215
        return -1;
216 217
    }
    ret = phypExec(session, cmd, &exit_status, conn);
218

219 220 221
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return exit_status;
222 223
}

224
static int
225
phypGetVIOSPartitionID(virConnectPtr conn)
226
{
227 228 229 230 231 232 233 234 235 236 237
    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 *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
    int id = -1;
    char *char_ptr;
    char *managed_system = phyp_driver->managed_system;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
238

239 240 241
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
E
Eric Blake 已提交
242 243
    virBufferAddLit(&buf, " -r lpar -F lpar_id,lpar_env"
                    "|sed -n '/vioserver/ {\n s/,.*$//\n p\n}'");
244 245 246 247 248 249
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);
250

251
    ret = phypExec(session, cmd, &exit_status, conn);
252

253
    if (exit_status < 0 || ret == NULL)
254
        goto cleanup;
255

256
    if (virStrToLong_i(ret, &char_ptr, 10, &id) == -1)
257
        goto cleanup;
258

259
  cleanup:
260 261
    VIR_FREE(cmd);
    VIR_FREE(ret);
262

263
    return id;
264
}
265

266 267 268 269 270 271
static virCapsPtr
phypCapsInit(void)
{
    struct utsname utsname;
    virCapsPtr caps;
    virCapsGuestPtr guest;
272

273
    uname(&utsname);
274

275 276
    if ((caps = virCapabilitiesNew(utsname.machine, 0, 0)) == NULL)
        goto no_memory;
277

278 279 280 281 282 283 284 285
    /* 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);
        VIR_WARN0
            ("Failed to query host NUMA topology, disabling NUMA capabilities");
286 287
    }

288 289 290
    /* XXX shouldn't 'borrow' KVM's prefix */
    virCapabilitiesSetMacPrefix(caps, (unsigned char[]) {
                                0x52, 0x54, 0x00});
291

292 293 294 295 296 297
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "linux",
                                         utsname.machine,
                                         sizeof(int) == 4 ? 32 : 8,
                                         NULL, NULL, 0, NULL)) == NULL)
        goto no_memory;
298

299 300 301
    if (virCapabilitiesAddGuestDomain(guest,
                                      "phyp", NULL, NULL, 0, NULL) == NULL)
        goto no_memory;
302

303
    return caps;
304

305 306 307 308
  no_memory:
    virCapabilitiesFree(caps);
    return NULL;
}
309

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
/* 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;
    int exit_status = 0;
326
    int ndom = -1;
327 328 329 330 331 332
    char *char_ptr;
    char *cmd = NULL;
    char *ret = NULL;
    char *managed_system = phyp_driver->managed_system;
    const char *state;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
333

334 335 336 337 338 339 340
    if (type == 0)
        state = "|grep Running";
    else if (type == 1) {
        if (system_type == HMC) {
            state = "|grep \"Not Activated\"";
        } else {
            state = "|grep \"Open Firmware\"";
341
        }
342 343
    } else
        state = " ";
344

345 346 347 348 349 350 351 352 353 354 355
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -F lpar_id,state %s |grep -c '^[0-9]*'",
                      state);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);
356

357
    ret = phypExec(session, cmd, &exit_status, conn);
358

359
    if (exit_status < 0 || ret == NULL)
360
        goto cleanup;
361

362
    if (virStrToLong_i(ret, &char_ptr, 10, &ndom) == -1)
363
        goto cleanup;
364

365
  cleanup:
366 367
    VIR_FREE(cmd);
    VIR_FREE(ret);
368

369
    return ndom;
370 371
}

372 373 374 375 376 377 378 379 380 381
/* 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)
382
{
383
    ConnectionData *connection_data = conn->networkPrivateData;
E
Eduardo Otubo 已提交
384
    phyp_driverPtr phyp_driver = conn->privateData;
385
    LIBSSH2_SESSION *session = connection_data->session;
E
Eduardo Otubo 已提交
386
    int system_type = phyp_driver->system_type;
387
    char *managed_system = phyp_driver->managed_system;
388
    int exit_status = 0;
389
    int got = -1;
390 391
    char *cmd = NULL;
    char *ret = NULL;
392
    char *line, *next_line;
393
    const char *state;
E
Eduardo Otubo 已提交
394 395
    virBuffer buf = VIR_BUFFER_INITIALIZER;

396 397 398 399 400
    if (type == 0)
        state = "|grep Running";
    else
        state = " ";

E
Eduardo Otubo 已提交
401 402 403
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
404 405
    virBufferVSprintf(&buf, " -F lpar_id,state %s | sed -e 's/,.*$//'",
                      state);
E
Eduardo Otubo 已提交
406 407
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
408
        virReportOOMError();
E
Eduardo Otubo 已提交
409
        return -1;
410
    }
E
Eduardo Otubo 已提交
411
    cmd = virBufferContentAndReset(&buf);
412

413
    ret = phypExec(session, cmd, &exit_status, conn);
414

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

    /* I need to parse the textual return in order to get the ids */
    line = ret;
    got = 0;
    while (*line && got < nids) {
        if (virStrToLong_i(line, &next_line, 10, &ids[got]) == -1) {
            VIR_ERROR(_("Cannot parse number from '%s'"), line);
            got = -1;
425
            goto cleanup;
426
        }
427 428 429 430
        got++;
        line = next_line;
        while (*line == '\n')
            line++; /* skip \n */
431
    }
432

433
  cleanup:
434
    VIR_FREE(cmd);
435
    VIR_FREE(ret);
436

437
    return got;
438 439
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

603
    return 0;
604 605

  err:
606
    return -1;
607 608
}

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

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

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

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

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

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

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

638
    return 0;
639 640

  err:
641
    return -1;
642 643
}

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

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

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

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

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

686
    VIR_FORCE_CLOSE(fd);
687
    return 0;
688 689

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

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

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

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

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

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

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

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

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

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

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

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

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

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

785 786 787 788 789 790 791 792 793 794
  exit:
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
    virBufferFreeAndReset(&username);
    return 0;
795 796

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

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

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

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

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

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

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

845 846 847
    /* try to get the table from server */
    if (phypUUIDTable_Pull(conn) == -1) {
        /* file not found in the server, creating a new one */
E
Eric Blake 已提交
848
        table_created = true;
849 850 851 852
        if (VIR_ALLOC_N(uuid_table->lpars, uuid_table->nlpars) >= 0) {
            for (i = 0; i < uuid_table->nlpars; i++) {
                if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
                    virReportOOMError();
E
Eric Blake 已提交
853
                    goto cleanup;
854 855
                }
                uuid_table->lpars[i]->id = ids[i];
856

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

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

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

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

E
Eric Blake 已提交
878 879 880 881 882 883 884
cleanup:
    if (ret < 0 && table_created) {
        for (i = 0; i < uuid_table->nlpars; i++) {
            VIR_FREE(uuid_table->lpars[i]);
        }
        VIR_FREE(uuid_table->lpars);
    }
885
    VIR_FREE(ids);
E
Eric Blake 已提交
886
    return ret;
887 888
}

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

894 895 896 897 898 899 900 901
    if (uuid_table == NULL)
        return;

    for (i = 0; i < uuid_table->nlpars; i++)
        VIR_FREE(uuid_table->lpars[i]);

    VIR_FREE(uuid_table->lpars);
    VIR_FREE(uuid_table);
902 903
}

904 905 906 907 908 909 910 911
#define SPECIALCHARACTER_CASES                                                \
    case '&': case ';': case '`': case '@': case '"': case '|': case '*':     \
    case '?': case '~': case '<': case '>': case '^': case '(': case ')':     \
    case '[': case ']': case '{': case '}': case '$': case '%': case '#':     \
    case '\\': case '\n': case '\r': case '\t':

static bool
contains_specialcharacters(const char *src)
912
{
913
    size_t len = strlen(src);
914 915
    size_t i = 0;

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

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

928 929
    return false;
}
930

931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
static char *
escape_specialcharacters(const char *src)
{
    size_t len = strlen(src);
    size_t i = 0, j = 0;
    char *dst;

    if (len == 0)
        return NULL;

    if (VIR_ALLOC_N(dst, len + 1) < 0) {
        virReportOOMError();
        return NULL;
    }

    for (i = 0; i < len; i++) {
        switch (src[i]) {
        SPECIALCHARACTER_CASES
            continue;
        default:
            dst[j] = src[i];
            j++;
        }
    }

    dst[j] = '\0';

    return dst;
959 960
}

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

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

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

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

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

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

1006
        username = virRequestUsername(auth, NULL, conn->uri->server);
1007

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

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

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

1027 1028 1029 1030 1031 1032 1033
    cur = ai;
    while (cur != NULL) {
        sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
        if (sock >= 0) {
            if (connect(sock, cur->ai_addr, cur->ai_addrlen) == 0) {
                goto connected;
            }
1034
            VIR_FORCE_CLOSE(sock);
1035 1036 1037
        }
        cur = cur->ai_next;
    }
1038

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

1044
  connected:
1045

1046
    (*internal_socket) = sock;
1047

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

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

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

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

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

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

1087
        password = virRequestPassword(auth, username, conn->uri->server);
1088

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

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

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

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

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

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

  exit:
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
    VIR_FREE(username);
    VIR_FREE(password);
    return session;
1133 1134
}

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
static virDrvOpenStatus
phypOpen(virConnectPtr conn,
         virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
{
    LIBSSH2_SESSION *session = NULL;
    ConnectionData *connection_data = NULL;
    int internal_socket;
    uuid_tablePtr uuid_table = NULL;
    phyp_driverPtr phyp_driver = NULL;
    char *char_ptr;
    char *managed_system = NULL;
E
Eduardo Otubo 已提交
1146

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

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

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

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

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

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

1194
        if (contains_specialcharacters(conn->uri->path)) {
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
            PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
                       "%s",
                       _("Error parsing 'path'. Invalid characters."));
            goto failure;
        }
    }

    if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) {
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
                   "%s", _("Error while opening SSH session."));
        goto failure;
    }

    connection_data->session = session;

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

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

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

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

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

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

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
    if (phyp_driver->system_type == HMC) {
        if ((phyp_driver->vios_id = phypGetVIOSPartitionID(conn)) == -1)
            goto failure;
    }

    return VIR_DRV_OPEN_SUCCESS;

  failure:
    if (phyp_driver != NULL) {
        virCapabilitiesFree(phyp_driver->caps);
        VIR_FREE(phyp_driver->managed_system);
        VIR_FREE(phyp_driver);
    }

    phypUUIDTable_Free(uuid_table);

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

    VIR_FREE(connection_data);

    return VIR_DRV_OPEN_ERROR;
1255 1256 1257
}

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

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

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


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

1283 1284 1285 1286 1287 1288

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

1291 1292 1293 1294 1295
static int
phypIsUpdated(virDomainPtr conn ATTRIBUTE_UNUSED)
{
    return 0;
}
1296 1297

/* return the lpar_id given a name and a managed system name */
1298
static int
1299 1300
phypGetLparID(LIBSSH2_SESSION * session, const char *managed_system,
              const char *name, virConnectPtr conn)
1301
{
1302
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1303
    int system_type = phyp_driver->system_type;
1304
    int exit_status = 0;
1305
    int lpar_id = -1;
1306
    char *char_ptr;
1307 1308
    char *cmd = NULL;
    char *ret = NULL;
E
Eduardo Otubo 已提交
1309 1310
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1311
    virBufferAddLit(&buf, "lssyscfg -r lpar");
E
Eduardo Otubo 已提交
1312 1313
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
1314
    virBufferVSprintf(&buf, " --filter lpar_names=%s -F lpar_id", name);
E
Eduardo Otubo 已提交
1315 1316
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
1317
        virReportOOMError();
E
Eduardo Otubo 已提交
1318
        return -1;
1319
    }
E
Eduardo Otubo 已提交
1320
    cmd = virBufferContentAndReset(&buf);
1321

1322
    ret = phypExec(session, cmd, &exit_status, conn);
1323

1324
    if (exit_status < 0 || ret == NULL)
1325
        goto cleanup;
1326

1327
    if (virStrToLong_i(ret, &char_ptr, 10, &lpar_id) == -1)
1328
        goto cleanup;
1329

1330
  cleanup:
1331 1332 1333
    VIR_FREE(cmd);
    VIR_FREE(ret);

1334
    return lpar_id;
1335 1336
}

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

1350 1351 1352 1353 1354 1355 1356 1357
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " --filter lpar_ids=%d -F name", lpar_id);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return NULL;
1358
    }
1359
    cmd = virBufferContentAndReset(&buf);
1360

1361
    ret = phypExec(session, cmd, &exit_status, conn);
1362

1363 1364 1365 1366
    if (exit_status < 0 || ret == NULL) {
        VIR_FREE(ret);
        goto cleanup;
    }
1367

1368
    char_ptr = strchr(ret, '\n');
1369

1370 1371
    if (char_ptr)
        *char_ptr = '\0';
1372

1373
  cleanup:
1374
    VIR_FREE(cmd);
1375

1376
    return ret;
1377 1378
}

1379 1380 1381 1382 1383 1384 1385 1386 1387

/* 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)
1388 1389
{
    phyp_driverPtr phyp_driver = conn->privateData;
1390 1391 1392
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    lparPtr *lpars = uuid_table->lpars;
    unsigned int i = 0;
1393

1394 1395 1396 1397 1398 1399
    for (i = 0; i < uuid_table->nlpars; i++) {
        if (lpars[i]->id == lpar_id) {
            memmove(uuid, lpars[i]->uuid, VIR_UUID_BUFLEN);
            return 0;
        }
    }
1400

1401
    return -1;
1402 1403
}

1404 1405 1406 1407 1408 1409 1410 1411
/*
 * type:
 * 0 - maxmem
 * 1 - memory
 * */
static unsigned long
phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id,
               int type)
1412
{
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
    phyp_driverPtr phyp_driver = conn->privateData;
    int system_type = phyp_driver->system_type;
    char *cmd = NULL;
    char *ret = NULL;
    char *char_ptr;
    int memory = 0;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1423

1424 1425
    if (type != 1 && type != 0)
        return 0;
1426

1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf,
                      " -r mem --level lpar -F %s --filter lpar_ids=%d",
                      type ? "curr_mem" : "curr_max_mem", lpar_id);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return 0;
1437
    }
1438
    cmd = virBufferContentAndReset(&buf);
1439

1440
    ret = phypExec(session, cmd, &exit_status, conn);
1441

1442
    if (exit_status < 0 || ret == NULL)
1443
        goto cleanup;
1444

1445
    char_ptr = strchr(ret, '\n');
1446

1447 1448 1449 1450
    if (char_ptr)
        *char_ptr = '\0';

    if (virStrToLong_i(ret, &char_ptr, 10, &memory) == -1)
1451
        goto cleanup;
1452

1453
  cleanup:
1454 1455
    VIR_FREE(cmd);
    VIR_FREE(ret);
1456

1457
    return memory;
1458 1459
}

1460 1461 1462
static unsigned long
phypGetLparCPUGeneric(virConnectPtr conn, const char *managed_system,
                      int lpar_id, int type)
1463
{
1464
    ConnectionData *connection_data = conn->networkPrivateData;
1465
    LIBSSH2_SESSION *session = connection_data->session;
1466
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1467
    int system_type = phyp_driver->system_type;
1468 1469
    char *cmd = NULL;
    char *ret = NULL;
1470 1471 1472
    char *char_ptr;
    int exit_status = 0;
    int vcpus = 0;
E
Eduardo Otubo 已提交
1473
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1474

1475
    virBufferAddLit(&buf, "lshwres");
E
Eduardo Otubo 已提交
1476 1477
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
1478 1479 1480
    virBufferVSprintf(&buf,
                      " -r proc --level lpar -F %s --filter lpar_ids=%d",
                      type ? "curr_max_procs" : "curr_procs", lpar_id);
E
Eduardo Otubo 已提交
1481 1482
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
1483
        virReportOOMError();
E
Eduardo Otubo 已提交
1484
        return 0;
1485
    }
E
Eduardo Otubo 已提交
1486
    cmd = virBufferContentAndReset(&buf);
1487

1488
    ret = phypExec(session, cmd, &exit_status, conn);
1489

1490
    if (exit_status < 0 || ret == NULL)
1491
        goto cleanup;
1492 1493 1494 1495 1496 1497 1498

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

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

    if (virStrToLong_i(ret, &char_ptr, 10, &vcpus) == -1)
1499
        goto cleanup;
1500

1501
  cleanup:
1502 1503
    VIR_FREE(cmd);
    VIR_FREE(ret);
1504

1505
    return vcpus;
1506
}
1507

1508 1509 1510 1511
static unsigned long
phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id)
{
    return phypGetLparCPUGeneric(conn, managed_system, lpar_id, 0);
1512 1513
}

1514
static int
1515
phypDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
1516 1517 1518
{
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    char *managed_system = phyp_driver->managed_system;
1519

1520 1521 1522 1523 1524
    if (flags != (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_MAXIMUM)) {
        PHYP_ERROR(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
        return -1;
    }

1525 1526 1527
    return phypGetLparCPUGeneric(dom->conn, managed_system, dom->id, 1);
}

1528 1529 1530 1531 1532 1533 1534
static int
phypGetLparCPUMAX(virDomainPtr dom)
{
    return phypDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                         VIR_DOMAIN_VCPU_MAXIMUM));
}

1535 1536 1537
static int
phypGetRemoteSlot(virConnectPtr conn, const char *managed_system,
                  const char *lpar_name)
1538
{
1539 1540
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
1541
    phyp_driverPtr phyp_driver = conn->privateData;
E
Eduardo Otubo 已提交
1542
    int system_type = phyp_driver->system_type;
1543 1544
    char *cmd = NULL;
    char *ret = NULL;
1545
    char *char_ptr;
1546
    int remote_slot = -1;
1547
    int exit_status = 0;
E
Eduardo Otubo 已提交
1548 1549
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1550
    virBufferAddLit(&buf, "lshwres");
E
Eduardo Otubo 已提交
1551 1552
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
1553 1554
    virBufferVSprintf(&buf, " -r virtualio --rsubtype scsi -F "
                      "remote_slot_num --filter lpar_names=%s", lpar_name);
E
Eduardo Otubo 已提交
1555 1556
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
1557
        virReportOOMError();
E
Eduardo Otubo 已提交
1558
        return -1;
1559
    }
E
Eduardo Otubo 已提交
1560
    cmd = virBufferContentAndReset(&buf);
1561

1562
    ret = phypExec(session, cmd, &exit_status, conn);
1563

1564
    if (exit_status < 0 || ret == NULL)
1565
        goto cleanup;
1566

1567 1568 1569 1570 1571 1572
    char_ptr = strchr(ret, '\n');

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

    if (virStrToLong_i(ret, &char_ptr, 10, &remote_slot) == -1)
1573
        goto cleanup;
1574

1575
  cleanup:
1576 1577 1578
    VIR_FREE(cmd);
    VIR_FREE(ret);

1579
    return remote_slot;
1580 1581
}

1582 1583 1584 1585 1586 1587
/* 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)
1588
{
1589 1590
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
1591
    phyp_driverPtr phyp_driver = conn->privateData;
1592 1593 1594 1595 1596 1597 1598 1599
    int system_type = phyp_driver->system_type;
    char *cmd = NULL;
    char *ret = NULL;
    int remote_slot = 0;
    int exit_status = 0;
    char *char_ptr;
    char *backing_device = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1600

1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
    if ((remote_slot =
         phypGetRemoteSlot(conn, managed_system, lpar_name)) == -1)
        return NULL;

    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -r virtualio --rsubtype scsi -F "
                      "backing_devices --filter slots=%d", remote_slot);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return NULL;
1614
    }
1615
    cmd = virBufferContentAndReset(&buf);
1616

1617
    ret = phypExec(session, cmd, &exit_status, conn);
1618

1619
    if (exit_status < 0 || ret == NULL)
1620
        goto cleanup;
1621

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
    /* 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
1636
            goto cleanup;
1637 1638 1639 1640 1641

        backing_device = strdup(char_ptr);

        if (backing_device == NULL) {
            virReportOOMError();
1642
            goto cleanup;
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
        }
    } else {
        backing_device = ret;
        ret = NULL;
    }

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

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

1654
  cleanup:
1655 1656
    VIR_FREE(cmd);
    VIR_FREE(ret);
1657

1658
    return backing_device;
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
}

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 *cmd = NULL;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1673
    char *char_ptr;
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689

    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf,
                      " -r prof --filter lpar_ids=%d -F name|head -n 1",
                      lpar_id);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return NULL;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

1690 1691 1692 1693
    if (exit_status < 0 || ret == NULL) {
        VIR_FREE(ret);
        goto cleanup;
    }
1694

1695
    char_ptr = strchr(ret, '\n');
1696 1697 1698 1699

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

1700
  cleanup:
1701 1702
    VIR_FREE(cmd);

1703
    return ret;
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
}

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;
    int exit_status = 0;
    char *char_ptr;
    char *cmd = NULL;
    char *ret = NULL;
    char *profile = NULL;
1720
    int slot = -1;
1721 1722 1723 1724
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!(profile = phypGetLparProfile(conn, vios_id))) {
        VIR_ERROR0(_("Unable to get VIOS profile name."));
1725
        return -1;
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
    }

    virBufferAddLit(&buf, "lssyscfg");

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

    virBufferVSprintf(&buf, " -r prof --filter "
                      "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);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
1743
        goto cleanup;
1744 1745 1746 1747 1748 1749 1750
    }

    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
1751
        goto cleanup;
1752 1753

    if (virStrToLong_i(ret, &char_ptr, 10, &slot) == -1)
1754
        goto cleanup;
1755

1756
    slot += 1;
1757

1758 1759
  cleanup:
    VIR_FREE(profile);
1760 1761
    VIR_FREE(cmd);
    VIR_FREE(ret);
1762 1763

    return slot;
1764 1765 1766 1767 1768
}

static int
phypCreateServerSCSIAdapter(virConnectPtr conn)
{
1769
    int result = -1;
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
    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 *cmd = NULL;
    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))) {
        VIR_ERROR0(_("Unable to get VIOS name"));
1788
        goto cleanup;
1789 1790 1791 1792
    }

    if (!(profile = phypGetLparProfile(conn, vios_id))) {
        VIR_ERROR0(_("Unable to get VIOS profile name."));
1793
        goto cleanup;
1794 1795 1796 1797
    }

    if ((slot = phypGetVIOSNextSlotNumber(conn)) == -1) {
        VIR_ERROR0(_("Unable to get free slot number"));
1798
        goto cleanup;
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
    }

    /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
     * be appended to this list
     * */
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -r prof --filter lpar_ids=%d,profile_names=%s"
                      " -F virtual_scsi_adapters|sed -e s/\\\"//g",
                      vios_id, profile);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
1813
        goto cleanup;
1814 1815 1816 1817 1818 1819
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
1820
        goto cleanup;
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833

    /* 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)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -r prof -i 'name=%s,lpar_id=%d,"
                      "\"virtual_scsi_adapters=%s,%d/server/any/any/1\"'",
                      vios_name, vios_id, ret, slot);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
1834
        goto cleanup;
1835 1836
    }

1837 1838 1839 1840
    VIR_FREE(cmd);
    VIR_FREE(ret);

    cmd = virBufferContentAndReset(&buf);
1841 1842 1843
    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
1844
        goto cleanup;
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857

    /* 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)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf,
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      vios_name, slot);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
1858
        goto cleanup;
1859 1860
    }

1861 1862 1863 1864
    VIR_FREE(cmd);
    VIR_FREE(ret);

    cmd = virBufferContentAndReset(&buf);
1865 1866 1867
    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
1868
        goto cleanup;
1869

1870
    result = 0;
1871

1872
  cleanup:
1873 1874 1875 1876
    VIR_FREE(profile);
    VIR_FREE(vios_name);
    VIR_FREE(cmd);
    VIR_FREE(ret);
1877 1878

    return result;
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
}

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 *cmd = NULL;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1894
    char *char_ptr;
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904

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

    virBufferVSprintf(&buf, "lsmap -all -field svsa backing -fmt , ");

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

E
Eric Blake 已提交
1905
    virBufferVSprintf(&buf, "|sed '/,[^.*]/d; s/,//g; q'");
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return NULL;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

1916 1917 1918 1919
    if (exit_status < 0 || ret == NULL) {
        VIR_FREE(ret);
        goto cleanup;
    }
1920

1921
    char_ptr = strchr(ret, '\n');
1922 1923 1924 1925

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

1926
  cleanup:
1927 1928
    VIR_FREE(cmd);

1929
    return ret;
1930 1931 1932 1933 1934 1935
}


static int
phypAttachDevice(virDomainPtr domain, const char *xml)
{
1936
    int result = -1;
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
    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 *char_ptr = NULL;
    char *cmd = NULL;
    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;

1957
    domain_name = escape_specialcharacters(domain->name);
1958

1959
    if (domain_name == NULL) {
1960
        goto cleanup;
1961 1962 1963 1964 1965 1966
    }

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

    if (def->os.type == NULL) {
        virReportOOMError();
1967
        goto cleanup;
1968 1969 1970 1971 1972
    }

    dev = virDomainDeviceDefParse(phyp_driver->caps, def, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
    if (!dev) {
1973
        goto cleanup;
1974 1975 1976 1977 1978 1979
    }

    if (!
        (vios_name =
         phypGetLparNAME(session, managed_system, vios_id, conn))) {
        VIR_ERROR0(_("Unable to get VIOS name"));
1980
        goto cleanup;
1981 1982 1983 1984 1985 1986 1987 1988 1989
    }

    /* 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) {
            VIR_ERROR0(_("Unable to create new virtual adapter"));
1990
            goto cleanup;
1991 1992 1993
        } else {
            if (!(scsi_adapter = phypGetVIOSFreeSCSIAdapter(conn))) {
                VIR_ERROR0(_("Unable to create new virtual adapter"));
1994
                goto cleanup;
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
            }
        }
    }

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

    virBufferVSprintf(&buf, "mkvdev -vdev %s -vadapter %s",
                      dev->data.disk->src, scsi_adapter);

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

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
2012
        goto cleanup;
2013 2014
    }

2015 2016 2017 2018
    VIR_FREE(cmd);
    VIR_FREE(ret);

    cmd = virBufferContentAndReset(&buf);
2019 2020 2021
    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
2022
        goto cleanup;
2023 2024 2025

    if (!(profile = phypGetLparProfile(conn, domain->id))) {
        VIR_ERROR0(_("Unable to get VIOS profile name."));
2026
        goto cleanup;
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
    }

    /* Let's get the slot number for the adapter we just created
     * */
    virBufferAddLit(&buf, "lshwres -r virtualio --rsubtype scsi");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf,
                      " slot_num,backing_device|grep %s|cut -d, -f1",
                      dev->data.disk->src);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
2040
        goto cleanup;
2041 2042
    }

2043 2044 2045 2046
    VIR_FREE(cmd);
    VIR_FREE(ret);

    cmd = virBufferContentAndReset(&buf);
2047 2048 2049
    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
2050
        goto cleanup;
2051 2052

    if (virStrToLong_i(ret, &char_ptr, 10, &slot) == -1)
2053
        goto cleanup;
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067

    /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
     * be appended to this list
     * */
    virBufferAddLit(&buf, "lssyscfg");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf,
                      " -r prof --filter lpar_ids=%d,profile_names=%s"
                      " -F virtual_scsi_adapters|sed -e 's/\"//g'",
                      vios_id, profile);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
2068
        goto cleanup;
2069 2070
    }

2071 2072 2073 2074
    VIR_FREE(cmd);
    VIR_FREE(ret);

    cmd = virBufferContentAndReset(&buf);
2075 2076 2077
    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
2078
        goto cleanup;
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093

    /* 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)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf,
                      " -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);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
2094
        goto cleanup;
2095 2096
    }

2097 2098 2099 2100
    VIR_FREE(cmd);
    VIR_FREE(ret);

    cmd = virBufferContentAndReset(&buf);
2101 2102 2103
    ret = phypExec(session, cmd, &exit_status, conn);

    if (virStrToLong_i(ret, &char_ptr, 10, &slot) == -1)
2104
        goto cleanup;
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117

    /* 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)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf,
                      " -p %s -o a -s %d -d 0 -a \"adapter_type=server\"",
                      domain_name, slot);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
2118
        goto cleanup;
2119 2120
    }

2121 2122 2123 2124
    VIR_FREE(cmd);
    VIR_FREE(ret);

    cmd = virBufferContentAndReset(&buf);
2125 2126 2127 2128 2129 2130
    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL) {
        VIR_ERROR0(_
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    "Contact your support to enable this feature."));
2131
        goto cleanup;
2132 2133
    }

2134
    result = 0;
2135

2136
  cleanup:
2137 2138
    VIR_FREE(cmd);
    VIR_FREE(ret);
2139 2140
    virDomainDeviceDefFree(dev);
    virDomainDefFree(def);
2141 2142
    VIR_FREE(vios_name);
    VIR_FREE(scsi_adapter);
2143 2144 2145 2146
    VIR_FREE(profile);
    VIR_FREE(domain_name);

    return result;
2147 2148
}

2149 2150
static char *
phypVolumeGetKey(virConnectPtr conn, const char *name)
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
{
    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 *cmd = NULL;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2162
    char *char_ptr;
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177

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

    virBufferVSprintf(&buf, "lslv %s -field lvid", name);

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

    virBufferVSprintf(&buf, "|sed -e 's/^LV IDENTIFIER://' -e 's/ //g'");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
2178
        return NULL;
2179 2180
    }

2181
    cmd = virBufferContentAndReset(&buf);
2182 2183
    ret = phypExec(session, cmd, &exit_status, conn);

2184 2185 2186 2187
    if (exit_status < 0 || ret == NULL) {
        VIR_FREE(ret);
        goto cleanup;
    }
2188

2189
    char_ptr = strchr(ret, '\n');
2190 2191 2192 2193

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

2194 2195 2196 2197
  cleanup:
    VIR_FREE(cmd);

    return ret;
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
}

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 *cmd = NULL;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2213
    char *char_ptr;
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234

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

    virBufferVSprintf(&buf, "lssp -detail -sp %s -field name", name);

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

    virBufferVSprintf(&buf, "|sed '1d; s/ //g'");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return NULL;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

2235 2236 2237 2238
    if (exit_status < 0 || ret == NULL) {
        VIR_FREE(ret);
        goto cleanup;
    }
2239

2240
    char_ptr = strchr(ret, '\n');
2241 2242 2243 2244

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

2245
  cleanup:
2246 2247
    VIR_FREE(cmd);

2248
    return ret;
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
}

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 exit_status = 0;
    int vios_id = phyp_driver->vios_id;
    char *cmd = NULL;
    char *ret = NULL;
2263
    int sp_size = -1;
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
    char *char_ptr;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

    virBufferVSprintf(&buf, "lssp -detail -sp %s -field size", name);

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

    virBufferVSprintf(&buf, "|sed '1d; s/ //g'");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
2288
        goto cleanup;
2289 2290

    if (virStrToLong_i(ret, &char_ptr, 10, &sp_size) == -1)
2291
        goto cleanup;
2292

2293
  cleanup:
2294 2295 2296
    VIR_FREE(cmd);
    VIR_FREE(ret);

2297
    return sp_size;
2298 2299
}

2300
static char *
2301
phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
2302
                unsigned int capacity)
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
{
    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 *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2314
    char *key = NULL;
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327

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

    virBufferVSprintf(&buf, "mklv -lv %s %s %d", lvname, spname, capacity);

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

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
2328
        return NULL;
2329 2330
    }

2331
    cmd = virBufferContentAndReset(&buf);
2332 2333 2334 2335
    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0) {
        VIR_ERROR(_("Unable to create Volume: %s"), ret);
2336
        goto cleanup;
2337 2338
    }

2339 2340 2341
    key = phypVolumeGetKey(conn, lvname);

    if (key == NULL)
2342
        goto cleanup;
2343

2344
  cleanup:
2345 2346 2347
    VIR_FREE(cmd);
    VIR_FREE(ret);

2348
    return key;
2349 2350 2351 2352 2353
}

static virStorageVolPtr
phypVolumeLookupByName(virStoragePoolPtr pool, const char *volname)
{
2354 2355
    char *key;
    virStorageVolPtr vol;
2356

2357
    key = phypVolumeGetKey(pool->conn, volname);
2358

2359
    if (key == NULL)
2360 2361
        return NULL;

2362 2363 2364 2365 2366
    vol = virGetStorageVol(pool->conn, pool->name, volname, key);

    VIR_FREE(key);

    return vol;
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441
}

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

    virStorageVolDefPtr voldef = NULL;
    virStoragePoolDefPtr spdef = NULL;
    virStorageVolPtr vol = NULL;
    char *key = NULL;

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

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

    if (memcpy(spdef->uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
        VIR_ERROR0(_("Unable to determine storage pool's uuid."));
        goto err;
    }

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

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

    spdef->source.ndevice = 1;

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

    if ((voldef = virStorageVolDefParseString(spdef, xml)) == NULL) {
        VIR_ERROR0(_("Error parsing volume XML."));
        goto err;
    }

    /* checking if this name already exists on this system */
    if (phypVolumeLookupByName(pool, voldef->name) != NULL) {
        VIR_ERROR0(_("StoragePool name already exists."));
        goto err;
    }

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

    if (voldef->capacity) {
        VIR_ERROR0(_("Capacity cannot be empty."));
        goto err;
    }

2442 2443 2444 2445
    key = phypBuildVolume(pool->conn, voldef->name, spdef->name,
                          voldef->capacity);

    if (key == NULL)
2446 2447 2448 2449 2450 2451 2452
        goto err;

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

2453 2454
    VIR_FREE(key);

2455 2456 2457
    return vol;

  err:
2458
    VIR_FREE(key);
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479
    virStorageVolDefFree(voldef);
    virStoragePoolDefFree(spdef);
    if (vol)
        virUnrefStorageVol(vol);
    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 *cmd = NULL;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2480
    char *char_ptr;
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501

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

    virBufferVSprintf(&buf, "lssp -detail -sp %s -field pvname", sp);

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

    virBufferVSprintf(&buf, "|sed 1d");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return NULL;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

2502 2503 2504 2505
    if (exit_status < 0 || ret == NULL) {
        VIR_FREE(ret);
        goto cleanup;
    }
2506

2507
    char_ptr = strchr(ret, '\n');
2508 2509 2510 2511

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

2512
  cleanup:
2513 2514
    VIR_FREE(cmd);

2515
    return ret;
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529
}

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;
    char *cmd = NULL;
    char *spname = NULL;
2530
    char *char_ptr;
2531 2532
    char *key = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2533
    virStorageVolPtr vol = NULL;
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555

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

    virBufferVSprintf(&buf, "lslv %s -field vgname", volname);

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

    virBufferVSprintf(&buf, "|sed -e 's/^VOLUME GROUP://g' -e 's/ //g'");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return NULL;
    }
    cmd = virBufferContentAndReset(&buf);

    spname = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || spname == NULL)
2556
        goto cleanup;
2557

2558
    char_ptr = strchr(spname, '\n');
2559 2560 2561 2562

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

2563
    key = phypVolumeGetKey(conn, volname);
2564

2565
    if (key == NULL)
2566
        goto cleanup;
2567

2568 2569
    vol = virGetStorageVol(conn, spname, volname, key);

2570 2571 2572
  cleanup:
    VIR_FREE(cmd);
    VIR_FREE(spname);
2573 2574 2575
    VIR_FREE(key);

    return vol;
2576 2577 2578 2579 2580 2581
}

static int
phypGetStoragePoolUUID(virConnectPtr conn, unsigned char *uuid,
                       const char *name)
{
2582
    int result = -1;
2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614
    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 *cmd = NULL;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

    virBufferVSprintf(&buf, "lsdev -dev %s -attr vgserial_id", name);

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

    virBufferVSprintf(&buf, "|sed '1,2d'");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
2615
        goto cleanup;
2616 2617

    if (memmove(uuid, ret, VIR_UUID_BUFLEN) == NULL)
2618
        goto cleanup;
2619

2620
    result = 0;
2621

2622
  cleanup:
2623 2624
    VIR_FREE(cmd);
    VIR_FREE(ret);
2625 2626

    return result;
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
}

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

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

    return virGetStoragePool(conn, name, uuid);
}

static char *
phypVolumeGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
{
2643 2644 2645
    virStorageVolDef voldef;
    virStoragePoolDef pool;
    virStoragePoolPtr sp;
2646 2647
    char *xml;

2648 2649 2650
    virCheckFlags(0, NULL);

    memset(&voldef, 0, sizeof(virStorageVolDef));
2651
    memset(&pool, 0, sizeof(virStoragePoolDef));
2652

2653
    sp = phypStoragePoolLookupByName(vol->conn, vol->pool);
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693

    if (!sp)
        goto err;

    if (sp->name != NULL) {
        pool.name = sp->name;
    } else {
        VIR_ERROR0(_("Unable to determine storage sp's name."));
        goto err;
    }

    if (memmove(pool.uuid, sp->uuid, VIR_UUID_BUFLEN) == NULL) {
        VIR_ERROR0(_("Unable to determine storage sp's uuid."));
        goto err;
    }

    if ((pool.capacity = phypGetStoragePoolSize(sp->conn, sp->name)) == -1) {
        VIR_ERROR0(_("Unable to determine storage sps's size."));
        goto err;
    }

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

    pool.source.ndevice = 1;

    if ((pool.source.adapter =
         phypGetStoragePoolDevice(sp->conn, sp->name)) == NULL) {
        VIR_ERROR0(_("Unable to determine storage sps's source adapter."));
        goto err;
    }

    if (vol->name != NULL)
        voldef.name = vol->name;
    else {
        VIR_ERROR0(_("Unable to determine storage pool's name."));
        goto err;
    }

2694 2695 2696 2697
    voldef.key = strdup(vol->key);

    if (voldef.key == NULL) {
        virReportOOMError();
2698 2699 2700 2701 2702
        goto err;
    }

    voldef.type = VIR_STORAGE_POOL_LOGICAL;

2703 2704 2705 2706 2707
    xml = virStorageVolDefFormat(&pool, &voldef);

    VIR_FREE(voldef.key);

    return xml;
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735

  err:
    return NULL;
}

/* 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;
    char *cmd = NULL;
    char *sp = NULL;
    char *path = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2736 2737
    char *char_ptr;
    char *pv;
2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760

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

    virBufferVSprintf(&buf, "lslv %s -field vgname", vol->name);

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

    virBufferVSprintf(&buf,
                      "|sed -e 's/^VOLUME GROUP://g' -e 's/ //g'");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return NULL;
    }
    cmd = virBufferContentAndReset(&buf);

    sp = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || sp == NULL)
2761
        goto cleanup;
2762

2763
    char_ptr = strchr(sp, '\n');
2764 2765 2766 2767

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

2768
    pv = phypVolumeGetPhysicalVolumeByStoragePool(vol, sp);
2769

2770 2771
    if (!pv)
        goto cleanup;
2772

2773 2774 2775 2776
    if (virAsprintf(&path, "/%s/%s/%s", pv, sp, vol->name) < 0) {
        virReportOOMError();
        goto cleanup;
    }
2777

2778
  cleanup:
2779 2780 2781
    VIR_FREE(cmd);
    VIR_FREE(sp);
    VIR_FREE(path);
2782 2783

    return path;
2784 2785 2786 2787 2788 2789 2790

}

static int
phypStoragePoolListVolumes(virStoragePoolPtr pool, char **const volumes,
                           int nvolumes)
{
2791
    bool success = false;
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
    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 *cmd = NULL;
    char *ret = NULL;
    char *volumes_list = NULL;
    char *char_ptr2 = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

    virBufferVSprintf(&buf, "lsvg -lv %s -field lvname", pool->name);

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

    virBufferVSprintf(&buf, "|sed '1,2d'");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    /* I need to parse the textual return in order to get the volumes */
    if (exit_status < 0 || ret == NULL)
2830
        goto cleanup;
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840
    else {
        volumes_list = ret;

        while (got < nvolumes) {
            char_ptr2 = strchr(volumes_list, '\n');

            if (char_ptr2) {
                *char_ptr2 = '\0';
                if ((volumes[got++] = strdup(volumes_list)) == NULL) {
                    virReportOOMError();
2841
                    goto cleanup;
2842 2843 2844 2845 2846 2847 2848 2849
                }
                char_ptr2++;
                volumes_list = char_ptr2;
            } else
                break;
        }
    }

2850 2851 2852 2853 2854 2855 2856 2857 2858
    success = true;

  cleanup:
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(volumes[i]);

        got = -1;
    }
2859 2860 2861

    VIR_FREE(cmd);
    VIR_FREE(ret);
2862 2863

    return got;
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
}

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;
    int exit_status = 0;
2875
    int nvolumes = -1;
2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900
    char *cmd = NULL;
    char *ret = NULL;
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    char *char_ptr;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (system_type == HMC)
        virBufferVSprintf(&buf, "viosvrcmd -m %s --id %d -c '",
                          managed_system, vios_id);
    virBufferVSprintf(&buf, "lsvg -lv %s -field lvname", pool->name);
    if (system_type == HMC)
        virBufferAddChar(&buf, '\'');
    virBufferVSprintf(&buf, "|grep -c '^.*$'");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
2901
        goto cleanup;
2902 2903

    if (virStrToLong_i(ret, &char_ptr, 10, &nvolumes) == -1)
2904
        goto cleanup;
2905 2906 2907 2908

    /* We need to remove 2 line from the header text output */
    nvolumes -= 2;

2909
  cleanup:
2910 2911 2912
    VIR_FREE(cmd);
    VIR_FREE(ret);

2913
    return nvolumes;
2914 2915 2916 2917 2918
}

static int
phypDestroyStoragePool(virStoragePoolPtr pool)
{
2919
    int result = -1;
2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
    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 *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

    virBufferVSprintf(&buf, "rmsp %s", pool->name);

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

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);
    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0) {
E
Eric Blake 已提交
2950
        VIR_ERROR(_("Unable to destroy Storage Pool: %s"), ret);
2951
        goto cleanup;
2952 2953
    }

2954
    result = 0;
2955

2956
  cleanup:
2957 2958
    VIR_FREE(cmd);
    VIR_FREE(ret);
2959 2960

    return result;
2961 2962 2963 2964 2965
}

static int
phypBuildStoragePool(virConnectPtr conn, virStoragePoolDefPtr def)
{
2966
    int result = -1;
2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
    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 *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

    virBufferVSprintf(&buf, "mksp -f %schild %s", def->name,
                      source.adapter);

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

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0) {
        VIR_ERROR(_("Unable to create Storage Pool: %s"), ret);
3000
        goto cleanup;
3001 3002
    }

3003
    result = 0;
3004

3005
  cleanup:
3006 3007
    VIR_FREE(cmd);
    VIR_FREE(ret);
3008 3009

    return result;
3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020

}

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;
    int exit_status = 0;
3021
    int nsp = -1;
3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049
    char *cmd = NULL;
    char *ret = NULL;
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    char *char_ptr;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

    virBufferVSprintf(&buf, "lsvg");

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

    virBufferVSprintf(&buf, "|grep -c '^.*$'");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
3050
        goto cleanup;
3051 3052

    if (virStrToLong_i(ret, &char_ptr, 10, &nsp) == -1)
3053
        goto cleanup;
3054

3055
  cleanup:
3056 3057 3058
    VIR_FREE(cmd);
    VIR_FREE(ret);

3059
    return nsp;
3060 3061 3062 3063 3064
}

static int
phypListStoragePools(virConnectPtr conn, char **const pools, int npools)
{
3065
    bool success = false;
3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100
    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 *cmd = NULL;
    char *ret = NULL;
    char *storage_pools = NULL;
    char *char_ptr2 = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

    virBufferVSprintf(&buf, "lsvg");

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

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    /* I need to parse the textual return in order to get the storage pools */
    if (exit_status < 0 || ret == NULL)
3101
        goto cleanup;
3102 3103 3104 3105 3106 3107 3108 3109 3110 3111
    else {
        storage_pools = ret;

        while (got < npools) {
            char_ptr2 = strchr(storage_pools, '\n');

            if (char_ptr2) {
                *char_ptr2 = '\0';
                if ((pools[got++] = strdup(storage_pools)) == NULL) {
                    virReportOOMError();
3112
                    goto cleanup;
3113 3114 3115 3116 3117 3118 3119 3120
                }
                char_ptr2++;
                storage_pools = char_ptr2;
            } else
                break;
        }
    }

3121 3122 3123 3124 3125 3126 3127 3128 3129
    success = true;

  cleanup:
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(pools[i]);

        got = -1;
    }
3130 3131 3132

    VIR_FREE(cmd);
    VIR_FREE(ret);
3133 3134

    return got;
3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217
}

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

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

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

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

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

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

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

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

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

  err:
    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;
    virStoragePoolPtr sp = NULL;

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

    /* checking if this name already exists on this system */
    if (phypStoragePoolLookupByName(conn, def->name) != NULL) {
        VIR_WARN0("StoragePool name already exists.");
        goto err;
    }

    /* checking if ID or UUID already exists on this system */
    if (phypGetStoragePoolLookUpByUUID(conn, def->uuid) != NULL) {
        VIR_WARN0("StoragePool uuid already exists.");
        goto err;
    }
3218

3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276
    if ((sp = virGetStoragePool(conn, def->name, def->uuid)) == NULL)
        goto err;

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

    return sp;

  err:
    virStoragePoolDefFree(def);
    if (sp)
        virUnrefStoragePool(sp);
    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 {
        VIR_ERROR0(_("Unable to determine storage pool's name."));
        goto err;
    }

    if (memmove(def.uuid, pool->uuid, VIR_UUID_BUFLEN) == NULL) {
        VIR_ERROR0(_("Unable to determine storage pool's uuid."));
        goto err;
    }

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

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

    def.source.ndevice = 1;

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

    return virStoragePoolDefFormat(&def);

  err:
    return NULL;
3277 3278
}

E
Eduardo Otubo 已提交
3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296
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 *char_ptr;
    char *cmd = NULL;
    char *ret = NULL;
E
Eric Blake 已提交
3297
    int rv = -1;
E
Eduardo Otubo 已提交
3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312

    /* Getting the remote slot number */

    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,slot_num|"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3313
        goto cleanup;
E
Eduardo Otubo 已提交
3314 3315 3316 3317 3318 3319
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, iface->conn);

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3320
        goto cleanup;
E
Eduardo Otubo 已提交
3321 3322

    if (virStrToLong_i(ret, &char_ptr, 10, &slot_num) == -1)
E
Eric Blake 已提交
3323
        goto cleanup;
E
Eduardo Otubo 已提交
3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340

    /* Getting the remote slot number */
    VIR_FREE(cmd);
    VIR_FREE(ret);

    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,lpar_id|"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3341
        goto cleanup;
E
Eduardo Otubo 已提交
3342 3343 3344 3345 3346 3347 3348 3349
    }
    cmd = virBufferContentAndReset(&buf);

    VIR_FREE(ret);

    ret = phypExec(session, cmd, &exit_status, iface->conn);

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3350
        goto cleanup;
E
Eduardo Otubo 已提交
3351 3352

    if (virStrToLong_i(ret, &char_ptr, 10, &lpar_id) == -1)
E
Eric Blake 已提交
3353
        goto cleanup;
E
Eduardo Otubo 已提交
3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369

    /* excluding interface */
    VIR_FREE(cmd);
    VIR_FREE(ret);

    virBufferAddLit(&buf, "chhwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      " -r virtualio --rsubtype eth"
                      " --id %d -o r -s %d", lpar_id, slot_num);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3370
        goto cleanup;
E
Eduardo Otubo 已提交
3371 3372 3373 3374 3375 3376
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, iface->conn);

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

E
Eric Blake 已提交
3379
    rv = 0;
E
Eduardo Otubo 已提交
3380

E
Eric Blake 已提交
3381
cleanup:
E
Eduardo Otubo 已提交
3382 3383
    VIR_FREE(cmd);
    VIR_FREE(ret);
E
Eric Blake 已提交
3384
    return rv;
E
Eduardo Otubo 已提交
3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406
}

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;
    char *char_ptr;
    char *cmd = NULL;
    int slot = 0;
    char *ret = NULL;
    char name[PHYP_IFACENAME_SIZE];
    char mac[PHYP_MAC_SIZE];
    virInterfaceDefPtr def;
E
Eric Blake 已提交
3407
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
3408 3409

    if (!(def = virInterfaceDefParseString(xml)))
E
Eric Blake 已提交
3410
        goto cleanup;
E
Eduardo Otubo 已提交
3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424

    /* Now need to get the next free slot number */
    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      " -r virtualio --rsubtype slot --level slot"
                      " -Fslot_num --filter lpar_names=%s"
                      " |sort|tail -n 1", def->name);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3425
        goto cleanup;
E
Eduardo Otubo 已提交
3426 3427 3428 3429 3430 3431
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3432
        goto cleanup;
E
Eduardo Otubo 已提交
3433 3434

    if (virStrToLong_i(ret, &char_ptr, 10, &slot) == -1)
E
Eric Blake 已提交
3435
        goto cleanup;
E
Eduardo Otubo 已提交
3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455

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

    /* Now adding the new network interface */
    VIR_FREE(cmd);
    VIR_FREE(ret);

    virBufferAddLit(&buf, "chhwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      " -r virtualio --rsubtype eth"
                      " -p %s -o a -s %d -a port_vlan_id=1,"
                      "ieee_virtual_eth=0", def->name, slot);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3456
        goto cleanup;
E
Eduardo Otubo 已提交
3457 3458 3459 3460 3461 3462
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret != NULL)
E
Eric Blake 已提交
3463
        goto cleanup;
E
Eduardo Otubo 已提交
3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485

    /* 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 */
    VIR_FREE(cmd);
    VIR_FREE(ret);

    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      " -r virtualio --rsubtype slot --level slot"
                      " |sed '/lpar_name=%s/!d; /slot_num=%d/!d; "
                      "s/^.*drc_name=//'", def->name, slot);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3486
        goto cleanup;
E
Eduardo Otubo 已提交
3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL) {
        /* roll back and excluding interface if error*/
        VIR_FREE(cmd);
        VIR_FREE(ret);

        virBufferAddLit(&buf, "chhwres ");
        if (system_type == HMC)
            virBufferVSprintf(&buf, "-m %s ", managed_system);

        virBufferVSprintf(&buf,
                " -r virtualio --rsubtype eth"
                " -p %s -o r -s %d", def->name, slot);

        if (virBufferError(&buf)) {
            virBufferFreeAndReset(&buf);
            virReportOOMError();
E
Eric Blake 已提交
3508
            goto cleanup;
E
Eduardo Otubo 已提交
3509 3510 3511 3512 3513
        }

        cmd = virBufferContentAndReset(&buf);

        ret = phypExec(session, cmd, &exit_status, conn);
E
Eric Blake 已提交
3514
        goto cleanup;
E
Eduardo Otubo 已提交
3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534
    }

    memcpy(name, ret, PHYP_IFACENAME_SIZE-1);

    /* Getting the new interface mac addr */
    VIR_FREE(cmd);
    VIR_FREE(ret);

    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      "-r virtualio --rsubtype eth --level lpar "
                      " |sed '/lpar_name=%s/!d; /slot_num=%d/!d; "
                      "s/^.*mac_addr=//'", def->name, slot);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3535
        goto cleanup;
E
Eduardo Otubo 已提交
3536 3537 3538 3539 3540 3541
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3542
        goto cleanup;
E
Eduardo Otubo 已提交
3543 3544 3545

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

E
Eric Blake 已提交
3548
cleanup:
E
Eduardo Otubo 已提交
3549 3550 3551
    VIR_FREE(cmd);
    VIR_FREE(ret);
    virInterfaceDefFree(def);
E
Eric Blake 已提交
3552
    return result;
E
Eduardo Otubo 已提交
3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570
}

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 *char_ptr;
    char *cmd = NULL;
    char *ret = NULL;
    int slot = 0;
    int lpar_id = 0;
    char mac[PHYP_MAC_SIZE];
3571
    virInterfacePtr result = NULL;
E
Eduardo Otubo 已提交
3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585

    /*Getting the slot number for the interface */
    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      " -r virtualio --rsubtype slot --level slot "
                      " -F drc_name,slot_num |"
                      " sed -n '/%s/ s/^.*,//p'", name);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3586
        goto cleanup;
E
Eduardo Otubo 已提交
3587 3588 3589 3590 3591 3592
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3593
        goto cleanup;
E
Eduardo Otubo 已提交
3594 3595

    if (virStrToLong_i(ret, &char_ptr, 10, &slot) == -1)
E
Eric Blake 已提交
3596
        goto cleanup;
E
Eduardo Otubo 已提交
3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620

    /*Getting the lpar_id for the interface */
    VIR_FREE(cmd);
    VIR_FREE(ret);

    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      " -r virtualio --rsubtype slot --level slot "
                      " -F drc_name,lpar_id |"
                      " sed -n '/%s/ s/^.*,//p'", name);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return NULL;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3621
        goto cleanup;
E
Eduardo Otubo 已提交
3622 3623

    if (virStrToLong_i(ret, &char_ptr, 10, &lpar_id) == -1)
E
Eric Blake 已提交
3624
        goto cleanup;
E
Eduardo Otubo 已提交
3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638

    /*Getting the interface mac */
    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F lpar_id,slot_num,mac_addr|"
                      " sed -n '/%d,%d/ s/^.*,//p'", lpar_id, slot);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3639
        goto cleanup;
E
Eduardo Otubo 已提交
3640 3641 3642 3643 3644 3645 3646 3647
    }
    cmd = virBufferContentAndReset(&buf);

    VIR_FREE(ret);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3648
        goto cleanup;
E
Eduardo Otubo 已提交
3649 3650 3651

    memcpy(mac, ret, PHYP_MAC_SIZE-1);

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

E
Eric Blake 已提交
3654
cleanup:
E
Eduardo Otubo 已提交
3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669
    VIR_FREE(cmd);
    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;
    int exit_status = 0;
E
Eric Blake 已提交
3670
    int state = -1;
E
Eduardo Otubo 已提交
3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686
    char *char_ptr;
    char *cmd = NULL;
    char *ret = NULL;

    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      " -r virtualio --rsubtype eth --level lpar "
                      " -F mac_addr,state |"
                      " sed -n '/%s/ s/^.*,//p'", iface->mac);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3687
        goto cleanup;
E
Eduardo Otubo 已提交
3688 3689 3690 3691 3692 3693
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, iface->conn);

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3694
        goto cleanup;
E
Eduardo Otubo 已提交
3695 3696

    if (virStrToLong_i(ret, &char_ptr, 10, &state) == -1)
E
Eric Blake 已提交
3697
        goto cleanup;
E
Eduardo Otubo 已提交
3698

E
Eric Blake 已提交
3699
cleanup:
E
Eduardo Otubo 已提交
3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721
    VIR_FREE(cmd);
    VIR_FREE(ret);
    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 *cmd = NULL;
    char *ret = NULL;
    char *networks = NULL;
    char *char_ptr2 = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
E
Eric Blake 已提交
3722
    bool success = false;
E
Eduardo Otubo 已提交
3723 3724 3725 3726 3727 3728 3729 3730 3731 3732

    virBufferAddLit(&buf, "lshwres");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -r virtualio --rsubtype slot  --level slot|"
                      " sed '/eth/!d; /lpar_id=%d/d; s/^.*drc_name=//g'",
                      vios_id);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3733
        goto cleanup;
E
Eduardo Otubo 已提交
3734 3735 3736 3737 3738
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

E
Eric Blake 已提交
3739 3740
    /* I need to parse the textual return in order to get the network
     * interfaces */
E
Eduardo Otubo 已提交
3741
    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3742
        goto cleanup;
E
Eduardo Otubo 已提交
3743 3744 3745 3746 3747 3748 3749 3750 3751 3752

    networks = ret;

    while (got < nnames) {
        char_ptr2 = strchr(networks, '\n');

        if (char_ptr2) {
            *char_ptr2 = '\0';
            if ((names[got++] = strdup(networks)) == NULL) {
                virReportOOMError();
E
Eric Blake 已提交
3753
                goto cleanup;
E
Eduardo Otubo 已提交
3754 3755 3756 3757 3758 3759 3760 3761
            }
            char_ptr2++;
            networks = char_ptr2;
        } else {
            break;
        }
    }

E
Eric Blake 已提交
3762 3763 3764 3765 3766
cleanup:
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);
    }
E
Eduardo Otubo 已提交
3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781
    VIR_FREE(cmd);
    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;
    int exit_status = 0;
E
Eric Blake 已提交
3782
    int nnets = -1;
E
Eduardo Otubo 已提交
3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798
    char *char_ptr;
    char *cmd = NULL;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "lshwres ");
    if (system_type == HMC)
        virBufferVSprintf(&buf, "-m %s ", managed_system);

    virBufferVSprintf(&buf,
                      "-r virtualio --rsubtype eth --level lpar|"
                      "grep -v lpar_id=%d|grep -c lpar_name", vios_id);

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
E
Eric Blake 已提交
3799
        goto cleanup;
E
Eduardo Otubo 已提交
3800 3801 3802 3803 3804 3805
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, conn);

    if (exit_status < 0 || ret == NULL)
E
Eric Blake 已提交
3806
        goto cleanup;
E
Eduardo Otubo 已提交
3807 3808

    if (virStrToLong_i(ret, &char_ptr, 10, &nnets) == -1)
E
Eric Blake 已提交
3809
        goto cleanup;
E
Eduardo Otubo 已提交
3810

E
Eric Blake 已提交
3811
cleanup:
E
Eduardo Otubo 已提交
3812 3813 3814 3815 3816
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return nnets;
}

3817 3818
static int
phypGetLparState(virConnectPtr conn, unsigned int lpar_id)
3819
{
3820
    ConnectionData *connection_data = conn->networkPrivateData;
3821
    phyp_driverPtr phyp_driver = conn->privateData;
3822 3823 3824 3825 3826 3827 3828 3829 3830
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
    char *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
    char *char_ptr = NULL;
    char *managed_system = phyp_driver->managed_system;
    int state = VIR_DOMAIN_NOSTATE;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3831

3832 3833 3834 3835 3836 3837
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -F state --filter lpar_ids=%d", lpar_id);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
3838
        virReportOOMError();
3839
        return state;
3840
    }
3841
    cmd = virBufferContentAndReset(&buf);
3842

3843
    ret = phypExec(session, cmd, &exit_status, conn);
3844

3845 3846
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3847

3848
    char_ptr = strchr(ret, '\n');
3849

3850 3851
    if (char_ptr)
        *char_ptr = '\0';
3852

3853 3854 3855 3856 3857 3858
    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;
3859

3860 3861 3862 3863
  cleanup:
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return state;
3864 3865
}

3866 3867 3868 3869
/* XXX - is this needed? */
static int phypDiskType(virConnectPtr, char *) ATTRIBUTE_UNUSED;
static int
phypDiskType(virConnectPtr conn, char *backing_device)
3870 3871
{
    phyp_driverPtr phyp_driver = conn->privateData;
3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
    int system_type = phyp_driver->system_type;
    char *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
    char *char_ptr;
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
    int disk_type = -1;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3883

3884 3885 3886 3887
    virBufferAddLit(&buf, "viosvrcmd");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -p %d -c \"lssp -field name type "
E
Eric Blake 已提交
3888
                      "-fmt , -all|sed -n '/%s/ {\n s/^.*,//\n p\n}'\"",
3889 3890 3891
                      vios_id, backing_device);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
3892
        virReportOOMError();
3893 3894 3895
        return disk_type;
    }
    cmd = virBufferContentAndReset(&buf);
3896

3897
    ret = phypExec(session, cmd, &exit_status, conn);
3898

3899 3900
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
3901

3902
    char_ptr = strchr(ret, '\n');
3903

3904 3905
    if (char_ptr)
        *char_ptr = '\0';
3906

3907 3908 3909 3910
    if (STREQ(ret, "LVPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_BLOCK;
    else if (STREQ(ret, "FBPOOL"))
        disk_type = VIR_DOMAIN_DISK_TYPE_FILE;
3911

3912 3913 3914 3915 3916
  cleanup:
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return disk_type;
}
3917

3918 3919 3920 3921 3922
static int
phypNumDefinedDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 1);
}
3923

3924 3925 3926 3927
static int
phypNumDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 0);
3928 3929
}

3930 3931
static int
phypListDomains(virConnectPtr conn, int *ids, int nids)
3932
{
3933 3934
    return phypListDomainsGeneric(conn, ids, nids, 0);
}
3935

3936 3937 3938
static int
phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames)
{
3939
    bool success = false;
3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952
    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 *cmd = NULL;
    char *ret = NULL;
    char *domains = NULL;
    char *char_ptr2 = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
3953

3954 3955 3956
    virBufferAddLit(&buf, "lssyscfg -r lpar");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
E
Eric Blake 已提交
3957 3958
    virBufferVSprintf(&buf, " -F name,state"
                      "|sed -n '/Not Activated/ {\n s/,.*$//\n p\n}'");
3959 3960
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
3961
        virReportOOMError();
3962
        return -1;
3963
    }
3964
    cmd = virBufferContentAndReset(&buf);
3965

3966
    ret = phypExec(session, cmd, &exit_status, conn);
3967

3968 3969
    /* I need to parse the textual return in order to get the domains */
    if (exit_status < 0 || ret == NULL)
3970
        goto cleanup;
3971 3972
    else {
        domains = ret;
3973

3974 3975
        while (got < nnames) {
            char_ptr2 = strchr(domains, '\n');
3976

3977 3978 3979
            if (char_ptr2) {
                *char_ptr2 = '\0';
                if ((names[got++] = strdup(domains)) == NULL) {
3980
                    virReportOOMError();
3981
                    goto cleanup;
3982
                }
3983 3984 3985 3986
                char_ptr2++;
                domains = char_ptr2;
            } else
                break;
3987
        }
3988 3989
    }

3990 3991 3992 3993 3994 3995 3996 3997 3998
    success = true;

  cleanup:
    if (!success) {
        for (i = 0; i < got; i++)
            VIR_FREE(names[i]);

        got = -1;
    }
3999

4000 4001
    VIR_FREE(cmd);
    VIR_FREE(ret);
4002 4003

    return got;
4004 4005
}

4006 4007
static virDomainPtr
phypDomainLookupByName(virConnectPtr conn, const char *lpar_name)
4008
{
4009 4010 4011 4012 4013 4014 4015
    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];
4016

4017 4018 4019
    lpar_id = phypGetLparID(session, managed_system, lpar_name, conn);
    if (lpar_id == -1)
        return NULL;
4020

4021 4022
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
        return NULL;
4023

4024 4025 4026 4027 4028 4029
    dom = virGetDomain(conn, lpar_name, lpar_uuid);

    if (dom)
        dom->id = lpar_id;

    return dom;
4030 4031
}

4032 4033
static virDomainPtr
phypDomainLookupByID(virConnectPtr conn, int lpar_id)
4034 4035
{
    ConnectionData *connection_data = conn->networkPrivateData;
4036
    phyp_driverPtr phyp_driver = conn->privateData;
4037
    LIBSSH2_SESSION *session = connection_data->session;
4038 4039 4040 4041
    virDomainPtr dom = NULL;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
    unsigned char lpar_uuid[VIR_UUID_BUFLEN];
E
Eduardo Otubo 已提交
4042

4043 4044
    char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
                                      conn);
4045

4046
    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
4047
        goto cleanup;
4048

4049
    if (exit_status < 0)
4050
        goto cleanup;
4051

4052
    dom = virGetDomain(conn, lpar_name, lpar_uuid);
4053

4054 4055
    if (dom)
        dom->id = lpar_id;
4056

4057
  cleanup:
4058
    VIR_FREE(lpar_name);
4059

4060
    return dom;
4061 4062
}

4063 4064
static char *
phypDomainDumpXML(virDomainPtr dom, int flags)
4065
{
4066 4067
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = dom->conn->privateData;
4068
    LIBSSH2_SESSION *session = connection_data->session;
4069 4070
    virDomainDef def;
    char *managed_system = phyp_driver->managed_system;
E
Eduardo Otubo 已提交
4071

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

4074 4075 4076 4077 4078 4079 4080 4081 4082
    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) {
        VIR_ERROR0(_("Unable to determine domain's name."));
        goto err;
E
Eduardo Otubo 已提交
4083 4084
    }

4085 4086
    if (phypGetLparUUID(def.uuid, dom->id, dom->conn) == -1) {
        VIR_ERROR0(_("Unable to generate random uuid."));
E
Eduardo Otubo 已提交
4087 4088
        goto err;
    }
4089

4090
    if ((def.mem.max_balloon =
4091 4092 4093 4094
         phypGetLparMem(dom->conn, managed_system, dom->id, 0)) == 0) {
        VIR_ERROR0(_("Unable to determine domain's max memory."));
        goto err;
    }
4095

4096
    if ((def.mem.cur_balloon =
4097 4098 4099 4100
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0) {
        VIR_ERROR0(_("Unable to determine domain's memory."));
        goto err;
    }
4101

E
Eric Blake 已提交
4102
    if ((def.maxvcpus = def.vcpus =
4103 4104
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0) {
        VIR_ERROR0(_("Unable to determine domain's CPU."));
4105
        goto err;
4106
    }
4107

4108
    return virDomainDefFormat(&def, flags);
4109

4110 4111 4112
  err:
    return NULL;
}
4113

4114 4115 4116
static int
phypDomainResume(virDomainPtr dom)
{
4117
    int result = -1;
4118 4119 4120 4121 4122 4123 4124 4125 4126
    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 *cmd = NULL;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
4127

4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -r lpar -o on --id %d -f %s",
                      dom->id, dom->name);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);
4139

4140
    ret = phypExec(session, cmd, &exit_status, dom->conn);
4141

4142
    if (exit_status < 0)
4143
        goto cleanup;
4144

4145
    result = 0;
4146

4147
  cleanup:
4148 4149
    VIR_FREE(cmd);
    VIR_FREE(ret);
4150 4151

    return result;
4152 4153
}

4154 4155
static int
phypDomainShutdown(virDomainPtr dom)
4156
{
4157
    int result = -1;
4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175
    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 *cmd = NULL;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "chsysstate");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -r lpar -o shutdown --id %d", dom->id);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
4176
        return -1;
4177
    }
4178
    cmd = virBufferContentAndReset(&buf);
4179

4180 4181 4182
    ret = phypExec(session, cmd, &exit_status, dom->conn);

    if (exit_status < 0)
4183
        goto cleanup;
4184

4185
    result = 0;
4186

4187
  cleanup:
4188 4189
    VIR_FREE(cmd);
    VIR_FREE(ret);
4190 4191

    return result;
4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219
}

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)
        VIR_WARN0("Unable to determine domain's max memory.");

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

    if ((info->nrVirtCpu =
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
        VIR_WARN0("Unable to determine domain's CPU.");

    return 0;
}

static int
phypDomainDestroy(virDomainPtr dom)
{
4220
    int result = -1;
4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237
    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 *cmd = NULL;
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "rmsyscfg");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -r lpar --id %d", dom->id);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
4238
        return -1;
4239 4240 4241 4242 4243 4244
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, dom->conn);

    if (exit_status < 0)
4245
        goto cleanup;
4246 4247

    if (phypUUIDTable_RemLpar(dom->conn, dom->id) == -1)
4248
        goto cleanup;
4249

4250
    dom->id = -1;
4251
    result = 0;
4252

4253
  cleanup:
4254 4255 4256
    VIR_FREE(cmd);
    VIR_FREE(ret);

4257
    return result;
4258
}
4259

4260 4261
static int
phypBuildLpar(virConnectPtr conn, virDomainDefPtr def)
4262
{
4263
    int result = -1;
4264 4265 4266 4267 4268 4269 4270 4271 4272
    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 *cmd = NULL;
    char *ret = NULL;
    int exit_status = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
4273

4274
    if (!def->mem.cur_balloon) {
4275 4276 4277
        PHYP_ERROR(VIR_ERR_XML_ERROR,"%s",
                _("Field \"<memory>\" on the domain XML file is missing or has "
                    "invalid value."));
4278
        goto cleanup;
4279 4280
    }

4281
    if (!def->mem.max_balloon) {
4282 4283 4284
        PHYP_ERROR(VIR_ERR_XML_ERROR,"%s",
                _("Field \"<currentMemory>\" on the domain XML file is missing or"
                    " has invalid value."));
4285
        goto cleanup;
4286 4287
    }

4288 4289 4290
    if (def->ndisks < 1) {
        PHYP_ERROR(VIR_ERR_XML_ERROR, "%s",
                   _("Domain XML must contain at least one \"<disk>\" element."));
4291
        goto cleanup;
4292 4293 4294 4295 4296 4297
    }

    if (!def->disks[0]->src) {
        PHYP_ERROR(VIR_ERR_XML_ERROR,"%s",
                   _("Field \"<src>\" under \"<disk>\" on the domain XML file is "
                     "missing."));
4298
        goto cleanup;
4299 4300
    }

4301 4302 4303 4304 4305
    virBufferAddLit(&buf, "mksyscfg");
    if (system_type == HMC)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " -r lpar -p %s -i min_mem=%d,desired_mem=%d,"
                      "max_mem=%d,desired_procs=%d,virtual_scsi_adapters=%s",
4306 4307 4308
                      def->name, (int) def->mem.cur_balloon,
                      (int) def->mem.cur_balloon, (int) def->mem.max_balloon,
                      (int) def->vcpus, def->disks[0]->src);
4309 4310 4311 4312 4313 4314
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    cmd = virBufferContentAndReset(&buf);
4315

4316
    ret = phypExec(session, cmd, &exit_status, conn);
4317

4318 4319
    if (exit_status < 0) {
        VIR_ERROR(_("Unable to create LPAR. Reason: '%s'"), ret);
4320
        goto cleanup;
4321
    }
4322

4323 4324
    if (phypUUIDTable_AddLpar(conn, def->uuid, def->id) == -1) {
        VIR_ERROR0(_("Unable to add LPAR to the table"));
4325
        goto cleanup;
4326
    }
4327

4328
    result = 0;
4329

4330
  cleanup:
4331 4332
    VIR_FREE(cmd);
    VIR_FREE(ret);
4333 4334

    return result;
4335
}
4336

4337 4338 4339 4340
static virDomainPtr
phypDomainCreateAndStart(virConnectPtr conn,
                         const char *xml, unsigned int flags)
{
E
Eduardo Otubo 已提交
4341
    virCheckFlags(0, NULL);
4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359

    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,
                                        VIR_DOMAIN_XML_SECURE)))
        goto err;

    /* checking if this name already exists on this system */
4360
    if (phypGetLparID(session, managed_system, def->name, conn) != -1) {
4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403
        VIR_WARN0("LPAR name already exists.");
        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) {
            VIR_WARN0("LPAR ID or UUID already exists.");
            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;

  err:
    virDomainDefFree(def);
    if (dom)
        virUnrefDomain(dom);
    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
4404 4405
phypDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                        unsigned int flags)
4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419
{
    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 *cmd = NULL;
    char *ret = NULL;
    char operation;
    unsigned long ncpus = 0;
    unsigned int amount = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

4420 4421 4422 4423 4424
    if (flags != VIR_DOMAIN_VCPU_LIVE) {
        PHYP_ERROR(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
        return -1;
    }

4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466
    if ((ncpus = phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
        return 0;

    if (nvcpus > phypGetLparCPUMAX(dom)) {
        VIR_ERROR0(_("You are trying to set a number of CPUs bigger than "
                     "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)
        virBufferVSprintf(&buf, " -m %s", managed_system);
    virBufferVSprintf(&buf, " --id %d -o %c --procunits %d 2>&1 |sed "
                      "-e 's/^.*\\([0-9][0-9]*.[0-9][0-9]*\\).*$/\\1/'",
                      dom->id, operation, amount);
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return 0;
    }
    cmd = virBufferContentAndReset(&buf);

    ret = phypExec(session, cmd, &exit_status, dom->conn);

    if (exit_status < 0) {
        VIR_ERROR0(_
                   ("Possibly you don't have IBM Tools installed in your LPAR."
                    " Contact your support to enable this feature."));
    }

    VIR_FREE(cmd);
    VIR_FREE(ret);
    return 0;
4467 4468

}
4469

4470 4471 4472 4473 4474 4475
static int
phypDomainSetCPU(virDomainPtr dom, unsigned int nvcpus)
{
    return phypDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_VCPU_LIVE);
}

4476
static virDrvOpenStatus
4477 4478
phypVIOSDriverOpen(virConnectPtr conn,
                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
4479
                   int flags ATTRIBUTE_UNUSED)
4480
{
4481 4482 4483
    if (conn->driver->no != VIR_DRV_PHYP)
        return VIR_DRV_OPEN_DECLINED;

4484 4485 4486 4487
    return VIR_DRV_OPEN_SUCCESS;
}

static int
4488
phypVIOSDriverClose(virConnectPtr conn ATTRIBUTE_UNUSED)
4489 4490 4491 4492
{
    return 0;
}

4493 4494 4495 4496 4497 4498 4499 4500
static virDriver phypDriver = {
    VIR_DRV_PHYP, "PHYP", phypOpen,     /* open */
    phypClose,                  /* close */
    NULL,                       /* supports_feature */
    NULL,                       /* type */
    NULL,                       /* version */
    NULL,                       /* libvirtVersion (impl. in libvirt.c) */
    NULL,                       /* getHostname */
E
Eric Blake 已提交
4501
    NULL,                       /* getSysinfo */
4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519
    NULL,                       /* getMaxVcpus */
    NULL,                       /* nodeGetInfo */
    phypConnectGetCapabilities, /* getCapabilities */
    phypListDomains,            /* listDomains */
    phypNumDomains,             /* numOfDomains */
    phypDomainCreateAndStart,   /* domainCreateXML */
    phypDomainLookupByID,       /* domainLookupByID */
    NULL,                       /* domainLookupByUUID */
    phypDomainLookupByName,     /* domainLookupByName */
    NULL,                       /* domainSuspend */
    phypDomainResume,           /* domainResume */
    phypDomainShutdown,         /* domainShutdown */
    NULL,                       /* domainReboot */
    phypDomainDestroy,          /* domainDestroy */
    NULL,                       /* domainGetOSType */
    NULL,                       /* domainGetMaxMemory */
    NULL,                       /* domainSetMaxMemory */
    NULL,                       /* domainSetMemory */
4520
    NULL,                       /* domainSetMemoryFlags */
4521 4522 4523 4524
    NULL,                       /* domainSetMemoryParameters */
    NULL,                       /* domainGetMemoryParameters */
    NULL,                       /* domainSetBlkioParameters */
    NULL,                       /* domainGetBlkioParameters */
4525 4526 4527 4528 4529
    phypDomainGetInfo,          /* domainGetInfo */
    NULL,                       /* domainSave */
    NULL,                       /* domainRestore */
    NULL,                       /* domainCoreDump */
    phypDomainSetCPU,           /* domainSetVcpus */
4530 4531
    phypDomainSetVcpusFlags,    /* domainSetVcpusFlags */
    phypDomainGetVcpusFlags,    /* domainGetVcpusFlags */
4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545
    NULL,                       /* domainPinVcpu */
    NULL,                       /* domainGetVcpus */
    phypGetLparCPUMAX,          /* domainGetMaxVcpus */
    NULL,                       /* domainGetSecurityLabel */
    NULL,                       /* nodeGetSecurityModel */
    phypDomainDumpXML,          /* domainDumpXML */
    NULL,                       /* domainXMLFromNative */
    NULL,                       /* domainXMLToNative */
    phypListDefinedDomains,     /* listDefinedDomains */
    phypNumDefinedDomains,      /* numOfDefinedDomains */
    NULL,                       /* domainCreate */
    NULL,                       /* domainCreateWithFlags */
    NULL,                       /* domainDefineXML */
    NULL,                       /* domainUndefine */
4546
    phypAttachDevice,           /* domainAttachDevice */
4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578
    NULL,                       /* domainAttachDeviceFlags */
    NULL,                       /* domainDetachDevice */
    NULL,                       /* domainDetachDeviceFlags */
    NULL,                       /* domainUpdateDeviceFlags */
    NULL,                       /* domainGetAutostart */
    NULL,                       /* domainSetAutostart */
    NULL,                       /* domainGetSchedulerType */
    NULL,                       /* domainGetSchedulerParameters */
    NULL,                       /* domainSetSchedulerParameters */
    NULL,                       /* domainMigratePrepare */
    NULL,                       /* domainMigratePerform */
    NULL,                       /* domainMigrateFinish */
    NULL,                       /* domainBlockStats */
    NULL,                       /* domainInterfaceStats */
    NULL,                       /* domainMemoryStats */
    NULL,                       /* domainBlockPeek */
    NULL,                       /* domainMemoryPeek */
    NULL,                       /* domainGetBlockInfo */
    NULL,                       /* nodeGetCellsFreeMemory */
    NULL,                       /* getFreeMemory */
    NULL,                       /* domainEventRegister */
    NULL,                       /* domainEventDeregister */
    NULL,                       /* domainMigratePrepare2 */
    NULL,                       /* domainMigrateFinish2 */
    NULL,                       /* nodeDeviceDettach */
    NULL,                       /* nodeDeviceReAttach */
    NULL,                       /* nodeDeviceReset */
    NULL,                       /* domainMigratePrepareTunnel */
    phypIsEncrypted,            /* isEncrypted */
    phypIsSecure,               /* isSecure */
    NULL,                       /* domainIsActive */
    NULL,                       /* domainIsPersistent */
4579
    phypIsUpdated,              /* domainIsUpdated */
4580 4581 4582 4583 4584
    NULL,                       /* cpuCompare */
    NULL,                       /* cpuBaseline */
    NULL,                       /* domainGetJobInfo */
    NULL,                       /* domainAbortJob */
    NULL,                       /* domainMigrateSetMaxDowntime */
4585
    NULL,                       /* domainMigrateSetMaxSpeed */
4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599
    NULL,                       /* domainEventRegisterAny */
    NULL,                       /* domainEventDeregisterAny */
    NULL,                       /* domainManagedSave */
    NULL,                       /* domainHasManagedSaveImage */
    NULL,                       /* domainManagedSaveRemove */
    NULL,                       /* domainSnapshotCreateXML */
    NULL,                       /* domainSnapshotDumpXML */
    NULL,                       /* domainSnapshotNum */
    NULL,                       /* domainSnapshotListNames */
    NULL,                       /* domainSnapshotLookupByName */
    NULL,                       /* domainHasCurrentSnapshot */
    NULL,                       /* domainSnapshotCurrent */
    NULL,                       /* domainRevertToSnapshot */
    NULL,                       /* domainSnapshotDelete */
C
Chris Lalancette 已提交
4600
    NULL,                       /* qemuMonitorCommand */
4601
    NULL, /* domainOpenConsole */
4602 4603
};

4604 4605
static virStorageDriver phypStorageDriver = {
    .name = "PHYP",
4606 4607
    .open = phypVIOSDriverOpen,
    .close = phypVIOSDriverClose,
4608

4609 4610
    .numOfPools = phypNumOfStoragePools,
    .listPools = phypListStoragePools,
4611 4612 4613
    .numOfDefinedPools = NULL,
    .listDefinedPools = NULL,
    .findPoolSources = NULL,
4614 4615
    .poolLookupByName = phypStoragePoolLookupByName,
    .poolLookupByUUID = phypGetStoragePoolLookUpByUUID,
4616
    .poolLookupByVolume = NULL,
4617
    .poolCreateXML = phypStoragePoolCreateXML,
4618 4619 4620 4621
    .poolDefineXML = NULL,
    .poolBuild = NULL,
    .poolUndefine = NULL,
    .poolCreate = NULL,
4622
    .poolDestroy = phypDestroyStoragePool,
4623 4624 4625
    .poolDelete = NULL,
    .poolRefresh = NULL,
    .poolGetInfo = NULL,
4626
    .poolGetXMLDesc = phypGetStoragePoolXMLDesc,
4627 4628
    .poolGetAutostart = NULL,
    .poolSetAutostart = NULL,
4629 4630
    .poolNumOfVolumes = phypStoragePoolNumOfVolumes,
    .poolListVolumes = phypStoragePoolListVolumes,
4631

4632
    .volLookupByName = phypVolumeLookupByName,
4633
    .volLookupByKey = NULL,
4634 4635
    .volLookupByPath = phypVolumeLookupByPath,
    .volCreateXML = phypStorageVolCreateXML,
4636 4637 4638
    .volCreateXMLFrom = NULL,
    .volDelete = NULL,
    .volGetInfo = NULL,
4639 4640
    .volGetXMLDesc = phypVolumeGetXMLDesc,
    .volGetPath = phypVolumeGetPath,
4641 4642 4643 4644
    .poolIsActive = NULL,
    .poolIsPersistent = NULL
};

E
Eduardo Otubo 已提交
4645
static virInterfaceDriver phypInterfaceDriver = {
4646 4647 4648
    .name = "PHYP",
    .open = phypVIOSDriverOpen,
    .close = phypVIOSDriverClose,
E
Eduardo Otubo 已提交
4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660
    .numOfInterfaces = phypNumOfInterfaces,
    .listInterfaces = phypListInterfaces,
    .numOfDefinedInterfaces = NULL,
    .listDefinedInterfaces = NULL,
    .interfaceLookupByName = phypInterfaceLookupByName,
    .interfaceLookupByMACString = NULL,
    .interfaceGetXMLDesc = NULL,
    .interfaceDefineXML = phypInterfaceDefineXML,
    .interfaceUndefine = NULL,
    .interfaceCreate = NULL,
    .interfaceDestroy = phypInterfaceDestroy,
    .interfaceIsActive = phypInterfaceIsActive
4661 4662
};

4663 4664 4665
int
phypRegister(void)
{
4666 4667 4668 4669
    if (virRegisterDriver(&phypDriver) < 0)
        return -1;
    if (virRegisterStorageDriver(&phypStorageDriver) < 0)
        return -1;
E
Eduardo Otubo 已提交
4670
    if (virRegisterInterfaceDriver(&phypInterfaceDriver) < 0)
4671
        return -1;
4672

4673 4674
    return 0;
}