Migrating KVM Virtual Machines to Amazon EC2: Image Conversion and Best Practices


4 views

When moving from on-premise KVM virtualization to Amazon EC2, you can't simply copy VM disk images due to differences in hypervisor technologies and cloud infrastructure requirements. The process requires conversion and careful preparation of your virtual machine images.

Amazon provides several methods to import KVM-based VMs:

  • Using AWS CLI and the aws ec2 import-image command
  • Via AWS VM Import/Export service
  • Converting to compatible format first (RAW or VMDK)

Here's how to prepare your KVM image for EC2:

# Convert qcow2 to raw format
qemu-img convert -f qcow2 -O raw source.qcow2 destination.raw

# Resize the disk if needed (minimum 8GB for EC2)
qemu-img resize destination.raw +8G

# Install AWS tools inside the VM before migration
sudo apt-get install cloud-utils  # For Ubuntu
sudo yum install cloud-utils      # For CentOS/RHEL

Before importing to EC2, upload your converted image to S3:

aws s3 cp destination.raw s3://your-bucket/vm-images/ \
  --storage-class STANDARD \
  --acl bucket-owner-full-control

Use this JSON manifest file (manifest.json) for the import:

{
  "Description": "My KVM VM Import",
  "Format": "raw",
  "UserBucket": {
    "S3Bucket": "your-bucket",
    "S3Key": "vm-images/destination.raw"
  }
}

Then run the import command:

aws ec2 import-image --description "KVM Migration" \
  --disk-containers file://manifest.json
  • Check network configuration (especially Elastic Network Interfaces)
  • Verify storage performance meets requirements
  • Update cloud-init configurations if used
  • Consider using EC2 Launch Templates for consistent deployments

For more complex environments, HashiCorp Packer provides a robust solution:

source "amazon-ebs" "kvm-migration" {
  source_image = "destination.raw"
  instance_type = "t3.medium"
  region = "us-west-2"
  ssh_username = "ec2-user"
}

build {
  sources = ["source.amazon-ebs.kvm-migration"]
}

When moving from on-premises KVM virtualization to AWS EC2, you're essentially dealing with two different virtualization technologies. KVM uses raw disk images (qcow2, raw) while AWS EC2 relies on Amazon Machine Images (AMIs) built on Xen (for older instances) or Nitro (for newer ones). The fundamental incompatibility means direct image copying isn't possible - but conversion is.

Here are the most reliable approaches for KVM-to-EC2 migration:


# Method 1: Using AWS CLI and EC2 Import
aws ec2 import-image \
  --disk-containers file://containers.json \
  --description "My KVM VM migration"

# Sample containers.json:
[
  {
    "Description": "CentOS 7 KVM Image",
    "Format": "qcow2",
    "UserBucket": {
      "S3Bucket": "my-migration-bucket",
      "S3Key": "centos7.qcow2"
    }
  }
]

For a typical CentOS/RHEL KVM guest:

  1. Convert qcow2 to raw: qemu-img convert -f qcow2 -O raw input.qcow2 output.raw
  2. Upload to S3 bucket
  3. Create import task via AWS CLI
  4. Monitor progress with: aws ec2 describe-import-image-tasks --import-task-ids import-ami-1234567890abcdef0

Before attempting migration:

  • Ensure VM uses paravirtualized or virtio drivers
  • Remove any KVM-specific packages (qemu-guest-agent)
  • Install AWS tools: yum install -y ec2-net-utils ec2-utils
  • Verify filesystem integrity (especially for Windows guests)

Frequent problems and fixes:

Error Solution
Bootloader not found Reinstall GRUB with AWS-compatible config
Network not coming up Replace legacy network scripts with ENI configuration