Skip to content

Cloud Init

Helge Heß edited this page Sep 20, 2018 · 1 revision

If you want to assign a different hostname, install an SSH key, setup packages or other more advanced stuff before flashing the image to the SD card, you can use a user-data.yml configuration file. You can read more about that in the Hypriot 1.8.0 Release Notes.

To use a config file, simply run Hypriot flash in the same directory, or pass over the configuration file using the --userdata option, e.g.:

helge@ZeeMBP zpi3b $ ~/bin/flash \
   --userdata ./user-data.yml \
   https://github.com/DieterReuter/image-builder-rpi64/releases/download/v20180429-184538/hypriotos-rpi64-v20180429-184538.img.zip

For example I used this user-data.yml to assign a static IP address to ...

... my Raspi 3b 32-bit:

#cloud-config
# vim: syntax=yaml
#

# The current version of cloud-init in the Hypriot rpi-64 is 0.7.9
# When dealing with cloud-init, it is SUPER important to know the version
# I have wasted many hours creating servers to find out the module I was trying to use wasn't in the cloud-init version I had
# Documentation: http://cloudinit.readthedocs.io/en/0.7.9/index.html

# Set your hostname here, the manage_etc_hosts will update the hosts file entries as well
hostname: zpi3
manage_etc_hosts: true

# This expands the root volume to the entire SD Card, similar to what the raspbian images did on first boot.
# This doesn't seem to be required, its more here for posterity in understanding what is going on
resize_rootfs: true
growpart:
    mode: auto
    devices: ["/"]
    ignore_growroot_disabled: false

# You could modify this for your own user information
users:
  - name: pirate
    gecos: "Hypriot Pirate"
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    groups: users,docker,video
    plain_text_passwd: hypriot
    lock_passwd: false
    ssh_pwauth: true
    chpasswd: { expire: false }

# Update our packages on first boot, saves us some time
package_update: true
package_upgrade: true
package_reboot_if_required: true

# Install any additional packages you need here
# I add ntp because.. without it, rpi is useless in keeping track of time.
packages:
  - ntp

# Set the locale of the system
locale: "en_US.UTF-8"

# Set the timezone
# Value of 'timezone' must exist in /usr/share/zoneinfo
timezone: "Europe/Berlin"

# Tell docker to tag this node appropriately
# Currently we need the experimental?
write_files:
    - path: "/etc/docker/daemon.json"
      owner: "root:root"
      content: |
        {
          "labels": [ "os=linux", "arch=arm64" ],
          "experimental": true
        }
# Static IP address
write_files:
  - content: |
      persistent
      # Generate Stable Private IPv6 Addresses instead of hardware based ones
      slaac private
      # static IP configuration:
      interface eth0
      static ip_address=192.168.2.40/24
      # static ip6_address=fd51:42f8:caae:d92e::ff/64
      static routers=192.168.2.1
      static domain_name_servers=192.168.2.1 8.8.8.8
    path: /etc/dhcpcd.conf

# These commands will be ran once on first boot only
runcmd:
  # Pickup the hostname changes
  - [ systemctl, restart, avahi-daemon ]

... my Raspi 3b+ 64-bit:

#cloud-config
# vim: syntax=yaml
#

# The current version of cloud-init in the Hypriot rpi-64 is 0.7.9
# When dealing with cloud-init, it is SUPER important to know the version
# I have wasted many hours creating servers to find out the module I was trying to use wasn't in the cloud-init version I had
# Documentation: http://cloudinit.readthedocs.io/en/0.7.9/index.html

# Set your hostname here, the manage_etc_hosts will update the hosts file entries as well
hostname: zpi3b
manage_etc_hosts: true

# This expands the root volume to the entire SD Card, similar to what the raspbian images did on first boot.
# This doesn't seem to be required, its more here for posterity in understanding what is going on
resize_rootfs: true
growpart:
    mode: auto
    devices: ["/"]
    ignore_growroot_disabled: false

# You could modify this for your own user information
users:
  - name: pirate
    gecos: "Hypriot Pirate"
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    groups: users,docker,video
    plain_text_passwd: hypriot
    lock_passwd: false
    ssh_pwauth: true
    chpasswd: { expire: false }

# Update our packages on first boot, saves us some time
package_update: true
package_upgrade: true
package_reboot_if_required: true

# Install any additional packages you need here
# I add ntp because.. without it, rpi is useless in keeping track of time.
packages:
  - ntp

# Set the locale of the system
locale: "en_US.UTF-8"

# Set the timezone
# Value of 'timezone' must exist in /usr/share/zoneinfo
timezone: "Europe/Berlin"

# Tell docker to tag this node appropriately
# Currently we need the experimental?
write_files:
    - path: "/etc/docker/daemon.json"
      owner: "root:root"
      content: |
        {
          "labels": [ "os=linux", "arch=arm64" ],
          "experimental": true
        }
# Static IP address
write_files:
  - content: |
      persistent
      # Generate Stable Private IPv6 Addresses instead of hardware based ones
      slaac private
      # static IP configuration:
      interface eth0
      static ip_address=192.168.2.42/24
      # static ip6_address=fd51:42f8:caae:d92e::ff/64
      static routers=192.168.2.1
      static domain_name_servers=192.168.2.1 8.8.8.8
    path: /etc/dhcpcd.conf

# These commands will be ran once on first boot only
runcmd:
  # Pickup the hostname changes
  - [ systemctl, restart, avahi-daemon ]
Clone this wiki locally