From d64ca870fea96b91f68b1e8a4c287e52e29d3e88 Mon Sep 17 00:00:00 2001 From: Rui Lopes Date: Sat, 28 Apr 2018 08:22:40 +0100 Subject: [PATCH] add support for the vsphere builder --- Makefile | 7 +++ README-vsphere.md | 20 ++++++++ README.md | 5 ++ autounattend.xml | 9 +++- provision.ps1 | 2 + vmtools.ps1 | 19 +++++++ windows-2016-vsphere.json | 104 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 README-vsphere.md create mode 100644 vmtools.ps1 create mode 100644 windows-2016-vsphere.json diff --git a/Makefile b/Makefile index 69b2f9a..e66feb0 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,8 @@ build-libvirt: windows-2016-amd64-libvirt.box build-virtualbox: windows-2016-amd64-virtualbox.box +build-vsphere: windows-2016-amd64-vsphere.box + build-windows-2019-virtualbox: windows-2019-amd64-virtualbox.box build-windows-server-core-1709-libvirt: windows-server-core-1709-amd64-libvirt.box @@ -33,6 +35,11 @@ windows-2016-amd64-virtualbox.box: windows-2016.json autounattend.xml Vagrantfil @echo to add to local vagrant install do: @echo vagrant box add -f windows-2016-amd64 $@ +windows-2016-amd64-vsphere.box: windows-2016.json autounattend.xml Vagrantfile.template *.ps1 + rm -f $@ + CHECKPOINT_DISABLE=1 packer build -on-error=abort windows-2016-vsphere.json + @echo BOX successfully built! + windows-2019-amd64-virtualbox.box: windows-2019.json autounattend.xml Vagrantfile.template *.ps1 rm -f $@ CHECKPOINT_DISABLE=1 packer build -only=windows-2019-amd64-virtualbox -on-error=abort windows-2019.json diff --git a/README-vsphere.md b/README-vsphere.md new file mode 100644 index 0000000..3e187ea --- /dev/null +++ b/README-vsphere.md @@ -0,0 +1,20 @@ +Download the latest `packer-builder-vsphere-iso.exe` release from [jetbrains-infra/packer-builder-vsphere](https://github.com/jetbrains-infra/packer-builder-vsphere/releases) and place it inside your `%APPDATA%\packer.d\plugins` directory. + +Download the Windows Evaluation iso (you can find the full iso URL in the [windows-2016.json](windows-2016.json) file) and place it inside the datastore as defined by the `vsphere_iso_url` user variable that is inside the [packer template](windows-2016-vsphere.json). + +Download the VMware Tools zip and extract the `windows.iso` file into the datastore defined by the `vsphere_tools_iso_url` user variable that is inside the [packer template](windows-2016-vsphere.json). + +Build the base box with: + +```bash +make build-vsphere +``` + +**NB** the generated template will include a reference to the extra iso images that were used to create the template, which is a bummer. + +**NB** these errors are expected (should be fixed when https://github.com/jetbrains-infra/packer-builder-vsphere/pull/82 is release): +``` +==> windows-2016-amd64-vsphere: error removing floppy: The operation is not supported on the object. +==> windows-2016-amd64-vsphere: error removing cdroms: The operation is not supported on the object. +``` + diff --git a/README.md b/README.md index e07c21c..7858480 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,11 @@ spicy --uri 'spice+unix:///tmp/packer-windows-2016-amd64-libvirt-spice.socket' **NB** the packer template file defines `qemuargs` (which overrides the default packer qemu arguments), if you modify it, verify if you also need include the default packer qemu arguments (see [builder/qemu/step_run.go](https://github.com/hashicorp/packer/blob/master/builder/qemu/step_run.go) or start packer without `qemuargs` defined to see how it starts qemu). +## vSphere + +See [README-vsphere.md](README-vsphere.md). + + ## WinRM access You can connect to this machine through WinRM to run a remote command, e.g.: diff --git a/autounattend.xml b/autounattend.xml index 44babc3..5fc872e 100644 --- a/autounattend.xml +++ b/autounattend.xml @@ -4,7 +4,10 @@ - a:\ + A:\ + + + E:\ @@ -116,6 +119,10 @@ 3 + PowerShell -File a:\vmtools.ps1 + + + 4 PowerShell -File a:\winrm.ps1 diff --git a/provision.ps1 b/provision.ps1 index a9d1d0a..3dcc96d 100644 --- a/provision.ps1 +++ b/provision.ps1 @@ -72,6 +72,8 @@ if ($systemVendor -eq 'QEMU') { if ($p.ExitCode) { throw "failed to install with exit code $($p.ExitCode). Check the logs at C:\Program Files\Oracle\VirtualBox Guest Additions\install.log." } +} elseif ($systemVendor -eq 'VMware, Inc.') { + # do nothing. VMware Tools were already installed by vmtools.ps1 (executed from autounattend.xml). } else { throw "Cannot install Guest Additions: Unsupported system ($systemVendor)." } diff --git a/vmtools.ps1 b/vmtools.ps1 new file mode 100644 index 0000000..a6bb127 --- /dev/null +++ b/vmtools.ps1 @@ -0,0 +1,19 @@ +Set-StrictMode -Version Latest +$ProgressPreference = 'SilentlyContinue' +$ErrorActionPreference = 'Stop' +trap { + Write-Host + Write-Output "ERROR: $_" + Write-Output (($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1') + Write-Host 'Sleeping for 60m to give you time to look around the virtual machine before self-destruction...' + Start-Sleep -Seconds (60*60) + Exit 1 +} + +$systemVendor = (Get-WmiObject Win32_ComputerSystemProduct Vendor).Vendor +if ($systemVendor -eq 'VMware, Inc.') { + Write-Output 'Installing VMware Tools...' + # silent install without rebooting. + E:\setup64.exe /s /v '/qn reboot=r' ` + | Out-String -Stream +} diff --git a/windows-2016-vsphere.json b/windows-2016-vsphere.json new file mode 100644 index 0000000..ff29d6a --- /dev/null +++ b/windows-2016-vsphere.json @@ -0,0 +1,104 @@ +{ + "variables": { + "vsphere_disk_size": "60", + "vsphere_iso_url": "[Datastore] iso/14393.0.161119-1705.RS1_REFRESH_SERVER_EVAL_X64FRE_EN-US.ISO", + "vsphere_tools_iso_url": "[Datastore] iso/vmware-tools-10.2.5/windows.iso", + "vsphere_host": "vcenter.local", + "vsphere_username": "administrator@vsphere.local", + "vsphere_password": "password", + "vsphere_esxi_host": "esxi.local", + "vsphere_datacenter": "Datacenter", + "vsphere_cluster": "Cluster", + "vsphere_datastore": "Datastore", + "vsphere_folder": "test", + "vsphere_network": "packer" + }, + "builders": [ + { + "name": "windows-2016-amd64-vsphere", + "vm_name": "windows-2016-amd64-vsphere", + "type": "vsphere-iso", + "guest_os_type": "windows9Server64Guest", + "disk_size": "{{user `vsphere_disk_size`}}", + "disk_thin_provisioned": true, + "disk_controller_type": "pvscsi", + "vcenter_server": "{{user `vsphere_host`}}", + "username": "{{user `vsphere_username`}}", + "password": "{{user `vsphere_password`}}", + "insecure_connection": "true", + "datacenter": "{{user `vsphere_datacenter`}}", + "cluster": "{{user `vsphere_cluster`}}", + "host": "{{user `vsphere_esxi_host`}}", + "folder": "{{user `vsphere_folder`}}", + "datastore": "{{user `vsphere_datastore`}}", + "network_card": "vmxnet3", + "network": "{{user `vsphere_network`}}", + "convert_to_template": true, + "RAM": 2048, + "CPUs": 4, + "iso_paths": [ + "{{user `vsphere_iso_url`}}", + "{{user `vsphere_tools_iso_url`}}" + ], + "floppy_files": [ + "autounattend.xml", + "vmtools.ps1", + "winrm.ps1" + ], + "boot_order": "disk,cdrom", + "communicator": "winrm", + "winrm_username": "vagrant", + "winrm_password": "vagrant", + "winrm_timeout": "4h", + "shutdown_command": "shutdown /s /t 0 /f /d p:4:1 /c \"Packer Shutdown\"" + } + ], + "provisioners": [ + { + "type": "powershell", + "script": "disable-windows-updates.ps1" + }, + { + "type": "powershell", + "inline": "Uninstall-WindowsFeature Windows-Defender-Features" + }, + { + "type": "powershell", + "inline": "Uninstall-WindowsFeature FS-SMB1" + }, + { + "type": "powershell", + "script": "virtualbox-prevent-vboxsrv-resolution-delay.ps1" + }, + { + "type": "windows-restart" + }, + { + "type": "windows-update", + "filters": [ + "exclude:$_.Title -like '*Preview*'", + "include:$_.Title -like '*Cumulative Update for Windows*'", + "include:$_.AutoSelectOnWebSites" + ] + }, + { + "type": "powershell", + "script": "provision.ps1" + }, + { + "type": "powershell", + "script": "provision-openssh.ps1" + }, + { + "type": "windows-restart" + }, + { + "type": "powershell", + "script": "eject-media.ps1" + }, + { + "type": "powershell", + "script": "optimize.ps1" + } + ] +}