- Joined
- May 13, 2015
- Messages
- 2,478
With FreeNAS shell scripts executed as post-init tasks.How do you setup the bootorder to make this work? (I read somewhere a grinch comment about boot strap paradoxes)
If you enable SSH on the ESXi server and set up 'passwordless' access with public keys, you can use VMware CLI commands to do pretty much anything you need to do on the ESXi server.
I have a set of startup scripts that:
> Force ESXi to re-scan all data stores. This makes the NFS or iSCSI FreeNAS datastores 'wake up' and become available to ESXi when FreeNAS is restarted.
> Start virtual machines
I also have shutdown scripts that gracefully powers down all running VMs when FreeNAS is shut down. This works great -- except with the current stable release of FreeNAS 9.10, though, oddly enough, the scripts work fine with the nightlys. So I anticipate that the next stable 9.10 release will fix the problem. I filed a bug report here (the actual problem is that networking - or name resolution, at least - is stopped before user shutdown tasks are executed) :
https://bugs.freenas.org/issues/15323
The shutdown scripts are available at the bug report, if you're interested in downloading and using them.
Here is the startup script I set up as a post-init task:
Code:
#!/bin/bash L_LOGFILE="/mnt/tank/sysadmin/log/vmware-startup.log" L_WAIT_DELAY=30 echo "$(date): Force datastore re-scan on FELIX..." > $L_LOGFILE ssh root@felix.ncs esxcli storage core adapter rescan --all >> $L_LOGFILE echo "Pausing $L_WAIT_DELAY seconds before starting virtual machines..." >> $L_LOGFILE sleep $L_WAIT_DELAY /mnt/tank/sysadmin/start-virtual-machine.sh root felix adonis >> $L_LOGFILE /mnt/tank/sysadmin/start-virtual-machine.sh root felix aphrodite >> $L_LOGFILE exit
And here is the script that starts up a given VM ('start-virtual-machine.sh'):
Code:
#!/bin/bash ################################################################################ # Usage: start-virtual-machine.sh user_id esxi_host_name vmx_base_filename # # Starts guest virtual machine with vmx file (vmx_base_filename'.vmx') on remote # VMware ESXi server (esxi_host_name) using given user credentials (user_id) # # Sends commands to the remote host using SSH, which must be configured before # using this script. # # Tested with FreeNAS 9.3 (STABLE) running as a VM on VMware ESXi v6.0 ################################################################################ # Check for usage errors if [ $# -ne 3 ] then echo "$0: error! Not enough arguments" echo "Usage is: $0 user_id esxi_host_name vmx_filename" echo "Only specify the vmx basefilename; leave off the '.vmx' extension" exit 1 fi # Gather command-line arguments for user ID, hostname, and datastore name: L_USER=$1 L_HOST=$2 L_VMXNAME=$3 # Get server ID for the VM with matching vmx file: L_GUEST_VMIDS=$(ssh ${L_USER}@${L_HOST} vim-cmd vmsvc/getallvms | grep "/${L_VMXNAME}.vmx" | awk '$1 ~ /^[0-9]+$/ {print $1}') echo "$0: $L_USER@$L_HOST vmx=$L_VMXNAME.vmx" for L_VMID in $L_GUEST_VMIDS do ssh ${L_USER}@${L_HOST} vim-cmd vmsvc/power.getstate $L_VMID | grep -i "off\|Suspended" > /dev/null 2<&1 L_SHUTDOWN_STATUS=$? if [ $L_SHUTDOWN_STATUS -ne 0 ]; then echo "Guest VM ID $L_VMID already powered up..." else echo "Powering up guest VM ID $L_VMID..." ssh ${L_USER}@${L_HOST} vim-cmd vmsvc/power.on $L_VMID fi done exit