前言:
lvm、
pv、
vg、
lv、
格式化、挂载、
xfs格式扩容、
ext4格式扩容、缩容
导图
LVM
1. 基本过程
把物理磁盘或者分区初始化称为物理卷(PV),然后把 PV 加入 VG(卷组),
最后在 VG 上划分逻辑的分区(LVM),LVM 可以当做普通的分区进行格式化和挂载。
名词 |
说明 |
LVM |
逻辑卷管理(可以动态调整分区大小) |
PV(physical volume) |
物理卷 |
VG(volume Group) |
卷组 |
LV(logical volume) |
逻辑卷 |
2. PV 操作
命令 |
说明 |
pvcreate |
创建 PV(将物理硬盘分区初始化为物理卷) |
pvscan |
搜寻 PV(扫描系统中所有硬盘的物理卷列表) |
pvdisplay |
显示 PV 状态(显示物理卷的属性) |
pvremove |
删除 PV(删除一个存在的物理卷) |
pvchange |
修改 PV(修改物理卷属性) |
[root@localhost ~]# fdisk -l | grep /dev/sdb
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
[root@localhost ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
[root@localhost ~]# pvscan | grep /dev/sdb
PV /dev/sdb lvm2 [20.00 GiB]
[root@localhost ~]# pvdisplay /dev/sdb
"/dev/sdb" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID TUrXPG-4nJW-3eMM-bxlQ-ekD0-gfXi-8XcPZm
[root@localhost ~]# pvremove /dev/sdb
Labels on physical volume "/dev/sdb" successfully wiped.
3. VG 操作
命令 |
说明 |
vgcreate |
创建 VG(用于创建LVM卷组) |
vgscan |
搜寻 VG(扫描并显示系统中的卷组) |
vgdisplay |
显示 VG 状态(显示LVM卷组的信息) |
vgextend |
扩容 VG(向卷组中添加物理卷) |
vgreduce |
缩容 VG(从卷组中删除物理卷) |
vgrename |
重命名 VG(使用vgrename命令可以重命名卷组的名称) |
vgchange |
修改 VG(修改卷组属性) |
vgremove |
删除 VG(用于用户删除LVM卷组) |
[root@localhost ~]# vgcreate vgtest /dev/sdb
Volume group "vgtest" successfully created
[root@localhost ~]# vgscan | grep vgtest
Found volume group "vgtest" using metadata type lvm2
[root@localhost ~]# vgdisplay vgtest
--- Volume group ---
VG Name vgtest
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 20.00 GiB
PE Size 4.00 MiB
Total PE 5119
Alloc PE / Size 0 / 0
Free PE / Size 5119 / 20.00 GiB
VG UUID Lrl26R-yXP4-ZA7z-DSd6-FYVW-e9d4-LHoDT2
[root@localhost ~]# vgextend vgtest /dev/sdc
Volume group "vgtest" successfully extended
[root@localhost ~]# vgreduce vgtest /dev/sdc
Removed "/dev/sdc" from volume group "vgtest"
[root@localhost ~]# vgrename vgtest vgtest_1
Volume group "vgtest" successfully renamed to "vgtest_1"
//将卷组"vgtest_1"设置为活动状态
[root@localhost ~]# vgchange -ay vgtest
0 logical volume(s) in volume group "vgtest" now active
[root@localhost ~]# vgremove vgtest_1
Volume group "vgtest_1" successfully removed
4、创建 LV
命令 |
说明 |
lvcreate |
创建 LV(用于创建LVM的逻辑卷) |
lvscan |
搜寻 LV(扫描逻辑卷) |
lvdisplay |
显示 LV 状态(显示逻辑卷属性) |
lvextend |
扩容 LV(扩展逻辑卷空间) |
lvreduce |
缩容 LV(收缩逻辑卷空间) |
lvremove |
删除 LV(删除指定LVM逻辑卷) |
lvresize |
调整 LV(调整逻辑卷空间大小) |
[root@localhost ~]# lvcreate -L 10G -n lvtest vgtest
Logical volume "lvtest" created.
[root@localhost ~]# lvscan | grep vgtest
ACTIVE '/dev/vgtest/lvtest' [10.00 GiB] inherit
[root@localhost ~]# lvdisplay /dev/vgtest/lvtest
--- Logical volume ---
LV Path /dev/vgtest/lvtest
LV Name lvtest
VG Name vgtest
LV UUID nBo7xv-yABi-R582-Csvx-q1s8-GuQa-vx8jiW
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2021-02-15 03:48:52 +0800
LV Status available
# open 0
LV Size 10.00 GiB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
[root@localhost ~]# lvextend -L +5G /dev/vgtest/lvtest
Size of logical volume vgtest/lvtest changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
Logical volume vgtest/lvtest successfully resized.
[root@localhost ~]# lvreduce -L -2G /dev/vgtest/lvtest
WARNING: Reducing active logical volume to 13.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vgtest/lvtest? [y/n]: y
Size of logical volume vgtest/lvtest changed from 15.00 GiB (3840 extents) to 13.00 GiB (3328 extents).
Logical volume vgtest/lvtest successfully resized.
[root@localhost ~]# lvremove -f /dev/vgtest/lvtest
Logical volume "lvtest" successfully removed
[root@localhost ~]# lvresize -L +1G /dev/vgtest/lvtest
Size of logical volume vgtest/lvtest changed from 13.00 GiB (3328 extents) to 14.00 GiB (3584 extents).
Logical volume vgtest/lvtest successfully resized.
[root@localhost ~]# lvresize -L -4G /dev/vgtest/lvtest
WARNING: Reducing active logical volume to 10.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vgtest/lvtest? [y/n]: y
Size of logical volume vgtest/lvtest changed from 14.00 GiB (3584 extents) to 10.00 GiB (2560 extents).
Logical volume vgtest/lvtest successfully resized.
4、格式化
[root@localhost ~]# mkfs.xfs /dev/vgtest/lvtest
5、挂载
[root@localhost ~]# mkdir /mnt/lvtest
[root@localhost ~]# mount /dev/vgtest/lvtest /mnt/lvtest
[root@localhost ~]# df -Th /mnt/lvtest/
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/vgtest-lvtest xfs 10G 33M 10G 1% /mnt/lvtest
LVM 扩展
1、XFS 格式的扩容
目前的 XFS 文件系统中,只能放大不能缩小
[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]# pvcreate /dev/sdb9
[root@localhost ~]# vgcreate testvg /dev/sdb9
[root@localhost ~]# lvcreate -L 500M -n testlv testvg
[root@localhost ~]# mkfs.xfs /dev/testvg/testlv
[root@localhost ~]# mkdir /TestLvm
[root@localhost ~]# mount /dev/testvg/testlv /TestLvm
[root@localhost ~]# lvextend -L +200M /dev/testlv/testvg
[root@localhost ~]# lvscan
[root@localhost ~]# df -hT /TestLvm
[root@localhost ~]# xfs_growfs /TestLvm
[root@localhost ~]# df -hT /TestLvm
2、ext4 格式的扩容
[root@localhost ~]# mkfs.ext4 /dev/vgtest/lvtest
[root@localhost ~]# mount /dev/vgtest/lvtest /mnt/lvtest
[root@localhost ~]# df -hT /dev/vgtest/lvtest
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/vgtest-lvtest ext4 9.8G 37M 9.2G 1% /mnt/lvtest
[root@localhost ~]# lvresize -L +2G /dev/vgtest/lvtest
Size of logical volume vgtest/lvtest changed from 10.00 GiB (2560 extents) to 12.00 GiB (3072 extents).
Logical volume vgtest/lvtest successfully resized.
[root@localhost ~]# umount /mnt/lvtest
[root@localhost ~]# e2fsck -f /dev/vgtest/lvtest
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vgtest/lvtest: 11/655360 files (0.0% non-contiguous), 83137/2621440 blocks
[root@localhost ~]# resize2fs /dev/vgtest/lvtest
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vgtest/lvtest to 3145728 (4k) blocks.
The filesystem on /dev/vgtest/lvtest is now 3145728 blocks long.
[root@localhost ~]# mount /dev/vgtest/lvtest /mnt/lvtest/
[root@localhost ~]# df -hT /dev/vgtest/lvtest
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/vgtest-lvtest ext4 12G 41M 12G 1% /mnt/lvtest
[root@localhost ~]# lvs vgtest
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvtest vgtest -wi-ao---- 12.00g
[root@localhost ~]# umount /mnt/lvtest
[root@localhost ~]# e2fsck -f /dev/vgtest/lvtest
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vgtest/lvtest: 11/786432 files (0.0% non-contiguous), 92388/3145728 blocks
//使用resize2fs对lv逻辑卷的逻辑边界空间大小调整为缩减后的大小
[root@localhost ~]# resize2fs /dev/vgtest/lvtest 10G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vgtest/lvtest to 2621440 (4k) blocks.
The filesystem on /dev/vgtest/lvtest is now 2621440 blocks long.
//使用lvreduce对lv逻辑卷的物理边界进行缩减
[root@localhost ~]# lvreduce -L 10G /dev/vgtest/lvtest
WARNING: Reducing active logical volume to 10.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vgtest/lvtest? [y/n]: y
Size of logical volume vgtest/lvtest changed from 12.00 GiB (3072 extents) to 10.00 GiB (2560 extents).
Logical volume vgtest/lvtest successfully resized.
[root@localhost ~]# mount /dev/vgtest/lvtest /mnt/lvtest/
[root@localhost ~]# df -hT /dev/vgtest/lvtest
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/vgtest-lvtest ext4 9.8G 37M 9.2G 1% /mnt/lvtest