Linux LVM逻辑卷管理
Posted llife
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux LVM逻辑卷管理相关的知识,希望对你有一定的参考价值。
使用Linux
好久了,一定会意识到一个问题,某个分区容量不够用了,想要扩容怎么办?这里就涉及到LVM
逻辑卷的管理了,可以动态调整Linux
分区容量。
LVM 概述
全称Logical Volume Manager
,可以动态调整磁盘容量,提高磁盘管理灵活性。
在安装CentOS 7
的过程中选择自动分区时,默认就是以LVM
的方案安装的系统。
但是/boot
分区必须独立出来,不能基于LVM
创建。
PV 物理卷
物理卷,Physical Volume
,是LVM
机制的基本存储设备,通常对应一个普通分区或是整个硬盘。
创建物理卷时,会在分区或磁盘头部创建一个用于记录LVM
属性的保留区块,并把存储空间分割成默认大小为4MB
的基本单元(Physical Extend,PE),从而构成物理卷。
普通分区先转换分区类型为8e
;整块硬盘,可以将所有的空间划分为一个主分区再做调整。
VG 卷组
卷组,Volume Group
,是由一个或多个物理卷组成的一个整体。可以动态添加、移除物理卷,创建时可以指定PE
大小。
LV 逻辑卷
逻辑卷,Logical Volume
,建立在卷组之上,与物理卷没有直接关系。格式化后,即可挂载使用。
三者关系
通过以上对三者的解释可以看出,建立LVM
的过程。首先,将普通分区或整个硬盘创建为物理卷;然后,将一个或多个物理卷创建为卷组;最后,在卷组上分割不同的数据存储空间形成逻辑卷。有了逻辑卷,就可以格式化、挂载使用了。
LVM 管理
LVM 常用命令
功能 | PV 管理命令 | VG 管理命令 | LV 管理命令 |
---|---|---|---|
Scan(扫描) | pvscan | vgscan | lvscan |
Create(建立) | pvcreate | vgcreate | lvcreate |
Display(显示) | pvdisplay | vgdisplay | lvdisplay |
Remove(移除) | pvremove | vgremove | lvremove |
Extend(扩展) | / | vgextend | lvextend |
Reduce(减少) | / | vgreduce | lvreduce |
PV 管理
pvsacn
- 扫描列出系统中所有的物理卷
-e
:仅显示属于输出卷组的物理卷
-n
:仅显示不属于任何卷组的物理卷
[root@localhost ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<19.00 GiB / 0 free]
PV /dev/sdb1 lvm2 [<20.00 GiB]
PV /dev/sdc1 lvm2 [<20.00 GiB]
Total: 3 [58.99 GiB] / in use: 1 [<19.00 GiB] / in no VG: 2 [<40.00 GiB]
pvcreate
- 将分区或整个硬盘转换成物理卷
[root@localhost ~]# pvcreate /dev/sdb,c1
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdc1" successfully created.
pvdisplay
- 显示物理卷的详细信息
[root@localhost ~]# 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 E4eovQ-CgPo-OSDK-75MO-BfD8-pGsP-BUlqip
"/dev/sdb1" is a new physical volume of "<20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID gR5mm3-f50o-XLTq-34GY-n6Iz-WXkQ-uMZAkJ
"/dev/sdc1" is a new physical volume of "<20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size <20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID O2gKxa-ONts-kyXK-zZrO-3dDg-FhtI-V6GPy9
pvremove
- 删除指定的物理卷,还原成普通分区或磁盘
TIPS:只有不属于卷组的物理卷才可以删除还原。
[root@localhost ~]# pvremove /dev/sdd1
Labels on physical volume "/dev/sdd1" successfully wiped.
VG 管理
vgscan
- 扫描系统中已建立的
LVM
卷组
[root@localhost ~]# vgscan
Reading volume groups from cache.
Found volume group "class" using metadata type lvm2
Found volume group "centos" using metadata type lvm2
vgcreate
- 将一个或多个物理卷创建为一个卷组
-l
:卷组上允许创建的最大逻辑卷数
-p
:卷组中允许添加的最大物理卷数
-s
:卷组上的物理卷的PE
大小
[root@localhost ~]# vgcreate class /dev/sdb,c1
Volume group "class" successfully created
vgdisplay
- 显示系统中各卷组的详细信息
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name class
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 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 0 / 0
Free PE / Size 10238 / 39.99 GiB
VG UUID ZtQvGX-YOdy-U8wj-mGQs-TT1e-1PBR-dBNKAG
--- 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.00 GiB
PE Size 4.00 MiB
Total PE 4863
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 0 / 0
VG UUID bLPJGb-SQQ3-KcUb-OSdo-qaMR-9hJE-MTkc7a
vgremove
- 删除指定的卷组
TIPS:卷组下面的逻辑卷没有被挂载使用,卷组删除时会一并删除下面的逻辑卷。
[root@localhost ~]# vgremove class
Volume group "class" successfully removed
vgextend
- 添加新的物理卷,动态扩展卷组的磁盘空间
[root@localhost ~]# vgextend class /dev/sdd1
Volume group "class" successfully extended
vgreduce
- 删除卷组中的物理卷,减少卷组容量,
-a
:如果命令行中没有指定要删除的物理卷,则删除所有的空物理卷。(all)
TIPS:删除啊前确保对应的物理卷没有在使用,就是下面没有逻辑卷,否则删除不了。
[root@localhost ~]# vgreduce class /dev/sdd1
Removed "/dev/sdd1" from volume group "class"
[root@localhost ~]# vgreduce -a class
Physical volume "/dev/sdb1" still in use
Physical volume "/dev/sdc1" still in use
Removed "/dev/sdd1" from volume group "class"
LV 管理
lvscan
- 扫描系统中已建立的
LVM
逻辑卷
[root@localhost ~]# lvscan
ACTIVE '/dev/class/stu01' [25.00 GiB] inherit
ACTIVE '/dev/class/stu02' [14.99 GiB] inherit
ACTIVE '/dev/centos/swap' [2.00 GiB] inherit
ACTIVE '/dev/centos/root' [<17.00 GiB] inherit
lvcreate
- 从指定的卷组中分割空间,创建新的逻辑卷
-L
:指定逻辑卷的大小,单位为KMGT
-l
:指定百分比创建,如50%vg
卷组的一半空间、100%free
所有剩下的。
-n
:指定逻辑卷的名称
[root@localhost ~]# lvcreate -L 25G -n stu01 class
Logical volume "stu01" created.
[root@localhost ~]# lvcreate -l 100%free -n stu02 class
Logical volume "stu02" created.
lvdisplay
- 显示逻辑卷的详细信息
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/class/stu01
LV Name stu01
VG Name class
LV UUID XUP7dq-IM9f-PdKk-XpDH-J1hx-kkuB-cOu3Qx
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-08-24 09:42:35 +0800
LV Status available
# open 0
LV Size 25.00 GiB
Current LE 6400
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/class/stu02
LV Name stu02
VG Name class
LV UUID P7iMC1-X0oT-6TCH-vLYj-HW2j-6QMu-rS7Jyp
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-08-24 09:59:30 +0800
LV Status available
# open 0
LV Size 14.99 GiB
Current LE 3838
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID NXiaXT-T6yv-ktM2-plyS-j6Hg-hkvu-1JQ2eL
LV Write Access read/write
LV Creation host, time localhost, 2019-08-16 17:12:19 +0800
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 7FnsfT-B8xi-3vgu-8N0k-nzNn-8Rra-eoE4iC
LV Write Access read/write
LV Creation host, time localhost, 2019-08-16 17:12:19 +0800
LV Status available
# open 1
LV Size <17.00 GiB
Current LE 4351
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
lvremove
- 删除指定的逻辑卷
TIPS:备份数据,解挂载,删除。
[root@localhost ~]# umount /mnt/stu02/
[root@localhost ~]# lvremove /dev/class/stu02
lvextend
- 动态扩展
LVM
逻辑卷的空间大小
-L
:指定扩展逻辑卷的大小,单位为KMGT
-l
:指定百分比扩建,如100%free
所有剩下的。
[root@localhost ~]# lvextend -L +5G /dev/class/stu02
Size of logical volume class/stu02 changed from 14.99 GiB (3838 extents) to 19.99 GiB (5118 extents).
Logical volume class/stu02 successfully resized.
TIPS:在线调整大小,xfs
使用xfs_growfs
,ext3
、ext4
使用resize2fs
。
[root@localhost ~]# xfs_growfs /dev/class/stu02
meta-data=/dev/mapper/class-stu02 isize=512 agcount=4, agsize=982528 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=3930112, 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 3930112 to 5240832
lvreduce
- 减少
LVM
逻辑卷的空间大小
TIPS:备份数据,解挂载,减少空间,强制格式化,重新挂载。
-L
:指定逻辑卷的大小,单位为KMGT
[root@localhost ~]# umount /mnt/stu02/
[root@localhost ~]# lvreduce -L -10G /dev/class/stu02
[root@localhost ~]# mkfs.xfs -f /dev/class/stu02
[root@localhost ~]# mount -a
[root@localhost ~]# df -hT
LVM 实验
创建 LVM 逻辑卷
1.添加两块20G
的磁盘,重启机器,并对其进行分区,全部空间分为一个主分区,类型改为Linux LVM
,ID为8e
。分区过程省略,不清楚可参考上一篇博客。
[root@localhost ~]# fdisk -l
Disk /dev/sda: 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: 0x0009e645
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
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: 0x5e081577
Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 8e Linux LVM
Disk /dev/sdc: 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: 0x25c06655
Device Boot Start End Blocks Id System
/dev/sdc1 2048 41943039 20970496 8e Linux LVM
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
2.将/dev/sdb1
、/dev/sdc1
转换为物理卷。
[root@localhost ~]# pvcreate /dev/sdb,c1
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdc1" successfully created.
3.创建名称为class
的卷组,并将sdb1
和sdc1
添加进卷组。
[root@localhost ~]# vgcreate class /dev/sdb,c1
Volume group "class" successfully created
4.划分大小为25G
,名称为stu01
的逻辑卷;剩下的空间创建名称为stu02
的逻辑卷。
[root@localhost ~]# lvcreate -L 25G -n stu01 class
Logical volume "stu01" created.
[root@localhost ~]# lvcreate -l 100%free -n stu02 class
Logical volume "stu02" created.
5.格式化逻辑卷,设置自动挂载,挂载后就可以使用了。
[root@localhost ~]# mkfs.xfs /dev/class/stu01
meta-data=/dev/class/stu01 isize=512 agcount=4, agsize=1638400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=6553600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=3200, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mkfs.xfs /dev/class/stu02
meta-data=/dev/class/stu02 isize=512 agcount=4, agsize=982528 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=3930112, 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
[root@localhost ~]# mkdir /mnt/stu01,2
[root@localhost ~]# vim /etc/fstab
//最后面添加下面两行内容
/dev/class/stu01 /mnt/stu01 xfs defaults 0 0
/dev/class/stu02 /mnt/stu02 xfs defaults 0 0
[root@localhost ~]# mount -a
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 17G 1.1G 16G 7% /
devtmpfs devtmpfs 901M 0 901M 0% /dev
tmpfs tmpfs 912M 0 912M 0% /dev/shm
tmpfs tmpfs 912M 8.7M 904M 1% /run
tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 143M 872M 15% /boot
tmpfs tmpfs 183M 0 183M 0% /run/user/0
/dev/mapper/class-stu01 xfs 25G 33M 25G 1% /mnt/stu01
/dev/mapper/class-stu02 xfs 15G 33M 15G 1% /mnt/stu02
扩容 LVM 逻辑卷
1.再添加一块20G的磁盘,重启机器,并对其进行分区,全部空间分为一个主分区,类型改为Linux LVM
,ID为8e
。
[root@localhost ~]# fdisk /dev/sdd
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 0xfaadbaba.
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 +sizeK,M,G (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdd: 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: 0xfaadbaba
Device Boot Start End Blocks Id System
/dev/sdd1 2048 41943039 20970496 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
2.将/dev/sdd1
转换为物理卷。
[root@localhost ~]# pvcreate /dev/sdd1
Physical volume "/dev/sdd1" successfully created.
3.若是卷组剩余空间不够,就要先对class
卷组进行扩容。
[root@localhost ~]# vgdisplay class
--- Volume group ---
VG Name class
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 10238 / 39.99 GiB
Free PE / Size 0 / 0
VG UUID iB1h9h-2uQa-rD7o-GBVK-pOCA-ek2c-TXieLr
[root@localhost ~]# vgextend class /dev/sdd1
Volume group "class" successfully extended
[root@localhost ~]# vgdisplay class
--- Volume group ---
VG Name class
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 7
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size <59.99 GiB
PE Size 4.00 MiB
Total PE 15357
Alloc PE / Size 10238 / 39.99 GiB
Free PE / Size 5119 / <20.00 GiB
VG UUID iB1h9h-2uQa-rD7o-GBVK-pOCA-ek2c-TXieLr
4.对stu02
扩容5G
,使用xfs_growfs
在线调整大小。
[root@localhost ~]# lvextend -L +5G /dev/class/stu02
Size of logical volume class/stu02 changed from 14.99 GiB (3838 extents) to 19.99 GiB (5118 extents).
Logical volume class/stu02 successfully resized.
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 17G 1.1G 16G 7% /
devtmpfs devtmpfs 901M 0 901M 0% /dev
tmpfs tmpfs 912M 0 912M 0% /dev/shm
tmpfs tmpfs 912M 8.7M 903M 1% /run
tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 143M 872M 15% /boot
/dev/mapper/class-stu01 xfs 25G 33M 25G 1% /mnt/stu01
/dev/mapper/class-stu02 xfs 15G 33M 15G 1% /mnt/stu02
tmpfs tmpfs 183M 0 183M 0% /run/user/0
[root@localhost ~]# xfs_growfs /dev/class/stu02
meta-data=/dev/mapper/class-stu02 isize=512 agcount=4, agsize=982528 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=3930112, 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 3930112 to 5240832
[root@localhost ~]# df -hT | grep stu02
/dev/mapper/class-stu02 xfs 20G 33M 20G 1% /mnt/stu02
总结
内容不多,但是还要实际操作才能了解。就是三步走,创建物理卷,创建卷组,创建逻辑卷,然后格式化并挂载使用。扩容的话,没有挂载使用,就正常扩,然后格式化挂载,已经挂载使用了,扩容后使用xfs_growfs
在线调整生效。还有下一篇关于磁盘配额。
以上是关于Linux LVM逻辑卷管理的主要内容,如果未能解决你的问题,请参考以下文章