硬盘分好区或者部署为RAID磁盘阵列之后,再想修改硬盘分区大小就不容易了。换句话说,当用户想要随着实际需求的变化调整硬盘分区的大小时,会受到硬盘“灵活性”的限制。这时就需要用到另外一项非常普及的硬盘设备资源管理技术了—LVM(逻辑卷管理器)。LVM可以允许用户对硬盘资源进行动态调整。
逻辑卷管理器是Linux系统用于对硬盘分区进行管理的一种机制,理论性较强,其创建初衷是为了解决硬盘设备在创建分区后不易修改分区大小的缺陷。尽管对传统的硬盘分区进行强制扩容或缩容从理论上来讲是可行的,但是却可能造成数据的丢失。而LVM技术是在硬盘分区和文件系统之间添加了一个逻辑层,它提供了一个抽象的卷组,可以把多块硬盘进行卷组合并。这样一来,用户不必关心物理硬盘设备的底层架构和布局,就可以实现对硬盘分区的动态调整。
部署LVM的好处:
1. 对多块硬盘合并
2. 扩容或缩小分区
3. 对分区生成快照
部署LVM时,需要逐个配置物理卷、卷组和逻辑卷。常用的部署命令:
这里用前面所学到的RAID技术组建了两个RAID 10级别的磁盘阵列,分别为 /dev/md0和/dev/md1来部署LVM。
部署LVM
1.使两块硬盘设备(磁盘阵列)先支持LVM技术
[root@localhost ~]# pvcreate /dev/md[0,1]
Physical volume "/dev/md0" successfully created.
Physical volume "/dev/md1" successfully created.
2.把两块硬盘设备加入到卷组中,然后可以使用vgdisplay查看卷组状态
[root@localhost ~]# vgcreate storage /dev/md[0,1]
Volume group "storage" successfully created
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name storage
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 7.98 GiB
PE Size 4.00 MiB
Total PE 2044
Alloc PE / Size 0 / 0
Free PE / Size 2044 / 7.98 GiB
VG UUID KJxS91-pmMO-prh7-7s67-Vrme-D0E3-kRjkVW
3.创建一个逻辑卷设备,然后可以使用lvdisplay命令查看逻辑卷状态,使用lvcreate命令创建,-n参数表示逻辑卷名称,-l或-L表示的卷大小,选其一使用,-l是以基本单元的个数为单位(每个基本单元默认大小为4M),-L是以MB为单位。
[root@localhost ~]# lvcreate -n vo -L 1000M storage
Logical volume "vo" created.
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/storage/vo
LV Name vo
VG Name storage
LV UUID euhgrY-5rh5-C9vF-usVA-5Gqo-C1rO-3MAThB
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2020-10-25 10:43:11 -0400
LV Status available
# open 0
LV Size 1000.00 MiB
Current LE 250
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
4.最后把生成好的逻辑卷格式化再挂载就可以使用了。XFS文件系统无法进行缩小操作,不建议XFS和LVM一起使用。
Linux系统会把LVM中的逻辑卷设备存放在/dev设备目录中(实际上是做了一个符号链接),同时会以卷组的名称来建立一个目录,其中保存了逻辑卷的设备映射文件(即/dev/卷组名称/逻辑卷名称)。
[root@localhost ~]# mkfs.ext4 /dev/storage/vo
mke2fs 1.44.3 (10-July-2018)
Creating filesystem with 256000 4k blocks and 64000 inodes
Filesystem UUID: bc3e43e7-990b-40e6-95a7-16e4d68510e7
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mount /dev/storage/vo /sata1
[root@localhost ~]# df -h | grep /sata1
/dev/mapper/storage-vo 969M 2.5M 900M 1% /sata1
可以将相关配置写入/etc/fstab配置文件使挂载永久生效。
最后查看挂载状态时会发现空间变小了,其实是正常的,因为硬盘制造时是按1:1000来计算的,系统按1:1024计算。
扩容逻辑卷
1.在redhat 8里面扩容是不需要卸载设备和挂载点的关联的。使用lvextend命令进行扩容操作。
[root@localhost ~]# lvextend -L 2000M /dev/storage/vo
Size of logical volume storage/vo changed from 1000.00 MiB (250 extents) to 1.95 GiB (500 extents).
Logical volume storage/vo successfully resized.
[root@localhost ~]# df -h | grep /sata1
/dev/mapper/storage-vo 969M 2.5M 900M 1% /sata1
2.扩容后再查看挂载状态会发现容量没增加,因为没有同步硬盘容量,先检查硬盘完整性再同步(卸载设备后才可进行)。e2fsck命令检查完整性,resize2fs命令同步。
[root@localhost ~]# umount /sata1
[root@localhost ~]# e2fsck -f /dev/storage/vo
e2fsck 1.44.3 (10-July-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/storage/vo: 11/128000 files (0.0% non-contiguous), 12890/512000 blocks
[root@localhost ~]# resize2fs /dev/storage/vo
resize2fs 1.44.3 (10-July-2018)
The filesystem is already 512000 (4k) blocks long. Nothing to do!
[root@localhost ~]# mount /dev/storage/vo /sata1/
[root@localhost ~]# df -h | grep /sata1
/dev/mapper/storage-vo 2.0G 3.0M 1.9G 1% /sata1
缩小逻辑卷
相较于扩容逻辑卷,在对逻辑卷进行缩容操作时,其丢失数据的风险更大。所以在生产环境中执行相应操作时,一定要提前备份好数据。另外Linux系统规定,在对LVM逻辑卷进行缩容操作之前,要先检查文件系统的完整性(当然这也是为了保证我们的数据安全)。在执行缩容操作前记得先把文件系统卸载掉
1.检查文件系统的完整性。
[root@localhost ~]# umount /sata1
[root@localhost ~]# e2fsck -f /dev/storage/vo
e2fsck 1.44.3 (10-July-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/storage/vo: 11/128000 files (0.0% non-contiguous), 12890/512000 blocks
2.缩小逻辑卷容量
[root@localhost ~]# resize2fs /dev/storage/vo 500M
resize2fs 1.44.3 (10-July-2018)
Resizing the filesystem on /dev/storage/vo to 128000 (4k) blocks.
The filesystem on /dev/storage/vo is now 128000 (4k) blocks long.
[root@localhost ~]# lvreduce -L 500M /dev/storage/vo
WARNING: Reducing active logical volume to 500.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce storage/vo? [y/n]: y
Size of logical volume storage/vo changed from 1.95 GiB (500 extents) to 500.00 MiB (125 extents).
Logical volume storage/vo successfully resized.
3.重新挂载并查看挂载状态,可以看到空间变小了
[root@localhost ~]# mount /dev/storage/vo /sata1/
[root@localhost ~]# df -h | grep sata1
/dev/mapper/storage-vo 477M 1.5M 443M 1% /sata1