From 6635f99ff278da6e70be7a9a3ea55869c7a46fd8 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Mon, 17 Apr 2023 16:17:04 -0400 Subject: [PATCH] Add a set of working VMware Fusion examples (#130) * Add example templates for Fusion v12 and v13 * Create initial template bundle structure with pkrvar files * Add variable for memory * Update preseed configuration * Add adapter types to config * Add output directory for each build Co-authored-by: Lucas Bajolet <105649352+lbajolet-hashicorp@users.noreply.github.com> --- .gitignore | 3 +- README.md | 3 + example/README.md | 41 +++++++ example/build.pkr.hcl | 23 ++-- example/data/debian/preseed.pkrtpl.hcl | 104 ++++++++++++++++++ example/http/preseed.cfg | 42 ------- example/pkrvars/debian/fusion-12.pkrvars.hcl | 8 ++ example/pkrvars/debian/fusion-13.pkrvars.hcl | 18 +++ example/sources.pkr.hcl | 40 +++++++ example/variables.pkr.hcl | 110 +++++++++++++++++++ 10 files changed, 335 insertions(+), 57 deletions(-) create mode 100644 example/README.md create mode 100644 example/data/debian/preseed.pkrtpl.hcl delete mode 100644 example/http/preseed.cfg create mode 100644 example/pkrvars/debian/fusion-12.pkrvars.hcl create mode 100644 example/pkrvars/debian/fusion-13.pkrvars.hcl create mode 100644 example/sources.pkr.hcl create mode 100644 example/variables.pkr.hcl diff --git a/.gitignore b/.gitignore index d5d465e4..cad49d31 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ main dist/* packer-plugin-vmware -example/output-example/ +example/builds/*/ +.DS_Store .docs diff --git a/README.md b/README.md index ce9b2de3..d1603797 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,9 @@ Matrix from General Availability to End of General Support. Learn more: Required if building the plugin. +## Usage + +For a few examples on how to use this plugin with Packer refer to the [example](example/) template directory. ## Installation ### Using Pre-built Releases diff --git a/example/README.md b/example/README.md new file mode 100644 index 00000000..50b488eb --- /dev/null +++ b/example/README.md @@ -0,0 +1,41 @@ +# VMware Packer Plugin Examples + +This directory contains a set of templates to illustrate how to run the plugin for different versions of VMware products, +as VMware ESXi, VMware Fusion, VMware Player, and VMware Workstation all work slightly differently. + +_Big thanks to @tenthirtyam and @Stromweld for their help in getting these templates working._ + +## Example Directory Structure +The source files are spread across multiple files: + - source.pkr.hcl contains the source block definition for the build image. + - variables.pkr.hcl contains a set of defined variables needed for building the image. + - build.pkr.hcl is the main entry point for Packer to build the VMware image defined in source.pkr.hcl. + - pkrvars/ contains a set of Packer variable definition files (*.pkrvars.hcl) partitioned by guest_os/vmware-version. + +## Running VMware Fusion Examples + > **Note** + > + > VMware Fusion 13 does not support lsilogic adapter types and require additional vmx_data configurations for supporting ARM based builds. + > Below are the adapter types and addition vmx_data configuration options that have been found to work. + > ``` + > vmx_data = { + > "svga.autodetect" = true + > "usb_xhci.present" = true + > } + > cdrom_adapter_type = "sata" + > disk_adapter_type = "sata" + > ``` + > Please refer to the [Fusion 13 pkvars.hcl file](pkrvars/debian/fusion-13.pkrvars.hcl) for exact details. + +**For Fusion 12 builds** +```shell +packer init . +packer build -var-file=pkrvars/debian/fusion-12.pkrvars.hcl . +``` + +**For Fusion 13 builds** +```shell +packer init . +packer build -var-file=pkrvars/debian/fusion-13.pkrvars.hcl . +``` + diff --git a/example/build.pkr.hcl b/example/build.pkr.hcl index b26314f2..b3b9afe1 100644 --- a/example/build.pkr.hcl +++ b/example/build.pkr.hcl @@ -1,21 +1,16 @@ # Copyright (c) HashiCorp, Inc. # SPDX-License-Identifier: MPL-2.0 -source "vmware-iso" "example" { - boot_command = ["", "", "", "/install/vmlinuz", " initrd=/install/initrd.gz", " auto-install/enable=true", " debconf/priority=critical", " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg", " -- ", ""] - boot_wait = "10s" - guest_os_type = "ubuntu-64" - headless = true - http_directory = "${path.root}/http" - iso_checksum = "sha256:946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2" - iso_url = "http://old-releases.ubuntu.com/releases/14.04.1/ubuntu-14.04.1-server-amd64.iso" - shutdown_command = "echo 'vagrant' | sudo -S shutdown -P now" - ssh_password = "vagrant" - ssh_timeout = "10000s" - ssh_username = "vagrant" - tools_upload_flavor = "linux" +packer { + required_version = ">= 1.7.0" + required_plugins { + vmware = { + version = ">= 1.0.7" + source = "github.com/hashicorp/vmware" + } + } } build { - sources = ["source.vmware-iso.example"] + sources = ["source.vmware-iso.debian"] } diff --git a/example/data/debian/preseed.pkrtpl.hcl b/example/data/debian/preseed.pkrtpl.hcl new file mode 100644 index 00000000..53465874 --- /dev/null +++ b/example/data/debian/preseed.pkrtpl.hcl @@ -0,0 +1,104 @@ +#_preseed_V1 +# Automatic installation +d-i auto-install/enable boolean true + +# Preseeding only locale sets language, country and locale. +d-i debian-installer/language string ${vm_guest_os_language} +d-i debian-installer/country string US +d-i debian-installer/locale string en_US.UTF-8 + +d-i console-setup/ask_detect boolean false +d-i debconf/frontend select noninteractive + +# Keyboard selection. +d-i keyboard-configuration/xkb-keymap select ${vm_guest_os_keyboard} +d-i keymap select ${vm_guest_os_keyboard} + +choose-mirror-bin mirror/http/proxy string +d-i apt-setup/use_mirror boolean true +d-i base-installer/kernel/override-image string linux-server + +### Clock and time zone setup +d-i clock-setup/utc boolean true +d-i clock-setup/utc-auto boolean true +d-i time/zone string ${vm_guest_os_timezone} + +# Avoid that last message about the install being complete. +d-i finish-install/reboot_in_progress note + +# This is fairly safe to set, it makes grub install automatically to the MBR +# if no other operating system is detected on the machine. +d-i grub-installer/only_debian boolean true + +# This one makes grub-installer install to the MBR if it also finds some other +# OS, which is less safe as it might not be able to boot that other OS. +d-i grub-installer/with_other_os boolean true + +# Set dev for grub boot +d-i grub-installer/bootdev string /dev/sda + +### Mirror settings +# If you select ftp, the mirror/country string does not need to be set. +d-i mirror/country string manual +d-i mirror/http/directory string /debian/ +d-i mirror/http/hostname string httpredir.debian.org +d-i mirror/http/proxy string + +# This makes partman automatically partition without confirmation. +d-i partman-efi/non_efi_system boolean true +d-i partman-auto-lvm/guided_size string max +d-i partman-auto/choose_recipe select atomic +d-i partman-auto/method string lvm +d-i partman-lvm/confirm boolean true +d-i partman-lvm/confirm_nooverwrite boolean true +d-i partman-lvm/device_remove_lvm boolean true +d-i partman/choose_partition select finish +d-i partman/confirm boolean true +d-i partman/confirm_nooverwrite boolean true +d-i partman/confirm_write_new_label boolean true + +### Account setup +d-i passwd/root-login boolean false +d-i passwd/user-fullname string ${build_username} +d-i passwd/username string ${build_username} +d-i passwd/user-uid string 1000 +d-i passwd/user-password password ${build_password} +d-i passwd/user-password-again password ${build_password} + +# The installer will warn about weak passwords. If you are sure you know +# what you're doing and want to override it, uncomment this. +d-i user-setup/allow-password-weak boolean true +d-i user-setup/encrypt-home boolean false + +### Package selection +tasksel tasksel/first multiselect standard, ssh-server +d-i pkgsel/include string openssh-server sudo bzip2 acpid cryptsetup zlib1g-dev wget curl dkms fuse make nfs-common net-tools cifs-utils rsync +d-i pkgsel/install-language-support boolean false + +# Prevent packaged version of VirtualBox Guest Additions being installed: +d-i preseed/early_command string sed -i \ +'/in-target/idiscover(){/sbin/discover|grep -v VirtualBox;}' \ +/usr/lib/pre-pkgsel.d/20install-hwpackages + +# Do not scan additional CDs +apt-cdrom-setup apt-setup/cdrom/set-first boolean false + +# Use network mirror +apt-mirror-setup apt-setup/use_mirror boolean true + +# disable automatic package updates +d-i pkgsel/update-policy select none +d-i pkgsel/upgrade select full-upgrade + +# Disable polularity contest +popularity-contest popularity-contest/participate boolean false + +# Select base install +tasksel tasksel/first multiselect standard, ssh-server + +# Setup passwordless sudo for packer user +d-i preseed/late_command string \ +echo "vagrant ALL=(ALL:ALL) NOPASSWD:ALL" > /target/etc/sudoers.d/vagrant && chmod 0440 /target/etc/sudoers.d/vagrant + +# remove cdrom from apt sources +d-i preseed/late_command string sed -i '/^deb cdrom:/s/^/#/' /target/etc/apt/sources.list diff --git a/example/http/preseed.cfg b/example/http/preseed.cfg deleted file mode 100644 index 559fb9f7..00000000 --- a/example/http/preseed.cfg +++ /dev/null @@ -1,42 +0,0 @@ -# Preseeding only locale sets language, country and locale. -d-i debian-installer/locale string en_US - -# Keyboard selection. -d-i console-setup/ask_detect boolean false -d-i keyboard-configuration/xkb-keymap select us - -choose-mirror-bin mirror/http/proxy string -d-i base-installer/kernel/override-image string linux-server -d-i clock-setup/utc boolean true -d-i clock-setup/utc-auto boolean true -d-i finish-install/reboot_in_progress note -d-i grub-installer/only_debian boolean true -d-i grub-installer/with_other_os boolean true -d-i mirror/country string manual -d-i mirror/http/directory string /ubuntu/ -d-i mirror/http/hostname string archive.ubuntu.com -d-i mirror/http/proxy string -d-i partman-auto-lvm/guided_size string max -d-i partman-auto/choose_recipe select atomic -d-i partman-auto/method string lvm -d-i partman-lvm/confirm boolean true -d-i partman-lvm/confirm boolean true -d-i partman-lvm/confirm_nooverwrite boolean true -d-i partman-lvm/device_remove_lvm boolean true -d-i partman/choose_partition select finish -d-i partman/confirm boolean true -d-i partman/confirm_nooverwrite boolean true -d-i partman/confirm_write_new_label boolean true -d-i passwd/user-fullname string vagrant -d-i passwd/user-uid string 1000 -d-i passwd/user-password password vagrant -d-i passwd/user-password-again password vagrant -d-i passwd/username string vagrant -d-i pkgsel/include string openssh-server cryptsetup build-essential libssl-dev libreadline-dev zlib1g-dev linux-source dkms nfs-kernel-server nfs-common linux-headers-$(uname -r) perl -d-i pkgsel/install-language-support boolean false -d-i pkgsel/update-policy select none -d-i pkgsel/upgrade select full-upgrade -d-i time/zone string UTC -d-i user-setup/allow-password-weak boolean true -d-i user-setup/encrypt-home boolean false -tasksel tasksel/first multiselect standard, server \ No newline at end of file diff --git a/example/pkrvars/debian/fusion-12.pkrvars.hcl b/example/pkrvars/debian/fusion-12.pkrvars.hcl new file mode 100644 index 00000000..fd1a5659 --- /dev/null +++ b/example/pkrvars/debian/fusion-12.pkrvars.hcl @@ -0,0 +1,8 @@ +iso_url = "https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-11.6.0-amd64-DVD-1.iso" +iso_checksum = "55f6f49b32d3797621297a9481a6cc3e21b3142f57d8e1279412ff5a267868d8" +data_directory = "data/debian" +guest_os_type = "debian-64" +hardware_version = 19 +boot_command = ["auto preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg netcfg/get_hostname={{ .Name }}"] +vm_name = "debian_x86_64" + diff --git a/example/pkrvars/debian/fusion-13.pkrvars.hcl b/example/pkrvars/debian/fusion-13.pkrvars.hcl new file mode 100644 index 00000000..76aa99a6 --- /dev/null +++ b/example/pkrvars/debian/fusion-13.pkrvars.hcl @@ -0,0 +1,18 @@ +cdrom_adapter_type = "sata" +data_directory = "data/debian" +disk_adapter_type = "sata" +network_adapter_type = "e1000e" +iso_url = "https://cdimage.debian.org/debian-cd/current/arm64/iso-dvd/debian-11.6.0-arm64-DVD-1.iso" +iso_checksum = "b27ff768c10808518790d72d670c5588cdc60cf8934ef92773a89274a193a65f" +guest_os_type = "arm-debian-64" +hardware_version = 20 +boot_command = ["einstall preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg debian-installer=en_US.UTF-8 auto locale=en_US.UTF-8 kbd-chooser/method=us keyboard-configuration/xkb-keymap=us netcfg/get_hostname={{ .Name }} netcfg/get_domain={{ .Name }} fb=false debconf/frontend=noninteractive console-setup/ask_detect=false console-keymaps-at/keymap=us grub-installer/bootdev=/dev/sda "] +vm_name = "debian_aarch64" +vmx_data = { + "cpuid.coresPerSocket" = "2" + "ethernet0.pciSlotNumber" = "32" + "svga.autodetect" = true + "usb_xhci.present" = true +} + + diff --git a/example/sources.pkr.hcl b/example/sources.pkr.hcl new file mode 100644 index 00000000..b3020a72 --- /dev/null +++ b/example/sources.pkr.hcl @@ -0,0 +1,40 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +locals { + data_directory = var.data_directory == null ? "data" : var.data_directory + memory = var.memory == null ? 2048 : var.memory +} + +source "vmware-iso" "debian" { + boot_command = var.boot_command + boot_wait = "10s" + cpus = 2 + cdrom_adapter_type = var.cdrom_adapter_type + disk_adapter_type = var.disk_adapter_type + guest_os_type = var.guest_os_type + headless = var.vm_headless + http_content = { # Use http_content template to dynamic configure preseed - https://www.hashicorp.com/blog/using-template-files-with-hashicorp-packer + "/preseed.cfg" = templatefile("${abspath(path.root)}/${local.data_directory}/preseed.pkrtpl.hcl", { + build_username = var.build_username + build_password = var.build_password + vm_guest_os_language = var.vm_guest_os_language + vm_guest_os_keyboard = var.vm_guest_os_keyboard + vm_guest_os_timezone = var.vm_guest_os_timezone + }) + } + iso_checksum = var.iso_checksum + iso_url = var.iso_url + memory = local.memory + network_adapter_type = var.network_adapter_type + output_directory = "builds/${var.vm_name}" + shutdown_command = "echo 'vagrant' | sudo -S shutdown -P now" + ssh_password = "vagrant" + ssh_timeout = "10000s" + ssh_username = "vagrant" + tools_upload_flavor = var.tools_upload_flavor + tools_upload_path = var.tools_upload_path + version = var.hardware_version + vmx_data = var.vmx_data +} + diff --git a/example/variables.pkr.hcl b/example/variables.pkr.hcl new file mode 100644 index 00000000..11b78a3e --- /dev/null +++ b/example/variables.pkr.hcl @@ -0,0 +1,110 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +variable "boot_command" { + type = list(string) + default = ["", "", "", "/install/vmlinuz", " initrd=/install/initrd.gz", " auto-install/enable=true", " debconf/priority=critical", " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg", " -- ", ""] +} + +variable "build_username" { + type = string + default = "vagrant" +} + +variable "build_password" { + type = string + default = "vagrant" +} + +variable "cdrom_adapter_type" { + type = string + default = "sata" +} + +variable "data_directory" { + type = string + default = "null" +} + +variable "disk_size" { + type = number + default = 65536 +} + +variable "disk_adapter_type" { + type = string + default = "lsilogic" +} + +variable "guest_os_type" { + type = string + default = null +} + +variable "hardware_version" { + type = number + default = 19 # Refer to VMware versions https://kb.vmware.com/s/article/1003746 +} + +variable "iso_checksum" { + type = string + default = null +} + +variable "iso_url" { + type = string + default = null +} + +variable "memory" { + type = number + default = null +} + +variable "network_adapter_type" { + type = string + default = null +} + +variable "tools_upload_flavor" { + type = string + default = null +} + +variable "tools_upload_path" { + type = string + default = null +} + +variable "vm_name" { + type = string + default = "debian" +} + +variable "vmx_data" { + type = map(string) + default = { + "cpuid.coresPerSocket" = "2" + } +} + +variable "vm_guest_os_language" { + type = string + default = "en" +} + +variable "vm_guest_os_keyboard" { + type = string + default = "us" +} + +variable "vm_guest_os_timezone" { + type = string + default = "UTC" +} + +variable "vm_headless" { + type = bool + default = true +} +