I am trying to create a KVM guest instance running Amazon Linux 2 in an on-premises environment, on a CentOS 7 host. I am doing this over SSH (e.g. my local machine -> SSH to the CentOS host).

I know my hypervisor setup works, as using the following commands, I can create a CentOS 7 guest with SSH access no problem at all:


# +++ Downloading image OS image
cd $BOOT
wget http://cloud.centos.org/centos/7/images/$OS_IMG

# +++ Creating meta/user data
cd $IMG/$VM
echo -e "instance-id: $VM\nlocal-hostname: $VM" > meta-data
cp $SCRIPTS/user-data user-data
cp $BOOT/$OS_IMG $VM.qcow2

# +++ Creating disk
qemu-img create -f qcow2 -o preallocation=metadata $VM.new.image 40G
virt-resize --quiet --expand /dev/sda1 $VM.qcow2 $VM.new.image
mv $VM.new.image $VM.qcow2

# +++ Creating CD-ROM containing cloud init data
mkisofs -o $VM-cidata.iso -V cidata -J -r user-data meta-data

# +++ Creating VM"
virt-install --import --name $VM \
    --memory 4096 --vcpus 2 --cpu host \
    --disk $VM.qcow2,format=qcow2,bus=virtio \
    --disk $VM-cidata.iso,device=cdrom \
    --network bridge=virbr0,model=virtio \
    --os-type=linux \
    --os-variant=centos7.0 \

Within seconds, I can run virsh net-dhcp-leases and see an IP address which I can SSH to.

I have tried to replicate this process with Amazon Linux 2, using the image found here: https://cdn.amazonlinux.com/os-images/2.0.20200304.0/kvm/

For user-data, I have tried my CentOS file (quite lengthy, including a lot of runcmd lines) and the recommended basic contents found here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/amazon-linux-2-virtual-machine.html. I don't see any difference when using the 2 different files.

This doesn't appear to work however, and I'm stuck as to how I can proceed further.

  • I am unable to run virsh console myvmname, as it just gets stuck on Escape character is ^] (return key does nothing). On the CentOS guest, I can run virsh console myvmname and with a single hit of the return key, I'm connected as expected
  • If I run virsh net-dhcp-leases default, there are no leases - so I don't have an IP address that I can attempt SSH connection on
  • Logs in /var/log/libvirt/qemu show nothing wrong; the failed AL2 logs look identical to the working CentOS guests' logs.

I have also tried to copy the process exactly as recommended on the previous Amazon link, including using genisoimage with the seed.iso file etc. but this appears to make no difference.

How can I get access to this VM, or at least attempt to work out what's going wrong? Boot logs would be ideal.


I stumbled across the fix here: https://github.com/giovtorres/kvm-install-vm/blob/master/kvm-install-vm#L630

Basically, I needed to use qemu-img resize rather than qemu-img create:

if [[ $al2 == "Y" ]]; then
    qemu-img resize $VM.qcow2 40G &>> ${VM}.log

    echo "+++ Creating CD-ROM containing cloud init data"
    genisoimage -output $VM-cidata.iso \
                -volid cidata \
                -joliet -r user-data meta-data &>> ${VM}.log
    export LIBGUESTFS_BACKEND=direct
    qemu-img create -f qcow2 -o preallocation=metadata $VM.new.image 40G
    virt-resize --quiet --expand /dev/sda1 $VM.qcow2 $VM.new.image
    mv $VM.new.image $VM.qcow2

    echo "+++ Creating CD-ROM containing cloud init data"
    mkisofs -o $VM-cidata.iso -V cidata -J -r user-data meta-data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.