邏輯卷工作原理和建立
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