Upgrade Old Instance to M5 C5 R5 Instance

Today we will upgrade our old m4.large RHEL instance to m5.large RHEL instance.

But before doing this activity, we have to check if our instance is compatible or not. As C5,M5 instances support the next generation Elastic Network Adapter (ENA) technology. The C4,M4 instance use the Intel 82599 VF interface for enhanced networking. This is a different mechanisms than the ENA. Also, instance types that support NVMe, such as C5, C5d, M5, M5d, R5, R5d, T3, and z1d, expose EBS volumes as NVMe block devices.

So to perform this activity,we will follow the below steps:

Step 1: Access the server which you want to upgrade.

Step 2: Create a new file "check.sh" in the server.

1
2▶ vim check.sh

Step 3: Copy the below code and paste it in the "check.sh".

  1
  2#!/bin/bash
  3
  4########################################################################
  5
  6# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  7#
  8# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
  9# except in compliance with the License. A copy of the License is located at
 10#
 11#     http://aws.amazon.com/apache2.0/
 12#
 13# or in the "license" file accompanying this file. This file is distributed on an "AS IS"
 14# BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 15# License for the specific language governing permissions and limitations under the License.
 16
 17
 18check_NVMe_in_initrd () {
 19
 20is_amazon=`cat /etc/os-release |sed -n 's|^ID="\([a-z]\{4\}\).*|\1|p'`      # Check if instance is using amazon AMI.
 21
 22    if [ -f /etc/redhat-release ] ; then
 23        # Distribution is Red hat
 24        lsinitrd /boot/initramfs-$(uname -r).img|grep nvme > /dev/null 2>&1
 25        if [ $? -ne 0 ]; then
 26        # NVMe module is not loaded in initrd/initramfs
 27        echo -e "\n\nERROR  NVMe Module is not loaded in the initramfs image.\n\t- Please run the following command on your instance to recreate initramfs:"
 28        echo -e '\t# sudo dracut -f -v'
 29        fi
 30
 31    elif [[ "${is_amazon}" == "amzn" ]]; then
 32        # Amazon Linux
 33        lsinitrd /boot/initramfs-$(uname -r).img|grep nvme > /dev/null 2>&1
 34        if [ $? -ne 0 ]; then
 35        # NVMe module is not loaded in initrd/initramfs
 36        echo -e "\n\nERROR  NVMe Module is not loaded in the initramfs image.\n\t- Please run the following command on your instance to recreate initramfs:"
 37        echo -e '\t# sudo dracut -f -v'
 38        fi
 39
 40    elif [ -f /etc/SuSE-release ] ; then
 41        # Distribution is SuSe Linux
 42        lsinitrd /boot/initrd-$(uname -r)|grep nvme > /dev/null 2>&1
 43        if [ $? -ne 0 ]; then
 44        # NVMe module is not loaded in initrd/initramfs
 45        echo -e "\n\nERROR  NVMe Module is not loaded in the initramfs image.\n\t- Please run the following command on your instance to recreate initramfs:"
 46        echo -e '\t# sudo dracut -f -v'
 47        fi
 48
 49    elif [ -f /etc/debian_version ] ; then
 50        # Distribution is debian based(Debian/Ubuntu)
 51        lsinitramfs /boot/initrd.img-$(uname -r)|grep nvme > /dev/null 2>&1
 52        if [ $? -ne 0 ]; then
 53        # NVMe module is not loaded in initrd/initramfs
 54        echo -e "\n\nERROR  NVMe Module is not loaded in the initramfs image.\n\t- Please run the following command on your instance to recreate initramfs:"
 55        echo -e '\t# sudo update-initramfs -c -k all'
 56        fi
 57
 58    else
 59        echo -e "\n\nUnsupported OS for this script."
 60        echo -e "\n\n------------------------------------------------"
 61        exit 1
 62    fi
 63}
 64########################################################################
 65
 66check_fstab () {
 67    time_stamp=$(date +%F-%H:%M:%S)
 68    cp /etc/fstab /etc/fstab.backup.$time_stamp
 69    sed -n 's|^/dev/\([sx][v]*d[a-z][0-9]*\).*|\1|p' </etc/fstab >/tmp/device_names   # Stores all /dev/sd* and /dev/xvd* entries from fstab into a temporary file
 70    while read LINE; do
 71            # For each line in /tmp/device_names
 72            UUID=`ls -l /dev/disk/by-uuid | grep "$LINE" | sed -n 's/^.* \([^ ]*\) -> .*$/\1/p'` # Sets the UUID name for that device
 73            if [ ! -z "$UUID" ]
 74            then
 75                sed -i "s|^/dev/${LINE}|UUID=${UUID}|" /etc/fstab               # Changes the entry in fstab to UUID form
 76            fi
 77    done </tmp/device_names
 78
 79    if [ -s /tmp/device_names ]; then
 80
 81        echo -e "\n\nERROR  Your fstab file contains device names. Mount the partitions using UUID's before changing an instance type to M5/C5."                                                         # Outputs the new fstab file
 82        printf "\nPress y to replace device names with UUID in your fstab file? (y/n) "
 83        read RESPONSE;
 84        case "$RESPONSE" in
 85            [yY]|[yY][eE][sS])                                              # If answer is yes, keep the changes to /etc/fstab
 86                    echo "Writing changes to /etc/fstab..."
 87                    echo -e "\n\n***********************"
 88                    cat /etc/fstab
 89                    echo -e "***********************"
 90                    echo -e "\nOriginal fstab file is stored as /etc/fstab.backup.$time_stamp"
 91                    ;;
 92            [nN]|[nN][oO]|"")                                               # If answer is no, or if the user just pressed Enter
 93                    echo -e "Aborting: Not saving changes...\nPrinting correct fstab file below:"                  # don't save the new fstab file
 94                    cat /etc/fstab
 95                    cp /etc/fstab.backup.$time_stamp /etc/fstab
 96                    rm /etc/fstab.backup.$time_stamp
 97                    ;;
 98            *)                                                              # If answer is anything else, exit and don't save changes
 99                    echo "Invalid Response"                                 # to fstab
100                    echo "Exiting"
101                    cp /etc/fstab.backup.$time_stamp /etc/fstab
102                    rm /etc/fstab.backup.$time_stamp
103                    exit 1
104                    echo -e "------------------------------------------------"
105                    ;;
106
107        esac
108        rm /tmp/device_names
109
110    else
111        echo -e "\n\nOK     fstab file looks fine and does not contain any device names. "
112    fi
113
114}
115
116########################################################################
117
118
119# Main code starts from here
120
121PATH=/bin:/sbin:/usr/bin:/usr/sbin
122
123if [ `id -u` -ne 0 ]; then                                              # Checks to see if script is run as root
124        echo -e "------------------------------------------------"
125        echo -e "\nThis script must be run as root" >&2                 # If it isn't, exit with error
126        echo -e "\n------------------------------------------------"
127        exit 1
128fi
129
130modinfo nvme > /dev/null 2>&1
131if [ $? -ne 0 ]
132    then
133    # NVMe Module is not installed.
134    echo -e "------------------------------------------------\nERROR  NVMe Module is not available on your instance. \n\t- Please install NVMe module before changing your instance type to M5/C5. Look at the following link for further guidance:"
135    echo -e "\t> https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html"
136
137else
138    echo -e "------------------------------------------------\n"
139    echo -e "OK     NVMe Module is installed and available on your instance"
140    check_NVMe_in_initrd                # Calling function to check if NVMe module is loaded in initramfs.
141fi
142
143
144modinfo ena > /dev/null 2>&1
145if [ $? -ne 0 ]
146    then
147    # ENA Module is not installed.
148    echo -e "\n\nERROR  ENA Module is not available on your instance. \n\t- Please install ENA module before changing your instance type to M5/C5. Look at the following link for further guidance:"
149    echo -e "\t> https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking-ena.html#enhanced-networking-ena-linux"
150
151else
152    ena_version=`modinfo ena|grep -Eo '^version:.*' | awk '{print $2}'`
153    echo -e "\n\nOK     ENA Module with version $ena_version is installed and available on your instance"
154
155fi
156
157
158check_fstab
159echo -e "\n------------------------------------------------"

Step 4: Save the file "check.sh" by pressing [esc] key and then enter below command:

1:wq!

Step 5: Give executable permission to the file.

1
2▶ chmod +x check.sh

Step 6: Run the file.

1
2▶ ./check.sh

Step 7: Check the Output:

If everything is fine you will get this message:

1
2OK     NVMe Module is installed and available on your instance
3OK     ENA Module with version 1.4.0U is installed and available on your instance
4OK     fstab file looks fine and does not contain any device names.

If the c5_m5_checks_script throws errors, you will have to follow the below steps :

1: To resize an existing instance to an instance type that supports enhanced networking, you must first install the ENA drivers or ixgbevf drivers on your instance, as appropriate. Please follow the steps mentioned in the following link to test/enable ENA.

2: To resize an existing instance to an instance type that supports NVMe, you must first install the NVMe drivers on your instance. Also, the device names for devices that you specify in the block device mapping are renamed using NVMe device names (/dev/nvme[0-26]n1). Therefore, to mount file systems at boot time using /etc/fstab, you must use UUID/Label instead of device names.

Step 8: If everything work fine, stop the server and changed the instances type from m4.large to m5.large.

Step 9: Last, Start your instance. Now it will upgraded to latest family instances.

Enjoy :)