LVM是**逻辑盘卷管理(Logical Volume Manager)**的简称,它是Linux环境下对硬盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高硬盘分区管理的灵活性。
在日常工作或者学习中,随着电脑的不断被使用,我们的硬盘空间可能会越来越小,这时候,我们不能直接更换大硬盘,因为硬盘有价,数据无价,这时候我们就可以使用LVM,LVM将存储虚拟化,使用逻辑卷,你不会受限于物理硬盘的大小,另外,和硬件相关的存储设置被其隐藏,你能不用停止应用或卸载文件系统来调整卷大小或数据迁移。这样能减少操作成本,LVM最大的特点就是可以对硬盘进行动态管理。因为逻辑卷的大小是可以动态调整的,而且不会丢失现有的数据。如果我们新增加了硬盘,其也不会改变现有上层的逻辑卷。作为一个动态硬盘管理机制,逻辑卷技术大大提高了硬盘管理的灵活性。
LVM就是通过将底层的物理硬盘抽象的封装起来,然后以逻辑卷的方式呈现给上层应用。在传统的硬盘管理机制中,我们的上层应用是直接访问文件系统,从而对底层的物理硬盘进行读取,而在LVM中,其通过对底层的硬盘进行封装,当我们对底层的物理硬盘进行操作时,其不再是针对于分区进行操作,而是通过一个叫做逻辑卷的东西来对其进行底层的硬盘管理操作。
物理卷(PV,Physical Volume)
物理卷就是指硬盘、硬盘分区或从逻辑上和硬盘分区具有同样功能的设备(如RAID)。它在LVM中处于最底层,是LVM的基本存储逻辑块。但与基本的物理存储介质相比,它包含了与LVM相关的管理参数。
卷组(VG,Volume Group)
卷组建立在物理卷之上,由一个或多个物理卷组成,在卷组建立之后可以动态添加物理卷到卷组中。
逻辑卷(LV,Logical Volume)
逻辑卷建立在卷组之上,卷组中的未分配空间可以用于建立新的逻辑卷,逻辑卷建立后可以动态地扩展和缩小空间。系统中的多个逻辑卷可以属于同一个卷组,也可以属于不同的卷组。
物理块(PE,Physical Extent)
LVM系统中最小的存储区块,每个PE区块默认大小为4M。
对上层应用或用户来说,逻辑卷(LV)就是分区,所以还需要格式化和挂载。
pvcreate
新建物理卷pvdisplay
显示物理卷信息pvremove
删除物理卷pvscan / pvs
简要显示物理卷信息
vgcreate
新建卷组(可选参数-s,设置PE大小,默认为4M)vgdisplay
显示卷组信息vgremove
删除卷组vgextend
扩展卷组vgscan / vgs
简要显示卷组信息vgreduce
缩减卷组大小
lvcreate
新建逻辑卷lvdisplay
显示逻辑卷信息lvremove
删除逻辑卷lvextend
扩展逻辑卷lvscan / vgs
简要显示逻辑卷信息lvreduce
缩减逻辑卷大小
为虚拟机添加一块硬盘(如5G),给原来的硬盘扩容一定空间(如10G)
检查系统中是否安装了LVM管理工具
rpm -qa |grep lvm
#llvm-private-7.0.1-1.el7.x86_64
#lvm2-libs-2.02.186-7.el7_8.2.x86_64
#lvm2-2.02.186-7.el7_8.2.x86_64
#mesa-private-llvm-3.9.1-3.el7.x86_64
如果未安装,则通过yum安装
yum install -y lvm*
fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors # 硬盘 sda(已扩容10G)
Units = sectors of 1 * 512 = 512 bytes # 每个扇区 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: 0x000ac1d5
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux # 分区 sda1
/dev/sda2 2099200 41943039 19921920 8e Linux LVM # 分区 sda2
Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors # 硬盘 sdb(新添加的5G硬盘)
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: 0x942a9fee
Device Boot Start End Blocks Id System # 无分区
Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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
可以看出:硬盘sda
总容量为32.2G,已经创建了两个分区,占用了近20G的空间;而硬盘sdb
总容量近5G(有一部分信息占用),还没有分区。
分别创建三个分区sda3(2G)
、sda4(3G)
、sdb1(5G)
fdisk /dev/sda ## 先创建硬盘sda1的两个分区
#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.
Command (m for help):
输入m
查看可以执行的操作
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition # 删除一个分区
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu # 显示本提示菜单
n add a new partition # 新建一个分区
o create a new empty DOS partition table
p print the partition table # 显示分区表
q quit without saving changes # 退出,不保存修改
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit # 保存并退出
x extra functionality (experts only)
按照以下流程创建硬盘sda
的分区
Command (m for help): n ## 输入n选择新建分区
#Partition type:
# p primary (2 primary, 0 extended, 2 free)
# e extended
Select (default p): p ## 输入p选择分区类型primary
Partition number (3,4, default 3): ## 输入分区编号,默认即可,直接按回车
First sector (41943040-62914559, default 41943040): ## 输入起始扇区,如无特殊需求,默认即可
#Using default value 41943040
Last sector, +sectors or +size{
K,M,G} (41943040-62914559, default 62914559):+2G ## 输入终止扇区,输入+2G
#Partition 3 of type Linux and of size 2 GiB is set
Command (m for help): n ## 相同方法建立分区sda4(3G)
#Partition type:
# p primary (3 primary, 0 extended, 1 free)
# e extended
Select (default e): p
# Selected partition 4
First sector (46137344-62914559, default 46137344):
#Using default value 46137344
Last sector, +sectors or +size{
K,M,G} (46137344-62914559, default 62914559): +3G
#Partition 4 of type Linux and of size 3 GiB is set
Command (m for help): p ## 查看分区表
#Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000ac1d5
# Device Boot Start End Blocks Id System
#/dev/sda1 * 2048 2099199 1048576 83 Linux
#/dev/sda2 2099200 41943039 19921920 8e Linux LVM
#/dev/sda3 41943040 46137343 2097152 83 Linux
#/dev/sda4 46137344 52428799 3145728 83 Linux
Command (m for help): w ## 写入并退出,输入w
#The partition table has been altered!
#Calling ioctl() to re-read partition table.
## 如果出现以下警告,需要通过partprobe命令刷新分区表
#WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
#The kernel still uses the old table. The new table will be used at
#the next reboot or after you run partprobe(8) or kpartx(8)
#Syncing disks.
按相同步骤创建sdb
的分区
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.
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-10485759, default 2048):
#Using default value 2048
Last sector, +sectors or +size{
K,M,G} (2048-10485759, default 10485759):
#Using default value 10485759
#Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): p
#Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0x942a9fee
# Device Boot Start End Blocks Id System
#/dev/sdb1 2048 10485759 5241856 83 Linux
Command (m for help): w
#The partition table has been altered!
#Calling ioctl() to re-read partition table.
#Syncing disks.
pvcreate /dev/sda3
# Physical volume "/dev/sda3" successfully created.
pvcreate /dev/sda4
# Physical volume "/dev/sda4" successfully created.
pvcreate /dev/sdb1
# Physical volume "/dev/sdb1" successfully created.
查看物理卷
pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4863
Free PE 0
Allocated PE 4863
PV UUID qTo5uY-ecSU-r1aW-1OMu-l1Pt-e57r-n2HbTM
"/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 ooXcxF-N5iX-CTMP-BZUX-M0gU-XYMS-GUMbJe
"/dev/sda4" is a new physical volume of "3.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda4
VG Name
PV Size 3.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID DYViEl-d73z-VfR8-a9Iw-2fUh-2iJ1-BqvpKl
"/dev/sda3" is a new physical volume of "2.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 2.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID q88GX5-4TdH-wSSY-Ijd0-9oWC-H1Io-zL9bPj
将分区sda3与分区sdb1组合创建为卷组vg2
vgcreate vg2 /dev/sda3 /dev/sdb1
# Volume group "vg2" successfully created
显示卷组信息
vgdisplay vg2
# --- Volume group ---
# VG Name vg2 ## 卷组名
# 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 6.99 GiB ## 卷组大小
# PE Size 4.00 MiB ## PE大小(默认4M)
# Total PE 1790
# Alloc PE / Size 0 / 0
# Free PE / Size 1790 / 6.99 GiB
# VG UUID Xdlgm2-UM6n-wMRi-1tY3-4L3Q-iDSZ-trI59P
创建属于卷组vg2的逻辑卷lv1
lvcreate -L 4G -n lv1 vg2 ## lvcreate: -l pe-num 以PE数建立逻辑卷
# Logical volume "lv1" created. ## -L size 指定容量建立逻辑卷
## -n name 指定逻辑卷名称
显示逻辑卷
lvdisplay /dev/vg2/lv1
# --- Logical volume ---
# LV Path /dev/vg2/lv1 ## 逻辑卷路径
# LV Name lv1 ## 逻辑卷名
# VG Name vg2 ## 逻辑卷所属卷组名
# LV UUID f1mvDg-Unck-0G9B-6zsD-Tfms-NViR-cgoIkK
# LV Write Access read/write
# LV Creation host, time centos, 2020-09-10 13:19:41 +0800
# LV Status available
# # open 0
# LV Size 4.00 GiB ## 逻辑卷大小
# Current LE 1024
# Segments 1
# Allocation inherit
# Read ahead sectors auto
# - currently set to 8192
# Block device 253:2
mkfs.xfs /dev/vg2/lv1 ## 将逻辑卷lv1格式化为xfs文件系统
#meta-data=/dev/vg2/lv1 isize=512 agcount=4, agsize=262144 blks ## 与逻辑卷对应的文件系统名称
# = sectsz=512 attr=2, projid32bit=1
# = crc=1 finobt=0, sparse=0
#data = bsize=4096 blocks=1048576, imaxpct=25
# = sunit=0 swidth=0 blks
#naming =version 2 bsize=4096 ascii-ci=0 ftype=1
#log =internal log bsize=4096 blocks=2560, version=2
# = sectsz=512 sunit=0 blks, lazy-count=1
#realtime =none extsz=4096 blocks=0, rtextents=0
将格式化后的逻辑卷挂载到系统目录中即可使用(确保挂载目录未被占用)
mount /dev/vg2/lv1 /mnt ## 挂载到/mnt目录
df -Th /mnt ## 查看挂载信息
#Filesystem Type Size Used Avail Use% Mounted on
#/dev/mapper/vg2-lv1 xfs 4.0G 33M 4.0G 1% /mnt ## /dev/mapper/vg2-lv1注意该名称,后续会用
进入挂载目录,新建一个文件并写入一些信息
cd /mnt
ls
echo test if this file will be cleaned > file
cat file
目前容量大小:
名称 | 大小 |
---|---|
vg2 | 7G |
lv1 | 4G |
sda4 | 3G |
根据卷组是否能够提供足够容量为逻辑卷扩容,扩容也分为两种:直接扩容逻辑卷和先扩容卷组再扩容逻辑卷。
目标:以物理卷sda4
扩容卷组vg2
,继而为逻辑卷lv1
扩容至9G
vgextend vg2 /dev/sda4
# Volume group "vg2" successfully extended
vgdisplay vg2
# --- Volume group ---
# VG Name vg2
# System ID
# Format lvm2
# Metadata Areas 3
# Metadata Sequence No 3
# VG Access read/write
# VG Status resizable
# MAX LV 0
# Cur LV 1
# Open LV 1
# Max PV 0
# Cur PV 3
# Act PV 3
# VG Size <9.99 GiB ## 扩容后的卷组大小
# PE Size 4.00 MiB
# Total PE 2557
# Alloc PE / Size 1024 / 4.00 GiB
# Free PE / Size 1533 / <5.99 GiB
# VG UUID Xdlgm2-UM6n-wMRi-1tY3-4L3Q-iDSZ-trI59P
lvextend -L +5G /dev/vg2/lv1
# Size of logical volume vg2/lv1 changed from 4.00 GiB (1024 extents) to 9.00 GiB (2304 extents).
# Logical volume vg2/lv1 successfully resized.
lvdisplay /dev/vg2/lv1
# --- Logical volume ---
# LV Path /dev/vg2/lv1
# LV Name lv1
# VG Name vg2
# LV UUID f1mvDg-Unck-0G9B-6zsD-Tfms-NViR-cgoIkK
# LV Write Access read/write
# LV Creation host, time centos, 2020-09-10 13:19:41 +0800
# LV Status available
# # open 1
# LV Size 9.00 GiB ## 扩容后的逻辑卷大小
# Current LE 2304
# Segments 3
# Allocation inherit
# Read ahead sectors auto
# - currently set to 8192
# Block device 253:2
但查看实际的硬盘容量,发现并没有改变,仍未4G,而非9G
df -Th /mnt
#Filesystem Type Size Used Avail Use% Mounted on
#/dev/mapper/vg2-lv1 xfs 4.0G 33M 4.0G 1% /mnt
这是因为需要更新文件系统才会使更改生效
xfs_growfs /dev/vg2/lv1
#meta-data=/dev/mapper/vg2-lv1 isize=512 agcount=4, agsize=262144 blks
# = sectsz=512 attr=2, projid32bit=1
# = crc=1 finobt=0 spinodes=0
#data = bsize=4096 blocks=1048576, imaxpct=25
# = sunit=0 swidth=0 blks
#naming =version 2 bsize=4096 ascii-ci=0 ftype=1
#log =internal bsize=4096 blocks=2560, version=2
# = sectsz=512 sunit=0 blks, lazy-count=1
#realtime =none extsz=4096 blocks=0, rtextents=0
#data blocks changed from 1048576 to 2359296
df -Th /mnt
#Filesystem Type Size Used Avail Use% Mounted on
#/dev/mapper/vg2-lv1 xfs 9.0G 33M 9.0G 1% /mnt
如果逻辑卷格式化时,文件系统为ext4格式,则更新命令应为 resize2fs
如果已存在的物理卷不足以将逻辑卷扩容至所需大小,则应先进行硬盘分区并创建物理卷后再按以上步骤进行扩容
查看挂载目录下的测试文件,并没有被清除,证明了LVM能够动态扩容容量而不丢失数据
cat /mnt/file
#test if this file will be cleaned
一般情况下,只是对磁盘进行扩容,缩减并没有实际意义。
并且,xfs文件系统只支持扩容而不支持缩减,ext4文件系统支持缩减,以下为简要步骤(假设刚才格式化文件系统为ext4格式)
-
则为缩减至4G)若要移除LVM,则按照创建的过程反向操作,即:
取消挂载点并检查(不要在相应挂载目录中操作,否则会报被占用)
umount /mnt
df -Th /mnt
#Filesystem Type Size Used Avail Use% Mounted on
#/dev/mapper/centos-root xfs 17G 14G 3.2G 82% /
移除逻辑卷LV并检查
lvremove /dev/mapper/vg2-lv1 ## 移除逻辑卷
#Do you really want to remove active logical volume vg2/lv1? [y/n]: y ## 输入y确认移除
# Logical volume "lv1" successfully removed
lvs
# LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
# root centos -wi-ao---- <17.00g
# swap centos -wi-ao---- 2.00g ## 逻辑卷lv1已不存在
移除卷组VG并检查
vgremove vg2
# Volume group "vg2" successfully removed
vgs
# VG #PV #LV #SN Attr VSize VFree
# centos 1 2 0 wz--n- <19.00g 0 ## 卷组vg2已不存在
移除物理卷PV并检查
pvremove /dev/sda3 /dev/sda4 /dev/sdb1
# Labels on physical volume "/dev/sda3" successfully wiped.
# Labels on physical volume "/dev/sda4" successfully wiped.
# Labels on physical volume "/dev/sdb1" successfully wiped.
pvs
# PV VG Fmt Attr PSize PFree
# /dev/sda2 centos lvm2 a-- <19.00g 0 ## 三个逻辑卷已不存在
至此,LVM已被移除,可选择继续删除分区。
删除分区并检查
fdisk /dev/sda
#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.
Command (m for help): d
Partition number (1-4, default 4): ## 回车选择默认sda4分区
#Partition 4 is deleted
Command (m for help): d
Partition number (1-3, default 3): ## 回车选择默认sda3分区
#Partition 3 is deleted
Command (m for help): w
#The partition table has been altered!
#Calling ioctl() to re-read partition table.
#WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
#The kernel still uses the old table. The new table will be used at
#the next reboot or after you run partprobe(8) or kpartx(8)
#Syncing disks.
partprobe ## 刷新分区表
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.
Command (m for help): d
#Selected partition 1
#Partition 1 is deleted
Command (m for help): w
#The partition table has been altered!
#Calling ioctl() to re-read partition table.
#Syncing disks.
fdisk -l /dev/sda /dev/sdb
#Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000ac1d5
# Device Boot Start End Blocks Id System
#/dev/sda1 * 2048 2099199 1048576 83 Linux
#/dev/sda2 2099200 41943039 19921920 8e Linux LVM ## 分区sda3、sda4已不存在
#Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0x942a9fee
# Device Boot Start End Blocks Id System ## 分区sdb1已不存在