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

There are several reasons you may want to have ESXi rescan for datastores at boot. For this example a FreeNAS guest VM was running an iSCSI target and sharing it back to the ESXi host which used it as a datastore. This script should be added to the following file in ESXi (using vi) so that it is preserved by ESXi upon reboot.

/etc/rc.local.d/local.sh
Note: the last line of this file should be preserved (exit 0).

#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/vmware-autostart.sh start

 

 

 

This entry was posted in Uncategorized. Bookmark the permalink.