Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu Focal headless setup on Raspberry pi 4 - cloud init wifi initialisation before first reboot

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?

like image 496
DragosN Avatar asked Jul 16 '20 08:07

DragosN


People also ask

Is there a Raspi config for Ubuntu?

raspi-config is a powerful tool when changing settings for the Raspberry Pi. Unfortunately, it is only available on Raspberry Pi OS (formerly called Raspbian), and can only be installed on that. It is available on Ubuntu MATE 20.04 LTS Focal Fossa, but it stopped appearing on Groovy Gorilla or Hirsute Hippo.

How do I change my OS from Raspberry Pi to Ubuntu?

Once this is done, start the Imager and open the “CHOOSE OS” menu. Scroll down the menu click “Other general-purpose OS”. You will then be able to see a list of Ubuntu downloads to choose from. Select the “Ubuntu 20.10 Desktop (Raspberry Pi 4)” option.

Can Raspberry Pi 4 install Linux?

The Raspberry Pi 4 has a 64-bit processor and can run 64-bit images. Because it can run 64-bit images, you can choose either Kali Linux RaspberryPi 2, 3, 4 and 400 (img. xz) or Kali Linux RaspberryPi 2 (v1. 2), 3, 4 and 400 (64-Bit) (img.


1 Answers

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

  • Ubuntu's Raspberry Pi WiFi Setup Page states the need for a reboot when using network-config with WiFi:

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
like image 99
azrobbo Avatar answered Nov 08 '22 08:11

azrobbo