Expanding Disk Partitions on Linux System Servers: Adding New Disks, Partitioning, and Extending Logical Volumes

In practical projects, Linux system servers often encounter situations where disk space is insufficient, necessitating disk expansion. There are two scenarios for disk expansion: one is to mount a new disk block to the server and then perform logical partitioning on the newly mounted disk block; the other is to expand existing disk partitions, meaning that a directory is running out of space and needs to be expanded, where the directory is actually the mount point of the disk partition. This article introduces the method for expanding disk partitions in the second scenario. The expansion method for the first scenario is detailed in the previous article. It has been tested and is effective! For those in need, you can refer to that article.【Linux】Adding a New Disk to the Linux System, Creating Partitions, Logical Volumes (LV), Joining Volume Groups, and Mounting (Useful, Recommended to Bookmark)

Background: Existing logical volume (LV)data, assuming the logical volumedata is mounted at/data, and now the space under the /data directory is insufficient, we need to expand the /data directory.

1Check the volume group where the partitiondata is located, using the command:vgs/vgdisplay, for example, if the volume group name is:volgroup,

2Mount the new disk, for example, it is/dev/vdd, you can check with the command fdisk -l

3Use thelsblk command to check the mount status

4Create a partition on the new diskvdd, syntax:fdisk /dev/vdd, sequentially use the commands n,p,1, default input,w, and finally, the partition/dev/vdd1 is created, with the partition size being the entire size of the disk vdd.

5Create a physical volume, syntax:pvcreate /dev/vdd1, which will create a physical volume named/dev/vdd1;

6Add the physical volume to a volume group, for example, adding it to the volume group namedvolgroup, thus the volume groupvolgroup will have additional space, specific command:

vgextend volgroup /dev/vdd1

7,Extend the additional space in the volume group to other logical volumes, for example, allocating it to the partitiondata;

lvextend -l +100%FREE /dev/volgroup/data (this will allocate all the newly added capacity from the previous steps in the volume groupvolgroup to/dev/volgroup/data this lv), note: here

/dev/volgroup/data corresponds to: /dev/volume group name/logical volume name

Another expansion command is: lvextend -L +10G /dev/volgroup/data

This command adds a specified size (10G) of capacity to a fixed partition.

lvextend -L -10G /dev/volgroup/data

Reducing capacity in the fixed partition.

8,Refresh the partition size, syntax is:resize2fs volgroup

resize2fs /dev/volgroup

resize2fs adjusts the size of ext2/ext3/ext4 file systems, it can enlarge or shrink the size of unmounted file systems. If the file system is already mounted, it can enlarge the file system size, provided that the kernel supports online resizing.

resize2fs command: adjusts the size of ext2/ext3/ext4 file systems, it can enlarge or shrink the size of unmounted file systems. If the file system is already mounted, it can enlarge the file system size, provided that the kernel supports online resizing.

xfs_growfs command: adjusts the size of an xfs file system (can only be expanded).

9,After the above steps, the expansion of the existing partition is complete.No further mounting is required, usedf -h /data command to check, and you will see that the available space under the /data directory has increased.

Note: Common commands to check logical volumes, physical volumes, and volume groups in Linux systems include:

lvs pvs vgs lvdisplay pvdisplay vgdisplay

Leave a Comment