In daily work, we often encounter the problem of insufficient remaining capacity of a certain partition, so we need to know how to expand the partition under the Linux server. For partition expansion, there are two cases here
-
LVM partition expansion
-
Expansion of non-LVM partition
Expansion of LVM partition
Generally, we recommend using LVM, which facilitates the elastic scaling of partitions. Regarding the introduction of LVM, physical volumes, logical groups, logical volumes, etc. will not be detailed here. You can read my other two articles about LVM.
Scenario: The /www directory is our website-related directory. The partition mounted on this directory uses LVM. Originally, this directory has a space of 1G, but with the operation of the system, the remaining space is only Less than 100M left. Now the partition needs to be expanded.
Preparation work, realize the scene
# pvcreate /dev/sdb1 # vgcreate vgwww /dev/sdb1 # lvcreate -l 255 vgwww # mkfs.ext4 /dev/vgwww/lvol0 # mount /dev/vgwww/lvol0 /www # dd if=/dev/zero of=/www/bigfile bs=1M count=900 # df -h ... /dev/mapper/vgwww-lvol0 988M 903M 19M 98% /www
Partition expansion
First, we need to see if there is any remaining space in vgwww, If not, you need to add pv in vgwww first.
# vgdisplay vgwww ... Free PE / Size 0 / 0 <=== No space left VG UUID xdw96k-xZNv-tmVf-Pkcx-SX7T-C1tz-ZZq3HG
From the above information, we can know that the vg has no remaining space. Then you need to add pv to this vg. Now let’s see if there is any pv that is not used. If not, we need to create pv first.
# pvscan PV /dev/sdb1 VG vgwww lvm2 [1020.00 MiB / 0 free] Total: 1 [1020.00 MiB] / in use: 1 [1020.00 MiB] / in no VG: 0 [0 ]
As you can see, there is no pv available. Then, create pv first
# pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created. #pvscan PV /dev/sdb1 VG vgwww lvm2 [1020.00 MiB / 0 free] PV /dev/sdc1 lvm2 [1023.00 MiB] Total: 2 [<2.00 GiB] / in use: 1 [1020.00 MiB] / in no VG: 1 [1023.00 MiB]
pv already exists, then add the new pv to the vgwww logical volume group middle.
# vgextend vgwww /dev/sdc1 Volume group "vgwww" successfully extended # vgdisplay vgwww ... Free PE / Size 255 / 1020.00 MiB VG UUID xdw96k-xZNv-tmVf-Pkcx-SX7T-C1tz-ZZq3HG
Now there are 255 more PEs in vg that are not used. Next, expand the LV capacity
# lvresize -l +255 /dev/vgwww/lvol0 Size of logical volume vgwww/lvol0 changed from 1020.00 MiB (255 extents) to 1.99 GiB (510 extents). Logical volume vgwww/lvol0 successfully resized. #lvdisplay --- Logical volume --- ... LV Size 1.99 GiB <=== lv has become 1.99G ...
At this time, although the size of lv has become larger, the /dev/mapper/vgwww-lvol0 file system has not increased.
# df -h File System Capacity Used Available % Used Mount Point ... /dev/mapper/vgwww-lvol0 988M 903M 19M 98% /www
At this time, we also need xfs_growfs (for xfs file system) or resize2fs (for ext file system) to process.
# The host uses the ext4 file system # resize2fs /dev/vgwww/lvol0 # df -h File System Capacity Used Available % Used Mount Point ... /dev/mapper/vgwww-lvol0 2.0G 904M 982M 48% /www
So far, the expansion of the file system has been completed.
Expansion of non-LVM partition
Recently, I encountered this Condition. I have a friend whose root partition of the Alibaba Cloud server is almost full. The original 40G cloud disk was added to 20G later. He found me and said, I have already added 20G, but it still shows 40G, let me help him solve the problem.
Unfortunately, its root directory does not use lvm, and I don’t know what to do for a while. After searching for relevant information, I found a solution. But this solution has limitations, that is, for the partition that needs to be expanded, this partition must be the last partition of the disk.
The following first simulates the scene: the /bak directory is mounted on a separate partition, which was divided into 500M at the beginning, and now it needs to be expanded
# df -h File System Capacity Used Available % Used Mount Point ... /dev/sdd1 497M 26M 472M 6% /bak
Start expansion operation
# First uninstall the mount point # fdisk /dev/sdd ... Command (type m for help): p ... Device Boot Start End Blocks Id System /dev/sdd12048 1026047 512000 83 Linux #Remember the starting point, which is 2048. Then delete the partition and recreate the partition Command (type m for help): d Partition 1 selected Partition 1 deleted Command (type m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p Partition number (1-4, default 1): Start sector (2048-2097151, default is 2048): 2048 Last sector, +sector or +size{K,M,G} (2048-2097151, default is 2097151): Will use the default value of 2097151 Partition 1 is set to type Linux with a size of 1023 MiB Command (type m for help): w # partprobe
At this time, the size of the partition has increased through the lsblk command, but the result of df -h has not changed, and it needs to be processed by the xfs_growfs command.
# xfs_growfs /dev/sdd1 # df -h File System Capacity Used Available % Used Mount Point ... /dev/sdd1 1020M 26M 995M 3% /bak
There is another point that needs special attention: the file system must not be written to the partition, so the previous data will be lost
For more related technical articles, please visit the Linux System Tutorial column!
The above is the detailed content of how to expand the partition capacity under Linux. For more information, please pay attention to other related articles on 1024programmer.com!