Skip to content

Instantly share code, notes, and snippets.

@bjornbouetsmith
Last active October 12, 2020 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjornbouetsmith/a50c1966b2750e6638b0fa3468995357 to your computer and use it in GitHub Desktop.
Save bjornbouetsmith/a50c1966b2750e6638b0fa3468995357 to your computer and use it in GitHub Desktop.
ESXi script to auto start virtual machine that exports NFS shares to ESXi, i.e. FreeNAS/TrueNAS/whatever
#!/bin/sh
# ESXi script to auto start virtual machine that exports NFS shares to ESXi, i.e. FreeNAS/TrueNAS/whatever
VMNAME=TrueNAS
SHARE=TrueNAS
MAXLOOPSVM=10
MAXLOOPSSHARE=10
COUNT=1
logger "local.sh: Checking status of $VMNAME"
ID=`vim-cmd vmsvc/getallvms|grep "$VMNAME" | awk '{print $1}'`
MSG="Starting VM '$VMNAME' with ID $ID"
echo $MSG
logger "local.sh: $MSG"
vim-cmd vmsvc/power.on $ID
STATUS=`vim-cmd vmsvc/get.guestheartbeatStatus "$ID"`
MSG="Checking status of VM: '$VMNAME' : '$STATUS'"
echo $MSG
logger "local.sh: $MSG"
while [ $STATUS != "green" ]
do
sleep 10
STATUS=`vim-cmd vmsvc/get.guestheartbeatStatus "$ID" `
COUNT=`expr $COUNT + 1`
if [ $COUNT -gt $MAXLOOPSVM ]
then
echo "Status of VM: $VMNAME - never changed to green - exiting loop"
logger "local.sh: Status of VM: $VMNAME - never changed to green - exiting loop"
break
fi
MSG="Checking status of VM: '$VMNAME' : '$STATUS'"
echo $MSG
logger "local.sh: $MSG"
done
COUNT=1
#Get field 4 from a line like, which is whether or not datastore is accessible
#TrueNAS 192.168.0.44 /mnt/tank/root/esxi true true false false Not Supported
SHARESTATUS=`esxcli storage nfs list|grep "$SHARE"| awk '{print $4}'`
MSG="Checking share status of datastore:'$SHARE' accessible:'$SHARESTATUS'"
echo $MSG
logger "local.sh: $MSG"
while [ $SHARESTATUS != "true" ]
do
sleep 10
SHARESTATUS=`esxcli storage nfs list|grep "$SHARE"| awk '{print $4}'`
COUNT=`expr $COUNT + 1`
if [ $COUNT -gt $MAXLOOPSSHARE ]
then
logger "local.sh: Status of datastore: "$SHARE" - never changed to accessible - exiting loop"
break
fi
MSG="Checking share status of datastore:'$SHARE' accessible:'$SHARESTATUS'"
echo $MSG
logger "local.sh: $MSG"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment