Skip to content

cloud-init for VMs — modules, datasources, and debugging in depth

A deep operator-grade walk through cloud-init — datasources, user-data, vendor-data, the module pipeline, and how to debug a guest that booted but didn't configure.

cloud-init is the standard first-boot configuration mechanism for Linux VMs in clouds and on hypervisors. It runs as a set of systemd services on the guest, pulls configuration from a “datasource” (a metadata service, a NoCloud seed ISO, an OpenStack config drive), and applies a sequence of modules — networking, users, packages, files, scripts. Most operators have a passing acquaintance with #cloud-config; this article goes deeper into how cloud-init actually works so you can diagnose a guest that booted but didn’t configure itself.

How to verify

On a running guest, cloud-init exposes its state in /run/cloud-init/ and a per-boot log in /var/log/cloud-init.log. The CLI summarizes the result.

cloud-init status --long
cloud-init analyze show
sudo cat /run/cloud-init/result.json
sudo journalctl -u cloud-init-local -u cloud-init -u cloud-config -u cloud-final --no-pager
sudo cloud-init query --all | head -40

cloud-init analyze blame is gold — it lists modules by how long they took. A first-boot that took 8 minutes because package_update: true pulled a slow mirror shows up immediately.

What’s happening

cloud-init runs in four stages as four systemd units. cloud-init-local runs early in boot, after the rootfs is mounted but before network is up — it identifies the datasource (often by reading a config drive or asking the kernel about devices). cloud-init runs after network is up and fetches the user-data and metadata. cloud-config runs the bulk of the modules — users, packages, files, runcmd. cloud-final runs last-stage modules like power-state and the final phase of runcmd.

The datasource is the abstraction over how config arrives. EC2’s instance metadata service, Azure’s IMDS, GCE’s metadata server, OpenStack config drive, NoCloud (a seed ISO or filesystem with user-data and meta-data files) — each is a datasource. cloud-init probes a configured list (/etc/cloud/cloud.cfg.d/) in order and picks the first that responds. user-data is what the operator provides (the #cloud-config document or a shell script). vendor-data is what the platform provides (e.g. AWS adds AMI-specific config). meta-data is identity info (instance ID, region, hostname).

The procedure

  1. Pin the datasource. Don’t rely on probing — set it explicitly. On a libvirt host using a seed ISO, force NoCloud:

    # /etc/cloud/cloud.cfg.d/99-nocloud.cfg (baked into the image)
    datasource_list: [ NoCloud, None ]
    datasource:
      NoCloud:
        fs_label: cidata

    The seed ISO must have the label cidata (or CIDATA) for cloud-init to mount it. cloud-localds (from cloud-image-utils) produces correctly-labeled ISOs.

  2. A complete user-data example — what you actually write. This is the level of detail that a production guest needs on first boot:

    #cloud-config
    hostname: web01
    fqdn: web01.dc1.example.com
    manage_etc_hosts: true
    
    users:
      - name: ops
        groups: [sudo, adm]
        shell: /bin/bash
        sudo: ALL=(ALL) NOPASSWD:ALL
        lock_passwd: true
        ssh_authorized_keys:
          - ssh-ed25519 AAAA... ops@bastion
    
    ssh_pwauth: false
    disable_root: true
    
    package_update: true
    package_upgrade: false   # leave to config-mgmt
    packages:
      - nginx
      - ufw
    
    write_files:
      - path: /etc/nginx/sites-available/health
        owner: root:root
        permissions: '0644'
        content: |
          server { listen 80; location /health { return 200 "ok\n"; } }
    
    runcmd:
      - ln -sf /etc/nginx/sites-available/health /etc/nginx/sites-enabled/health
      - systemctl reload nginx
      - ufw allow OpenSSH
      - ufw --force enable
    
    power_state:
      mode: reboot
      condition: True
  3. Networking — network cloud-config vs Netplan. cloud-init can write /etc/netplan/50-cloud-init.yaml from the network: user-data block. For static IPs on a hypervisor without DHCP, this is the right path; for cloud images where the datasource provides networking, leave it alone:

    network:
      version: 2
      ethernets:
        enp1s0:
          addresses: [10.0.10.21/24]
          routes:
            - to: default
              via: 10.0.10.1
          nameservers:
            addresses: [1.1.1.1, 8.8.8.8]
  4. Debug a guest that booted but didn’t configure. First, did cloud-init run at all? Check cloud-init status. If disabled, look at /etc/cloud/cloud-init.disabled and the kernel command-line for cloud-init=disabled. If error, check the log for the failing module. Reset and rerun (this is destructive on a running prod system — use only when triaging a fresh guest):

    sudo cloud-init clean --logs --seed
    sudo cloud-init init --local
    sudo cloud-init init
    sudo cloud-init modules --mode=config
    sudo cloud-init modules --mode=final
  5. Stage your modules — the order matters. Modules run in three groups: cloud_init_modules (early), cloud_config_modules (most of them), cloud_final_modules (late). Editing /etc/cloud/cloud.cfg lets you change the order — useful when you need a package installed before a runcmd that uses it.

  6. Multi-document user-data with mime parts. When you need both #cloud-config and a shell script, wrap them in a multi-part MIME archive. The make-mime helper (or cloud-init devel make-mime) builds it:

    cloud-init devel make-mime -a config.yaml:cloud-config -a setup.sh:x-shellscript > user-data
  7. Validate before booting. cloud-init schema --config-file user-data checks syntax and known keys. CI should run this on every change.

Operational notes

  • A guest that boots, gets to a login prompt, but never received its keys means cloud-init’s datasource probe failed silently — usually a missing or mislabeled seed ISO. Check /run/cloud-init/result.json.
  • package_update: true runs apt update (or yum equivalent) and is slow on a fresh image; if you preinstalled packages via Packer, set it to false and rely on config management for ongoing updates.
  • cloud-init runs once per “instance ID” — cloning a VM and not resetting the instance ID means cloud-init thinks it’s already done. Either cloud-init clean before cloning or accept that clones are pre-configured.
  • The runcmd runs once on first boot only; bootcmd runs on every boot. Operators sometimes pick the wrong one and wonder why their script ran once and never again.
  • Combining power_state: reboot with a runcmd that itself reboots leaves cloud-init in an ambiguous state; pick one mechanism.

Stack Harbor treats cloud-init user-data as code — templated by Ansible, validated in CI, versioned in the same repo as the application. Clients whose VMs were configured by hand after boot (“we SSH in and run a script”) get those VMs templated and the boot-time config moved into cloud-init as part of Managed Operations. See also libvirt + cloud-init combined for the libvirt-specific recipe.