41

Is it possible to create a hardware virtual machine (HVM) AMI from an existing paravirtual (PV) AMI.

My initially thought was to start a new PV instance and use the ec2-create-image command to create a new image while specifying HVM as the virutalization type. However, ec2-create-image does not have a command line parameter to specify the virtualization type.

Is there another way to go about doing this?

javacavaj
  • 513
  • 1
  • 4
  • 7

5 Answers5

22

Update

AWS has enabled this feature in the EC2 API. It is available as the --virtualization-type option to aws ec2 register-image in the new Boto based awscli.

Original answer

Yes! Unfortunately, there is not a direct way to do so. Also, some PV instances may need kernel and bootloader modifications.

  1. Create a volume from your existing PV AMI. If it was your own PV AMI, you can make a volume from the snapshot. If it is a 3rd party AMI, you will need to launch an instance and take a snapshot.
  2. Launch an HVM instance with any AMI.
  3. Stop that HVM instance.
  4. Detach the root volume from that instance.
  5. Attach the PV volume as the root volume(/dev/sda1 or /dev/sda if it was partitioned) to the HVM instance.
  6. Run ec2-create-image on the HVM instance.
  7. Launch other instances with your new HVM AMI.

If that doesn't work, then before step 5, you will need to attach that volume to a running instance, set up a chroot, and install a kernel and bootloader for your distribution. You may also want to clear logs and any cloud-init cache.

Jeff Strunk
  • 2,207
15

In my case, I had to do the conversion manually since the instance that I create using aws ec2 register-image did not boot. My solution is based on this post on the AWS EC2 Forum.

Preparation

Make sure that all the volumes are in the same availability zone.

  1. SSH to your PV machine that you want to migrate from and apply all updates, then log out.

  2. Go to the AWS Console and launch a new HVM instance by selecting the same base AMI that the PV system was created from (in my case, the Amazon 64-bit Linux AMI).

  3. SSH to this new instance and apply all updates, then log out.

  4. Go to the AWS Console and stop the PV instance. Take a snapshot of the root device and create a new volume (SOURCE VOLUME) from this snapshot.

  5. Stop the HVM instance. Take a snapshot of the root device on the new instance and create a new volume (TARGET VOLUME) from this snapshot. Start the HVM (new) instance again.

  6. Using the AWS Console:

  • Attach SOURCE VOLUME to the new instance as /dev/xvdf.
  • Attach TARGET VOLUME to the new instance as /dev/xvdg.

