1: The creation and expansion of LVM
逻辑卷管理(LVM)在 Linux 系统中扮演着重要的角色,它可以提高可用性、磁盘 I/O、性能和磁盘管理的能力。
你打算把两个10G磁盘合成一个磁盘来使用,它们是 /dev/sdb、/dev/sdc 。
Run the following command to discover the newly added LUN or disk in Linux:
[root@test3 ~]# fdisk -l
pvcreate creates a physical volume
语法: pvcreate disk disk......
[root@test3 ~]# pvcreate /dev/sdb /dev/sdc
View physical volume pvs or pvdisplay (show more detailed information)
Please note:
上面的命令将删除给定磁盘 /dev/sdb、/dev/sdc 上的所有数据。
vgcreate creates a volume group (use vgs or vgdisplay to view volume group information)
语法: vgcreate [卷组名] [物理卷名]
[root@test3 ~]# vgcreate vg_name disk disk ........
[root@test3 ~]# vgcreate vg01 /dev/sdb /dev/sdc
lvcreate creates a logical volume
语法: lvcreate –n [逻辑卷名] –L [逻辑卷大小] [要创建的 LV 所在的卷组名称]
[root@test3 ~]# lvcreate -n lv001 -L 19.99G vg01
Use the lvs and lvdisplay commands to display the information of the LV you created:
Format disk file system
mkfs –t [文件系统类型] /dev/[LV 所在的卷组名称]/[LV 名称]
[root@test3 ~]# mkfs -t ext4 /dev/vg01/lv001
Create a directory and mount LV Luo just press
[root@test3 ~]# mkdir /data/hanye
[root@test3 ~]# mount /dev/vg01/lv001 /data/hanye/
[root@test3 ~]# df -h
Add boot auto mount to fstab file
echo "/dev/vg01/lv001 /data/hanye ext4 defaults 0 0" >> /etc/fstab
2: LVM expansion and increase (lvextend)
扩展逻辑卷前提:
检查逻辑卷(LV)所在的卷组中是否有足够的未分配磁盘空间
将物理磁盘转换为物理卷(PV)
扩展卷组
增加逻辑卷大小
扩大文件系统
检查扩展的文件系统大小
Expand lvextend when the original logical volume is left
语法: lvextend [要增加的额外空间] [现有逻辑卷名称]
[root@test3 ~]# lvextend -L +10G /dev/vg01/lv001 #增加lv001磁盘10G
Add disk expansion
新增磁盘 /dev/sdd(10G) 新磁盘
Create physical volumes and add volume groups
[root@test3 ~]# pvcreate /dev/sdd
[root@test3 ~]# vgextend vg01 /dev/sdd
Add to logical volume
语法: lvextend [要增加的额外空间] [现有逻辑卷名称]
[root@test3 ~]# lvextend -L +5G /dev/vg01/lv001 #扩增5G磁盘
Now that the logical volume has been expanded, you need to adjust the size of the file system to expand the space in the logical volume
检查逻辑卷
[root@test3 ~]# e2fsck -f /dev/vg01/lv001
调整逻辑卷
[root@test3 ~]# resize2fs /dev/vg01/lv001 #对于xfs 需要(xfs_growfs /dev/vg01/lv001)
df view disk size
3: Reduction of LVM
减少/缩小逻辑卷是数据损坏的最高风险
如果可能的话,尽量避免这种情况。
缩减 LVM 之前,建议先做一个备份。
当你在 LVM 中的磁盘空间耗尽时,你可以通过缩小现有的没有使用全部空间的 LVM,而不是增加一个新的物理磁盘,在卷组上腾出一些空闲空间。
需要注意的是: 在 GFS2 或者 XFS 文件系统上不支持缩小。
Steps
Need to unmount the file system
- [root@test3 ~]# umount /data/hanye/
Check the file system (whether it is damaged) - [root@test3 ~]# e2fsck -f /dev/vg01/lv001
Reduce size - Syntax: resize2fs [existing logical volume name] [new file system size]
- resize2fs /dev/vg01/lv001 80G Shrink the disk to 15G
Shrink the logical volume - Syntax: lvreduce [new LVM size] [existing logical volume name]
- lvreduce -L 80G /dev/vg01/lv001 Reduce the disk to 15G
and recheck the file system (whether it is damaged) - e2fsck -f /dev/vg01/lv001
mount system files - mount /data/hanye/
check if the logical volume is reduced - df -h
Unmount the logical volume
[root@test3 ~]# umount /data/hanye/
[root@test3 ~]# lvremove /dev/vg01/lv001
[root@test3 ~]# vgremove vg01
[root@test3 ~]# pvremove /dev/sdb /dev/sdc /dev/sdd