How to run Amazon Linux 2023 KVM qcow2 image with libvirt
It can be really useful to run Amazon Linux 2023 as a VM locally to test and develop without using (and paying for) an EC2 instance.
Download Amazon Linux 2023 image
Visit https://docs.aws.amazon.com/linux/al2023/ug/outside-ec2-download.html and follow link to cdn.amazonlinux.com. Download the correct KVM image for your architecture.
cd /var/lib/libvirt/images/
sudo wget https://cdn.amazonlinux.com/al2023/os-images/2023.4.20240401.1/kvm/al2023-kvm-2023.4.20240401.1-kernel-6.1-x86_64.xfs.gpt.qcow2
Create Amazon Linux 2023 virtual machine and login via console
virt-install --name delta --memory 2000 --vcpus 2 --disk=size=25,backing_store=/var/lib/libvirt/images/al2023-kvm-2023.4.20240401.1-kernel-6.1-x86_64.xfs.gpt.qcow2 --osinfo=fedora38 --cloud-init --autostart
Running this command will create the VM, generate a root password and allow you to login via console.
This is the example output.
[root@carbon ~]# virt-install --name delta --memory 2000 --vcpus 2 --disk=size=25,backing_store=/var/lib/libvirt/images/al2023-kvm-2023.4.20240401.1-kernel-6.1-x86_64.xfs.gpt.qcow2 --osinfo=fedora38 --cloud-init --autostart
WARNING Defaulting to --cloud-init root-password-generate=yes,disable=yes
Starting install...
Password for first root login is: FO7RUDwoixceupDD
Allocating 'delta.qcow2' | 0 B 00:00:00 ...
Creating domain... | 0 B 00:00:00
Running text console command: virsh --connect qemu:///system console delta
Connected to domain 'delta'
To disconnect Ctrl + ]
To reconnect to the VM
# virsh console delta
Delete the VM
virsh has rather cryptic termanology
# virsh destroy delta ; virsh undefine --domain delta --remove-all-storage
Example output
[root@carbon ~]# virsh destroy delta ; virsh undefine --domain delta --remove-all-storage
Domain 'delta' destroyed
Domain 'delta' has been undefined
Volume 'vda'(/var/lib/libvirt/images/delta.qcow2) removed.
Create Amazon Linux 2023 virtual machine, with SSH pub key for SSH login
You are more likely to want to setup Amazon Linux 2023 VM with SSH public key and access via SSH, in similar way to you would an ec2 instance.
Create VM with clouduser option
Creates the VM and uses SSH public key specified for Amazon Linux admin user ec2-user
# virt-install --name delta --memory 2000 --vcpus 2 --disk=size=25,backing_store=/var/lib/libvirt/images/al2023-kvm-2023.4.20240401.1-kernel-6.1-x86_64.xfs.gpt.qcow2 --osinfo=fedora38 --cloud-init clouduser-ssh-key=/home/laurence/.ssh/id_rsa.pub --autostart --noautoconsole
Find the IP address assigned
# virsh domifaddr delta
In this example the IP address is 192.168.124.123
# virsh domifaddr delta
Name MAC address Protocol Address
-------------------------------------------------------------------------------
vnet1 52:54:00:1e:22:9a ipv4 192.168.124.123/24
Connect using SSH
Make sure you are logged in to the user with the SSH key that you used for clouduser-ssh-key
.
$ ssh [email protected]
Warning: Permanently added '192.168.124.123' (ED25519) to the list of known hosts.
[ec2-user@localhost ~]$