Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ubuntu 20.04.3 - Build with hcl #11275

Closed
talheim-it opened this issue Sep 16, 2021 · 10 comments
Closed

Ubuntu 20.04.3 - Build with hcl #11275

talheim-it opened this issue Sep 16, 2021 · 10 comments
Labels

Comments

@talheim-it
Copy link

talheim-it commented Sep 16, 2021

Hello Community!

I tried to create a new Ubuntu20.04.3 box by myself. The builder runs completely through without issues what I can see.

But when I am adding it to vagrant, it is not coming up, no ssh connection can be established. I guess I did anything wrong in the packer build so I wanted to share it first before digging into vagrant, since the build for CentOS, Fedora, AlmaLinux, RockyLinux worked.

Build with:
Fedora 34 Workstation
Packer v1.7.5

ubuntu2004.pkr.hcl:

##################################################################################
# VARIABLES
##################################################################################

variable "vm_ssh_username" {
  type    = string
  description = "The username to use to authenticate over SSH."
  default = "ubuntu"
}

variable "vm_ssh_password" {
  type    = string
  description = "The plaintext password to use to authenticate over SSH."
  default = "ubuntu"
}

variable "iso_url" {
  type    = string
  description = "The path on the source vSphere datastore for ISO images."
  default = ""
}

variable "iso_file" {
  type = string
  description = "The file name of the guest operating system ISO image installation media."
  default = ""
}

variable "iso_checksum" {
  type    = string
  description = "The SHA-512 checkcum of the ISO image."
  default = ""
}

variable "http_directory" {
  type    = string
  description = "Directory of config files(user-data, meta-data)."
  default = ""
}

variable "vm_firmware" {
  type    = string
  description = "The virtual machine firmware. (e.g. 'bios' or 'efi')"
  default = ""
}

variable "vm_cpu_cores" {
    type = number
    description = "Number of CPU cores for the VM"
    default = 2
}

variable "vm_mem_size" {
    type = number
    description = "Size of the virtual memory im MB"
    default = 2048
}

variable "vm_disk_size" {
    type = number
    description = "The size of the virtual disk in MB"
}

variable "vm_host_name" {
    type = string
    description = "Name of the virtual machine"
    default = "Ubuntu2004"
}

variable "vm_output_directory" {
    type = string
    description = "Location where the box image is exported to"
    default = "/home/username/Development/images/libvirt/Ubuntu2004"
}

##################################################################################
# SOURCE
##################################################################################

source "qemu" "ubuntu2004" {
  # VM resource parameters
  cpus              = "${var.vm_cpu_cores}"
  memory            = "${var.vm_mem_size}"
  disk_interface    = "virtio"
  disk_size         = "${var.vm_disk_size}"
  iso_url           = "${var.iso_url}"
  iso_checksum      = "${var.iso_checksum}"
  output_directory  = "${var.vm_output_directory}"
  shutdown_command  = "echo '${var.vm_ssh_password}' | sudo -S shutdown -P now"
  format            = "qcow2"
  accelerator       = "kvm"
  http_directory    = "${var.http_directory}"
  ssh_username      = "${var.vm_ssh_username}"
  ssh_password      = "${var.vm_ssh_password}"
  ssh_handshake_attempts = "100"
  vm_name           = "${var.vm_host_name}"
  net_device        = "virtio-net"
  net_bridge        = "virbr0"
  cd_files          = ["./http/user-data", "./http/meta-data"]
  cd_label          = "cidata"
  boot_wait         = "2s"
  qemu_binary       = "/usr/bin/qemu-system-x86_64"
  boot_command      = [
    "<enter><wait2><enter><wait><f6><esc><wait>",
    " autoinstall<wait2> ds=nocloud;",
    "<wait><enter>"
  ]
}

build {
  sources = ["source.qemu.ubuntu2004"]

  post-processor "vagrant" {}
}

File: variables.pkr.hcl:

##################################################################################
# VARIABLES
##################################################################################

vm_cpu_cores          = 2
vm_mem_size           = 4096
vm_disk_size          = 40000

vm_ssh_username       = "vagrant"
vm_ssh_password       = "vagrant"

http_directory        = "http"

