Linux LVM management

Linux LVM (Logical Volume Manager) is a part of the Linux kernel used to manage disk partitions and volumes. The main goal of LVM is to increase the management flexibility of disk partitions and volumes. LVM is based on the hard disk and partition system of the Linux kernel, making it easier for users to allocate and manage disk space.

In LVM, there are two main concepts:

  1. Volume (volume): Volume is the basic unit in LVM. It is a logical device that can contain multiple partitions or one partition. Volumes can be mounted on filesystems to expand disk space.
  2. LV (logical volume): LV is the abbreviation of Volume, which is a special form of volume. LV contains one or more partitions, which can be mounted to the file system. The size of an LV is made up of the partitions or disks it contains.

LVM realizes the dynamic management of disk space by creating multiple volumes on the physical hard disk, and then mounting these volumes to the file system. Users can combine one or more volumes together to form a large volume, thereby expanding the disk space. LVM also supports read and write operations across volumes, which improves file system performance.

In LVM, volumes and LVs are managed using a virtual interface. This interface is supported by a system call called "abstract I/O". Through this interface, LVM can realize the management of volumes and LVs, thereby improving the efficiency of disk management.

In general, LVM is the part of the Linux kernel used to manage disk partitions and volumes. It realizes the dynamic management of disk space by creating multiple volumes on the physical hard disk, and then mounting these volumes to the file system. LVM supports read and write operations across volumes, thereby improving the performance of the file system. Users can combine one or more volumes together to form a large volume, thereby expanding the disk space.

The following is relevant knowledge that is often used in daily use:

1. create

Before creating lv, you need to create a disk partition first, then create pv, then create vg, and finally create lv in vg.

1.1. Create partitions

Use the fdisk command to partition the disk. It is recommended to change the partition System to Linux LVM, which is easy to distinguish and manage.

fdisk /dev/sdb

1.2. Create pv

After actual verification, there is no need to manually create pv, and pv will be created automatically when vg is created or expanded.

It is personally recommended not to do this step.

pvcreate /dev/sdb1

1.3. Create vg

vgcreate vg_data /dev/sdb1

1.4. Create lv

lvcreate -l 100%vg -n lv_data vg_data

1.5. Format lv

# XFS
mkfs.xfs /dev/mapper/vg_data-lv_data

# EXT4
mkfs.ext /dev/mapper/vg_data-lv_data

1.6. Mount

moount /dev/mapper/vg_data-lv_data /data

It is recommended to write /etc/fstab here to ensure that the disk is automatically mounted after the server restarts.

2. Expansion

The steps of lv expansion are basically similar to creating lv. You still need to partition the disk (create pv), add pv to vg, then expand lv, and finally expand lv online.

The following is the lv expansion command:

# lvextend
lvextend -l 100%vg /dev/mapper/vg_data-lv_data
# 在线扩容
# ext4格式
resize2fs /dev/mapper/vg_data-lv_data
# xfs格式
xfs_growfs /dev/mapper/vg_data-lv_data

3. Shrinkage

The following are the steps to shrink the LV using LVM in the Linux operating system. Generally, the root directory cannot be shrunk.

In addition, the ext4 file system can be expanded and reduced, but the xfs file system can only be expanded, not reduced.

So if you need to shrink xfs, you can use xfsdump to back up the file system first, then shrink the logical volume (/ partition) (the original xfs file system will be damaged at this time), and then reformat the logical volume (/ partition), and finally xfsrestore restores the backup to this logical volume (/ partition). Of course, the data can also be backed up and restored in other ways, as long as the data is not lost.

3.1. Uninstall lv

umount /dev/mapper/vg_data-lv_data

3.2. Shrinkage

# 减小5G
lvresize -L -5G /dev/mapper/vg_data-lv_data
# 查看是否成功,以及缩容后大小
lvs

# 缩容文件系统
resize2fs /dev/mapper/vg_data-lv_data

The lvresize command is more flexible. It can be used to shrink or expand the capacity. You can directly specify the target size, or use the +|- algorithm to expand or shrink the capacity. For more information, please refer to the command help.

3.3. Mount lv

moount /dev/mapper/vg_data-lv_data /data

Use df to check path size after mount.

4. Remove

4.1 remove lv

Before removing the lv, you also need to unmount the lv that has been mounted to the operating system.

umount /dev/mapper/vg_data-lv_data

After unmounting the filesystem, delete the lv

lvremove /dev/mapper/vg_data-lv_data

delete vg

vgremove centos
# 如果卷组中还有lv,移出时,会提示,是否也移出,咱们这里直接移出

delete pv

pvremove /dev/sdb1

5. Other

Remove this hard drive from PV:

pvremove /dev/sdb1

Remove this hard drive from VG:

vgreduce /dev/sdb1

Remove PV unknown device from VG: 

vgreduce --removemissing /dev/sdb1

Move data from sda1 to sdb1:

pvmove /dev/sda1 /dev/sdb1

Guess you like

Origin blog.csdn.net/m0_38004228/article/details/130410102