Exemplo n.º 1
0
bool
VirshType::ResumeFromSoftSuspend(void)
{
	vmprintf(D_FULLDEBUG, "Inside VirshType::ResumeFromSoftSuspend\n");
	if( (m_configfile.Length() == 0)) {
		return false;
	}

	if( m_is_soft_suspended ) {

		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);
		    vmprintf(D_ALWAYS, "Error finding domain %s: %s\n", m_vm_name.Value(), (err ? err->message : "No reason found"));
		    return false;
		  }

		priv = set_root_priv();
		int result = virDomainResume(dom);
		virDomainFree(dom);
		set_priv(priv);
		if( result != 0 ) {
			// unpause failed.
			vmprintf(D_ALWAYS, "Unpausing VM failed in "
					"VirshType::ResumeFromSoftSuspend\n");
			return false;
		}
		m_is_soft_suspended = false;
	}
	return true;
}
Exemplo n.º 2
0
static int
suspend_and_resume(virConnectPtr conn,
                   const char *dom_name,
                   unsigned int seconds)
{
    int ret = -1;
    virDomainPtr dom;
    virDomainInfo dom_info;

    if (!(dom = virDomainLookupByName(conn, dom_name))) {
        ERROR("Unable to find domain '%s'", dom_name);
        goto cleanup;
    }

    if (virDomainGetInfo(dom, &dom_info) < 0) {
        ERROR("Unable to get domain info");
        goto cleanup;
    }

    DEBUG("Domain state %d", dom_info.state);

    switch (dom_info.state) {
    case VIR_DOMAIN_NOSTATE:
    case VIR_DOMAIN_RUNNING:
    case VIR_DOMAIN_BLOCKED:
        /* In these states the domain can be suspended */
        DEBUG("Suspending domain");
        if (virDomainSuspend(dom) < 0) {
            ERROR("Unable to suspend domain");
            goto cleanup;
        }

        DEBUG("Domain suspended. Entering sleep for %u seconds.", seconds);
        sleep(seconds);
        DEBUG("Sleeping done. Resuming the domain.");

        if (virDomainResume(dom) < 0) {
            ERROR("Unable to resume domain");
            goto cleanup;
        }
        break;

    default:
        /* In all other states domain can't be suspended */
        ERROR("Domain is not in a state where it can be suspended: %d",
              dom_info.state);
        goto cleanup;
    }

    ret = 0;
 cleanup:
    if (dom)
        virDomainFree(dom);
    return ret;
}
Exemplo n.º 3
0
/**
 * SuspendAndResumeDomain:
 * @id: the id of the domain
 *
 * extract the domain 0 information
 */
