ESX Auto Build Script


As part of my Intel NUC As A ESXi Host series, the following script helps to automate almost all the configuration steps needed for me to quickly provision a new NUC server, or to rebuild an existing one.

It performs the following basic steps…

  • Set root password,
  • Enables the ESXi SSH Shell, and disables the warning associated with it,
  • Renames the local datastore name to be “Local – [hostname]“,
  • Sets up all the networking virtual switches and VLANs that I need,
  • Enables vMotion,
  • Enables and configures the software iSCSI adapter,
  • Puts the host into maintenance mode before the final reboot

I’ll go through the script in detail below.


The Script

rootpw [enter-password-here]
keyboard 'United Kingdom'
install --firstdisk --overwritevmfs
network --bootproto=dhcp

%firstboot --interpreter=busybox

# Enable & start remote ESXi shell
vim-cmd hostsvc/enable_ssh
vim-cmd hostsvc/start_ssh

# Enable & start ESXi shell
vim-cmd hostsvc/enable_esx_shell
vim-cmd hostsvc/start_esx_shell

# Suppress ESXi shell shell warning and set time-out
esxcli system settings advanced set -o /UserVars/SuppressShellWarning -i 1
esxcli system settings advanced set -o /UserVars/ESXiShellInteractiveTimeOut -i 3600

# Rename local data store
vim-cmd hostsvc/datastore/rename datastore1 "Local - $(hostname -s)"

# Configure networking and disable IPv6
esxcli network ip set --ipv6-enabled=false
esxcli network vswitch standard portgroup add -p "Live" -v vSwitch0
esxcli network vswitch standard portgroup add -p "Private" -v vSwitch0
esxcli network vswitch standard portgroup set -p "Private" -v 42
esxcli network vswitch standard portgroup remove -p "VM Network" -v vSwitch0

# Enable vMotion on adapter
vim-cmd hostsvc/vmotion/vnic_set vmk0

# Enable and configure iSCSI software adapter
esxcli iscsi software set --enabled=true
sleep 5
iscsi_adapter=$(esxcli iscsi physicalnetworkportal list | grep vmhba | awk '{print $1}')
esxcli iscsi networkportal add -A ${iscsi_adapter} -n vmk0
esxcli iscsi adapter set -A ${iscsi_adapter} -a $(hostname -s)
esxcli iscsi adapter param set -A ${iscsi_adapter} -k HeaderDigest -v required
esxcli iscsi adapter param set -A ${iscsi_adapter} -k DataDigest -v required
esxcli iscsi adapter discovery sendtarget add -A ${iscsi_adapter} -a [iscsi-target-address-here]
esxcli storage core adapter rescan -A ${iscsi_adapter}

# Enter maintenance mode, then reboot
esxcli system maintenanceMode set -e true
esxcli system shutdown reboot -d 10 -r "Rebooting"


Detailed Line-By-Line Descriptions

  • Line 1 : Accept the VMware End User Licence Agreement
  • Line 2 : Set the master ROOT password for this ESXi host
  • Line 3 : Set keyboard to British English, United Kingdom – the correct type of English.
  • Line 4 : Install ESXi to the first drive, overwriting anything it finds
  • Line 5 : Use DHCP to obtain an IP address for the network
  • Line 6 : Reboot
  • Line 8 : Set the command interpreter to ‘BusyBox’ for the following instructions
  • Line 10-16 : Enable and start SSH and the ESXi Shell
  • Line 18-20 : Disable the SSH warning, and set the shell timeout to 1 hour
  • Line 23 : Rename the local datastore name to “Local – [hostname]”
  • Line 25-30 : Set up networking.  I use two networks…
    • LIVE : For all the virtual machines that are available on my live home network
    • PRIVATE :  For all the virtual machines that are only available to each other, and can’t be accessed from anywhere.
      • This network also uses a different VLAN to help separate the network traffic – VLAN 42.
  • Line 33 : Enable vMotion on the interface
  • Line 35-44 : Enable the software iSCSI adapter and configure it.  Perform a adapter rescan too
  • Line 47 : Put host into maintenance mode
  • Line 48 : One final reboot, ready for homelab production

Leave a Reply

Your email address will not be published.