Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 407 lines (366 sloc) 15.476 kb
1 2 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
// -*- mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
// vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:

/*************************************************************************
* Copyright 2009-2012 Eucalyptus Systems, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
* CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
* additional information or have any questions.
*
* This file may incorporate work covered under the following copyright
* and permission notice:
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Regents of the University of California
* All rights reserved.
*
* Redistribution and use of this software in source and binary forms,
* with or without modification, are permitted provided that the
* following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. USERS OF THIS SOFTWARE ACKNOWLEDGE
* THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE LICENSED MATERIAL,
* COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS SOFTWARE,
* AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA,
* SANTA BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY,
* WHICH IN THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION,
* REPLACEMENT OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO
* IDENTIFIED, OR WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT
* NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS.
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#define __USE_GNU /* strnlen */
#include <string.h> /* strlen, strcpy */
#include <time.h>
#include <sys/types.h> /* fork */
#include <sys/wait.h> /* waitpid */
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h> /* SIGINT */
#include <sys/stat.h>
#include <fcntl.h>

#include "ipc.h"
#include "misc.h"
#include "handlers.h"
#include "eucalyptus.h"
#include "euca_auth.h"
#include "backing.h"
#include "xml.h"
#include "diskutil.h"
#include "iscsi.h"
#include "sensor.h"

/* coming from handlers.c */
extern sem *hyp_sem;
extern sem *inst_sem;
extern bunchOfInstances *global_instances;
extern struct nc_state_t nc_state;

#define HYPERVISOR_URI "qemu:///system"

static int doInitialize(struct nc_state_t *nc)
{
    char *s = NULL;

    // set up paths of Eucalyptus commands NC relies on
    snprintf(nc->get_info_cmd_path, MAX_PATH, EUCALYPTUS_GET_KVM_INFO, nc->home, nc->home);
    strcpy(nc->uri, HYPERVISOR_URI);
    nc->convert_to_disk = 1;
    nc->capability = HYPERVISOR_HARDWARE; // TODO: indicate virtio support?

    s = system_output(nc->get_info_cmd_path);
#define GET_VALUE(name,var) \
if (get_value (s, name, &var)) { \
logprintfl (EUCAFATAL, "did not find %s in output from %s\n", name, nc->get_info_cmd_path); \
if (s) free (s); \
return ERROR_FATAL; \
}

    GET_VALUE("nr_cores", nc->cores_max);
    GET_VALUE("total_memory", nc->mem_max);
    if (s)
        free(s);

    // we leave 256M to the host
    nc->mem_max -= 256;

    return OK;
}

