一、原理简介
1、LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauelshagen在Linux 2.4内核上实现。
2、LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。
3、与传统的磁盘与分区相比,LVM为计算机提供了更高层次的磁盘存储。它使系统管理员可以更方便的为应用与用户分配存储空间。在LVM管理下的存储卷可以按需要随时改变大小与移除(可能需对文件系统工具进行升级)。LVM也允许按用户组对存储卷进行管理,允许管理员用更直观的名称(如"sales'、'development')代替物理磁盘名(如'sda'、'sdb')来标识存储卷
4、LVM功能实际是通过内核中的dm模块(device mapper)实现,它将一个或多个底层块设备组织成一个逻辑设备的模块,在/dev/目录下以dm-#形式展现
5、只要是块设备都可以用于创建LVM2。注意分区时ID号要是8e
- 物理存储介质(The physical media):指系统的存储设备--硬盘,如:/dev/hda1、/dev/sda等等,是存储系统最低层的存储单元
- 物理卷PV(physical volume):物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数
- 卷组VG(volume group):在较低的逻辑层从多个PV中抽象出来的卷组,由一个或多个物理卷组成
- PE(physical extend):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB
- 逻辑卷LV(logical volume):由多个LV“块”组成可供挂载使用的设备文件
二、LVM的管理命令
常用的LVM管理命令
功能 | PV管理命令 | VG管理命令 | LV管理命令 |
---|---|---|---|
Scan(扫描) | pvscan | vgscan | Ivscan |
Create (建立) | pvcreate | vgcreate | Ivcreate |
Display (显示) | pvdisplay | vgdisplay | Ivdisplay |
Remove (移除) | pvremove | vgremove | Ivremove |
Extend (扩展) | vgextend | Ivextend | |
Reduce (减少) | vgreduce | Ivreduce |
1、创建分区
创建三个主分区sdb1、sdb2、sdb3,每个磁盘的空间大小为 20GB
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-125829119, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-125829119, default 125829119): +20G
Created a new partition 1 of type 'Linux' and of size 20 GiB.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (2-4, default 2):
First sector (41945088-125829119, default 41945088):
Last sector, +sectors or +size{K,M,G,T,P} (41945088-125829119, default 125829119): +20G
Created a new partition 2 of type 'Linux' and of size 20 GiB.
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (3,4, default 3):
First sector (83888128-125829119, default 83888128):
Last sector, +sectors or +size{K,M,G,T,P} (83888128-125829119, default 125829119): +20G
Value out of range.
Last sector, +sectors or +size{K,M,G,T,P} (83888128-125829119, default 125829119):
Created a new partition 3 of type 'Linux' and of size 20 GiB.
将分区类型的ID标记号改为 8e,磁盘类型的ID为 8e 表示该分区是Linux LVM类型的,支持LVM机制
Command (m for help): t
Partition number (1-3, default 3): 1
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): t
Partition number (1-3, default 3): 2
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): t
Partition number (1-3, default 3): 3
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): p
Disk /dev/sdb: 60 GiB, 64424509440 bytes, 125829120 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
Disklabel type: dos
Disk identifier: 0xd80c792b
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 41945087 41943040 20G 8e Linux LVM
/dev/sdb2 41945088 83888127 41943040 20G 8e Linux LVM
/dev/sdb3 83888128 125829119 41940992 20G 8e Linux LVM
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
1、创建PV
配置物理磁盘
# fdisk -l
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: dos
Disk identifier: 0x0009b7f3
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 83886079 81786880 39G 8e Linux LVM
Disk /dev/sdb: 60 GiB, 64424509440 bytes, 125829120 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
Disklabel type: dos
Disk identifier: 0xd80c792b
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 41945087 41943040 20G 8e Linux LVM
/dev/sdb2 41945088 83888127 41943040 20G 8e Linux LVM
/dev/sdb3 83888128 125829119 41940992 20G 8e Linux LVM
Disk /dev/mapper/centos-root: 35.1 GiB, 37706792960 bytes, 73646080 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: 3.9 GiB, 4160749568 bytes, 8126464 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
1、pvcreate命令
用于将分区或整个硬盘转换成物理卷,主要是添加LVM属性信息并划分PE存储单位
pvcreate 设备名1 设备名2 (创建)
# pvcreate /dev/sdb{1..3} # { .. }意思为1-3
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdb2" successfully created.
Physical volume "/dev/sdb3" successfully created.
2、pvscan命令
用于扫描系统中所有的物理卷,并输出相关信息
# pvscan
PV /dev/sda2 VG centos lvm2 [<39.00 GiB / 4.00 MiB free]
PV /dev/sdb1 lvm2 [20.00 GiB]
PV /dev/sdb3 lvm2 [<20.00 GiB]
PV /dev/sdb2 lvm2 [20.00 GiB]
Total: 4 [<99.00 GiB] / in use: 1 [<39.00 GiB] / in no VG: 3 [<60.00 GiB]
最后一行显示为整体 PV 的量 / 已经被使用到 VG 的 PV 量 / 剩余的 PV 量
3、pvdisplay命令
显示物理卷的详细信息,需要使用指定的物理卷作为命令参数,默认时将显示所有物理卷的信息
# pvdisplay /dev/sdb1
"/dev/sdb1" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1 # 实际的partition
VG Name # 所在VG组,这还没有分配
PV Size 20.00 GiB # 容量大小
Allocatable NO # 是否被分配
PE Size 0 # PE大小
Total PE 0 # PE数量
Free PE 0 # 没被LV用到的PE
Allocated PE 0 # 尚可分配出去的PE
PV UUID TcxxHf-2Rpg-YDQL-kXz0-VP1A-Ufsc-1ZBFpO
4、pvremove命令
用于将物理卷还原成普通分区或磁盘,不再用于LVM体系,被移除的物理卷将无法被pvscan识别
# pvremove /dev/sdb3
Labels on physical volume "/dev/sdb3" successfully wiped.
# pvscan
PV /dev/sda2 VG centos lvm2 [<39.00 GiB / 4.00 MiB free]
PV /dev/sdb1 lvm2 [20.00 GiB]
PV /dev/sdb2 lvm2 [20.00 GiB]
Total: 3 [<79.00 GiB] / in use: 1 [<39.00 GiB] / in no VG: 2 [40.00 GiB]
2、创建VG
1、vgcreate命令
用于将一个或多个物理卷创建为一个卷组,第一个命令参数用于设置新卷组的名称,其后依次指定需要加入该卷组的物理卷作为参数
vgcreate 卷组名 物理卷名1 物理卷名2 (创建)
将刚创建的3个PV中创建成一个VG
# vgcreate vgadmin /dev/sdb{1..3}
Volume group "vgadmin" successfully created
2、vgscan命令
用于扫描系统中已建立的LVM卷组及相关信息
# vgscan
Reading volume groups from cache.
Found volume group "vgadmin" using metadata type lvm2
Found volume group "centos" using metadata type lvm2
3、vgdisplay命令
用于显示系统中各卷组的详细信息,需要使用指定卷组名作为命令参数(未指定卷组时将显示所有卷组的信息
# vgdisplay vgadmin
--- Volume group ---
VG Name vgadmin
System ID
Format lvm2
Metadata Areas 3
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 3
Act PV 3
VG Size <59.99 GiB # 整个VG容量
PE Size 4.00 MiB # 内部每个PE大小
Total PE 15357 # 总共PE数量
Alloc PE / Size 0 / 0
Free PE / Size 15357 / <59.99 GiB # 可配置给LV的PE数量/总容量
VG UUID Z3XApa-ktL0-wdgd-Ot2R-2xRF-CyeL-vdoMeH
4、vgremove命令
用于删除指定的卷组,将指定卷组名称作为参数即可
# vgremove vgadmin
3、创建LV
1、Ivcreate命令
用于从指定的卷组中分割空间,以创建新的逻辑卷
lvcreate -L 容量大小 -n 逻辑卷名 卷组名 (创建)
# lvcreate -L 2G -n lvadmin vgadmin
Logical volume "lvadmin" created.
# ls /dev/vgadmin/lvadmin # 逻辑卷lvadmin的链接文件
/dev/vgadmin/lvadmin
# ls /dev/mapper/vgadmin-lvadmin # 逻辑卷lvadmin的设备文件
/dev/mapper/vgadmin-lvadmin
2、Ivscan命令
用于扫描系统中已建立的逻辑卷及相关信息
# lvscan
ACTIVE '/dev/vgadmin/lvadmin' [2.00 GiB] inherit
ACTIVE '/dev/centos/swap' [<3.88 GiB] inherit
ACTIVE '/dev/centos/root' [<35.12 GiB] inherit
3、Ivdispla命令
用于显示逻辑卷的详细信息,可以指定逻辑卷的设备文件作为参数,也可以使用卷组名作为参数,以显示该卷组中所有逻辑卷的信息
# lvdisplay /dev/vgadmin/lvadmin
--- Logical volume ---
LV Path /dev/vgadmin/lvadmin
LV Name lvadmin
VG Name vgadmin
LV UUID kFmos6-Oby6-MF4c-aq4a-7DXZ-qZnN-46LzIM
LV Write Access read/write
LV Creation host, time mysql01, 2024-01-20 23:43:28 +0800
LV Status available
# open 0
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
5、Ivremove命令
用于删除指定的逻辑卷,直接使用逻辑卷的设备文件作为参数即可
lvremove /dev/vgadmin/lvadmin
4、格式化
# mkfs.xfs /dev/vgadmin/lvadmin # LV的名称必须使用全名
meta-data=/dev/vgadmin/lvadmin isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, 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
5、挂载
# mkdir /mnt/admin
# mount /dev/vgadmin/lvadmin /mnt/admin/ # 临时挂载
# df -hT /mnt/admin/
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vgadmin-lvadmin xfs 2.0G 33M 2.0G 2% /mnt/admin
# vim /etc/fstab # 开机自动挂载
/dev/vgadmin/lvadmin /mnt/admin/ xfs defaults 0 0
# mount -a # 将/etc/fstab的所有内容重新加载
6、LVM扩展
将/dev/sdb5转换成PV卷
# pvcreate /dev/sdb5
Physical volume "/dev/sdb5" successfully created.
1、vgextend命令
用于扩展卷组的磁盘空间
vgextend 卷组名 物理卷名 (扩展vg)
# vgextend vgadmin /dev/sdb5 # 添加到vgadmin中
Volume group "vgadmin" successfully extended
2、Ivextend命令
用于动态扩展逻辑卷的空间
lvextend -L +大小 /dev/卷组名/逻辑卷名 (扩展lv)
# lvextend -L +1G /dev/vgadmin/lvadmin
Size of logical volume vgadmin/lvadmin changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents).
Logical volume vgadmin/lvadmin successfully resized.
更新文件系统大小
# xfs_growfs /dev/vgadmin/lvadmin
meta-data=/dev/mapper/vgadmin-lvadmin isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=524288, 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 524288 to 786432
# df -hT /mnt/admin/
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/vgadmin-lvadmin xfs 3.0G 33M 3.0G 2% /mnt/admin
三、使用SSM工具
1、查看磁盘信息
# yum -y install system-storage-manager
# ssm list dev //列出设备信息
-----------------------------------------------------------------
Device Free Used Total Pool Mount point
-----------------------------------------------------------------
/dev/sda 20.00 GB
/dev/sda1 1.00 GB /boot
/dev/sda2 0.00 KB 19.00 GB 19.00 GB centos
/dev/sdb 20.00 GB
/dev/sdb1 0.00 KB 1020.00 MB 1.00 GB vgadmin
/dev/sdb2 0.00 KB 1020.00 MB 1.00 GB vgadmin
/dev/sdb3 1012.00 MB 8.00 MB 1.00 GB vgadmin
/dev/sdb4 1020.00 MB 0.00 KB 1.00 GB vgadmin
-----------------------------------------------------------------
# ssm list pool //存储池信息
---------------------------------------------------
Pool Type Devices Free Used Total
---------------------------------------------------
centos lvm 1 0.00 KB 19.00 GB 19.00 GB
vgadmin lvm 4 1.98 GB 2.00 GB 3.98 GB
---------------------------------------------------
2、快速创建存储
格式:ssm create -s lv大小 -n lv名称 --fstype lv文件系统类型 -p 卷组名 设备 挂载点
# mkdir /backup
# ssm create -s 1G -n lvadmin --fstype xfs -p vgadmin /dev/sdb[1-4] /backup
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdb2" successfully created.
Physical volume "/dev/sdb3" successfully created.
Physical volume "/dev/sdb4" successfully created.
Volume group "vgadmin" successfully created
Logical volume "lvadmin" created.
meta-data=/dev/vgadmin/lvadmin isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=262144, 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
# df -hT /backup
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/vgadmin-lvadmin xfs 1014M 33M 982M 4% /backup
评论区