Chapter 4. Creating a virtual machine with cloud-init


You need to create meta-data and user-data files, then include them in a Red Hat Enterprise Linux (RHEL) ISO image, and attach it to a VM created from a KVM Guest Image. The VM will use cloud-init to configure the VM during the first boot.

  • The meta-data file includes instance details.
  • The user-data file includes information to create a user and grant access.

Though most of the public cloud platforms support cloud-init, you can also configure cloud-init for the NoCloud environment. Considering datasource is NoCloud, you can manage cloud instances for both a local configuration (without network access) and configurations fetched from a remote server. Additionally, you can create a VM template by configuring cloud-init on one VM. With this template, you can create additional VMs or clusters of VMs.

Prerequisites

  • You have created a Red Hat account.
  • You have downloaded a KVM guest image which has cloud-init preinstalled and cloud-init is set to run automatically on the VM.

Procedure

  1. Create a cloudinitiso directory and set it as your working directory:

    $ mkdir cloudinitiso
    $ cd cloudinitiso
  2. Edit the meta-data file:

    $ vi meta-data
    
    instance-id: citest
    local-hostname: citest-1
  3. Edit the user-data file:

    $ vi user-data
    
    #cloud-config
    password: cilogon
    chpasswd: {expire: False}
    ssh_pwauth: True
    ssh_authorized_keys:
      - <ssh-rsa AAA...fhHQ== sample@example.com>
    Note

    You can find your SSH public keys in the ~/.ssh/id_rsa.pub file.

  4. Create an ISO image that includes user-data and meta-data:

    # genisoimage -output ciiso.iso -volid cidata -joliet -rock user-data meta-data
    
    I: -input-charset not specified, using utf-8 (detected in locale settings)
    Total translation table size: 0
    Total rockridge attributes bytes: 331
    Total directory bytes: 0
    Path table size(bytes): 10
    Max brk space used 0
    183 extents written (0 MB)
  5. Download a KVM Guest Image from the Red Hat Customer Portal to the /var/lib/libvirt/images directory.
  6. Create a new VM from the KVM Guest Image using the virt-install utility and attach the downloaded image to the existing image:

    # virt-install \
        --memory 4096 \
        --vcpus 4 \
        --name mytestcivm \
        --disk /var/lib/libvirt/images/rhel-8.1-x86_64-kvm.qcow2,device=disk,bus=virtio,format=qcow2 \
        --disk /home/sample/cloudinitiso/ciiso.iso,device=cdrom \
        --os-type Linux \
        --os-variant rhel10.0 \
        --virt-type kvm \
        --graphics none \
        --import
  7. Log on to your image with the default username cloud-user and default password cilogon:

    citest-1 login: cloud-user
    Password:
    [cloud-user@citest-1 ~]$

Verification

  • Check the status of the cloud-init service to confirm that the utility has completed its defined tasks:

    [cloud-user@citest-1 instance]$ cloud-init status
    status: done

    The cloud-init utility creates the cloud-init directory layout under /var/lib/cloud when it runs, and it updates or changes certain directory contents based upon the directives you have specified.

  • For example, you can confirm that the datasource is NoCloud by checking the datasource file.

    $ cd /var/lib/cloud/instance
    $ cat datasource
    
    DataSourceNoCloud: DataSourceNoCloud [seed=/dev/sr0][dsmode=net]
  • cloud-init copies user-data into /var/lib/cloud/instance/user-data.txt:

    $ cat user-data.txt
    
    #cloud-config
    password: cilogon
    chpasswd: {expire: False}
    ssh_pwauth: True
    ssh_authorized_keys:
      - ssh-rsa AAA...fhHQ== sample@redhat.com
Note

For OpenStack, the Creating and managing instances includes information for configuring an instance using cloud-init. See Creating a customized instance for specific procedures.

Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2026 Red Hat
Back to top