[SOLVED] Using Packer to deploy Ubuntu 20.04 to Proxmox

aRustyDev

Member
Aug 10, 2021
6
1
8
31
DISCLAIMER
I am not sure if this is even "the right" place for this, but it seems like a great place to start. This mostly relates to the Proxmox API and how VMs are actually created, so if you have any deep knowledge or even just a bright idea I might've missed, shoot it my way :)

Problem

TLDR
: I've got this doing most of the AutoInstall (I think?) but it keeps bringing up the GUI install after it goes through the AutoInstall. Looking for any input into why it keeps bringing up the GUI. /TLDR

Longer Version
: I am trying to figure out how to deploy ubuntu (and eventually other distros) to Proxmox by using Hashicorp Packer. I've got most of the configs setup and working... sort of? It's successfully "deploying" but it can't finish the initial setup, it keeps going through the AutoInstall, but then still brings up the GUI install. From stepping through the GUI manually, after it finishes the AutoInstall portion, it seems everything is already filled out (minus the user identity portion, ie FullName/Username/etc). It doesn't seem to even need to find an IP addr, like when you step through just the GUI, so I think DHCP is working fine?

Theories
  1. I've got a few theories, but none seem to work out quite right, or I'm not sure how to test them.
  2. I'm forgetting something in the `user-data` (yaml), and its trying to bring up the GUI because of this.
  3. I'm missing some config parameter in the `.pkr.hcl` file that is necessary for proxmox to set up the vm correctly?
  4. Something is weird with the Ubuntu version I'm using, maybe I need a different ISO? But the stuff I've read just says that the *Cloud-init* feature just swaps out the legacy `preseed.cfg` for the `user-data` (yaml) file, so I'm doubtful about this theory.
  5. I need to set some kernel feature? Honestly not sure where to start here, I'm also pretty sure if this was the case I would have seen something about it somewhere else. But hey, maybe I just looked in the wrong places?
Hopeful outcomes
  1. If you know the magic sauce, please grace me with it :)
  2. If you know how to get Ubuntu 20.04 to redirect some of its logs during install, so I can take a deeper look, that'd be appreciated! Same for better packer logging (aside from `-debug`, I've already tried that)
  3. If you have any ideas on how to test some of my theories, or any other theories I'd love to hear them!
  4. If you can see what I'm doing wrong in the below config files please point them out! I've been trying to do this in a SILO for a few days now and am up against a wall I don't know how to get over
Hint
If you're cool enough to try replicating this stuff, or if you've stumbled into this after its (hopefully) been answered, make your life easier by using a `example.auto.pkrvars.hcl` file.
Versions
  • Proxmox: Proxmox VE 7.1-4
  • Windows: Windows: `Windows 10 Pro : 10.0.19043.0`
  • Packer: Packer: 1.7.10

