Add ubuntu-2404 example

- Uses new cd_files functionality to pass in autoinstall user-data, as
  Ubuntu 24.04 blocks for 5 minutes on first boot if a floppy is used
- Installs xen-guest-agent from APT as older xe-guest-utilities didn't
  seem to work on Ubuntu 24.04
- Waits for cloud-init to complete after first reboot before this plugin
  shuts down the VM to create the template
This commit is contained in:
Chris Hillery 2024-06-21 16:45:12 -07:00
parent 65ae17abd3
commit 9f5c74f363
3 changed files with 174 additions and 0 deletions

View File

View File

@ -0,0 +1,49 @@
#cloud-config
autoinstall:
version: 1
identity:
hostname: ubuntu-server
# This is the crypted pass of 'ubuntu'
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
username: testuser
ssh:
install-server: true
allow-pw: true
packages:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
package_upgrade: false
# late-commands are run at the very end of the *installer*, before the first
# reboot. However users are actually created by cloud-init after the first
# reboot. So these commands can't affect anything that requires a user to
# actually exist.
late-commands:
# Add xen-guest-agent - this is impossible (!) on Ubuntu 24.04 using
# the normal apt/packages autoinstall configuration, because (a) the
# xen-guest-agent apt repository does not use the "components" field
# (odd, but legal); (b) curtin's apt-config process hard requires
# deb822 .sources to have a "Components:" key, and (c) even if we
# try to configure it with a .list entry rather than .source entry
# under "apt:", on Ubuntu 24 curtin force-converts "deb" lines to
# deb822 format. I wasted days on this. Have to just do it the
# old-fashioned way here.
- 'echo "deb [trusted=yes] https://gitlab.com/api/v4/projects/xen-project%252Fxen-guest-agent/packages/generic/deb-amd64/ release/" > /target/etc/apt/sources.list.d/xen.list'
- curtin in-target --target /target -- apt-get update
- curtin in-target --target /target -- apt-get install -y xen-guest-agent
# If you want to do anything custom with files from "cd_files",
# uncomment these lines
#- mount /dev/sr1 /mnt
#- cp /mnt/my-cool.conf /target/etc/path/cool.conf
#- umount /mnt
# User-data is passed to the "real" cloud-init, which runs after the first
# reboot after the installer has finished. So you can run commands which
# modify users here, such as added 'testuser' to a new group.
#user-data:
# runcmd:
# - [ 'usermod', '-a', '-G', 'backup', 'testuser' ]

View File

@ -0,0 +1,125 @@
packer {
required_plugins {
xenserver= {
version = ">= v0.6.0"
source = "github.com/ddelnano/xenserver"
}
}
}
# The ubuntu_version value determines what Ubuntu iso URL and sha256 hash we lookup. Updating
# this will allow a new version to be pulled in.
data "null" "ubuntu_version" {
input = "24.04"
}
locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
ubuntu_version = data.null.ubuntu_version.output
# Update this map depending on what templates are available on your Xen server.
ubuntu_template_name = {
24.04 = "Ubuntu Focal Fossa 20.04"
}
}
# TODO(ddelnano): Update this to use a local once https://github.com/hashicorp/packer/issues/11011
# is fixed.
data "http" "ubuntu_sha_and_release" {
url = "https://releases.ubuntu.com/${data.null.ubuntu_version.output}/SHA256SUMS"
}
local "ubuntu_sha256" {
expression = regex("([A-Za-z0-9]+)[\\s\\*]+ubuntu-.*server", data.http.ubuntu_sha_and_release.body)
}
variable "remote_host" {
type = string
description = "The ip or fqdn of your XenServer. This will be pulled from the env var 'PKR_VAR_remote_host'"
sensitive = true
default = null
}
variable "remote_password" {
type = string
description = "The password used to interact with your XenServer. This will be pulled from the env var 'PKR_VAR_remote_password'"
sensitive = true
default = null
}
variable "remote_username" {
type = string
description = "The username used to interact with your XenServer. This will be pulled from the env var 'PKR_VAR_remote_username'"
sensitive = true
default = null
}
variable "sr_iso_name" {
type = string
description = "The name of the SR packer will use to store the installation ISO"
default = ""
}
variable "sr_name" {
type = string
description = "The name of the SR packer will use to create the VM"
default = ""
}
source "xenserver-iso" "ubuntu-2404" {
iso_checksum = "sha256:${local.ubuntu_sha256.0}"
iso_url = "https://releases.ubuntu.com/${local.ubuntu_version}/ubuntu-${local.ubuntu_version}-live-server-amd64.iso"
sr_name = var.sr_name
sr_iso_name = var.sr_iso_name
remote_host = var.remote_host
remote_password = var.remote_password
remote_username = var.remote_username
clone_template = local.ubuntu_template_name[data.null.ubuntu_version.output]
vm_name = "ubuntu-${data.null.ubuntu_version.output}-gold"
vm_description = "Built at ${local.timestamp}"
vm_memory = 8192
disk_size = 10240
vcpus_max = 4
vcpus_atstartup = 4
boot_command = [
"c<wait5>",
"set gfxpayload=keep<enter>",
"linux /casper/vmlinuz autoinstall ---<enter>",
"initrd /casper/initrd<enter>",
"boot<enter>"
]
cd_files = [
"examples/http/ubuntu-2404/meta-data",
"examples/http/ubuntu-2404/user-data"
]
# The xenserver plugin needs to SSH in to the new VM, so we give it
# the information to do so
ssh_username = "testuser"
ssh_password = "ubuntu"
ssh_wait_timeout = "60000s"
ssh_handshake_attempts = 10000
output_directory = null
keep_vm = "always"
}
build {
sources = ["xenserver-iso.ubuntu-2404"]
# Things to do on the new VM once it's past first reboot:
# Wait for cloud-init to finish everything. We need to do this as a
# packer provisioner to prevent packer-plugin-xenserver from shutting
# the VM down before all cloud-init processing is complete.
provisioner "shell" {
inline = [
"echo ubuntu | sudo -S cloud-init status --wait"
]
valid_exit_codes = [0, 2]
}
}