diff --git a/include/DispatchManager.h b/include/DispatchManager.h index 3acfd57f65..57a93c821e 100644 --- a/include/DispatchManager.h +++ b/include/DispatchManager.h @@ -145,11 +145,13 @@ public: /** * Powers off a VM. * @param vid VirtualMachine identification + * @param hard True to force the poweroff (cancel instead of shutdown) * @return 0 on success, -1 if the VM does not exits or -2 if the VM is * in a wrong a state */ int poweroff ( - int vid); + int vid, + bool hard); /** * Holds a VM. diff --git a/include/LifeCycleManager.h b/include/LifeCycleManager.h index 0541da9349..af7f93e5e4 100644 --- a/include/LifeCycleManager.h +++ b/include/LifeCycleManager.h @@ -87,6 +87,7 @@ public: UNDEPLOY, /**< Sent by the DM to undeploy a running VM */ UNDEPLOY_HARD, /**< Sent by the DM to force undeploy a running VM */ POWEROFF, /**< Sent by the DM to power off a running VM */ + POWEROFF_HARD, /**< Sent by the DM to power off hard a running VM */ RESTART, /**< Sent by the DM to restart a deployed VM */ DELETE, /**< Sent by the DM to delete a VM */ CLEAN, /**< Sent by the DM to cleanup a VM for resubmission*/ @@ -253,6 +254,10 @@ private: void poweroff_action(int vid); + void poweroff_hard_action(int vid); + + void poweroff_action(int vid, bool hard); + void failure_action(VirtualMachine * vm); void restart_action(int vid); diff --git a/src/cli/onevm b/src/cli/onevm index 30ee3232ef..eb3f0ad6d4 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -350,12 +350,16 @@ cmd=CommandParser::CmdParser.new(ARGV) do EOT command :poweroff, poweroff_desc, [:range,:vmid_list], - :options => [OneVMHelper::SCHEDULE] do + :options => [OneVMHelper::SCHEDULE, OneVMHelper::HARD] do + + command_name='poweroff' + command_name<<'-hard' if options[:hard] + if (!options[:schedule].nil?) helper.schedule_actions(args[0], options, @comm_name) else helper.perform_actions(args[0],options,"shutting down") do |vm| - vm.poweroff + vm.poweroff(options[:hard]==true) end end end diff --git a/src/dm/DispatchManagerActions.cc b/src/dm/DispatchManagerActions.cc index 0bd8c1c9fd..35e2d2a11d 100644 --- a/src/dm/DispatchManagerActions.cc +++ b/src/dm/DispatchManagerActions.cc @@ -263,7 +263,8 @@ error: /* -------------------------------------------------------------------------- */ int DispatchManager::poweroff ( - int vid) + int vid, + bool hard) { ostringstream oss; VirtualMachine * vm; @@ -284,7 +285,14 @@ int DispatchManager::poweroff ( Nebula& nd = Nebula::instance(); LifeCycleManager * lcm = nd.get_lcm(); - lcm->trigger(LifeCycleManager::POWEROFF,vid); + if (hard) + { + lcm->trigger(LifeCycleManager::POWEROFF_HARD,vid); + } + else + { + lcm->trigger(LifeCycleManager::POWEROFF,vid); + } } else { diff --git a/src/lcm/LifeCycleActions.cc b/src/lcm/LifeCycleActions.cc index 4f0ed150e3..a247ecec55 100644 --- a/src/lcm/LifeCycleActions.cc +++ b/src/lcm/LifeCycleActions.cc @@ -397,6 +397,22 @@ void LifeCycleManager::undeploy_action(int vid, bool hard) /* -------------------------------------------------------------------------- */ void LifeCycleManager::poweroff_action(int vid) +{ + poweroff_action(vid, false); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void LifeCycleManager::poweroff_hard_action(int vid) +{ + poweroff_action(vid, true); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void LifeCycleManager::poweroff_action(int vid, bool hard) { VirtualMachine * vm; @@ -427,7 +443,14 @@ void LifeCycleManager::poweroff_action(int vid) //---------------------------------------------------- - vmm->trigger(VirtualMachineManager::SHUTDOWN,vid); + if (hard) + { + vmm->trigger(VirtualMachineManager::CANCEL,vid); + } + else + { + vmm->trigger(VirtualMachineManager::SHUTDOWN,vid); + } } else { diff --git a/src/lcm/LifeCycleManager.cc b/src/lcm/LifeCycleManager.cc index bf762928de..1e6a9b2bfb 100644 --- a/src/lcm/LifeCycleManager.cc +++ b/src/lcm/LifeCycleManager.cc @@ -260,6 +260,10 @@ void LifeCycleManager::trigger(Actions action, int _vid) aname = "POWEROFF"; break; + case POWEROFF_HARD: + aname = "POWEROFF_HARD"; + break; + default: delete vid; return; @@ -473,6 +477,10 @@ void LifeCycleManager::do_action(const string &action, void * arg) { poweroff_action(vid); } + else if (action == "POWEROFF_HARD") + { + poweroff_hard_action(vid); + } else if (action == ACTION_FINALIZE) { NebulaLog::log("LCM",Log::INFO,"Stopping Life-cycle Manager..."); diff --git a/src/lcm/LifeCycleStates.cc b/src/lcm/LifeCycleStates.cc index 3c96f3b50d..f08d759ec1 100644 --- a/src/lcm/LifeCycleStates.cc +++ b/src/lcm/LifeCycleStates.cc @@ -923,6 +923,7 @@ void LifeCycleManager::cancel_success_action(int vid) { Nebula& nd = Nebula::instance(); TransferManager * tm = nd.get_tm(); + DispatchManager * dm = nd.get_dm(); VirtualMachine * vm; time_t the_time = time(0); @@ -985,6 +986,30 @@ void LifeCycleManager::cancel_success_action(int vid) tm->trigger(TransferManager::EPILOG_STOP,vid); } + else if (vm->get_lcm_state() == VirtualMachine::SHUTDOWN_POWEROFF) + { + //---------------------------------------------------- + // POWEROFF STATE + //---------------------------------------------------- + + vm->delete_snapshots(); + + vmpool->update(vm); + + vm->set_running_etime(the_time); + + vm->set_etime(the_time); + + vm->set_vm_info(); + + vm->set_reason(History::STOP_RESUME); + + vmpool->update_history(vm); + + //---------------------------------------------------- + + dm->trigger(DispatchManager::POWEROFF_SUCCESS,vid); + } else { vm->log("LCM",Log::ERROR,"cancel_success_action, VM in a wrong state"); @@ -1011,11 +1036,12 @@ void LifeCycleManager::cancel_failure_action(int vid) } if ( vm->get_lcm_state() == VirtualMachine::CANCEL || - vm->get_lcm_state() == VirtualMachine::SHUTDOWN_UNDEPLOY ) + vm->get_lcm_state() == VirtualMachine::SHUTDOWN_UNDEPLOY || + vm->get_lcm_state() == VirtualMachine::SHUTDOWN_POWEROFF) { - //---------------------------------------------------- - // RUNNING STATE FROM CANCEL - //---------------------------------------------------- + //------------------------------------------------------------------ + // RUNNING STATE FROM CANCEL, SHUTDOWN_POWEROFF OR SHUTDOWN_UNDEPLOY + //------------------------------------------------------------------ vm->set_state(VirtualMachine::RUNNING); diff --git a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java index dec5c96720..b8e941b532 100644 --- a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java +++ b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java @@ -857,7 +857,20 @@ public class VirtualMachine extends PoolElement{ */ public OneResponse poweroff() { - return action("poweroff"); + return poweroff(false); + } + + /** + * Powers off a running VM. + * @param hard True to perform a hard (no acpi) shutdown, false for a + * graceful shutdown + * @return If an error occurs the error message contains the reason. + */ + public OneResponse poweroff(boolean hard) + { + String actionSt = hard ? "poweroff-hard" : "poweroff"; + + return action(actionSt); } /** diff --git a/src/oca/ruby/opennebula/virtual_machine.rb b/src/oca/ruby/opennebula/virtual_machine.rb index 905a7e83e8..52a98b00f9 100644 --- a/src/oca/ruby/opennebula/virtual_machine.rb +++ b/src/oca/ruby/opennebula/virtual_machine.rb @@ -220,8 +220,8 @@ module OpenNebula end # Powers off a running VM - def poweroff - action('poweroff') + def poweroff(hard=false) + action(hard ? 'poweroff-hard' : 'poweroff') end # Reboots an already deployed VM diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index 66c9acb70d..e3f7a19c7d 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -379,7 +379,11 @@ void VirtualMachineAction::request_execute(xmlrpc_c::paramList const& paramList, } else if (action == "poweroff") { - rc = dm->poweroff(id); + rc = dm->poweroff(id, false); + } + else if (action == "poweroff-hard") + { + rc = dm->poweroff(id, true); } else if (action == "undeploy") { diff --git a/src/scheduler/src/pool/VirtualMachineXML.cc b/src/scheduler/src/pool/VirtualMachineXML.cc index b8e93e365b..6cecc6ad93 100644 --- a/src/scheduler/src/pool/VirtualMachineXML.cc +++ b/src/scheduler/src/pool/VirtualMachineXML.cc @@ -309,6 +309,7 @@ int VirtualMachineXML::parse_action_name(string& action_st) && action_st != "reboot" && action_st != "reboot-hard" && action_st != "poweroff" + && action_st != "poweroff-hard" && action_st != "snapshot-create") { return -1;