Home  >  Article  >  Operation and Maintenance  >  How to expand the partition capacity under Linux if the partition capacity is insufficient?

How to expand the partition capacity under Linux if the partition capacity is insufficient?

齐天大圣
齐天大圣Original
2020-11-19 14:28:593234browse

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 situations here

  • LVM partition expansion

  • Non-LVM partition expansion

LVM partition expansion

Generally, we recommend using LVM, which facilitates the elastic expansion of partitions. Regarding the introduction of LVM, I will not go into details here about physical volumes, logical groups, logical volumes, etc. You can read my other two articles about LVM.

Scenario: The /www directory is our website-related directory. The partition mounted in this directory uses LVM. Originally, this directory had 1G of space, but as the system runs, the remaining space is only The download is less than 100M. Now the partition needs to be expanded.

Preparation work, implementation scenario

# 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 to vgwww first.

# vgdisplay vgwww
 ……
  Free  PE / Size       0 / 0   <=== 没哟剩余空间了
  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 are any unused pvs. If not, we need to create the pvs 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 available pv. Then, create pv

# 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.

# 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 vg has 255 more PEs that are not used. The next step is to expand the LV

# 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已经变为了1.99G了
  ……

At this time, although the LV size has become larger, the /dev/mapper/vgwww-lvol0 file system has not become larger.

# df -h
文件系统                 容量  已用  可用 已用% 挂载点
……
/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 it.

# 该主机使用的是ext4文件系统
# resize2fs /dev/vgwww/lvol0 
# df -h
文件系统                 容量  已用  可用 已用% 挂载点
……
/dev/mapper/vgwww-lvol0  2.0G  904M  982M   48% /www

At this point, the expansion of the file system is completed.

Expansion of non-LVM partition

Recently, I encountered this situation. I have a friend whose Alibaba Cloud server root partition is almost full. The original 40G cloud disk was later added to 20G. He came to me and said that I had already added 20G, but it still showed 40G. He asked me to help him solve the problem.

Unfortunately, lvm is not used in its root directory, and I don’t know what to do for a while. Later, I searched for relevant information and found a solution. However, 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 simulates the scenario first: the /bak directory is mounted on a separate partition. The partition was initially divided into 500M, and now it needs to be expanded.

# df -h
文件系统                 容量  已用  可用 已用% 挂载点
……
/dev/sdd1                497M   26M  472M    6% /bak

Start the expansion operation

# 首先卸载挂载点

# fdisk /dev/sdd
……
命令(输入 m 获取帮助):p
……
   设备 Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048     1026047      512000   83  Linux

#记住开始点,即2048。然后删除该分区,重新建立分区
命令(输入 m 获取帮助):d
已选择分区 1
分区 1 已删除

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
分区号 (1-4,默认 1):
起始 扇区 (2048-2097151,默认为 2048):2048
Last 扇区, +扇区 or +size{K,M,G} (2048-2097151,默认为 2097151):
将使用默认值 2097151
分区 1 已设置为 Linux 类型,大小设为 1023 MiB

命令(输入 m 获取帮助):w


# partprobe

At this time, you can use the lsblk command to check that the partition size has become larger, but the result of df -h has not changed. You need to use the xfs_growfs command to process it.

 # xfs_growfs /dev/sdd1
 
 # df -h
文件系统                 容量  已用  可用 已用% 挂载点
……
/dev/sdd1               1020M   26M  995M    3% /bak

Another point that needs special attention: the file system must not be written to the partition, otherwise 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 if the partition capacity is insufficient?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn