1

I am a beginner to Packer. I am using a template found at https://github.com/StefanScherer/packer-windows for Windows 10, and have been able to create a virtual machine in Hyper-V using the template and my own .ISO.

Now I want to take the next step and add some of my own PowerShell scripts to the process, but I notice this template has multiple references to 'floppy_files' at various parts of the template, yet they are the same script files repeated? Can someone explain why? Do I need to put my script in each floppy_files collection? I'd rather understand why before I do that even if it does happen to work. Template shown below for clarity.

{
  "builders": [
    {
      "accelerator": "kvm",
      "boot_wait": "6m",
      "communicator": "winrm",
      "cpus": "2",
      "disk_size": "{{user `disk_size`}}",
      "floppy_files": [
        "{{user `autounattend`}}",
        "./floppy/WindowsPowershell.lnk",
        "./floppy/PinTo10.exe",
        "./scripts/fixnetwork.ps1",
        "./scripts/disable-screensaver.ps1",
        "./scripts/disable-winrm.ps1",
        "./scripts/enable-winrm.ps1",
        "./scripts/microsoft-updates.bat",
        "./scripts/win-updates.ps1"
      ],
      "headless": true,
      "iso_checksum": "{{user `iso_checksum`}}",
      "iso_url": "{{user `iso_url`}}",      
      "memory": "{{user `memory`}}",
      "output_directory": "windows_10-qemu",
      "qemuargs": [
        [
          "-drive",
          "file=windows_10-qemu/{{ .Name }},if=virtio,cache=writeback,discard=ignore,format=qcow2,index=1"
        ],
        [
          "-drive",
          "file={{ user `iso_url` }},media=cdrom,index=2"
        ],
        [
          "-drive",
          "file={{ user `virtio_win_iso` }},media=cdrom,index=3"
        ]
      ],
      "shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
      "type": "qemu",
      "vm_name": "{{user `vm_name`}}",
      "winrm_password": "vagrant",
      "winrm_timeout": "{{user `winrm_timeout`}}",
      "winrm_username": "vagrant"
    },
    {
      "boot_wait": "6m",
      "communicator": "winrm",
      "configuration_version": "8.0",
      "cpus": "2",
      "disk_size": "{{user `disk_size`}}",
      "floppy_files": [
        "{{user `autounattend`}}",
        "./floppy/WindowsPowershell.lnk",
        "./floppy/PinTo10.exe",
        "./scripts/fixnetwork.ps1",
        "./scripts/disable-screensaver.ps1",
        "./scripts/disable-winrm.ps1",
        "./scripts/enable-winrm.ps1",
        "./scripts/microsoft-updates.bat",
        "./scripts/win-updates.ps1"
      ],
      "guest_additions_mode": "none",
      "iso_checksum": "{{user `iso_checksum`}}",
      "iso_url": "{{user `iso_url`}}",
      "memory": "{{user `memory`}}",
      "shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
      "switch_name": "{{user `switch_name`}}",
      "type": "hyperv-iso",
      "vm_name": "{{user `vm_name`}}",
      "winrm_password": "vagrant",
      "winrm_timeout": "{{user `winrm_timeout`}}",
      "winrm_username": "vagrant"
    },
    {
      "boot_command": "",
      "boot_wait": "6m",
      "communicator": "winrm",
      "cpus": 2,
      "disk_adapter_type": "lsisas1068",
      "disk_size": "{{user `disk_size`}}",
      "disk_type_id": "{{user `disk_type_id`}}",
      "floppy_files": [
        "{{user `autounattend`}}",
        "./floppy/WindowsPowershell.lnk",
        "./floppy/PinTo10.exe",
        "./scripts/fixnetwork.ps1",
        "./scripts/disable-screensaver.ps1",
        "./scripts/disable-winrm.ps1",
        "./scripts/enable-winrm.ps1",
        "./scripts/microsoft-updates.bat",
        "./scripts/win-updates.ps1"
      ],
      "guest_os_type": "windows9-64",
      "headless": "{{user `headless`}}",
      "iso_checksum": "{{user `iso_checksum`}}",
      "iso_url": "{{user `iso_url`}}",
      "memory": "{{user `memory`}}",
      "shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
      "type": "vmware-iso",
      "version": "{{user `vmx_version`}}",
      "vm_name": "{{user `vm_name`}}",
      "vmx_data": {
        "RemoteDisplay.vnc.enabled": "false",
        "RemoteDisplay.vnc.port": "5900"
      },
      "vmx_remove_ethernet_interfaces": true,
      "vnc_port_max": 5980,
      "vnc_port_min": 5900,
      "winrm_password": "vagrant",
      "winrm_timeout": "{{user `winrm_timeout`}}",
      "winrm_username": "vagrant"
    },
    {
      "boot_command": "",
      "boot_wait": "6m",
      "communicator": "winrm",
      "cpus": 2,
      "disk_size": "{{user `disk_size`}}",
      "floppy_files": [
        "{{user `autounattend`}}",
        "./floppy/WindowsPowershell.lnk",
        "./floppy/PinTo10.exe",
        "./scripts/fixnetwork.ps1",
        "./scripts/disable-screensaver.ps1",
        "./scripts/disable-winrm.ps1",
        "./scripts/enable-winrm.ps1",
        "./scripts/microsoft-updates.bat",
        "./scripts/win-updates.ps1"
      ],
      "guest_additions_mode": "disable",
      "guest_os_type": "Windows10_64",
      "headless": "{{user `headless`}}",
      "iso_checksum": "{{user `iso_checksum`}}",
      "iso_url": "{{user `iso_url`}}",
      "memory": "{{user `memory`}}",
      "shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
      "type": "virtualbox-iso",
      "vm_name": "{{user `vm_name`}}",
      "winrm_password": "vagrant",
      "winrm_timeout": "{{user `winrm_timeout`}}",
      "winrm_username": "vagrant"
    },
    {
      "boot_command": "",
      "boot_wait": "6m",
      "communicator": "winrm",
      "cpus": 2,
      "disk_size": "{{user `disk_size`}}",
      "floppy_files": [
        "{{user `autounattend`}}",
        "./floppy/WindowsPowershell.lnk",
        "./floppy/PinTo10.exe",
        "./scripts/fixnetwork.ps1",
        "./scripts/disable-screensaver.ps1",
        "./scripts/disable-winrm.ps1",
        "./scripts/enable-winrm.ps1",
        "./scripts/microsoft-updates.bat",
        "./scripts/win-updates.ps1"
      ],
      "guest_os_type": "win-10",
      "iso_checksum": "{{user `iso_checksum`}}",
      "iso_url": "{{user `iso_url`}}",
      "memory": "{{user `memory`}}",
      "shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
      "type": "parallels-iso",
      "parallels_tools_flavor": "win",
      "parallels_tools_mode": "disable",
      "prlctl": [
        [
          "set",
          "{{.Name}}",
          "--adaptive-hypervisor",
          "on"
        ],
        [
          "set",
          "{{.Name}}",
          "--efi-boot",
          "off"
        ]
      ],
      "vm_name": "{{user `vm_name`}}",
      "winrm_password": "vagrant",
      "winrm_timeout": "{{user `winrm_timeout`}}",
      "winrm_username": "vagrant"
    }
  ],
  "post-processors": [
    {
      "keep_input_artifact": false,
      "output": "windows_10_{{.Provider}}.box",
      "type": "vagrant",
      "vagrantfile_template": "vagrantfile-windows_10.template"
    }
  ],
  "provisioners": [
    {
      "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"",
      "remote_path": "/tmp/script.bat",
      "scripts": [
        "./scripts/enable-rdp.bat"
      ],
      "type": "windows-shell"
    },
    {
      "scripts": [
        "./scripts/vm-guest-tools.ps1",
        "./scripts/debloat-windows.ps1"
      ],
      "type": "powershell"
    },
    {
      "restart_timeout": "{{user `restart_timeout`}}",
      "type": "windows-restart"
    },
    {
      "scripts": [
        "./scripts/set-powerplan.ps1",
        "./scripts/docker/disable-windows-defender.ps1"
      ],
      "type": "powershell"
    },
    {
      "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"",
      "remote_path": "/tmp/script.bat",
      "scripts": [
        "./scripts/pin-powershell.bat",
        "./scripts/compile-dotnet-assemblies.bat",
        "./scripts/set-winrm-automatic.bat",
        "./scripts/uac-enable.bat",
        "./scripts/dis-updates.bat",
        "./scripts/compact.bat"
      ],
      "type": "windows-shell"
    }
  ],
  "variables": {
    "autounattend": "./answer_files/10/Autounattend.xml",
    "disk_size": "61440",
    "disk_type_id": "1",
    "memory": "2048",
    "headless": "false",
    "iso_checksum": "sha256:668fe1af70c2f7416328aee3a0bb066b12dc6bbd2576f40f812b95741e18bc3a",
    "iso_url": "https://software-download.microsoft.com/download/sg/17763.107.101029-1455.rs5_release_svc_refresh_CLIENT_LTSC_EVAL_x64FRE_en-us.iso",
    "restart_timeout": "5m",
    "vhv_enable": "false",
    "virtio_win_iso": "~/virtio-win.iso",
    "vm_name": "windows_10_pos",
    "winrm_timeout": "6h",
    "vmx_version": "14"
  }
}

1 Answer 1

1

You're looking at a list of different builders. there can be multiple builder entries, and they're indicated by a certain 'type' - see the field named type.

In the example from Stefan there are qemu, hyperv-iso, vmware-iso, virtualbox-iso and finally parallels-iso.

Each of them contains

  • a full set of parameters for building a VM "artifact"
  • the right parameters for that respective hypervisor.
  • the scripts to install the specific guest tools
  • the scripts for shared tasks they all need, i.e. the automatic update scripts.

An artifact here is an installed VM in the format of this specific hypervisor / an iso of that / something.

And so that's why you can have some duplication, and as such, that's why you probably only need to copy one builder section .

1
  • Thank you Florian, I ended up figuring this out but forgot to come back here! Credit to you though, appreciated. In the end it turns out I just needed hyperv-iso. Now I have to figure out how to get the VM I've created with all my updates on it, back out to something bootable onto a new PC/server. :) Apr 10, 2021 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.