iso_url               = "https://releases.ubuntu.com/20.04/ubuntu-20.04.3-live-server-amd64.iso"
iso_checksum          = "file:https://releases.ubuntu.com/20.04/SHA256SUMS"

vm_output_directory   = "/home/username/Development/images/libvirt/Ubuntu2004"

File: http/user-data:

#cloud-config
autoinstall:
  version: 1
  user-data:
    users:
      - name: vagrant
        passwd: $6$ZTEqV3tdvUeoVgC7$MLAObfljDKhULgtoq6Hx.SC.1dD8bo84PqH8poZzcJqo9czDYutON68/PjAmxVK3BQU3N.fB4G9MuZZ5OQ3GF1
        groups: [adm, cdrom, dip, plugdev, lxd, sudo]
        lock-passwd: false
        sudo: ALL=(ALL) NOPASSWD:ALL
        shell: /bin/bash
        ssh_authorized_keys:
          - examplekey
  locale: en_US
  keyboard:
      layout: de
      variant: de
  network:
    version: 2
    ethernets:
        ens3:
          dhcp4: true
  storage:
    layout:
      name: direct
    config:
      - type: disk
        id: disk0
        match:
          size: largest
      - type: partition
        id: boot-partition
        device: disk0
        size: 500M
      - type: partition
        id: root-partition
        device: disk0
        size: -1
  ssh:
    install-server: true
    allow-pw: true
  packages:
      - bash-completion
      - wget
      - vim
      - curl
      - qemu-guest-agent
@talheim-it talheim-it added the bug label Sep 16, 2021
@todeb
Copy link

todeb commented Oct 1, 2021

Seems that I might have similar issue. The network seems to not bring up. Then Packer is waiting for ssh and after few minutes timeouts.
But if I login to a machine via VM Console and execute from it ping to gw or any external IP, appeareantly it will make the machine avaiable and packer could finish build.

@it-pappa
Copy link

it-pappa commented Nov 3, 2021

@talheim-it Hi,
Can you console into the Vm while its building? I had the same issue and found it was a mix of using the live server img, wrong boot command and not killing the SSH at the start of the build.

I dont know which hypervisor you are using but i have two perfecetly tested and working buils for both proxmox and vsphere here: https://github.com/it-pappa

Try adding
early-commands: - sudo systemctl stop ssh
in the very start of the autinstall (after verion 1).

@talheim-it
Copy link
Author

@it-pappa what image did you use? since ubuntu is only using live images it is a bit hard.

I am primarly using libvirt/kvm.

@it-pappa
Copy link

@talheim-it Im using this: https://releases.ubuntu.com/focal/ubuntu-20.04.3-live-server-amd64.iso

Check my Ubuntu Packer repo and take a look at the start of my http file. Stop ssh server since i think Ubuntu will use the ssh for itself while installing.

I will update my repo today so it works better with Terraform/Vagrant. But to get it to work you need to stop the ssh server like you see in my http file.

@talheim-it
Copy link
Author

@it-pappa thanks I am already building, just checked that I have increase the ssh_timeout.

Keep you updated.

@talheim-it
Copy link
Author

talheim-it commented Nov 12, 2021

@it-pappa

I only have one issue left:
After it runs the earyl-commands I get this:
image

Thats how the config looks:
image

  boot_command = [
    "<esc><esc><esc>",
    "<enter><wait>",
    "/casper/vmlinuz ",
    "initrd=/casper/initrd ",
    "autoinstall ",
    "<enter>"
  ]

@it-pappa
Copy link

@talheim-it I would remove:
refresh installer.
I have now update my packer repo.You can download it and just change the variables. :)
https://github.com/it-pappa/Packer-vsphere/tree/main/Ubuntu.20.04

@nywilken
Copy link
Contributor

nywilken commented Jun 10, 2022

👋 it looks like this issue has been resolved and may have been a combination for network and ssh timeout changes to get it working. But it is not clear. Is there anything that needs to change in Packer to or the Vagrant plugin to make this easier to work with?

@nywilken
Copy link
Contributor

Hi there 👋 , I'm going to close this issue since there hasn't been any updates to it. But if you are still running into issues please feel free to leave a comment on the issue and we will gladly reopen.

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants