Score:0

Dynamic Hostnames from DHCP in Autoinstall

sg flag

In the user_data autoinstall I have being served via tftp, the hostname field is required. I'm using statically mapped dhcp entries to match machine to hostname, ip, etc.

How can I get the autoinstall hostname to accept the value from dhcp on pxe boot?

Thanks

Score:0
jp flag

I do not believe there is any way to make subiquity use the hostname in the DHCP offer. However, there are a number of ways that early-commands and late-commands could be used to do what you want.

My suggested solution is a user-data autoinstall file similar to this snippet. It will read the hostname in the DHCP offer from the DHCP lease file and overwrite /etc/hostname on the installed system.

#cloud-config
autoinstall:
  # NO identity section
  #identity:
  user-data:
    users:
    - name: ubuntu
      passwd: REDACTED
      shell: /bin/bash
      lock-passwd: false
      sudo: ALL=(ALL) NOPASSWD:ALL
      groups: users
  late-commands:
    - |
      bash <<'EOF'
        source <(cat /run/systemd/netif/leases/* | grep ^HOSTNAME=)
        [[ -n "${HOSTNAME}" ]] && echo "${HOSTNAME}" > /target/etc/hostname
      EOF
      true

These are some gotchas to watch out for no matter what approach you try.

  • The autoinstall identity and user-data sections do not co-exist well. My preference is to use a user-data section and to not include an identity section. Because the only provided way to configure the hostname is the identity section another method must be used to configure the hostname (e.g. late-commands).
  • If there is no identity section then subiquity changes behavior. The generated cloud-init configuration (in /target/etc/cloud/cloud.cfg.d/99-installer.cfg) will be different. There will be no /target/etc/hosts content generated and the file will be left empty. There are potentially other differences.
  • Conversely, if there is an identity section then the provided hostname will be configured in multiple files. In /target/etc/hosts and potentially others.

see also

notes

  • I tested using Ubuntu 22.04 (subiquity 22.04.2).
I sit in a Tesla and translated this thread with Ai:

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.