GithubHelp home page GithubHelp logo

marcinbojko / hv-packer Goto Github PK

View Code? Open in Web Editor NEW
178.0 17.0 70.0 4.42 MB

Hyper-V Generation 2 Packer Templates for Windows 2016/2019/2022, CentOS 7x/AlmaLinux 8.x/AlmaLinux 9.x/RockyLinux 8.x/9.x/OracleLinux 8.x/9.x Ubuntu 20.04/Ubuntu 22.04

Shell 35.29% Batchfile 0.50% PowerShell 36.66% Ruby 6.97% HCL 20.58%
packer powershell hyper-v windows linux almalinux centos rockylinux vagrant oracle

hv-packer's Introduction

Set of Hashicorp's Packer templates to create Microsoft Hyper-V virtual machines

RockyLinux OracleLinux AlmaLinux UbuntuLinux Windows2019 Windows2022

"Buy Me A Coffee"

Consider buying me a coffee if you like my work. All donations are appreciated. All donations will be used to pay for pipeline running costs

Requirements

  • packer <=1.9.1. Do not use packer below 1.7.0 version. For previous packer versions use previous releases from this repository
  • Microsoft Hyper-V Server 2016/2019 or Microsoft Windows Server 2016/2019 (not 2012/R2) with Hyper-V role installed as host to build your images
  • firewall exceptions for packer http server (look down below)
  • [OPTIONAL] Vagrant >= 2.3.4 - for vagrant version of scripts. Boxes (prebuilt) are already available here: https://app.vagrantup.com/marcinbojko
  • be aware, for 2016 - VMs are in version 8.0, for 2019 - VMs are in version 9.0. There is no way to reuse higher version in previous operating system. If you need v8.0 - build and use only VHDX.
  • properly constructed virtual switch in Hyper-v allowing virtual machine to get IP from DHCP and contact Hyper-V server on mentioned packer ports. This is a must, if kickstart is reachable over the network.

Requirements - Quick Start

Install packer from Chocolatey

choco install packer --version=1.9.1 -y

Install required plugins

In root folder of a repository

packer init --upgrade config.pkr.hcl

Use account with Administrator privileges for Hyper-V

Add firewal exclusions for TCP ports 8000-9000 (default range)

Remove-NetFirewallRule -DisplayName "Packer_http_server" -Verbose
New-NetFirewallRule -DisplayName "Packer_http_server" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 8000-9000

Adjust Hyper-V settings

To adjust to your Hyper-V, please check variables below and/or in ./variables files

  • (variable vlan_id in /variables/variables.*.pkvars.hcl) - proper VLAN ID . Look up to find your build server vEthernet setings.
  • (variable switch_name in /variables/variables.*.pkvars.hcl) - proper Hyper-V Virtual Switch name (access to Internet will be required). Make sure you're using pre-existing switch in your Hyper-V server - creation of new switch by packer, instead of reusing existing one can cause lack of Internet access, thus failing the build.
# example of mentioned variables
vlan_id = ""
switch_name = "vSwitch"

Default passwords

OS username password
Windows Administrator password
CentOS/RHEL root password
Ubuntu ubuntu password

Scripts

Windows Machines

  • all available updates will be applied (3 passes)

  • latest version of chocolatey

  • packages from a list below:

    Package Version Mandatory/Optional
    dotnetfx latest Mandatory
    sysinternals latest Mandatory
    tabby latest Optional
  • phase3.ps1 Puppet agent settings will be customized (server=foreman.example.com) with parameters:

    • Version - puppet chocolatey version, for example "6.26.0"
    • AddPrivateChoco ($true/$false) - if set to true, private MyGet repository will be added as public
    • PuppetMaster (foreman.example.com) - if set, in puppet.conf section server will point to that variable

    Example of usage:

    .\phase3.ps1 -Version 7.14.0 -AddPrivateChoco $true -PuppetMaster foreman.example.com

    Puppet is set to clear any temp SSL keys and to be stopped after generalize phase

  • phase5b-docker.ps1 - Docker settings can be customised

    • requiredVersion - which version of docker module to install - defaults to 19.03.1
    • installCompose ($true/$false) - install docker-compose from chocolatey packages
    • dockerLocation - of set, will default docker images and settings there. On empty, docker location is not being set.
    • configDockerLocation - default place for docker's config file

    Example of usage

    .\phase5b-docker.ps1 -requiredVersion "19.03.1" -installCompose $true -dockerLocation "d:\docker" -configDockerLocation "C:\ProgramData\Docker\config"

