Difference between revisions of "Shutting down a VM"

From Xen
(Created page with "This page shows how to use the XenAPI to shut down a VM. The examples are written in python (feel free to add translations to other languages) and will use the API via the local …")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
This page shows how to use the XenAPI to shut down a VM. The examples are written in python (feel free to add translations to other languages) and will use the API via the local Unix domain socket.
 
This page shows how to use the XenAPI to shut down a VM. The examples are written in python (feel free to add translations to other languages) and will use the API via the local Unix domain socket.
  +
  +
== Shutting down cleanly ==
   
 
To request a clean shutdown of a VM with a given uuid: (Note this will block for up to 20 minutes while the VM is shutting down)
 
To request a clean shutdown of a VM with a given uuid: (Note this will block for up to 20 minutes while the VM is shutting down)
Line 15: Line 17:
 
session.xenapi.login_with_password("root", "")
 
session.xenapi.login_with_password("root", "")
 
try:
 
try:
vm = xenapi.VM.get_by_uuid(uuid)
+
vm = session.xenapi.VM.get_by_uuid(uuid)
xenapi.VM.clean_shutdown(vm)
+
session.xenapi.VM.clean_shutdown(vm)
  +
finally:
  +
session.xenapi.session.logout()
  +
  +
== Forcibly shutting down ==
  +
  +
On recent version of xapi we can use:
  +
  +
#!/usr/bin/env python
  +
import XenAPI, sys
  +
  +
if len(sys.argv) <> 2:
  +
print "Usage:"
  +
print sys.argv[0], "<VM uuid>"
  +
sys.exit(1)
  +
uuid = sys.argv[1]
  +
  +
session = XenAPI.xapi_local()
  +
session.xenapi.login_with_password("root", "")
  +
try:
  +
vm = session.xenapi.VM.get_by_uuid(uuid)
  +
session.xenapi.VM.hard_shutdown(vm)
  +
finally:
  +
session.xenapi.session.logout()
  +
  +
On older versions of xapi the above code will block, and eventually timeout, if a clean_shutdown is in progress. To forcibly shutdown a VM on older versions of xapi we must therefore cancel any running tasks operating on that VM:
  +
  +
#!/usr/bin/env python
  +
import XenAPI, sys
  +
  +
if len(sys.argv) <> 2:
  +
print "Usage:"
  +
print sys.argv[0], "<VM uuid>"
  +
sys.exit(1)
  +
uuid = sys.argv[1]
  +
  +
session = XenAPI.xapi_local()
  +
session.xenapi.login_with_password("root", "")
  +
try:
  +
# find the VM
  +
vm = session.xenapi.VM.get_by_uuid(uuid)
  +
fields = session.xenapi.VM.get_record(vm)
  +
# cancel any tasks operating on the VM (e.g. clean_shutdown)
  +
current = fields["current_operations"]
  +
for t in current.keys():
  +
try:
  +
print "\n Cancelling operation on VM: %s" % fields["name_label"],
  +
sys.stdout.flush()
  +
session.xenapi.task.cancel(t)
  +
except Exception, e:
  +
print "Failed to cancel task: %s" % t,
  +
sys.stdout.flush()
  +
raise e
  +
session.xenapi.VM.hard_shutdown(vm)
 
finally:
 
finally:
 
session.xenapi.session.logout()
 
session.xenapi.session.logout()

Latest revision as of 13:22, 31 October 2013

This page shows how to use the XenAPI to shut down a VM. The examples are written in python (feel free to add translations to other languages) and will use the API via the local Unix domain socket.

Shutting down cleanly

To request a clean shutdown of a VM with a given uuid: (Note this will block for up to 20 minutes while the VM is shutting down)

#!/usr/bin/env python
import XenAPI, sys

if len(sys.argv) <> 2:
   print "Usage:"
   print sys.argv[0], "<VM uuid>"
   sys.exit(1)
uuid = sys.argv[1]

session = XenAPI.xapi_local()
session.xenapi.login_with_password("root", "")
try:
   vm = session.xenapi.VM.get_by_uuid(uuid)
   session.xenapi.VM.clean_shutdown(vm)
finally:
   session.xenapi.session.logout()

Forcibly shutting down

On recent version of xapi we can use:

#!/usr/bin/env python
import XenAPI, sys

if len(sys.argv) <> 2:
   print "Usage:"
   print sys.argv[0], "<VM uuid>"
   sys.exit(1)
uuid = sys.argv[1]

session = XenAPI.xapi_local()
session.xenapi.login_with_password("root", "")
try:
   vm = session.xenapi.VM.get_by_uuid(uuid)
   session.xenapi.VM.hard_shutdown(vm)
finally:
   session.xenapi.session.logout()

On older versions of xapi the above code will block, and eventually timeout, if a clean_shutdown is in progress. To forcibly shutdown a VM on older versions of xapi we must therefore cancel any running tasks operating on that VM:

#!/usr/bin/env python
import XenAPI, sys

if len(sys.argv) <> 2:
   print "Usage:"
   print sys.argv[0], "<VM uuid>"
   sys.exit(1)
uuid = sys.argv[1]

session = XenAPI.xapi_local()
session.xenapi.login_with_password("root", "")
try:
   # find the VM
   vm = session.xenapi.VM.get_by_uuid(uuid)
   fields = session.xenapi.VM.get_record(vm)
   # cancel any tasks operating on the VM (e.g. clean_shutdown)
   current = fields["current_operations"]
   for t in current.keys():
     try:
       print "\n  Cancelling operation on VM: %s" % fields["name_label"],
       sys.stdout.flush()
       session.xenapi.task.cancel(t)
     except Exception, e:
       print "Failed to cancel task: %s" % t,
       sys.stdout.flush()
       raise e
   session.xenapi.VM.hard_shutdown(vm)
finally:
   session.xenapi.session.logout()