static void
SuspendAndResumeDomain(int id) {
    virDomainPtr dom = NULL;   /* the domain being checked */
    int ret, state;

    /* Find the domain of the given id */
    dom = virDomainLookupByID(conn, id);
    if (dom == NULL) {
        fprintf(stderr, "Failed to find Domain %d\n", id);
        goto error;
    }

    /* Check state */
    state = checkDomainState(dom);
    if ((state == VIR_DOMAIN_RUNNING) ||
        (state == VIR_DOMAIN_NOSTATE) ||
        (state == VIR_DOMAIN_BLOCKED)) {
        printf("Suspending domain...\n");
        ret = virDomainSuspend(dom);
        if (ret < 0) {
            fprintf(stderr, "Failed to suspend Domain %d\n", id);
            goto error;
        }
        state = checkDomainState(dom);
        if (state != VIR_DOMAIN_PAUSED) {
            fprintf(stderr, "Domain %d state is not suspended\n", id);
        } else {
            printf("Domain suspended, resuming it...\n");
        }
        ret = virDomainResume(dom);
        if (ret < 0) {
            fprintf(stderr, "Failed to resume Domain %d\n", id);
            goto error;
        }
        state = checkDomainState(dom);
        if ((state == VIR_DOMAIN_RUNNING) ||
            (state == VIR_DOMAIN_NOSTATE) ||
            (state == VIR_DOMAIN_BLOCKED)) {
            printf("Domain resumed\n");
        } else {
            fprintf(stderr, "Domain %d state indicate it is not resumed\n", id);
        }
    } else {
        fprintf(stderr, "Domain %d is not in a state where it should be suspended\n", id);
        goto error;
    }

error:
    if (dom != NULL)
        virDomainFree(dom);
}
void resume_all_vm() {
    int state, reason, count, i;

    // resume all the VM-es (except for the Domain-0)
    for (i = 1; i < numDomains; i++)
        virDomainResume(activeDomains[i]);

    // capture the time after resume action
    clock_gettime(CLOCK_MONOTONIC, &tp_start);

    // this function will looped until all machines are resumed.
    while (1) {
        count = 0;
        for (i = 1; i < numDomains; i++) {
            // check if the state is RUNNING (or BLOCKED in Xen)
            virDomainGetState(activeDomains[i], &state, &reason, 0);
            if (state == VIR_DOMAIN_BLOCKED || state == VIR_DOMAIN_RUNNING)
                count++;
        }
        if (count == numDomains - 1) break;
    }
}
Exemplo n.º 5
0
static PromiseResult RunningVirt(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);
            virDomainFree(dom);
            return PROMISE_RESULT_FAIL;
        }

        switch (info.state)
        {
        case VIR_DOMAIN_RUNNING:
            cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Virtual domain '%s' running - promise kept", pp->promiser);
            break;

        case VIR_DOMAIN_BLOCKED:
            cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a,
                 "Virtual domain '%s' running but waiting for a resource - promise kept as far as possible",
                 pp->promiser);
            break;

        case VIR_DOMAIN_SHUTDOWN:
            cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' is shutting down", pp->promiser);
            result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
            Log(LOG_LEVEL_VERBOSE,
                  "It is currently impossible to know whether it will reboot or not - deferring promise check until it has completed its shutdown");
            break;

        case VIR_DOMAIN_PAUSED:

            if (virDomainResume(dom) == -1)
            {
                cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' failed to resume after suspension",
                     pp->promiser);
                virDomainFree(dom);
                result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
                return result;
            }

            cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Virtual domain '%s' was suspended, resuming", pp->promiser);
            result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
            break;

        case VIR_DOMAIN_SHUTOFF:

            if (virDomainCreate(dom) == -1)
            {
                cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' failed to resume after halting",
                     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' was inactive, booting...", pp->promiser);
            result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
            break;

        case VIR_DOMAIN_CRASHED:

            if (virDomainReboot(dom, 0) == -1)
            {
                cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' has crashed and rebooting failed",
                     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' has crashed, rebooting...", 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;
        }

        if (a.env.cpus > 0)
        {
            if (virDomainSetVcpus(dom, a.env.cpus) == -1)
            {
                Log(LOG_LEVEL_INFO, " Unable to set the number of cpus to %d", a.env.cpus);
            }
            else
            {
                Log(LOG_LEVEL_INFO, "Setting the number of virtual cpus to %d", a.env.cpus);
            }
        }

        if (a.env.memory != CF_NOINT)
        {
            if (virDomainSetMaxMemory(dom, (unsigned long) a.env.memory) == -1)
            {
                Log(LOG_LEVEL_INFO, " Unable to set the memory limit to %d", a.env.memory);
            }
            else
            {
                Log(LOG_LEVEL_INFO, "Setting the memory limit to %d", a.env.memory);
            }

            if (virDomainSetMemory(dom, (unsigned long) a.env.memory) == -1)
            {
                Log(LOG_LEVEL_INFO, " Unable to set the current memory to %d", a.env.memory);
            }
        }

        if (a.env.disk != CF_NOINT)
        {
            Log(LOG_LEVEL_VERBOSE, "Info: env_disk parameter is not currently supported on this platform");
        }

        virDomainFree(dom);
    }
    else
    {
        Log(LOG_LEVEL_VERBOSE, "Virtual domain '%s' cannot be located, attempting to recreate", pp->promiser);
        result = PromiseResultUpdate(result, CreateVirtDom(ctx, vc, a, pp));
    }

    return result;
}
Exemplo n.º 6
0
Arquivo: test.c Projeto: twintu/cloud
int main(int argc, char *argv[])
{
    virConnectPtr conn;
    virDomainPtr vdp;
    int i=-1,ch; 	
    char *host;
    conn = virConnectOpen("xen:///");
    if (conn == NULL) {
        fprintf(stderr, "Failed to open connection to xen:///\n");
        return 1;
    }
    //host = virConnectGetHostname(conn);

    vdp=virDomainLookupByName(conn,"ubuntu_connection");
    printf("\nMenu\n----\n1.Connect\n2.Suspend\n3.Resume\n 4.Shutdown\n");

    printf("Enter your choice:\n");
    scanf("%d",&ch);

    switch(ch) {

        case 1:

            i=virDomainCreate(vdp);
            if(i==0)
            {
                printf("Success\n");
            }else{
                printf("Failed\n");
            }
            break;


        case 2:

            /* suspend*/	    
            i=virDomainSuspend(vdp);
            if(i==0)
            {
                printf("Success\n");
            }else{
                printf("Failed\n");	
            }
            break;


        case 3:

            /*resume*/
            i=virDomainResume(vdp);
            if(i==0)
            {	
                printf("Success\n");
            }else{
                printf("Failed\n");	
            }
            break;

        case 4:
           
            /*shutdown*/
            i=virDomainDestroy(vdp);
            if(i==0)
            {
                printf("Success\n");
            }else{
                printf("Failed\n");	
            }
            break;        

         default:
        
            printf("INVALID");


            virConnectClose(conn);
            return 0;
    }
}
Exemplo n.º 7
0
static int RunningVirt(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_RUNNING:
            cfPS(cf_verbose, CF_NOP, "", pp, a, " -> Virtual domain \"%s\" running - promise kept\n", pp->promiser);
            break;

        case VIR_DOMAIN_BLOCKED:
            cfPS(cf_verbose, CF_NOP, "", pp, a,
                 " -> Virtual domain \"%s\" running but waiting for a resource - promise kept as far as possible\n",
                 pp->promiser);
            break;

        case VIR_DOMAIN_SHUTDOWN:
            cfPS(cf_verbose, CF_INTERPT, "", pp, a, " -> Virtual domain \"%s\" is shutting down\n", pp->promiser);
            CfOut(cf_verbose, "",
                  " -> It is currently impossible to know whether it will reboot or not - deferring promise check until it has completed its shutdown");
            break;

        case VIR_DOMAIN_PAUSED:

            if (virDomainResume(dom) == -1)
            {
                cfPS(cf_verbose, CF_INTERPT, "", pp, a, " -> Virtual domain \"%s\" failed to resume after suspension\n",
                     pp->promiser);
                virDomainFree(dom);
                return false;
            }

            cfPS(cf_verbose, CF_CHG, "", pp, a, " -> Virtual domain \"%s\" was suspended, resuming\n", pp->promiser);
            break;

        case VIR_DOMAIN_SHUTOFF:

            if (virDomainCreate(dom) == -1)
            {
                cfPS(cf_verbose, CF_INTERPT, "", pp, a, " -> Virtual domain \"%s\" failed to resume after halting\n",
                     pp->promiser);
                virDomainFree(dom);
                return false;
            }

            cfPS(cf_verbose, CF_CHG, "", pp, a, " -> Virtual domain \"%s\" was inactive, booting...\n", pp->promiser);
            break;

        case VIR_DOMAIN_CRASHED:

            if (virDomainReboot(dom, 0) == -1)
            {
                cfPS(cf_verbose, CF_INTERPT, "", pp, a, " -> Virtual domain \"%s\" has crashed and rebooting failed\n",
                     pp->promiser);
                virDomainFree(dom);
                return false;
            }

            cfPS(cf_verbose, CF_CHG, "", pp, a, " -> Virtual domain \"%s\" has crashed, rebooting...\n", pp->promiser);
            break;

        default:
            CfOut(cf_verbose, "", " !! Virtual domain \"%s\" is reported as having no state, whatever that means",
                  pp->promiser);
            break;
        }

        if (a.env.cpus > 0)
        {
            if (virDomainSetVcpus(dom, a.env.cpus) == -1)
            {
                CfOut(cf_inform, "", " !!! Unable to set the number of cpus to %d", a.env.cpus);
            }
            else
            {
                CfOut(cf_inform, "", " -> Setting the number of virtual cpus to %d", a.env.cpus);
            }
        }

        if (a.env.memory != CF_NOINT)
        {
            if (virDomainSetMaxMemory(dom, (unsigned long) a.env.memory) == -1)
            {
                CfOut(cf_inform, "", " !!! Unable to set the memory limit to %d", a.env.memory);
            }
            else
            {
                CfOut(cf_inform, "", " -> Setting the memory limit to %d", a.env.memory);
            }

            if (virDomainSetMemory(dom, (unsigned long) a.env.memory) == -1)
            {
                CfOut(cf_inform, "", " !!! Unable to set the current memory to %d", a.env.memory);
            }
        }

        if (a.env.disk != CF_NOINT)
        {
            CfOut(cf_verbose, "", " -> Info: env_disk parameter is not currently supported on this platform");
        }

        virDomainFree(dom);
    }
    else
    {
        CfOut(cf_verbose, "", " -> Virtual domain \"%s\" cannot be located, attempting to recreate", pp->promiser);
        CreateVirtDom(vc, uri, a, pp);
    }

    return true;
}
Exemplo n.º 8
0
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);
    }
}
Exemplo n.º 10
0
/**
 * @brief resumes the domain
 * @return if the domain was resumed without error, otherwise false
 */
