Reprint LV and VG expansion in Linux LVM mode

1. Introduction to LVM
LVM (Logical Volume Manager) is a logical volume manager that allows users to combine multiple hard disk partitions or the entire hard disk into one or more logical volumes. LVM can dynamically change the size of logical volumes while running without requiring a system shutdown or reboot. It can also combine the storage spaces of multiple hard drives to form a large-capacity storage pool, making data management more flexible and convenient. LVM has become one of the standard features of many Linux distributions and is widely used in data centers, servers, virtualization environments and other scenarios. As shown in the figure below, in simple terms, LVM converts partition or disk logic into PV (Physical Volume), adds PV to VG (Volume Group), and then allocates the storage space in VG to LV (Logical Volume) according to needs. middle. The blog post will use the example of expanding the disk space of the /home partition to introduce how LVM performs disk expansion under the EXT4 file system format. In fact, LVM supports dynamic adjustment, including expansion and reduction, but the actual operation is also related to the file system format. The ext4 format can both expand and reduce (reclaim unused space), while the xfs file system format only supports Expansion.

Experimental environment description:

Operating system: centos7.9
File system format: ext4
2. LV expansion
When expanding the LVM disk, we first check whether there is remaining space in the VG. If there is, we can allocate the required disk space from the VG to the specified LV. In the experimental environment, the /home partition size is 20G and LVM logical volumes are used. Assume that we need to adjust the /home partition size to 30G.

1. Check the remaining space of vg
Using the vgdisplay command, we can see that the vg name is centos_s178, the allocated space is 42GB, and the remaining space is 16.99GB.

vgdisplay

– Volume group –
VG Name centos_s178
AllocPE/Size 10752/42.00 GiB
Free PE/Size 4350/16.99 GiB

2. View the LV logical volume name and path
Use the lvdisplay command to check the status of the logical volume. You can see that there are multiple logical volumes, and you can see the logical volume path, logical volume name, VG to which it belongs, storage space size, etc.

 lvdisplay

– Logical volume –
LV Path /dev/centos_s178/home
LV Name home
VG Name centos_s178

LV Size 20.00 GiB

– Logical volume –
LV Path /dev/centos_s178/root
LV Name root
VG Name centos_s178

3. Expand the storage space of the specified size to LV
Use the lvextend command to expand the logical volume size, and use the -L parameter to expand the specified size.

lvextend -L + 10G /dev/centos_s178/home

Size of logical volume centos_s178/home changed from 20.00 GiB (5120 extents) to 30.00 GiB (7680 extents).
Logical volume centos_s178/home successfully resized.

3. Check the disk size

4. Expansion space is written to the file system
The resize2fs command writes the expansion space into the file system. If it is in xfs format, you can use the command xfs_growfs instead.

resize2fs /dev/centos_s178/home

resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/centos_s178/home is mounted on /home; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/centos_s178/home is now 7864320 blocks long.

5. Verify again
Use the df command to check the disk size again and find that the /home partition has been adjusted to 30G.

3. PV expansion
Suppose we need to expand the /home partition to 40G. Because the remaining space of the VG is only 16.99G, the disk space is insufficient. We need to add a disk first, add the disk to the VG, and then allocate the specified space to the LV.

1. Check the disk
Add or insert a new disk /dev/sdb, which can be seen using the fdisk command.

fdisk -l

Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors

Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 sectors

2. Disk partition

parted /dev/sdb

GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type help’ to view a list of commands.
(parted) mklabel gpt
(parted) mkpart
Partition name? []? disk2
File system type? [ext2]? ext4
Start? 1
End? -1
(parted)

3. Create PV

pvcreate /dev/sdb1

Physical volume “/dev/sdb1” successfully created.

4. View PV information

pvs

PV VG Fmt Attr PSize PFree
/dev/sda2 centos_s178 lvm2 a– 42.00g 0
/dev/sda3 centos_s178 lvm2 a– 16.99g 6.99g
/dev/sdb1 lvm2 – <40.00g <40.00g
[root@s178 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos_s178 2 3 0 wz–n- 58.99g 6.99g

vgscan

Reading volume groups from cache.
Found volume group “centos_s178” using metadata type lvm2

5. Expansion of VG

vgextend centos_s178 /dev/sdb1

Volume group “centos_s178” successfully extended

6. View VG information

vgdisplay

– Volume group –
VG Name centos_s178
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No. 7
VG Access read/write
VG Status resizable
MAXLV 0
Cur LV 3
OpenLV3
Max PV 0
Cur PV 3
Act PV 3
VG Size <98.99 GiB
PE Size 4.00 MiB
Total PE 25341
AllocPE/Size 13312/52.00 GiB
Free PE/Size 12029/<46.99 GiB
VG UUID tTBWJ4-BFKA-tP95-kRNh-kZ0G-nf51-d3XSUt

7. Expand LV
Use the command lvextend LV PV to allocate a certain PV to the specified LV. Use the lvextend command to expand the logical volume size. Remember to use the -L parameter without + to extend the disk to the specified size, and with + to expand the space of the specified size. The lvreduce command can reduce the logical volume size.

lvextend -L 40G /dev/centos_s178/home

Size of logical volume centos_s178/home changed from 36.99 GiB (9470 extents) to 40.00 GiB (10240 extents).
Logical volume centos_s178/home successfully resized.

8. Expansion space is written into the file system

resize2fs /dev/centos_s178/home

resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/centos_s178/home is mounted on /home; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 5
The filesystem on /dev/centos_s178/home is now 10485760 blocks long.

9. Verify the expansion results

Original link: https://blog.csdn.net/carefree2005/article/details/130221262