Linux Machines

  • Repositories:

    Repository Package switch default
    Epel 7/8/9 epel-release can be switched off by setting "install_epel" to false true
    Zabbix 6.0 zabbix-agent can be switched on by setting "install_zabbix" to true false
    Puppet 7 puppet-agent can be switched off by setting "install_puppet" to false false
    Webmin webmin can be switched on by setting "install_webmin" to false false
    Cockpit cockpit can be switched on by setting "install_zabbix" to true true
    Hyper-V SCVMM Agent can be switched off by setting "install_hyperv" to false true
    Neofetch neofetch can be switched off by setting "install_neofetch" to false true

    Be aware, turning off latest System Center Virtual Machine Agent will cause System Center fail to deploy machines

Ansible Playbooks (CentOS/AlmaLinux/RockyLinux/OracleLinux)

During deployment ansible-base and ansible are installed in operating system. After deployment ends, these packages are removed. Playbooks are held in /extra/playbooks folder, with proper OS variables.

  • adjust ./variables/*.yml files to achieve override for ansible
install_epel:                  true  # install Epel
install_webmin:                true  # install Webmin
install_hyperv:                true  # install Hyper-v and scvmm agent
install_zabbix:                false # install Zabbix-agent
install_zabbix_as_root:        false # install Zabbix-agent as root
install_cockpit:               false # install Cockpit
install_puppet:                true  # Install Puppet
install_docker_workaround:     true  # add `fsck.repair=yes` to grub
install_kubernetes_workaround: false # add `cgroup.memory=nokmem` to grub
remove_puppet_ssl_keys:        false # remove any ssl keys after puppet installation
install_neofetch:              true  # install neofetch
install_updates:               true  # install updates
install_extra_groups:          true  # install extra groups
docker_prepare:                false # prepare extra volumen for docker
extra_device:                  ""    # prepare mkfs and mount extra block device for docker
install_motd:                  true  # install motd (neofetch run)

Usage

Building machines is realised through a dedicated script hv_generic.ps1 with proper parameters.

hv_generic.ps1 parameters

Building Microsoft Windows

Building iso files needed for provisioning

For Generation 2 prepare secondary.iso with folder structure:

- ./extra/files/windows/2022/std/Autounattend.xml     => /Autounattend.xml
- ./extra/scripts/hyper-v/bootstrap.ps1            => /bootstrap.ps1

This template uses this image name in Autounattendes.xml. If youre using different ISO you'll have to adjust that part in proper file and rebuild secondary.iso image.

<InstallFrom>
    <MetaData wcm:action="add">
        <Key>/IMAGE/NAME </Key>
        <Value>Windows Server 2022 SERVERSTANDARD</Value>
    </MetaData>
</InstallFrom>
Action Version Template Log OS
build windows_server_2019_std windows 0/1 Microsoft Server 2019 Standard
build windows_server_2019_dc windows 0/1 Microsoft Server 2019 Datacenter
build windows_server_2022_std windows 0/1 Microsoft Server 2022 Standard
build windows_server_2022_dc windows 0/1 Microsoft Server 2022 Datacenter

Examples for Windows

Example for Windows 2019 Standard

```powershell
./hv_generic.ps1 -Action build -Version windows_server_2019_std -Template windows -Log 0

Example for Windows 2019 Datacenter

./hv_generic.ps1 -Action build -Version windows_server_2019_dc -Template windows -Log 0

Example for Windows 2022 Standard

./hv_generic.ps1 -Action build -Version windows_server_2022_std -Template windows -Log 0

Example for Windows 2022 Datacenter

./hv_generic.ps1 -Action build -Version windows_server_2022_dc -Template windows -Log 0

Building AlmaLinux Machines

Action Version Template Log OS
build almalinux-8.8 rhel 0/1 Alma Linux 8.8
build almalinux-9.2 rhel 0/1 Alma Linux 9.2

Examples for AlmaLinux

.\hv_generic.ps1 -Action build -Version almalinux-8.8 -Template rhel -Log 0
.\hv_generic.ps1 -Action build -Version almalinux-9.2 -Template rhel -Log 0

Building RockyLinux Machines

Action Version Template Log OS
build rockylinux-8.8 rhel 0/1 Rocky Linux 8.8
build rockyinux-9.2 rhel 0/1 Rocky Linux 9.2

Examples for RockyLinux

.\hv_generic.ps1 -Action build -Version rockylinux-8.8 -Template rhel -Log 0
.\hv_generic.ps1 -Action build -Version rockylinux-9.2 -Template rhel -Log 0

Building OracleLinux Machines

Action Version Template Log OS
build oraclelinux-8.8 rhel 0/1 Oracle Linux 8.8
build oraclelinux-9.2 rhel 0/1 Oracle Linux 9.2

Examples for OracleLinux

.\hv_generic.ps1 -Action build -Version oraclelinux-8.8 -Template rhel -Log 0
.\hv_generic.ps1 -Action build -Version oraclelinux-9.2 -Template rhel -Log 0

Building Ubuntu Machines

Action Version Template Log OS
build ubuntu-20.04 ubuntu 0/1 Ubuntu 20.04
build ubuntu-22.04 ubuntu 0/1 Ubuntu 22.04

Examples for Ubuntu

.\hv_generic.ps1 -Action build -Version ubuntu-20.04 -Template ubuntu -Log 0
.\hv_generic.ps1 -Action build -Version ubuntu-22.04 -Template ubuntu -Log 0

Known issues

I have general problem not covered here

Please create an issue in github. There is slim chance I'll find the time to be your personal helpdesk ;)

I'd like to contribute

Sure. If I can ask - create your PR in smaller sizes, this is repo used for my work, so smaller changes - bigger chances to succeed.

Infamous UEFI/Secure boot WIndows implementation

During the deployment secure keys are stored in *.vmcx file and are separated from *.vhdx file. To countermeasure it - there is added extra step in a form of (/usr/local/bin/uefi.sh) script that will check for existence of CentOS folder in EFI and will add extra entry in UEFI. In manual setup you can run it as a part of your deploy. In SCVMM deployment I'd recommend using RunOnce feature.

On Windows Server 2019/Windows 10 1809 image boots to fast for packer to react

hashicorp/packer#7278 (comment)

Fixed in version 1.4.4. Do not use previous versions

When Hyper-V host has more than one interface Packer sets {{ .HTTPIP }} variable to inproper interface

Fixed in version 1.4.4. Do not use lower versions No resolution so far, template needs to be changed to pass real IP address, or there should be connection between these addresses. Limiting these, end with timeout errors.**

Packer version 1.3.0/1.3.1 have bug with windows-restart provisioner

hashicorp/packer#6733

Packer won't run until VirtualSwitch is created as shared

hashicorp/packer#5023 Will be fixed in 1.4.x revision

I have problem how to find a proper WIM name in Windows ISO to pick proper version

You can use number. If you have 4 images on the list of choice - use ImageIndex with proper Value

<ImageInstall>
    <OSImage>
        <InstallFrom>
            <MetaData wcm:action="add">
                <Key>/IMAGE/INDEX </Key>
                <Value>2</Value>
            </MetaData>
        </InstallFrom>
        <InstallTo>
            <DiskID>0</DiskID>
            <PartitionID>2</PartitionID>
        </InstallTo>
    </OSImage>
</ImageInstall>

On Windows machines, build break during updates phase, when update cycles are interfering with each other

Increase variable update_timeout in ./variables/*.json file - this will create longer pauses between stages, allowing cycles to complete before jumping to another one.

Why don't you use ansible instead of shell scripts for provisioning

I wish. In short - Windows. These builds should be done with minimum effort (Hyper-V role is enough). Building custom ansible station with lots of checks right now fails in my tryouts.

Support me

"Buy Me A Coffee"

Consider buying me a coffee if you like my work. All donations are appreciated. All donations will be used to pay for pipeline running costs

About

Work based on https://github.com/jacqinthebox/packer-templates.git

hv-packer's People

Contributors

marcinbojko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hv-packer's Issues

centos 7 and 8 build error

hi , i am getting error "dracut-initqueue : warning cloud not boot" have you come across similar issue ? if yes how did you fix it?

I get a warning and it says that source code is not available

I get the warning below. But I run the script from the root of the repository, what source code is not available?

Building: Ubuntu 22.04
Warning: Hyper-V might fail to create a VM if there is not enough free memory in the system.

  on ./templates/hv_ubuntu2204_g2.pkr.hcl line 95:
  (source code not available)


The configuration is valid.

packer not taking kickstart file

Hi, I am trying to auto-install the ubuntu22.04 live server with packer hyperv-iso builder.
A few times it worked without any issues and now suddenly it stopped working and it is not taking the kickstart file and popping GUI to select the language. When Packer starts It automatically serves HTTP and after that, I checked in the browser with IP:PORT I can see the user-data and meta-data files present. I don't know what went wrong. Can you please help me here?

packer version 1.9.4

Operating system and Environment details
Windows 10

The variables file :

boot_command=["c<wait>","linux /casper/vmlinuz --- autoinstall ds=\"nocloud-net;seedfrom=http://{{.HTTPIP}}:{{.HTTPPort}}/\"","<enter><wait>","initrd /casper/initrd","<enter><wait>","boot","<enter>"]
disk_additional_size=["150000"]
disk_size="70000"
http_directory="cidata"
iso_checksum_type="sha256"
iso_checksum="a4acfda10b18da50e2ec50ccaf860d7f20b389df8765611142305c0e911d16fd"
iso_url="https://releases.ubuntu.com/jammy/ubuntu-22.04.3-live-server-amd64.iso"
output_directory="output-ubuntu2204"
#output_vagrant="./vbox/packer-ubuntu2204-g2.box"
provision_script_options="-z false -h true -p false"
ssh_password="ubuntu"
ssh_username="ubuntu"
switch_name="packer-hyperv-iso"
#vagrantfile_template="./vagrant/hv_ubuntu2204_g2.template"
vlan_id=""
vm_name="packer-ubuntu2204-g2"
uefi_file="./Boot-scripts/uefi.sh"
provision_file="./Boot-scripts/provision.sh"
motd_file="./Boot-scripts/motd.sh"
zeroing_file="./Boot-scripts/zeroing.sh"
neofetch_file="./Boot-scripts/prepare_neofetch.sh"

Template file:

variable "ansible_override" {
  type    = string
  default = ""
}

variable "boot_command" {
}

variable "disk_size" {
  type    = string
  default = "70000"
}

variable "disk_additional_size" {
  type    = list(number)
  default = ["1024"]
}

variable "memory" {
  type    = string
  default = "1024"
}

variable "cpus" {
  type    = string
  default = "1"
}

variable "iso_checksum" {
  type    = string
  default = ""
}

variable "iso_checksum_type" {
  type    = string
  default = "none"
}

variable "iso_url" {
  type    = string
  default = ""
}

variable "output_directory" {
  type    = string
  default = ""
}

variable "provision_script_options" {
  type    = string
  default = ""
}

variable "output_vagrant" {
  type    = string
  default = ""
}

variable "ssh_password" {
  type    = string
  default = ""
  sensitive = true
}

variable "switch_name" {
  type    = string
  default = ""
}

variable "vagrantfile_template" {
  type    = string
  default = ""
}

variable "vlan_id" {
  type    = string
  default = ""
}

variable "vm_name" {
  type    = string
  default = ""
}

variable "http_directory" {
  type    = string
  default = ""
}

variable "ssh_username" {
  type    = string
  default = "ubuntu"
}

variable  "uefi_file" {
  type    = string
  default = ""
}

variable  "provision_file" {
  type    = string
  default = ""
}

variable  "motd_file" {
  type    = string
  default = ""
}

variable  "neofetch_file" {
  type    = string
  default = ""
}

variable  "zeroing_file" {
  type    = string
  default = ""
}

source "hyperv-iso" "vm" {
  boot_command          = "${var.boot_command}"
  boot_wait             = "1s"
  communicator          = "ssh"
  cpus                  = "${var.cpus}"
  disk_block_size       = "1"
  disk_size             = "${var.disk_size}"
  enable_dynamic_memory = "true"
  enable_secure_boot    = false
  generation            = 2
  guest_additions_mode  = "disable"
  http_directory        = "${var.http_directory}"
  iso_checksum          = "${var.iso_checksum_type}:${var.iso_checksum}"
  iso_url               = "${var.iso_url}"
  memory                = "${var.memory}"
  output_directory      = "${var.output_directory}"
  shutdown_command      = "echo 'ubuntu' | sudo -S shutdown -P now"
  shutdown_timeout      = "30m"
  ssh_password          = "${var.ssh_password}"
  ssh_timeout           = "4h"
  ssh_username          = "${var.ssh_username}"
  switch_name           = "${var.switch_name}"
  temp_path             = "."
  vlan_id               = "${var.vlan_id}"
  vm_name               = "${var.vm_name}"
}

build {
  sources = ["source.hyperv-iso.vm"]

  
  provisioner "file" {
    destination = "/tmp/uefi.sh"
    source      = "${var.uefi_file}"
  }

  provisioner "file" {
    destination = "/tmp/provision.sh"
    source      = "${var.provision_file}"
  }

  provisioner "file" {
    destination = "/tmp/scvmmguestagent.1.0.3.1028.x64.tar"
    source      = "./Boot-scripts/scvmmguestagent.1.0.3.1028.x64.tar"
  }

  provisioner "file" {
    destination = "/tmp/install"
    source      = "./Boot-scripts/install.sh"
  }

  provisioner "shell" {
    execute_command   = "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'"
    expect_disconnect = true
    inline            = ["chmod +x /tmp/provision.sh", "chmod +x /tmp/uefi.sh", "mv /tmp/uefi.sh /usr/local/bin/uefi.sh", "/tmp/provision.sh ${var.provision_script_options}", "sync;sync;reboot"]
    inline_shebang    = "/bin/sh -x"
  }

  provisioner "file" {
    destination = "/tmp/motd.sh"
    source      = "${var.motd_file}"
  }

  provisioner "file" {
    destination = "/tmp/prepare_neofetch.sh"
    source      = "${var.neofetch_file}"
  }

  provisioner "file" {
    destination = "/tmp/zeroing.sh"
    source      = "${var.zeroing_file}"
  }

  provisioner "shell" {
    execute_command = "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'"
    inline          = ["echo Last Phase",
    "chmod +x /tmp/prepare_neofetch.sh",
    "chmod +x /usr/local/bin/uefi.sh",
    "chmod +x /tmp/zeroing.sh",
    "/tmp/prepare_neofetch.sh",
    "/tmp/zeroing.sh",
    "/bin/rm -rfv /tmp/*",
    "/bin/rm -f /etc/ssh/*key*",
    "/usr/bin/ssh-keygen -A",
    "echo 'packerVersion: ${packer.version}' >>/etc/packerinfo"]
    inline_shebang  = "/bin/sh -x"
  }
  provisioner "shell" {
    execute_command = "echo '${var.ssh_password}' | {{.Vars}} sudo -S -E bash '{{.Path}}'"
    script = "./Shell-Scripts/azure.sh"
  }
  provisioner "shell" {
    execute_command = "echo '${var.ssh_password}' | {{.Vars}} sudo -S -E bash '{{.Path}}'"
    script = "./Shell-Scripts/cleanup.sh"
  }
  

}

Kickstart/cloud-init file:

#cloud-config
autoinstall:
  version: 1
  indentity:
    hostname: ubuntu2204
    password: $6$rounds=4096$JFVw4zdHh/ahPcjO$NFG4XVjVbcB7kX9KR7nf9zSXTHZnfVqztfC3wPQJbR6tLkFmxEEKuk7lr8Kp6p7z6cdb9qsqINnEtteJYk5h21
    username: ubuntu
  early-commands:
    - systemctl stop ssh # otherwise packer tries to connect and exceed max attempts
  network:
    network:
      version: 2
      ethernets:
        eth0:
          dhcp4: yes
          dhcp-identifier: mac
  update: no
  apt:
    geoip: true
    preserve_sources_list: false
    primary:
      - arches: [amd64]
        uri: "http://archive.ubuntu.com/ubuntu/"
  packages:
    - mc
    - curl
    - wget
    - sudo
    - tar
    - bzip2
    - build-essential
    - linux-image-virtual
    - linux-tools-virtual
#    - linux-cloud-tools-virtual
    - net-tools
    - qemu-guest-agent
  locale: en_US.UTF-8
  timezone: Europe/Copenhagen
  ssh:
    install-server: true
    allow-pw: true
  user-data:
    disable_root: false
    lock-passwd: false
    ssh_pwauth: true
    hostname: ubuntu2204
    password: $6$rounds=4096$JFVw4zdHh/ahPcjO$NFG4XVjVbcB7kX9KR7nf9zSXTHZnfVqztfC3wPQJbR6tLkFmxEEKuk7lr8Kp6p7z6cdb9qsqINnEtteJYk5h21
    username: ubuntu
  late-commands:
    #- echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu
    - sed -ie 's/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX="net.ifnames=0 ipv6.disable=1 biosdevname=0 elevator=noop"/' /target/etc/default/grub
    - sed -ie 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /target/etc/ssh/sshd_config
    - sed -ie 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /target/etc/ssh/sshd_config
    - sed -ie 's/#AllowAgentForwarding yes/AllowAgentForwarding yes/' /target/etc/ssh/sshd_config
    - curtin in-target -- update-grub2
    # virtual package is required for some cloud images. Lack of it causes failure in communication.
    - curtin in-target -- apt-get -y install linux-cloud-tools-virtual||true
    - curtin in-target -- systemctl enable qemu-guest-agent
storage:
  config:
    - type: disk
      id: disk0
      ptable: gpt
      wipe: superblock
      grub_device: true
      match:
        size: largest
    - id: efi-partition # create partitions on disk (like sda1)
      type: partition
      device: disk0
      size: 512MB
      flag: boot # EFI system partition needs boot flag
    - type: partition
      id: boot-partition
      device: disk0
      size: 2GB
    - type: partition
      device: disk0
      id: root-partition
      size: -1
    - id: efi-partition-fs # format partitions on disk
      type: format
      volume: efi-partition
      fstype: fat32
      label: EFI
    - id: boot-partition-fs
      type: format
      fstype: ext4
      volume: boot-partition
    - id: root-partition-fs
      type: format
      fstype: ext4
      volume: root-partition
    - id: efi-partition-fs-mount # mount partitions
      type: mount
      device: efi-partition-fs
      path: /boot/efi
    - id: root-partition-fs-mount
      type: mount
      path: /
      device: root-partition-fs
    - id: boot-partition-fs-mount
      type: mount
      path: /boot
      device: boot-partition-fs

ubuntu 20.04 LVM not created as per user-data

Hi @marcinbojko,

Following your step i did a small modification to add more FS in the LVM but the image create takes/create its LVM. I hope that can help me out to create the FS as below rather then 1 getting created of its currently

storage:
  config:
    - {ptable: gpt, path: /dev/sda, wipe: superblock, preserve: false, name: '', grub_device: true, type: disk, id: disk-sda}
    - {device: disk-sda, size: 1M, flag: bios_grub, number: 1, preserve: false, grub_device: false, type: partition, id: partition-0}
    - {device: disk-sda, size: 1G, wipe: superblock, flag: '', number: 2, preserve: false, grub_device: false, type: partition, id: partition-1}
    - {device: disk-sda, size: 69G, wipe: superblock, flag: '', number: 3, preserve: false, grub_device: false, type: partition, id: partition-2}
    - name: ubuntu-vg
      devices: [partition-2]
      preserve: false
      type: lvm_volgroup
      id: lvm_volgroup-0
    - {fstype: xfs, volume: partition-1, preserve: false, type: format, id: format-2}
    - {name: swap, volgroup: lvm_volgroup-0, size: 2G, preserve: false, type: lvm_partition, id: lvm_partition-1}
    - {fstype: swap, volume: lvm_partition-1, preserve: false, type: format, id: format-3}
    - {device: format-3, path: '', type: mount, id: mount-3}
    - {name: var, volgroup: lvm_volgroup-0, size: 5G, preserve: false, type: lvm_partition, id: lvm_partition-2}
    - {fstype: xfs, volume: lvm_partition-2, preserve: false, type: format, id: format-4}
    - {name: tmp, volgroup: lvm_volgroup-0, size: 5G, preserve: false, type: lvm_partition, id: lvm_partition-3}
    - {fstype: xfs, volume: lvm_partition-3, preserve: false, type: format, id: format-5}
    - {name: var-log, volgroup: lvm_volgroup-0, size: 5G, preserve: false, type: lvm_partition, id: lvm_partition-4}
    - {fstype: xfs, volume: lvm_partition-4, preserve: false, type: format, id: format-6}
    - {name: var-log-audit, volgroup: lvm_volgroup-0, size: 5G, preserve: false, type: lvm_partition, id: lvm_partition-5}
    - {fstype: xfs, volume: lvm_partition-5, preserve: false, type: format, id: format-7}
    - {name: var-tmp, volgroup: lvm_volgroup-0, size: 5G, preserve: false, type: lvm_partition, id: lvm_partition-6}
    - {fstype: xfs, volume: lvm_partition-6, preserve: false, type: format, id: format-8}
    - {name: home, volgroup: lvm_volgroup-0, size: 10G, preserve: false, type: lvm_partition, id: lvm_partition-7}
    - {fstype: xfs, volume: lvm_partition-7, preserve: false, type: format, id: format-9}
    - {name: root, volgroup: lvm_volgroup-0, size: 34G, preserve: false, type: lvm_partition, id: lvm_partition-8}
    - {device: format-10, path: /, type: mount, id: mount-10}
    - {device: format-2, path: /boot, type: mount, id: mount-2}
    - {device: format-4, path: /var, type: mount, id: mount-4}
    - {device: format-5, path: /tmp, type: mount, id: mount-5}
    - {device: format-6, path: /var/log, type: mount, id: mount-6}
    - {device: format-7, path: /var-log-audit, type: mount, id: mount-7}
    - {device: format-8, path: /var/tmp, type: mount, id: mount-8}
    - {device: format-9, path: /home, type: mount, id: mount-9}

After image is created i see the lvdisplay and df -h details as below
image
image

disk space

Hi,

could you document how much disk space you allocated for the (gitlab-)CI builds?

I just know my normal builder will never fit all those images, but don't know how big the new one should become ;-)

Support for Packer Windows Update Provisioner

Have you considered integrating Packer Windows Update Provisioner into your provision steps? The output of this plugin is much cleaner when ran via PowerShell vs raw output of the WU com object with PSWindowsupdate.

I'll be testing this against Windows Server 2019 DC and will report back if there are any issues.

Love the work you've done so far! Having come from a VMware shop to HyperV shop has been challenging, but your code has helped tremendously!

WS22 - Datacenter Edition - Incorrect Value

vagrant/hv_win2022_dc.template contains an incorrect setting to define a ws22dc setup.
config.vm.define "vagrant-w2k19-dc-trial" should be w2k22-dc
config.vm.box = "w2k19-dc-trial" should be w2k22-dc

Unless I'm completely off-base here and missing something.
Otherwise, love your work. Hyper-V is a godsend compared to dealing with VMware licensing terms + pricing

I fail to override variable values

I don't know if I miss something obvious, but when I try:
.\hv_ubuntu2204.ps1 -var 'output_directory=test'
I still get the default, "output-ubuntu2204" from
variables_ubuntu2204.pkvars.hcl .

Provisioning step had errors - Ubuntu 22.04 - Windows 11 Hyper-v

  • Packer Version:

1.9.4

  • Platform:

Windows 11

  • Error:
image
  • variables
boot_command=["<wait3>c<wait3>","linux /casper/vmlinuz quiet autoinstall net.ifnames=0 biosdevname=0 ip=dhcp ipv6.disable=1 ds=nocloud-net\\;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ <enter>","initrd /casper/initrd <enter>","boot <enter>"]
disk_additional_size=["150000"]
disk_size="70000"
http_directory="./extra/files/ubuntu/22.04/hyperv"
iso_checksum_type="sha256"
iso_checksum="a4acfda10b18da50e2ec50ccaf860d7f20b389df8765611142305c0e911d16fd"
iso_url="https://releases.ubuntu.com/jammy/ubuntu-22.04.3-live-server-amd64.iso"
output_directory="output-ubuntu2204"
#output_vagrant="./vbox/packer-ubuntu2204-g2.box"
provision_script_options="-z false -h true -p false"
ssh_password="password"
ssh_username="ubuntu"
switch_name="Default Switch"
#vagrantfile_template="./vagrant/hv_ubuntu2204_g2.template"
vlan_id=""
vm_name="packer-ubuntu2204-g2"
uefi_file="extra/files/ubuntu/22.04/uefi.sh"
provision_file="extra/files/ubuntu/shared/provision.sh"
motd_file="extra/files/ubuntu/shared/motd.sh"
zeroing_file="extra/files/ubuntu/shared/zeroing.sh"
neofetch_file="extra/files/ubuntu/shared/prepare_neofetch.sh"
  • Template
variable "ansible_override" {
  type    = string
  default = ""
}

variable "boot_command" {
}

variable "disk_size" {
  type    = string
  default = "70000"
}

variable "disk_additional_size" {
  type    = list(number)
  default = ["1024"]
}

variable "memory" {
  type    = string
  default = "1024"
}

variable "cpus" {
  type    = string
  default = "1"
}

variable "iso_checksum" {
  type    = string
  default = ""
}

variable "iso_checksum_type" {
  type    = string
  default = "none"
}

variable "iso_url" {
  type    = string
  default = ""
}

variable "output_directory" {
  type    = string
  default = ""
}

variable "provision_script_options" {
  type    = string
  default = ""
}

variable "output_vagrant" {
  type    = string
  default = ""
}

variable "ssh_password" {
  type    = string
  default = ""
  sensitive = true
}

variable "switch_name" {
  type    = string
  default = ""
}

variable "vagrantfile_template" {
  type    = string
  default = ""
}

variable "vlan_id" {
  type    = string
  default = ""
}

variable "vm_name" {
  type    = string
  default = ""
}

variable "http_directory" {
  type    = string
  default = ""
}

variable "ssh_username" {
  type    = string
  default = "ubuntu"
}

variable  "uefi_file" {
  type    = string
  default = ""
}

variable  "provision_file" {
  type    = string
  default = ""
}

variable  "motd_file" {
  type    = string
  default = ""
}

variable  "neofetch_file" {
  type    = string
  default = ""
}

variable  "zeroing_file" {
  type    = string
  default = ""
}

source "hyperv-iso" "vm" {
  boot_command          = "${var.boot_command}"
  boot_wait             = "1s"
  communicator          = "ssh"
  cpus                  = "${var.cpus}"
  disk_block_size       = "1"
  disk_size             = "${var.disk_size}"
  enable_dynamic_memory = "true"
  enable_secure_boot    = false
  generation            = 2
  guest_additions_mode  = "disable"
  http_directory        = "${var.http_directory}"
  iso_checksum          = "${var.iso_checksum_type}:${var.iso_checksum}"
  iso_url               = "${var.iso_url}"
  memory                = "${var.memory}"
  output_directory      = "${var.output_directory}"
  shutdown_command      = "echo 'password' | sudo -S shutdown -P now"
  shutdown_timeout      = "30m"
  ssh_password          = "${var.ssh_password}"
  ssh_timeout           = "4h"
  ssh_username          = "${var.ssh_username}"
  switch_name           = "${var.switch_name}"
  temp_path             = "."
  vlan_id               = "${var.vlan_id}"
  vm_name               = "${var.vm_name}"
}

build {
  sources = ["source.hyperv-iso.vm"]

  provisioner "file" {
    destination = "/tmp/uefi.sh"
    source      = "${var.uefi_file}"
  }

  provisioner "file" {
    destination = "/tmp/provision.sh"
    source      = "${var.provision_file}"
  }

  provisioner "file" {
    destination = "/tmp/scvmmguestagent.1.0.3.1028.x64.tar"
    source      = "extra/files/scagent/1.0.3.1028/scvmmguestagent.1.0.3.1028.x64.tar"
  }

  provisioner "file" {
    destination = "/tmp/install"
    source      = "extra/files/scagent/1.0.3.1028/install.sh"
  }

  provisioner "shell" {
    execute_command   = "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'"
    expect_disconnect = true
    inline            = ["chmod +x /tmp/provision.sh", "chmod +x /tmp/uefi.sh", "mv /tmp/uefi.sh /usr/local/bin/uefi.sh", "/tmp/provision.sh ${var.provision_script_options}", "sync;sync;reboot"]
    inline_shebang    = "/bin/sh -x"
  }

  provisioner "file" {
    destination = "/tmp/motd.sh"
    source      = "${var.motd_file}"
  }

  provisioner "file" {
    destination = "/tmp/prepare_neofetch.sh"
    source      = "${var.neofetch_file}"
  }

  provisioner "file" {
    destination = "/tmp/zeroing.sh"
    source      = "${var.zeroing_file}"
  }

  provisioner "shell" {
    execute_command = "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'"
    inline          = ["echo Last Phase",
    "chmod +x /tmp/prepare_neofetch.sh",
    "chmod +x /usr/local/bin/uefi.sh",
    "chmod +x /tmp/zeroing.sh",
    "/tmp/prepare_neofetch.sh",
    "/tmp/zeroing.sh",
    "/bin/rm -rfv /tmp/*",
    "/bin/rm -f /etc/ssh/*key*",
    "/usr/bin/ssh-keygen -A",
    "echo 'packerVersion: ${packer.version}' >>/etc/packerinfo"]
    inline_shebang  = "/bin/sh -x"
  }

}

From what I can tell, it crashes after the provisioning script is completed and right before it places the motd.sh file into the tmp folder. Could you please let me know where I might be going wrong with this? I tried running this using the powershell script you have provided as well as just running the packer command. The results are the same.

Can't login

Hi,

For some reason after my build finishes with ubuntu 20.04.03 and I launch it in Hyper-V, I can't login. I'm using root/password and the password is always wrong. What am I doing wrong? I haven't changed anything except for the ISO.

Thanks

Confirmation on the use of Ansible Provisioner

Hi @marcinbojko ,

I wanted to confirm from your end on the use of ansible remote or local provisioner with Packer when we run packer in hyper-v and the image which is getting build is also Windows.

I believe this can't be done as both the source(Hyper-V) and image getting build are windows machine and ansible can't be install on it. But can u confirm if their is any alternative to get this done?

Error: hyperv-iso: Error shutting down VM: PowerShell error: Hyper-V\Stop-VM : Failed to stop.

Added the post-processor to your JSON to export the hyper-v vhdx, but now getting the below error message, Would you please help.

hyperv-iso: WINDOWS-2016V7 restarted.
hyperv-iso: #< CLIXML
hyperv-iso: <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObje

ctSystem.Object1Preparing modules for first use.0-1-1Completed-1

==> hyperv-iso: Machine successfully restarted, moving on
==> hyperv-iso: Provisioning with Powershell...
==> hyperv-iso: Provisioning with powershell script: ./extra/scripts/windows-updates.ps1
==> hyperv-iso: Disconnecting from vmconnect...
==> hyperv-iso: Error shutting down VM: PowerShell error: Hyper-V\Stop-VM : Failed to stop.
==> hyperv-iso: At C:\Users\Administrator\AppData\Local\Temp\2\ps320263257.ps1:5 char:5
==> hyperv-iso: + Hyper-V\Stop-VM -VM $vm -Force -Confirm:$false
==> hyperv-iso: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==> hyperv-iso: + CategoryInfo : NotSpecified: (:) [Stop-VM], VirtualizationException
==> hyperv-iso: + FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.StopVM
==> hyperv-iso: Clean up secondary dvd drives...
==> hyperv-iso: Clean up os dvd drive...
==> hyperv-iso: Unregistering and deleting virtual machine...
==> hyperv-iso: Deleting output directory...
==> hyperv-iso: Deleting build directory...
Build 'hyperv-iso' errored: Script exited with non-zero exit status: 2147943515. Allowed exit codes are: [0]

==> Some builds didn't complete successfully and had errors:
--> hyperv-iso: Script exited with non-zero exit status: 2147943515. Allowed exit codes are: [0]

==> Builds finished but no artifacts were created.

Template Fail to Boot

Hi,

Without making much of the change to the template expect the switch, iso and iso check-sum, the packer build fails stating could not build /dev/root does not exist.

can help why its failing down?

image
image

centos 7 build error

Hello.
Trying to run centos 7.8 on the Windows Server 2019 Hyper V and getting dracut-initqueue timeout - starting timeout scripts error.
Not sure if it's similar to #4

Ubuntu 22.04.1 Not able to Install

I am trying to build the latest ubuntu server using hyper-v on windows 11 machine, but the process is stuck at

image

Here is a screen grab from the virtual machine.

image

I have installed same iso manually and it works.

Please Help

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.