Run Amazon Linux 2 on KVM using Qcow2 image

Posted on 255 views

Amazon Linux 2 is an operating system created and optimized specifically by Amazon for use in Amazon Web Services (AWS) Cloud platform. Amazon Linux 2 is designed with security focus in mind, to be stable, and fit for high-performance execution environment where you can develop and run cloud applications. You can run Amazon Linux 2 at no additional charge. AWS is responsible for the provision of ongoing security and maintenance updates for Amazon Linux 2.

In this article we shall discuss on how you can run Amazon Linux 2 on KVM virtualization platform. We won’t be performing the installation from ISO file but rather create a running instance using provided Qcow2 image. Before you can install Amazon Linux 2 on KVM, you need KVM installed and configured on a system with CPU virtualization extension enabled.

Refer to our guides below on how to install KVM and configure it.

Download Amazon Linux 2 Qcow2 for KVM

Download latest Qcow2 image to your local system with the commands below.

wget https://cdn.amazonlinux.com/os-images/2.0.20220912.1/kvm/amzn2-kvm-2.0.20220912.1-x86_64.xfs.gpt.qcow2

Check file format it should show as QEMU QCOW.

$ file amzn2-kvm-2.0.20220912.1-x86_64.xfs.gpt.qcow2
amzn2-kvm-2.0.20220912.1-x86_64.xfs.gpt.qcow2: QEMU QCOW Image (v3), 26843545600 bytes

Create directory on your KVM host that will contain Virtual Machine templates.

sudo mkdir /var/lib/libvirt/images/templates

Let’s move the image downloaded to created directory

sudo mv amzn2-kvm-2.0.20220912.1-x86_64.xfs.gpt.qcow2  /var/lib/libvirt/images/templates/amzn2-template.qcow2

Listing directory contents should show amzn2-template.qcow2 available.

$ ls /var/lib/libvirt/images/templates/
amzn2-template.qcow2

Run Amazon Linux 2 on KVM using Qcow2 image

Set the name of the virtual machine to be created.

export VM_NAME="Amazom-Linux-2"

Convert template we created into Virtual Machine image.

sudo qemu-img convert \
  -f qcow2 \
  -O qcow2 \
  /var/lib/libvirt/images/templates/amzn2-template.qcow2 \
  /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2

Check if the file was created inside /var/lib/libvirt/images directory.

$ file  /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2
/var/lib/libvirt/images/Amazom-Linux-2-root-disk.qcow2: QEMU QCOW Image (v3), 26843545600 bytes

Check the virtual disk size of the image

$ qemu-img info /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2
image: /var/lib/libvirt/images/Amazom-Linux-2-root-disk.qcow2
file format: qcow2
virtual size: 25 GiB (26843545600 bytes)
disk size: 1.3 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

You can extend to a higher value depending on your needs.

# I'm setting mine to 30GB - set yours accordingly
export VM_ROOT_DISK_SIZE=40G

# Resize Debian 11 VM disk
sudo qemu-img resize \
  /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2 \
  $VM_ROOT_DISK_SIZE

The output should look similar to below.

Image resized.

We can confirm the new virtual disk size.

$ qemu-img  info /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2
image: /var/lib/libvirt/images/Amazom-Linux-2-root-disk.qcow2
file format: qcow2
virtual size: 40 GiB (42949672960 bytes)
disk size: 1.3 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

Choose a network to use while creating a Virtual Machine on your KVM host.

$ sudo virsh net-list
 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   yes         yes

Save network to variable

export NET="default"

With the network identified we can proceed to provision the operating system. Change values required respectively.

virt-install \
    --memory 2048 \
    --vcpus 2 \
    --name $VM_NAME \
    --disk /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2,device=disk,bus=virtio,format=qcow2 \
    --os-type Linux \
    --os-variant centos7.0 \
    --network network=$NET,model=virtio \
    --virt-type kvm \
    --graphics none \
    --import

VM installation should begin shortly.

Starting install...
Connected to domain Amazom-Linux-2
Escape character is ^]

See next section for how to enable VNC console.

Enable VNC on existing VM instance

List domains on KVM

$ virsh list --all

Stop the instance

virsh shutdown 

Edit the VM domain config using virsh edit command.

$ virsh edit 

Add below XML contents within  block (Accessible from outside)


  

Update root user password

Reboot the server and Press “e” in edit menu.

run-amazon-linux-kvm-01-1024x774

Edit linux16 line to add rd.break.

run-amazon-linux-kvm-02-1024x458

Add at the end of linux16 line “rd.break

run-amazon-linux-kvm-03-1024x443

Press “Ctrl+x” to reboot the server

run-amazon-linux-kvm-04-1024x570

Remount /sysroot with rw and chroot to it, then set new root password.

mount -o remount,rw /sysroot
chroot /sysroot
passwd root

See screenshot below:

run-amazon-linux-kvm-05-1024x164

Add .autorelabel empty file inside / to reconfigure SELinux on reboot.

touch /.autorelabel 
exit

See below screenshot

run-amazon-linux-kvm-06-1024x94

Login with the username root and password set earlier.

run-amazon-linux-kvm-07-1024x572

You should now have access to Amazon Linux console. Server IP address can be checked with ip ad command.

run-amazon-linux-kvm-08-1024x569

Root user login and password authentication can be enabled by changing PermitRootLogin and PasswordAuthentication parameters.

# vi /etc/ssh/sshd_config
PermitRootLogin yes
PasswordAuthentication yes

Restart sshd service:

sudo systemctl restart sshd

We can test ssh login from our Workstation.

$ ssh [email protected]
Warning: Permanently added '192.168.204.146' (ED25519) to the list of known hosts.
[email protected]'s password:
Last login: Wed Mar 23 23:16:33 2022

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
[[email protected] ~]#

Set hostname of your server

hostnamectl set-hostname amzn2-linux.localdomain

Set correct timezone to ensure apps dare synchronization is accurate.

sudo timedatectl set-timezone Africa/Nairobi

Upgrade your Amazon Linux 2 server to ensure all packages are latest.

yum -y update

Conclusion

In this article we’ve been able to deploy and install Amazon Linux 2 on KVM using Qcow2 image file downloaded from official project website. We hope this guide was helpful. If you encounter any issue kindly drop a comment for us.

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.