/* thread that does the actual reboot */
static void *rebooting_thread(void *arg)
{
    virConnectPtr *conn;
    ncInstance *instance = (ncInstance *) arg;
    struct stat statbuf;
    int rc = 0;

    logprintfl(EUCADEBUG, "[%s] spawning rebooting thread\n", instance->instanceId);
    char *xml = file2str(instance->libvirtFilePath);
    if (xml == NULL) {
        logprintfl(EUCAERROR, "[%s] cannot obtain instance XML file %s\n", instance->instanceId, instance->libvirtFilePath);
        return NULL;
    }

    conn = check_hypervisor_conn();
    if (!conn) {
        logprintfl(EUCAERROR, "[%s] cannot restart instance %s, abandoning it\n", instance->instanceId, instance->instanceId);
        change_state(instance, SHUTOFF);
        free(xml);
        return NULL;
    }

    sem_p(hyp_sem);
    virDomainPtr dom = virDomainLookupByName(*conn, instance->instanceId);
    sem_v(hyp_sem);
    if (dom == NULL) {
        free(xml);
        return NULL;
    }

    sem_p(hyp_sem);
    // for KVM, must stop and restart the instance
    logprintfl(EUCADEBUG, "[%s] destroying domain\n", instance->instanceId);
    int error = virDomainDestroy(dom); // TODO: change to Shutdown? TODO: is this synchronous?
    virDomainFree(dom);
    sem_v(hyp_sem);

    if (error) {
        free(xml);
        return NULL;
    }

    // Add a shift to values of three of the metrics: ones that
    // drop back to zero after a reboot. The shift, which is based
    // on the latest value, ensures that values sent upstream do
    // not go backwards .
    sensor_shift_metric (instance->instanceId, "CPUUtilization");
    sensor_shift_metric (instance->instanceId, "NetworkIn");
    sensor_shift_metric (instance->instanceId, "NetworkOut");

    // domain is now shut down, create a new one with the same XML
    sem_p(hyp_sem);
    logprintfl(EUCAINFO, "[%s] rebooting\n", instance->instanceId);
    dom = virDomainCreateLinux(*conn, xml, 0);
    sem_v(hyp_sem);
    free(xml);

    sensor_refresh_resources(instance->instanceId, "", 1); // refresh stats so we set base value accurately

    char *remoteDevStr = NULL;
    // re-attach each volume previously attached
    for (int i = 0; i < EUCA_MAX_VOLUMES; ++i) {
        ncVolume *volume = &instance->volumes[i];
        if (strcmp(volume->stateName, VOL_STATE_ATTACHED) && strcmp(volume->stateName, VOL_STATE_ATTACHING))
            continue; // skip the entry unless attached or attaching

        logprintfl(EUCADEBUG, "[%s] volumes [%d] = '%s'\n", instance->instanceId, i, volume->stateName);
        char *xml = NULL;
        int rc = 0;
        // get credentials, decrypt them
        remoteDevStr = get_iscsi_target(volume->remoteDev);
        if (!remoteDevStr || !strstr(remoteDevStr, "/dev")) {
            logprintfl(EUCAERROR, "[%s] failed to get local name of host iscsi device when re-attaching\n", instance->instanceId);
            rc = 1;
        } else {
            // set the path
            char path[MAX_PATH];
            char lpath[MAX_PATH];
            snprintf(path, sizeof(path), EUCALYPTUS_VOLUME_XML_PATH_FORMAT, instance->instancePath, volume->volumeId); // vol-XXX.xml
            snprintf(lpath, sizeof(lpath), EUCALYPTUS_VOLUME_LIBVIRT_XML_PATH_FORMAT, instance->instancePath, volume->volumeId); // vol-XXX-libvirt.xml

            // read in libvirt XML, which may have been modified by the hook above
            xml = file2str(lpath);
            if (xml == NULL) {
                logprintfl(EUCAERROR, "[%s][%s] failed to read volume XML from %s\n", instance->instanceId, volume->volumeId, lpath);
                rc = 1;
            }
        }
        if (remoteDevStr)
            free(remoteDevStr);

        if (!rc) {
            // zhill - wrap with retry in case libvirt is dumb.
            int err = 0;
#define REATTACH_RETRIES 3
            for (int i = 1; i < REATTACH_RETRIES; i++) {
                // protect libvirt calls because we've seen problems during concurrent libvirt invocations
                sem_p(hyp_sem);
                err = virDomainAttachDevice(dom, xml);
                sem_v(hyp_sem);
                if (err) {
                    logprintfl(EUCAERROR, "[%s][%s] failed to reattach volume (attempt %d of %d)\n", instance->instanceId, volume->volumeId, i, REATTACH_RETRIES);
                    logprintfl(EUCADEBUG, "[%s][%s] error from virDomainAttachDevice: %d xml: %s\n", instance->instanceId, volume->volumeId, err, xml);
                    sleep(3); // sleep a bit and retry
                } else {
                    logprintfl(EUCAINFO, "[%s][%s] volume reattached as '%s'\n", instance->instanceId, volume->volumeId, volume->localDevReal);
                    break;
                }
            }
            int log_level_for_devstring = EUCATRACE;
            if (err)
                log_level_for_devstring = EUCADEBUG;
            logprintfl(log_level_for_devstring, "[%s][%s] remote device string: %s\n", instance->instanceId, volume->volumeId, volume->remoteDev);
        }

        if (xml)
            free(xml);
    }
    if (dom == NULL) {
        logprintfl(EUCAERROR, "[%s] failed to restart instance\n", instance->instanceId);
        change_state(instance, SHUTOFF);
        return NULL;
    }

    sem_p(hyp_sem);
    virDomainFree(dom);
    sem_v(hyp_sem);
    return NULL;
}

static int doRebootInstance(struct nc_state_t *nc, ncMetadata * meta, char *instanceId)
{
    sem_p(inst_sem);
    ncInstance *instance = find_instance(&global_instances, instanceId);
    sem_v(inst_sem);
    if (instance == NULL) {
        logprintfl(EUCAERROR, "[%s] cannot find instance\n", instanceId);
        return ERROR;
    }

    pthread_t tcb;
    // since shutdown/restart may take a while, we do them in a thread
    if (pthread_create(&tcb, NULL, rebooting_thread, (void *)instance)) {
        logprintfl(EUCAERROR, "[%s] failed to spawn a reboot thread\n", instanceId);
        return ERROR_FATAL;
    }
    if (pthread_detach(tcb)) {
        logprintfl(EUCAERROR, "[%s] failed to detach the rebooting thread\n", instanceId);
        return ERROR_FATAL;
    }

    return OK;
}

