Skip to content

How do I increase the size of an adjacent partition in an Amazon EBS volume on an Amazon EC2 Linux instance?

16 minute read
0

I want to increase the size of an adjacent partition that's blocking another partition in an Amazon Elastic Block Store (Amazon EBS) volume on an Amazon Elastic Compute Cloud (Amazon EC2) Linux instance.

Short description

When you extend an Amazon EBS volume, the system attaches the additional storage space at the end of the disk. If a partition immediately follows another partition on the disk, then you can't expand the adjacent partition.

To resolve this issue, you must create a new, larger Amazon EBS volume with the correct partition layout. Copy all partition data and file system contents from the original volume to the new volume. Then, replace the original volume with the new volume on the instance.

Choose a resolution based on one of the following scenarios:

  • If the blocked partition is on your instance boot volume, then complete the steps in the Expand a root partition in a root volume with an adjacent data partition section. This resolution requires instance downtime, GRand Unified Bootloader (GRUB) reconfiguration, Extensible Firmware Interface (EFI) partition replication, and a root device swap.
  • If the blocked partition is on a non-boot data volume that's attached to your instance, then complete the steps in the Expand a partition in a secondary volume with an adjacent data partition section. Secondary volumes don't contain ESPs, BIOS boot partitions, or bootloader configurations, so no boot partitions, GRUB reinstallation, or instance stop is required.

Resolution

Important: For root volume operations, plan for instance downtime so that you can create snapshots and modify Amazon EBS volumes.

Expand a root partition in a root volume with an adjacent data partition

The following scenario applies to an Amazon Linux 2023 (AL2023) instance. The instance has an Amazon EBS root volume that contains a root partition, /, of 20 GB that's expanding to 40 GB and a data partition, /data, of 10 GB.

Note: The following scenario applies to AL2023 and other Red Hat Packaging (RPM)-based Linux distributions, such as Red Hat Enterprise Linux (RHEL) and CentOS 7 and later versions. Adjust the EFI partition structure, bootloader configuration paths, and file system types. It's a best practice to have a system administrator perform the following resolution.

View the volume partition layout

Run the following fdisk command to view the partition table:

sudo fdisk -l /dev/nvme0n1

Example output:

Device         Start       End   Sectors  Size  Type
/dev/nvme0n1p1 24576     41943006  41918431   20G  Linux filesystem  -> / (XFS)
/dev/nvme0n1p2 41943040  62914526  20971487 10G  Linux filesystem -> /data (XFS)
/dev/nvme0n1p127 22528   24575     2048    1M   BIOS boot (raw, no filesystem)
/dev/nvme0n1p128 2048    22527     20480   10M   EFI System -> /boot/efi (FAT32)

Run the following lsblk command to view the block device layout:

lsblk

Example output:

