I'm using plain KVM + Libvirt as my hypervisor of choice in my Homelab since it gives me a lot of flexibility, reliability and performance. Installing VMs using traditional installers allows for customizations during install but if all you're doing is quickly spinning up a VM to test something, pre-built Cloud Images are probably a better choice.
I'll not create a few Files required for Cloud Init and the Cloud Init Data ISO File.
The Cloud Images can be customized though before importing them using tools like virt-sysprep or cloud-init.
In this article, I'll be covering a workflow using provided Cloud Images and Cloud Init to bootstrap ephemeral Linux Servers.
First, we'll have to download the cloud image, I'll be using a Amazonlinux Cloud Image this time:
[root@hyv02 ~]# curl -4 -f -k -L -Z -o '/var/kvm/nfs-vm-templates/amazonlinux-2023-2025-07-21-x86_64.qcow2' -X 'GET' -H 'Accept: application/octet-stream' -H 'User-Agent: curl/1.33.7' https://cdn.amazonlinux.com/al2023/os-images/2023.8.20250721.2/kvm/al2023-kvm-2023.8.20250721.2-kernel-6.1-x86_64.xfs.gpt.qcow2
[root@hyv02 ~]# chown root:root /var/kvm/nfs-vm-templates/amazonlinux-2023-2025-07-21-x86_64.qcow2; chmod 600 /var/kvm/nfs-vm-templates/amazonlinux-2023-2025-07-21-x86_64.qcow2
You can now clone the cloud-image and create a new qcow2 image using it as a reference for the new VM:
[root@hyv02 ~]# qemu-img create -b /var/kvm/nfs-vm-templates/amazonlinux-2023-2025-07-21-x86_64.qcow2 -f qcow2 -F qcow2 /var/kvm/nfs-vm-data/amazonlinux-cloud-init.qcow2 10G
[root@hyv02 ~]# mkdir -p -m 700 ~/cloud-init
[root@hyv02 ~]# cat << EOF > ~/cloud-init/meta-data
instance-id: almalinux-cloud-init
local-hostname: amazonlinux-cloud-init
EOF
[root@hyv02 ~]# cat << EOF > ~/cloud-init/user-data
#cloud-config
create_hostname_file: true
preserve_hostname: false
hostname: almalinux-cloud-init
users:
- name: archy
groups: sudo
shell: /bin/bash
sudo: ['ALL=(ALL:ALL) NOPASSWD:ALL']
ssh_authorized_keys:
- ssh-ed25519 AAAAC3Nza1
- ssh-ed25519 AAAAC3Nza2
EOF
[root@hyv02 ~]# cat << EOF > ~/cloud-init/network-config
version: 2
ethernets:
id0:
match:
name: en*
dhcp4: true
dhcp6: false
EOF
[root@hyv02 ~]# genisoimage -output /var/kvm/nfs-vm-templates/cidata.iso -V cidata -r -J ~/cloud-init/user-data ~/cloud-init/meta-data ~/cloud-init/network-config
Refresh all storage pools for good measure:
[root@hyv02 ~]# for pool in $(virsh -c 'qemu:///system' pool-list --name | awk NF); do virsh -c 'qemu:///system' pool-refresh --pool ${pool}; done
From here on out you can use virt-install, virt-manager or cockpit machines to create a vm and start the VM. An example for virt-install could look like this:
[root@hyv02 ~]# virt-install --import --hvm --noautoconsole \
--connect 'qemu:///system' \
--name 'amazonlinux-cloud-init' \
--memory '1024' \
--vcpus '2' \
--cpu 'host-model' \
--controller 'type=virtio-serial' \
--disk '/var/kvm/nfs-vm-data/amazonlinux-cloud-init.qcow2,bus=virtio' \
--disk '/var/kvm/nfs-vm-templates/cidata.iso,device=cdrom' \
--network 'bridge=br-internal,model=virtio' \
--arch 'x86_64' \
--machine 'q35' \
--os-variant 'linux2024' \
--rng '/dev/urandom'
This command will create a VM named 'amazonlinux-cloud-init' with 1024MB of RAM and 2 CPU Cores, utilizing the existing Disk and adding the Cloud Init Data ISO created earlier.
Feel free to comment and / or suggest a topic.
Comments
Post a Comment