HeroCC

Proxmox Shutdown

Dec 20th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # Proxmox cluster shut down script, intended to shut down all vms and containers on each node,
  3. # once they shut down signal all nodes that they are shut down safely, stop ceph, and shut down the node.
  4. # Written as a shutdown script to be called from NUT.
  5. #
  6. # 2015/06/02 Erik Staab.
  7.  
  8. VM=$(qm list | awk '{if ($3 == "running") print $1}')
  9. CT=$(pvectl list | awk '{if ($3 == "running") print $1}')
  10. HOSTS=$(pvecm nodes | awk '{if ($1 != "Node") print $6}')
  11.  
  12. # check if there are running vms or containers and shut them down, if there are none set safeshutdown to 1
  13. if [ -n "$VM" ] || [ -n "$CT" ]
  14. then
  15.         for vm in $VM; do
  16.                 qm shutdown $vm > /dev/null 2>&1
  17.         done
  18.         for ct in $CT; do
  19.                 pvectl shutdown $ct > /dev/null 2>&1
  20.         done
  21.         echo 0 > /etc/pve/nodes/`hostname`/safeshutdown
  22. else
  23.         echo 1 > /etc/pve/nodes/`hostname`/safeshutdown
  24. fi
  25.  
  26. # if there are running vms or containers, hold in loop until they complete shutting down, once complete set own safeshutdown to 1
  27. while [ -n "$VM" ] || [ -n "$CT" ]
  28. do
  29.         VM=$(qm list | awk '{if ($3 == "running") print $1}')
  30.         CT=$(pvectl list | awk '{if ($3 == "running") print $1}')
  31.         if [ -z "$VM" ] && [ -z "$CT" ]
  32.         then
  33.                 echo 1 > /etc/pve/nodes/`hostname`/safeshutdown
  34.         else
  35.                 sleep 10
  36.         fi
  37. done
  38.  
  39. # once the vms and containers are shut down check other hosts to see if they are safe to shut down
  40. HOSTSREADY=0
  41. while [ $HOSTSREADY == 0 ]
  42. do
  43.         ALLCLEAR=1
  44.         for host in $HOSTS
  45.         do
  46.                 if [ $host != `hostname` ]
  47.                 then
  48.                         if [ $(cat /etc/pve/nodes/$host/safeshutdown) == "0" ]
  49.                         then
  50.                                 ALLCLEAR=0
  51.                         fi
  52.                 fi
  53.         done
  54.         if [ $ALLCLEAR == 1 ]
  55.         then
  56.                 HOSTSREADY=1
  57.         else
  58.                 sleep 30
  59.         fi
  60. done
  61.  
  62. # if all the hosts are ready to shut down, stop ceph, and shut down the node
  63. if [ $HOSTSREADY == 1 ]
  64. then
  65.         /usr/bin/ceph osd set noout
  66.         /usr/sbin/service ceph stop
  67.         /sbin/shutdown -h now
  68. fi
Add Comment
Please, Sign In to add comment