static int testDomainDefine(const void *data) { const objecteventTest *test = data; lifecycleEventCounter counter; int eventId = VIR_DOMAIN_EVENT_ID_LIFECYCLE; virDomainPtr dom = NULL; int id; int ret = 0; lifecycleEventCounter_reset(&counter); id = virConnectDomainEventRegisterAny(test->conn, NULL, eventId, VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb), &counter, NULL); /* Make sure the define event is triggered */ dom = virDomainDefineXML(test->conn, domainDef); if (dom == NULL || virEventRunDefaultImpl() < 0) { ret = -1; goto cleanup; } if (counter.defineEvents != 1 || counter.unexpectedEvents > 0) { ret = -1; goto cleanup; } /* Make sure the undefine event is triggered */ virDomainUndefine(dom); if (virEventRunDefaultImpl() < 0) { ret = -1; goto cleanup; } if (counter.undefineEvents != 1 || counter.unexpectedEvents > 0) { ret = -1; goto cleanup; } cleanup: virConnectDomainEventDeregisterAny(test->conn, id); if (dom != NULL) virDomainFree(dom); return ret; }
int main(int argc, char *argv[]) { virConnectPtr conn; virDomainPtr dom; char *xmlconfig = NULL; if(argc != 2) { printf("usage: ./define_domain domain.xml\n"); return -1; } if ((xmlconfig = GetXml(argv[1])) == NULL) { fprintf(stderr, "Failed to get xml\n"); return -1; } conn = virConnectOpen("qemu:///system"); if (conn == NULL) { fprintf(stderr, "Failed to open connection to qemu:///system\n"); free(xmlconfig); return -1; } dom = virDomainDefineXML(conn, xmlconfig); if (!dom) { printf("Domain is not found\n"); free(xmlconfig); virConnectClose(conn); return -1; } fprintf(stderr, "Guest %s is defined\n", virDomainGetName(dom)); free(xmlconfig); virDomainFree(dom); virConnectClose(conn); return 0; }
Manageable::status_t NodeWrap::ManagementMethod(uint32_t methodId, Args& args, std::string &errstr) { virDomainPtr domain_ptr; cout << "Method Received: " << methodId << endl; int ret; switch (methodId) { case _qmf::Node::METHOD_DOMAINDEFINEXML: { _qmf::ArgsNodeDomainDefineXML *io_args = (_qmf::ArgsNodeDomainDefineXML *) &args; domain_ptr = virDomainDefineXML(conn, io_args->i_xmlDesc.c_str()); if (!domain_ptr) { errstr = FORMAT_ERR(conn, "Error creating domain using xml description (virDomainDefineXML).", &ret); return STATUS_USER + ret; } else { // Now we have to check to see if this domain is actually new or not, because it's possible that // one already exists with this name/description and we just replaced it.. *ugh* for (std::vector<DomainWrap*>::iterator iter = domains.begin(); iter != domains.end();) { if (strcmp((*iter)->domain_name.c_str(), virDomainGetName(domain_ptr)) == 0) { // We're just replacing an existing domain, however I'm pretty sure the // old domain pointer becomes invalid at this point, so we should destroy // the old domain reference. The other option would be to replace it and // keep the object valid.. not sure which is better. printf("Old domain already exists, removing it in favor of new object."); delete(*iter); iter = domains.erase(iter); } else { iter++; } } DomainWrap *domain; try { domain = new DomainWrap(agent, this, domain_ptr, conn); domains.push_back(domain); io_args->o_domain = domain->GetManagementObject()->getObjectId(); } catch (int i) { delete domain; errstr = FORMAT_ERR(conn, "Error constructing domain object in virDomainDefineXML.", &ret); return STATUS_USER + i; } return STATUS_OK; } } case _qmf::Node::METHOD_STORAGEPOOLDEFINEXML: { _qmf::ArgsNodeStoragePoolDefineXML *io_args = (_qmf::ArgsNodeStoragePoolDefineXML *) &args; virStoragePoolPtr pool_ptr; pool_ptr = virStoragePoolDefineXML (conn, io_args->i_xmlDesc.c_str(), 0); if (pool_ptr == NULL) { errstr = FORMAT_ERR(conn, "Error defining storage pool using xml description (virStoragePoolDefineXML).", &ret); return STATUS_USER + ret; } PoolWrap *pool; try { pool = new PoolWrap(agent, this, pool_ptr, conn); pools.push_back(pool); io_args->o_pool = pool->GetManagementObject()->getObjectId(); } catch (int i) { delete pool; errstr = FORMAT_ERR(conn, "Error constructing pool object in virStoragePoolDefineXML.", &ret); return STATUS_USER + i; } return STATUS_OK; } case _qmf::Node::METHOD_STORAGEPOOLCREATEXML: { _qmf::ArgsNodeStoragePoolCreateXML *io_args = (_qmf::ArgsNodeStoragePoolCreateXML *) &args; virStoragePoolPtr pool_ptr; pool_ptr = virStoragePoolCreateXML (conn, io_args->i_xmlDesc.c_str(), 0); if (pool_ptr == NULL) { errstr = FORMAT_ERR(conn, "Error creating storage pool using xml description (virStoragePoolCreateXML).", &ret); return STATUS_USER + ret; } PoolWrap *pool; try { pool = new PoolWrap(agent, this, pool_ptr, conn); pools.push_back(pool); io_args->o_pool = pool->GetManagementObject()->getObjectId(); } catch (int i) { delete pool; errstr = FORMAT_ERR(conn, "Error constructing pool object in virStoragePoolCreateXML.", &ret); return STATUS_USER + i; } return STATUS_OK; } case _qmf::Node::METHOD_FINDSTORAGEPOOLSOURCES: { _qmf::ArgsNodeFindStoragePoolSources *io_args = (_qmf::ArgsNodeFindStoragePoolSources *) &args; char *xml_result; xml_result = virConnectFindStoragePoolSources(conn, io_args->i_type.c_str(), io_args->i_srcSpec.c_str(), 0); if (xml_result == NULL) { errstr = FORMAT_ERR(conn, "Error creating storage pool using xml description (virStoragePoolCreateXML).", &ret); return STATUS_USER + ret; } io_args->o_xmlDesc = xml_result; free(xml_result); return STATUS_OK; } } return STATUS_NOT_IMPLEMENTED; }
std::string VM_Controller::new_vm_config(std::string vm_name, std::string img_path, std::string config_path, int cpu_num, int mem_in_mb){ Json::Value j; tinyxml2::XMLDocument doc; doc.LoadFile(config_path.c_str()); tinyxml2::XMLElement *root = doc.RootElement(); if(root == NULL){ std::string error = "could not open "; error += config_path; LOG_ERROR(error); j["status"] = error; return j.toStyledString(); } tinyxml2::XMLElement *name = root->FirstChildElement("name"); if(name){ name->SetText(vm_name.c_str()); } tinyxml2::XMLElement *vcpu = root->FirstChildElement("vcpu"); if(vcpu){ vcpu->SetText(Tools::to_string(cpu_num, 10).c_str()); } tinyxml2::XMLElement *uuid = root->FirstChildElement("uuid"); if(uuid){ char uuid_str[37] = {0}; uuid_t uuid_v; uuid_generate(uuid_v); uuid_unparse(uuid_v, uuid_str); uuid->SetText(uuid_str); } tinyxml2::XMLElement *memory = root->FirstChildElement("memory"); if(memory){ memory->SetText(Tools::to_string(mem_in_mb, 10).c_str()); } root->FirstChildElement("devices") ->FirstChildElement("disk") ->FirstChildElement("source") ->SetAttribute("file", img_path.c_str()); if(doc.SaveFile(config_path.c_str()) != 0){ j["status"] = "save config file error"; return j.toStyledString(); } tinyxml2::XMLPrinter stream; doc.Print(&stream); virDomainPtr domain = virDomainDefineXML(_conn, stream.CStr()); if(domain == NULL){ j["status"] = "define domain error"; return j.toStyledString(); } else{ virDomainFree(domain); j["status"] = "ok"; return j.toStyledString(); } }
static int testDomainCreateXMLMixed(const void *data) { const objecteventTest *test = data; lifecycleEventCounter counter; virDomainPtr dom; int ret = -1; int id1 = -1; int id2 = -1; bool registered = false; lifecycleEventCounter_reset(&counter); /* Fun with mixing old and new API, also with global and * per-domain. Handler should be fired three times, once for each * registration. */ dom = virDomainDefineXML(test->conn, domainDef); if (dom == NULL) goto cleanup; id1 = virConnectDomainEventRegisterAny(test->conn, dom, VIR_DOMAIN_EVENT_ID_LIFECYCLE, VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb), &counter, NULL); if (id1 < 0) goto cleanup; if (virConnectDomainEventRegister(test->conn, domainLifecycleCb, &counter, NULL) != 0) goto cleanup; registered = true; id2 = virConnectDomainEventRegisterAny(test->conn, NULL, VIR_DOMAIN_EVENT_ID_LIFECYCLE, VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb), &counter, NULL); if (id2 < 0) goto cleanup; dom = virDomainCreateXML(test->conn, domainDef, 0); if (dom == NULL || virEventRunDefaultImpl() < 0) goto cleanup; if (counter.startEvents != 3 || counter.unexpectedEvents > 0) goto cleanup; if (virConnectDomainEventDeregister(test->conn, domainLifecycleCb) != 0) goto cleanup; registered = false; if (virConnectDomainEventDeregisterAny(test->conn, id1) != 0) goto cleanup; id1 = -1; if (virConnectDomainEventDeregisterAny(test->conn, id2) != 0) goto cleanup; id2 = -1; ret = 0; cleanup: if (id1 >= 0) virConnectDomainEventDeregisterAny(test->conn, id1); if (id2 >= 0) virConnectDomainEventDeregisterAny(test->conn, id2); if (registered) virConnectDomainEventDeregister(test->conn, domainLifecycleCb); if (dom != NULL) { virDomainUndefine(dom); virDomainDestroy(dom); virDomainFree(dom); } return ret; }