Extending Partitions and File Systems (Linux)

Last updated: 2020-10-20 14:10:15

    Introduction

    Cloud disks are expandable storage devices on the cloud. After creating a cloud disk, you can expand its capacity at any time to increase its storage space without losing any original data.
    After expanding a cloud disk, you need to either assign its expanded capacity to an existing partition, or format it into an independent new partition.

    Prerequisites

    Note:

    Expanding the file system may affect existing data. We strongly recommend that you manually create a snapshot to back up your data before the operation.
    To protect your existing data, we have added two options to umount existing partitions and run fsck for a filesystem check during the expansion process. You can choose to use them as needed.

    Directions

    Confirming the expansion method

    1. Run the following command as the root user to view the partition format of the cloud disk.
      fdisk -l
      • If the result indicates that the device has no partition (for example, it only shows /dev/vdb), see extending the file system.
      • If the result similar to the following two figures (which may vary according to the operating system) is returned, GPT partition format is used.

      • If the result similar to the following two figures (which may vary according to the operating system) is returned, MBR partition format is used.

        Note:

        The maximum disk capacity supported by MBR partition format is 2 TB. If your disk partition is in MBR format, and you need to expand its capacity to more than 2 TB, we recommend that you create and mount a new data disk, and copy the data to the new disk using GPT partition format. For Linux operating system, if the disk partition format is GPT, the fdisk partition tool can no longer be used, and parted tool must be used.


    2. Follow Step 1 to view the cloud disk partition format, and select the corresponding operations guide.

    Partition format Operations guide Description
    - Extend the file system Applicable to scenarios where a file system is created directly on a bare device and no partition is created.
    GPT Assign the expanded capacity to an existing partition (GPT) Also applicable to scenarios of direct formatting when no partition is created.
    Format the expanded capacity into an independent new partition (GPT) The original partition can be retained without changes.
    MBR Assign the expanded capacity to an existing partition (MBR) Also applicable to scenarios of direct formatting when no partition is created.
    Format the expanded capacity into an independent new partition (MBR) The original partition can be retained without changes.

    Extending the file system

    1. Use different commands to extend the file system, depending on the file system type:
      • For an EXT file system, run the resize2fs command to extend the file system.
      • For an XFS file system, run the xfs_growfs command to extend the file system.
        Taking /dev/vdb as an example, run the following command to extend an EXT file system:
        resize2fs /dev/vdb
        Taking /dev/vdb as an example, run the following command to extend an XFS file system:
        xfs_growfs /dev/vdb
    2. Run the following command to view the new partition:
      df -h

    Assigning the expanded capacity to an existing partition (GPT)

    1. Run the following command as the root user to confirm changes in cloud disk capacity:

      parted <Disk path> print

      Taking the disk path /dev/vdb as an example, run the following command:

      parted /dev/vdb print

      If a message as shown in the following figure appears in the process, enter Fix.

      As shown in the following figure, the cloud disk capacity is 107 GB after expansion and the existing partition capacity is 10.7 GB.

    2. Run the following command to check whether the cloud disk has partitions mounted:

      mount | grep '<Disk path>' 

      Taking the disk path /dev/vdb as an example, run the following command:

      mount | grep '/dev/vdb'

      As shown in the following figure, the cloud disk has one partition (vdb1) mounted to /data.

    3. Run the following command to unmount the data disk:

      umount <mount point>

      Taking the mount point /datamount point as an example, run the following command:

      umount /data

      Note:

      Unmount the file systems from all partitions on the cloud disk, and execute the operations in Step 4 again. You can run the following command again to confirm that the unmounting is successful.

      mount | grep '/dev/vdb'

      The file systems are unmounted from all partitions on the cloud disk, as shown in the following figure.

    4. Run the following command to access the parted tool.

      parted '<Disk path>'

      Taking the disk path /dev/vdb as an example, run the following command:

      parted '/dev/vdb'
    5. Run the following command to change the unit from the default “GB” to “sector” for display and operation:

      unit s
    6. Run the following command to view partitions and record their Start values:

      Note:

      After a partition is deleted and a new one is created, the Start value must remain unchanged. Otherwise, it may cause data loss.

      print

      Take the Start value 2048s as an example:

    7. Run the following command to delete the existing partition:

      rm <Partition Number>

      For example, run the following command to delete the partition “1” from the CVM:

      rm 1

      The following figure shows the command output.

    8. Run the following command to create a new primary partition:

      mkpart primary <Start sector of the original partition> 100%

      The 100% in the command indicates this partition goes to the end of the disk.
      Assume that the primary partition starts from sector 2048 (it must be the same as that of the previously deleted partition, that is, the Start value must be 2048s), run the following command:

      mkpart primary 2048s 100%

      If a status as shown in the following figure appears, enter Ignore.

    9. Run the following command to check whether the new partition has been created successfully:

      print

      If the result as shown in the following figure is returned, the new partition has been created successfully.

    10. Run the following command to exit the parted tool:

      quit
    11. Run the following command to check the extended partition:

      e2fsck -f <Partition path>

      Taking the newly created partition “1” (that is, the partition path is /dev/vdb1) as an example, run the following command:

      e2fsck -f /dev/vdb1

      The following figure shows the command output.

      Extend your file system depending on the type:

      • For an EXT file system:
        1. Run the following command to extend the EXT file system in the new partition:
          resize2fs <Partition path>
          Taking the partition path /dev/vdb1 as an example, run the following command:
          resize2fs /dev/vdb1
          If the result as shown in the following figure is returned, the expansion is successful.
        2. Run the following command to manually mount the new partition:
          mount <Partition path> <Mount point>
          Taking the partition path /dev/vdb1 and the mount point /data as an example, run the following command:
          mount /dev/vdb1 /data
      • For an XFS file system:
        1. Run the following command to manually mount the partition:
          mount <Partition path> <Mount point>
          Taking the partition path /dev/vdb1 and the mount point /data as an example, run the following command:
          mount /dev/vdb1 /data
        2. Run the following command to extend the XFS file system in the new partition:
          xfs_growfs <Partition path>
          Taking the partition path /dev/vdb1 as an example, run the following command:
          xfs_growfs /dev/vdb1
    12. Run the following command to view the new partition:

      df -h

      If the result as shown in the following figure is returned, the mount is successful and you can see the data disk.

    Formatting the expanded capacity into an independent new partition (GPT)

    1. Run the following command as the root user to confirm changes in cloud disk capacity:
      parted <Disk path> print
      Taking the partition path /dev/vdb as an example, run the following command:
      parted /dev/vdb print
      If a message as shown in the following figure appears in the process, enter Fix.

      As shown in the following figure, the cloud disk capacity is 107 GB after expansion and the existing partition capacity is 10.7 GB.
    2. Run the following command to check whether the cloud disk has partitions mounted:
      mount | grep '<Disk path>' 
      Taking the disk path /dev/vdb as an example, run the following command:
      mount | grep '/dev/vdb'
      As shown in the following figure, the cloud disk has one partition (vdb1) mounted to /data.
    3. Run the following command to unmount the data disk:
      umount <mount point>
      Taking the mount point /data as an example, run the following command:
      umount /data

      Note:

      Unmount the file systems from all partitions on the cloud disk, and execute the operations in Step 4 again. You can run the following command again to confirm that the unmounting is successful.

      mount | grep '/dev/vdb'
      The file systems are unmounted from all partitions on the cloud disk, as shown in the following figure.

    4. Run the following command to access the parted partition tool:
      parted '<Disk path>'
      Taking the disk path /dev/vdb as an example, run the following command:
      parted '/dev/vdb'
    5. Run the following command to view partitions and record their End values, which will be used as the start offset of the next partition:
      print
    6. Run the following command to create a new primary partition. This partition starts from the end of the existing partition, and covers all the newly added space on the disk.
      mkpart primary start end
      Taking the End value “10.7 GB” as an example, run the following command:
      mkpart primary 10.7GB 100%
    7. Run the following command to check whether the new partition has been created successfully:
      print
    8. Run the following command to exit the parted tool:
      quit
    9. Run the following command to format the newly created partition:
      mkfs.<fstype> <Partition path> 
      Select a file system format such as EXT2 or EXT3 as needed.
      Taking the EXT3 file system as an example, run the following command:
      mkfs.ext3 /dev/vdb2

    Assigning the expanded capacity to an existing partition (MBR)

    You can use the fdisk/e2fsck/resize2fs automatic expansion tools to add the newly expanded disk space to the existing file system on a Linux CVM. For a successful expansion, the following four requirements must be met:

    • The file system is EXT2, EXT3, EXT4, or XFS.
    • The current file system does not have any error.
    • The disk size after expansion does not exceed 2 TB.
    • The current tools only support Python version 2, not Python version 3.
    1. Run the following command as the root user to unmount the partition:
      umount <Mount point>
      Taking the mount point /data as an example, run the following command:
      umount /data
    2. Run the following command to download a tool:
      wget -O /tmp/devresize.py https://raw.githubusercontent.com/tencentyun/tencentcloud-cbs-tools/master/devresize/devresize.py
    3. Run the following command to use the expansion tool for expansion:
      python /tmp/devresize.py <Disk path>
      Taking the disk path /dev/vdb and the file system vdb1 as an example, run the following command:
      python /tmp/devresize.py /dev/vdb
      • If “The filesystem on /dev/vdb1 is now XXXXX blocks long.” is returned, the expansion is successful. Execute Step 4.
      • If “[ERROR] - e2fsck failed!!” is returned, execute the following steps:
        a. Run the following command to fix the partition where the file system resides:
        fsck -a <Partition path>
        Taking the disk path /dev/vdb and the file system vdb1 as an example, run the following command:
        fsck -a /dev/vdb1
        b. Run the following command again after the fix to use the expansion tool for expansion:
        python /tmp/devresize.py /dev/vdb
    4. Run the following command to manually mount the extended partition:
      mount <Partition path> <Mount point>
      Take the mount point /data as an example.
      • If there is a partition before expansion and the partition path is /dev/vdb1, run the following command:
        mount /dev/vdb1 /data
      • If there is no partition before expansion, run the following command:
        mount /dev/vdb /data
    5. Run the following command to view the partition capacity after expansion:
      df -h
      The result similar to to what is shown below is returned, the mount is successful, and you can see the data disk.
    6. Run the following command to view the data of the original partition after expansion and check whether the new storage space has been added to the file system.
      ll /data

    Formatting the expanded capacity into an independent new partition (MBR)

    1. Run the following command as the root user to view the partitions of the mounted data disk:
      df -h
    2. Run the following command to view the data disk after expansion without partitions:
      fdisk -l
    3. Run the following command to unmount all mounted partitions:
      umount <mount point>
      Taking the mount point /data as an example, run the following command:
      umount /data

      Note:

      Unmount the file systems from all partitions on the cloud disk, and execute the operations in Step 4 again. You can run the following command again to confirm that unmounting is successful.

      mount | grep '<Disk path>'
      If it returns null, all file systems have been unmounted from partitions on the cloud disk.
    4. Run the following command to create a partition.
      fdisk <Disk path>
      Taking the disk path /dev/xvdc as an example, run the following command:
      fdisk /dev/xvdc
      When prompted, sequentially enter p (check existing partitions), n (create a partition), p (create a primary partition), 2(create a second primary partition), press Enter twice (keep default configurations), enter w (save the partition table), and start the partition, as shown in the following figure:

      Note:

      This document uses creating one partition as an example. You can also create multiple partitions as needed.

    5. Run the following command to view the new partition:
      fdisk -l
      As shown in the following figure, the new partition “xvdc2” has been created.
    6. Run the following command to format the new partition and create a file system:
      mkfs.<fstype> <Partition path> 
      Select a file system format such as EXT2 or EXT3 as needed.
      Taking the EXT3 file system as an example, run the following command:
      mkfs.ext3 /dev/xvdc2
    7. Run the following command to create a mount point:
      mkdir <New mount point>
      This example taking /data1 as the new mount point, run the following command:
      mkdir /data1
    8. Run the following command to manually mount the new partition:
      mount <New partition path> <New mount point>
      This example taking /dev/xvdc2 as the new partition path and /data1 as the new mount point, run the following command:
      mount /dev/xvdc2 /data1
    9. Run the following command to view the new partition:
      df -h
      If the result as shown in the following figure is returned, the mount is successful, and you can see the data disk.

      Note:

      To allow the CVM to automatically mount a data disk at restart or startup, perform Step 10 and Step 11 to add the new partition information to /etc/fstab.

    10. Run the following command to add information:
      echo '/dev/xvdc2 /data1 ext3 defaults 0 0' >> /etc/fstab
    11. Run the following command to view information:
      cat /etc/fstab
      If the result as shown in the following figure is returned, the partition information is successfully added.

    Extending partitions and file systems (Windows)

    FAQs

    If you encounter any problem when using a cloud disk, refer to the following documents for troubleshooting based on your actual situation.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help