//! //! Destroy the backing store for a given instance //! //! @param[in] instance the instance identifier for which we will destroy the backing store //! @param[in] do_destroy_files set to TRUE if we need to destroy the content otherwise set to FALSE. //! //! @return EUCA_OK on success //! //! @pre The instance parameter must not be NULL and do_destroy_files must be a valid boolean. //! //! @note //! int destroy_instance_backing(ncInstance * instance, boolean do_destroy_files) { int i = 0; int ret = EUCA_OK; char path[EUCA_MAX_PATH] = ""; char work_regex[1024] = ""; // {userId}/{instanceId}/.* char scURL[512] = ""; if (get_localhost_sc_url(scURL) != EUCA_OK || strlen(scURL) == 0) { LOGWARN("[%s] could not obtain SC URL (is SC enabled?)\n", instance->instanceId); scURL[0] = '\0'; } // find and detach iSCSI targets, if any for (i = 0; i < EUCA_MAX_VOLUMES; ++i) { ncVolume *volume = &instance->volumes[i]; if (!is_volume_used(volume)) continue; if (disconnect_ebs_volume (scURL, localhost_config.use_ws_sec, localhost_config.ws_sec_policy_file, volume->attachmentToken, volume->connectionString, localhost_config.ip, localhost_config.iqn) != 0) { LOGERROR("[%s][%s] failed to disconnect volume\n", instance->instanceId, volume->volumeId); } } // see if instance directory is there (sometimes startup fails before it is created) set_path(path, sizeof(path), instance, NULL); if (check_path(path)) return (ret); if (do_destroy_files) { set_id2(instance, "/.*", work_regex, sizeof(work_regex)); if (blobstore_delete_regex(work_bs, work_regex) == -1) { LOGERROR("[%s] failed to remove some artifacts in %s\n", instance->instanceId, path); } for (i = 0; i < EUCA_MAX_VOLUMES; ++i) { ncVolume *volume = &instance->volumes[i]; snprintf(path, sizeof(path), EUCALYPTUS_VOLUME_XML_PATH_FORMAT, instance->instancePath, volume->volumeId); unlink(path); } // delete all non-blob files in the directory if (blobstore_delete_nonblobs(work_bs, instance->instancePath) < 0) { LOGERROR("[%s] failed to delete some non-blob files under %s\n", instance->instanceId, instance->instancePath); } } // Finally try to remove the directory. If either the user or our code introduced // any new files, this last step will fail. set_path(path, sizeof(path), instance, NULL); if (rmdir(path) && do_destroy_files) { LOGERROR("[%s] failed to remove backing directory %s\n", instance->instanceId, path); } return (ret); }
//! //! Callback used when checking for the integrity of the work blobstore. //! //! @param[in] bb pointer to the blockblob to examine //! //! @return EUCA_OK on success or EUCA_ERROR on failure //! //! @see check_backing_store() //! @see blobstore_fsck() //! //! @pre The bb field must not be NULL. //! static int stale_blob_examiner(const blockblob * bb) { char *s = NULL; char *user_id = NULL; char *inst_id = NULL; char *file = NULL; char path[EUCA_MAX_PATH] = ""; char work_path[EUCA_MAX_PATH] = ""; int work_path_len = 0; ncInstance *instance = NULL; set_path(work_path, sizeof(work_path), NULL, NULL); work_path_len = strlen(work_path); assert(work_path_len > 0); s = strstr(bb->blocks_path, work_path); if ((s == NULL) || (s != bb->blocks_path)) { // blob not under work blobstore path return (EUCA_OK); } // parse the path past the work directory base euca_strncpy(work_path, bb->blocks_path, sizeof(work_path)); s = work_path + work_path_len + 1; user_id = strtok(s, "/"); inst_id = strtok(NULL, "/"); file = strtok(NULL, "/"); if (((instance = find_instance(instances, inst_id)) == NULL) // not found among instances => stale || instance->state == TEARDOWN) { // found among instances, but is already marked as terminated // if this instance is not among those we know about, // load it into memory and report it in Teardown state //! @TODO technically, disk state for this instance is not gone, //! but it soon will be, once this examiner function returns error if ((instance == NULL) && ((instance = load_instance_struct(inst_id)) != NULL)) { LOGINFO("marking non-running instance %s as terminated\n", inst_id); instance->terminationTime = time(NULL); // set time to now, so record won't get expired immediately change_state(instance, TEARDOWN); int err = add_instance(instances, instance); // we are not using locks because we assume the caller does if (err) { free_instance(&instance); } } // while we're here, try to delete extra files that aren't managed by the blobstore snprintf(path, sizeof(path), "%s/work/%s/%s", instances_path, user_id, inst_id); blobstore_delete_nonblobs(bb->store, path); return (EUCA_ERROR); } return (EUCA_OK); }
//! //! Destroy the backing store for a given instance //! //! @param[in] instance the instance identifier for which we will destroy the backing store //! @param[in] do_destroy_files set to TRUE if we need to destroy the content otherwise set to FALSE. //! //! @return EUCA_OK on success //! //! @pre The instance parameter must not be NULL and do_destroy_files must be a valid boolean. //! //! @note //! int destroy_instance_backing(ncInstance * instance, boolean do_destroy_files) { int i = 0; int ret = EUCA_OK; char path[EUCA_MAX_PATH] = ""; char work_regex[1024] = ""; // {userId}/{instanceId}/.* char scURL[512] = ""; ncVolume *volume = NULL; virtualMachine *vm = &(instance->params); virtualBootRecord *vbr = NULL; if (get_localhost_sc_url(scURL) != EUCA_OK || strlen(scURL) == 0) { LOGWARN("[%s] could not obtain SC URL (is SC enabled?)\n", instance->instanceId); scURL[0] = '\0'; } // find and detach iSCSI targets, if any for (i = 0; ((i < EUCA_MAX_VBRS) && (i < vm->virtualBootRecordLen)); i++) { vbr = &(vm->virtualBootRecord[i]); if (vbr->locationType == NC_LOCATION_SC) { if (disconnect_ebs_volume (scURL, localhost_config.use_ws_sec, localhost_config.ws_sec_policy_file, vbr->resourceLocation, vbr->preparedResourceLocation, localhost_config.ip, localhost_config.iqn) != 0) { LOGERROR("[%s] failed to disconnect volume attached to '%s'\n", instance->instanceId, vbr->backingPath); } } } // there may be iSCSI targets for volumes if instance disappeared or was migrated for (i = 0; i < EUCA_MAX_VOLUMES; ++i) { ncVolume *volume = &instance->volumes[i]; if (!is_volume_used(volume)) continue; if (disconnect_ebs_volume (scURL, localhost_config.use_ws_sec, localhost_config.ws_sec_policy_file, volume->attachmentToken, volume->connectionString, localhost_config.ip, localhost_config.iqn) != 0) { LOGERROR("[%s][%s] failed to disconnect volume\n", instance->instanceId, volume->volumeId); } } // see if instance directory is there (sometimes startup fails before it is created) set_path(path, sizeof(path), instance, NULL); if (check_path(path)) return (ret); // to ensure that we are able to delete all blobs, we chown files back to 'eucalyptus' // (e.g., libvirt on KVM on Maverick chowns them to libvirt-qemu while // VM is running and then chowns them to root after termination) { DIR *dir = NULL; if ((dir = opendir(path)) == NULL) { return (-1); } struct dirent *dir_entry; while ((dir_entry = readdir(dir)) != NULL) { char *entry_name = dir_entry->d_name; if (!strcmp(".", entry_name) || !strcmp("..", entry_name)) continue; // get the path of the directory item char entry_path[BLOBSTORE_MAX_PATH]; snprintf(entry_path, sizeof(entry_path), "%s/%s", path, entry_name); if (diskutil_ch(entry_path, EUCALYPTUS_ADMIN, NULL, BACKING_FILE_PERM)) { LOGWARN("[%s] failed to chown files before cleanup\n", instance->instanceId); } } closedir(dir); } if (do_destroy_files) { set_id2(instance, "/.*", work_regex, sizeof(work_regex)); if (blobstore_delete_regex(work_bs, work_regex) == -1) { LOGERROR("[%s] failed to remove some artifacts in %s\n", instance->instanceId, path); } for (i = 0; i < EUCA_MAX_VOLUMES; ++i) { volume = &instance->volumes[i]; snprintf(path, sizeof(path), EUCALYPTUS_VOLUME_XML_PATH_FORMAT, instance->instancePath, volume->volumeId); unlink(path); } // delete all non-blob files in the directory if (blobstore_delete_nonblobs(work_bs, instance->instancePath) < 0) { LOGWARN("[%s] failed to delete some non-blob files under %s\n", instance->instanceId, instance->instancePath); } } // Finally try to remove the directory. If either the user or our code introduced // any new files, this last step will fail. set_path(path, sizeof(path), instance, NULL); if (rmdir(path) && do_destroy_files) { LOGWARN("[%s] failed to remove backing directory %s\n", instance->instanceId, path); } return (ret); }