Це оновлення версія попередньої публікації
https://onchigor.blogspot.com/2016/07/apcupsd-vm-exsi.html
Але цього разу використав досвід з
https://gist.github.com/gschora/a10f0692e6e691aa1af8
https://habr.com/ru/sandbox/101324/
Послідовність викладена так
#################################################################
# these are instructions for automating the suspend and shutdown of esxi vm's
# and host in case of a power failure.
##################################################################
0# make a new VM and install Ubuntu-Server on it
1# install apcupsd
apt-get install apcupsd
2# configure /etc/apcupsd/apcupsd.conf
3# enable apcupsd
vim /etc/default/apcupsd
ISCONFIGURED=yes
4# restart apcupsd
sudo service apcupsd restart
-------------------------------------------------
5# enable ssh on esxi
configuration-tab - security profile - properties (right upper corner)
6# copy shutdown_esxi.sh on esxi into datastore e.g. "/vmfs/volumes/MyDataStore/scripts
7# make the file executable
chmod +x shutdown_esxi.sh
--------------------------------------------------
8# configure passwordless ssh access on esxi
make keys on ubuntu server (as root, because apcupsd runs under root)
sudo -s
ssh-keygen -t rsa -b 2048
copy public key to esxi server
cat /root/.ssh/id_rsa.pub | ssh root@server_address ‘cat >> /etc/ssh/keys-root/authorized_keys’
now automated login should work on ubuntu server
ssh root@server_address (the first time it asks to store esxi's key in local database, answer with "yes")
---------------------------------------------------
9# configure apc-script to run remote script on esxi
copy "doshutdown" to /etc/apcupsd (you mustn't rename it, otherwise apcupsd won't run it automatically)
make it executable
chmod +x doshutdown
#10 restart apcupsd
sudo service apcupsd restart
---------------------------------------------------
Скрипт doshutdown
#!/bin/sh
WALL=wall
# this script is triggered in case the power fails
echo "suspending all VMs on ESXI..." | ${WALL}
# configured ssh-automatic-access with keys
ssh root@server_address "nohup /vmfs/volumes/MyDataStore/scripts/shutdown_esxi.sh > /dev/null 2>&1 &"
echo "finished shutting ESXI down..." | ${WALL}
# exit code 99 - apccontrol stops after this script, so no shutdown of this host. this is for testing purposes
# exit code 0 - apccontrol continues with shutdown after this script
exit 99
Скрипт shutdown_esxi.sh
#/bin/sh
########################################################################
#courtesy of http://www.c-note.dk/2011/12/04/wmware-esxi-suspend-all-guests/
#
#copy this file somewhere to datastore
#e.g. ‘/vmfs/volumes/myDataStore/scripts/’
#
# looks which vm's are running, sends them into suspend, waits until suspending-process
# is finished, then powers off esxi completely
#########################################################################
VMS=`vim-cmd vmsvc/getallvms | grep -v Vmid | awk '{print $1}'`#################################################
for VM in $VMS ; do
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
if [ "$PWR" == "Powered on" ] ; then
name=`vim-cmd vmsvc/get.config $VM | grep -i "name =" | awk '{print $3}' | head -1 | cut -d "\"" -f2`
echo "Powered on: $name"
echo "Suspending: $name"
vim-cmd vmsvc/power.suspend $VM > /dev/null &
fi
done
while true ; do
RUNNING=0
for VM in $VMS ; do
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
if [ "$PWR" == "Powered on" ] ; then
echo "Waiting..."
RUNNING=1
fi
done
if [ $RUNNING -eq 0 ] ; then
echo "Gone..."
break
fi
sleep 1
done
echo "Now we shutdown the host..."
/sbin/shutdown.sh && /sbin/poweroff
Але є нюанси
1. Для копіювання public key до esxi краще використати іншій метод
sudo ssh-copy-id -i ~/.ssh/id_rsa.pub root@server_address
ssh root@server_address
sudo cp /.ssh/authorized_keys /etc/ssh/keys-root/authorized_keys
2. Якщо машина, котра відстежує apcupsd це є одна з віртуальних мащин, то при відновленні енергоживлення, вона буде у невідомому режимі, тому що вона почала робити shutdown, а скрипт shutdown_esxi.sh зробив suspend. #################################################
Тому замість
vim-cmd vmsvc/power.suspend $VM > /dev/null &
краще робити
vim-cmd vmsvc/power.shutdown $VM > /dev/null &
Або взагалі використати методику, вказаний у другому посиланні, а саме
1. В /etc/apcupsd/apccontrol.
Замість:
echo "UPS ${2} initiated Shutdown Sequence" | ${WALL}
${SHUTDOWN} -h now "apcupsd UPS ${2} initiated shutdown"
Вказуємо
echo "UPS ${2} initiated Shutdown Sequence" | ${WALL}
/usr/bin/ssh root@server_address/shutdown_esxi.sh
2. А сам скрипт shutdown_esxi.sh
#!/bin/sh
# ID all Vms
VMID=$(/usr/bin/vim-cmd vmsvc/getallvms | grep ^[0-9] | awk '{print $1}')
# rotate for all Vms
for i in $VMID
do
# Get state (turned on, off, whatever)
STATE=$(/usr/bin/vim-cmd vmsvc/power.getstate $i | tail -1 | awk '{print $2}')
# if Vm is on then switch off
if [ $STATE == on ]
then
/usr/bin/vim-cmd vmsvc/power.shutdown $i
fi
done
# Pause for waiting for Vms is down
sleep 180
# And now shutdown Vm host
/sbin/shutdown.sh
/sbin/poweroff
Ще варіанти знайшов тут
https://yakim.org.ua/ru/servera/71-nastroika-ups-v-vmware-esxi-cherez-ubuntu-server.html
http://itstream.net/tips/395/
Тут
http://www.designervisuals.com/Shutdown_ESXi_and_VMs_During_a_Power_Failure.html
пропонується простішій варіант з використанням скриптів, що у складі esxi
"plink -ssh -2 -pw password root@192.168.0.4 "/sbin/shutdown.sh && /sbin/poweroff""
Коментарі
Дописати коментар