CentOS8提高篇9:LVM(逻辑卷管理)
Posted 北方的流星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS8提高篇9:LVM(逻辑卷管理)相关的知识,希望对你有一定的参考价值。
一、LVM概念
LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。
LVM的工作原理其实很简单,它就是通过将底层的物理硬盘抽象的封装起来,然后以逻辑卷的方式呈现给上层应用。在传统的磁盘管理机制中,我们的上层应用是直接访问文件系统,从而对底层的物理硬盘进行读取,而在LVM中,其通过对底层的硬盘进行封装,当我们对底层的物理硬盘进行操作时,其不再是针对于分区进行操作,而是通过一个叫做逻辑卷的东西来对其进行底层的磁盘管理操作。比如说我增加一个物理硬盘,这个时候上层的服务是感觉不到的,因为呈现给上层服务的是以逻辑卷的方式。
LVM最大的特点就是可以对磁盘进行动态管理。因为逻辑卷的大小是可以动态调整的,而且不会丢失现有的数据。如果我们新增加了硬盘,其也不会改变现有上层的逻辑卷。作为一个动态磁盘管理机制,逻辑卷技术大大提高了磁盘管理的灵活性。
二、LVM术语
PV(Physical Volume)- 物理卷
物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘。
VG(Volumne Group)-卷组
卷组建立在物理卷之上,一个卷组中至少要包括一个物理卷,在卷组建立之后可动态添加物理卷到卷组中。一个逻辑卷管理系统工程中可以只有一个卷组,也可以拥有多个卷组。
LV(Logical Volume)- 逻辑卷
逻辑卷建立在卷组之上,卷组中的未分配空间可以用于建立新的逻辑卷,逻辑卷建立后可以动态地扩展和缩小空间。系统中的多个逻辑卷可以属于同一个卷组,也可以属于不同的多个卷组
PE(Physical Extent):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
LE(Logical Extent):逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
如下图所示PV、VG、LV三者关系:
三、常用命令
PV:Physical(物理卷)
pvcreate、pvs 、pvdisplay 、pvremove、pvmove、pvscan
VG:Volume Group(卷组)
vgcreate、vgs、vgdisplay、vgremove、vgrename、vgreduce、vgextent、vgscan
LV:logical Volume(逻辑卷)
lvcreate、lvs、lvdisplay、lvremove、lvextend、lvresize、lvscan、lvrename
四、环境准备
1、虚拟机新增两块磁盘
ESXI线添加虚拟磁盘,fdisk -l没发现,一般情况下重启服务器就可以。但是不能影响业务,为什么新增磁盘或磁盘扩容后,Linux系统识别不到呢?因为连接存储设备的SCSI总线需要重新扫描,才能识别到这些新的存储设备
[root@Node1 ~]# ls /sys/class/scsi_host/ //确定主机总线SCSI总线号
host0 host1 host2
[root@Node1 ~]# echo "- - -" > /sys/class/scsi_host/host0/scan //重新扫描 SCSI总线添加磁盘
[root@Node1 ~]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@Node1 ~]# echo "- - -" > /sys/class/scsi_host/host2/scan
[root@Node1 ~]# fdisk -l | grep "^Disk /dev" //检查硬盘是否在线
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 sectors
[root@Node1 ~]#
2、用fidsk分别对/dev/sdb与/dev/sdc进行分区
[root@Node1 ~]# fdisk /dev/sdb //对/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 0xafcc8a41.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p //主分区
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +sizeK,M,G (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 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): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (20973568-41943039, default 20973568):
Using default value 20973568
Last sector, +sectors or +sizeK,M,G (20973568-41943039, default 41943039):
Using default value 41943039
Partition 2 of type Linux and of size 10 GiB is set
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
3、partprobe让内核重新识别分区表
[root@Node1 ~]#partprobe /dev/sdb,c
[root@Node1 ~]# partx -a /dev/sdc
[root@Node1 ~]# partx -s /dev/sdc
[root@Node1 ~]# cat /proc/partitions //查看分区信息表
major minor #blocks name
8 0 52428800 sda
8 1 204800 sda1
8 2 20971520 sda2
8 3 20971520 sda3
8 4 0 sda4
8 5 5242880 sda5
8 6 2097152 sda6
8 7 2936832 sda7
8 16 20971520 sdb
8 17 10485760 sdb1
8 18 10484736 sdb2
8 32 31457280 sdc
8 33 1 sdc1
8 37 20971520 sdc5
8 38 10482688 sdc6
11 0 4415488 sr0
[root@Node1 ~]# ls /dev/sdb* //查看/dev/sdb分区情况
/dev/sdb /dev/sdb1 /dev/sdb2
[root@Node1 ~]# ls /dev/sdc* //查看/dev/sdc分区情况
/dev/sdc /dev/sdc1 /dev/sdc5 /dev/sdc6
4、验证磁盘分区结果
[root@Node1 ~]# fdisk -l | grep "LVM$"
/dev/sdb1 2048 20973567 10485760 8e Linux LVM
/dev/sdb2 20973568 41943039 10484736 8e Linux LVM
/dev/sdc5 4096 41947135 20971520 8e Linux LVM
/dev/sdc6 41949184 62914559 10482688 8e Linux LVM
四、LVM常用操作命令
1、安装LVM
[root@Node1 ~]# cat /etc/system-release //查看系统版本
CentOS Linux release 7.6.1810 (Core)
[root@Node1 ~]# yum -y install lvm2 //安装LVM包
[root@Node1 ~]# rpm -qa | grep lvm
llvm-private-6.0.1-2.el7.x86_64
lvm2-2.02.180-10.el7_6.8.x86_64
lvm2-libs-2.02.180-10.el7_6.8.x86_64
2、PV物理卷常用操作
pvcreate /dev/DEVICE: 创建PV
pvs:简要PV信息显示
pvdisplay:显示PV的详细信息
pvremove /dev/DEVICE: 移除PV
pvscan: 扫描系统中连接的所有硬盘,列出找到的物理卷列表
[root@Node1 ~]# pvcreate /dev/sdb1,2 //创建2个物理卷pv
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdb2" successfully created.
[root@Node1 ~]# pvs //查看物理卷摘要信息
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 10.00g 10.00g
/dev/sdb2 lvm2 --- <10.00g <10.00g
[root@Node1 ~]# pvscan //搜索已存在的物理卷
PV /dev/sdb2 lvm2 [<10.00 GiB]
PV /dev/sdb1 lvm2 [10.00 GiB]
Total: 2 [<20.00 GiB] / in use: 0 [0 ] / in no VG: 2 [<20.00 GiB]
[root@Node1 ~]# pvremove /dev/sdb2 //移除/dev/sdb2
Labels on physical volume "/dev/sdb2" successfully wiped.
[root@Node1 ~]# pvdisplay /dev/sdb1 //显示详细的物理卷信息
"/dev/sdb1" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID k6YMJN-BfTI-DL8G-qjEr-onjT-CmDK-IzC9vB
[root@Node1 ~]# pvcreate /dev/sdb2 // 将/dev/sdb2加入物理卷
Physical volume "/dev/sdb2" successfully created.
[root@Node1 ~]# pvs -o +pv_uuid
PV VG Fmt Attr PSize PFree PV UUID
/dev/sdb1 datavg lvm2 a-- <10.00g <10.00g GX3Clm-pGDv-4xtw-VIr3-x00m-vIlD-zPOooJ
/dev/sdb2 datavg lvm2 a-- <10.00g <10.00g zuMtAt-6S6z-3vDa-xnj2-WADw-hk0s-H9V1Wi
/dev/sdc5 datavg lvm2 a-- <10.00g <10.00g orxgvl-GKbn-LMN0-YOUY-mIGA-TqJp-g1V8iv
[root@Node1 ~]# pvs -v
3、VG常用操作
vgcreate /dev/DEVICE: 创建VG卷组
vgs: 简要VG信息显示
vgextend:动态扩展LVM卷组,它通过向卷组中添加物理卷来增加卷组的容量
vgreduce:通过删除LVM卷组中的物理卷来减少卷组容量,不能删除LVM卷组中剩余的最后一个物理卷
vgdisplay:显示VG的详细信息
vgscan:查找系统中存在的LVM卷组,并显示找到的卷组列表
vgremove:删除卷组,其上的逻辑卷必须处于离线状态
[root@Node1 ~]# vgcreate datavg /dev/sdb1,2 //创建datavg卷组
Volume group "datavg" successfully created
[root@Node1 ~]# vgdisplay datavg //显示datavg详细信息,也可以使用vgdisplay -vv或vgdisplay -vvv命令 --- Volume group ---
VG Name datavg
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 19.99 GiB
PE Size 4.00 MiB
Total PE 5118
Alloc PE / Size 0 / 0
Free PE / Size 5118 / 19.99 GiB
VG UUID thDWMH-ZXq6-zJP5-LtTn-xmQh-OpG5-b3qrUu
[root@Node1 ~]# vgs //显示VG的简要信息
VG #PV #LV #SN Attr VSize VFree
datavg 2 0 0 wz--n- 19.99g 19.99g
[root@Node1 ~]# vgextend datavg /dev/sdc5 //向datavg卷组中添加物理卷
Physical volume "/dev/sdc5" successfully created.
Volume group "datavg" successfully extended
[root@Node1 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 datavg lvm2 a-- <10.00g <10.00g
/dev/sdb2 datavg lvm2 a-- <10.00g <10.00g
/dev/sdc5 datavg lvm2 a-- <10.00g <10.00g
[root@Node1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
datavg 3 0 0 wz--n- <29.99g <29.99g
[root@Node1 ~]# vgreduce datavg /dev/sdc5 //从datavg卷组中移除物理卷
Removed "/dev/sdc5" from volume group "datavg"
[root@Node1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
datavg 2 0 0 wz--n- 19.99g 19.99g
[root@Node1 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 datavg lvm2 a-- <10.00g <10.00g
/dev/sdb2 datavg lvm2 a-- <10.00g <10.00g
/dev/sdc5 lvm2 --- 20.00g 20.00g
[root@Node1 ~]# vgremove datavg //移除datavg
Volume group "datavg" successfully removed.
[root@Node1 ~]# vgs
[root@Node1 ~]# vgcreate datavg /dev/sdb1,2
Volume group "datavg" successfully created
[root@Node1 ~]# vgdisplay datavg
--- Volume group ---
VG Name datavg
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 19.99 GiB
PE Size 4.00 MiB
Total PE 5118
Alloc PE / Size 0 / 0
Free PE / Size 5118 / 19.99 GiB
VG UUID T8c1W9-xpJF-vvnS-bm5y-gqmY-5RBK-wRkD9Y
[root@Node1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
datavg 2 0 0 wz--n- 19.99g 19.99g
[root@Node1 ~]# vgextend datavg /dev/sdc5
Volume group "datavg" successfully extended
[root@Node1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
datavg 3 0 0 wz--n- <29.99g <29.99g
[root@Node1 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 datavg lvm2 a-- <10.00g <10.00g
/dev/sdb2 datavg lvm2 a-- <10.00g <10.00g
/dev/sdc5 datavg lvm2 a-- <10.00g <10.00g
[root@Node1 ~]# pvscan //扫描系统中连接的所有硬盘,列出找到的物理卷列表
PV /dev/sdb1 VG datavg lvm2 [<10.00 GiB / <10.00 GiB free]
PV /dev/sdb2 VG datavg lvm2 [<10.00 GiB / <10.00 GiB free]
PV /dev/sdc5 VG datavg lvm2 [<10.00 GiB / <10.00 GiB free]
Total: 3 [<29.99 GiB] / in use: 3 [<29.99 GiB] / in no VG: 0 [0 ]
[root@Node1 ~]# vgscan //显示系统中所有卷组
Reading volume groups from cache.
Found volume group "datavg" using metadata type lvm2
[root@Node1 ~]# vgs -o +pv_name
VG #PV #LV #SN Attr VSize VFree PV
datavg 3 0 0 wz--n- <29.99g <29.99g /dev/sdb1
datavg 3 0 0 wz--n- <29.99g <29.99g /dev/sdb2
datavg 3 0 0 wz--n- <29.99g <29.99g /dev/sdc5
[root@Node1 ~]# vgs -v
VG Attr Ext #PV #LV #SN VSize VFree VG UUID VProfile
datavg wz--n- 4.00m 3 0 0 <29.99g <29.99g 2b9qgs-mCb0-Isc2-Af74-5uWo-Yf8Q-4KOmns
4、LV常用操作
lvcreate : 用来创建LVM的逻辑卷
lvcreate -L #[mMgGtT] -n NAME VolumeGroup
用法:lvcreate [选项] [参数]
选项:
-L:指定大小
-l:指定大小(LE数)
-n:指定名称
-s:创建快照
-p r:设置为只读(该选项一般用于创建快照中
lvs : 显示逻辑卷信息
lvscan:扫描当前系统中的所有逻辑卷,及其对应的设备文件
lvdisplay:显示逻辑卷属性
lvextend:可在线扩展逻辑卷空间
lvreduce:缩减逻辑卷空间,一般离线使用
lvremove:删除逻辑卷,需要处于离线(卸载)状态
[root@Node1 ~]# lvcreate -L 5G -n datalv datavg //创建datalv逻辑卷
Logical volume "datalv" created.
[root@Node1 ~]# ls /dev/mapper/
control datavg-datalv
[root@Node1 ~]# mkfs.xfs -L Backup /dev/datavg/datalv //格式化逻辑卷
meta-data=/dev/datavg/datalv isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1310720, 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@Node1 ~]# mkdir /dbbackup
[root@Node1 ~]# mount /dev/mapper/datavg-datalv /dbbackup/ //挂载至/dbbackup目录
[root@Node1 ~]# df -hT | grep datalv //查询datalv的挂载情况
/dev/mapper/datavg-datalv xfs 5G 33M 5G 1% /dbbackup
[root@Node1 ~]# lvs //显示逻辑卷信息
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
datalv datavg -wi-ao---- 5.00g
[root@Node1 ~]# lvscan //扫描系统中的逻辑卷
ACTIVE '/dev/datavg/datalv' [5.00 GiB] inherit
[root@Node1 ~]# lvdisplay //显示逻辑卷详细信息
--- Logical volume ---
LV Path /dev/datavg/datalv
LV Name datalv
VG Name datavg
LV UUID Y1P5zJ-Wy1Q-kERv-VUtC-4vAm-NsMb-3aCy1y
LV Write Access read/write
Centos 6.6配置LVM(逻辑卷管理)
LVM简介
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和 分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。LVM将若干个磁盘分区连接为一个整块的卷组 (volumegroup),形成一个存储池。管理员可以在卷组上随意创建逻辑卷组(logicalvolumes),并进一步在逻辑卷组上创建文件系 统。
与传统的磁盘与分区相比,LVM为计算机提供了更高层次的磁盘存储。它使系统管理员可以更方便的为应用与用户分配存储空间。在LVM管理下的存储卷可以按需要随时改变大小与移除(可能需对文件系统工具进行升级)。 LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。
基本的逻辑卷管理概念:
物理卷 (physical volume)
物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
卷组 (Volume Group)
LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
逻辑卷(logical volume)
LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
PE (physical extent)
每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
LE (logical extent)
逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
如下图所示PV、VG、LV三者关系:
系统环境
系统环境: Centos 6.6 最小化安装
LVM管理工具: lvm2-2.02.143-12.el6.
磁盘分区:挂载/dev/sdb 、/dev/sdc 、/dev/sdd、/dev/sde 、/dev/sdf、 /dev/sdg 共6块磁盘
实验目的:
1、通过LVM管理工具,分别创建pv、vg 和lvm。
2、模拟扩展和缩减LVM,模拟扩展或缩减VG
3、卸载LVM,删除LVM
一、设置磁盘(虚拟机中增加6块20G磁盘),安装LVM 管理工具。
在Vmware 虚拟机中,增加8块磁盘 。
[[email protected] /]# fdisk -l #查看磁盘状态 Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000c6c5f Device Boot Start End Blocks Id System /dev/sda1 * 1 256 2048000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 256 511 2048000 82 Linux swap / Solaris Partition 2 does not end on cylinder boundary. /dev/sda3 511 3060 20480000 83 Linux Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 ..... Disk /dev/sdf: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/sdg: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 [[email protected] ~]# rpm -qa |grep lvm #确认lvm管理工具是否安装 [[email protected] ~]# yum install lvm* -y #安装lvm管理
二、对增加的磁盘进行分区并修改分区类型为8e(Linux LVM)
[[email protected] /]# fdisk /dev/sdb #对硬盘/dev/sdb 进行分区并调整分区类型 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xb9977492. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won‘t be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) and change display units to sectors (command ‘u‘). 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 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) Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): Using default value 2610 Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xb9977492 Device Boot Start End Blocks Id System /dev/sdb1 1 2610 20964793+ 83 Linux Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xb9977492 Device Boot Start End Blocks Id System /dev/sdb1 1 2610 20964793+ 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
说明:#fdisk /dev/sdb 对其他磁盘进行分区;
输入"m", 获取帮助;
输入"p", 查看分区前磁盘状态;
输入“n”,新建磁盘分区;
输入“e”,新建逻辑分区;
输入“p”,新建主分区;
输入“t”,修改分区类型;
输入“8e”,调整为linux LVM 类型 ;
输入“p”,显示分区状态表;
输入“w”,保存新建分区。
以同样的方式,对其他磁盘进行分区。磁盘分区完成后结果如下:
[[email protected] /]# fdisk -l Disk /dev/sdb: 21.5 GB, 21474836480 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 2610 20964793+ 8e Linux LVM Disk /dev/sdc: 21.5 GB, 21474836480 bytes Device Boot Start End Blocks Id System /dev/sdc1 1 2610 20964793+ 8e Linux LVM Disk /dev/sdd: 21.5 GB, 21474836480 bytes Device Boot Start End Blocks Id System /dev/sdd1 1 2610 20964793+ 8e Linux LVM Disk /dev/sde: 21.5 GB, 21474836480 bytes Device Boot Start End Blocks Id System /dev/sde1 1 2610 20964793+ 8e Linux LVM Disk /dev/sdf: 21.5 GB, 21474836480 bytes Device Boot Start End Blocks Id System /dev/sdf1 1 2610 20964793+ 8e Linux LVM Disk /dev/sdg: 21.5 GB, 21474836480 bytes Device Boot Start End Blocks Id System /dev/sdg1 1 2610 20964793+ 8e Linux LVM
三、创建PV 物理卷
[[email protected] /]# pvcreate /dev/sd{b..g}1 #对磁盘分区分别创建PV Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdc1" successfully created Physical volume "/dev/sdd1" successfully created Physical volume "/dev/sde1" successfully created Physical volume "/dev/sdf1" successfully created Physical volume "/dev/sdg1" successfully created [[email protected] dev]# pvdisplay --- Physical volume --- PV Name /dev/sdd1 VG Name myvg2 PV Size 19.99 GiB / not usable 1.43 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 5118 Free PE 0 Allocated PE 5118 PV UUID PJp0SF-3H2d-zfXe-fAve-1JmW-FsSm-itsC2q --- Physical volume --- PV Name /dev/sde1 VG Name myvg2 PV Size 19.99 GiB / not usable 1.43 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 5118 Free PE 0 Allocated PE 5118 PV UUID qbLHW1-NNam-qCPS-zphy-tEzx-Cb39-j2u4Uf --- Physical volume --- PV Name /dev/sdf1 VG Name myvg2 PV Size 19.99 GiB / not usable 1.43 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 5118 Free PE 0 Allocated PE 5118 PV UUID xooWla-DpOK-Jca8-2QKF-j29H-Tsgc-5Lamtv --- Physical volume --- PV Name /dev/sdg1 VG Name myvg2 PV Size 19.99 GiB / not usable 1.43 MiB Allocatable yes PE Size 4.00 MiB Total PE 5118 Free PE 5112 Allocated PE 6 PV UUID OHvLBz-c3Kw-Gq0H-48ty-5Zyg-B1nC-U0b0dg --- Physical volume --- PV Name /dev/sdc1 VG Name myvg1 PV Size 19.99 GiB / not usable 1.43 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 5118 Free PE 0 Allocated PE 5118 PV UUID LxdE2p-ER9f-TZyQ-Kcf3-6pEr-zA3r-HKHgSG --- Physical volume --- PV Name /dev/sdb1 VG Name myvg1 PV Size 19.99 GiB / not usable 1.43 MiB Allocatable yes PE Size 4.00 MiB Total PE 5118 Free PE 2556 Allocated PE 2562 PV UUID 5T4fjJ-lSWx-jny5-chTp-w7Wk-3bTC-iJ6sIt [[email protected] ~]# pvscan #查看PV简要信息 PV /dev/sdb1 lvm2 [19.99 GiB] PV /dev/sdc1 lvm2 [19.99 GiB] PV /dev/sdd1 lvm2 [19.99 GiB] PV /dev/sde1 lvm2 [19.99 GiB] PV /dev/sdf1 lvm2 [19.99 GiB] PV /dev/sdg1 lvm2 [19.99 GiB] Total: 6 [119.96 GiB] / in use: 0 [0 ] / in no VG: 6 [119.96 GiB] [[email protected] ~]# pvs #查看PV简要信息 PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 ---- 19.99g 19.99g /dev/sdc1 lvm2 ---- 19.99g 19.99g /dev/sdd1 lvm2 ---- 19.99g 19.99g /dev/sde1 lvm2 ---- 19.99g 19.99g /dev/sdf1 lvm2 ---- 19.99g 19.99g /dev/sdg1 lvm2 ---- 19.99g 19.99g
说明:#pvcreate /dev/sd{b..g}1 创建pv物理卷;
# pvdisplay | pvs |pvscan 查看pv状态信息
PV Name, 物理卷名;
VG Name, 所属卷组名;
PV Size,物理卷大小;
PV UUID ,PV UUID 号
四、创建myvg1、myvg2 卷组
[[email protected] ~]# vgcreate myvg1 /dev/sdb1 /dev/sdc1 #创建vg卷组myvg1 ,使用物理卷/dev/sdb1 和 /dev/sdc1 Volume group "myvg1" successfully created [[email protected] ~]# vgcreate myvg2 /dev/sd{d..g}1 #创建vg卷组myvg2,使用物理卷/dev/sdd1、/dev/sde1、/dev/sdf1 、/dev/sdg1 Volume group "myvg2" successfully created [[email protected] dev]# vgdisplay --- Volume group --- VG Name myvg2 System ID Format lvm2 Metadata Areas 4 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 4 Act PV 4 VG Size 79.97 GiB PE Size 4.00 MiB Total PE 20472 Alloc PE / Size 15360 / 60.00 GiB Free PE / Size 5112 / 19.97 GiB VG UUID xcT3bS-xjke-tGEI-GL0p-hRe4-3WAs-72355D --- Volume group --- VG Name myvg1 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 8 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 39.98 GiB PE Size 4.00 MiB Total PE 10236 Alloc PE / Size 7680 / 30.00 GiB Free PE / Size 2556 / 9.98 GiB VG UUID rASgft-0OPt-5blH-BACG-oR4b-glCX-7OJZcF [[email protected] ~]# vgscan #查看vg简要信息 Reading all physical volumes. This may take a while... Found volume group "myvg2" using metadata type lvm2 Found volume group "myvg1" using metadata type lvm2 [[email protected] ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg1 2 0 0 wz--n- 39.98g 39.98g myvg2 4 0 0 wz--n- 79.97g 79.97g
说明:#vgcreate myvg1 /dev/sd{b..c}1 创建myvg1卷组;
# vgdisplay | vgs |vgscan 查看vg状态信息
VG Name, 卷组名;
VG Size,卷组Size大小;
PE Size ,PE的大小,默认为4MB
Total PE,PE的总数量
Free PE / Size ,空闲PE的数量及大小
VG UUID ,VG UUID 号
五、创建mylv1、mylv2 逻辑卷, 格式化并挂载
[[email protected] ~]# lvcreate -n mylv1 -L 30G myvg1 Logical volume "mylv1" created. [[email protected] ~]# lvcreate -n mylv2 -L 60G myvg2 Logical volume "mylv2" created. [[email protected] ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1 myvg1 -wi-a----- 30.00g mylv2 myvg2 -wi-a----- 60.00g [[email protected] dev]# lvdisplay --- Logical volume --- LV Path /dev/myvg2/mylv2 LV Name mylv2 VG Name myvg2 LV UUID pKGpwi-1aN4-jrcR-uGcj-D9ZN-7AcV-PN0yue LV Write Access read/write LV Creation host, time study, 2017-09-22 23:10:03 +0800 LV Status available # open 1 LV Size 60.00 GiB Current LE 15360 Segments 4 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 --- Logical volume --- LV Path /dev/myvg1/mylv1 LV Name mylv1 VG Name myvg1 LV UUID uiX2dG-crHV-lO83-qqUq-fBqj-mov6-2KQefT LV Write Access read/write LV Creation host, time study, 2017-09-22 23:09:05 +0800 LV Status available # open 1 LV Size 30.00 GiB Current LE 7680 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 [[email protected] ~]# fdisk -l #查看myvg1-mylv1 、myvg2-mylv2逻辑卷状态。 ... Disk /dev/mapper/myvg1-mylv1: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/myvg2-mylv2: 64.4 GB, 64424509440 bytes 255 heads, 63 sectors/track, 7832 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 [[email protected] dev]# vgs #查看分配完LVM后剩余VG容量 VG #PV #LV #SN Attr VSize VFree myvg1 2 1 0 wz--n- 39.98g 9.98g myvg2 4 1 0 wz--n- 79.97g 19.97g [[email protected] dev]# pvs # 查看分配完LVM后剩余PV容量 PV VG Fmt Attr PSize PFree /dev/sdb1 myvg1 lvm2 a--u 19.99g 9.98g /dev/sdc1 myvg1 lvm2 a--u 19.99g 0 /dev/sdd1 myvg2 lvm2 a--u 19.99g 0 /dev/sde1 myvg2 lvm2 a--u 19.99g 0 /dev/sdf1 myvg2 lvm2 a--u 19.99g 0 /dev/sdg1 myvg2 lvm2 a--u 19.99g 19.97g [[email protected] ~]# cd /dev/mapper/ #逻辑卷磁盘存放于/dev/mapper目录中 [[email protected] mapper]# ls control myvg1-mylv1 myvg2-mylv2 [[email protected] mapper]# mkfs.ext4 myvg1-mylv1 #对LVM卷进行格式化 [[email protected] mapper]# mkfs.ext4 myvg2-mylv2 [[email protected] dev]# mount /dev/mapper/myvg1-mylv1 lvm1 #对LVM卷进行挂载 [[email protected] dev]# mount /dev/mapper/myvg2-mylv2 lvm2 [[email protected] dev]# df -TH #查看磁盘挂载状态 Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 21G 1.2G 19G 7% / tmpfs tmpfs 1.3G 0 1.3G 0% /dev/shm /dev/mapper/myvg1-mylv1 ext4 32G 47M 30G 1% /dev/lvm1 /dev/mapper/myvg2-mylv2 ext4 64G 55M 61G 1% /dev/lvm2
说明:#lvcreate -n mylv1 -L 30G myvg1 从myvg1 卷组中分配30G并创建mylv1逻辑卷;-n 指定LVM名字 ,-L 分配容量大小
# lvdisplay | lvs | lvscan 查看lv状态信息
LV Path ,LV的路径
LV Name, LV的名字VG Name 所属的VG
LV Size , LV的大小
六、扩展或缩减mylv1、mylv2 逻辑卷
LVM最大的好处就是可以对磁盘进行动态管理,而且不会丢失现有的数据。
1、扩展LVM的思路:
需要先扩展逻辑卷;然后扩展文件系统
[[email protected] ~]# lvextend -L +5G /dev/mapper/myvg1-mylv1 #扩展5G空间给myvg1-mylv1 Size of logical volume myvg1/mylv1 changed from 30.00 GiB (7680 extents) to 35.00 GiB (8960 extents). Logical volume mylv1 successfully resized. [[email protected] ~]# lvextend -L +10G /dev/mapper/myvg2-mylv2 #扩展10G空间给myvg2-mylv2 Size of logical volume myvg2/mylv2 changed from 60.00 GiB (15360 extents) to 70.00 GiB (17920 extents). Logical volume mylv2 successfully resized. [[email protected] ~]# resize2fs -p /dev/mapper/myvg1-mylv1 #扩展myvg1-mylv1卷下的文件体统,扩展大小与逻辑卷增加的大小一致 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/myvg1-mylv1 is mounted on /dev/lvm1; on-line resizing required old desc_blocks = 2, new_desc_blocks = 3 Performing an on-line resize of /dev/mapper/myvg1-mylv1 to 9175040 (4k) blocks. The filesystem on /dev/mapper/myvg1-mylv1 is now 9175040 blocks long. [[email protected] ~]# resize2fs -p /dev/mapper/myvg2-mylv2 #扩展myvg2-mylv2卷下的文件体统,扩展大小与逻辑卷增加的大小一致 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/myvg2-mylv2 is mounted on /dev/lvm2; on-line resizing required old desc_blocks = 4, new_desc_blocks = 5 Performing an on-line resize of /dev/mapper/myvg2-mylv2 to 18350080 (4k) blocks. The filesystem on /dev/mapper/myvg2-mylv2 is now 18350080 blocks long. [[email protected] ~]# df -TH #查看磁盘空间,mylv1 及mylv2 均已扩展 Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 21G 1.2G 19G 7% / tmpfs tmpfs 1.3G 0 1.3G 0% /dev/shm /dev/mapper/myvg1-mylv1 ext4 37G 52M 35G 1% /dev/lvm1 /dev/mapper/myvg2-mylv2 ext4 74G 57M 71G 1% /dev/lvm2
说明:#lvextend -L +5G /dev/myvg1-mylv1 扩展5G容量分配到mylv1逻辑卷; -L 分配容量大小 +5G 扩展5G
# resize2fs -p /dev/mapper/myvg1-mylv1 扩展文件系统与逻辑卷大小一致 。-p 表示跟逻辑卷扩展大小保持一致
2、缩减LVM思路:
不能再线缩减,得先卸载;确保缩减后的空间大小依然能存储原有的所有数据;缩减前应该先强行检查文件,以确保文件系统处于一致性状态;缩减文件系统,缩减LVM逻辑卷;重新挂载
[[email protected] ~]# umount /dev/mapper/myvg1-mylv1 #先卸载逻辑卷分区/dev/mapper/mgvg1-mylv1 [[email protected] ~]# umount /dev/mapper/myvg2-mylv2 [[email protected] ~]# e2fsck -f /dev/mapper/myvg1-mylv1 #强行检查文件,以确保文件系统处于一致性状态 e2fsck 1.41.12 (17-May-2010) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/mapper/myvg1-mylv1: 43/2293760 files (0.0% non-contiguous), 189423/9175040 blocks [[email protected] ~]# e2fsck -f /dev/mapper/myvg2-mylv2 e2fsck 1.41.12 (17-May-2010) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/mapper/myvg2-mylv2: 42/4587520 files (0.0% non-contiguous), 334367/18350080 blocks [[email protected] ~]# resize2fs /dev/mapper/myvg1-mylv1 5G #缩减/dev/mapper/myvg1-mylv1 文件系统到5G大小 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/myvg1-mylv1 to 1310720 (4k) blocks. The filesystem on /dev/mapper/myvg1-mylv1 is now 1310720 blocks long. [[email protected] ~]# resize2fs /dev/mapper/myvg2-mylv2 15G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/myvg2-mylv2 to 3932160 (4k) blocks. The filesystem on /dev/mapper/myvg2-mylv2 is now 3932160 blocks long. [[email protected] ~]# lvreduce -L 5G /dev/mapper/myvg1-mylv1 #缩减/dev/mapper/myvg1-mylv1 逻辑卷到5G大小 resize2fs 1.41.12 (17-May-2010) WARNING: Reducing active logical volume to 5.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvg1/mylv1? [y/n]: y Size of logical volume myvg1/mylv1 changed from 10.00 GiB (2560 extents) to 5.00 GiB (1280 extents). Logical volume mylv1 successfully resized. [[email protected] ~]# lvreduce /dev/mapper/myvg2-mylv2 -L 15G WARNING: Reducing active logical volume to 15.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvg2/mylv2? [y/n]: y Size of logical volume myvg2/mylv2 changed from 70.00 GiB (17920 extents) to 15.00 GiB (3840 extents). Logical volume mylv2 successfully resized. [[email protected] ~]# mount /dev/mapper/myvg1-mylv1 /dev/lvm1 #挂载磁盘 [[email protected] ~]# mount /dev/mapper/myvg2-mylv2 /dev/lvm2 [[email protected] ~]# df -TH #查看磁盘空间 Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 21G 1.2G 19G 7% / tmpfs tmpfs 1.3G 0 1.3G 0% /dev/shm /dev/mapper/myvg1-mylv1 ext4 5.2G 36M 4.9G 1% /dev/lvm1 /dev/mapper/myvg2-mylv2 ext4 16G 44M 15G 1% /dev/lvm2
说明:# e2fsck -f /dev/mapper/myvg1-mylv1 强制检查文件系统; -f 强制
# resize2fs /dev/mapper/myvg1-mylv1 5G 缩减文件系统为5G 。
#lvextend -L 5G /dev/myvg1-mylv1 分配到mylv1逻辑卷大小为5G; -L 分配容量大小
七、扩展或缩减myvg1、myvg2 卷组
[[email protected] ~]# vgs #查看vg状态 VG #PV #LV #SN Attr VSize VFree myvg1 2 1 0 wz--n- 39.98g 34.98g myvg2 4 1 0 wz--n- 79.97g 64.97g [[email protected] ~]# pvs #查看PV状态 ,可以看出/myvg1-mylv1 中/dev/sdb1 物理卷处于空闲状态,/myvg2-mylv2 中/dev/sd{e..g}1 物理卷处于空闲状态 PV VG Fmt Attr PSize PFree /dev/sdb1 myvg1 lvm2 a--u 19.99g 19.99g /dev/sdc1 myvg1 lvm2 a--u 19.99g 14.99g /dev/sdd1 myvg2 lvm2 a--u 19.99g 4.99g /dev/sde1 myvg2 lvm2 a--u 19.99g 19.99g /dev/sdf1 myvg2 lvm2 a--u 19.99g 19.99g /dev/sdg1 myvg2 lvm2 a--u 19.99g 19.99g [[email protected] ~]# vgreduce myvg1 /dev/sdb1 #删减卷组myvg1 中的/dev/sdb1 物理卷 Removed "/dev/sdb1" from volume group "myvg1" [[email protected] ~]# vgreduce myvg2 /dev/sd{e..g}1 Removed "/dev/sde1" from volume group "myvg2" Removed "/dev/sdf1" from volume group "myvg2" Removed "/dev/sdg1" from volume group "myvg2" [[email protected] ~]# pvs #查看状态 PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 ---- 19.99g 19.99g /dev/sdc1 myvg1 lvm2 a--u 19.99g 14.99g /dev/sdd1 myvg2 lvm2 a--u 19.99g 4.99g /dev/sde1 lvm2 ---- 19.99g 19.99g /dev/sdf1 lvm2 ---- 19.99g 19.99g /dev/sdg1 lvm2 ---- 19.99g 19.99g [[email protected] ~]# vgextend myvg1 /dev/sdb1 #增添卷组myvg1 中的/dev/sdb1 物理卷 Volume group "myvg1" successfully extended [[email protected] ~]# vgextend myvg2 /dev/sd{e..g}1 Volume group "myvg2" successfully extended [[email protected] ~]# pvs #查看状态 PV VG Fmt Attr PSize PFree /dev/sdb1 myvg1 lvm2 a--u 19.99g 19.99g /dev/sdc1 myvg1 lvm2 a--u 19.99g 14.99g /dev/sdd1 myvg2 lvm2 a--u 19.99g 4.99g /dev/sde1 myvg2 lvm2 a--u 19.99g 19.99g /dev/sdf1 myvg2 lvm2 a--u 19.99g 19.99g /dev/sdg1 myvg2 lvm2 a--u 19.99g 19.99g
八、卸载删除lVM
[[email protected] ~]# umount /dev/mapper/myvg1-mylv1 #卸载逻辑卷 [[email protected] ~]# lvremove /dev/mapper/myvg1-mylv1 #移除LVM Do you really want to remove active logical volume mylv1? [y/n]: y Logical volume "mylv1" successfully removed [[email protected] ~]# vgremove myvg1 #移除VG Volume group "myvg1" successfully removed [[email protected] ~]# pvremove /dev/sd{b,c}1 #移除PV Labels on physical volume "/dev/sdb1" successfully wiped Labels on physical volume "/dev/sdc1" successfully wiped
至此 ,实验完成 。
本文出自 “Wish_亮” 博客,请务必保留此出处http://itwish.blog.51cto.com/11439802/1968324
以上是关于CentOS8提高篇9:LVM(逻辑卷管理)的主要内容,如果未能解决你的问题,请参考以下文章