逻辑卷管理器LVM(Logicl Volume Manager),通过使用逻辑卷管理器对硬盘存储设备进行管理,可以实现硬盘空间的动态划分和调整
逻辑卷管理基本概念
-
组成部分
逻辑卷管理由三部分组成:物理卷、卷组、逻辑卷
物理卷(PV-Physical Volume):可以是实际物理硬盘的分区,也可以是整个物理硬盘
卷组(VG-Volume Group):建立在物理卷之上,一个卷组至少包含一个物理卷,在卷组建立后可以动态添加物理卷
逻辑卷:(LV-Logical Volume):建立在卷组之上,建立后可以动态扩展和缩小空间,系统中多个逻辑卷可以属于同一个卷组,也可属于不同的多个卷组
-
两种表示方式
/dev/卷组/逻辑卷
/dev/mapper/卷组-逻辑卷
- 查看方式 下文的实验,统一使用如下命令监控pv,vg,lv的变化情况
十八 、LVM逻辑卷管理 十八 、LVM逻辑卷管理
逻辑卷创建过程
-
更改分区卷标为8e
首先 fdisk /dev/vdb 将现有分区卷标改为8e,将分区类型做成 Linux LVM可扩展类型
十八 、LVM逻辑卷管理 - pvcreate 物理卷、vgcreate 卷组
[[email protected] ~] # pvcreate /dev/sdb1 # 创建物理卷 [[email protected] ~] # vgcreate weixingroup /dev/sdb1 # 创建卷组,组内包含/dev/sdb1 # 创建VG卷组 # vgcreate [options] VGname pv1 pv2 # 常见选项: # -s 指定PE大小,默认为4M # 物理扩展单元PE:将VG中的PV按照一定标准划分,划分标准称为PE,PE是LVM寻址的最小单元
十八 、LVM逻辑卷管理 十八 、LVM逻辑卷管理 - lvcreate 逻辑卷
[[email protected] ~] # lvcreate -L 100M -n test weixingroup # 创建逻辑卷,-L指定大小, # -n指定名称,从weixingroup组中取 [[email protected] ~] # mkfs.xfs /dev/weixingroup/test # 创建文件系统 [[email protected] ~] # mount /dev/weixingroup/test /mnt/ # 挂载
十八 、LVM逻辑卷管理 十八 、LVM逻辑卷管理 # 补充,创建lv时,可加入选项 -l -L # lvcreate -l pe数量 -n lv名称 vg名 # lvcreate -L lv大小 -n lv名称 vg名 [[email protected] ~] # lvcreate -l 100 -n test weixingroup # 指定test大小为100个pe [[email protected] ~] # lvcreate -L 400 -n test weixingroup # 指定test大小为400M
拉升与缩减逻辑卷
-
逻辑卷拉升(xfs)
xfs文件系统支持拉升,但不支持缩小
[[email protected] ~] # lvextend -L 150M /dev/weixingroup/test # 将逻辑卷test拉升到150M [[email protected] ~] # xfs_growfs /dev/weixingroup/test # 拉升文件系统
十八 、LVM逻辑卷管理 如果我们想获得更大的容量,就得给卷组中新增物理卷十八 、LVM逻辑卷管理 [[email protected] ~] # pvcreate /dev/sdb2 # 新增物理卷/dev/sdb2,提前改卷标 [[email protected] ~] # vgextend weixingroup /dev/sdb2 # 将/dev/sdb2加入卷组weixingroup [[email protected] ~] # lvextend -L 300M /dev/weixingroup/test # 拉升逻辑卷test到300M [[email protected] ~] # xfs_growfs /dev/weixingroup/test # 拉升文件系统
十八 、LVM逻辑卷管理 十八 、LVM逻辑卷管理
逻辑卷拉升,先扩逻辑卷,再扩文件系统[[email protected] ~] # lvextend -r -L 300M /dev/weixingroup/test #拉升逻辑卷同时同步文件系统,加-r
- 逻辑卷缩减(ext4)
umount /mnt # 之前挂载了逻辑卷,先卸载 mkfs.ext4 /dev/weixingroup/test # 格式化为ext4文件系统,因为xfs无法缩减 e2fsck -f /dev/weixingroup/test # 检查文件系统完整性 resize2fs /dev/weixingroup/test 100M # 缩减文件系统到100M 扩容时不加‘100M’ lvreduce -L 100M /dev/weixingroup/test # 缩减逻辑卷到100M mount /dev/weixingroup/test /mnt/ # 重新挂载
十八 、LVM逻辑卷管理 十八 、LVM逻辑卷管理 十八 、LVM逻辑卷管理
逻辑卷快照
[[email protected] ~] # touch /weixindata/data{1..5} # 在原始设备test挂载点中创建文件
[[email protected] ~] # ls /weixindata # 该目录下生成5个文件
file1 file2 file3 file4 file5
[[email protected] ~] # umount /weixindata # 卸载test
[[email protected] ~] # ls /weixindata # test卸载,该目录下无文件
[[email protected] ~] # lvcreate -L 20M -n test_photo -s /dev/weixingroup/test # 创建快照,-n名称
# -s从谁照
[[email protected] ~] # mount /dev/weixingroup/test_photo /wexindata/ # 挂载快照
[[email protected] ~] # ls /weixindata/ # 查看有上面5个文件
file1 file2 file3 file4 file5
原始逻辑卷test创建文件,方便对比:
创建快照test_photo:
挂载快照test_photo,也能看到test上创建的五个file文件:
[[email protected] ~] # rm -fr /weixindata/* # 删除/weixindata目录中所有文件
[[email protected] ~] # umount /weixindata # 卸载/weixindata挂载的test_photo
[[email protected] ~] # lvremove /dev/weixingroup/test_photo # 删除该逻辑卷快照
[[email protected] ~] # lvcreate -L 50M -n test_photo2 -s /dev/weixingroup/test # 重新创建快照test_photo2
[[email protected] ~] # mount /dev/weixingroup/test_photo2 /weixindata # 重新挂载快照2
[[email protected] ~] # ls /weixindata/ # 查看该目录下,依然有这五个文件
file1 file2 file3 file4 file5
逻辑卷清除
[[email protected] ~] # umout /dev/weixingroup/test # 先卸载逻辑卷
[[email protected] ~] # lvremove /dev/weixingroup/test # 删除逻辑卷
[[email protected] ~] # vgremove weixingroup # 删除卷组
[[email protected] ~] # pvremove /dev/sdb{1,2} # 删除物理卷
[[email protected] ~] # fdisk /dev/sdb # 删除物理分区