【IT168 專稿】Red hat 下的LVM 上 LVM是Logical Volume Manager(邏輯卷管理器)的簡寫,它為主機提供了更高層次的磁盤存儲管理能力。LVM可以幫助系統管理員為應用與用戶方便地分配存儲空間。在LVM管理下的邏輯卷可以按需改變大小或添加移除。另外,LVM可以為所管理的邏輯卷提供定制的命名標識。因此,使用LVM主要是方便了對存儲系統的管理,增加了系統的擴展性。
Linux,AIX,Hp-uinx平臺下的LVM 命令是不一樣的。下面以Redhat 下的命令來做一些說明。
一. LVM 理論知識
1. 先來看一下LVM創建的流程: PV-->VG-->LV.
PV(Physical Volume) :物理卷,可以是單獨磁盤,也可以是硬盤分區。
VG(Volume Group) :卷組,是PV的組合,可以看成單獨的邏輯磁盤。
LV(Logical Volume) :邏輯分區,當于物理分區的/dev/hdaX,只有邏輯卷才可以寫數據。
PE(Physical Extent) :物理范圍。VG單元,類似于RAID的條帶大小。 當多個PV組成一個VG時,LVM會在所有PV上做類似格式化的動作,將每個PV切成一塊塊的空間,這一塊塊的空間就稱為PE, 通常是4MB。
LE(Logical Extent) :邏輯范圍。LV的組成單位。大小為PE的倍數(通常為1:1)。
2. 工作原理
LVM 在每個物理卷頭部都維護了一個 metadata,每個 metadata 中都包含了整個 VG 的信息,包括每個 VG 的布局配置、PV 的編號、LV 的編號,以及每個 PE 到 LE 的映射關系。同一個 VG 中的每個 PV 頭部的信息是相同的,這樣有利于故障時進行數據恢復。
LVM 對上層文件系統提供 LV 層,隱藏了操作細節。對文件系統而言,對 LV 的操作與原先對 Partition 的操作沒有差別。當對 LV 進行寫入操作時,LVM 定位相應的 LE,通過 PV 頭部的映射表,將數據寫入到相應的 PE 上。
LVM 實現的關鍵在于在 PE 和 LE 間建立映射關系,不同的映射規則決定了不同的 LVM 存儲模型。LVM 支持多個 PV 的 Stripe 和 Mirror,這點和軟 Raid 的實現十分相似。
使用 LVM 的優勢:
?文件系統可以跨多個磁盤,因此大小不會受物理磁盤的限制。
?可以在系統運行狀態下動態地擴展文件系統大小。
?可以增加新磁盤到 LVM 的存儲池中。
?可以以鏡像的方式冗余重要數據到多個物理磁盤上。
?可以很方便地導出整個卷組,并導入到另外一臺機器上。
使用 LVM 的限制:
?在從卷組中移除一個磁盤時必須使用 reducevg,否則會出問題。
?當卷組中的一個磁盤損壞時,整個卷組都會受影響。
?不能減小文件系統大小(受文件系統類型限制)。
?因為加入了額外的操作,存儲性能會受影響(使用 Stripe 的情況另當別論)。
二. 創建相應的磁盤分區
1. 修改磁盤,創建相應的分區
[root@san iscsi]# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 255 2048256 83 Linux
/dev/sda2 256 382 1020127+ 82 Linux swap / Solaris
/dev/sda3 383 5221 38869267+ 5 Extended
/dev/sda5 383 395 104391 83 Linux
/dev/sda6 396 408 104391 83 Linux
/dev/sda7 409 421 104391 83 Linux
/dev/sda8 422 434 104391 83 Linux
這里創建了4個擴展分區,每個100M。 用fdisk /dev/sda創建的。 分區沒有格式化。
2. 使用磁盤分區生效
#partprobe
三. 創建PV
1.相關命令
pvcreate 創建PV
pvscan 掃描PV
pvdisplay 顯示PV
pvremove 刪除PV
partprobe
2.創建物理卷
[root@san iscsi]# pvcreate /dev/sda5 /dev/sda6
Physical volume "/dev/sda5" successfully created
Physical volume "/dev/sda6" successfully created
[root@san iscsi]# pvscan
PV /dev/sda5 lvm2 [101.94 MB]
PV /dev/sda6 lvm2 [101.94 MB] Total: 2 [203.89 MB] / in use: 0 [0 ] / in no VG: 2 [203.89 MB]
[root@san iscsi]# pvdisplay
"/dev/sda5" is a new physical volume of "101.94 MB"
--- NEW Physical volume ---
PV Name /dev/sda5
VG Name
PV Size 101.94 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID hDtAhK-adlx-5Ex0-ogc1-Alvm-H274-a8u7c2
"/dev/sda6" is a new physical volume of "101.94 MB"
--- NEW Physical volume ---
PV Name /dev/sda6
VG Name
PV Size 101.94 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Sy1wPG-XnjQ-eYD5-91vj-Ug5b-EGgU-i30lCS
四. 創建VG
1.相關命令
vgcreate 創建VG
vgscan 掃描VG
vgdispaly
vgextend
vgreduce
vgchange
vgremove
2.創建邏輯卷VG
[root@san iscsi]# vgcreate vg0 /dev/sda5 /dev/sda6
Volume group "vg0" successfully created
[root@san iscsi]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "vg0" using metadata type lvm2
Device '/dev/sda6' has been left open.
Device '/dev/sda5' has been left open.
[root@san iscsi]# vgdisplay
--- Volume group ---
VG Name vg0
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 200.00 MB
PE Size 4.00 MB /*分配的塊大小默認為4M*/ Total PE 50
Alloc PE / Size 0 / 0
Free PE / Size 50 / 200.00 MB
VG UUID iE7uoy-boR3-N6Cv-91Gf-G5vE-ftD6-gSGVlB
3.刪除與添加邏輯卷
[root@san iscsi]# vgreduce vg0 /dev/sda5
Removed "/dev/sda5" from volume group "vg0"
[root@san iscsi]# vgextend vg0 /dev/sda5
Volume group "vg0" successfully extended
4. PE 和LV 大小的關系
In order to limit the Linux kernel memory usage, there is a limit of 65,536 physical extents (PE) per logical volume (LV). Hence, the LVM PE size will directly determine the maximum size of a logical volume (LV)! For example, 4MB PE size (the default PE size) will limit single logical volume (LV) to 256GB, 16MB PE size will limit single LV to grow beyond 1TB, etc.
Beside the PE size, the maximum size of single LV is also limited by CPU architecture and Linux kernel version:
Linux kernel version 2.4.x limit the maximum LV size to 2TB.
Some older Linux kernel prior to 2.4.x, the maximum LV size is limited to 1TB (caused by the integer signedness problems in the block layer).
The combination of 32-bit CPU and Linux kernel version 2.6.x, the limit of logical volume size is maximized at 16TB.
For Linux kernel 2.6.x running on 64-bit CPU, the maximum LV size is 8EB (extremely terrible big storage for this time being!)
這段問題里有如下2個重點信息:
1. PE 的大小決定LV 的容量,默認4M 的PE 最大支持的256G 的LV。 16M的PE 最大支持1TB的LV。
2. 影響LV 容量的還有CPU 架構。 對于32bit 下,Linux 內核為2.6.X的支持16TB,64bit 下,Linux 內核為2.6.x的支持8EB。
所以如果要想支持大量的LV。 那么對在創建LV 的時候,還需要指定PE 的大小。 這個我們在vgcreate命令中加一個-s 參數即可:
[root@san iscsi]# vgremove vg0
Volume group "vg0" successfully removed
[root@san iscsi]# vgcreate -s 16MB vg0 /dev/sda5 /dev/sda6
Volume group "vg0" successfully created
[root@san iscsi]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "vg0" using metadata type lvm2
Device '/dev/sda6' has been left open.
Device '/dev/sda5' has been left open.
[root@san iscsi]# vgdisplay
--- Volume group ---
VG Name vg0
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 192.00 MB
PE Size 16.00 MB -這里我們看到PE 改成16MB了。
Total PE 12
Alloc PE / Size 0 / 0
Free PE / Size 12 / 192.00 MB
VG UUID A9w4ho-3Fdo-ELYl-VRqY-yhaZ-61rt-68sXsi