Skip to content

First boot with cloud-init ​

Cloud disks have no users and no network setup until something tells them what to do. vz hands each node a cloud-init seed on its first boot: users, passwords, SSH keys, packages, network settings, files and commands. You write the seed; vz packs it and attaches it to the VM.

The init key ​

init points at a user-data file or at a seed folder, relative to the lab file:

yaml
nodes:
  - name: vm1
    disks:
      - { path: ~/.vz/images/debian-13-genericcloud-arm64.raw, overlay: true }
    init: ./init/vm1          # a folder; a single file also works
init isvz uses
a filethe file as user-data, and writes meta-data for you
a folderuser-data, meta-data, network-config and vendor-data from the folder, when present. user-data is required. Other files are ignored.

When the seed has no meta-data, vz writes one:

yaml
instance-id: <lab>-<node>
local-hostname: <node>

The instance ID depends only on the lab and node names, so it stays the same across restarts.

How the seed reaches the guest ​

vz builds <state>/<node>/cidata.iso with hdiutil, labelled cidata (the cloud-init NoCloud data source), and attaches it as a read-only virtio disk after all of the node's own disks. cloud-init in the guest finds it there.

This is how init works on the default generic platform. On platform: apple the same #cloud-config file is read by vz on the Mac instead, since macOS has no cloud-init: the first user becomes the account macOS creates on its first boot, and ssh_pwauth turns Remote Login on or off. No seed disk is attached. See macOS guests.

The seed is rebuilt every time the lab starts, so edits to your seed files are picked up by the next vz up. Whether cloud-init acts on them is up to cloud-init: see Run cloud-init again.

A complete seed ​

This is the seed of the single VM example.

init/vm1/user-data:

yaml
#cloud-config
users:
  - name: lab
    shell: /bin/bash
    sudo: ALL=(ALL) NOPASSWD:ALL
    lock_passwd: false
chpasswd:
  expire: false
  users:
    - { name: lab, password: lab, type: text }
ssh_pwauth: true
package_update: true
packages:
  - htop

init/vm1/network-config:

yaml
version: 2
ethernets:
  mgmt:
    match: { macaddress: "52:54:00:01:01:00" }
    set-name: mgmt
    dhcp4: true
    dhcp-identifier: mac

Network config: match NICs by MAC ​

The interface name in the lab file is a label for vz. It is not pushed into the guest, which names NICs its own way (enp0s1, eth0, ...). To make the names line up, give each interface a fixed mac in the lab file and match on it in network-config:

yaml
# lab.yaml
interfaces:
  - { name: mgmt, attach: switch/mgmt, mac: "52:54:00:01:01:00" }
yaml
# init/vm1/network-config
ethernets:
  mgmt:
    match: { macaddress: "52:54:00:01:01:00" }
    set-name: mgmt

Now ip -br link in the guest shows mgmt, and so does vz node ls.

Why dhcp-identifier: mac ​

DHCP reservations on a switch match the client's MAC. Some guests (Debian, for example) send a DUID as their DHCP client identifier instead, so the Mac's DHCP server does not see the MAC and hands out a different address than the one you reserved. dhcp-identifier: mac makes the guest identify itself by its MAC, so:

  • the address in dhcpReservations is the one the guest gets, and
  • vz node ls can match the lease to the NIC.

When a guest ignores its reservation, vz node ls says so:

text
vm1/mgmt is using 10.101.0.5, not its reservation 10.101.0.11: the guest's DHCP client id is not its MAC. Set dhcp-identifier: mac in its network-config.

See Networking for how addresses are found.

Static addresses ​

On a switch with dhcp: false, or on a link (links have no DHCP), set addresses in network-config. From the static addressing example:

yaml
version: 2
ethernets:
  lan:
    match: { macaddress: "52:54:00:04:01:00" }
    set-name: lan
    dhcp4: false
    dhcp6: false
    accept-ra: false
    addresses: ["10.104.0.11/24"]
    routes:
      - { to: default, via: 10.104.0.1 }
    nameservers: { addresses: [10.104.0.1] }

The Mac has the first address of each switch subnet (.1 here), and it also answers DNS there while the switch's dnsProxy is on.

The examples' login ​

Every example creates the user lab with the password lab, passwordless sudo, and SSH password login:

bash
ssh lab@10.101.0.11        # password: lab

CAUTION

This is fine for a lab on your own Mac and nowhere else. Anyone who can reach the VM can log in and become root. Port forwarding in particular can make a guest reachable from beyond your Mac. For anything longer lived, use your own user, SSH keys (ssh_authorized_keys) and ssh_pwauth: false. The examples that ship key pairs (26, 28, 30) say the keys are public; use them only in those labs.

First boot takes a while ​

vz up returns once the VMs are running, not when cloud-init is done. Installing packages can take a minute or two (k3s in example 29 takes two or three). Watch it happen:

bash
vz node console vm1                       # live
vz node logs vm1 --follow                 # with consoleLog: true
ssh lab@10.101.0.11 'cloud-init status --wait'

Run cloud-init again ​

cloud-init runs its setup once per instance ID. Because the ID stays <lab>-<node>, editing the seed and restarting the node is not enough. Reset the node to start again from the clean base image, where cloud-init has never run:

bash
vz down
vz node reset vm1 --yes     # deletes the overlay, UEFI variables and the seed
vz up

This only works for disks with overlay: true. A disk without an overlay keeps every change, including cloud-init's record that it already ran.

TIP

To force a new run without a reset, put your own meta-data in the seed folder with a new instance-id.

See also ​