逻辑卷工作原理和创建
1.LVM介绍
LVM: Logical Volume Manager 可以允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小, 允许在多个物理设备间重新组织文件系统
LVM可以弹性的更改LVM的容量
通过交换PE来进行资料的转换,将原来LV内的PE转移到其他的设备中以降低LV的容量,或将其他设备 中的PE加到LV中以加大容量
实现过程
将设备指定为物理卷
用一个或者多个物理卷来创建一个卷组,物理卷是用固定大小的物理区域(Physical Extent,PE)来定义的
在物理卷上创建的逻辑卷, 是由物理区域(PE)组成
可以在逻辑卷上创建文件系统并挂载
2.介绍几个LVM相关的术语:
1)物理卷(PV):在LVM系统中的最底层,可以是物理硬盘或物理硬盘上的分区,创建分区时必须指定分区格式为LVM所支持的格式即8e。硬盘分区后(还未格式化为文件系统)使用pvcreate命令可以将分区创建为pv。
2)卷组(VG):在PV基础上,通过vgcreate将一个或多个PV组合成VG,VG整合了多个PV,就如同整合了多个分区的硬盘一样。一旦VG被创建,可动态的将PV添加到VG中实现扩容,且在LVM中可创建福哦个VG。另外,创建VG时会将VG所有的空间根据指定的PE大小划分多个PE,在LVM模式下的存储都以PE为单元,类似文件系统的Block。
3)物理扩展(PE):也称物理区域,是LVM中最小的存储单位也是物理卷中可用于分配的最小单位,PE是构成VG的基本单位,就如同Block是构成分区的基本单位一样,PE的大小可以设定,也决定了LVM的灵活性。
4)逻辑卷(LV):VG相当于整合过的硬盘,那么LV相当于分区,只不过该分区是通过VG来划分的,可动态缩减和扩容。VG中有很多PE单元,可以指定将多少个PE划分给一个LV,也可以直接指定大小(如多少兆)来划分。划分LV之后就相当于划分了分区,只需再对LV进行格式化即可变成文件系统。
5)逻辑扩展(LE):PE是物理存储单元,而LE则是逻辑存储单元,也即为LV中的逻辑存储单元,和PE的大小是一样的。从VG中划分LV,实际上是从VG上划分PE,只不过划分LV后它不再称为PE,而称为LE。
3.通俗的讲,非LVM管理的分区步骤是将硬盘分区,然后将分区格式化为文件系统。而使用LVM,则是在硬盘分区为特定的LVM标识符的分区后将其转变为LVM可管理的PV,其实PV仍类似于分区,然后将几个PV重新整合为类似于磁盘的VG,最后划分VG为LV,此时的LV就成了LVM可管理的分区,只需再对其格式化即可成为文件系统。
LVM之所以能够伸缩容量,其实现的方法就是将VG中空闲的PE添加到LV中,此为扩容,或者将LV中空闲的PE移回到VG中,此为缩减容量。而且,可以随时向VG中添加新的PV,这就使得LVM管理的分区可以随意增加大小。
4.创建逻辑卷
第一步,创建PV:
先创建硬盘的分区
[root@centos8 ~ 820]#fdisk /dev/sda 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. Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 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 Rufus alignment 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 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: 20 GiB, 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 Disklabel type: dos Disk identifier: 0xc08016ad Device Boot Start End Sectors Size Id Type /dev/sda1 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@centos8 ~ 820]#pvs
[root@centos8 ~ 821]#pvdisplay
查看现有的卷组:
[root@centos8 ~ 822]#vgs
[root@centos8 ~ 823]#vgdisplay
显示逻辑卷:
[root@centos8 ~ 824]#lvs
[root@centos8 ~ 825]#lvdisplay
将/dev/sda1和/dev/sdb变成物理卷:
[root@centos8 ~ 826]#pvcreate /dev/sd{a1,b} Physical volume "/dev/sda1" successfully created. Physical volume "/dev/sdb" successfully created. [root@centos8 ~ 827]#pvs PV VG Fmt Attr PSize PFree /dev/sda1 lvm2 --- 5.00g 5.00g /dev/sdb lvm2 --- 10.00g 10.00g
第二步,创建卷组
[root@centos8 ~ 828]#vgcreate vg0 /dev/sd{a1,b} Volume group "vg0" successfully created [root@centos8 ~ 829]#pvdisplay --- Physical volume --- PV Name /dev/sda1 VG Name vg0 PV Size 5.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 1279 Free PE 1279 Allocated PE 0 PV UUID e3FxpO-ioaN-Qeoz-DmzJ-2ZoH-QAtg-5JaeFY --- Physical volume --- PV Name /dev/sdb VG Name vg0 PV Size 10.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 2559 Free PE 2559 Allocated PE 0 PV UUID N3uAIl-rj0h-3GNE-oeBi-hgSl-9l31-1SqH14 [root@centos8 ~ 830]#vgs VG #PV #LV #SN Attr VSize VFree vg0 2 0 0 wz--n- 14.99g 14.99g
第三步,逻辑卷的创建
我们先创建一个1G的叫mysql的逻辑卷
[root@centos8 ~ 832]#lvcreate -n mysql -L 1G vg0 Logical volume "mysql" created
[root@centos8 ~ 833]#lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mysql vg0 -wi-a----- 1.00g
[root@centos8 ~ 834]#lvdisplay
--- Logical volume ---
LV Path /dev/vg0/mysql
LV Name mysql
VG Name vg0
LV UUID 3ddR47-HaaR-6Mh9-9obW-rhk4-2ijk-LZetcy
LV Write Access read/write
LV Creation host, time centos8.localdomain, 2022-02-15 21:23:33 +0800
LV Status available
# open 0
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
我们再来观察vgdisplay:
[root@centos8 ~ 835]#vgdisplay --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 14.99 GiB PE Size 4.00 MiB Total PE 3838 Alloc PE / Size 256 / 1.00 GiB Free PE / Size 3582 / 13.99 GiB VG UUID iJg5gR-r05g-YTK3-7DVi-Yvna-2AJP-mtpijK
表示分配了256个PE还剩3582个PE
5.那么问题来了:我们刚才创建的逻辑卷来自哪个物理卷?
其实不用关心,但是也可以看:
[root@centos8 ~ 837]#pvdisplay --- Physical volume --- PV Name /dev/sda1 VG Name vg0 PV Size 5.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 1279 Free PE 1023 Allocated PE 256 PV UUID e3FxpO-ioaN-Qeoz-DmzJ-2ZoH-QAtg-5JaeFY --- Physical volume --- PV Name /dev/sdb VG Name vg0 PV Size 10.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 2559 Free PE 2559 Allocated PE 0 PV UUID N3uAIl-rj0h-3GNE-oeBi-hgSl-9l31-1SqH14
你会发现/dev/sda1总共1279个PE,而现在空闲的有1023个,说明有256个PE被用了,也就是刚才的逻辑卷,所以是来自/dev/sda1中。
6.1)我们现在通过lvdisplay看到的设备名其实是一个软链接:
[root@centos8 ~ 839]#ll /dev/vg0/mysql lrwxrwxrwx 1 root root 7 Feb 15 21:23 /dev/vg0/mysql -> ../dm-0
它真正的设备名:
[root@centos8 ~ 841]#ll /dev/dm-0 brw-rw---- 1 root disk 253, 0 Feb 15 21:23 /dev/dm-0
我们再创建一个逻辑卷:
[root@centos8 ~ 842]#lvcreate -n log -l 500 vg0 Logical volume "log" created. [root@centos8 ~ 843]#lvdisplay --- Logical volume --- LV Path /dev/vg0/mysql LV Name mysql VG Name vg0 LV UUID 3ddR47-HaaR-6Mh9-9obW-rhk4-2ijk-LZetcy LV Write Access read/write LV Creation host, time centos8.localdomain, 2022-02-15 21:23:33 +0800 LV Status available # open 0 LV Size 1.00 GiB Current LE 256 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0 --- Logical volume --- LV Path /dev/vg0/log LV Name log VG Name vg0 LV UUID Y5ZpaZ-Ogt5-a9Od-dypt-sEog-MsF5-eqwMYg LV Write Access read/write LV Creation host, time centos8.localdomain, 2022-02-15 21:41:29 +0800 LV Status available # open 0 LV Size 1.95 GiB Current LE 500 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 [root@centos8 ~ 844]#ll /dev/vg0/log lrwxrwxrwx 1 root root 7 Feb 15 21:41 /dev/vg0/log -> ../dm-1 [root@centos8 ~ 845]#ll /dev/dm-1 brw-rw---- 1 root disk 253, 1 Feb 15 21:41 /dev/dm-1
2)除了第一个软链接还有一个:
[root@centos8 ~ 846]#ll /dev/mapper/vg0-log lrwxrwxrwx 1 root root 7 Feb 15 21:41 /dev/mapper/vg0-log -> ../dm-1 [root@centos8 ~ 847]#ll /dev/mapper/vg0-mysql lrwxrwxrwx 1 root root 7 Feb 15 21:23 /dev/mapper/vg0-mysql -> ../dm-0
这也是它的名字。
所以对于当前逻辑卷来说,你有三个命名方式来用:第一,/dev/vg0/xxx;第二,/dev/dm-x;第三,/dev/mapper/vg0-xxx
7.使用逻辑卷
第一步,创建文件系统:
[root@centos8 ~ 848]#mkfs.ext4 /dev/vg0/mysql mke2fs 1.45.6 (20-Mar-2020) Creating filesystem with 262144 4k blocks and 65536 inodes Filesystem UUID: be265ed6-7b5a-4f08-96b0-8f936fab6102 Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done [root@centos8 ~ 849]#blkid /dev/nvme0n1: PTUUID="9d86d599" PTTYPE="dos" /dev/nvme0n1p1: UUID="b3631a44-f636-461a-a651-05ad78ab6f7e" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="9d86d599-01" /dev/nvme0n1p2: UUID="95c58cc8-c171-4df2-9e23-3b825af5d2d9" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="9d86d599-02" /dev/nvme0n1p3: UUID="a52f0b13-1f54-47d3-9660-b1dcafbfc0fb" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="9d86d599-03" /dev/nvme0n1p5: UUID="f2672235-5b02-449e-81c7-a7675a04d9af" TYPE="swap" PARTUUID="9d86d599-05" /dev/sr0: BLOCK_SIZE="2048" UUID="2021-11-13-01-04-26-00" LABEL="CentOS-8-5-2111-x86_64-dvd" TYPE="iso9660" PTUUID="5fb10f71" PTTYPE="dos" /dev/sdb: UUID="N3uAIl-rj0h-3GNE-oeBi-hgSl-9l31-1SqH14" TYPE="LVM2_member" /dev/sda1: UUID="e3FxpO-ioaN-Qeoz-DmzJ-2ZoH-QAtg-5JaeFY" TYPE="LVM2_member" PARTUUID="c08016ad-01" /dev/mapper/vg0-mysql: UUID="be265ed6-7b5a-4f08-96b0-8f936fab6102" BLOCK_SIZE="4096" TYPE="ext4"
第二步,挂载
[root@centos8 ~ 850]#vim /etc/fstab
将其挂上
[root@centos8 ~ 852]#mkdir /mnt/mysql [root@centos8 ~ 853]#mount -a [root@centos8 ~ 854]#df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 889204 0 889204 0% /dev tmpfs 916616 0 916616 0% /dev/shm tmpfs 916616 9680 906936 2% /run tmpfs 916616 0 916616 0% /sys/fs/cgroup /dev/nvme0n1p2 52403200 6300372 46102828 13% / /dev/nvme0n1p1 1038336 345564 692772 34% /boot /dev/nvme0n1p3 31441920 256588 31185332 1% /data tmpfs 183320 32 183288 1% /run/user/0 /dev/sr0 10540998 10540998 0 100% /run/media/root/CentOS-8-5-2111-x86_64-dvd /dev/mapper/vg0-mysql 999320 2564 927944 1% /mnt/mysql
就可以正常使用了。
8.清理系统缓存:
echo 3 > /proc/sys/vm/drop_caches
9.对比测试逻辑卷速度:
[root@centos8 ~ 858]#dd if=/dev/zero of=/data/test.img bs=1M count=800 800+0 records in 800+0 records out 838860800 bytes (839 MB, 800 MiB) copied, 1.52432 s, 550 MB/s [root@centos8 ~ 859]#echo 3 > /proc/sys/vm/drop_caches [root@centos8 ~ 860]#dd if=/dev/zero of=/mnt/mysql/test.img bs=1M count=800 800+0 records in 800+0 records out 838860800 bytes (839 MB, 800 MiB) copied, 0.998698 s, 840 MB/s
同样的大小,同样的内容,逻辑卷的性能还是有所提高的。
2022-2-15 22:07