Ubuntu Focal headless setup on Raspberry pi 4 - cloud init wifi initialisation before first reboot [closed]
Asked Answered
O

1

9

i'm having trouble in setting up a full headless install for Ubuntu Server Focal (ARM) on a Raspberry pi 4 using cloud init config. The whole purpose of doing this is to simplify the SD card swap in case of failure. I'm trying to use cloud-init config files to apply static config for lan/wlan, create new user, add ssh authorized keys for the new user, install docker etc. However, whatever i do it seems the Wifi settings are not applied before the first reboot.

Step1: burn the image on SD Card. Step2: rewrite SD card system-boot/network_config and system-boot/user-data with config files

network-config

version: 2
renderer: networkd
ethernets:
  eth0:
    dhcp4: false
    optional: true
    addresses: [192.168.100.8/24]
    gateway4: 192.168.100.2
    nameservers:
      addresses: [192.168.100.2, 8.8.8.8]
wifis:
  wlan0:
    optional: true
    access-points:
      "AP-NAME":
        password: "AP-Password"
    dhcp4: false
    addresses: [192.168.100.13/24]
    gateway4: 192.168.100.2
    nameservers:
      #search: [mydomain, otherdomain]
      addresses: [192.168.100.2, 8.8.8.8]

user-data

chpasswd:
  expire: true
  list:
    - ubuntu:ubuntu

# Enable password authentication with the SSH daemon
ssh_pwauth: true
groups:
  - myuser
  - docker
users:
  - default
  - name: myuser
    gecos: My Name
    primary_group: myuser
    groups: sudo
    shell: /bin/bash
    ssh_authorized_keys:
      - ssh-rsa AAAA....
    lock_passwd: false
    passwd: $6$rounds=4096$7uRxBCbz9$SPdYdqd...

packages:
  - apt-transport-https
  - ca-certificates
  - curl
  - gnupg-agent
  - software-properties-common
  - git
runcmd:
  - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
  - add-apt-repository "deb [arch=arm64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  - apt-get update -y
  - apt-get install -y docker-ce docker-ce-cli containerd.io
  - systemctl start docker
  - systemctl enable docker
## TODO: add git deployment and configure folders
power_state:
  mode: reboot

During the first boot cloud-init always applies the fallback network config.

I also tried to apply the headless config for wifi as described here.

Created wpa_supplicant.conf and copied it to SD system-boot folder.

trl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=RO

network={
    ssid="AP-NAME"
    psk="AP-Password"
}

Also created an empty ssh file and copied it to system-boot

The run commands always fail since during the first boot cloud-init applies the fallback network config. After reboot, lan/wlan settings are applied, the user is created, ssh authorized keys added. However i still need to ssh into the PI and install install the remaining packages: docker etc, and i wanted to avoid this. Am i doing something wrong?

Outdoor answered 16/7, 2020 at 8:38 Comment(3)
Hey did you ever solve this?Solander
Im also having issues with this. Its specific to using Wifi as only network connection. Everything works with Ethernet port as primaryTrelu
@Solander nope, i will test to see if chd recommendation for using the ethernet port as primary works.Outdoor
S
6

I'm not sure if you ever found a workaround, but I'll share some information I found when researching options.

Note: During the first boot, your Raspberry Pi will try to connect to this network. It will fail the first time around. Simply reboot sudo reboot and it will work.

There's an interesting workaround & approach in this repo.

  • It states it was created for 18.04, but it should work with 20.04 as both Server versions use netplan and systemd-networkd.

Personally, I've gone a different route.

  • I create custom images that contain my settings & packages, then burn to uSD or share via a TFTP server. I was surprised at how easy this was.
    • There's a good post on creating custom images here
    • Some important additional info is here
Susceptive answered 29/12, 2020 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.