Jef Hauschild

Complete ESXi 5.1 Host Installation Script

Discussion created by Jef Hauschild on Jan 29, 2014
Latest reply on Feb 6, 2014 by Andrew Wark

I thought I'd put up the ESXi deployment script that I've brought together from various sources for the scripting blitz.  There's a couple Nimble-centric settings in it.  In addition to all the 'normal' setup options, it will change the default path selection policy, rename the iSCSI initiator to match the host name, scans the Nimble for existing luns, and sets the round robin PSP to change every 0 iops.  I've got one for each of our hosts and it makes re-deploying a snap.  Every time I need to restore our dev server to a 'new' state I'm happy to have it.

 

#########################################
#     ESXi 5.1 Config Script #
#########################################


# command
# ks=ftp://10.10.1.17/esxi_config_script.cfg nameserver=10.10.1.14 ip=10.10.1.199 netmask=255.255.0.0 gateway=10.10.1.244


# Accept EULA, install, set root password, reboot
vmaccepteula

# install to first disk, set root password, and reboot
install --firstdisk --overwritevmfs
rootpw pa$$word
reboot

%include /tmp/networkconfig

%pre --interpreter=busybox

#########################################
# Change the following: #
IPADDR="10.10.3.92"
HOSTNAME="ESXi1"
NETMASK="255.255.0.0"
DNS="10.10.1.14,10.10.1.9"
GATEWAY="10.10.1.244"
#########################################


# Configure management network
echo "network --bootproto=static --addvmportgroup=false --device=vmnic0 --ip=${IPADDR} --netmask=${NETMASK} --gateway=${GATEWAY} --nameserver=${DNS} --hostname=${HOSTNAME}" > /tmp/networkconfig

%firstboot --interpreter=busybox

#########################################
# Change the following:
#
ISCSI1_ADDRESS="1.1.1.111"
ISCSI2_ADDRESS="1.1.1.112"
HOSTNAME="ESXi1"
NIMBLEIP="1.1.1.10"
#########################################

###########################
#  vSwitch configuration  #
###########################

#########################################
#  Data Network 
#
#  vSwitch0 : Active -> vmnic0,vmnic4
# failback: yes
# failback detection: link
# load balancing: portid
# notify switches: yes
# allow forged transmits: yes
# allow mac changes: yes
# allow promiscuous mode: no
# cdp status: both


# Add NIC
esxcli network vswitch standard uplink add --uplink-name vmnic4 --vswitch-name vSwitch0

# Add Port Groups
esxcli network vswitch standard portgroup add --portgroup-name "Voice VLAN" --vswitch-name vSwitch0
esxcli network vswitch standard portgroup set --portgroup-name "Voice VLAN" --vlan-id 3
esxcli network vswitch standard portgroup add --portgroup-name "VM Network" --vswitch-name vSwitch0

# Config vSwitch
esxcli network vswitch standard set --cdp-status both --vswitch-name vSwitch0
esxcli network vswitch standard policy failover set --active-uplinks vmnic0,vmnic4 --vswitch-name vSwitch0
esxcli network vswitch standard policy failover set --failback yes --failure-detection link --load-balancing portid --notify-switches yes --vswitch-name vSwitch0
esxcli network vswitch standard policy security set --allow-forged-transmits yes --allow-mac-change yes --allow-promiscuous no --vswitch-name vSwitch0

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

# make vmnics active on Management Network
esxcli network vswitch standard portgroup policy failover set --active-uplinks=vmnic0 --portgroup-name="Management Network"


#########################################
#  iSCSI Network
#
# vSwitch1 : Active -> vmnic1,vmnic5
# failback: yes
# failback detection: link
# load balancing: portid
# notify switches: yes
# allow forged transmits: yes
# allow mac changes: yes
# allow promiscuous mode: no
# cdp status: both
# mtu: 9000


# create vSwitch and add NICs
esxcli network vswitch standard add --ports 128 --vswitch-name vSwitch1
esxcli network vswitch standard uplink add --uplink-name vmnic1 --vswitch-name vSwitch1
esxcli network vswitch standard uplink add --uplink-name vmnic5 --vswitch-name vSwitch1

# setup port groups
esxcli network vswitch standard portgroup add --portgroup-name "iSCSI Network" --vswitch-name vSwitch1
esxcli network vswitch standard portgroup add --portgroup-name "iSCSI1" --vswitch-name vSwitch1
esxcli network vswitch standard portgroup add --portgroup-name "iSCSI2" --vswitch-name vSwitch1