Conversion Process

  1. SSH to the new instance and get root access:

     sudo su
    
  2. Mount the source and target drives.

     mkdir -p /mnt/source && mount /dev/xvdf /mnt/source
     mkdir -p /mnt/target && mount /dev/xvdg1 /mnt/target
    

    In my case, the devices were /dev/xvdf (source) and /dev/xvdg1 (target). These may change in your configuration based on the number of partitions and where you attached them (see step 6 in Preparation). Use ls -al /dev/xvd* to see the drives.

  3. Backup /lib/modules/* (If the kernel of the PV ami differs from the new HVM machine. This module is used by some services of AWS.)

  4. Delete everything but /boot on the target volume:

     cd /mnt/target && ls | grep -v boot | xargs rm -Rf
    
  5. Delete /boot on the source volume:

     rm -Rf /mnt/source/boot
    
  6. Copy the source volume's data to the target volume preserving all the attributes:

     rsync -aAXHPv /mnt/source/ /mnt/target
    
  7. Edit /mnt/target/etc/fstab for / partition, so that it references the TARGET VOLUME when mounted on its final location in step (8). Either using a label or simply something along:

     /dev/xvda1 /     ext4    defaults,barrier=0 1 1
    

Then restore /lib/modules/ that was backed up in Step 3. (If the kernel of the PV ami differs from the new HVM machine.)

  1. Stop the system and detach all volumes using the AWS console. Attach the TARGET VOLUME on the new instance as /dev/xvda.

    Be sure to note where the original root device was mounted. In most cases, it should be /dev/xvda.

  2. Start your HVM instance. It should now be an exact duplicate of your PV system. If everything looks OK, you may now delete your PV instance and also SOURCE VOLUME.

tolgamorf
  • 251
  • 3
  • 7
10

TLDR:

ec2-register -a x86_64 -d '3.15.7-200.fc20.x86_64' -n 'Fedora_20_HVM_AMI'  --sriov simple --virtualization-type hvm -s snap-b44feb18 --root-device-name /dev/sda1 

Detailed steps:

Answering further based upon Jeff Strunk's response to simplify the steps and giving a bit more details on the ec2 register image:

  1. Create Instance using PV Image. Make / update any changes you want.

  2. Create Image from the above instance.

  3. Find the snapshot id used by the above AMI under EC2 > Elastic Block Store > Snapshot in EC2 Console.

    or if you have the ec2 api tools setup:

    ec2-describe-images ami-id_of_above_created_ami

    and find the snapshot id for the ami

    .. Assumptions for further steps: Your ec2 keys and api tools are set and ready to use:

  4. Register a new HVM AMI using the above snapshot: example:

ec2-register -a x86_64 -d '3.15.7-200.fc20.x86_64' -n 'Fedora_20_HVM_AMI' --sriov simple --virtualization-type hvm -s snap-b44feb18 --root-device-name /dev/sda1

where

  • -d is AMI description
  • -n is AMI name
  • -s is snapshot id from step 3.
  • -a is architecture
  • --virtualization-type is required for making it hvm
  • --sriov is for enabling enhanced networking , though it might be redundant, not sure.

For more information:

2

You can do this from inside of the AWS web interface. Navigate to snapshots, click the desired snapshot you wish to convert to hvm and click on actions and then create image. In the dropdown in the create image wizard select HVM.

Justin
  • 5,668
1

Having tried all of the suggestions herein, none of which worked for me, I found an excellent blog entry on the subject, at https://www.opswat.com/blog/aws-2015-why-you-need-switch-pv-hvm .

The elements (details) of the procedure are:

  1. Install grub on the PV instance to be migrated (source instance).

  2. Make a precautionary snapshot of the root volume on the source instance (source volume, SV).

  3. Create a temporary HVM instance which will migrate the volume.

    1. I used an Amazon Linux instance
  4. Create a destination volume (DV), and attach both this and the SV to the temporary instance.

    1. The DV should be at least as big as the SV.

    2. Attach the SV as /dev/{sd,xvd}f, and the DV as /dev/{sd,xvd}g.

    3. Partition the DV:

    parted /dev/xvdg --script 'mklabel msdos mkpart primary 1M -1s print quit'

    partprobe /dev/xvdg

    udevadm settle

  5. Resize to minimum size the SV's FS, and using dd image it onto the DV.

    1. Clean the source volume's FS: e2fsck -f /dev/xvdf

    2. Minimise the same: resize2fs -M /dev/xvdf

    3. Observe the output from the resize2fs (eg Resizing the file system on /dev/xvdf to 269020 (4k) blocks) and note it down for the next step.

    4. Duplicate SV to DV: dd if=/dev/xvdf of=/dev/xvdg1 bs=<block size from previous step, here 4k> count=<use block count from last step, here 269020>

    5. Expand the FS on the new partition: resize2fs /dev/xvdg1

  6. Install grub into the DV's boot block

    1. Temporarily create device files on the DV: mount /dev/xvdg1 /mnt; cp -a /dev/xvdg /dev/xvdg1 /mnt/dev/

    2. Install grub files:

    rm -f /mnt/boot/grub/*stage*

    cp /mnt/usr/*/grub/*/*stage* /mnt/boot/grub/

    rm -f /mnt/boot/grub/device.map

    1. Install grub in a chroot environment:

    cat << ARNIE | chroot /mnt grub --batch

    device (hd0) /dev/xvdg

    root (hd0,0)

    setup (hd0)

    ARNIE

  7. Having made some other minor changes on the destination volume, snap the volume, and make an AMI from it.

    1. Tidy up the temporary device files: rm -f /mnt/dev/xvdg /mnt/dev/xvdg1

    2. In /mnt/boot/grub/grub.conf, change root (hd0) to root (hd0,0), add (or replace console=*) console=ttyS0 to kernel line, and if necessary replace root=* with root=LABEL=/ in the kernel line

    3. In /mnt/etc/fstab, ensure that the root FS's line contains a labelled reference, eg

    LABEL=/ / ext4 defaults,noatime 1 1

    1. Label the new root FS with e2label /dev/xvdg1 /

    2. Unmount DV from the temporary instance, detach both SV and DV from the temporary instance.

    3. Snap the DV, and from that snap create an AMI image.

  8. Launch an HVM instance from that HMI. That is your migrated instance.

MadHatter
  • 81,580