How can I extend an existing filesystem partition without destroying data?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux 10
  • Red Hat Enterprise Linux 9
  • Red Hat Enterprise Linux 8
  • Red Hat Enterprise Linux 7

Issue

  • Need to extend partition size and file system size without destroying any existing data
  • How do you extend an existing non-lvm partition without destroying any data?
  • I would like to extend a disk partition /dev/sdX1, is there any supported method to accomplish this?
  • What are the steps for increasing a non LVM disk size for root?
  • /var utilization is 100%, how to extend the size of partition?

Resolution


Disclaimer: The following information has been provided by Red Hat, but is outside the scope of the posted This content is not included.Service Level Agreements and support procedures (Production Support - Red Hat Customer Portal). The information is provided as-is and any configuration settings or installed applications made from the information in this article could make the Operating System unsupported by Red Hat Global Support Services. The intent of this article is to provide information to accomplish the system's needs. Use of the information in this article at the user's own risk.

From a technical point of view, resizing filesystems and their underlying partitions (making changes to the existing partition table) can be very dangerous. Also, it is only possible to resize partitions from their end position on the disk. It is not possible to move partitions on the disk or resize them from their beginning. Such operations can result in a corrupted partition table, which will render all data on the disk device inaccessible, leading to data loss.


There are a few points to consider before attempting to resize your volume:

  • Storage resize commands are dangerous and can cause a complete loss of data. Backup all important information before attempting to follow the steps in this document.

  • This document only covers resizing the last partition on your device. If this is not the last partition, you will need to backup your data, rebuild the disk and partitions, then restore the data.

  • This document assumes you're using either a GPT partition table, or an msdos partition table using primary partition types.

  • The commands below use the follow variables that you need to adjust for your devices:

    • /mnt is the active mountpoint
    • /dev/vdc1 is the partition we are resizing
    • /dev/vdc is the device that holds the partition
  • Check the size of your current volume using the df command

    # df -hT /mnt
    Filesystem     Type  Size  Used Avail Use% Mounted on
    /dev/vdc1      ext3  5.0G  139M  4.6G   3% /mnt
    
  • Take note of the existing partition table using parted. Be sure to specify the device, not the partition. Only the highest numbered partition on the device can be resized.

    # parted /dev/vdc u s p
    Model: Virtio Block Device (virtblk)
    Disk /dev/vdc: 20971520s
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos  
           
    Number  Start  End        Size       Type     File system  Flags
     1      63s    10485215s  10485153s  primary  ext3
    
    • We can see that the ending sector is 10485215, which is smaller than the full disk size of 20971520. This indicates that the partition has room to grow.
  • Unmount the existing file-system ( all partitions on the disk will need to be unmounted )

    # umount /mnt
    
  • Next, resize the partition using the below command:

        # parted /dev/vdc resizepart 1 100%
    
        # partx -uv /dev/vdc
    
    • In RHEL7.4 and earlier versions, delete the existing partition with parted by specifying the partition number:
    # parted /dev/vdc rm 1
    
    • Then recreate the deleted partition with the larger size. Specify the same starting sector as the previous partition, and use 100% to fill the whole disk:
    # parted -s /dev/vdc mkpart primary 63s 100%
    Warning: The resulting partition is not properly aligned for best performance.
    
  • Verify the partition has extended by comparing the End sector to the previous output:

     # parted /dev/vdc u s p
     Model: Virtio Block Device (virtblk)
     Disk /dev/vdc: 20971520s
     Sector size (logical/physical): 512B/512B
     Partition Table: msdos
          
     Number  Start  End        Size       Type     File system  Flags
      1      63s    20971519s  20971457s  primary  ext3
    
  • Before resizing the filesystem, run the repairing tool about it to get any metadata inconsistency fixed:

    • Ext4:

      # fsck.ext4 /dev/vdc1
      
    • XFS:

      # xfs_repair /dev/vdc1
      
  • Then resize the filesystem using the appropriate tool:

    • Ext4:

      # resize2fs -f /dev/vdc1
      resize2fs 1.41.12 (17-May-2010)
      Resizing the filesystem on /dev/vdc1 to 2621432 (4k) blocks.
      The filesystem on /dev/vdc1 is now 2621432 blocks long.
      
    • XFS:

      # xfs_growfs /dev/vdc1
      
  • Finally, mount the filesystem and check the space available:

        # mount /dev/vdc1 /mnt
    
        # df -hT /mnt
        Filesystem     Type  Size  Used Avail Use% Mounted on
        /dev/vdc1      ext3  9.9G  140M  9.3G   2% /mnt
    

Root Cause

  • Making modifications to the partition table does not affect the underlying data under regular circumstances.
  • The partition table resides at the beginning of the disk and is a pointer for the OS to find the filesystem superblock location.
Components

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.