Score:0

assign ip to ubuntu 20.04 during installation (packer/cloud-init context)

cn flag

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.

Score:2
cn flag

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 server-ip (which is for nfs - not needed), then the hostname, 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 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 :)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.