Converting Amazon EC2 AMI to VirtualBox VMDK: Fixing Root Device Mount Issues in Xen-to-KVM Migration


2 views

When attempting to convert an Amazon Linux 2011.09 AMI to a VirtualBox-compatible VMDK format, many developers encounter the critical error during boot:

dracut Warning: No root device "block:/dev/xvda1" found
Kernel panic - not syncing: Attempted to kill init!

This occurs because Amazon's AMIs use Xen-optimized kernels expecting specific virtualized storage devices (/dev/xvda*), while VirtualBox presents storage as SATA (/dev/sda*) or IDE devices.

Instead of modifying the existing Xen kernel, a more reliable method is to:

  1. Start with a fresh Amazon Linux installation in VirtualBox
  2. Replicate your production environment configuration
  3. Package as a Vagrant box

Here's a streamlined process:

# Create new VirtualBox VM with similar specs to EC2 instance
VBoxManage createvm --name "amazon_linux_clone" --ostype "Linux_64" --register
VBoxManage modifyvm "amazon_linux_clone" --memory 2048 --cpus 2

# Create virtual disk
VBoxManage createhd --filename "amazon_linux.vdi" --size 8192 --format VDI
VBoxManage storagectl "amazon_linux_clone" --name "SATA" --add sata --controller IntelAhci
VBoxManage storageattach "amazon_linux_clone" --storagectl "SATA" --port 0 --device 0 --type hdd --medium "amazon_linux.vdi"

If you must use the original AMI contents, try these kernel modifications:

# Inside the mounted image
chroot /mnt/ec2-image
yum install kernel -y
sed -i 's/xvda/sda/g' /boot/grub/menu.lst

Key files to modify:

  • /boot/grub/menu.lst (change root=/dev/xvda1 to root=/dev/sda1)
  • /etc/fstab (update device references)
  • /boot/initramfs-*.img (may need rebuilding)

For development environments, consider this Vagrantfile template:

Vagrant.configure("2") do |config|
  config.vm.box = "custom_amazon_linux"
  config.vm.box_url = "file:///path/to/your/box.vmdk"
  
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
    vb.cpus = 2
  end

  # Provisioning to match EC2 environment
  config.vm.provision "shell", inline: <<-SHELL
    yum update -y
    yum install -y httpd mysql-server
    systemctl enable httpd
    systemctl enable mysqld
  SHELL
end

When debugging boot issues:

  • Add "rdshell" to kernel command line for emergency shell
  • Check kernel messages with dmesg | grep -i error
  • Verify initramfs contains proper storage drivers
  • Test with different VirtualBox storage controllers (IDE/SATA/SCSI)

When migrating Amazon Linux AMIs to VirtualBox, many developers hit the same roadblock: the dreaded dracut Warning: No root device error during boot. This typically occurs because:

  • Xen-specific kernel modules in Amazon Linux
  • Device mapping differences between EC2 (xvda) and VirtualBox (sda)
  • Initramfs not containing proper drivers for VirtualBox

Here's a working approach that addresses the root device mounting issue:


# 1. Prepare your EC2 instance
sudo yum install -y kernel kernel-devel grub
sudo cp /boot/grub/menu.lst /boot/grub/menu.lst.bak

# 2. Modify GRUB configuration
cat <

After transferring the image to VirtualBox, these adjustments are critical:

  • VirtualBox Storage Controller: Use "SATA" instead of IDE
  • /etc/fstab: Replace /dev/xvda1 with /dev/sda1
  • GRUB_CMDLINE_LINUX: Add nomodeset if experiencing display issues

For Amazon Linux 2011.09, consider these kernel options:


# Install alternative kernel from ELRepo
sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
sudo rpm -Uvh http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm
sudo yum --enablerepo=elrepo-kernel install kernel-ml

Remember to update GRUB after kernel installation:

sudo grub-mkconfig -o /boot/grub/grub.cfg

For reproducible builds, use this Packer template snippet:


{
  "builders": [{
    "type": "amazon-ebs",
    "ami_name": "virtualbox-ready-{{timestamp}}",
    "instance_type": "t2.micro",
    "source_ami": "ami-1ecae776",
    "ssh_username": "ec2-user",
    "provisioners": [
      {
        "type": "shell",
        "inline": [
          "sudo yum install -y kernel kernel-devel grub",
          "sudo sed -i 's/xvda/sda/g' /etc/fstab",
          "sudo dracut --force --add-drivers 'virtio_pci virtio_blk' /boot/initramfs-$(uname -r).img $(uname -r)"
        ]
      }
    ]
  }]
}
  • Boot with rd.debug parameter to see initramfs execution
  • Check VirtualBox logs for disk detection issues
  • Verify kernel modules are present: lsinitrd | grep virtio
  • For older AMIs, consider using PV-GRUB bootloader

When done correctly, you should see successful boot messages showing proper root device mounting and VirtualBox-compatible drivers loading.