Amazon EC2: how to convert an existing PV AMI to HVM

Solution 1:

Answer:

  1. Start an Ubuntu HVM linux, any version, new

  2. Start an Ubuntu / with my existing AMI / PVM linux, and install grub packages on them: apt-get install grub-pc grub-pc-bin grub-legacy-ec2 grub-gfxpayload-lists

  3. Stop PVM linux

  4. Detach root (/dev/sda1) partition at PVM linux

  5. Attach PVM linux root partition to running HVM linux somewhere, e.g.: /dev/sdf

  6. On HVM linux: mkdir -p /mnt/xvdf && mount /dev/xvdf /mnt/xvdf

  7. rsync -avzXA /boot/ /mnt/xvdf/boot/

  8. mount -o bind /dev /mnt/xvdf/dev && mount -o bind /dev/pts /mnt/xvdf/dev/pts && mount -o bind /proc /mnt/xvdf/proc && mount -o bind /sys /mnt/xvdf/sys

  9. chroot /mnt/xvdf

  10. grub-install --no-floppy --recheck --force /dev/xvdf

  11. update-grub2

  12. exit chroot: CTRL+D

  13. stop HVM Linux

  14. detach /dev/sda1 original root AND detach /dev/sdf PVM root

  15. attach PVM root to HVM linux as /dev/sda1

  16. Start HVM linux, voilà!

  17. Create a new AMI image from the running HVM linux, it will be HVM virtualized.

Solution 2:

The answer from @divyenduz works but needs some cleanup and clarification for modern (circa 2019) AWS EC2. Importantly, modern instance classes translate the device name differently.

Here are my modified steps.

For clarity, nodes are:

  • Original PVM node that you want to upgrade to HVM is "PVM01"
  • New HVM node that you will migrate PVM01's root disk to is "HVM01"

BEFORE PROCEEDING: Back up Original Node PVM01

  1. Install prerequisites on Node PVM01

    • install grub packages on PVM01

      apt-get install  grub-pc  grub-pc-bin grub-legacy-ec2 grub-gfxpayload-lists
      
  2. Stop node PVM01

    • Create snapshot of /dev/sda1
    • OR create AMI of entire node
  3. Create snapshot of original root volume on PVM01 and create new volume from this snapshot
    • Create snap of /dev/sda1 on PVM01
    • Create vol from this snap
    • Label each resource accordingly
      • Tag: Name; Value: pvm01:/dev/sda1:snapshot-for-conversion-to-hvm:2019-07-01
      • Etc
  4. create-instance: New Ubuntu HVM instance "HVM01". Any instance class should work; however, device name may be different, see notes, below
    • I used Bionic Ubuntu 18.04 HVM AMI ID ami-0a313d6098716f372 and instance class C5.XLARGE
  5. Attach PVM01 root partition (new volume from previous step) to new HVM01 /dev/sdf

    • NOTE: Older instance classes such as C3 will translate the volume name to /dev/xvdf
    • NOTE: Newer instance classes such as C5 will translate the volume name to /dev/nvme1
      • On newer instance classes, the root vol will be /dev/nvme0 -- ZERO
      • On newer instance classes, the attached, secondary vol will be /dev/nvme1 -- ONE
    • Make a note of the attached volume device name

    ssh PVM01 sudo fdisk -l

  6. On HVM01:

    # For xvdf, e.g. on C3.XLARGE
    DEVNAME=xvdf1
    # For nvme, e.g. on C5.XLARGE
    DEVNAME=nvme1n1
    mkdir -p /mnt/${DEVNAME} && mount /dev/${DEVNAME} /mnt/${DEVNAME}
    rsync -avzXA /boot/ /mnt/${DEVNAME}/boot/
    mount -o bind /dev /mnt/${DEVNAME}/dev && mount -o bind /dev/pts /mnt/${DEVNAME}/dev/pts && mount -o bind /proc /mnt/${DEVNAME}/proc && mount -o bind /sys /mnt/${DEVNAME}/sys
    chroot /mnt/${DEVNAME}
    grub-install --no-floppy --recheck --force /dev/${DEVNAME}
    update-grub2
    
  7. Exit chroot with CTRL+D

  8. Stop HVM01
  9. Detach both volumes
    • detach /dev/nvme0 PVM01 root
      • NOTE: This would be /dev/sda1 on a C3 instance class
    • detach /dev/${DEVNAME} (DEVNAME from above script)
  10. attach PVM01 root Volume to HVM01 as /dev/sda1
    • Once again, the /dev/sda1 name is in the console; this name will get translated to /dev/nvme0 or /dev/xvda1 depending on how modern the instance class is
  11. Start HVM01
  12. OPTIONAL: Create a new AMI image from the the now-running HVM01, it will be HVM virtualized.