一、lvm简介:
逻辑卷管理(Logical Volume Management,LVM)是一种用于在Linux系统中管理磁盘空间的工具。LVM允许管理员将物理磁盘抽象成逻辑卷和卷组,从而提供了更大的灵活性和可管理性。
LVM将一个或多个磁盘分区(PV)虚拟为一个卷组(VG),相当于一个大的硬盘,我们可以在上面划分一些逻辑卷(LV)。当卷组的空间不够使用时,可以将新的磁盘分区加入进来。我们还可以从卷组剩余空间上划分一些空间给空间不够用的逻辑卷使用。
-
物理卷(Physical Volume,PV):一个物理磁盘分区(或整个磁盘)可以被初始化为物理卷,用于存储数据
-
卷组(Volume Group,VG):多个物理卷可以被组合成一个卷组,卷组是逻辑卷管理的最小单元
-
逻辑卷(Logical Volume,LV):从卷组分配的逻辑存储空间,类似于传统的硬盘分区,但具有更强大的管理能力。
二、示例:
1、新添加的100G磁盘做lvm
-
将vdc扩容到vgdata-lvdata中
2、做分区
# fdisk /dev/vdc
Welcome to fdisk (util-linux 2.35.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.
Created a new DOS disklabel with disk identifier 0xa27d51e2.
Command (m for help): p #在此输入p,显示磁盘信息
Disk /dev/vdc: 100 GiB, 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
Disklabel type: dos
Disk identifier: 0xa27d51e2
Command (m for help): n #在此输入n,"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): 1 #在此输入1
First sector (2048-209715199, default 2048): #回车
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199): #回车
Created a new partition 1 of type 'Linux' and of size 100 GiB.
Command (m for help): t #在此输入t "t" 命令来更改分区的类型
Selected partition 1
Hex code or alias (type L to list all): L #查看可用的分区类型
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Linux extended
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
Aliases:
linux - 83
swap - 82
extended - 05
uefi - EF
raid - FD
lvm - 8E
linuxex - 85
Hex code or alias (type L to list all): 8e #输入lvm类型的hex代码
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): p #在此输入p显示磁盘信息
Disk /dev/vdc: 100 GiB, 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
Disklabel type: dos
Disk identifier: 0xa27d51e2
Device Boot Start End Sectors Size Id Type
/dev/vdc1 2048 209715199 209713152 100G 8e Linux LVM
Command (m for help): w #保存
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
3、创建物理卷PV
(1)、查看是否分好区
[root@xs-database-29 data]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 40G 0 disk
└─vda1 253:1 0 40G 0 part /
vdb 253:16 0 100G 0 disk
└─vdb1 253:17 0 100G 0 part
└─vgdata-lvdata 252:0 0 100G 0 lvm /data
vdc 253:32 0 100G 0 disk
└─vdc1 253:33 0 100G 0 part
(2)、创建物理卷
[root@xs-database-29 data]# pvcreate /dev/vdc1
Physical volume "/dev/vdc1" successfully created.
(3)、查看物理卷创建情况
[root@xs-database-29 data]# pvdisplay
--- Physical volume ---
PV Name /dev/vdb1
VG Name vgdata
PV Size <100.00 GiB / not usable <3.97 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 25599
Free PE 0
Allocated PE 25599
PV UUID L86lPR-EnXK-g3vW-IUVX-6WtG-ecz1-BonjsC
"/dev/vdc1" is a new physical volume of "<100.00 GiB"
--- NEW Physical volume ---
PV Name /dev/vdc1 #显示了刚刚创建的物理卷
VG Name
PV Size <100.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID qBdN9B-epP7-SWYM-Mmef-HPmQ-9Xsi-yWc3wg
(4)、将物理卷PV加入卷组VG
1、查看卷组情况
[root@xs-database-29 data]# vgdisplay
--- Volume group ---
VG Name vgdata #卷组名字
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size <100.00 GiB
PE Size 4.00 MiB
Total PE 25599
Alloc PE / Size 25599 / <100.00 GiB
Free PE / Size 0 / 0
VG UUID QmL2eY-WCQe-9vxK-RdSb-enxz-M1hD-C05Zdo
2、 将新的分区/dev/vdc1加入到卷组中,这里的卷组名为vgdata中
[root@xs-database-29 data]# vgextend vgdata /dev/vdc1
scan_dev_close /dev/vdc1 no DEV_IN_BCACHE set
scan_dev_close /dev/vdc1 already closed
Volume group "vgdata" successfully extended
(5)、添加完成后再次查看卷组信息
[root@xs-database-29 data]# vgdisplay
--- Volume group ---
VG Name vgdata
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 199.99 GiB ####容量增加了100GB
PE Size 4.00 MiB
Total PE 51198
Alloc PE / Size 25599 / <100.00 GiB
Free PE / Size 25599 / <100.00 GiB
VG UUID QmL2eY-WCQe-9vxK-RdSb-enxz-M1hD-C05Zdo
(6)、扩容已有分区
下面是对现有分区进行逻辑卷扩容,我们这里根分区为100G,我们准备扩容/data分区 查看根分区的LV路径 /dev/mapper/vgdata-lvdata
root@xs-database-29 data]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 252K 16G 1% /dev/shm
tmpfs 16G 386M 15G 3% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vda1 40G 7.8G 30G 21% /
tmpfs 16G 4.0K 16G 1% /tmp
tmpfs 3.1G 0 3.1G 0% /run/user/0
/dev/mapper/vgdata-lvdata 100G 75G 26G 75% /data
下面进行扩容
[root@xs-database-29 data]# lvextend -l +100%FREE /dev/mapper/vgdata-lvdata
Size of logical volume vgdata/lvdata changed from <100.00 GiB (25599 extents) to 199.99 GiB (51198 extents).
Logical volume vgdata/lvdata successfully resized.
(7)、查看扩容后的逻辑卷大小
[root@xs-database-29 data]# lvdisplay /dev/mapper/vgdata-lvdata
--- Logical volume ---
LV Path /dev/vgdata/lvdata
LV Name lvdata
VG Name vgdata
LV UUID JOLarw-lNqh-uBto-ivr8-y7C2-9mGA-DtPdiM
LV Write Access read/write
LV Creation host, time xh-databases-29, 2024-04-29 11:40:15 +0800
LV Status available
# open 1
LV Size 199.99 GiB
Current LE 51198
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 252:0
(8)、使用xfs_groupfs进行在线调整xfs格式的文件系统大小
[root@xs-database-29 data]# xfs_growfs /dev/mapper/vgdata-lvdata
meta-data=/dev/mapper/vgdata-lvdata isize=512 agcount=4, agsize=6553344 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=26213376, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=12799, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 26213376 to 52426752
##data blocks changed from 26213376 to 52426752 表示文件系统数据块的数量已经从26213376个扩展到52426752个,即文件系统已成功扩展。
(9)、最后查看下根分区的大小是否扩容成功
[root@xs-database-29 data]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 252K 16G 1% /dev/shm
tmpfs 16G 386M 15G 3% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vda1 40G 7.8G 30G 21% /
tmpfs 16G 4.0K 16G 1% /tmp
tmpfs 3.1G 0 3.1G 0% /run/user/0
/dev/mapper/vgdata-lvdata 200G 76G 125G 38% /data
标签:VG,16G,dev,LV,新加,lvm,磁盘,vgdata,data From: https://www.cnblogs.com/j-qingtian/p/18222930