Peter's zsh configuration files
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

virsh.zsh 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ################################################################################
  2. # Returns 0 if a VM is running, otherwise 1.
  3. virsh_running () {
  4. if [ $# -ne 1 ]; then
  5. echo "Usage: virsh_running name"
  6. return 2
  7. fi
  8. name=$1
  9. state=`virsh dominfo $name | grep ^State | awk '{print $2}'`
  10. if [ "$state" = "running" ]; then
  11. return 0
  12. else
  13. return 1
  14. fi
  15. }
  16. ################################################################################
  17. # Returns 0 if the given VM is registered with libvirt, otherwise 1.
  18. virsh_is_registered () {
  19. if [ $# -ne 1 ]; then
  20. echo "Usage: virsh_is_registered name"
  21. return 2
  22. fi
  23. name=$1
  24. if virsh dominfo $name > /dev/null 2>&1; then
  25. return 0
  26. else
  27. return 1
  28. fi
  29. }
  30. ################################################################################
  31. # Returns 0 if the VM has managed save info, otherwise 1.
  32. virsh_has_managed_save () {
  33. if [ $# -ne 1 ]; then
  34. echo "Usage: virsh_has_managed_save name"
  35. return 2
  36. fi
  37. name=$1
  38. save=`virsh dominfo $name | grep '^Managed save' | awk '{print $3}'`
  39. if [ "$save" = "yes" ]; then
  40. return 0
  41. else
  42. return 1
  43. fi
  44. }
  45. ################################################################################
  46. # Returns the current version of libvirt
  47. virsh_version () {
  48. virsh version | head -1 | awk -F: '{print $2}' | awk '{print $2}'
  49. }
  50. ################################################################################
  51. # Start a VM and wait for it to be running.
  52. virsh_start () {
  53. if [ $# -ne 1 ]; then
  54. echo "Usage: virsh_start name"
  55. return 1
  56. fi
  57. name=$1
  58. hostname="${name}.pmade.com"
  59. # First check to see if we're already running.
  60. virsh_running $name && return 0
  61. # Record if we have managed state
  62. if virsh_has_managed_save $name; then
  63. with_save=1
  64. else
  65. with_save=0
  66. fi
  67. # Start it and wait for it to be pingable
  68. virsh start $name || return 1
  69. echo "==> Waiting for $name to start..."
  70. [ $with_save -eq 0 ] && sleep 60
  71. ping -c 5 -q -w 120 $hostname
  72. }
  73. ################################################################################
  74. # Stop a running VM using managedsave.
  75. virsh_stop () {
  76. if [ $# -ne 1 ]; then
  77. echo "Usage: virsh_stop name"
  78. return 1
  79. fi
  80. name=$1
  81. flags=""
  82. if [ $(virsh_version) != '0.9.8' ]; then
  83. flags="--verbose"
  84. fi
  85. # Can't stop a domain unless it's running
  86. virsh_running $name || return 0
  87. echo "==> Waiting for $name to stop"
  88. virsh managedsave $name $flags
  89. }
  90. ################################################################################
  91. # Open a vnc connection to a virtual machine
  92. virsh_vnc () {
  93. if [ $# -ne 1 ]; then
  94. echo "Usage: virsh_vnc name"
  95. return 1
  96. fi
  97. name=$1
  98. port=$(virsh vncdisplay $name|head -1|sed 's/^.*://')
  99. port=$(expr 5900 + $port)
  100. vnc localhost:$port
  101. }