void XSLoadHostThread::run() { QMutexLocker(data->xen_lock); emit progressChanged(0, trUtf8("Connecting to %1").arg(data->name)); data->session = xen_session_login_with_password(call_func, reinterpret_cast<void*>(data), data->username.toUtf8().data(), data->password.toUtf8().data(), xen_api_latest_version); if(data->session == nullptr) return error(trUtf8("Failed to connect to %1").arg(data->name), nullptr); data->connected = true; data->connect_button->setText(trUtf8("Disconnect")); emit progressChanged(10, trUtf8("Fetching Host")); xen_host host_handle = nullptr; if(!xen_session_get_this_host(data->session, &host_handle, data->session)) return error(trUtf8("Failed to fetch host!"), data->session); emit progressChanged(15, trUtf8("Fetching Data")); if(!xen_host_get_record(data->session, &data->data, host_handle)) return error(trUtf8("Failed to fetch data!"), data->session); emit progressChanged(20, trUtf8("Fetching VMs")); xen_vm_xen_vm_record_map *vms; if(!xen_vm_get_all_records(data->session, &vms)) return error(trUtf8("Failed to fetch VMs!"), data->session); data->vm_list_widget->clear(); data->vm_count = vms->size; data->vms = new xsvm_data*[vms->size]; for(size_t i = 0; i < data->vm_count; i++) { xen_vm_record *vm = vms->contents[i].val; xsvm_data *vm_data = new xsvm_data; vm_data->data = vm; vm_data->host = data; vm_data->tunnel = nullptr; vm_data->window = nullptr; data->vms[i] = vm_data; if(vm->is_a_snapshot || vm->is_a_template) continue; doInGUIThread([this,vm_data] { QListWidgetItem *item = new QListWidgetItem(QString::fromUtf8(vm_data->data->name_label), data->vm_list_widget); item->setData(Qt::UserRole, QVariant::fromValue(reinterpret_cast<void*>(vm_data))); }); } doInGUIThread([this] { data->vm_list_widget->setDisabled(false); }); emit progressChanged(100, trUtf8("Connected to %1").arg(data->name)); }
int main(int argc, char **argv) { if (argc != 4) { fprintf(stderr, "Usage:\n\n%s <url> <username> <password>\n", argv[0]); } url = argv[1]; /* General setup */ xen_init(); curl_global_init(CURL_GLOBAL_ALL); xen_session *session = xen_session_login_with_password(call_func, NULL, argv[2], argv[3]); if(session->ok) { /* Get the host */ xen_host host; xen_session_get_this_host(session, &host, session); /* Get the set of VMs */ struct xen_vm_set * VMs; xen_host_get_resident_vms(session, &VMs, host); /* Print the names */ for(unsigned int i=0 ; i<VMs->size ; i++) { char * name; xen_host_get_name_label(session, &name, host); printf("VM %d: %s\n", i, name); } } else { printf(stderr, "Connection failed\n"); } xen_session_logout(session); curl_global_cleanup(); xen_fini(); return 0; }
int main(int argc, char **argv) { if (argc != 5) { usage(); } url = argv[1]; char *sr_name = argv[2]; char *username = argv[3]; char *password = argv[4]; xmlInitParser(); xmlKeepBlanksDefault(0); xen_init(); curl_global_init(CURL_GLOBAL_ALL); #define CLEANUP \ do { \ xen_session_logout(session); \ curl_global_cleanup(); \ xen_fini(); \ xmlCleanupParser(); \ } while(0) \ xen_session *session = xen_session_login_with_password(call_func, NULL, username, password, xen_api_latest_version); /* --------------------------------------------------------------------- Read host, capabilities and API vsn --------------------------------------------------------------------- */ printf("\n\nQuerying host...\n"); xen_host host; if (!xen_session_get_this_host(session, &host, session)) { print_error(session); CLEANUP; return 1; } xen_string_string_map *versions; if (!xen_host_get_software_version(session, &versions, host)) { print_error(session); xen_host_free(host); CLEANUP; return 1; } xen_string_set *supported_bootloaders; if (!xen_host_get_supported_bootloaders(session, &supported_bootloaders, host)) { print_error(session); xen_string_string_map_free(versions); xen_host_free(host); CLEANUP; return 1; } xen_string_set *capabilities; if (!xen_host_get_capabilities(session, &capabilities, host)) { print_error(session); xen_string_set_free(supported_bootloaders); xen_string_string_map_free(versions); xen_host_free(host); CLEANUP; return 1; } for (size_t i = 0; i < versions->size; i++) { printf("%s -> %s.\n", versions->contents[i].key, versions->contents[i].val); } printf("Host supports the following bootloaders:"); for (size_t i = 0; i < supported_bootloaders->size; i++) { printf(" %s", supported_bootloaders->contents[i]); } printf("\n"); printf("Host has the following capabilities:"); for (size_t i = 0; i < capabilities->size; i++) { printf(" %s", capabilities->contents[i]); } printf("\n"); xen_host_free(host); xen_string_string_map_free(versions); xen_string_set_free(supported_bootloaders); xen_string_set_free(capabilities); /* --------------------------------------------------------------------- Create a new VM with a blank disk: --------------------------------------------------------------------- */ printf("\n\nCreating new HVM VM...\n"); xen_vm hvm_vm = create_new_vm(session, "Other install media", sr_name, false); if (!session->ok) { /* Error has been logged, just clean up. */ CLEANUP; return 1; } print_vm_power_state(session, hvm_vm); /* Blob handling used to crash when we tried to free the VM record: * CA-38872. */ xen_blob blob; xen_blob_create(session, &blob, "hello", false); xen_vm_create_new_blob(session, &blob, hvm_vm, "test", "test", false); printf("\nBlob created.\n"); xen_blob_record *blob_record; xen_blob_get_record(session, &blob_record, blob); printf("Blob record retrieved.\n"); printf("\nGetting VM record...\n"); xen_vm_record *vm_record; xen_vm_get_record(session, &vm_record, hvm_vm); printf("VM record: %s blob: %s\n", (char *)vm_record->handle, (char *)blob_record->handle); printf("Freeing VM record...\n"); xen_vm_record_free(vm_record); printf("VM record freed.\n"); printf("Freeing blob record...\n"); xen_blob_record_free(blob_record); printf("Blob record freed.\n"); if (!session->ok) { /* Error has been logged, just clean up. */ xen_vm_free(hvm_vm); CLEANUP; return 1; } /* Test Enum parsing by setting actions after shutdown */ xen_vm_set_actions_after_shutdown(session, hvm_vm, XEN_ON_NORMAL_EXIT_RESTART); /* Test getting a map and having a play */ xen_string_string_map *hvm_boot_params; if (!xen_vm_get_hvm_boot_params(session, &hvm_boot_params, hvm_vm)) { print_error(session); CLEANUP; return 1; } printf("HVM_boot_params contains:\n"); for (size_t i = 0; i < hvm_boot_params->size; i++) { printf("%s -> %s.\n", hvm_boot_params->contents[i].key, hvm_boot_params->contents[i].val); } xen_string_string_map_free(hvm_boot_params); cycle_vm(session, hvm_vm); xen_vm_free(hvm_vm); /* TODO uncomment this when we test against real hosts, as SDK doesn't have debian template printf("\n\nCreating new PV VM...\n"); xen_vm pv_vm = create_new_vm(session, "Debian Etch 4.0", sr_name, true); if (!session->ok) { CLEANUP; return 1; } if (!xen_vm_get_hvm_boot_params(session, &hvm_boot_params, pv_vm)) { print_error(session); CLEANUP; return 1; } for (size_t i = 0; i < hvm_boot_params->size; i++) { printf("%s -> %s.\n", hvm_boot_params->contents[i].key, hvm_boot_params->contents[i].val); } cycle_vm(session, pv_vm); xen_vm_free(pv_vm); */ CLEANUP; }
int main(int argc, char **argv) { if (argc != 6) { usage(); } url = argv[1]; char *username = argv[2]; char *password = argv[3]; source_url = argv[4]; target_url = argv[5]; xmlInitParser(); xmlKeepBlanksDefault(0); xen_init(); curl_global_init(CURL_GLOBAL_ALL); #define CLEANUP \ do { \ xen_session_logout(session); \ curl_global_cleanup(); \ xen_fini(); \ xmlCleanupParser(); \ } while(0) \ xen_session *session = xen_session_login_with_password(call_func, NULL, username, password, xen_api_latest_version); printf("\n\nQuerying host...\n"); xen_host host; if (!xen_session_get_this_host(session, &host, session)) { print_error(session); CLEANUP; return 1; } /* Read in the source host and target host using their name labels */ xen_host_set *source_hosts; int rc = 0; rc = get_host_names(session, &source_hosts, source_url ); if (rc !=0 ) { fprintf(stderr, "source host lookup failed.\n"); print_error(session); return 1; } xen_host_set *target_hosts; rc = 0; rc = get_host_names(session, &target_hosts, target_url ); if (rc !=0 ) { fprintf(stderr, "target host lookup failed.\n"); print_error(session); return 1; } struct xen_vm_set *all_vms_in_pool; xen_vm_get_all(session, &all_vms_in_pool); bool *vm_to_be_migrated = calloc(all_vms_in_pool->size, sizeof(bool)); int num_vms_to_migrate = 0; enum xen_task_status_type task_status; xen_vm_record* result; for (size_t i = 0; i < all_vms_in_pool->size; i++ ) { xen_vm a_vm = all_vms_in_pool->contents[i]; xen_vm_get_record(session, &result, a_vm); /* * we can only migrate VMs that are * -not templates * -not control domains * -and are running * * resident_on is used to identify the eligible VMs on the user * requested source_host */ if ( !result->is_a_template && !result->is_control_domain && (result->power_state == XEN_VM_POWER_STATE_RUNNING) && (strcmp(result->resident_on->u.handle, (char*)source_hosts->contents[0]) == 0) ) { // flag this VM as one suitable to migrate vm_to_be_migrated[i] = true; num_vms_to_migrate++; } else { vm_to_be_migrated[i] = false; } } if (!session->ok) { /* Error has been logged, just clean up. */ xen_host_set_free(source_hosts); xen_host_set_free(target_hosts); xen_vm_set_free(all_vms_in_pool); xen_vm_record_free(result); free(vm_to_be_migrated); CLEANUP; return 1; } xen_task* task_list = calloc(num_vms_to_migrate, sizeof(xen_task)); xen_string_string_map* options = xen_string_string_map_alloc(0); xen_string_set *error_msgs = NULL; int idx = 0; for (size_t i = 0; i < all_vms_in_pool->size; i++ ) { if (vm_to_be_migrated[i] == true) { xen_vm_pool_migrate_async(session, &task_list[idx], all_vms_in_pool->contents[i], target_hosts->contents[0], options ); idx++; printf(" Migrating VM %zd \n", i); } } // time out after certain number of iterations int max_iter = 50; int iter = 0; int pause_interval_secs = 4; int tasks_running = 0; int tasks_completed = 0; xen_task a_task; bool tasks_still_pending = true; /* Poll how many of the migration tasks have completed. * * The task querying below isn't is intended to provide a sample of useful * syntax. In practice a user would probably consider using functions such * as xen_task_cancel or indeed the asynchronous equivalent * xen_task_cancel_async. * These functions and other task handling ones are defined in xen_task.c */ while ( iter < max_iter && tasks_still_pending ) { tasks_running = 0; tasks_completed = 0; for (int j = 0; j < num_vms_to_migrate; j++) { a_task = task_list[j]; xen_task_get_status(session, &task_status, a_task); if (task_status == XEN_TASK_STATUS_TYPE_PENDING) { tasks_running++; } else { /* See the xen_task_status_type enum definitions * defined in xen_task_status_type.h a task can have * failed, or be cancelled or in the process of being cancelled * amongst others. * The definition of tasks_completed in this context is tasks * not pending. */ if (task_status == XEN_TASK_STATUS_TYPE_FAILURE) { if (xen_task_get_error_info(session, &error_msgs, task_list[j] )) { /* VMs may need to meet certain criteria for migration to be * possible between hosts; such as shared storage between * hosts. It is advisable to check the criteria needed for * migration on the particular version of XenServer. * The error messages output below should give information * that allows the identification of an unsupported * operation */ printf("-------------------------------------\n"); printf("Failed while trying to migrate VM: \n"); for(size_t k=0; k<error_msgs->size; k++) { printf("error_msg %zu : %s \n", k, error_msgs->contents[k]); } } } tasks_completed++; } } if (tasks_running == 0) { tasks_still_pending = false; // stop the iteration early printf("All tasks completed \n"); } printf("*********************************************\n"); printf("VM migration progress, poll number %d \n", iter); printf("----------------------------------------\n"); printf(" Tasks pending : %d \n", tasks_running); printf(" ended : %d \n", tasks_completed); printf("*********************************************\n"); iter++; sleep(pause_interval_secs); } xen_string_set_free(error_msgs); xen_string_string_map_free(options); xen_host_set_free(source_hosts); xen_host_set_free(target_hosts); free(task_list); free(vm_to_be_migrated); xen_vm_set_free(all_vms_in_pool); CLEANUP; }