Script to Automatically Rescan and Mount Software iSCSI Datastores on Startup for ESXi

Update 09-May-2020: The original script didn’t really work so I have commented it out.
Update 11-May-2020: Added method to search by Volume Name.
Update 23-Mar-2023: This also works for TrueNAS

There are several reasons you may want to have ESXi rescan iSCSI at boot. For this example a FreeNAS guest VM was running an iSCSI target and sharing it back to the ESXi host. ESXi was then using it as a datastore with other VMs on it that also needed to be autostart. The trick is to start the FreeNAS guest and give it enough time to boot before letting ESXi proceed to the autostart step.

For troubleshooting, the script uses logger to send text to /var/log/syslog.log of the host.

This script should be added to the following file in ESXi (using vi) so that it is preserved by ESXi upon reboot.
Note: the last line of this file should be preserved (exit 0).

#Establish our timer

#Power on the guest VM with the specified Vmid
#use the command vim-cmd vmsvc/getallvms to find which Vmid to use
vim-cmd vmsvc/power.on 1

#Now continuously rescan for the iSCSI device until it is found
#or the maximum time of 10 minutes is reached.
#This command will search all Logical Devices for one that has "Vendor_Name" in the Display Name (e.g., FreeNAS)
while ! esxcfg-scsidevs -c | grep -q 'Vendor_Name'

#Alternatively if you have multiple iSCSI targets that share the same Display Name
#(iSCSI Vendor) then you may want to instead search by Volume Name.
#This method allows you to single out a specific server since Volume Name is user configurable.
#The command below will search for the volume name 'Your_custom_volume_name' and that Mounted status is true.
#while ! esxcli storage filesystem list | grep -q "Your_custom_volume_name.*true"
    #print some debugging info to the syslog
    logger " Forcing rescan since iSCSI target is not yet available..."
    #Rescan SCSI HBAs to search for new Devices, remove DEAD paths and update path state. 
    #This operation will also run an claim operation equivalent to the claimrule run command and a filesystem rescan.
    esxcli storage core adapter rescan --all

    #Now wait (in seconds) before checking again
    sleep 30

    #Increase the timer
    count=`expr $count + 30`

    #Check if maximum time has been reached (in seconds)
    if [ $count -ge 600 ]
       logger " Aborting, maximum time reached while searching for iSCSI target."

logger " Search time for iSCSI target was" $count "seconds."

#-------------- Old script is below, do not use

#Adjust the delay in seconds as needed to allow time for NAS to boot up fully
#sleep 300

#Enable iSCSI Initiator and rescan for iSCSI specific LUNs.
#esxcfg-swiscsi -e
#esxcfg-swiscsi -s

#Search for new VMFS datastores. If a new datastore has been detected, it is mounted in /vmfs/volumes/
#vmkfstools -V

#another delay just to be sure
#sleep 10

#Run the ESXi autostart script again to start all VMs that are on the datastore
#sh /usr/sbin/ start




This entry was posted in Uncategorized. Bookmark the permalink.