Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Proxmox cluster shut down script, intended to shut down all vms and containers on each node,
- # once they shut down signal all nodes that they are shut down safely, stop ceph, and shut down the node.
- # Written as a shutdown script to be called from NUT.
- #
- # 2015/06/02 Erik Staab.
- VM=$(qm list | awk '{if ($3 == "running") print $1}')
- CT=$(pvectl list | awk '{if ($3 == "running") print $1}')
- HOSTS=$(pvecm nodes | awk '{if ($1 != "Node") print $6}')
- # check if there are running vms or containers and shut them down, if there are none set safeshutdown to 1
- if [ -n "$VM" ] || [ -n "$CT" ]
- then
- for vm in $VM; do
- qm shutdown $vm > /dev/null 2>&1
- done
- for ct in $CT; do
- pvectl shutdown $ct > /dev/null 2>&1
- done
- echo 0 > /etc/pve/nodes/`hostname`/safeshutdown
- else
- echo 1 > /etc/pve/nodes/`hostname`/safeshutdown
- fi
- # if there are running vms or containers, hold in loop until they complete shutting down, once complete set own safeshutdown to 1
- while [ -n "$VM" ] || [ -n "$CT" ]
- do
- VM=$(qm list | awk '{if ($3 == "running") print $1}')
- CT=$(pvectl list | awk '{if ($3 == "running") print $1}')
- if [ -z "$VM" ] && [ -z "$CT" ]
- then
- echo 1 > /etc/pve/nodes/`hostname`/safeshutdown
- else
- sleep 10
- fi
- done
- # once the vms and containers are shut down check other hosts to see if they are safe to shut down
- HOSTSREADY=0
- while [ $HOSTSREADY == 0 ]
- do
- ALLCLEAR=1
- for host in $HOSTS
- do
- if [ $host != `hostname` ]
- then
- if [ $(cat /etc/pve/nodes/$host/safeshutdown) == "0" ]
- then
- ALLCLEAR=0
- fi
- fi
- done
- if [ $ALLCLEAR == 1 ]
- then
- HOSTSREADY=1
- else
- sleep 30
- fi
- done
- # if all the hosts are ready to shut down, stop ceph, and shut down the node
- if [ $HOSTSREADY == 1 ]
- then
- /usr/bin/ceph osd set noout
- /usr/sbin/service ceph stop
- /sbin/shutdown -h now
- fi
Add Comment
Please, Sign In to add comment