Raspberry Pi Zero 2 W

ยท 412 words ยท 2 minute read

Bootstrap Raspberry Pi Zero 2 W with Ubuntu using cloud-init. OTG configured as network card, WiFi, ssh-keys from GitHub and zram swap enabled. All examples are made to be run on a Fedora Desktop. Adjust scripts if you use another distro.

This blog post is written in the influence of a cold. Take care with your copy and pasting. Hope you enjoy this nifty way to bootstrap Ubuntu PI systems.

๐Ÿ‘ Tip! Fetch your public SSH keys from GitHub with

wget -O - https://github.com/username.keys

Before you begin ๐Ÿ”—

If you want to access your Pi over OTG, use connection sharing, for example in NetworkManger. Remember to open incoming ports for DHCP and DNS in your firewall.

  • 53
  • 67
  • 68

Write OS to SD card ๐Ÿ”—

Download Raspberry Ubuntu from Install Ubuntu on a Raspberry Pi. Why Ubuntu and not Raspberry Pi OS. Ubuntu contain btrfs kernel module in initramfs.

๐Ÿ˜ฏ Important! Do not copy and paste from the internet without examine the code. Ensure that your SD card is mounted on /dev/sdb or change all the scripts to your device.

sudo bash <<EOF
  xzcat ubuntu-22.04-preinstalled-server-arm64+raspi.img.xz | dd of=/dev/sdb bs=1M status=progress
EOF

Take out and put the SD card back into to the computer. Let the computer mount the file systems before you continue.

cloud-init ๐Ÿ”—

Export your WiFi network and password to shell variables.

export WIFI_NAME="internett"
export WIFI_PASS="password"

Write cloud-init configuration files.

sudo bash <<EOF
  cd /run/media/${USER}
  cat <<CONF > system-boot/user-data
#cloud-config
hostname: zero
password: ubuntu
ssh_pwauth: true
ssh_import_id:
  - gh:${USER}

write_files:
  - path: /boot/firmware/config.txt
    append: true
    content: |
      dtoverlay=dwc2
  - path: /etc/modules
    permissions: '0644'
    content: |
      dwc2
      g_ether
  - path: /etc/modprobe.d/g_ether.conf
    permissions: '0644'
    content: |
      options g_ether host_addr=42:00:00:00:00:02 dev_addr=42:00:00:00:00:01

packages:
  - linux-modules-extra-raspi
  - zram-tools
package_update: true

timezone: UTC

runcmd:
  - /usr/sbin/update-initramfs -u
  - /usr/bin/systemctl enable zramswap --now

power_state:
  delay: now
  mode: reboot
  message: reboot into new initramfs
  condition: true
CONF

  cat <<CONF > writable/etc/cloud/cloud.cfg.d/10-network.cfg
network:
  version: 2
  ethernets:
    usb0:
      dhcp4: true
      dhcp6: true
      optional: true
  wifis:
    wlan0:
      dhcp4: yes
      dhcp6: yes
      optional: true
      access-points:
        "${WIFI_NAME}":
            password: "${WIFI_PASS}"
CONF
EOF

Convert root file system to Btrfs ๐Ÿ”—

If you want to use Btrfs instead of default ext4.

sudo bash <<EOF
  cd /run/media/${USER}
  sed -i 's/ext4/btrfs/' system-boot/cmdline.txt
  sed -E -i 's|(.*)ext4.*|\1btrfs compress=zstd:1,noatime,nodiratime 0 0|' writable/etc/fstab
  umount system-boot writable
  btrfs-convert -p /dev/sdb2
EOF

Re-mount SD card and then run the script to clean up after conversion and compress existing operative system.

sudo bash <<EOF
  cd /run/media/${USER}
  btrfs sub del writable/ext2_saved
  btrfs fi def -czstd -r -v writable/
EOF