marcinbojko / hv-packer

Hyper-V Generation 2 Packer Templates for Windows 2019/2022, AlmaLinux 8.x/AlmaLinux 9.x/RockyLinux 8.x/9.x/OracleLinux 8.x/9.x Ubuntu 20.04/Ubuntu 22.04/Ubuntu 24.04
188 stars 74 forks source link

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

Closed Souldiv closed 11 months ago

Souldiv commented 11 months ago

1.9.4

Windows 11

image
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"
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.

Souldiv commented 11 months ago

Update:

Made it work using a hack, the reason why it was failing is because after running the provision script it reboots the machine and the machine is assigned another IP upon boot and the IP previously used for SSH by packer doesn't work. I was able to login after the provisioning step and assign the IP in the netplan config and apply the changes. It completed the build but I think a better solution is definitely required.

Souldiv commented 11 months ago

Update 2:

The setup works without any hacks by removing the reboot command. @marcinbojko do you foresee any issues without reboot?

marcinbojko commented 11 months ago

Please adjust DHCP settings in your environment. This has nothing to do with scripts or packer itself.

Souldiv commented 10 months ago

As DHCP doesn't necessarily mean that after reboot the machine will be given the same IP, this is a possible issue with packer and I have created an issue here. This issue is similar to the one mentioned here