Virtualization is the creation of a virtual — rather than actual — version of something, such as an operating system, a server, a storage device or network resources.
Linux Amazon Machine Images use one of two types of virtualization: paravirtual (PV) or hardware virtual machine (HVM). The main difference between PV and HVM AMIs is the way in which they boot and whether they can take advantage of special hardware extensions like CPU, network and storage for better performance.
Until now, most average Amazon (AWS) users could effectively ignore the distinction between paravirtual instances (PV) and hardware assisted virtual instances (HVM). HVM was a technology reserved for high-performance, large-capacity use cases. But with changes being made in AWS, all users can and should be considering HVM for their instances.
AWS is providing newer and better offerings for HVM to the exclusion of PV. Even if you want to convert existing PV instances to HVM, it’s always easy or possible. There’s no additional cost, so you should make HVM your default choice for any new EC2 instances you deploy.
Steps to Convert instances PV to HVM
- Instance must be EBS-backed (EBS root drive)
- Grub must be installed
- A ‘working’ EC2 instance with Amazon Linux
- Sufficient privileges in AWS
- Root privileges for the original instance
1. Install grub on the source instance
sudo yum install grub -y
2. Create an EC2 instance in the same AZ, using Amazon Linux 64-bit
3. Check the root EBS volume. If it has a marketplace product code AWS will likely block you from attaching it to another instance unless it’s the root volume.
4. Stripping the marketplace code (if necessary):
i. Create a new EBS volume in the same AZ as the source (size equal or greater).
ii. Attach this empty volume to the source instance.
iii. SSH to the source instance.
iv. Use dd command to copy the source root drive to the new empty volume
sudo dd if=/dev/<source> of=/dev/<destination> bs=4k conv=notrunc,noerror,sync
v. Wait a while for it to complete.
5. Detach the newly created (and cloned) volume from the source instance and attach it to the Amazon ‘working’ instance as /dev/sdm
6. Create another EBS volume of the same or equal size, attach it to the Amazon Linux ‘working’ server as /dev/sdo.
7. SSH to the Amazon Linux server. Change to root
sudo su –
8. Partition the ‘destination’ volume.
parted /dev/xvdo –script ‘mklabel msdos mkpart primary 1M -1s print quit’
9. Check the source file system using fsck.ext4 or whatever is appropriate for your source
e2fsck -f /dev/xvdm
10. Minimize the original filesystem to speed up the copy
resize2fs -M /dev/xvdm
Observe the output from the resize2fs and note it down for the next step.
11. Duplicate ‘source’ to ‘destination’ volume.
dd if=/dev/xvdm of=/dev/xvdo1 bs=<block size from previous step> count=<use block count from last step>
12. Expand the FS on the new partition
13. Prepare the ‘destination’ volume for the chrooted grub installation
mount /dev/xvdo1 /mnt
cp -a /dev/xvdo /dev/xvdo1 /mnt/dev/
rm -f /mnt/boot/grub/*stage*
cp /mnt/usr/*/grub/*/*stage* /mnt/boot/grub/
rm -f /mnt/boot/grub/device.map
14. Install grub in the chroot environment. This step will do an offline grub installation on the destination device, which is required for the HVM instance:
cat <<EOF | chroot /mnt grub –batch
device (hd0) /dev/xvdo
15. Remove the temporary device from the destination volume, which was required to install grub (as above)
rm -f /mnt/dev/xvdo /mnt/dev/xvdo1
16. Update the grub configuration
– Change root (hd0) to root (hd0,0)
– Add (or replace console=*) console=ttyS0 to kernel line
– Replace root=* with root=LABEL=/ in the kernel line
– Add xen_pv_hvm=enable to kernel line (No longer required, but I still add it)
If there are two entries then make these modifications to both.
17. Update fstab
LABEL=/ / ext4 defaults,noatime 1 1
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
(keep or comment any other attached volumes)
18. Create the label on /dev/xvdo1 and unmount the device.
e2label /dev/xvdo1 /
19. Return to the AWS console. Find the new ‘destination’ volume and make a snapshot. Create a new HVM AMI from this snapshot. Be sure to select virtualization type = HVM.
20. Launch a new instance from this HVM AMI and confirm it works.
21. Terminate the existing PV instance (terminate, not stop).
This process takes a Paravirtualized instance, turns it off. Then it takes a snapshot of an EBS volume, makes a new AMI using this snapshot. By using this new AMI it launches an HVM instance. This way user can easily convert a Paravirtualized instance into HVM instance.