nvme0n1       259:0  0  30G  0  disk
|-nvme0n1p1   259:1  0  20G  0  part  /
|-nvme0n1p2   259:2  0  10G  0  part  /data
|-nvme0n1p127 259:3  0   1M  0  part
`-nvme0n1p128 259:4  0  10M  0  part  /boot/efi

Create an Amazon EBS snapshot of the source volume

Create an Amazon EBS snapshot of the source volume. For Description, enter pre-partition-resize-backup or something similar.

Create a larger Amazon EBS volume

Create a new Amazon EBS volume as your destination volume. For Volume type, choose the appropriate type, such as gp3. For Size (GiB), enter a larger size, such as 50. For Availability Zone, choose the same Availability Zone as your Amazon EC2 instance. For Description, enter new-root-volume or something similar.

Attach the new volume to an instance

Attach the new destination volume to a running EC2 instance. For Instance, choose your running instance. For Device name, choose an available device name, such as /dev/sdf. Wait for the state to change to in-use.

Note: On newer instance types with NVMe, the device appears as /dev/nvme1n1 after the attachment completes.

Partition the new volume with the correct layout

Complete the following steps:

  1. Run the following gdisk command to partition the new volume:

    sudo gdisk /dev/nvme1n1

    The following session creates four partitions on the new volume:

    Command (? for help): n
    Partition number (1-128, default 1): 128
    First sector (34-104857566, default = 2048) : [Press Enter for default value]
    Last sector (2048-104857566, default = 104857566) or {+-}size{KMGTP}: +10M
    Current type is 8300 (Linux filesystem)
    Hex code or GUID (L to show codes, Enter = 8300): EF00
    Changed type of partition to 'EFI system partition'
    
    Command (? for help): n
    Partition number (1-128, default 1): 127
    First sector (34-104857566, default = 22528) or {+-}size{KMGTP}: [Press Enter for default value]
    Last sector (22528-104857566, default = 104857566) or {+-}size{KMGTP}: +1M
    Current type is 8300 (Linux filesystem)
    Hex code or GUID (L to show codes, Enter = 8300): EF02
    Changed type of partition to 'BIOS boot partition'
    
    Command (? for help): n
    Partition number (1-128, default 1): [Press Enter for default value]
    First sector (34-104857566, default = 24576) or {+-}size{KMGTP}: [Press Enter for default value]
    Last sector (24576-104857566, default = 104857566) or {+-}size{KMGTP}: +40G
    Current type is 8300 (Linux filesystem)
    Hex code or GUID (L to show codes, Enter = 8300): [Press Enter for default value]
    Changed type of partition to 'Linux filesystem'
    
    Command (? for help): n
    Partition number: 2
    First sector: [Press Enter for default value]
    Last sector: [Press Enter for default value]
    Hex code or GUID: 8300

    Note: The preceding partitions include an EFI system partition (ESP) of 10 MB and a BIOS boot partition of 1 MB. The partitions also include a root partition of 40 GB and a data partition for the remaining space.

    Example output:

    Command (? for help): p
    Number  Start (sector)    End (sector)  Size       Code  Name
       1           24576        83910655   40.0 GiB    8300  Linux filesystem
       2        83910656       104857566   10.0 GiB    8300  Linux filesystem
     127           22528           24575   1024.0 KiB  EF02  BIOS boot partition
     128            2048           22527   10.0 MiB    EF00  EFI system partition
  2. For Command, enter w, and for Do you want to proceed? enter Y to write the partition table:

    Command (? for help): w
    Do you want to proceed? (Y/N): Y

    Note: The root partition is now mounted at /, is larger, and uses the additional space. EF02 is the BIOS boot partition type code in gdisk. This partition stores GRUB bootloader stage data and has no file system.

Create file systems on the new partitions

Create the correct file system type on each partition. The ESP requires FAT32, and the BIOS boot partition requires no file system. The root and data partitions use XFS with labels.

Warning: Don't run mkfs on the /dev/nvme1n1p127 BIOS boot partition or the /dev/nvme1n1p128 ESP. The dd command copies the partition data.

Complete the following steps:

  1. Run the following mkfs.xfs command to create the root partition file system:

    sudo mkfs.xfs -L root /dev/nvme1n1p1
  2. Run the following mkfs.xfs command to create the data partition file system:

    sudo mkfs.xfs -L data /dev/nvme1n1p2

    Note: The -L flag sets a file system label. Labels allow /etc/fstab to use LABEL=root and LABEL=data entries instead of device paths or Universally Unique Identifiers (UUIDs).

Migrate data to the new volume

Each partition type requires a different copy method. BIOS boot and EFI use dd and XFS data partitions use rsync.

Complete the following steps:

  1. Run the following dd command to copy the BIOS boot partition in raw format:

    sudo dd if=/dev/nvme0n1p127 of=/dev/nvme1n1p127 bs=512 status=progress
  2. Run the following dd command to copy the ESP in raw format:

    sudo dd if=/dev/nvme0n1p128 of=/dev/nvme1n1p128 bs=512 status=progress
  3. Run the following commands to confirm that the EFI contents copied:

    sudo mkdir -p /mnt/newefi
    sudo mount /dev/nvme1n1p128 /mnt/newefi
    ls -la /mnt/newefi/EFI/BOOT/
    ls -la /mnt/newefi/EFI/amzn/
    sudo umount /mnt/newefi

    Example output:

    /mnt/newefi/EFI/BOOT/BOOTX64.EFI  (~1.3MB)
    /mnt/newefi/EFI/amzn/grub.cfg

Mount the new root and data partitions

Complete the following steps:

  1. Run the following mkdir command to create the mount point for the new root partition:

    sudo mkdir -p /mnt/newroot
  2. Run the following mount command to mount the new root partition:

    sudo mount -o nouuid /dev/nvme1n1p1 /mnt/newroot
  3. Run the following mkdir command to create the mount point for the EFI partition:

    sudo mkdir -p /mnt/newroot/boot/efi
  4. Run the following mount command to mount the EFI partition:

    sudo mount /dev/nvme1n1p128 /mnt/newroot/boot/efi
  5. Run the following mkdir command to create the mount point for the data partition:

    sudo mkdir -p /mnt/newroot/data
  6. Run the following mount command to mount the data partition:

    sudo mount -o nouuid /dev/nvme1n1p2 /mnt/newroot/data
  7. Run the following rsync command to copy the root file system:

    sudo rsync -avxHAX --progress \
       --exclude=/proc/* \
       --exclude=/sys/* \
       --exclude=/dev/* \
       --exclude=/run/* \
       --exclude=/mnt/* \
       --exclude=/media/* \
       / /mnt/newroot/

    Note: The -a flag activates archive mode to keep permissions, ownership, and timestamps. The -v flag activates verbose output. The -x flag limits the copy only to the current file system. The -H flag keeps hard links, -A keeps access control lists (ACLs), and -X keeps extended attributes. The trailing /* on exclude paths excludes the contents of virtual file systems when it keeps the empty directories as bind-mount points.

  8. Run the following rsync command to copy the /data file system:

    sudo rsync -avxHAX --progress /data/ /mnt/newroot/data/

Update /etc/fstab on the new volume

The new volume must have an updated /etc/fstab that references the correct UUIDs and labels for the new partitions.

Complete the following steps:

  1. Run the following commands to get UUIDs and labels for all new partitions:

    sudo blkid /dev/nvme1n1p128
    sudo xfs_admin -l /dev/nvme1n1p1
    sudo xfs_admin -l /dev/nvme1n1p2
  2. Run the following vi command to edit fstab on the new volume:

    sudo vi /mnt/newroot/etc/fstab

    Update the fstab entries to match the following format:

    # EFI System Partition - update UUID from blkid output
    UUID=####-####  /boot/efi   vfat   umask=0077  0 1
    
    # Root partition - LABEL works without changes
    LABEL=root      /           xfs   defaults    0 0
    
    # Data partition - LABEL works without changes
    LABEL=data      /data       xfs   defaults    0 0

    Note: Replace ####-#### with the UUID from the blkid output. XFS file systems use labels with the -L flag during mkfs. Update only the ESP UUID because FAT32 uses a volume ID rather than a label.

Update EFI grub.cfg with the new root UUID

Important: Before you swap volumes, complete the following procedure. If the EFI grub.cfg still contains the old root UUID after the swap, then the instance doesn't boot.

Complete the following steps:

  1. Run the following blkid command to get the UUID of the new root partition:

    sudo blkid /dev/nvme1n1p1

    Example output:

    /dev/nvme1n1p1: LABEL="root" UUID="your-root-partition-UUID" TYPE="xfs"
  2. Run the following cat command to find the old root UUID in the EFI grub.cfg:

    cat /mnt/newroot/boot/efi/EFI/amzn/grub.cfg

    Note: Replace the old UUID with the new UUID from the blkid command output.

  3. Run the following sed command to update the new volume grub.cfg with the new root UUID:

    sudo sed -i 's/OLD-UUID/NEW-UUID/' /mnt/newroot/boot/efi/EFI/amzn/grub.cfg

    Note: Replace OLD-UUID with the old UUID and NEW-UUID with the new UUID.

  4. Run the following cat command to confirm that the UUID updated:

    cat /mnt/newroot/boot/efi/EFI/amzn/grub.cfg

Regenerate the GRUB configuration in chroot

Note: AL2023 with Secure Boot doesn't support the grub2-install command. To regenerate the GRUB menu configuration with the correct kernel paths and root UUID, you must run grub2-mkconfig. To prevent UUID conflicts, use the -o nouuid mount option for XFS file systems. When you simultaneously mount the same XFS file system UUID in two places, use the nouuid option.

Complete the following steps:

  1. Run the following mount command to mount the new root partition:

    sudo mount -o nouuid /dev/nvme1n1p1 /mnt/newroot
  2. Run the following mount command to mount /boot/efi:

    sudo mount /dev/nvme1n1p128 /mnt/newroot/boot/efi
  3. Run the following command to bind-mount virtual file systems to the new root:

    for m in dev proc run sys; do sudo mount -o bind {,/mnt/newroot}/$m; done
  4. Run the following mount command to bind-mount EFI firmware variables:

    sudo mount --bind /sys/firmware/efi/efivars /mnt/newroot/sys/firmware/efi/efivars
  5. Run the following chroot command to enter the chroot environment:

    sudo chroot /mnt/newroot
  6. Run the following ls command to verify that BOOTX64.EFI exists in chroot:

    ls -la /boot/efi/EFI/BOOT/BOOTX64.EFI
  7. Run the following cat command to verify that EFI grub.cfg has the correct UUID inside chroot:

    cat /boot/efi/EFI/amzn/grub.cfg
  8. Run the following grub2-mkconfig command to regenerate the GRUB menu configuration:

    grub2-mkconfig -o /boot/grub2/grub.cfg
  9. Run the following command to confirm that the generated configuration references the correct root UUID inside chroot:

    grep -i 'root=UUID' /boot/grub2/grub.cfg | head -3
  10. Run the following command to exit the chroot environment:

    exit

Validate the new volume configuration and unmount file systems

Complete the following steps:

  1. Run the following fdisk command to confirm that all partitions exist on the new volume with the correct sizes and types:

    sudo fdisk -l /dev/nvme1n1
  2. Run the following xfs_admin commands to make sure that the root and data partitions have the correct file system labels:

    sudo xfs_admin -l /dev/nvme1n1p1
    sudo xfs_admin -l /dev/nvme1n1p2
  3. Run the following cat command to verify that EFI grub.cfg has the new root UUID:

    cat /mnt/newroot/boot/efi/EFI/amzn/grub.cfg
  4. Run the following command to make sure that /boot/grub2/grub.cfg references the correct root UUID:

    grep 'root=UUID' /mnt/newroot/boot/grub2/grub.cfg | head -2
  5. Run the following commands to confirm that root data and /data contents exist on the new root volume:

    ls /mnt/newroot/data/
  6. Run the following commands to make sure that fstab on the new volume has the correct entries and passes findmnt --verify validation:

    cat /mnt/newroot/etc/fstab
    sudo findmnt --verify --tab-file /mnt/newroot/etc/fstab
  7. Run the following commands to unmount the file systems in reverse mount order and release the new root volume:

    sudo umount /mnt/newroot/sys/firmware/efi/efivars
    for m in dev proc run sys; do sudo umount /mnt/newroot/$m; done
    sudo umount /mnt/newroot/boot/efi
    sudo umount /mnt/newroot/data
    sudo umount /mnt/newroot

Stop the instance and swap volumes

Complete the following steps:

  1. Open the Amazon EC2 console.
  2. In the navigation pane, choose Instances and then select your instance.
  3. Choose Instance state, and then choose Stop instance.
    Note: Wait for the instance state to show Stopped.
  4. In the navigation pane, under Elastic Block Store, choose Volumes, and then select the old root volume.
  5. Choose Actions, and then choose Detach volume.
  6. Note the device name that's attached to the source volume, such as /dev/xvda or /dev/sda1.
  7. Select the new volume.
  8. Choose Actions, and then choose Detach volume.
    Note: Wait for the new volume status to show Available.
  9. Select the new volume.
  10. Choose Actions, and then choose Attach volume.
  11. For Instance, choose your instance, and then for Device, enter the root device name.
  12. Choose Attach volume.
  13. Choose Start instance.

Verify file systems and partition layout on the new volume

Complete the following steps:

  1. Run the following df command to use SSH to connect to the instance and confirm that all file systems mounted correctly with the expected sizes:

    df -h
  2. Run the following lsblk command to make sure that the partition layout shows the correct partition sizes, including the expanded root partition:

    lsblk
  3. Run the following fdisk command to make sure that the partition table shows the correct partition types and no overlapping or missing partitions:

    sudo fdisk -l

Expand a partition in a secondary volume with an adjacent data partition

Create a snapshot and new volume

Complete the following steps:

  1. Create a snapshot of the secondary volume.
  2. Create a new volume with a larger size in the same Availability Zone as the instance.
  3. Attach the new volume as a tertiary device to the running instance, for example /dev/nvme2n1.

Partition the new volume

Complete the following steps:

  1. Run the following gdisk command to partition the new volume:

    sudo gdisk /dev/nvme2n1
  2. In your interactive session, enter the following values to create the partitions on the new volume:

    Command (? for help): n
    Partition number (1-128, default 1): 1
    First sector (34-104857566, default = 2048): [Press Enter for default value]
    Last sector (+/-size{K,M,G,T,P}...): [Press Enter to use all space, or specify size such as +40G]
    Hex code or GUID (L to show codes): 8300
    
    Command (? for help): n
    Partition number (2-128, default 2): 2
    First sector: [Press Enter for default value]
    Last sector: [Press Enter to use remaining space]
    Hex code or GUID (L to show codes): 8300
    
    Command (? for help): p

    Note: The interactive session creates the partitions on the new volume. If your layout requires only one partition, then remove the second partition entry.

  3. For Command, enter w, and for Do you want to proceed? enter Y to write the partition table:

    Command (? for help): w
    Do you want to proceed? (Y/N): Y

Create file systems on the new partitions

Complete the following steps:

  1. Run the following mkfs.xfs commands to create file systems on the new partitions:

    sudo mkfs.xfs -L data /dev/nvme2n1p1
    sudo mkfs.xfs -L data2 /dev/nvme2n1p2

    Note: If your layout includes a second data partition, then create the second partition file system.

  2. Run the following blkid commands to check the file systems and labels:

    sudo blkid /dev/nvme2n1p1
    sudo blkid /dev/nvme2n1p2

Copy data from the source volume to the new volume

Complete the following steps:

  1. Run the following commands to create mount points and mount the new partitions:

    sudo mkdir -p /mnt/newdata
    sudo mount /dev/nvme2n1p1 /mnt/newdata
  2. Run the following rsync command to copy data from the secondary source volume:

    sudo rsync -avxHAX --progress /data/ /mnt/newdata/
  3. Run the following diff command to verify that the data successfully copied to the new data partition:

    sudo diff -rq /data/ /mnt/newdata/
  4. Run the following umount command to unmount the new partition:

    sudo umount /mnt/newdata

Swap the secondary volume

Complete the following steps:

  1. Run the following umount command to unmount the secondary source volume:

    sudo umount /data
  2. Detach the new volume, and then reattach the new volume at the original device path.

  3. Run the following mount command to mount the new volume to the existing mount points:

    sudo mount /dev/nvme1n1p1 /data
  4. Run the following commands to verify that the /data partition contents are accessible on the new volume:

    ls -la /data/
    df -h /data
  5. Run the following cat command to update the UUID or check the LABEL=entry in /etc/fstab:

    cat /etc/fstab

Related information

Extend the file system after resizing an Amazon EBS volume

Requirements for Amazon EBS volume modifications

AWS OFFICIALUpdated 6 days ago