Highly Available and Scalable Information System
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

eurybox.functions.vm 4.9 KiB

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