Example.pkr.hcl
Code:
packer {
  required_plugins {
    proxmox = {
      version = " >= 1.0.1"
      source  = "github.com/hashicorp/proxmox"
    }
  }
}
source "proxmox-iso" "proxmox-ubuntu-20" {
  proxmox_url = "https://proxmox.lan:8006/api2/json"
  vm_name     = "packer-ubuntu-20"
  iso_url      = "http://iso.repo.lan/ubuntu-20.04.3-live-server-amd64.iso"
  iso_checksum = "f8e3086f3cea0fb3fefb29937ab5ed9d19e767079633960ccb50e76153effc98"
  username         = "${var.pm_user}"
  password         = "${var.pm_pass}"
  token            = "${var.pm_token}"
  node             = "proxmox"
  iso_storage_pool = "local"
  ssh_username           = "${var.ssh_user}"
  ssh_password           = "${var.ssh_pass}"
  ssh_timeout            = "20m"
  ssh_pty                = true
  ssh_handshake_attempts = 20
  boot_wait      = "5s"
  http_directory = "http" # Starts a local http server, serves Preseed file
  boot_command = [
    "<esc><wait><esc><wait><f6><wait><esc><wait>",
    "<bs><bs><bs><bs><bs>",
    "autoinstall ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/",
    "--- <enter>"
  ]
  insecure_skip_tls_verify = true
  template_name        = "packer-ubuntu-20"
  template_description = "packer generated ubuntu-20.04.3-server-amd64"
  unmount_iso          = true
  pool       = "packer"
  memory     = 4096
  cores      = 1
  sockets    = 1
  os         = "l26"
  qemu_agent = true
  cloud_init = true
  # scsi_controller = "virtio-scsi-pci"
  disks {
    type              = "scsi"
    disk_size         = "30G"
    storage_pool      = "local-lvm"
    storage_pool_type = "lvm"
    format            = "raw"
  }
  network_adapters {
    bridge   = "vmbr0"
    model    = "virtio"
    firewall = true
    # vlan_tag = 1
  }
}
build {
  sources = ["source.proxmox-iso.proxmox-ubuntu-20"]
  provisioner "shell" {
    inline = [
      "while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
      "ls /"
    ]
  }
}


vars.pkr.hcl
Code:
variable "pm_user" {
  type        = string
  description = "Proxmox User Name"
  default     = "packer@pam!<api-tag>"
}
variable "pm_pass" {
  type        = string
  description = "Proxmox User Password"
  default     = "packer"
}
variable "pm_token" {
  type        = string
  description = "Proxmox API Token"
}
variable "ssh_user" {
  type        = string
  description = "SSH User"
  default     = "packer"
}
variable "ssh_pass" {
  type        = string
  description = "SSH Password"
  default     = "packer"
}
variable "vm_name" {
  type        = string
  description = "VM Name"
  default     = "packer_hostname"
}

http/user-data
YAML:
autoinstall:
  version: 1
  locale: en_US.UTF-8
  keyboard:
    layout: en
    variant: us
  identity:
    hostname: packer-ubuntu-20
    password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0" #ubuntu
    username: ubuntu
    realname: ubuntu
  refresh-installer:
    update: false
  ssh:
    install-server: yes
    allow-pw: yes
  packages:
    - qemu-guest-agent
  storage:
    layout:
      name: direct
    swap:
      size: 0



Sources
https://www.aerialls.eu/posts/ubuntu-server-2004-image-packer-subiquity-for-proxmox/
https://ubuntu.com/server/docs/install/autoinstall-reference
https://cloudinit.readthedocs.io/en/latest/topics/datasources/nocloud.html
https://cloudinit.readthedocs.io/en/latest/topics/modules.html#write-files
https://cloudalbania.com/posts/2022-01-homelab-with-proxmox-and-packer/

I'm also planning to put this question up on Stack Exchange, but mucked up my first attempt (put it on VI.stackexchange.com) so now I'm here first.
 

Attachments

  • Like
Reactions: taderson
UPDATE :
  1. A good place to look for logs during the install is the /var/log/installer directory. Get to it by keying up to [HELP] and then down to Shell from the GUI installer
 
Answer
As luck would have it, I was searching in a completely erroneous direction SMH.
The issue is that I didn't read the Cloud-init FAQs closely enough. All I had to do was add #cloud-config as the first line in my user-data file.

The below configs will work, for anyone who's interested.

(SHAMELESS PLUG) If you're looking for some other OS/method, you can checkout my GitHub Repo to see if I have a working solution that fits your need
https://github.com/aRustyDev/packer

Example.pkr.hcl
Code:
packer {
  required_plugins {
    proxmox = {
      version = " >= 1.0.1"
      source  = "github.com/hashicorp/proxmox"
    }
  }
}

