[Ovirt-devel] [PATCH server] Implement "poweroff" in the WUI.

Chris Lalancette clalance at redhat.com
Wed Nov 5 15:57:26 UTC 2008


Implement the concept of "poweroff" for VMs in the WUI, and in the backend
taskomatic bits.  This is the counterpart to "shutdown"; while
shutdown does a graceful shutdown of the guest (with an ACPI event), poweroff
pull the plug.  This is necessary for situations in which the guest
doesn't understand the ACPI event (like 2.4 based Linux guests), where the
ACPI event fails for some reason, or where you just want to pull the plug.

Signed-off-by: Chris Lalancette <clalance at redhat.com>
---
 src/app/controllers/resources_controller.rb |    3 ++-
 src/app/models/vm.rb                        |    3 +++
 src/app/models/vm_task.rb                   |    9 +++++++++
 src/task-omatic/task_vm.rb                  |   22 ++++++++++++----------
 src/task-omatic/taskomatic.rb               |    1 +
 5 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/src/app/controllers/resources_controller.rb b/src/app/controllers/resources_controller.rb
index 2c54ccc..6a2482c 100644
--- a/src/app/controllers/resources_controller.rb
+++ b/src/app/controllers/resources_controller.rb
@@ -53,7 +53,8 @@ class ResourcesController < PoolController
   # resource's vms list page
   def show_vms    
     @actions = [VmTask.label_and_action(VmTask::ACTION_START_VM),
-                (VmTask.label_and_action(VmTask::ACTION_SHUTDOWN_VM) << "break"),
+                VmTask.label_and_action(VmTask::ACTION_SHUTDOWN_VM),
+                (VmTask.label_and_action(VmTask::ACTION_POWEROFF_VM) << "break"),
                 VmTask.label_and_action(VmTask::ACTION_SUSPEND_VM),
                 VmTask.label_and_action(VmTask::ACTION_RESUME_VM),
                 VmTask.label_and_action(VmTask::ACTION_SAVE_VM),
diff --git a/src/app/models/vm.rb b/src/app/models/vm.rb
index 9a4c4fa..227f343 100644
--- a/src/app/models/vm.rb
+++ b/src/app/models/vm.rb
@@ -68,6 +68,7 @@ class Vm < ActiveRecord::Base
 
   STATE_UNREACHABLE    = "unreachable"
 
+  STATE_POWERING_OFF   = "powering_off"
   STATE_STOPPING       = "stopping"
   STATE_STOPPED        = "stopped"
   STATE_STARTING       = "starting"
@@ -92,6 +93,7 @@ class Vm < ActiveRecord::Base
 
   RUNNING_STATES       = [STATE_RUNNING,
                           STATE_SUSPENDED,
+                          STATE_POWERING_OFF,
                           STATE_STOPPING,
                           STATE_STARTING,
                           STATE_SUSPENDING,
@@ -105,6 +107,7 @@ class Vm < ActiveRecord::Base
                        STATE_CREATING      => STATE_STOPPED,
                        STATE_RUNNING       => STATE_RUNNING,
                        STATE_STOPPING      => STATE_STOPPED,
+                       STATE_POWERING_OFF  => STATE_STOPPED,
                        STATE_STOPPED       => STATE_STOPPED,
                        STATE_STARTING      => STATE_RUNNING,
                        STATE_SUSPENDING    => STATE_SUSPENDED,
diff --git a/src/app/models/vm_task.rb b/src/app/models/vm_task.rb
index c38f24f..27e3e65 100644
--- a/src/app/models/vm_task.rb
+++ b/src/app/models/vm_task.rb
@@ -23,6 +23,7 @@ class VmTask < Task
 
   ACTION_START_VM    = "start_vm"
   ACTION_SHUTDOWN_VM = "shutdown_vm"
+  ACTION_POWEROFF_VM = "poweroff_vm"
 
   ACTION_SUSPEND_VM  = "suspend_vm"
   ACTION_RESUME_VM   = "resume_vm"
@@ -64,6 +65,14 @@ class VmTask < Task
                                       :failure => Vm::STATE_RUNNING,
                                       :privilege => [Permission::PRIV_VM_CONTROL,
                                                      PRIV_OBJECT_VM_POOL]},
+              ACTION_POWEROFF_VM => { :label => "Poweroff",
+                                      :icon => "icon_x.png",
+                                      :start => Vm::STATE_RUNNING,
+                                      :running => Vm::STATE_POWERING_OFF,
+                                      :success => Vm::STATE_STOPPED,
+                                      :failure => Vm::STATE_RUNNING,
+                                      :privilege => [Permission::PRIV_VM_CONTROL,
+                                                     PRIV_OBJECT_VM_POOL]},
               ACTION_SUSPEND_VM  => { :label => "Suspend",
                                       :icon  => "icon_suspend.png",
                                       :start => Vm::STATE_RUNNING,
diff --git a/src/task-omatic/task_vm.rb b/src/task-omatic/task_vm.rb
index c30c6a9..56f024c 100644
--- a/src/task-omatic/task_vm.rb
+++ b/src/task-omatic/task_vm.rb
@@ -267,9 +267,7 @@ def create_vm(task)
   end
 end
 
-def shutdown_vm(task)
-  puts "shutdown_vm"
-
+def shut_or_destroy_vm(task, which)
   # here, we are given an id for a VM to shutdown; we have to lookup which
   # physical host it is running on
 
@@ -291,13 +289,7 @@ def shutdown_vm(task)
   begin
     conn = Libvirt::open("qemu+tcp://" + vm.host.hostname + "/system")
     dom = conn.lookup_domain_by_uuid(vm.uuid)
-    # FIXME: crappy.  Right now we destroy the domain to make sure it
-    # really went away.  We really want to shutdown the domain to make
-    # sure it gets a chance to cleanly go down, but how can we tell when
-    # it is truly shut off?  And then we probably need a timeout in case
-    # of problems.  Needs more thought
-    #dom.shutdown
-    dom.destroy
+    dom.send(which)
 
     begin
       dom.undefine
@@ -321,6 +313,16 @@ def shutdown_vm(task)
   setVmShutdown(vm)
 end
 
+def shutdown_vm(task)
+  puts "shutdown_vm"
+  shut_or_destroy_vm(task, "shutdown")
+end
+
+def poweroff_vm(task)
+  puts "poweroff_vm"
+  shut_or_destroy_vm(task, "destroy")
+end
+
 def start_vm(task)
   puts "start_vm"
 
diff --git a/src/task-omatic/taskomatic.rb b/src/task-omatic/taskomatic.rb
index 1264207..651e652 100755
--- a/src/task-omatic/taskomatic.rb
+++ b/src/task-omatic/taskomatic.rb
@@ -96,6 +96,7 @@ loop do
       case task.action
       when VmTask::ACTION_CREATE_VM then create_vm(task)
       when VmTask::ACTION_SHUTDOWN_VM then shutdown_vm(task)
+      when VmTask::ACTION_POWEROFF_VM then poweroff_vm(task)
       when VmTask::ACTION_START_VM then start_vm(task)
       when VmTask::ACTION_SUSPEND_VM then suspend_vm(task)
       when VmTask::ACTION_RESUME_VM then resume_vm(task)
-- 
1.5.4.3




More information about the ovirt-devel mailing list