├── .gitignore ├── scripts ├── esxi-settings.sh ├── esxi-cloning_configuration.sh ├── vagrant.pub ├── esxi-dvfilter_install.sh ├── esxi-kickstart.cfg ├── esxi-vnic-fix.sh └── esxi-vagrantfile_template.rb ├── README.md └── packer-esxi.json /.gitignore: -------------------------------------------------------------------------------- 1 | *.tar.gz 2 | *.box 3 | packer_cache 4 | iso 5 | output-vmware-esxi 6 | -------------------------------------------------------------------------------- /scripts/esxi-settings.sh: -------------------------------------------------------------------------------- 1 | esxcli network firewall ruleset set -e true -r httpClient 2 | esxcli system settings advanced set -o /Net/GuestIPHack -i 1 3 | -------------------------------------------------------------------------------- /scripts/esxi-cloning_configuration.sh: -------------------------------------------------------------------------------- 1 | # Settings to ensure that ESXi cloning goes smooth, thanks @lamw ! see: 2 | # http://www.virtuallyghetto.com/2013/12/how-to-properly-clone-nested-esxi-vm.html 3 | esxcli system settings advanced set -o /Net/FollowHardwareMac -i 1 4 | sed -i 's#/system/uuid.*##' /etc/vmware/esx.conf 5 | 6 | # Ensure changes are persistent 7 | /sbin/auto-backup.sh 8 | -------------------------------------------------------------------------------- /scripts/vagrant.pub: -------------------------------------------------------------------------------- 1 | ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key 2 | -------------------------------------------------------------------------------- /scripts/esxi-dvfilter_install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Install ESXI MAC LEARNING DVFILTER directly from VMware website, make 3 | # sure you have internet connection available or change the URL to a local 4 | # one. 5 | 6 | os_ver=$(uname -r | sed -rn 's/.*([0-9])\.[0-9].*\.[0-9].*/\1/p') 7 | 8 | # This VIB is no longer needed with vSphere 7 9 | if [ "$os_ver" -gt 6 ] ; then 10 | exit 0 11 | fi 12 | 13 | esxcli software vib install -v https://download3.vmware.com/software/vmw-tools/esxi-mac-learning-dvfilter/esx-dvfilter-maclearn-6.5.0.vib -f 14 | -------------------------------------------------------------------------------- /scripts/esxi-kickstart.cfg: -------------------------------------------------------------------------------- 1 | # 2 | # Sample scripted installation file 3 | # 4 | # Accept EULA 5 | vmaccepteula 6 | # Set root password 7 | rootpw Vagrant!23 8 | #Install on local disk overwriting any existing VMFS datastore 9 | install --firstdisk --overwritevmfs 10 | # Network configuration 11 | network --bootproto=dhcp --device=vmnic0 12 | #Reboot after installation completed 13 | reboot 14 | 15 | %firstboot --interpreter=busybox 16 | #esx/ssh 17 | vim-cmd hostsvc/enable_ssh 18 | vim-cmd hostsvc/start_ssh 19 | esxcli system settings advanced set -o /UserVars/SuppressShellWarning -i 1 20 | #esxi/ssh end 21 | -------------------------------------------------------------------------------- /scripts/esxi-vnic-fix.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # When an ESX VM is cloned, vnic0 gets a new mac from the vmx's 4 | # ethernet0.generatedAddress, but vmk0's mac is persisted in /etc/vmware/esx.conf 5 | # vagrant uses ethernet0.generatedAddress to lookup the VM ip in 6 | # vmnet-dhcpd-vmnet8.leases, reconfigure here if needed. 7 | 8 | os_ver=$(uname -r | sed -rn 's/.*([0-9])\.[0-9].*\.[0-9].*/\1/p') 9 | 10 | # This issue was fixed in vSphere 7 11 | if [ "$os_ver" -gt 6 ] ; then 12 | exit 0 13 | fi 14 | 15 | if [ "$os_ver" -lt 6 ] ; then 16 | vnic0_mac=$(esxcli --formatter csv network nic list | grep vmnic0 | awk -F, '{print $5}') 17 | vmk0_mac=$(esxcli --formatter csv network ip interface list | grep vmk0 | awk -F, '{print $2}') 18 | else 19 | vnic0_mac=$(esxcli --formatter csv network nic list | grep vmnic0 | awk -F, '{print $7}') 20 | vmk0_mac=$(esxcli --formatter csv network ip interface list | grep vmk0 | awk -F, '{print $2}') 21 | fi 22 | 23 | if [ "$vnic0_mac" != "$vmk0_mac" ] ; then 24 | esxcli network ip interface remove -i vmk0 25 | 26 | esxcli network ip interface add -i vmk0 -M $vnic0_mac -p "Management Network" 27 | 28 | esxcli network ip interface ipv4 set -i vmk0 -t dhcp 29 | fi 30 | -------------------------------------------------------------------------------- /scripts/esxi-vagrantfile_template.rb: -------------------------------------------------------------------------------- 1 | # The contents below (if any) are custom contents provided by the 2 | # Packer template during image build. 3 | Vagrant.require_version '>= 2.0.0' 4 | 5 | Vagrant.configure('2') do |config| 6 | # We're using the root user here. 7 | config.ssh.username = 'root' 8 | # Set default shell to SH. 9 | config.ssh.shell = 'sh' 10 | 11 | # Vagrant doesn't support insert key on ESXi so let's stick with the unsecure 12 | # key for now. 13 | config.ssh.insert_key = false 14 | 15 | # Do not sync default /vagrant folder on ESXi. 16 | config.vm.synced_folder '.', '/vagrant', disabled: true 17 | 18 | # We don't have NFS working inside ESXi so we flag this just in case. 19 | config.nfs.functional = false 20 | 21 | config.vm.provider "vmware_desktop" do |v| 22 | v.vmx['memsize'] = '4096' 23 | v.vmx['numvcpus'] = '2' 24 | # Use paravirtualized virtual hardware on VMW hypervisors. 25 | v.vmx["ethernet0.virtualDev"] = "vmxnet3" 26 | # use paravirtualized scsi hardware on VMW hypervisors. 27 | v.vmx['scsi0.virtualdev'] = 'pvscsi' 28 | # Enable nested virtualization. 29 | v.vmx['vhv.enable'] = 'true' 30 | end 31 | 32 | config.vm.provider :vcenter do |vcenter| 33 | vcenter.num_cpu = 2 34 | vcenter.memory = 4096 35 | vcenter.enable_vm_customization = false 36 | end 37 | end 38 | 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # VMware [ESXi](https://www.vmware.com/products/esxi-and-esx/overview) [Packer](http://packer.io) templates 2 | 3 | 4 | ## Prerequisites 5 | 6 | * [Packer](http://packer.io) > 1.5 7 | * VMware Fusion or Workstation 8 | 9 | ## Build artifacts 10 | 11 | This packer template requires to variables to be set either via command line or separate JSON file: 12 | 13 | 1. `iso_file` - is the ESXi ISO for the build (can be local or remote). 14 | 1. `iso_checksum` - is the ESXi ISO checksum you can find on MyVMware when downloading the image, prefixed with the type (such as `md5:`). 15 | 16 | To kick off a full build: 17 | 18 | ```shell 19 | packer build \ 20 | -var 'iso_file=iso/VMware-VMvisor-Installer-7.0U1c-17325551.x86_64.iso' \ 21 | -var 'iso_checksum=md5:4829a336445741bf13f10bcb79723d40' \ 22 | packer-esxi.json 23 | ``` 24 | 25 | The Packer template and the scripts have been tested with ESXi version 5.5, 6.x and 7.0.1. 26 | 27 | Default `root` password for the resulting Vagrant box is `Vagrant!23`. 28 | 29 | ## Legal 30 | 31 | Copyright © 2015 VMware, Inc. All Rights Reserved. 32 | 33 | Licensed under the Apache License, Version 2.0 (the “License”); you may not 34 | use this file except in compliance with the License. You may obtain a copy of 35 | the License at http://www.apache.org/licenses/LICENSE-2.0 36 | 37 | Unless required by applicable law or agreed to in writing, software distributed 38 | under the License is distributed on an “AS IS” BASIS, without warranties or 39 | conditions of any kind, EITHER EXPRESS OR IMPLIED. See the License for the 40 | specific language governing permissions and limitations under the License. 41 | -------------------------------------------------------------------------------- /packer-esxi.json: -------------------------------------------------------------------------------- 1 | { 2 | "variables": { 3 | "iso_file": null, 4 | "iso_checksum": "", 5 | "disk_size": "40960", 6 | "root_password": "Vagrant!23" 7 | }, 8 | "builders": [ 9 | { 10 | "name": "vmware-esxi", 11 | "vm_name": "vmware-esxi", 12 | "cpus": 2, 13 | "memory": 4096, 14 | "vmdk_name": "vmware-esxi-disk0", 15 | "type": "vmware-iso", 16 | "headless": false, 17 | "disk_size": "{{ user `disk_size` }}", 18 | "disk_type_id": 0, 19 | "disk_adapter_type": "scsi", 20 | "guest_os_type": "vmkernel6", 21 | "iso_url": "{{user `iso_file`}}", 22 | "iso_checksum": "{{user `iso_checksum`}}", 23 | "network_adapter_type": "vmxnet3", 24 | "ssh_username": "root", 25 | "ssh_password": "{{user `root_password`}}", 26 | "ssh_wait_timeout": "60m", 27 | "shutdown_command": "esxcli system maintenanceMode set -e true -t 0 ; esxcli system shutdown poweroff -d 10 -r 'Packer Shutdown' ; esxcli system maintenanceMode set -e false -t 0", 28 | "http_directory": "scripts", 29 | "boot_wait": "5s", 30 | "version": 11, 31 | "vmx_data": { 32 | "scsi0.virtualdev": "pvscsi", 33 | "vhv.enable": "TRUE" 34 | }, 35 | "boot_command": [ 36 | "O ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/esxi-kickstart.cfg" 37 | ] 38 | } 39 | ], 40 | "provisioners": [ 41 | { 42 | "type": "file", 43 | "source": "scripts/vagrant.pub", 44 | "destination": "/etc/ssh/keys-root/authorized_keys" 45 | }, 46 | { 47 | "type": "shell", 48 | "scripts": [ 49 | "scripts/esxi-settings.sh", 50 | "scripts/esxi-dvfilter_install.sh" 51 | ] 52 | }, 53 | { 54 | "type": "file", 55 | "source": "scripts/esxi-vnic-fix.sh", 56 | "destination": "/etc/rc.local.d/local.sh" 57 | }, 58 | { 59 | "type": "shell", 60 | "script": "scripts/esxi-cloning_configuration.sh" 61 | } 62 | ], 63 | "post-processors": [ 64 | [ 65 | { 66 | "type": "vagrant", 67 | "compression_level": 9, 68 | "vagrantfile_template": "scripts/esxi-vagrantfile_template.rb" 69 | } 70 | ] 71 | ] 72 | } 73 | --------------------------------------------------------------------------------