LVM
LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。物理卷(physical volume)物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
优势:能够在线扩容当前的分区容量。
结构:在整个LVM结构当中以此分为:“物理卷、物理卷分区、卷组、逻辑卷”,下面我详细解释一下这四种结构:
物理卷:Physical Volume,简称PV,一个物理卷只不过是一个有LVM管理数据添加在里面的物理存储介质。要使用LVM系统,首先对要用于LVM的磁盘进行初始化,初始化的目的就是将磁盘或分区标识为LVM 的物理卷。使用pvcreate 命令可以将一个磁盘标记为 LVM 物理卷。
物理分区:Physical Extents,简称PE,LVM将每个物理卷分别叫做物理分区的可寻址存储单元,存储单元的大小通常为几MB。磁盘的开头部分为LVM元数据,之后从索引为零开始,每个物理分区的索引依次递增一,按顺序进行分配。
卷组:Volume Group,简称VG,物理卷可以组织为卷组。卷组可以由一个或多个物理卷组成,同时系统中可以有多个卷组。创建了卷组之后,该卷组(而不是磁盘)便是表示数据存储的实体。因此,尽管以前是将磁盘从一个系统移动到另一个系统,使用了 LVM 之后,会将卷组从一个系统移动到另一个系统。出于这种原因,通常在一个系统上创建多个卷组会比较方便。
逻辑分区:Logical Extents,简称LE,逻辑卷的基本分配单元称为逻辑分区。逻辑分区映射到物理分区,因此,如果物理分区的尺寸小为4MB,那么逻辑分区的尺寸也将为4MB。逻辑卷的大小取决于所分配的逻辑分区数量。
生产环境
[root@Nexus3 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@Nexus3 ~]# lvm version
LVM version: 2.02.185(2)-RHEL7 (2019-05-13)
Library version: 1.02.158-RHEL7 (2019-05-13)
Driver version: 4.37.1
硬盘扩容20G->100G
[root@Nexus3 ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 3.9G 8.8M 3.9G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
-----/dev/mapper/centos-root是我们需要扩的卷-------------------------
/dev/mapper/centos-root xfs 17G 1.2G 16G 7% /
/dev/sda1 xfs 1014M 136M 879M 14% /boot
tmpfs tmpfs 799M 0 799M 0% /run/user/0
查看本地物理硬盘
[root@Nexus3 ~]# fdisk -l
-----磁盘容量100G-------------------------
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000c35ff
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
-----正在使用18.2G,我们需要将此硬盘扩容-------------------------
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
硬盘分区
根据实际情况选择自己的硬盘
[root@Nexus3 ~]# 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): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000c35ff
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
-----注意后面Id System 后面用到-------------------------
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
-----创建分区n,一路回车就会创建sda3全部容量-------------------------
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (41943040-209715199, default 41943040):
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-209715199, default 209715199):
Using default value 209715199
Partition 3 of type Linux and of size 80 GiB is set
-----验证分区-------------------------
Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000c35ff
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
-----看看后面Id System需要修改-------------------------
/dev/sda3 41943040 209715199 83886080 83 Linux
-----修改Id-------------------------
Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000c35ff
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 209715199 83886080 8e Linux LVM
-----保存!!!!!-------------------------
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.
-----同步磁盘状态-------------------------
[root@Nexus3 ~]# partprobe
创建物理卷
pvcreate
-----查询当前PV-------------------------
[root@Nexus3 ~]# 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 2MrxDf-dTQK-I7iK-5VUs-bcrv-Af2t-fVaAiL
-----创建新的PV-------------------------
[root@Nexus3 ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
[root@Nexus3 ~]# 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 2MrxDf-dTQK-I7iK-5VUs-bcrv-Af2t-fVaAiL
-----这个是我们创建的-------------------------
"/dev/sda3" is a new physical volume of "80.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 80.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID kfO9Yc-ToQb-2vnf-8muc-MNsZ-oxJV-vi1vVe
扩展卷组
vgextend
-----查看当前VG-------------------------
[root@Nexus3 ~]# vgdisplay
--- Volume group ---
-----VG Name需要用到-------------------------
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 Wx79ds-a3LO-VhXo-HJdk-fr0X-C9UX-KOqfSK
-----扩展卷组-------------------------
[root@Nexus3 ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
逻辑卷扩展
lvextend -l +100%free #前面df -hT看到的信息
-----查看逻辑卷-------------------------
[root@Nexus3 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID bgsynr-YUZE-LX2j-NIgG-sqqU-wVxb-GIZDg7
LV Write Access read/write
LV Creation host, time localhost, 2019-11-22 14:16:37 +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 lkRm4c-8g6X-ZWm2-Y86h-aKXs-i2lN-ZKdGaD
LV Write Access read/write
LV Creation host, time localhost, 2019-11-22 14:16:37 +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
-----扩充-------------------------
[root@Nexus3 ~]# lvextend -l +100%free /dev/mapper/centos-root
Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to 96.99 GiB (24830 extents).
Logical volume centos/root successfully resized.
动态格式化扩容磁盘
[root@Nexus3 ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4455424, 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 4455424 to 25425920
验证
[root@Nexus3 ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 3.9G 8.9M 3.9G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
-----100G-------------------------
/dev/mapper/centos-root xfs 97G 1.2G 96G 2% /
/dev/sda1 xfs 1014M 136M 879M 14% /boot
tmpfs tmpfs 799M 0 799M 0% /run/user/0
END