Blame


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