packer-plugin-qemu icon indicating copy to clipboard operation
packer-plugin-qemu copied to clipboard

Additional Resources for QEMU builder [ file download datasource ?]

Open hc-github-team-packer opened this issue 3 years ago • 2 comments

This issue was originally opened by @m-ildefons in https://github.com/hashicorp/packer/issues/11184 and has been migrated to this repository. The original issue description is below.


Description

Dear Packer Maintainers,

In my particular use case of the QMEU/KVM builder, I think there is a feature missing. When installing Windows as described by the examples linked from this page in the documentation using QEMU/KVM, one needs to mount an additional ISO image to provide the virtio drivers to Windows (https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso). I see no way currently to have Packer obtain and checksum that ISO in a similar way to the installer ISO image. Once the image is obtained the installation works fine. With the current state of affairs, that ISO image containing the drivers must be obtained manually before a build can be started.

What I'd like to be able to do is something like this and have Packer obtain this additional ISO file for me:

{
  "builders": [
    {
      "type": "qemu"
      ...
      "additional_resources": [
        ["file:https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso", "md5:XXXXXXXXXXXXXXX"]
      ]
      "qemuargs": [
        ["-drive", "file=virtio-win.iso,media=cdrom,index=3"]
      ]
      ...
    }
  ]
}

This could also be achieved with a pre-processor, although I understand that no pre-processor is planned at this stage.

Use Case(s)

Obtaining additional resources and providing those to a QEMU based build. In particular:

  • obtaining and providing additional drivers while building Windows images with QEMU/KVM builder.
  • obtaining Providing custom UEFI/BIOS images to QEMU

Potential configuration

{
  "builders": [
    {
      "communicator": "winrm",
      "cpus": "{{ user `cpus` }}",
      "disk_size": "{{user `disk_size`}}",
      "floppy_files": [
        "{{ user `floppy_dir` }}/{{ user `unattended_file_path` }}"
      ],
      "headless": "{{ user `headless` }}",
      "iso_checksum": "{{ user `iso_checksum` }}",
      "iso_url": "{{ user `iso_url` }}",
      "memory": "{{ user `memory` }}",
      "output_directory": "{{ user `build_directory` }}/packer-{{user `template`}}-qemu",
      "qemuargs": [
        [
          "-m",
          "{{ user `memory` }}"
        ],
        [
          "-smp",
          "{{ user `cpus` }}"
        ],
        [
          "-drive",
          "file={{ user `virtio_win_iso` }},media=cdrom,index=3"
        ],
        [
          "-drive",
          "file={{ user `build_directory` }}/packer-{{ user `template` }}-qemu/{{ .Name }},if=virtio,cache=writeback,discard=ignore,format=qcow2,index=1"
        ]
      ],
      "shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
      "shutdown_timeout": "15m",
      "type": "qemu",
      "winrm_password": "vagrant",
      "winrm_timeout": "12h",
      "winrm_username": "vagrant"
    }
  ],
  "post-processors": [
    [
      {
        "keep_input_artifact": true,
        "output": "{{ user `template` }}-{{.Provider}}.box",
        "type": "vagrant",
        "vagrantfile_template": "{{template_dir}}/vagrantfile-windows.template"
      }
    ]
  ],
  "provisioners": [
    {
      "cookbook_paths": [
        "{{template_dir}}/cookbooks"
      ],
      "guest_os_type": "windows",
      "run_list": [
        "packer::disable_uac",
        "packer::disable_restore",
        "packer::disable_windows_update",
        "packer::remove_defender",
        "packer::configure_power",
        "packer::disable_screensaver"
      ],
      "type": "chef-solo"
    },
    {
      "type": "windows-restart"
    },
    {
      "cookbook_paths": [
        "{{template_dir}}/cookbooks"
      ],
      "guest_os_type": "windows",
      "run_list": [
        "packer::vm_tools",
        "packer::features",
        "packer::enable_file_sharing",
        "packer::enable_remote_desktop",
        "packer::ui_tweaks"
      ],
      "type": "chef-solo"
    },
    {
      "type": "windows-restart"
    },
    {
      "cookbook_paths": [
        "{{template_dir}}/cookbooks"
      ],
      "guest_os_type": "windows",
      "run_list": [
        "packer::cleanup",
        "packer::defrag"
      ],
      "type": "chef-solo"
    },
    {
      "elevated_password": "vagrant",
      "elevated_user": "vagrant",
      "script": "{{template_dir}}/scripts/cleanup.ps1",
      "type": "powershell"
    }
  ],
  "variables": {
    "build_directory": "../../builds",
    "cpus": "2",
    "disk_size": "40000",
    "floppy_dir": "{{template_dir}}/answer_files",
    "guest_additions_mode": "attach",
    "guest_additions_url": "",
    "headless": "true",
    "hyperv_switch": "bento",
    "iso_checksum": "3022424f777b66a698047ba1c37812026b9714c5",
    "iso_url": "https://software-download.microsoft.com/download/pr/17763.737.190906-2324.rs5_release_svc_refresh_SERVER_EVAL_x64FRE_en-us_1.iso",
    "memory": "4096",
    "template": "windows-2019-standard",
    "unattended_file_path": "2019/Autounattend.xml",
    "virtio_win_iso": "~/virtio-win.iso"
  }
}

Potential References

Unattended Windows installation with Packer: https://www.packer.io/guides/automatic-operating-system-installs/autounattend_windows Example builder: https://github.com/chef/bento/blob/master/packer_templates/windows/windows-2019.json Example autounattend.xml: https://github.com/chef/bento/blob/master/packer_templates/windows/answer_files/2019/Autounattend.xml Documentation on virtio-drivers for Windows virtual machines: https://docs.fedoraproject.org/en-US/quick-docs/creating-windows-virtual-machines-using-virtio-drivers/index.html#virtio-win-direct-downloads

hc-github-team-packer avatar Aug 04 '21 08:08 hc-github-team-packer

Just chiming in here and, maybe this could be a datasource:

data "download" "virtio-iso" {
  urls     = ["possible", "iso", "urls"]
  checksum = "xyz"
}

This could maybe download a file in packer's cache, checksum it and give a data.cache.virtio-iso.local_path to use.

azr avatar Aug 18 '21 13:08 azr

Just want to say such datasource would be really useful in another case I hit just recently.. I'm trying to build an vagrant box and I want to download the insecure vagrant key pair to store the public key in a VM using the cloud-init.. at the same time I'd like to use the private key in a builder (ssh_private_key_file). I can get the private key content with the http datasource. But there is no way to store it in a file BEFORE parsing the source definition.

Another way is to extend the http datasource to allow the body checksumming and storing it in a file.

jay7x avatar Dec 04 '22 15:12 jay7x