Linux Amazon Machine Images use one of two types of virtualization: paravirtual (PV) or hardware virtual machine (HVM). The main differences between PV and HVM AMIs are the way in which they boot and whether they can take advantage of special hardware extensions (CPU, network, and storage) for better performance.
This post will discuss the steps for converting an EC2 Linux instance from PV to HVM.
- A PV based instance, HVM instance (needs to be created) and a conversion instance (needs to be created).
- Snapshots of the EBS volume(s) of the instance to be converted.
- You'll need to snapshot the volume of the existing PV instance. Ideally the snapshot is taken with the instance in a stopped state.
You may also have an instance store volume. During a stop action any data on the instance store will be lost.
1. Create an HVM instance, using as close as possible your current OS, (i.e., make sure the kernel versions match).
1.A. Create the HVM instance in the same Availability Zone (AZ) as your PV instance (eu-west-1b) and with the same size EBS volume 15GB.
1.B. Once it has started and is running you can stop it. Tag the Volume with something like OS-HVM and detach it from the HVM instance.
2. Start a conversion instance wait for it to fully start. (It is important that the instance is fully started before attaching the other volumes)
2.A. SSH into the conversion instance.
2.B. Attach the HVM volume to the conversion instance as /dev/xvdf.
sudo mkdir /hvm
sudo mount /dev/xvdf1 /hvm
The following 2.C, 2.D and 2.E steps may not be needed on all Linux versions, however it is recommended.
2.C. Ensure you keep the HVM boot directory by moving it to the tmp directory.
sudo mv /hvm/boot /tmp/boot.hvm
2.D. Empty out the rest of the drive with:
sudo rm -Rf /hvm/*
2.E. Verify the drive is now empty with:
sudo ls -al /hvm
3. With the snapshot of the Volume from your PV instance you'll want to Create Volume in the EC2 console. Keep the current volume size and create it in the same AZ (eu-west-1b). Find the new EBS volume and tag it OS-PV.
3.A. Attach it to the conversion instance as /dev/xvdg
sudo mkdir /pv
sudo mount /dev/xvdg /pv
3.B. Verify that it is mounted and has the correct file structure.
# You should see the boot and other root directories.
sudo ls /pv
3.C. Then copy the contents of /pv to /hvm
sudo cp -p -R /pv/* /hvm
4. Change the boot directory to be HVM based.
4.A. Remove the PV boot directory and replace it with the HVM boot directory.
sudo rm -R /hvm/boot
4.B. Copy back the saved HVM boot directory.
sudo mv /tmp/boot.hvm /hvm/boot
5. Verify there are now all the needed directories and files on the HVM volume with.
sudo ls -al /hvm
6. You can now unmount the hvm volume.
sudo umount /hvm
7. Find the OS-HVM volume and detach it from the conversion instance.
7.A. Attach it back to the HVM instance as device /dev/xvda
7.B. Start the HVM instance.
8. Clean up the conversion instance and any unwanted or left over volumes/snapshots.
If you liked the post, then you may purchase my first cup of coffee ever, thanks in advance :)