KVM: Testing cloud-init locally using KVM for a RHEL cloud image

The ability to quickly stand up a guest OS with cloud-init is most often associated with deployment of virtual machines in an IaaS like EC2 or Azure.

But cloud-init is not just for remote cloud providers, and using cloud-init for local images that can be quickly deployed in KVM works great for local development and testing.

This article will step through testing a guest RHEL 8 cloud image on KVM, from an Ubuntu Host.

Prerequisites

As a prerequisite for this article, you must install KVM and libvirt as described here.

Also install additional packages needed to manage cloud-images:

sudo apt-get install -y cloud-image-utils libosinfo-bin

RHEL Cloud Image

We will use the RHEL8 KVM image called “rhel-8.1-x86_64-kvm.qcow2” which uses cloud-init.  Download this 780Mb file to your “~/Downloads” directory.

Create a snapshot so that we can branch from this disk image without affecting the parent.  We will also use this opportunity to increase the root filesystem from 10G to 12G.

# original image is 8G, create snapshot and make it 10G
qemu-img create -b ~/Downloads/rhel-8.1-x86_64-kvm.qcow2 -f qcow2 snapshot-rhel8-cloudimg.qcow2 12G

# show snapshot info
qemu-img info snapshot-rhel8-cloudimg.qcow2

Create ssh keypair

In order to use ssh public/private key login later, we need to generate a keypair.  cloud-init will embed the public side of the key into the running OS.

ssh-keygen -t rsa -b 4096 -f id_rsa -C rtest1 -N "" -q

This creates files named “id_rsa” and “id_rsa.pub”.

Create cloud-init configuration

Create a file named “cloud_init.cfg” with the below content.

#cloud-config
hostname: rtest1
fqdn: rtest1.example.com
manage_etc_hosts: true
users:
  - name: rhel
    sudo: ALL=(ALL) NOPASSWD:ALL
    groups: adm,sys
    home: /home/rheol
    shell: /bin/bash
    lock_passwd: false
    ssh-authorized-keys:
      - <sshPUBKEY>
# only cert auth via ssh (console access can still login)
ssh_pwauth: false
disable_root: false
chpasswd:
  list: |
     root:linux
     centos:newpass123
  expire: False

# manually set BOOTPROTO for static IP
# cloud-config related bug?
runcmd:
    - [ sh, -c, 'sed -i s/BOOTPROTO=dhcp/BOOTPROTO=static/ /etc/sysconfig/network-scripts/ifcfg-eth0' ]
    - [ sh, -c, 'ifdown eth0 && sleep 1 && ifup eth0 && sleep 1 && ip a' ]

# written to /var/log/cloud-init.log, /var/log/messages
final_message: "The system is finally up, after $UPTIME seconds"

Then replace the “<sshPUBKEY>” placeholder in the file above, with the content of “id_rsa.pub”.

Create network configuration

Create a file named “network_config_static.cfg” to define the networking parameters.  We will use a simple static configuration on the default KVM bridge subnet.

version: 2
ethernets:
  eth0:
     dhcp4: false
     # default libvirt network
     addresses: [ 192.168.122.153/24 ]
     gateway4: 192.168.122.1
     nameservers:
       addresses: [ 192.168.122.1,8.8.8.8 ]
     search: [ example.com ]

Insert metadata into seed image

Now we generate a seed disk that has the cloud-config metadata.

# insert network and cloud config into seed image
cloud-localds -v --network-config=network_config_static.cfg rtest1-seed.qcow2 cloud_init.cfg

# show seed disk just generated
$ qemu-img info rtest1-seed.qcow2 

image: rtest1-seed.qcow2
file format: raw
virtual size: 368K (376832 bytes)
disk size: 368K

Start VM

Now we use virtlib to create the guest VM with the cloud image and seed disk that has the cloud-init metadata.

# get latest os-variant value supported (rhel7.5 on my system)
osinfo-query os | grep rhel

# create guest VM
virt-install --name rtest1 \
  --virt-type kvm --memory 2048 --vcpus 2 \
  --boot hd,menu=on \
  --disk path=rtest1-seed.qcow2,device=cdrom \
  --disk path=snapshot-rhel8-cloudimg.qcow2,device=disk \
  --graphics vnc \
  --os-type Linux --os-variant rhel7.5 \
  --network network:default

After the machine has booted up and you have given cloud-init a couple of minutes to configure networking, you should be able to login to this guest OS from either virt-viewer or ssh.

ssh rhel@192.168.122.153 -i id_rsa

# final cloud-init status
cat /run/cloud-init/result.json

# cloud logs
sudo less /var/log/cloud-init.log

# cloud logs output
sudo grep cloud-init /var/log/messages

 

Disable cloud-init system

Once setup with the proper hostname, network config, packages, etc., you can either leave cloud-config enabled to enforce these settings, or you can disable cloud-init so that you can manage them yourself (or another tool).

From within OS:

# flag that signals that cloud-init should not run
sudo touch /etc/cloud/cloud-init.disabled

# optional, remove cloud-init completely
sudo yum remove cloud-init -y

# shutdown VM so CDROM seed can be ejected
sudo shutdown -h now

From host OS using virsh to control libvirt eject.

# get name of target path
targetDrive=$(virsh domblklist rtest1 | grep rtest1-seed.qcow2 | awk {' print $1 '})

# force ejection of CD
virsh change-media rtest1 --path $targetDrive --eject --force

cloud-init will no longer be invoked when the guest VM is powered back on.

 

More advanced example

For a more advanced example see my RHEL8.1 example.   This has full shell scripts and support for additional data disks formatted as xfs.

 

 

REFERENCES

redhat, installing qcow2 image

redhat discussion, cloud-init enabled images

redhat, 8.1 downloads

theurbanpenguin, cloud-images and kvm

stafwag, centos and cloud-init on kvm

blog.strandboge.com, cloud images, qemu, cloud-init

debian, cloud-localds man page

man virt-install

man qemu-img

bugs.launchpad.net, cloud-image known bug “error: no such device: root”

cloudinit docs, removing cloud-init using /etc/cloud/cloud-init.disabled

redhat, unconfined user question

 

 

check os-variant list

sudo apt install libosinfo-bin
osinfo-query os