source "proxmox-iso" "proxmox-ubuntu-20" {
  proxmox_url = "https://proxmox.lan:8006/api2/json"
  vm_name     = "packer-ubuntu-20"
  iso_url      = "http://iso.repo.lan/ubuntu-20.04.3-live-server-amd64.iso"
  iso_checksum = "f8e3086f3cea0fb3fefb29937ab5ed9d19e767079633960ccb50e76153effc98"
  username         = "${var.pm_user}"
  password         = "${var.pm_pass}"
  token            = "${var.pm_token}"
  node             = "proxmox"
  iso_storage_pool = "local"

  ssh_username           = "${var.ssh_user}"
  ssh_password           = "${var.ssh_pass}"
  ssh_timeout            = "20m"
  ssh_pty                = true
  ssh_handshake_attempts = 20

  boot_wait      = "5s"
  http_directory = "http" # Starts a local http server, serves Preseed file
  boot_command = [
    "<esc><wait><esc><wait><f6><wait><esc><wait>",
    "<bs><bs><bs><bs><bs>",
    "ip=${cidrhost("192.168.0.0/24", 9)}::${cidrhost("192.168.0.0/24", 1)}:${cidrnetmask("192.168.0.0/24")}::::${cidrhost("192.168.0.0/24", 1)} ",
    " autoinstall ds=nocloud-net;s=http://httpd.proxmox.local:80/preseed/ubuntu-20/ ",
    "--- <enter>"
  ]

  insecure_skip_tls_verify = true

  template_name        = "packer-ubuntu-20"
  template_description = "packer generated ubuntu-20.04.3-server-amd64"
  unmount_iso          = true

  pool       = "packer"
  memory     = 4096
  cores      = 1
  sockets    = 1
  os         = "l26"
  qemu_agent = true
  disks {
    type              = "scsi"
    disk_size         = "30G"
    storage_pool      = "local-lvm"
    storage_pool_type = "lvm"
    format            = "raw"
  }
  network_adapters {
    bridge   = "vmbr0"
    model    = "virtio"
    firewall = true
  }
}

build {
  sources = ["source.proxmox-iso.proxmox-ubuntu-20"]
  provisioner "shell" {
    inline = [
      "while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
      "ls /"
    ]
  }
}

vars.pkr.hcl
Code:
variable "pm_user" {
  type        = string
  description = "Proxmox User Name"
  default     = "packer@pam!<api-tag>"
}
variable "pm_pass" {
  type        = string
  description = "Proxmox User Password"
  default     = "packer"
}
variable "pm_token" {
  type        = string
  description = "Proxmox API Token"
}
variable "ssh_user" {
  type        = string
  description = "SSH User"
  default     = "packer"
}
variable "ssh_pass" {
  type        = string
  description = "SSH Password"
  default     = "packer"
}
variable "vm_name" {
  type        = string
  description = "VM Name"
  default     = "packer_hostname"
}

http/user-data

YAML:
#cloud-config
autoinstall:
  version: 1
  locale: en_US.UTF-8
  keyboard:
    layout: en
    variant: us
  identity:
    hostname: packer-ubuntu-20
    password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0" #ubuntu
    username: ubuntu
    realname: ubuntu
  refresh-installer:
    update: false
  ssh:
    install-server: yes
    allow-pw: yes
  packages:
    - qemu-guest-agent
  storage:
    layout:
      name: direct
    swap:
      size: 0
 
Just wanted to let you know this helped me with a struggle I was having with packer and proxmox. I first tested my setup with your user-data file and got the following error
Code:
"sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper"

I then added a user-data block back to my user-data file:
Code:
    users:
      - name: ubuntu
        groups: [adm, sudo]
        lock-passwd: false
        sudo: ALL=(ALL) NOPASSWD:ALL
        shell: /bin/bash
        passwd: ubuntu
        # - or -
        ssh_authorized_keys:
            - public-key-goes-here
and everything went sqimmingly. I want to now make this a job on my gitlab so I can schedule it. Totally not necessary but a fun way to learn "devops". I just wanted to say thanks I was in a SILO as well for the last 24 hours, lol.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!