LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,最新版本为:稳定版1.0.5,开发版 1.1.0-rc2,以及LVM2开发版。Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。
图1所示LVM抽象模型,展示了PV、VG、LV三者之间关系:
和非LVM系统将包含分区信息的元数据保存在位于分区的起始位置的分区表中一样,逻辑卷以及卷组相关的元数据也是保存在位于物理卷起始处的VGDA(卷组描述符区域)中。VGDA包括以下内容:PV描述符、VG描述符、LV描述符、和一些PE描述符。
系统启动LVM时激活VG,并将VGDA加载至内存,来识别LV的实际物理存储位置。当系统进行I/O操作时,就会根据VGDA建立的映射机制来访问实际的物理位置。
LVM通常用于装备大量磁盘的系统,但它同样适于仅有一、两块硬盘的小系统。
小系统使用LVM的益处
传统的文件系统是基于分区的,一个文件系统对应一个分区。这种方式比较直观,但不易改变:
1.不同的分区相对独立,无相互联系,各分区空间很易利用不平衡,空间不能充分利用;
2.当一个文件系统/分区已满时,无法对其扩充,只能采用重新分区/建立文件系统,非常麻烦;或把分区中的数据移到另一个更大的分区中;或采用符号连接的方式使用其它分区的空间。
3.如果要把硬盘上的多个分区合并在一起使用,只能LVM
采用再分区的方式,这个过程需要数据的备份与恢复。当采用LVM时,情况有所不同:
1.硬盘的多个分区由LVM统一为卷组管理,可以方便的加入或移走分区以扩大或减小卷组的可用容量,充分利用硬盘空间;
2.文件系统建立在逻辑卷上,而逻辑卷可根据需要改变大小(在卷组容量范围内)以满足要求;
3.文件系统建立在LVM上,可以跨分区,方便使用;
大系统使用LVM的益处
在使用很多硬盘的大系统中,使用LVM主要是方便管理、增加了系统的扩展性。
在一个有很多不同容量硬盘的大型系统中,对不同的用户的空间分配是一个技巧性的工作,要在用户需求与实际可用空间中寻求平衡。
用户/用户组的空间建立在LVM上,可以随时按要求增大,或根据使用情况对各逻辑卷进行调整。当系统空间不足而加入新的硬盘时,不必把用户的数据从原硬盘迁移到新硬盘,而只须把新的分区加入卷组并扩充逻辑卷即可。同样,使用LVM可以在不停服务的情况下。把用户数据从旧硬盘转移到新硬盘空间中去。
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─centos-root 253:0 0 17.5G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
sr0 11:0 1 1024M 0 rom
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xc9e99c4c.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (10487808-41943039, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G
Partition 2 of type Linux and of size 5 GiB is set
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xc9e99c4c
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 20973567 5242880 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─centos-root 253:0 0 17.5G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part
└─sdb2 8:18 0 5G 0 part
sr0 11:0 1 1024M 0 rom
- 再创建物理卷
[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdb2
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
##查看物理卷信息
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- 19.51g 40.00m
/dev/sdb1 lvm2 --- 5.00g 5.00g
/dev/sdb2 lvm2 --- 5.00g 5.00g
##详细查看物理卷信息
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 4994
Free PE 10
Allocated PE 4984
PV UUID FWWZhg-n32R-2AqB-eu6m-kg6Y-BrP4-SmBPfL
"/dev/sdb2" is a new physical volume of "5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 34noEq-wS5f-AcCX-lRsE-maah-VoHG-HkbTxM
"/dev/sdb1" is a new physical volume of "5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID xEXe7U-WqDH-6ocE-XLWe-g9L1-OV5T-0Zh6Yb
## 创建一个myvg的卷组
[root@localhost ~]# vgcreate myvg /dev/sdb1 /dev/sdb2
Volume group "myvg" successfully created
##查看卷组信息
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- 19.51g 40.00m
myvg 2 0 0 wz--n- 9.99g 9.99g
##详细查看纳卷组信息
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4984 / 19.47 GiB
Free PE / Size 10 / 40.00 MiB
VG UUID vKEpwt-8M0v-O0w3-KWCd-875K-v6Z0-38aeBn
--- Volume group ---
VG Name myvg
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 9.99 GiB
PE Size 4.00 MiB
Total PE 2558
Alloc PE / Size 0 / 0
Free PE / Size 2558 / 9.99 GiB
VG UUID SiZRqd-CYnB-9nim-ZYHg-2qd9-Mvvv-Gu0le2
## 创建lv的逻辑卷
[root@localhost ~]# lvcreate -n lv -l 50 myvg
Logical volume "lv" created.
##查看逻辑卷信息
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 17.47g
swap centos -wi-ao---- 2.00g
lv myvg -wi-a----- 200.00m
## 详细查看逻辑卷信息
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID OR9DVA-AeG5-jbFn-teQh-e6SA-TiUJ-XJkoVC
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2022-06-20 14:08:30 -0400
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID S3yXNO-wYMD-Liyy-B3aF-Rl3M-dHB2-CxAOdE
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2022-06-20 14:08:30 -0400
LV Status available
# open 1
LV Size 17.47 GiB
Current LE 4472
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/myvg/lv
LV Name lv
VG Name myvg
LV UUID sE9uxm-tqPd-vBit-rQIT-3Yu0-s9Gd-5b6Sah
LV Write Access read/write
LV Creation host, time localhost, 2022-07-11 21:50:54 -0400
LV Status available
# open 0
LV Size 200.00 MiB
Current LE 50
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
#创建挂在点
[root@localhost ~]# mkdir /mnt/lv
#格式化逻辑卷
[root@localhost ~]# mkfs.ext4 /dev/myvg/lv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
#临时挂载
[root@localhost ~]# mount /dev/myvg/lv /mnt/lv/
#查看已经挂载的设备
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 18G 1.1G 17G 6% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 125M 373M 25% /boot
tmpfs tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/myvg-lv ext4 190M 1.6M 175M 1% /mnt/lv
# 卸载挂载点
[root@localhost ~]# umount /mnt/lv/
# -L 直接指定扩容的大小
[root@localhost ~]# lvextend -L 5G /dev/myvg/lv
Size of logical volume myvg/lv changed from 200.00 MiB (50 extents) to 5.00 GiB (1280 extents).
Logical volume lv successfully resized.
# 查看扩容信息
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 17.47g
swap centos -wi-ao---- 2.00g
lv myvg -wi-a----- 5.00g
#详细察看
[root@localhost ~]# lvdisplay /dev/myvg/lv
--- Logical volume ---
LV Path /dev/myvg/lv
LV Name lv
VG Name myvg
LV UUID sE9uxm-tqPd-vBit-rQIT-3Yu0-s9Gd-5b6Sah
LV Write Access read/write
LV Creation host, time localhost, 2022-07-11 21:50:54 -0400
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 1280
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
#挂载使用
[root@localhost ~]# mount /dev/myvg/lv /mnt/lv/
# 可以看到逻辑卷没有扩容到5G
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 18G 1.1G 17G 6% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 125M 373M 25% /boot
tmpfs tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/myvg-lv ext4 190M 1.6M 175M 1% /mnt/lv
# 同步文件系统容量到内核
[root@localhost ~]# resize2fs /dev/myvg/lv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/lv is mounted on /mnt/lv; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 40
The filesystem on /dev/myvg/lv is now 5242880 blocks long.
#可以看到逻辑卷扩容到5G
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 18G 1.1G 17G 6% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 125M 373M 25% /boot
tmpfs tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/myvg-lv ext4 4.9G 3.0M 4.7G 1% /mnt/lv
#卸载挂载dian
[root@localhost ~]# umount /dev/myvg/lv
#检查文件系统的完整性。
[root@localhost ~]# e2fsck -f /dev/myvg/lv
e2fsck 1.42.9 (28-Dec-2013)
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/myvg/lv: 11/1310720 files (0.0% non-contiguous), 172857/5242880 blocks
#通知系统内核将逻辑卷vo的容量减小到1G。
[root@localhost ~]# resize2fs /dev/myvg/lv 1G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/lv to 1048576 (1k) blocks.
The filesystem on /dev/myvg/lv is now 1048576 blocks long.
#将LV逻辑卷的容量修改为1G。
[root@localhost ~]# lvreduce -L 1G /dev/myvg/lv
WARNING: Reducing active logical volume to 1.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv? [y/n]: y
Size of logical volume myvg/lv changed from 5.00 GiB (1280 extents) to 1.00 GiB (256 extents).
Logical volume lv successfully resized.
#挂载使用
[root@localhost ~]# mount /dev/myvg/lv /mnt/lv/
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 18G 1.1G 17G 6% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 125M 373M 25% /boot
tmpfs tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/myvg-lv ext4 988M 2.7M 940M 1% /mnt/lv
[root@localhost ~]# umount /dev/myvg/lv
[root@localhost ~]# lvremove /dev/myvg/lv
Do you really want to remove active logical volume lv? [y/n]: y
Logical volume "lv" successfully removed
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 17.47g
swap centos -wi-ao---- 2.00g
[root@localhost ~]# vgremove /dev/myvg
Volume group "myvg" successfully removed
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- 19.51g 40.00m
[root@localhost ~]# pvremove /dev/sdb[1-2]
Labels on physical volume "/dev/sdb1" successfully wiped
Labels on physical volume "/dev/sdb2" successfully wiped
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- 19.51g 40.00m