static int __domain_op_simple(char * name, int op) { if (g_conn == NULL) return -1; virDomainPtr domain; domain = virDomainLookupByName(g_conn, name); if (domain == NULL) { logerror(_("%s: connect domain by name(%s) error. " "domain may not exist\n"), __func__, name); return -1; } int ret; if (op == __DOMAIN_OP_STOP) ret = virDomainShutdown(domain); else if (op == __DOMAIN_OP_STOP_FORCE) ret = virDomainDestroy(domain); else if (op == __DOMAIN_OP_REBOOT) ret = virDomainReboot(domain, 0); else ret = -1; virDomainFree(domain); return ret; }
static PromiseResult DownVirt(EvalContext *ctx, virConnectPtr vc, Attributes a, const Promise *pp) { virDomainPtr dom; virDomainInfo info; dom = virDomainLookupByName(vc, pp->promiser); PromiseResult result = PROMISE_RESULT_NOOP; if (dom) { if (virDomainGetInfo(dom, &info) == -1) { cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Unable to probe virtual domain '%s'", pp->promiser); result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL); virDomainFree(dom); return result; } switch (info.state) { case VIR_DOMAIN_BLOCKED: case VIR_DOMAIN_RUNNING: if (virDomainShutdown(dom) == -1) { cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' failed to shutdown", pp->promiser); result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED); virDomainFree(dom); return result; } cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Virtual domain '%s' running, terminating", pp->promiser); result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE); break; case VIR_DOMAIN_SHUTOFF: case VIR_DOMAIN_SHUTDOWN: cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Virtual domain '%s' is down - promise kept", pp->promiser); break; case VIR_DOMAIN_PAUSED: cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' is suspended - ignoring promise", pp->promiser); result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED); break; case VIR_DOMAIN_CRASHED: if (virDomainSuspend(dom) == -1) { cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' is crashed and failed to shutdown", pp->promiser); result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED); virDomainFree(dom); return false; } cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Virtual domain '%s' is in a crashed state, terminating", pp->promiser); result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE); break; default: Log(LOG_LEVEL_VERBOSE, "Virtual domain '%s' is reported as having no state, whatever that means", pp->promiser); break; } virDomainFree(dom); } else { cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Virtual domain '%s' cannot be found - take promise as kept", pp->promiser); } return result; }
static int DownVirt(virConnectPtr vc, char *uri, Attributes a, Promise *pp) { virDomainPtr dom; virDomainInfo info; dom = virDomainLookupByName(vc, pp->promiser); if (dom) { if (virDomainGetInfo(dom, &info) == -1) { cfPS(cf_inform, CF_FAIL, "", pp, a, " !! Unable to probe virtual domain \"%s\"", pp->promiser); virDomainFree(dom); return false; } switch (info.state) { case VIR_DOMAIN_BLOCKED: case VIR_DOMAIN_RUNNING: if (virDomainShutdown(dom) == -1) { cfPS(cf_verbose, CF_INTERPT, "", pp, a, " -> Virtual domain \"%s\" failed to shutdown!\n", pp->promiser); virDomainFree(dom); return false; } cfPS(cf_verbose, CF_CHG, "", pp, a, " -> Virtual domain \"%s\" running, terminating\n", pp->promiser); break; case VIR_DOMAIN_SHUTOFF: case VIR_DOMAIN_SHUTDOWN: cfPS(cf_verbose, CF_NOP, "", pp, a, " -> Virtual domain \"%s\" is down - promise kept\n", pp->promiser); break; case VIR_DOMAIN_PAUSED: cfPS(cf_verbose, CF_INTERPT, "", pp, a, " -> Virtual domain \"%s\" is suspended - ignoring promise\n", pp->promiser); break; case VIR_DOMAIN_CRASHED: if (virDomainSuspend(dom) == -1) { cfPS(cf_verbose, CF_INTERPT, "", pp, a, " -> Virtual domain \"%s\" is crashed and failed to shutdown\n", pp->promiser); virDomainFree(dom); return false; } cfPS(cf_verbose, CF_CHG, "", pp, a, " -> Virtual domain \"%s\" is in a crashed state, terminating\n", pp->promiser); break; default: CfOut(cf_verbose, "", " !! Virtual domain \"%s\" is reported as having no state, whatever that means", pp->promiser); break; } virDomainFree(dom); } else { cfPS(cf_verbose, CF_NOP, "", pp, a, " -> Virtual domain \"%s\" cannot be found - take promise as kept\n", pp->promiser); } return true; }
static int DownVirt(EvalContext *ctx, virConnectPtr vc, Attributes a, Promise *pp) { virDomainPtr dom; virDomainInfo info; dom = virDomainLookupByName(vc, pp->promiser); if (dom) { if (virDomainGetInfo(dom, &info) == -1) { cfPS(ctx, OUTPUT_LEVEL_INFORM, PROMISE_RESULT_FAIL, "", pp, a, " !! Unable to probe virtual domain \"%s\"", pp->promiser); virDomainFree(dom); return false; } switch (info.state) { case VIR_DOMAIN_BLOCKED: case VIR_DOMAIN_RUNNING: if (virDomainShutdown(dom) == -1) { cfPS(ctx, OUTPUT_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, "", pp, a, " -> Virtual domain \"%s\" failed to shutdown!\n", pp->promiser); virDomainFree(dom); return false; } cfPS(ctx, OUTPUT_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, "", pp, a, " -> Virtual domain \"%s\" running, terminating\n", pp->promiser); break; case VIR_DOMAIN_SHUTOFF: case VIR_DOMAIN_SHUTDOWN: cfPS(ctx, OUTPUT_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, "", pp, a, " -> Virtual domain \"%s\" is down - promise kept\n", pp->promiser); break; case VIR_DOMAIN_PAUSED: cfPS(ctx, OUTPUT_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, "", pp, a, " -> Virtual domain \"%s\" is suspended - ignoring promise\n", pp->promiser); break; case VIR_DOMAIN_CRASHED: if (virDomainSuspend(dom) == -1) { cfPS(ctx, OUTPUT_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, "", pp, a, " -> Virtual domain \"%s\" is crashed and failed to shutdown\n", pp->promiser); virDomainFree(dom); return false; } cfPS(ctx, OUTPUT_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, "", pp, a, " -> Virtual domain \"%s\" is in a crashed state, terminating\n", pp->promiser); break; default: CfOut(OUTPUT_LEVEL_VERBOSE, "", " !! Virtual domain \"%s\" is reported as having no state, whatever that means", pp->promiser); break; } virDomainFree(dom); } else { cfPS(ctx, OUTPUT_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, "", pp, a, " -> Virtual domain \"%s\" cannot be found - take promise as kept\n", pp->promiser); } return true; }
void main() { virConnectPtr connection; virDomainPtr vdp_id, vdp_name; int i,ret_val=-1; int ch,ch_id,domainnum; int *domains; char name[25]; const char *active_name; connection = virConnectOpen("xen:///"); if (connection == NULL) { fprintf(stderr, "Error opening connection to XEN:/// \n"); exit(1); } else { domainnum=virConnectNumOfDomains(connection); domains=malloc(sizeof(int) * domainnum); domainnum = virConnectListDomains(connection, domains, domainnum); printf("\t\t ----Active Domains---- \n"); for (i = 0 ; i < domainnum ; i++) { vdp_id=virDomainLookupByID(connection,i); active_name=virDomainGetName(vdp_id); printf(" \t\t%s \n", active_name); } free(domains); while(1) { printf("1.Start\n2.Suspend\n3.Resume\n4.stop\n5.exit\n "); printf("Enter Your Choice : "); scanf("%d",&ch); if(ch != 5) { printf("\n Please Insert the Active Domian Name: "); scanf("%s",name); vdp_name = virDomainLookupByName(connection,name); } switch(ch) { case 1: ret_val=virDomainCreate(vdp_name); if(ret_val==0) printf("Domain %s started\n", name); else printf("Start Operation Failed\n"); break; case 2: ret_val=virDomainSuspend(vdp_name); if(ret_val==0) printf("Domain %s Suspended\n", name); else printf("Suspend Operation Failed\n"); break; case 3: ret_val=virDomainResume(vdp_name); if(ret_val==0) printf("Domain %s Resumed\n", name); else printf("Resume Operation Failed\n"); break; case 4: ret_val=virDomainShutdown(vdp_name); if(ret_val==0) printf("Domain %s Stopped\n", name); else printf("Stop Operation Failed\n"); break; case 5: exit(1); default: exit(1); } } virConnectClose(connection); } }
void main() { int i,val=-1,ch,no_domains; char name[50]; int *act_domains; virConnectPtr con_ptr; virDomainPtr dom_ptr; con_ptr = virConnectOpen("xen:///"); if (con_ptr == NULL) { fprintf(stderr, "Error opening connection to XEN:/// \n"); return 1; } else { no_domains=virConnectNumOfDomains(con_ptr); act_domains=malloc(sizeof(int) * no_domains); no_domains = virConnectListDomains(con_ptr, act_domains, no_domains); free(act_domains); while(1) { printf("1.Start\n2.Suspend\n3.Resume\n4.Shutdown\n5.exit "); scanf("%d",&ch); printf("\n Enter Active Domian name "); scanf("%s",&name); dom_ptr=virDomainLookupByName(con_ptr,name); switch(ch) { case 1: val=virDomainCreate(dom_ptr); if(val==0) printf("Success"); else printf("Failed"); break; case 2: val=virDomainSuspend(dom_ptr); if(val==0) printf("Success"); else printf("Failed"); break; case 3: val=virDomainResume(dom_ptr); if(val==0) printf("Success"); else printf("Failed"); break; case 4: val=virDomainShutdown(dom_ptr); if(val==0) printf("Success"); else printf("Failed"); break; default:exit(1); } } virConnectClose(con_ptr); } }
/** * @brief shuts down the domain * @return true if the domain was shut down without error, otherwise false */ bool Domain::shutdown() { // TODO(txwikinger): check if it can be stopped return (virDomainShutdown(m_domain) == 0); }
bool VirshType::Shutdown() { vmprintf(D_FULLDEBUG, "Inside VirshType::Shutdown\n"); if( (m_configfile.Length() == 0) ) { m_result_msg = VMGAHP_ERR_INTERNAL; return false; } if( getVMStatus() == VM_STOPPED ) { if( m_self_shutdown ) { if( m_vm_no_output_vm ) { // A job user doesn't want to get back VM files. // So we will delete all files in working directory. m_delete_working_files = true; m_is_checkpointed = false; }else { if( !m_suspendfile.IsEmpty() ) { unlink(m_suspendfile.Value()); } m_suspendfile = ""; // delete the created xen vm configuration file if( !m_configfile.IsEmpty() ) { unlink(m_configfile.Value()); } // delete the checkpoint timestamp file MyString tmpfilename; tmpfilename.formatstr("%s%c%s", m_workingpath.Value(), DIR_DELIM_CHAR, XEN_CKPT_TIMESTAMP_FILE); unlink(tmpfilename.Value()); // We need to update timestamp of transferred writable disk files updateLocalWriteDiskTimestamp(time(NULL)); } } // We here set m_self_shutdown to false // So, above functions will not be called twice m_self_shutdown = false; return true; } // if( getVMStatus() == VM_SUSPENDED ) // do nothing // If a VM is soft suspended, resume it first ResumeFromSoftSuspend(); if( getVMStatus() == VM_RUNNING ) { priv_state priv = set_root_priv(); virDomainPtr dom = virDomainLookupByName(m_libvirt_connection, m_vm_name.Value()); set_priv(priv); if(dom == NULL) { virErrorPtr err = virConnGetLastError(m_libvirt_connection); if (err && err->code != VIR_ERR_NO_DOMAIN) { vmprintf(D_ALWAYS, "Error finding domain %s: %s\n", m_vm_name.Value(), (err ? err->message : "No reason found")); return false; } } else { priv = set_root_priv(); int result = virDomainShutdown(dom); virDomainFree(dom); set_priv(priv); if( result != 0 ) { // system error happens // killing VM by force killVM(); } } // Now we don't need working files any more m_delete_working_files = true; m_is_checkpointed = false; } setVMStatus(VM_STOPPED); m_stop_time.getTime(); return true; }