Fixing Kernel Panic in Ubuntu Packer Builds

When building an Ubuntu image using Packer I was building out my own .pkr.hcl template file. Everything seemed to be fine, except I could never get the build to boot into the cloud-init and start installing the operating system.

The VM was built and after the boot command was entered the VM would show a Kernel panic error like below.

Kernel panic - not syncing: No working init found.

After a lot of trial and error I finally found the extremely simple solution. I wasn't giving the VM enough RAM. After adding the following to the source declaration the VM booted up and auto installed using my cidata files.

1  cpus = 2
2  memory = 2048

Here is the full ubuntu2204-vmware.pkr.hcl file that worked for me:

 1packer {
 2  required_version = ">= 1.7.0"
 3  required_plugins {
 4    vmware = {
 5      version = ">= 1.0.0"
 6      source  = "github.com/hashicorp/vmware"
 7    }
 8  }
 9}
10
11source "vmware-iso" "ubuntu" {
12  boot_wait   = "10s"
13  ssh_timeout = "20m"
14  boot_command = [
15    "<wait>e<down><down><down><end>",
16    " autoinstall ds=nocloud;",
17    "<F10>",
18  ]
19  cd_files = [
20    "./cidata/meta-data",
21    "./cidata/user-data"
22  ]
23  cd_label = "cidata"
24
25  iso_url          = "./ISO/ubuntu-22.04.2-live-server-amd64.iso"
26  iso_checksum     = "5e38b55d57d94ff029719342357325ed3bda38fa80054f9330dc789cd2d43931"
27  ssh_username     = "vagrant"
28  ssh_password     = "vagrant"
29  shutdown_command = "sudo shutdown -P now"
30  // cpus             = 2
31  // memory           = 2048
32}
33
34build {
35  name = "packer-ubuntu2204"
36  sources = [
37    "sources.vmware-iso.ubuntu"
38  ]
39
40  post-processor "vagrant" {
41
42  }
43}

Here is the associated user-data file, it uses the Vagrant insecure public key and credentials vagrant:vagrant:

  1#cloud-config
  2autoinstall:
  3  version: 1
  4  early-commands:
  5    - sudo systemctl stop ssh
  6  apt:
  7    disable_components: []
  8    geoip: true
  9    preserve_sources_list: false
 10    primary:
 11    - arches:
 12      - amd64
 13      - i386
 14      uri: http://us.archive.ubuntu.com/ubuntu
 15    - arches:
 16      - default
 17      uri: http://ports.ubuntu.com/ubuntu-ports
 18  drivers:
 19    install: false
 20  identity:
 21    hostname: ubuntu
 22    password: $6$uNphLrAzLra/kNRo$L1umupXWSPFHA34UiYGHWzC4paSr/Ru9lw8JKMXKd48sVHUT0W0S8hv0n.C2.bHHXbfxSiwt0gXbOXUkIeF0Q.
 23    realname: Vagrant
 24    username: vagrant
 25  kernel:
 26    package: linux-generic
 27  keyboard:
 28    layout: us
 29    toggle: null
 30    variant: ''
 31  locale: en_US.UTF-8
 32  network:
 33    ethernets:
 34      ens33:
 35        dhcp4: true
 36    version: 2
 37  source:
 38    id: ubuntu-server
 39    search_drivers: false
 40  ssh:
 41    allow-pw: true
 42    authorized-keys: [
 43      "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key"
 44    ]
 45    install-server: true
 46  storage:
 47    config:
 48    - ptable: gpt
 49      path: /dev/sda
 50      wipe: superblock-recursive
 51      preserve: false
 52      name: ''
 53      grub_device: true
 54      type: disk
 55      id: disk-sda
 56    - device: disk-sda
 57      size: 1048576
 58      flag: bios_grub
 59      number: 1
 60      preserve: false
 61      grub_device: false
 62      offset: 1048576
 63      type: partition
 64      id: partition-0
 65    - device: disk-sda
 66      size: 1902116864
 67      wipe: superblock
 68      number: 2
 69      preserve: false
 70      grub_device: false
 71      offset: 2097152
 72      type: partition
 73      id: partition-1
 74    - fstype: ext4
 75      volume: partition-1
 76      preserve: false
 77      type: format
 78      id: format-0
 79    - device: disk-sda
 80      size: 19569573888
 81      wipe: superblock
 82      number: 3
 83      preserve: false
 84      grub_device: false
 85      offset: 1904214016
 86      type: partition
 87      id: partition-2
 88    - name: ubuntu-vg
 89      devices:
 90      - partition-2
 91      preserve: false
 92      type: lvm_volgroup
 93      id: lvm_volgroup-0
 94    - name: ubuntu-lv
 95      volgroup: lvm_volgroup-0
 96      size: 10737418240B
 97      wipe: superblock
 98      preserve: false
 99      type: lvm_partition
100      id: lvm_partition-0
101    - fstype: ext4
102      volume: lvm_partition-0
103      preserve: false
104      type: format
105      id: format-1
106    - path: /
107      device: format-1
108      type: mount
109      id: mount-1
110    - path: /boot
111      device: format-0
112      type: mount
113      id: mount-0
114  updates: security
115  late-commands:
116    - echo 'vagrant ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu
117    - curtin in-target --target=/target -- chmod 440 /etc/sudoers.d/ubuntu