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-datafile includes instance details. -
The
user-datafile 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-initpreinstalled andcloud-initis set to run automatically on the VM.
Procedure
Create a
cloudinitisodirectory and set it as your working directory:$ mkdir cloudinitiso $ cd cloudinitisoEdit the
meta-datafile:$ vi meta-data instance-id: citest local-hostname: citest-1Edit the
user-datafile:$ vi user-data #cloud-config password: cilogon chpasswd: {expire: False} ssh_pwauth: True ssh_authorized_keys: - <ssh-rsa AAA...fhHQ== sample@example.com>NoteYou can find your SSH public keys in the
~/.ssh/id_rsa.pubfile.Create an ISO image that includes
user-dataandmeta-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)-
Download a KVM Guest Image from the Red Hat Customer Portal to the
/var/lib/libvirt/imagesdirectory. Create a new VM from the KVM Guest Image using the
virt-installutility 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 \ --importLog on to your image with the default username
cloud-userand default passwordcilogon:citest-1 login: cloud-user Password: [cloud-user@citest-1 ~]$
Verification
Check the status of the
cloud-initservice to confirm that the utility has completed its defined tasks:[cloud-user@citest-1 instance]$ cloud-init status status: doneThe
cloud-initutility creates thecloud-initdirectory layout under/var/lib/cloudwhen 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
NoCloudby checking the datasource file.$ cd /var/lib/cloud/instance $ cat datasource DataSourceNoCloud: DataSourceNoCloud [seed=/dev/sr0][dsmode=net]cloud-initcopies 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
For OpenStack, the Creating and managing instances includes information for configuring an instance using cloud-init. See Creating a customized instance for specific procedures.