Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019-2021 Mischa Peters <mischa @ openbsd.amsterdam>
4 #
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #
17 DELAY=0
18 SLEEP=30
19 CPU=$(($(sysctl -n hw.ncpuonline)-2))
21 COUNTER=0
22 for i in $(vmctl show | sort | awk '/ running / {print $9}' | xargs); do
23 VMS[${COUNTER}]=${i}
24 COUNTER=$((${COUNTER}+1))
25 done
27 echo -n "Are you sure you want to power off ${COUNTER} vms? Press any key to continue... "
28 read input
29 echo -n "Are you really sure? Press any key to continue... "
30 read input
32 CYCLES=$((${#VMS[*]}/${CPU}+1))
33 echo "Stopping ${#VMS[*]} VMs on ${CPU} CPUs in ${CYCLES} cycle(s), waiting ${SLEEP} seconds after each cycle."
35 COUNTER=0
36 for i in ${VMS[*]}; do
37 COUNTER=$((${COUNTER}+1))
38 vmctl stop ${i} ; sleep ${DELAY}
39 if [ $COUNTER -eq $CPU ]; then
40 sleep ${SLEEP}
41 COUNTER=0
42 fi
43 done
44 sleep 30