Skip to content

Instantly share code, notes, and snippets.

@mikaelhg
Created July 10, 2020 13:40
Show Gist options
  • Save mikaelhg/e060976d8e5bf7c4d42429a12bd11633 to your computer and use it in GitHub Desktop.
Save mikaelhg/e060976d8e5bf7c4d42429a12bd11633 to your computer and use it in GitHub Desktop.
Using Packer to provision Amazon Linux 2 without seed.iso

(I also reported this to AWS: awsdocs/amazon-ec2-user-guide#123)

The "Running Amazon Linux 2 as a virtual machine onpremises" page describes a fairly cumbersome way of running Amazon Linux 2 in local virtual machines through using various tools to provision ISO9660 seed.iso files just to serve the VM instance two small data files.

It would be great it the documentation also pointed out that since the VM provisioning is being done with cloud-init, and the image has configured a fairly extensive datasource_list: [ NoCloud, AltCloud, ConfigDrive, OVF, None ] which starts with NoCloud, that NoCloud also allows you to serve these files over HTTP.

There are two easy ways of using network configuration instead of seed.iso. Either you tell GRUB to add a parameter to the kernel boot configuration, or you tell KVM/VMWare/Virtualbox to set the virtual machine's SMBIOS value to something which cloud-init's NoCloud understands.

The documentation page https://cloudinit.readthedocs.io/en/latest/topics/datasources/nocloud.html describes the parameters.

If you wish to use Packer, for example, to provision local KVM qcow2 files of AL2, you'd use a configuration which looks a bit like this:

{
  "builders": [
    {
      "vm_name": "alpha",
      "type": "qemu",
      "accelerator": "kvm",
      "qemu_binary": "/usr/local/bin/qemu5.0.0-system-x86_64",
      "qemuargs": [
        ["-display", "none"],
        ["-smbios", "type=1,serial=ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/"]
      ],
      "format": "qcow2",
      "iso_url": "images/amzn2-kvm-2.0.20200602.0-x86_64.xfs.gpt.qcow2",
      "iso_checksum": "sha256:1ca3af1df04dd9c46240414be3fbc024cdd1fab5fe9b00a975614f24e682da85",
      "disk_image": true,

      "ssh_handshake_attempts": "20",
      "ssh_username": "ec2-user",

      "net_device": "virtio-net",
      "disk_interface": "virtio",

      "http_directory": "httpserver",
      "output_directory": "output"

    }
  ]
}

the sillier way to achieve the same is to tell your VM creator to send keypresses to the virtual terminal which is booting up AL2 for the first time:

"boot_wait": "1s",
"boot_command": [
    "<shift><shift><wait><wait>e<down><down><down><down><down><down><down><down><down><down><down><down><left>",
    " ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/",
    "<leftCtrlOn>x<leftCtrlOff>"
  ]
@bootswithdefer
Copy link

bootswithdefer commented Jun 29, 2023

With your pointers, got it working on Packer 1.9.1.

amzn2.pkr.hcl

source "qemu" "amzn2-qemu" {
  accelerator      = "none"
  cpus             = 2
  disk_size        = "32G"
  http_directory   = "scripts-amzn2-qemu"
  disk_image       = true
  iso_url          = "https://cdn.amazonlinux.com/os-images/2.0.20230612.0/kvm/amzn2-kvm-2.0.20230612.0-x86_64.xfs.gpt.qcow2"
  iso_checksum     = "file:https://cdn.amazonlinux.com/os-images/2.0.20230612.0/kvm/SHA256SUMS"
  memory           = 1024
  output_directory = "builds-amzn2-qemu"
  ssh_username     = "ec2-user"
  ssh_password     = "password"
  vm_name          = "amzn2-qemu"
  qemuargs = [
    ["-display", "none"],
    ["-smbios", "type=1,serial=ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/seedconfig/"]
  ]
}

scripts-amzn2-qemu/seedconfig/user-data

#cloud-config
password: password
users:
  - default
chpasswd:
  list: |
    ec2-user:password
ssh_pwauth: True

scripts-amzn2-qemu/seedconfig/meta-data

local-hostname: packer
network-interfaces: |
  auto eth0
  iface eth0 inet dhcp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment