Increase the size of an Amazon EBS volume on an EC2 instance

Increase the size of an Amazon EBS volume on an EC2 instance

·

11 min read

This tutorial shows you how to launch an Amazon EC2 instance and add an Amazon Elastic Block Store (Amazon EBS) volume using the EC2 launch instance wizard. The added volume must be formatted with a file system and mounted to be available for use.

This tutorial uses an Amazon EC2 T3 instance, which is a low-cost general-purpose instance type based on the Nitro System.

Consider the following as you complete the steps in this tutorial:

  • Two volumes – After you launch your Amazon EC2 instance you have two volumes—a root volume and an added volume. To differentiate between these volumes, the added volume will be called a data volume in this tutorial.

  • Data volume availability – To make the data volume available, you need to format and mount the data volume.

  • File system – To take advantage of the increased size of the data volume, the file system needs to be extended.


Step 1: Launch EC2 instance.

  • Provide name as tutorial-volumes.

  • AMI: Ubuntu

  • Instance type: t3.micro

  • Create key pair:

    • Under Network settings, ensure that Allow SSH traffic check box is selected. This allows you to connect to your instance using SSH.

    • Under Configure storage, choose Add new volume. Ensure that the added EBS volume size is 8 GB and the type is gp3.

      • To ensure that the data volume is deleted upon termination of the instance, choose Advanced. Under Volume 2 (Custom), select Delete on termination, and select Yes.

  • Launch instance.

Step 2: Make the data volume available for use

The Amazon EC2 instance launched in Step 1 has two EBS volumes—a root device and an added storage device (the data volume). Use the following procedure to make the data volume available for use.

In this tutorial, you use the XFS file system.

XFS file system

  • Commonly used in Linux, particularly in enterprise environments.

  • Known for scalability and high-performance attributes.

The instance type (t3.micro) used in this tutorial is built on the Nitro System, which exposes EBS volumes as NVMe block devices.

Here's a breakdown of the key components:

Nitro System: This is a technology framework used by Amazon Web Services (AWS) for their EC2 instances. It's designed to enhance the performance, security, and scalability of virtual machines.

NVMe block devices: NVMe stands for Non-Volatile Memory Express, which is a high-speed interface used for storage devices like SSDs. When Nitro-based instances access EBS volumes as NVMe block devices, it means they can work efficiently with the attached storage, allowing for faster data access and better performance.

To format and mount the data volume

  • Connect to your instance

Paste the following command:

sudo lsblk -f

sudo: This is a command that allows you to execute another command with superuser privileges, essentially running the following command with administrative rights.

lsblk: This is a command used to list information about block devices, which typically include hard drives, SSDs, USB drives, and other storage devices. It provides details about the devices and their partitions.

-f: This is an option or flag for the lsblk command. The -f flag instructs lsblk to display additional information about the filesystems on the block devices. This can include information such as the filesystem type (e.g., ext4, ntfs), UUIDs, and labels.

When you run sudo lsblk -f, you'll get a listing of the block devices on your system along with information about the filesystems they contain.

The output of lsblk removes the /dev/ prefix from full device paths.

This is done to make the output cleaner and more human-readable. It only displays the name of the device or partition without the /dev/ prefix.

For example, if you have a device located at /dev/sda, lsblk will display it as sda. Similarly, partitions like /dev/sda1 would be displayed as sda1.

The /dev directory is a special directory known as the "device file system." It contains special files that represent and allow access to various hardware devices and device drivers on the system.

The following example output shows that there are two devices attached to the instance—the root device /dev/nvme0n1 and the data volume /dev/nvme1n1. In this example, you can see under the column FSTYPE that the data volume does not have a file system.

The nvme0n1 device is further divided into partitions (nvme0n1p1, nvme0n1p14, and nvme0n1p15). The first partition (nvme0n1p1) is using the ext4 filesystem and is mounted as the root filesystem (/). The last partition (nvme0n1p15) is using the vfat filesystem and is mounted as the EFI system partition (/boot/efi).

To create a file system, use the following command with the data volume name (/dev/nvme1n1).

Paste the following command:

sudo mkfs -t xfs /dev/nvme1n1

mkfs: make file system

-t: type

xfs: one of the file system

/dev/nvme1n1: at this location

New volumes are raw block devices, and you must create a file system on them before you can mount and use them. Volumes that were created from snapshots likely have a file system on them already; if you create a new file system on top of an existing file system, the operation overwrites your data.

To create a mount point directory for the data volume, use the following command.

sudo mkdir /data

The mount point is where the volume is located in the file system tree and where you read and write files to after you mount the volume. The following example creates a directory named /data.

A mount point is the location in your file system where you can access the contents of a disk or storage device. When you mount a storage device at a specific directory, the contents of that device become accessible through that directory.

To mount the data volume at the directory you just created, use the following command with the data volume name (/dev/nvme1n1) and the mount point directory name (/data).

sudo mount /dev/nvme1n1 /data

When you manually mount a data volume on your Linux instance, the information about the mount point (the location where the volume is attached within the file system) is not automatically saved, and it won't persist after the instance is restarted or rebooted.

Let's see it happening.

After running the above command, type

sudo lsblk -f

You can see the mount point directory for nvme1n1 as /data.

But when we reboot our instance and type the command sudo lsblk -f, it'll be gone.

To reboot an instance, you can choose two ways:

  • sudo reboot (refresh after a while)

  • manually rebooting the instance from the EC2 console and then connecting again.

To make it stick,

We follow the following commands:

  • Find the UUID of the Volume:

    • To ensure that the volume gets mounted correctly, it's a good practice to use the Universally Unique Identifier (UUID) of the volume instead of the device name. To find the UUID, use the blkid command:

        sudo blkid /dev/nvme1n1 # Replace /dev/nvme1n1 with the actual device name (if any)
      

  • Note down the UUID for the next step.

  • Edit the /etc/fstab File:

    [ In Unix-like operating systems, including Linux, the "etc" directory (short for "et cetera") is a common directory that contains a wide variety of configuration files and subdirectories that govern how the operating system and various software applications work. It is typically located at the root of the file system, and its full path is often "/etc." ]

    • Open the /etc/fstab file in a text editor with superuser privileges (e.g., using sudo nano, sudo vi, or your preferred text editor):

    • I am using vim editor here, you can choose either vim or nano.

        sudo vim /etc/fstab
      
      • Click i to enter into insert mode.

      • Type the following command:

      • Replace <****> with your UUID that you had noted earlier.

          UUID=<****>  /data  xfs  defaults,nofail  0  2
        

In the following example, we mount the device with UUID 01981eba-31ca-4504-9703-493b38bd4705 to mount point /data and we use the xfs file system. We also use the defaults and nofail flags. We specify 0 to prevent the file system from being dumped, and we specify 2 to indicate that it is a non-root device.

Now, Ctrl+C to exit from INSERT mode and type :wq! to Save and Exit.

  • Type the following command:

      sudo mount -a
    

    The command sudo mount -a is used to remount all file systems specified in the /etc/fstab file. Here's what it does:

    1. mount: This is the Linux command used to mount file systems. It attaches a file system to the file system hierarchy and makes it accessible.

    2. -a: This option tells the mount command to mount all file systems listed in the /etc/fstab file that have not already been mounted.

Running sudo mount -a is typically used after editing the /etc/fstab file to apply the changes without requiring a system reboot.

You can now run the following command to see the mount point /data.

sudo lsblk -f

Step 3: Increase the size of the data volume

Suppose after six months of using the above configuration you run out of storage space on your data volume. You decide to double the size of your data volume. To do this, first you create a snapshot, and then you increase the size of the data volume. When you create a snapshot, use a description to identify the snapshot. In this tutorial you create a snapshot called tutorial-volumes-backup.

When you modify an EBS volume, it goes through a sequence of states—modifying, optimizing, and completed. Keep in mind that the file system cannot be extended until the volume enters the optimizing state.

To increase the size of the data volume
  1. Create a snapshot of the data volume, in case you need to roll back your changes.

    1. In the Amazon EC2 console, in the navigation pane, choose Instances, and select tutorial-volumes.

    2. On the Storage tab, under Block devices select the Volume ID of the data volume.

    3. On the Volumes detail page, choose Actions, and Create snapshot.

    4. Under Description, enter tutorial-volumes-backup.

    5. Choose Create snapshot.

  2. To increase the data volume size, in the navigation pane, choose Instances, and select tutorial-volumes.

  3. Under the Storage tab, select the Volume ID of your data volume.

    You should be selecting the data volume. To distinguish, you can see the root volume as:

  4. Select the check box for your Volume ID, choose Actions, and then Modify volume.

  5. The Modify volume screen displays the volume ID and the volume’s current configuration, including type, size, input/output operations per second (IOPS), and throughput. In this tutorial you double the size of the data volume.

    1. For Volume type, do not change value.

    2. For Size, change to 16 GB.

    3. For IOPS, do not change value.

    4. For Throughput, do not change value.

  6. Choose Modify, and when prompted for confirmation choose Modify again. You are charged for the new volume configuration after volume modification starts.

  7. After some time, you can see the changes you've made:

After you've made a modification to an EBS volume, such as resizing it, AWS enforces a waiting period of at least six hours. During this time, you may not perform additional modifications to the same volume.

Step 4: Extend the file system

When you increase the size of an EBS volume on an Amazon EC2 instance, the file system within that volume does not automatically grow to use the newly allocated space. This is because the file system and the storage volume are separate entities, and the file system doesn't automatically know that more space is available.

You need to manually extend the file system to make use of the additional space you've allocated to the EBS volume.

Once the data volume enters the optimizing state, you can use file system-specific commands to extend the file system to the new, larger size.

To extend the file system
  1. In the terminal window, use the following command to get the size of the file system.

     df -hT
    

    The df -hT command is used to display information about mounted file systems in a human-readable and tabular format. Here's a breakdown of what each part of the command does:

    • df: This is the command itself, which stands for "disk free." It's used to report file system disk space usage.

    • -h: This is an option or flag that stands for "human-readable." When used with df, it formats the output in a more human-friendly way, converting sizes to gigabytes (GB), megabytes (MB), and kilobytes (KB) when appropriate.

    • -T: This is another option or flag that, when used with df, includes the file system type in the output. It shows the type of file system (e.g., ext4, xfs, vfat) for each mounted file system.

The following example output shows that the file system size of the data volume /dev/nvme1n1 is 8 GB. In the previous procedure, you increased the data volume size to 16 GB. Now you need to extend the file system to take advantage of the added storage.

Use the following command to extend the XFS file system that is mounted on the /data mount point.

    sudo xfs_growfs -d /data

The sudo xfs_growfs command is used to resize an XFS file system to fill available space on a specified mount point.

Here's a breakdown of the command:

  • xfs_growfs: This is the command for resizing an XFS file system.

  • -d: This option specifies the directory where the XFS file system is mounted. In your case, it's /data.

Use the following command again to verify that the file system has been extended.

    df -hT

You can see the changes.

Step 5: Clean up unneeded resources

It is a good practice to delete resources that you are no longer using. By deleting a resource you'll also stop incurring charges for that resource.

Deleted, terminated, or released resources can't be recovered.

To terminate an instance
  1. Go to the EC2 console.

  2. Select the check box for tutorial-volumes.

  3. Choose Instance state, and then Terminate instance.

  4. Choose Terminate when prompted for confirmation.

To delete a snapshot
  1. In the navigation pane, under Elastic Block Store, choose Snapshots.

  2. Select the check box for tutorial-volumes-backup.

  3. Under Actions, choose Delete snapshot.

  4. Choose Delete when prompted for confirmation.

This concludes the lab.

Thank you.