Convert Your AWS EC2 Linux PV Instance into an HVM Instance
Tags: aws, ec2, linux, hvm
Published: 2022-05-15 13:01:42

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.

## Prerequisites

- 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.

## Instructions

1\. Create an **HVM instance**, using as **close as possible your current OS**, \(i.e., make sure the kernel versions match\).

&ensp;&ensp;&ensp;&ensp;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.

&ensp;&ensp;&ensp;&ensp;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\)

&ensp;&ensp;&ensp;&ensp;2.A. SSH into the conversion instance.

&ensp;&ensp;&ensp;&ensp;2.B. Attach the HVM volume to the conversion instance as _/dev/xvdf_.

```bash
cd /    
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.

&ensp;&ensp;&ensp;&ensp;2.C. Ensure you keep the HVM boot directory by moving it to the tmp directory.

```bash
sudo mv /hvm/boot  /tmp/boot.hvm
```

&ensp;&ensp;&ensp;&ensp;2.D. Empty out the rest of the drive with:

```bash
sudo rm -Rf /hvm/*
```

&ensp;&ensp;&ensp;&ensp;2.E. Verify the drive is now empty with:

```bash
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**.

&ensp;&ensp;&ensp;&ensp;3.A. Attach it to the **conversion** instance as _/dev/xvdg_

```bash
sudo mkdir /pv
sudo mount /dev/xvdg /pv
```

&ensp;&ensp;&ensp;&ensp;3.B. Verify that it is mounted and has the correct file structure.

```bash
# You should see the boot and other root directories.
sudo ls /pv
```

&ensp;&ensp;&ensp;&ensp;3.C. Then copy the contents of /pv to /hvm

```bash
sudo cp -p -R /pv/* /hvm
```

4\. Change the boot directory to be HVM based.

&ensp;&ensp;&ensp;&ensp;4.A. Remove the PV boot directory and replace it with the HVM boot directory.

```bash
sudo rm -R /hvm/boot
```

&ensp;&ensp;&ensp;&ensp;4.B. Copy back the saved HVM boot directory.

```bash
sudo mv /tmp/boot.hvm  /hvm/boot
```

5\. Verify there are now all the needed directories and files on the HVM volume with.

```bash
sudo ls -al /hvm
```

6\. You can now unmount the hvm volume.

```bash
sudo umount /hvm
```

7\. Find the **OS-HVM** volume and detach it from the conversion instance.

&ensp;&ensp;&ensp;&ensp;7.A. Attach it back to the HVM instance as device _/dev/xvda_

&ensp;&ensp;&ensp;&ensp;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 :)**

<a href="https://www.buymeacoffee.com/yasithab" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>

## References

[AWS Linux AMI Virtualization Types](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/virtualization_types.html)