一、LVM简介
LVM(Logical Volume Manager,逻辑卷管理器)是Linux系统用于对硬盘分区进行管理的一种机制,其创建初衷是为了解决硬盘设备在创建分区后不易修改分区大小的缺陷。尽管对传统的硬盘分区进行强制扩容或缩容从理论上来讲是可行的,但是却可能造成数据的丢失。而LVM技术是在硬盘分区和文件系统之间添加了一个逻辑层,它提供了一个抽象的卷组,可以把多块硬盘进行卷组合并。这样一来,用户不必关心物理硬盘设备的底层架构和布局,就可以实现对硬盘分区的动态调整
二、实验环境
操作平台:VMware Workstation 17 Pro
虚机操作系统:RokcyLinux 8.9
三、参考实例
3.1 创建物理卷(PV)
LVM支持使用整块磁盘创建PV,或用分区创建PV
例如使用/dev/sdb磁盘创建PV
[root@localhost ~]# parted /dev/sdb print # 查看磁盘信息 Error: /dev/sdb: unrecognised disk label Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: unknown Disk Flags: [root@localhost ~]# pvcreate /dev/sdb # 创建 Physical volume "/dev/sdb" successfully created.
以下三条命令均可查看pv状态
[root@localhost ~]# pvscan PV /dev/sda2 VG rl lvm2 [<49.00 GiB / 0 free] PV /dev/sdb lvm2 [10.00 GiB] Total: 2 [<59.00 GiB] / in use: 1 [<49.00 GiB] / in no VG: 1 [10.00 GiB] [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 rl lvm2 a-- <49.00g 0 /dev/sdb lvm2 --- 10.00g 10.00g [root@localhost ~]# pvdisplay /dev/sdb "/dev/sdb" is a new physical volume of "10.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb VG Name PV Size 10.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID J24xM9-b5SN-NlA0-q8cv-jQw3-eBRW-aLdNKk
使用磁盘分区创建PV
[root@localhost ~]# fdisk /dev/sdc # 首先对磁盘进行分区 Welcome to fdisk (util-linux 2.32.1). 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 0xa6c9a99a. 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-20971519, default 2048): # 分区的起始扇区,默认即可,直接回车 Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +5G # 分区大小5G Created a new partition 1 of type 'Linux' and of size 5 GiB. Command (m for help): t # 更改分区类型 Selected partition 1 Hex code (type L to list all codes): 8e # 选择逻辑卷类型,对应编号为8e Changed type of partition 'Linux' to 'Linux LVM'. Command (m for help): p # 打印分区信息 Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 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: 0xa6c9a99a Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 10487807 10485760 5G 8e Linux LVM Command (m for help): w # 保存分区表 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
[root@localhost ~]# pvcreate /dev/sdc1 # 创建 Physical volume "/dev/sdc1" successfully created. [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 rl lvm2 a-- <49.00g 0 /dev/sdb lvm2 --- 10.00g 10.00g /dev/sdc1 lvm2 --- 5.00g 5.00g [root@localhost ~]# pvdisplay /dev/sdc1 "/dev/sdc1" is a new physical volume of "5.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size 5.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID K3MapS-m1yx-1E5p-pSjw-OTLB-OdMI-hPNCMe
3.2 创建卷组(VG)
例如:使用物理卷“/dev/sdb”创建一个名为“vgtest”的卷组
[root@localhost ~]# vgcreate vgtest /dev/sdb Volume group "vgtest" successfully created
以下命令均可查看VG的状态
[root@localhost ~]# vgscan Found volume group "vgtest" using metadata type lvm2 Found volume group "rl" using metadata type lvm2 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree rl 1 2 0 wz--n- <49.00g 0 vgtest 1 0 0 wz--n- <10.00g <10.00g [root@localhost ~]# vgdisplay vgtest --- Volume group --- VG Name vgtest System ID Format lvm2 Metadata Areas 1 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 1 Act PV 1 VG Size <10.00 GiB PE Size 4.00 MiB # 默认的PE大小为4MiB Total PE 2559 Alloc PE / Size 0 / 0 Free PE / Size 2559 / <10.00 GiB VG UUID yb2LkE-6N02-Pv6w-6Wb6-sVII-1E1c-9VrNrj
也可以在创建卷组时指定PE的大小,使用“-s”参数指定,单位是“M”
[root@localhost ~]# vgcreate -s 2M vgtest1 /dev/sdc1 Volume group "vgtest1" successfully created [root@localhost ~]# pvdisplay /dev/sdc1 --- Physical volume --- PV Name /dev/sdc1 VG Name vgtest1 PV Size 5.00 GiB / not usable 2.00 MiB Allocatable yes PE Size 2.00 MiB # PE大小为2 Total PE 2559 Free PE 2559 Allocated PE 0 PV UUID K3MapS-m1yx-1E5p-pSjw-OTLB-OdMI-hPNCMe
可以使用多个PV来创建卷组,这样创建出来的VG容量就是多个PV的总和
[root@localhost ~]# pvcreate /dev/sdd Physical volume "/dev/sdd" successfully created. [root@localhost ~]# pvcreate /dev/sde Physical volume "/dev/sde" successfully created. [root@localhost ~]# [root@localhost ~]# vgcreate vgtest2 /dev/sdd /dev/sde Volume group "vgtest2" successfully created [root@localhost ~]# vgdisplay vgtest2 --- Volume group --- VG Name vgtest2 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 jI8V7c-A4Pa-W2GU-cGc4-kglH-IYii-TNfFRp
3.3 创建逻辑卷(LV)
使用卷组“vgtest”来创建逻辑卷,名为“lvtest”,将“vgtest”的所有空间分配给“lvtest”
[root@localhost ~]# lvcreate -l 100%FREE vgtest -n lvtest Logical volume "lvtest" created.
以下命令均可查看逻辑卷状态
[root@localhost ~]# lvscan ACTIVE '/dev/vgtest/lvtest' [<10.00 GiB] inherit ACTIVE '/dev/rl/swap' [3.91 GiB] inherit ACTIVE '/dev/rl/root' [45.08 GiB] inherit [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root rl -wi-ao---- 45.08g swap rl -wi-ao---- 3.91g lvtest vgtest -wi-a----- <10.00g [root@localhost ~]# lvdisplay vgtest/lvtest --- Logical volume --- LV Path /dev/vgtest/lvtest LV Name lvtest VG Name vgtest LV UUID hJX97w-kJtH-izv3-5N2U-rlVf-OZfe-8OMedE LV Write Access read/write LV Creation host, time localhost.localdomain, 2023-12-30 2257 +0800 LV Status available # open 0 LV Size <10.00 GiB Current LE 2559 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2
可以在创建逻辑卷时指定其容量
[root@localhost ~]# lvcreate -L 3G vgtest1 -n lvtest1 # 指定LV容量为3G Logical volume "lvtest1" created. [root@localhost ~]# lvdisplay vgtest1/lvtest1 --- Logical volume --- LV Path /dev/vgtest1/lvtest1 LV Name lvtest1 VG Name vgtest1 LV UUID ltVLNf-oyj0-25Ct-MVmx-VyeC-6y03-9LZdFV LV Write Access read/write LV Creation host, time localhost.localdomain, 2023-12-30 2213 +0800 LV Status available # open 0 LV Size 3.00 GiB # LV容量为3GiB Current LE 1536 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3
也可以在创建逻辑卷时指定为其分配的PE个数,例如,我想为创建的LV分配容量800M的容量,默认PE大小为4M,则为200个PE
[root@localhost ~]# vgdisplay vgtest2 --- Volume group --- VG Name vgtest2 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 # PE大小为4MiB Total PE 5118 Alloc PE / Size 0 / 0 Free PE / Size 5118 / 19.99 GiB [root@localhost ~]# lvcreate -l 200 vgtest2 -n lvtest2 Logical volume "lvtest2" created. [root@localhost ~]# lvdisplay vgtest2/lvtest2 --- Logical volume --- LV Path /dev/vgtest2/lvtest2 LV Name lvtest2 VG Name vgtest2 LV UUID rdYOR3-ETaO-Lc33-L0Si-ke3a-J3dV-qRTuGs LV Write Access read/write LV Creation host, time localhost.localdomain, 2023-12-30 2207 +0800 LV Status available # open 0 LV Size 800.00 MiB # LV容量为800MiB Current LE 200 # 分配到的PE个数为200 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:4
3.4 逻辑卷扩容
例如,如果我想对逻辑卷“lvtest2”进行扩容,增加200M容量
[root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root rl -wi-ao---- 45.08g swap rl -wi-ao---- 3.91g lvtest vgtest -wi-a----- <10.00g lvtest1 vgtest1 -wi-a----- 3.00g lvtest2 vgtest2 -wi-a----- 800.00m # 查看到lvtest2使用的是vgtest2这个卷组 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree rl 1 2 0 wz--n- <49.00g 0 vgtest 1 1 0 wz--n- <10.00g 0 vgtest1 1 1 0 wz--n- <5.00g <2.00g vgtest2 2 1 0 wz--n- 19.99g 19.21g # 查看到vgtest2还有19.21G的空闲容量 [root@localhost ~]# lvextend -L +200M vgtest2/lvtest2 Size of logical volume vgtest2/lvtest2 changed from 800.00 MiB (200 extents) to 1000.00 MiB (250 extents). Logical volume vgtest2/lvtest2 successfully resized. [root@localhost ~]# lvdisplay vgtest2/lvtest2 --- Logical volume --- LV Path /dev/vgtest2/lvtest2 LV Name lvtest2 VG Name vgtest2 LV UUID rdYOR3-ETaO-Lc33-L0Si-ke3a-J3dV-qRTuGs LV Write Access read/write LV Creation host, time localhost.localdomain, 2023-12-30 2207 +0800 LV Status available # open 0 LV Size 1000.00 MiB # 容量增长为1000MiB Current LE 250 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:4
也可以指定增加的PE个数来对逻辑卷进行扩容
[root@localhost ~]# lvextend -l +25 vgtest2/lvtest2 Size of logical volume vgtest2/lvtest2 changed from 1000.00 MiB (250 extents) to 1.07 GiB (275 extents). Logical volume vgtest2/lvtest2 successfully resized. [root@localhost ~]# lvdisplay vgtest2/lvtest2 --- Logical volume --- LV Path /dev/vgtest2/lvtest2 LV Name lvtest2 VG Name vgtest2 LV UUID rdYOR3-ETaO-Lc33-L0Si-ke3a-J3dV-qRTuGs LV Write Access read/write LV Creation host, time localhost.localdomain, 2023-12-30 2207 +0800 LV Status available # open 0 LV Size 1.07 GiB # 容量增长为1.07 Current LE 275 # 增加了25个PE Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:4
或者将VG的空闲容量全部分配给LV
[root@localhost ~]# lvextend -l +100%FREE vgtest2/lvtest2 Size of logical volume vgtest2/lvtest2 changed from 1.07 GiB (275 extents) to 19.99 GiB (5118 extents). Logical volume vgtest2/lvtest2 successfully resized. [root@localhost ~]# lvdisplay vgtest2/lvtest2 --- Logical volume --- LV Path /dev/vgtest2/lvtest2 LV Name lvtest2 VG Name vgtest2 LV UUID rdYOR3-ETaO-Lc33-L0Si-ke3a-J3dV-qRTuGs LV Write Access read/write LV Creation host, time localhost.localdomain, 2023-12-30 2207 +0800 LV Status available # open 0 LV Size 19.99 GiB # 容量增长为19.99 Current LE 5118 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:4
将逻辑卷“lvtest2”挂载
[root@localhost ~]# mkfs.xfs /dev/vgtest2/lvtest2 # 创建xfs文件系统 meta-data=/dev/vgtest2/lvtest2 isize=512 agcount=4, agsize=1310208 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 bigtime=0 inobtcount=0 data = bsize=4096 blocks=5240832, 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 /data [root@localhost ~]# mount /dev/vgtest2/lvtest2 /data # 挂载逻辑卷 [root@localhost ~]# df -Th /data Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vgtest2-lvtest2 xfs 20G 175M 20G 1% /data
如果想要永久挂载,需要在“/etc/fstab”文件中增加一行配置
[root@localhost ~]# blkid /dev/vgtest2/lvtest2 # 查看逻辑卷的UUID,通过UUID的方式挂载 /dev/vgtest2/lvtest2: UUID="544fae3b-7d08-4090-a6a7-aa5bcc4d9be0" BLOCK_SIZE="512" TYPE="xfs" [root@localhost ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Wed Nov 29 1550 2023 # # Accessible filesystems, by reference, are maintained under '/dev/disk/'. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info. # # After editing this file, run 'systemctl daemon-reload' to update systemd # units generated from this file. # /dev/mapper/rl-root / xfs defaults 0 0 UUID=9d58ddae-ddae-4a83-8841-4b9863b55ab5 /boot xfs defaults 0 0 /dev/mapper/rl-swap none swap defaults 0 0 UUID="544fae3b-7d08-4090-a6a7-aa5bcc4d9be0" /data xfs defaults 0 0 # 添加这一行 [root@localhost ~]# mount -a # 读取“/etc/fstab”配置文件,挂载文件中定义的设备
现在,假设服务器目录“/data”所涉及的业务量日益增加,而逻辑卷“lvtest2”的空间不足以支撑,需要对它再次进行扩容,我们可以这样做:
因为逻辑卷“lvtest2”关联的卷组是“vgtest2”,而“vgtest2”的所有容量已分配给了“lvtest2”,我们需要先对“vgtest2”进行扩容
新增一些scsi磁盘,如果希望在不重启的情况下让系统识别到新增的硬盘,可以这样做
[root@localhost ~]# ls /sys/class/scsi_host/ host0 host1 host2 [root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host0/scan # 有多少个目录就对应执行多个目录的命令 [root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host1/scan [root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host2/scan [root@localhost ~]# ls /sys/class/scsi_device/ 1:0:0:0 2:0:0:0 2:0:1:0 2:0:10:0 2:0:2:0 2:0:3:0 2:0:4:0 2:0:5:0 2:0:6:0 2:0:8:0 2:0:9:0 [root@localhost ~]# echo 1 > /sys/class/scsi_device/1:0:0:0/device/rescan # 有多少个目录就对应执行多个目录的命令 [root@localhost ~]# echo 1 > /sys/class/scsi_device/2:0:0:0/device/rescan [root@localhost ~]# echo 1 > /sys/class/scsi_device/2:0:1:0/device/rescan [root@localhost ~]# echo 1 > /sys/class/scsi_device/2:0:10:0/device/rescan [root@localhost ~]# echo 1 > /sys/class/scsi_device/2:0:2:0/device/rescan [root@localhost ~]# echo 1 > /sys/class/scsi_device/2:0:3:0/device/rescan [root@localhost ~]# echo 1 > /sys/class/scsi_device/2:0:4:0/device/rescan [root@localhost ~]# echo 1 > /sys/class/scsi_device/2:0:5:0/device/rescan [root@localhost ~]# echo 1 > /sys/class/scsi_device/2:0:6:0/device/rescan [root@localhost ~]# echo 1 > /sys/class/scsi_device/2:0:8:0/device/rescan [root@localhost ~]# echo 1 > /sys/class/scsi_device/2:0:9:0/device/rescan [root@localhost ~]# lsblk # 重新扫描后,就可以看到新增的块设备了 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 50G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 49G 0 part ├─rl-root 253:0 0 45.1G 0 lvm / └─rl-swap 253:1 0 3.9G 0 lvm [SWAP] sdb 8:16 0 10G 0 disk └─vgtest-lvtest 253:2 0 10G 0 lvm sdc 8:32 0 10G 0 disk └─sdc1 8:33 0 5G 0 part └─vgtest1-lvtest1 253:3 0 3G 0 lvm sdd 8:48 0 10G 0 disk └─vgtest2-lvtest2 253:4 0 20G 0 lvm /data sde 8:64 0 10G 0 disk └─vgtest2-lvtest2 253:4 0 20G 0 lvm /data sdf 8:80 0 10G 0 disk sdg 8:96 0 10G 0 disk sdh 8:112 0 10G 0 disk sdi 8:128 0 10G 0 disk sdj 8:144 0 10G 0 disk sr0 11:0 1 1024M 0 rom
首先,我们使用“/dev/sdf”这块磁盘创建PV,用于“vgtest2”的扩容
[root@localhost ~]# pvcreate /dev/sdf Physical volume "/dev/sdf" successfully created. [root@localhost ~]# vgextend vgtest2 /dev/sdf Volume group "vgtest2" successfully extended [root@localhost ~]# vgs vgtest2 VG #PV #LV #SN Attr VSize VFree vgtest2 3 1 0 wz--n- <29.99g <10.00g # 现在vgtest2的容量增长为30
然后,对逻辑卷“lvtest2”扩容,将空闲容量全部分配给它
[root@localhost ~]# lvextend -l +100%FREE vgtest2/lvtest2 Size of logical volume vgtest2/lvtest2 changed from 19.99 GiB (5118 extents) to <29.99 GiB (7677 extents). Logical volume vgtest2/lvtest2 successfully resized.
虽然逻辑卷“lvtest2”的容量已扩容至30G,但是挂载的目录仍显示可用容量为20G
[root@localhost ~]# lvs vgtest2/lvtest2 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lvtest2 vgtest2 -wi-ao---- <29.99g [root@localhost ~]# df -Th /data Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vgtest2-lvtest2 xfs 20G 175M 20G 1% /data
需要对文件系统进行扩容,ext类型的文件系统的扩容命令是“resize2fs”,xfs类型的文件系统扩容命令是xfs_growfs
[root@localhost ~]# xfs_growfs /dev/vgtest2/lvtest2 meta-data=/dev/mapper/vgtest2-lvtest2 isize=512 agcount=4, agsize=1310208 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 bigtime=0 inobtcount=0 data = bsize=4096 blocks=5240832, 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 data blocks changed from 5240832 to 7861248 [root@localhost ~]# df -Th /data Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vgtest2-lvtest2 xfs 30G 247M 30G 1% /data # 容量扩容至30G了
或者,我们可以在扩容逻辑卷的同时,一并把文件系统扩容,只需要在lvextend命令后面加上一个“-r”参数
对“lvtest2”再次进行扩容
[root@localhost ~]# pvcreate /dev/sdg # 创建PV Physical volume "/dev/sdg" successfully created. [root@localhost ~]# vgextend vgtest2 /dev/sdg # 扩容VG Volume group "vgtest2" successfully extended [root@localhost ~]# lvextend -L +5G -r vgtest2/lvtest2 # 扩容LV,增加5G容量,同时扩容文件系统 Size of logical volume vgtest2/lvtest2 changed from <29.99 GiB (7677 extents) to <34.99 GiB (8957 extents). Logical volume vgtest2/lvtest2 successfully resized. meta-data=/dev/mapper/vgtest2-lvtest2 isize=512 agcount=6, agsize=1310208 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 bigtime=0 inobtcount=0 data = bsize=4096 blocks=7861248, 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 data blocks changed from 7861248 to 9171968 [root@localhost ~]# df -Th /data Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vgtest2-lvtest2 xfs 35G 283M 35G 1% /data # 容量扩容至35
审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !