frapposelli / esxi-packer-templates

VMware ESXi Packer templates
23 stars 12 forks source link

vnic-fix doesn't work since esxcli report "connection failed" #15

Open leslie-qiwa opened 6 years ago

leslie-qiwa commented 6 years ago

Thanks for the patch. It is exactly useful. I'm trying to do nested ESX with the template, but MAC address is not updated when new VM boots. I tried to log message, and turned out it reports "connection failed" by esxcli.

My ESX version is 6.5.0.

icnocop commented 3 years ago

Hi @leslie-qiwa

Did you ever resolve this issue? If so, how did you resolve it?

Thank you.

leslie-qiwa commented 3 years ago

unfortunately, not yet.

icnocop commented 3 years ago

I was able to resolve this issue by delaying/waiting until a connection could be established.

I got the hint from this post: https://communities.vmware.com/t5/ESXi-Discussions/Imaging-ESX-5-1-MAC-address-issues-with-DHCP/td-p/2152927 Sleep command is critical with out this delay the script executes too fast during the boot process and script will not work at boot time,

#!/bin/sh

# When an ESX VM is cloned, vnic0 gets a new mac from the vmx's
# ethernet0.generatedAddress, but vmk0's mac is persisted in /etc/vmware/esx.conf
# vagrant uses ethernet0.generatedAddress to lookup the VM ip in
# vmnet-dhcpd-vmnet8.leases, reconfigure here if needed.

os_ver=$(uname -r | awk -F. '{print $1}')

while [ "$vnic0_mac" == "" -o "$vmk0_mac" == "" ]
do
  if [ "$os_ver" -lt 6 ] ; then
    vnic0_mac=$(esxcli --formatter csv network nic list | grep vmnic0 | awk -F, '{print $5}')
    vmk0_mac=$(esxcli --formatter csv network ip interface list | grep vmk0 | awk -F, '{print $2}')
  else
    vnic0_mac=$(esxcli --formatter csv network nic list | grep vmnic0 | awk -F, '{print $7}')
    vmk0_mac=$(esxcli --formatter csv network ip interface list | grep vmk0 | awk -F, '{print $3}')
  fi
done

if [ "$vnic0_mac" != "$vmk0_mac" ] ; then
  esxcli network ip interface remove -i vmk0

  esxcli network ip interface add -i vmk0 -M $vnic0_mac -p "Management Network"

  esxcli network ip interface ipv4 set -i vmk0 -t dhcp
fi