1

I am trying to provision Ubuntu 20.04 through packer and I am trying to use packer's http server to provide the cloud-init configuration. On Ubuntu 18.04 I could do that by preseeding the ip using d-i (debian-installer), which in the mean time has become obsolete, it seems, but I don't know how I can do the same with Ubuntu 20. As far as I understand, Ubuntu 20.04 can use dhcp, but I'm interested in assigning a static IP, so that the VM can connect to the http server. Any ideas how I can achieve this? Or is the approach maybe wrong?

This is what what packer runs during the boot process (before the actual installer):

       "/casper/vmlinuz/ ",
        "initrd=/casper/initrd debug= ",
        "autoinstall ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ ",
        "--- <enter>"

The OS freezes at the "Host and Network Name Lookups" target (just before cloud-init is loaded/initialised). This is the content of cloud-init:

#cloud-config
autoinstall:
  version: 1
  locale: en_US
  keyboard:
    layout: fr
  ssh:
    install-server: true
    allow-pw: true
  packages:
    - qemu-guest-agent
  storage:
    layout:
      name: direct
    swap:
      size: 0
  user-data:
    package_upgrade: true
    timezone: Europe/Paris
    users:
      - name: username
        passwd: $6$xyz$1D0kz5pThgRWqxWw6JaZy.6FdkUCSRndc/PMtDr7hMK5mSw7ysChRdlbhkX83PBbNBpqXqef3sBkqGw3Rahs..
        groups: [adm, cdrom, dip, plugdev, lxd, sudo]
        lock-passwd: false
        sudo: ALL=(ALL) NOPASSWD:ALL
        shell: /bin/bash
        ssh_authorized_keys:
          - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJEXrziuUOCpWPvwOsGuF4K+aq1ufToGMi4ra/1omOZb

(the password hash is found publicly on the internet, please don't discuss that:) )

This is not strictly related to packer, actually. I would simply like to know how I can make sure a Ubuntu 20.04 installation can connect remotely to a web server to retrieve the cloud-init configuration. That's basically it :)

Thanks! My setup is proxmox 6.4-9, packer 1.7.4 (running on apple m1) and Ubuntu 20.04.3

What I did afterwards was add this to the kernel cli:

ip=10.88.88.159:10.88.88.126:255.255.255.0::eth0:off autoinstall ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/

I'm using eth0, because this seems to be the name of the interface before being changed into ens18. However, the installer keeps complaining saying that it doesn't recognize the device. Of course I've tried both names to no avail.

Lethargos
  • 150

1 Answers1

4

I was able to solve it by adding the ip= directive in the kernel command line. This is what boot_command looks like now:

      "boot_command": [
        "<esc><enter><f6><esc><wait> ",
        "<bs><bs><bs><bs><bs>",
        "ip={{ user `vm_ip` }}::{{ user `vm_gateway` }}:{{ user `vm_netmask` }}::::{{ user `vm_dns` }} ",
        "autoinstall ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ ",
        "--- <enter>"
      ]

This is the syntax:

client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>:
   <dns0-ip>:<dns1-ip>:<ntp0-ip>:...

Reference: https://git.kernel.org/pub/scm/libs/klibc/klibc.git/tree/usr/kinit/ipconfig/README.ipconfig

So I skipped the following: server-ip (which is for nfs - not needed), then the hostname, the device (which is the network interface - don't need it, it only led to more confusion on my part) and autoconf, and I added dns0 after the gateway's netmask. For example:

ip=192.168.0.20::192.168.0.1:255.255.255.0::::8.8.8.8

So for anyone interested out there, the full command would be (dummy IPs):

initrd=/casper/initrd quiet ip=192.168.0.20::192.168.0.1:255.255.255.0::::8.8.8.8 autoinstall ds=nocloud-net;s=http://192.168.0.50:80 ---

--- is important in that whatever is written afterwards persists in the proc cmdline (/proc/cmdline), so it means it will be run over and over at boot time. You probably don't want to do that with autoinstall and the network configuration (which is going to be copied into cloud-init automatically, anyway, so that persists anyhow).

Maybe this is going to help someone. There's much I still need to understand myself :)

Update:
for Ubuntu 22.04 I've updated the boot_command as such:

      "boot_command": [
        "<e><bs><down><down><down>",
        "<right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right>",
        "<spacebar>",
        "ip={{ user `vm_ip` }}::{{ user `vm_gateway` }}:{{ user `vm_netmask` }}::::{{ user `vm_dns` }} ",
        "autoinstall 'ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/' ",
        "<F10>"
      ]

The main difference is that I'm placing ds=nocloud... in quotes and that I'm editing the grub config directly (so it's a slightly different approach. F6 didn't seem to work at first glance, so I simply didn't bother further).

Lethargos
  • 150
  • This solution is very effective when DHCP is not available, but it would be preferable to replace the sequence with a single to move the cursor to the right. – MaxiReglisse Sep 08 '23 at 15:25
  • Then there's a question: how did you declare and define vm_ip, vm_gateway, etc. variables? – MaxiReglisse Sep 08 '23 at 15:27
  • Well, just like you'd define any variable in packer: https://developer.hashicorp.com/packer/docs/templates/legacy_json_templates/user-variables

    In the meantime I've switched to hcl and to ubuntu ova cloud images. Quite a different setup :)

    – Lethargos Sep 08 '23 at 15:56