Build Hyper-V VM with hyperv-iso from Ubuntu

I’m trying to build a Hyper-V VM using hyperv-iso builder, from Ubuntu. I have a Hyper-V server in the cloud with SSH credentials, but when I try to build, I just get this error:

sgd-hyperv: output will be in this color.

Build 'sgd-hyperv' errored: Failed creating Hyper-V driver: Applies to Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2 only

==> Some builds didn't complete successfully and had errors:
--> sgd-hyperv: Failed creating Hyper-V driver: Applies to Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2 only

==> Builds finished but no artifacts were created.

I’m able to successfully build VMWare and Virtualbox images with packer.

Is it just not possible to build Hyper-V images from linux with packer? There’s nothing in the docs saying that?

My config for this build:

{
      "boot_command": [
        "<enter><wait>",
        "<f6><esc>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs>",
        "/install/vmlinuz ",
        "passwd/root-password-again\"{{user `root_password`}}\" ",
        "passwd/root-password=\"{{user `root_password`}}\" ",
        "passwd/user-fullname=\"{{user `ssh_user`}}\" ",
        "passwd/username=\"{{user `ssh_user`}}\" ",
        "passwd/user-password=\"{{user `ssh_user_password`}}\" ",
        "passwd/user-password-again=\"{{user `ssh_user_password`}}\" ",
        "user-setup/allow-password-weak boolean true ",
        "initrd=/install/initrd.gz ",
        "net.ifnames=0 ",
        "auto-install/enable=true ",
        "debconf/priority=critical ",
        "preseed/file=/floppy/preseed-supervisor.cfg ",
        "<enter>"
      ],
      "boot_wait": "30s",
      "disk_size": "{{user `disk_size`}}",
      "floppy_files": [
        "http/ubuntu-16.04/preseed-supervisor.cfg"
      ],
      "headless": "{{user `headless`}}",
      "http_directory": "http",
      "iso_checksum": "{{user `iso_checksum_type`}}:{{user `iso_checksum`}}",
      "iso_url": "{{user `mirror`}}/{{user `mirror_directory`}}/{{user `iso_name`}}",
      "keep_registered": "{{user `esxi_keep_registered`}}",
      "name": "sgd-hyperv",
      "output_directory": "output-ubuntu-16.04.2_64-{{build_type}}",
      "shutdown_command": "echo '{{ user `ssh_user_password` }}' | sudo -S /sbin/shutdown -P now",
      "shutdown_timeout": "20m",
      "ssh_host": "<redacted>",
      "ssh_password": "<redacted>",
      "ssh_timeout": "60m",
      "ssh_username": "<redacted>",
      "type": "hyperv-iso",
      "vm_name": "supervisor-{{user `supervisor_build_string` }}-{{timestamp}}"
    }

any help would be greatly appreciated!

I don’t think you can use Packer to build on a remote machine. You need to run it on the same machine where hyper-v is installed.

I have the same error but when attempting to run packer from WSL2 (Ubuntu) against my local hyper-v install.

Here’s my use-case:

  1. I want to build locally what will eventually be built in my CI/CD pipeline.
  2. When packer is run from Linux (WSL in the local case) or CI/CD otherwise, I want to produce a set of Ubuntu virtual machine images (Hyper-V, AWS AMI, Vagrantbox).
  3. I want to use as few technologies to do this as possible.

The error I get when running Hyper-V (locally) from WSL against my local Hyper-V install is this:
Build ‘hyperv-iso.vm’ errored after 545 microseconds: Failed creating Hyper-V driver: Applies to Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2 only

==> Wait completed after 569 microseconds

==> Some builds didn’t complete successfully and had errors:
→ hyperv-iso.vm: Failed creating Hyper-V driver: Applies to Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2 only

The problem is in builder/hyperv/common/driver_ps_4.go at line 22 (in function NewHypervPS4Driver()).

Line 22 specifically requires that the hyperv build occur in Windows:

if runtime.GOOS != "windows" { ...

No fix exists at this time. But I will update this if I can devise a workaround.