LVM command:
PV (physical volume)
VG (volume Group)
LV (logical volume)
Note: Enter PV/VG/LV and then press TAB two to see the relevant commands.
PE: Physical expansion
LE: Logical extension
Note: The default size for each PE is 4M.
Through the case to understand the PV, VG, LV creation, mount, capacity expansion, shrinkage, and uninstall Delete related operations commands.
Case 1: Manage SDB, SDC hard drives with LVM, create them as a volume group called VG0, create a 7G in the VG0 volume group and name the LV
0 logical volumes are then formatted into the Ext4 file system, manually mounted to the/mnt/lv0 directory, and copied/etc/host* to the/mnt/lv0 directory.
#第1步, create PV, and check status information.
Lsblk #显示块设备
PVCREATE/DEV/SD[BC] #创建pv. Format Description: Pvcreate hard disk or partition device
PVs #显示pv状态 (select one of the commands as needed)
Pvdisplay #显示所有pv的详细信息
Pvscan #扫描已存在的pv设备
#第2步, create the VG volume group, and check the status information.
PVs #显示pv状态
Vgcreate VG0/DEV/SD[BC] #将sdb和sdc组成vg0卷组. Format Description: Vgcreate volume Group name PV device
VGs #显示vg状态 (select one of the commands as needed)
Vgdisplay #显示所有vg的详细信息
Vgscan #扫描已存在的vg设备
#第3步, create a LV that displays status information.
VGS #显示vg状态
Lvcreate-l 7g-n lv0 vg0 3 Create LV. Format Description: Lvcreate-l capacity-N LV Name Volume group name
LVS #显示lv状态 (select one of the commands as needed)
Lvdisplay #显示所有lv的详细信息
Lvscan #扫描已存在的lv设备
#第4步, format the file system, manually mount the LV. Write the data.
Lvscan #扫描已存在的lv设备
Mkfs.ext4/dev/vg0/lv0 #格式化
Mkdir-pv/mnt/lv0 #创建目录
Mount/dev/vg0/lv0/mnt/lv0 #手动挂载
Mount 3 Check Mount information
Cp-v/etc/host*/mnt/lv0/#复制文件
Case 2: Add the SDD and SDE to the VG0 Volume Group (Volume group expansion), and then the SDE is removed from the VG0 volume Group (Volume Group indent).
PVs
Pvcreate/dev/sd[de]
VGs
Vgextend Vg0/dev/sd[de] #vg扩展
Vgreduce Vg0/dev/sde #vg缩容
Warning: If you have data on the hard disk that you want to remove when the VG is shrunk, you must first migrate the data from the hard drive to the other hard disks in the VG volume group with Pvmove. Example: PVMOVE/DEV/SDB/DEV/SDD
Case 3: Expand the capacity of the lv0 logical volume to 12G and view the status, and then view the space usage status of the file system.
Idea: Expand the capacity of the logical volume before updating the file system capacity values.
VGs
Lvextend-l +5g/dev/vg0/lv0
Lvs
Df-h #发现lv0的文件系统容量并未更新
Resize2fs-p/dev/vg0/lv0 #更新文件系统的容量值
Note: XFS file system update capacity value with "Xfs_growfs mount point"
Case 4: Reduce the capacity of the lv0 logical volume to 6G and view the status, and then view the space usage status of the file system.
Idea: Unmount the file system of the logical volume first, then scan the logical volume's file system for errors, reduce the file system capacity, and finally reduce the capacity of the logical volume.
Umount/dev/vg0/lv0 #卸载
E2fsck-f/dev/vg0/lv0 #强制扫描
Resize2fs/dev/vg0/lv0 6G # Shrink File system
Lvs
Lvreduce-l 6g/dev/vg0/lv0 #缩逻辑卷
Mount/dev/vg0/lv0/mnt/lv0 #挂载
Df-h #发现lv0的文件系统容量并未更新
Ls/mnt/lv0 #显示lv0逻辑卷中的文件
Cat/mnt/lv0/hosts #查看指定文件, found the content is good
Note: XFS file system update capacity value with "Xfs_growfs mount point"
About snapshot volumes
1. Create a snapshot for lv0
Lvcreate-s-N lv0.snap-l 1g/dev/vg0/lv0
LVS information is as follows
LV VG Attr lsize Pool Origin data% meta% Move Log cpy%sync Convert
Root Cl-wi-ao----17.00g
Swap Cl-wi-ao----2.00g
Lv0 vg0 Owi-aos---6.00g
Lv0.snap vg0 swi-a-s---1.00g lv0 0.00
Case 1: Change the data of the source logical volume to see the change of the data% value in the LVS.
Df
Cp-v/etc/*.conf/mnt/lv0
DD If=/dev/zero of=/mnt/lv0/100m bs=10m count=10
Lvs
Case 2: Mount the snapshot volume. See what the data in the snapshot is.
Mkdir-pv/mnt/lv0snap
Lvscan
Mount/dev/vg0/lv0.snap/mnt/lv0snap
Ls/mnt/lv0snap #发现快照卷中是创建快照时刻的数据.
Case 3: Recovering data
1. Packaging the data from the backup snapshot volume (optional)
Tar-cvf/opt/lv0snap.tar/mnt/lv0snap #保留快照数据就做tar这一步
Umount/mnt/lv0 #卸载
Umount/mnt/lv0snap
Lvremove/dev/vg0/lv0snap #删除快照卷
2. Recover data will delete snapshot volume before recovery to uninstall if no uninstall to restart to recover data
Note: The data that is changed in the source logical volume is lost when the data is recovered.
Lvconvert--merge/dev/vg0/lv0.snap #恢复快照
Lvs
Mount/dev/vg0/lv0/mnt/lv0 #挂载
Ls/mnt/lv0 #发现是创建快照时候的数据
Finally, I would like to talk about the popular and simple understanding of the snapshot volume (please understand the deficiencies):
If Lv0 contains H, a, m three files, at this time to lv0 create a snapshot Lv0.snap, at this time the Lv0.snap there is no file, that is, Lv0.snap is an empty file. When you change the H file inside the Lv0, the original H file, which is the H file before the change, is backed up to the Lv0.snap file, and the H.new file is not saved to Lv0.snap after the change, and the H.new file is not backed up to lv0.snap regardless of subsequent changes. That is, the original H, a, m files will be backed up to Lv0.snap only when the original files H, a, m are lv0.snap when the snapshot is created. That is, Lv0.snap does not save any files other than the original H, a, m files.
CENTOS7 LVM Logical Volume management