overview
Sometimes the disk file of the ESXI virtual machine will be damaged when it is shut down unexpectedly (such as: power failure), so that the virtual machine cannot be started after restarting. Use the following script and set it to start on boot to solve this problem.
screenplay
#!/bin/bash
#配置部分(根据自己情况修改)
#脚本开机运行教程: https://kb.vmware.com/s/article/2043564?lang=zh_CN
#homelede虚拟机ID,可使用命令查询: vim-cmd vmsvc/getallvms
vmid=14
#homelede磁盘文件目录
dir=/vmfs/volumes/datastore1/HomeLEDE-主
# 检查是否开机间隔时间(若启动较快可以设短些)
interval=3m
#命令部分
#最多重试11次尝试修复
for i in {
1..11}
do
# 这一步检查你当前的虚拟机是否开机
vim-cmd vmsvc/power.getstate $vmid | grep 'Powered on'
ret=$?
if [ $ret == 0 ]
then
echo 'vm is running, do nothing ...'
exit 0
else
echo 'vm is stoped, repair it and restarting...'
# repairing ...
echo 'repairing ...'
# 如果当前虚拟机未开机,执行下面的修复命令:先遍历磁盘目录下的vmdk文件,再执行repair
vmdkFiles=$dir/*.vmdk
for file in $vmdkFiles
do
vmkfstools -x repair $file
done
# starting ...
echo 'Repair end. Start power on ...'
vim-cmd vmsvc/power.on $vm
echo 'Done.'
fi
# 等待一段时间,让其启动
sleep $interval;
done
Set boot
Script startup tutorial
Note: It is best to place the script file in the data directory (eg: /vmfs/volumes/datastore1/
), and it may be deleted after restarting if placed in other directories.