# Config vSwitch
esxcli network vswitch standard set --mtu 9000 --cdp-status both --vswitch-name vSwitch1
esxcli network vswitch standard policy failover set --active-uplinks vmnic1,vmnic5 --vswitch-name vSwitch1
esxcli network vswitch standard policy failover set --failback yes --failure-detection link --load-balancing portid --notify-switches yes --vswitch-name vSwitch1
esxcli network vswitch standard policy security set --allow-forged-transmits yes --allow-mac-change yes --allow-promiscuous no --vswitch-name vSwitch1

# add network interfaces and assign IPs
esxcli network ip interface add --interface-name vmk1 --mtu 9000 --portgroup-name iSCSI1
esxcli network ip interface add --interface-name vmk2 --mtu 9000 --portgroup-name iSCSI2
esxcli network ip interface ipv4 set --interface-name vmk1 --ipv4 ${ISCSI1_ADDRESS} --netmask 255.255.255.0 --type static
esxcli network ip interface ipv4 set --interface-name vmk2 --ipv4 ${ISCSI2_ADDRESS} --netmask 255.255.255.0 --type static

# Set Active
esxcli network vswitch standard portgroup policy failover set --active-uplinks=vmnic1 --portgroup-name=iSCSI1
esxcli network vswitch standard portgroup policy failover set --active-uplinks=vmnic5 --portgroup-name=iSCSI2

# Override Failover
esxcli network vswitch standard portgroup policy failover set -p iSCSI1 -a vmnic1
esxcli network vswitch standard portgroup policy failover set -p iSCSI2 -a vmnic5

# Bond NICs to ISCSI Adapters
esxcli iscsi networkportal add -A ${ADAPTER} -n vmk1
esxcli iscsi networkportal add -A ${ADAPTER} -n vmk2

##########################
#  ISCSI Configuration   #
##########################

# Set PSP to default to Round Robin for Nimble
esxcli storage nmp satp set --satp VMW_SATP_ALUA --default-psp VMW_PSP_RR

# Enable iSCSI software adapter
esxcli iscsi software set --enabled=true

# Get iSCSI software initator Name and Rename iSCSI Initiator to match hostname
ADAPTER=`esxcli iscsi adapter list | grep Software | awk '{print $1;}'`
esxcli iscsi adapter set -A ${ADAPTER} --name iqn.1998-01.com.vmware:${HOSTNAME}

# Add Nimble target IP
esxcli iscsi adapter discovery sendtarget add -A ${ADAPTER} -a ${NIMBLEIP}

# Scan software ISCSI bus for datastores
esxcli storage core adapter rescan --adapter ${ADAPTER}

# change round robin path selection policy to switch every 0 iops
i=`esxcli storage nmp device list | awk '/Nimble iSCSI Disk/{print $7}' | sed -e 's/(//' -e 's/)//'`
for p in $i; do esxcli storage nmp psp roundrobin deviceconfig set -d $p --iops 0 --type=iops;done

# NTP Config
cat > /etc/ntp.conf << __NTP_CONFIG__
restrict default kod nomodify notrap noquerynopeer
restrict 127.0.0.1
server 0.us.pool.ntp.org
server 1.us.pool.ntp.org
__NTP_CONFIG__
/sbin/chkconfig ntpd on

# backup ESXi configuration to persist changes 
/sbin/auto-backup.sh

# enter maintenance mode
esxcli system maintenanceMode set -e true

# copy logs to datastore
cp /var/log/hostd.log "/vmfs/volumes/DS2/DeploymentLogs/${HOSTNAME}/firstboot-hostd.log"
cp /var/log/esxi_install.log "/vmfs/volumes/DS2/DeploymentLogs/${HOSTNAME}/firstboot-esxi_install.log"

# Reboot host to finish all setup
esxcli system shutdown reboot -d 60 -r "rebooting after host configurations"

 

Deploying is pretty easy, put in the install media, hit Shift-O when prompted, put in the location of your script (we deploy from an FTP server, but VMware says you can also use HTTP, NFS, USB, or CDROM), and wait.  After a few reboots you'll have a fully configured host waiting for you.

 

Once I have a host deployed, I use Derek Seaman's great script to deploy a SSL cert from here:

 

http://www.derekseaman.com/2013/02/vmware-vcenter-51-installation-part-15.html

 

Add it to vCenter and away we go.  I haven't looked at ESXi 5.5 yet, but likely there would only have to be a few changes to make work.

 

Hopefully this helps someone out there, I know we like it!

Outcomes