From d3dbf7e0ac075fb55f0bd0295ec672a2eb711e02 Mon Sep 17 00:00:00 2001 From: Jarred Wilson Date: Tue, 17 Oct 2023 08:02:59 -0400 Subject: [PATCH] Add windows server 2022 support --- .gitmodules | 9 +++ windows/Makefile | 23 ++++++ windows/cloudbase-init | 1 + windows/packer-windows | 1 + windows/post.sh | 11 +++ windows/windows-curtin-hooks | 1 + windows/windows2022.json.pkr.hcl | 118 +++++++++++++++++++++++++++++++ 7 files changed, 164 insertions(+) create mode 100644 .gitmodules create mode 100644 windows/Makefile create mode 160000 windows/cloudbase-init create mode 160000 windows/packer-windows create mode 100644 windows/post.sh create mode 160000 windows/windows-curtin-hooks create mode 100644 windows/windows2022.json.pkr.hcl diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..d12cb171 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "windows/cloudbase-init"] + path = windows/cloudbase-init + url = https://github.com/cloudbase/cloudbase-init.git +[submodule "windows/windows-curtin-hooks"] + path = windows/windows-curtin-hooks + url = https://github.com/cloudbase/windows-curtin-hooks.git +[submodule "windows/packer-windows"] + path = windows/packer-windows + url = https://github.com/StefanScherer/packer-windows.git diff --git a/windows/Makefile b/windows/Makefile new file mode 100644 index 00000000..eab00244 --- /dev/null +++ b/windows/Makefile @@ -0,0 +1,23 @@ +PACKER ?= packer +ISO = ${ISO} +VERSION = ${VERSION} + +.PHONY: all init clean ${VERSION} + +all: windows-server-${VERSION}.dd.gz + +virtio-win.iso: + @if [ ! -f ./virtio-win.iso ]; then\ + echo "Fetching virtio-win ISO...";\ + wget -nv -nc https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso -O virtio-win.iso;\ + fi + +init: + sudo packer init windows${VERSION}.json.pkr.hcl + +windows-server-${VERSION}.dd.gz: clean virtio-win.iso init + sudo PACKER_LOG=1 ${PACKER} build --var iso_url=${ISO} --var virtio_win_iso=./virtio-win.iso windows${VERSION}.json.pkr.hcl + reset + +clean: + sudo ${RM} -rf output-windows_${VERSION} windows-server-${VERSION}.dd.gz \ No newline at end of file diff --git a/windows/cloudbase-init b/windows/cloudbase-init new file mode 160000 index 00000000..36ed9368 --- /dev/null +++ b/windows/cloudbase-init @@ -0,0 +1 @@ +Subproject commit 36ed9368032b79c28eb7eaf1f301f9a3daf0b82a diff --git a/windows/packer-windows b/windows/packer-windows new file mode 160000 index 00000000..794ff55d --- /dev/null +++ b/windows/packer-windows @@ -0,0 +1 @@ +Subproject commit 794ff55d94ece085aafc4d1f86281479644a7f62 diff --git a/windows/post.sh b/windows/post.sh new file mode 100644 index 00000000..2a23b68b --- /dev/null +++ b/windows/post.sh @@ -0,0 +1,11 @@ +#!/bin/sh -x + +echo 'Adding curtin-hooks to image...' +mkdir -p $TMP_DIR/curtin +cp windows-curtin-hooks/curtin/* $TMP_DIR/curtin/ +sync -f $TMP_DIR/curtin + +echo 'Adding cloudbase to image...' +mkdir -p $TMP_DIR/'Program Files'/'Cloudbase Solutions'/Cloudbase-Init +cp -r ./cloudbase-init/* $TMP_DIR/'Program Files'/'Cloudbase Solutions'/Cloudbase-Init/ +sync -f $TMP_DIR/'Program Files' \ No newline at end of file diff --git a/windows/windows-curtin-hooks b/windows/windows-curtin-hooks new file mode 160000 index 00000000..bb30d56c --- /dev/null +++ b/windows/windows-curtin-hooks @@ -0,0 +1 @@ +Subproject commit bb30d56c432f1907d098d71d9d4f2fc6ffe8e192 diff --git a/windows/windows2022.json.pkr.hcl b/windows/windows2022.json.pkr.hcl new file mode 100644 index 00000000..cd25a7d6 --- /dev/null +++ b/windows/windows2022.json.pkr.hcl @@ -0,0 +1,118 @@ +packer { + required_plugins { + qemu = { + source = "github.com/hashicorp/qemu" + version = "~> 1" + } + } +} + +variable "autounattend" { + type = string + default = "./packer-windows/answer_files/2022/Autounattend.xml" +} + +variable "disk_size" { + type = string + default = "61440" +} + +variable "disk_type_id" { + type = string + default = "1" +} + +variable "headless" { + type = string + default = "false" +} + +variable "iso_checksum" { + type = string + default = "sha256:3e4fa6d8507b554856fc9ca6079cc402df11a8b79344871669f0251535255325" +} + +variable "iso_url" { + type = string + default = "https://software-static.download.prss.microsoft.com/sg/download/888969d5-f34g-4e03-ac9d-1f9786c66749/SERVER_EVAL_x64FRE_en-us.iso" +} + +variable "manually_download_iso_from" { + type = string + default = "https://www.microsoft.com/en-us/evalcenter/evaluate-windows-server-2022" +} + +variable "memory" { + type = string + default = "2048" +} + +variable "restart_timeout" { + type = string + default = "5m" +} + +variable "virtio_win_iso" { + type = string + default = "./virtio-win.iso" +} + +variable "winrm_timeout" { + type = string + default = "2h" +} + +source "qemu" "autogenerated_1" { + accelerator = "kvm" + boot_wait = "0s" + communicator = "winrm" + cpus = 2 + disk_size = "${var.disk_size}" + floppy_files = ["${var.autounattend}", "./packer-windows/scripts/disable-screensaver.ps1", "./packer-windows/scripts/disable-winrm.ps1", "./packer-windows/scripts/enable-winrm.ps1", "./packer-windows/scripts/microsoft-updates.bat", "./packer-windows/scripts/unattend.xml", "./packer-windows/scripts/sysprep.bat", "./packer-windows/scripts/win-updates.ps1"] + headless = true + iso_checksum = "${var.iso_checksum}" + iso_url = "${var.iso_url}" + memory = "${var.memory}" + output_directory = "output-windows_server-2022" + qemuargs = [["-drive", "file=output-windows_server-2022/{{ .Name }},if=virtio,cache=writeback,discard=ignore,format=qcow2,index=1"], ["-drive", "file=${var.iso_url},media=cdrom,index=2"], ["-drive", "file=${var.virtio_win_iso},media=cdrom,index=3"]] + shutdown_command = "a:/sysprep.bat" + vm_name = "packer-windows_server-2022" + winrm_password = "vagrant" + winrm_timeout = "${var.winrm_timeout}" + winrm_username = "vagrant" +} + +build { + sources = ["source.qemu.autogenerated_1"] + + provisioner "windows-shell" { + execute_command = "{{ .Vars }} cmd /c \"{{ .Path }}\"" + scripts = ["./packer-windows/scripts/enable-rdp.bat"] + } + + provisioner "powershell" { + scripts = ["./packer-windows/scripts/vm-guest-tools.ps1", "./packer-windows/scripts/debloat-windows.ps1"] + } + + provisioner "windows-restart" { + restart_timeout = "${var.restart_timeout}" + } + + provisioner "windows-shell" { + execute_command = "{{ .Vars }} cmd /c \"{{ .Path }}\"" + scripts = ["./packer-windows/scripts/pin-powershell.bat", "./packer-windows/scripts/set-winrm-automatic.bat", "./packer-windows/scripts/uac-enable.bat", "./packer-windows/scripts/compile-dotnet-assemblies.bat", "./packer-windows/scripts/dis-updates.bat", "./packer-windows/scripts/compact.bat"] + } + + post-processor "shell-local" { + inline = [ + "SOURCE=windows_server-2022", + "IMG_FMT=raw", + "source ../scripts/fuse-nbd", + "source ./post.sh", + ] + inline_shebang = "/bin/bash -e" + } + post-processor "compress" { + output = "windows-server-2022.dd.gz" + } +}