Configure cloud-init for a VM application

You can configure cloud-init user-data in a VM application to configure guest credentials, install packages, and run scripts on first boot. Without cloud-init configuration, the VM guest has no default credentials and you cannot log in.

Before you begin

  • A VM application is defined in a device specification or you are creating a new VM application.
  • The VM guest image has cloud-init installed and enabled. If you build custom Red Hat Enterprise Linux guest images from bootc, install cloud-init in the Containerfile and enable cloud-init.target before you create the qcow2 or raw disk image. For a Containerfile example, see Build for specific target platforms in the Additional resources section.

Procedure

  1. To configure cloud-init by using the Red Hat Edge Manager web console guided form, enter values in the following fields:
    • SSH key: Paste a public SSH key to inject into the default user account defined by the guest image. For Red Hat Enterprise Linux cloud images, the default user is typically cloud-user. Other guest images use their own default user; for example, community Fedora container disk images use fedora.
    • Password: Set a password for that same default user account.
    • Cloud-init user-data: Enter cloud-config YAML for additional configuration.

    The guided form generates the corresponding cloudInitNoCloud volume in the KubeVirt VirtualMachine YAML. The form does not set a user: directive. It injects ssh_authorized_keys, password, ssh_pwauth, and chpasswd directives that cloud-init applies to the guest image default user. To use a different username, set user: or users: in the Cloud-init user-data field or in raw YAML.

  2. To configure cloud-init by using raw YAML, add a cloudInitNoCloud volume and disk to the vm.yaml content.

    The following example configures the fedora user with an SSH key and password authentication enabled. The example uses a community container disk image for illustration purposes. Pre-built community images are not supported by Red Hat. For Red Hat Enterprise Linux cloud images, use cloud-user as the default user instead of fedora.

    apiVersion: kubevirt.io/v1
    kind: VirtualMachine
    metadata:
      name: my-vm
    spec:
      running: true
      template:
        spec:
          domain:
            devices:
              disks:
              - name: rootdisk
                disk:
                  bus: virtio
              - name: cloudinitdisk
                disk:
                  bus: virtio
            resources:
              requests:
                memory: 2048Mi
          volumes:
          - name: rootdisk
            containerDisk:
              image: quay.io/containerdisks/fedora:40
          - name: cloudinitdisk
            cloudInitNoCloud:
              userData: |
                #cloud-config
                user: fedora
                ssh_pwauth: true
                chpasswd:
                  expire: false
                  users:
                  - name: fedora
                    password: <password>
                    type: text
                ssh_authorized_keys:
                - <ssh_public_key>

    Where:

    cloudinitdisk
    A disk entry for the cloud-init volume. Must be included in both the disks and volumes lists.
    cloudInitNoCloud.userData
    Inline cloud-config YAML. Must start with #cloud-config.
    <password>
    The password for the fedora account.
    <ssh_public_key>
    The SSH public key to authorize for the fedora account.
  3. To create a custom user account instead of using the image default user, add a users entry in the Cloud-init user-data field or in raw YAML. The SSH key and password fields in the guided form always apply to the image default user. To log in as a different account, configure that user in cloud-init and connect with that username.

    The following userData example creates an appadmin user with SSH key access:

          - name: cloudinitdisk
            cloudInitNoCloud:
              userData: |
                #cloud-config
                users:
                - name: appadmin
                  groups: wheel
                  sudo: ALL=(ALL) NOPASSWD:ALL
                  shell: /bin/bash
                  ssh_authorized_keys:
                  - <ssh_public_key>

    Log in with ssh -p <host_port> appadmin@<device_host> when SSH port publishing is configured.

  4. To install packages and run commands on first boot, add the packages and runcmd directives to the userData field:
          - name: cloudinitdisk
            cloudInitNoCloud:
              userData: |
                #cloud-config
                user: fedora
                ssh_pwauth: true
                chpasswd:
                  expire: false
                  users:
                  - name: fedora
                    password: <password>
                    type: text
                packages:
                - httpd
                - vim
                runcmd:
                - systemctl enable --now httpd
  5. Apply the device specification by using the flightctl CLI or save the configuration in the web console.

Results

  1. After the VM enters the Running state, connect to the VM through SSH or the serial console.
  2. Verify that cloud-init completed successfully by checking the cloud-init status:
    $ cloud-init status

    Expected output:

    status: done
  3. Review the cloud-init log for any errors:
    $ cat /var/log/cloud-init-output.log