bool Domain::resume()
{
// TODO(txwikinger): check if it can be resumed
    return (virDomainResume(m_domain) == 0);
}
Exemplo n.º 11
0
int main(int argc, char *argv[])
{
    virConnectPtr conn;
    virDomainPtr vdp;
    int val=-1;
	int choice;	
    char *host;
    conn = virConnectOpen("xen:///");
    if (conn == NULL) {
        fprintf(stderr, "Failed to open connection to xen:///\n");
        return 1;
    }
	printf("1.Suspend\n2.Resume\n3.Stop\n4.Start\n4.Exit");
	scanf("%d",&choice);
	
	while(1)
	{
		vdp=virDomainLookupById(conn,1);
	
		if(choice==1)
		{
			/* suspend*/	    
			val=virDomainSuspend(vdp);
			if(val==0)
			{
				printf("Success");
			}else{
				printf("Failed");	
			}
		}
		else if(choice==2)
		{
			/*resume*/
			val=virDomainResume(vdp);
			if(val==0)
			{
				printf("Success");
			}else{
				printf("Failed");	
			}
		}
		else if(choice==3)
		{
			/*shutdown*/
			val=virDomainDestroy(vdp);
			if(val==0)
			{
				printf("Success");
			}else{
				printf("Failed");	
			}
		}
		else if(choice==4)
		{
			/*start*/
			val=virDomainCreate(vdp);
			if(val==0)
			{
				printf("Success");
			}else{
				printf("Failed");	
			}
		}
		else if(choice==5)
		{
			virConnectClose(conn);
			break;
		}
	}	
    return 0;
}
Exemplo n.º 12
0
void main()
{

    int i,val=-1,choice,choice_id,num_domains;
    int *active_domains;
    virConnectPtr conn;
    virDomainPtr vdp;

    conn = virConnectOpen("xen:///");

    if (conn == NULL) {
        fprintf(stderr, "Error opening connection to XEN:///  \n");
        return 1;
    }
    else
    {

		//For finding Active Devices
        num_domains=virConnectNumOfDomains(conn);
        active_domanins=malloc(sizeof(int) * num_domains);
        num_domains = virConnectListDomains(conn, active_domains, num_domains);

        printf("Active domain IDs : \n");
        for (i = 0 ; i < num_domains ; i++) {
            printf("  %d\n", active_domains[i]);
        }
        free(active_domains);

        while(1)
        {
            
            printf("1.Start\n2.Suspend\n3.Resume\n4.stop\n5.exit ");
            scanf("%d",&choice);

            printf("\n Please Insert the Active Domian ID ");
            scanf("%d",&choice_id);
  
            vdp=virDomainLookupById(conn,choice_id);
        
            switch(choice)
            {
                case 1:/* Start */
		       val=virDomainCreate(vdp);
                       if(val==0)      
                           printf("Success");
                       else
                           printf("Failed");   
                       break;
                case 2:/* Suspend */
		       val=virDomainSuspend(vdp);
                       if(val==0)
                           printf("Success");
                       else
                           printf("Failed");   
                       break;
                case 3:/* Resume */ 
		        val=virDomainResume(vdp);
                        if(val==0)                     
                            printf("Success");
                        else
                            printf("Failed");   
                        break;

                case 4: /* stop */
			val=virDomainStop(vdp);
                        if(val==0)                     
                            printf("Success");
                        else
                            printf("Failed");   
                        break;
                default:exit(1);
            }
        }
        virConnectClose(conn);
    }
}