Highly Available and Scalable Information System
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.

eurybox.functions.vm 5.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/bin/bash
  2. #EuryBOX vm functions file
  3. #Desc: shutdown a VM through libvirt ACPI emulation (doesn't work for OS without ACPI support)
  4. #1 arg required: vm_name
  5. eurybox_vm_shutdown_acpi ()
  6. {
  7. local NAME=$1
  8. local STATUS
  9. #Waiting for the VM to be stopped
  10. local EURYBOX_VM_NAME
  11. local EURYBOX_VM_STATE=1
  12. local EURYBOX_VM_STOPPED=0
  13. local NB_TRY_LEFT=$EURYBOX_MAX_RETRY_ON_FAILURE
  14. while [[ ( $EURYBOX_VM_STATE -eq 1 ) && !( $NB_TRY_LEFT -eq 0 ) ]]
  15. do
  16. EURYBOX_VM_NAME=`sudo virsh $EURYBOX_VIRSH_OPTIONS list | grep $NAME`
  17. if [ -z "$EURYBOX_VM_NAME" ]
  18. then
  19. EURYBOX_VM_STATE=0
  20. else
  21. if [[ $EURYBOX_VM_STOPPED -eq 0 ]]
  22. then
  23. sudo virsh $EURYBOX_VIRSH_OPTIONS shutdown $NAME
  24. STATUS=$?
  25. if [[ !($STATUS -eq 0) ]]
  26. then
  27. eurybox_display_message warning VM "Error on vm $NAME stop - trying again in 5 seconds / $NB_TRY_LEFT left"
  28. NB_TRY_LEFT=$(( $NB_TRY_LEFT - 1 ))
  29. else
  30. EURYBOX_VM_STOPPED=1
  31. eurybox_display_message message VM "Successfully initiated vm $NAME stop - waiting for vm poweroff"
  32. NB_TRY_LEFT=$EURYBOX_MAX_RETRY_ON_FAILURE
  33. fi
  34. else
  35. NB_TRY_LEFT=$(( $NB_TRY_LEFT - 1 ))
  36. eurybox_display_message debug VM "VM $NAME - still running - trying again in 5 seconds / $NB_TRY_LEFT left"
  37. fi
  38. sleep 5
  39. fi
  40. done
  41. if [[ $NB_TRY_LEFT -eq 0 ]]
  42. then
  43. eurybox_display_message error VM "VM $NAME - stop failed - VM still running after $EURYBOX_MAX_RETRY_ON_FAILURE: $EURYBOX_VM_NAME"
  44. else
  45. eurybox_display_message message VM "VM $NAME - stop OK"
  46. fi
  47. }
  48. #Desc: shutdown a VM through ssh connection
  49. #4 arg required: vm_name vm_host vm_user vm_port
  50. eurybox_vm_shutdown_ssh ()
  51. {
  52. local NAME=$1
  53. local HOST=$2
  54. local USER=$3
  55. local PORT=$4
  56. local EURYBOX_VM_STATE=1
  57. local EURYBOX_VM_STOPPED=0
  58. local STATUS
  59. local EURYBOX_VM_NAME
  60. local SSH_OUT
  61. local NB_TRY_LEFT=$EURYBOX_MAX_RETRY_ON_FAILURE
  62. while [[ ( $EURYBOX_VM_STATE -eq 1 ) && !( $NB_TRY_LEFT -eq 0 ) ]]
  63. do
  64. EURYBOX_VM_NAME=`sudo virsh $EURYBOX_VIRSH_OPTIONS list | grep $NAME`
  65. if [ -z "$EURYBOX_VM_NAME" ]
  66. then
  67. EURYBOX_VM_STATE=0
  68. else
  69. if [[ $EURYBOX_VM_STOPPED -eq 0 ]]
  70. then
  71. SSH_OUT=`ssh $EURYBOX_SSH_OPTIONS -p $PORT $USER@$HOST "halt -p >/dev/null &" 2>&1`
  72. STATUS=$?
  73. if [[ !($STATUS -eq 0) ]]
  74. then
  75. eurybox_display_message warning VM "VM $NAME - stop failed - trying again in 5 seconds:\n$SSH_OUT\n$NB_TRY_LEFT left"
  76. NB_TRY_LEFT=$(( $NB_TRY_LEFT - 1 ))
  77. else
  78. EURYBOX_VM_STOPPED=1
  79. eurybox_display_message message VM "Successfully initiated vm $NAME stop - waiting for vm poweroff"
  80. NB_TRY_LEFT=$EURYBOX_MAX_RETRY_ON_FAILURE
  81. fi
  82. else
  83. NB_TRY_LEFT=$(( $NB_TRY_LEFT - 1 ))
  84. eurybox_display_message debug VM "VM $NAME - still running - trying again in 5 seconds:\n$EURYBOX_VM_NAME\n$NB_TRY_LEFT left"
  85. fi
  86. sleep 5
  87. fi
  88. done
  89. if [[ $NB_TRY_LEFT -eq 0 ]]
  90. then
  91. eurybox_display_message error VM "VM $NAME - stop failed - VM still running after $EURYBOX_MAX_RETRY_ON_FAILURE:\n$EURYBOX_VM_NAME"
  92. else
  93. eurybox_display_message message VM "VM $NAME - stop OK"
  94. fi
  95. }
  96. #Desc: start a VM through libvirt and check via ssh startup success
  97. #4 arg required: vm_name vm_host vm_user vm_port
  98. eurybox_vm_start ()
  99. {
  100. local NAME=$1
  101. local HOST=$2
  102. local USER=$3
  103. local PORT=$4
  104. local STATUS
  105. local VM_OUT
  106. local EURYBOX_VM_AVAILABLE
  107. local VIRSH_OUT
  108. #Check if VM is operationnal (at network level)
  109. local EURYBOX_VM_STATE=0
  110. local EURYBOX_VM_STARTED=0
  111. local NB_TRY_LEFT=$EURYBOX_MAX_RETRY_ON_FAILURE
  112. while [[ ( $EURYBOX_VM_STATE -eq 0 ) && !( $NB_TRY_LEFT -eq 0 ) ]]
  113. do
  114. EURYBOX_VM_AVAILABLE=`ssh $EURYBOX_SSH_OPTIONS -p $PORT $USER@$HOST "touch eurybox_check_ssh_ok" 2>&1`
  115. STATUS=$?
  116. if [[ $STATUS -eq 0 ]]
  117. then
  118. EURYBOX_VM_STATE=1
  119. else
  120. if [[ $EURYBOX_VM_STARTED -eq 0 ]]
  121. then
  122. VIRSH_OUT=`sudo virsh $EURYBOX_VIRSH_OPTIONS start $NAME 2>&1`
  123. STATUS=$?
  124. if [[ !($STATUS -eq 0) ]]
  125. then
  126. VM_OUT=`sudo virsh $EURYBOX_VIRSH_OPTIONS list | grep $NAME`
  127. if [[ -z "$VM_OUT" ]]
  128. then
  129. eurybox_display_message warning VM "Error on vm $NAME start - trying again in 5 seconds:\n$VIRSH_OUT\n$NB_TRY_LEFT left"
  130. NB_TRY_LEFT=$(( $NB_TRY_LEFT - 1 ))
  131. else
  132. eurybox_display_message debug VM "VM $NAME already running - waiting for network contact"
  133. EURYBOX_VM_STARTED=1
  134. NB_TRY_LEFT=$EURYBOX_MAX_RETRY_ON_FAILURE
  135. fi
  136. else
  137. EURYBOX_VM_STARTED=1
  138. eurybox_display_message debug VM "Successfully initiated vm $NAME start - waiting for network contact"
  139. NB_TRY_LEFT=$EURYBOX_MAX_RETRY_ON_FAILURE
  140. fi
  141. else
  142. NB_TRY_LEFT=$(( $NB_TRY_LEFT - 1 ))
  143. eurybox_display_message debug VM "VM $NAME - still unreachable - trying again in 5 seconds\n$EURYBOX_VM_AVAILABLE\n$NB_TRY_LEFT left"
  144. fi
  145. sleep 5
  146. fi
  147. done
  148. if [[ $NB_TRY_LEFT -eq 0 ]]
  149. then
  150. eurybox_display_message error VM "VM $NAME - start failed - no network contact after $EURYBOX_MAX_RETRY_ON_FAILURE"
  151. else
  152. eurybox_display_message debug VM "VM $NAME - start OK"
  153. fi
  154. }