{"id":2778,"date":"2014-11-05T07:25:52","date_gmt":"2014-11-05T07:25:52","guid":{"rendered":"http:\/\/blog.cloudthat.com\/?p=2778"},"modified":"2024-06-25T11:14:09","modified_gmt":"2024-06-25T11:14:09","slug":"steps-to-convert-rhel-based-pv-instances-to-hvm","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/steps-to-convert-rhel-based-pv-instances-to-hvm","title":{"rendered":"Steps to convert RHEL-BASED PV instances to HVM"},"content":{"rendered":"<p>My previous blog will give you an idea as to why HVM instances in AWS are better than PV instances and why people prefer using HVM instances over PV instances. Moreover, the HVM instances offered by Amazon are cheaper than the PV ones. For example, PV m1.medium costs $0.087 per Hour whereas, HVM m3.medium costs $0.070 per Hour. Inspite of the reduced price, we get same memory plus one more extra core with HVM. If you have a PV machine, you should definitely convert it to HVM because of the lower cost and also, PV instances might retire eventually.<\/p>\n<h2>Here are the detailed steps for doing so:<\/h2>\n<p>These steps are strictly for <b>RHEL based <\/b>PV instances like Amazon Linux, CentOS, RedHat etc.<\/p>\n<p>1. Login to your instance (SSH) .<\/p>\n<p>2. Install grub on it.<\/p>\n<p><i>*<\/i><b><i>Grub<\/i><\/b><i> selects a specific kernel configuration available on a particular operating system&#8217;s partitions.<\/i><\/p>\n<pre class=\"lang:default decode:true\">sudo yum install grub \u2013y<\/pre>\n<p>3. Stop the instance and create snapshot of the root volume.<\/p>\n<p><i>*To overcome the<\/i><b><i> downtime<\/i><\/b><i> here, you can alternatively create an image of your instance and launch another machine.<\/i><\/p>\n<p>4. \u00a0Launch an Amazon Linux &#8216;<i>working<\/i>&#8216; instance (PV) and login to that instance.<\/p>\n<pre class=\"lang:default decode:true\">ssh -i path-to-your-pem-file.pem ec2-user@&lt;machine-public-dns-name&gt;<\/pre>\n<p>5. Create a new &#8216;source&#8217; volume (in the same AZ) from the snapshot created in step 3 and attach it to the &#8216;<i>working<\/i>&#8216; instance as \/dev\/xvdf. The ideal size of this volume could be kept same as your source instance (Step 1) or it can even be increased.<\/p>\n<p>6. Create a new &#8216;destination&#8217; volume (in the same AZ) from the snapshot created in step 3 and attach it to the &#8216;<i>working<\/i>&#8216; instance as \/dev\/xvdo.<\/p>\n<p>7. Next, we partition the destination volume.<\/p>\n<p>a. We use <em><strong>parted<\/strong><\/em> for partitioning the destination volume.<\/p>\n<p><i>*<\/i><b><i>Parted <\/i><\/b><i>is a program for creating and manipulating partition tables.<\/i><\/p>\n<pre class=\"lang:default decode:true\">sudo parted \/dev\/xvdo --script 'mklabel msdos mkpart primary 1M -1s print quit'<\/pre>\n<p>Output:<\/p>\n<pre class=\"lang:default decode:true \">Model: Xen Virtual Block Device (xvd)\r\n\r\nDisk \/dev\/xvdo: 8590MB\r\n\r\nSector size (logical\/physical): 512B\/512B\r\n\r\nPartition Table: msdos\r\n\r\nNumber \u00a0Start \u00a0\u00a0End \u00a0\u00a0\u00a0Size \u00a0Type \u00a0\u00a0\u00a0\u00a0File system \u00a0Flags\r\n\r\n1 \u00a0\u00a0\u00a0\u00a0\u00a01049kB \u00a08590MB \u00a08589MB \u00a0primary\r\n\r\n<\/pre>\n<p>b. The OS \u00a0kernel checks the partition table of destination volume which was modified and updates the table.<\/p>\n<p><i>*<\/i><b><i>partprobe<\/i><\/b><i> to informs the operating system kernel of partition table changes, by requesting that the operating system re-read the partition table.<\/i><\/p>\n<pre class=\"lang:default decode:true\">\u00a0sudo partprobe \/dev\/xvdo<\/pre>\n<p>c. Update the destination information.<\/p>\n<p><b><i>*udevadm settle\u00a0<\/i><\/b><i>waits for <\/i><i>udevd<\/i><i> to process the device creation events for all hardware devices, thus ensuring that any device nodes have been created successfully before proceeding.<\/i><\/p>\n<pre class=\"lang:default decode:true\">\u00a0 udevadm settle<\/pre>\n<p>8. \u00a0Check the &#8216;source&#8217; volume.<b>\u00a0\u00a0<\/b><\/p>\n<p><i>a. <\/i><b><i>e2fsck<\/i><\/b><i> is used to check a Linux second extended file system.<\/i><\/p>\n<pre class=\"lang:default decode:true\">\u00a0sudo e2fsck -f \/dev\/xvdf<\/pre>\n<p>Output:<\/p>\n<pre class=\"lang:default decode:true\">e2fsck 1.42.8 (4-Nov-2014)\r\n\r\nPass 1: Checking inodes, blocks, and sizes\r\n\r\nPass 2: Checking directory structure\r\n\r\nPass 3: Checking directory connectivity\r\n\r\nPass 4: Checking reference counts\r\n\r\nPass 5: Checking group summary information\r\n\r\n\/: 39159\/524288 files (0.3% non-contiguous), 366426\/2097152 blocks<\/pre>\n<p>b. Minimize the size of original filesystem to speed up the process and also to eliminate a copy free disk space in the next step.<\/p>\n<p><b><i>*resize2fs<\/i><\/b><i> program will resize ext2, ext3, or ext4 file systems. Run resize2fs to resize the ext2 filesystem to use all of the space in the newly enlarged partition.<\/i><\/p>\n<pre class=\"lang:default decode:true\">\u00a0sudo resize2fs -M \/dev\/xvdf<\/pre>\n<p>Output:<\/p>\n<pre class=\"lang:default decode:true \">resize2fs 1.42.8 (4-Nov-2014)\r\nResizing the filesystem on \/dev\/xvdf to 346784 (4k) blocks.\r\n\r\nThe filesystem on \/dev\/xvdf is now 346784 blocks long.<\/pre>\n<p><b><i>Note:<\/i><\/b><i> Write down the blocks and 4k values.<\/i><\/p>\n<p>9. Duplicate the source volume to the newly partitioned destination volume.<\/p>\n<p><i>*<\/i><b><i>dd<\/i><\/b><i> is a command on Unix systems whose primary purpose is to convert and copy a file.<\/i> <i>Note<\/i>: Make sure you specify the exact number of bs and count values. (returned from the previous command). The execution of this command might take some time.<\/p>\n<pre class=\"lang:default decode:true\">sudo dd if=\/dev\/xvdf of=\/dev\/xvdo1 bs=4K count=346784<\/pre>\n<p>Output:<\/p>\n<pre class=\"lang:default decode:true\">346784+0 records in\r\n\r\n346784+0 records out\r\n\r\n1420427264 bytes (1.4 GB) copied, 166.497 s, 8.5 MB\/s<\/pre>\n<p>10. \u00a0Resize the filesystem on the newly partitioned destination volume.<\/p>\n<pre class=\"lang:default decode:true\">sudo resize2fs \/dev\/xvdo1<\/pre>\n<p>11. \u00a0Next, we have to prepare the newly partitioned destination volume for the chrooted grub installation.<\/p>\n<p>a. Mount the newly partitioned destination.<\/p>\n<pre class=\"lang:default decode:true\">\u00a0sudo mount \/dev\/xvdo1 \/mnt<\/pre>\n<p>b. Copy all the files archived from old destination to the newly partitioned one.<\/p>\n<pre class=\"lang:default decode:true\">\u00a0sudo cp -a \/dev\/xvdo \/dev\/xvdo1 \/mnt\/dev\/\r\n<\/pre>\n<p>c.<\/p>\n<pre class=\"lang:default decode:true\">\u00a0sudo rm -f \/mnt\/boot\/grub\/*stage*<\/pre>\n<p>d.<\/p>\n<pre class=\"lang:default decode:true\">sudo cp \/mnt\/usr\/*\/grub\/*\/*stage* \/mnt\/boot\/grub\/<\/pre>\n<p>e.<\/p>\n<pre class=\"lang:default decode:true\">sudo rm -f \/mnt\/boot\/grub\/device.map<\/pre>\n<p>12. Next, we do an offline grub installation on the destination device in the chroot environment, which is required for the HVM instance. You need to be a root user to perform this step:<\/p>\n<pre class=\"lang:default decode:true\">\u00a0\u00a0sudo su<\/pre>\n<p>Execute the code below:<\/p>\n<pre class=\"lang:default decode:true\">sudo cat &lt;&lt;EOF | chroot \/mnt grub \u2013batch\r\n\r\ndevice (hd0) \/dev\/xvdo\r\n\r\nroot (hd0,0)\r\n\r\nsetup (hd0)\r\n\r\nEOF<\/pre>\n<p>13. \u00a0\u00a0Remove the old destination volume from the new destination volume.<\/p>\n<pre class=\"lang:default decode:true\">sudo rm -f \/mnt\/dev\/xvdo \/mnt\/dev\/xvdo1<\/pre>\n<p>14. Updating the new grub configuration. <b>\u00a0<\/b><\/p>\n<p><b>*tty<\/b> prints the filename of the terminal connected to standard input.<\/p>\n<pre class=\"lang:default decode:true\">vi \/mnt\/boot\/grub\/menu.lst<\/pre>\n<p>Change root (hd0) to root (hd0,0)<\/p>\n<p>&#8211; Add (or replace console=*) console=ttyS0 to kernel line.<\/p>\n<p>&#8211; Replace root=* with root=LABEL=\/ in the kernel line.<\/p>\n<p>&#8211; Go to \/mnt\/boot\/grub\/menu.lst &#8211; *ensure the filenames match the files in \/mnt\/boot<\/p>\n<p>a.<\/p>\n<pre class=\"lang:default decode:true\">\u00a0cd \/mnt\/boot<\/pre>\n<p>b.<\/p>\n<pre class=\"lang:default decode:true\">\u00a0 ls<\/pre>\n<p>Output:<\/p>\n<pre class=\"lang:default decode:true \">config-3.4.43-43.43.amzn1.x86_64 \u00a0initramfs-3.4.43-43.43.amzn1.x86_64.img\r\n\r\nefi \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.map-3.4.43-43.43.amzn1.x86_64\r\n\r\ngrub \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0vmlinuz-3.4.43-43.43.amzn1.x86_64<\/pre>\n<p>c. Once you verify that the filenames are same, save and quit the file (:wq).<\/p>\n<pre class=\"lang:default decode:true\">vi \/mnt\/boot\/grub\/menu.lst<\/pre>\n<p>15. Update.<\/p>\n<p><b><i>*fstab<\/i><\/b><i> gives you the static information about the filesystems.<\/i><\/p>\n<pre class=\"lang:default decode:true\">vi \/mnt\/etc\/fstab<\/pre>\n<p>Update the root (\/) entry in \/mnt\/etc\/fstab, as per the sample fstab below:<\/p>\n<p>Output:<\/p>\n<pre class=\"lang:default decode:true \">LABEL=\/ \/ ext4 defaults,noatime 1 1\r\n\r\nnone \/dev\/pts devpts gid=5,mode=620 0 0\r\n\r\nnone \/dev\/shm tmpfs defaults 0 0\r\n\r\nnone \/proc proc defaults 0 0\r\n\r\nnone \/sys sysfs defaults 0 0\u00a0\r\n<\/pre>\n<p>16. Change the label on the filesystem.<\/p>\n<p>a.\u00a0<b>e2label<\/b> command changes the label on an ext2 filesystem i.e; \/dev\/xvdo1<\/p>\n<pre class=\"lang:default decode:true\">\u00a0e2label \/dev\/xvdo1 \/<\/pre>\n<p>b.<\/p>\n<pre class=\"lang:default decode:true\">\u00a0 sync<\/pre>\n<p>c. Come out of mnt directory and unmount the device:<\/p>\n<pre class=\"lang:default decode:true\">\u00a0umount \/mnt<\/pre>\n<p>17. <b>Final Steps<\/b>. Create a snapshot of the &#8216;destination&#8217; volume. Select your snapshot and under <b>Actions<\/b> click <b>Create Image<\/b> as shown in the below image. <a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/Screen-Shot-2014-11-04-at-4.43.00-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2779\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/Screen-Shot-2014-11-04-at-4.43.00-PM.png\" alt=\"Screen Shot 2014-11-04 at 4.43.00 PM\" width=\"399\" height=\"173\" \/><\/a><\/p>\n<p>Select <b>Virtualization type<\/b> as Hardware-assisted virtualization, use the default <b>Kernel ID<\/b> and click Create.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/Screen-Shot-2014-11-04-at-4.44.33-PM-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-2798\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/Screen-Shot-2014-11-04-at-4.44.33-PM-1-1024x452.png\" alt=\"Screen Shot 2014-11-04 at 4.44.33 PM (1)\" width=\"940\" height=\"414\" \/><\/a><\/p>\n<p>Launch instance from the above AMI. The new HVM instance should now boot successfully and will be an exact copy of the old source PV instance (if you used the correct source volume). Once you have confirmed that everything is working, the source instance can be stopped. Cleanup by removing all temporary volumes (source and destination).<\/p>\n<p>Stay tuned to my next <a href=\"https:\/\/blog.cloudthat.com\/steps-to-convert-ubuntu-based-pv-instances-to-hvm\/\">blog<\/a> where I talk about converting Ubuntu-based PV to HVM along with detailed steps.<\/p>\n<p>Let me know if you have any questions by commenting below.<\/p>\n","protected":false},"author":219,"featured_media":0,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3606,3607,3664,3665],"user_email":"prarthitm@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/2778"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/219"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=2778"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/2778\/revisions"}],"predecessor-version":[{"id":45658,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/2778\/revisions\/45658"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=2778"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=2778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}