static int doGetConsoleOutput(struct nc_state_t *nc, ncMetadata * meta, char *instanceId, char **consoleOutput)
{

    char *console_output = NULL, *console_append = NULL, *console_main = NULL;
    char console_file[MAX_PATH], userId[48];
    int rc, fd, ret, readsize;
    struct stat statbuf;
    ncInstance *instance = NULL;

    *consoleOutput = NULL;
    readsize = 64 * 1024;

    // find the instance record
    sem_p(inst_sem);
    instance = find_instance(&global_instances, instanceId);
    if (instance) {
        snprintf(console_file, 1024, "%s/console.append.log", instance->instancePath);
        snprintf(userId, 48, "%s", instance->userId);
    }
    sem_v(inst_sem);

    if (!instance) {
        logprintfl(EUCAERROR, "[%s] cannot locate instance\n", instanceId);
        return (1);
    }
    // read from console.append.log if it exists into dynamically allocated 4K console_append buffer
    rc = stat(console_file, &statbuf);
    if (rc >= 0) {
        if (diskutil_ch(console_file, nc->admin_user_id, nc->admin_user_id, 0) != OK) {
            logprintfl(EUCAERROR, "[%s] failed to change ownership of %s\n", instanceId, console_file);
            return (1);
        }
        fd = open(console_file, O_RDONLY);
        if (fd >= 0) {
            console_append = malloc(4096);
            if (console_append) {
                bzero(console_append, 4096);
                rc = read(fd, console_append, (4096) - 1);
                close(fd);
            }
        }
    }

    sem_p(inst_sem);
    snprintf(console_file, MAX_PATH, "%s/console.log", instance->instancePath);
    sem_v(inst_sem);

    // read the last 64K from console.log or the whole file, if smaller, into dynamically allocated 64K console_main buffer
    rc = stat(console_file, &statbuf);
    if (rc >= 0) {
        if (diskutil_ch(console_file, nc->admin_user_id, nc->admin_user_id, 0) != OK) {
            logprintfl(EUCAERROR, "[%s] failed to change ownership of %s\n", instanceId, console_file);
            if (console_append)
                free(console_append);
            return (1);
        }
        fd = open(console_file, O_RDONLY);
        if (fd >= 0) {
            rc = lseek(fd, (off_t) (-1 * readsize), SEEK_END);
            if (rc < 0) {
                rc = lseek(fd, (off_t) 0, SEEK_SET);
                if (rc < 0) {
                    logprintfl(EUCAERROR, "[%s] cannot seek to beginning of file\n", instanceId);
                    if (console_append)
                        free(console_append);
                    close(fd);
                    return (1);
                }
            }
            console_main = malloc(readsize);
            if (console_main) {
                bzero(console_main, readsize);
                rc = read(fd, console_main, (readsize) - 1);
                close(fd);
            }
        } else {
            logprintfl(EUCAERROR, "[%s] cannot open '%s' read-only\n", instanceId, console_file);
        }
    } else {
        logprintfl(EUCAERROR, "[%s] cannot stat console_output file '%s'\n", instanceId, console_file);
    }

    // concatenate console_append with console_main, base64-encode this, and put into dynamically allocated buffer consoleOutput
    ret = 1;
    console_output = malloc((readsize) + 4096);
    if (console_output) {
        bzero(console_output, (readsize) + 4096);
        if (console_append) {
            strncat(console_output, console_append, 4096);
        }
        if (console_main) {
            strncat(console_output, console_main, readsize);
        }
        *consoleOutput = base64_enc((unsigned char *)console_output, strlen(console_output));
        ret = 0;
    }

    if (console_append)
        free(console_append);
    if (console_main)
        free(console_main);
    if (console_output)
        free(console_output);

    return (ret);
}

struct handlers kvm_libvirt_handlers = {
    .name = "kvm",
    .doInitialize = doInitialize,
    .doDescribeInstances = NULL,
    .doRunInstance = NULL,
    .doTerminateInstance = NULL,
    .doRebootInstance = doRebootInstance,
    .doGetConsoleOutput = doGetConsoleOutput,
    .doDescribeResource = NULL,
    .doStartNetwork = NULL,
    .doAssignAddress = NULL,
    .doPowerDown = NULL,
    .doAttachVolume = NULL,
    .doDetachVolume = NULL,
    .doDescribeSensors = NULL
};
Something went wrong with that request. Please try again.