概述
為了滿足在性能和備援等方面的需求,LVM支援了下面三種Logic Volume:
Linear Logic Volume --線性邏輯卷
Striped Logic Volume --條帶化邏輯卷
Mirror Logic Volume --鏡像邏輯卷
我們用lvcreate指令預設建立出來的就是線性邏輯卷,線性邏輯卷使用的PE可以來自一個PV,也可以來自多個PV,一般情況下是先從第一個PV中配置設定PE,如果這個PV的PE已經配置設定完了,再依次從第二個PV、第三個PV裡配置設定。可以通過指定PV甚至PE的号段來讓Linear LV的PE分散到各個PV上,但是如果其中一個PV壞了,那麼這個Linear LV可能也就沒法用了。Linear LV的size可以直接用-L指定大小,也可以用-l指定配置設定PE的個數。往Linear LV中寫入資料時,先往第一個PV的PE中寫,直到第一個PV上配置設定的空間用完了才會将資料寫到第二個PV。
Linear LV隻能滿足彈性配置設定的需求,無法滿足性能和備援的需求,是最普通的volume,但是Linear LV也可以通過lvconvert指令切換成Mirror LV來提供備援能力。
root@hunk-virtual-machine:/home/hunk# lvcreate -l 100 -n linearlv VolGroup1 /dev/sdc:1280-1305 /dev/sdd:1280-1
305 /dev/sde:1280-1305 /dev/sdf:1280-1305
Striped LV的底層存儲布局類似于RAID0,它是跨多個PV的,具體是跨多少個PV用-i指定,但是肯定不能超過VG中PV的數量,Striped LV的最大size取決于剩餘PE最少的那個PV。
Striping的意思是将每個參與Striping的PV劃分成等大小的chunk(也叫做stripe unit),每個PV同一位置的這些chunk共同組成一個stripe。比如下面這張圖(來自于RedHat6官方文檔),包含三個PV,那麼紅色辨別的1、2、3這3個chunk就組成了stripe1,4、5、6組成stripe2。chunk的大小可以通過-I或者--stripesize來指定,但是不能超過PE的大小。
比如,向Striped LV寫入資料時,資料被分成等大小的chunk,然後将這些chunk順序寫入這些PV中。這樣的話就會有多個底層disk drive并發處理I/O請求,可以得到成倍的聚合I/O性能。還是下面這張圖,假如現在有一個4M資料塊需要寫入LV,stripesize設定的512K,LVM把它切成8個chunk,分别辨別為chunk1、chunk2...,這些chunk寫入PV的順序如下:
chunk1寫入PV1
chunk2寫入PV2
chunk3寫入PV3
chunk4寫入PV1
...
因為LVM無法判斷多個Physics Volume是否來自同一個底層disk,如果Striped LV使用的多個Physics Volume實際上是同一個實體磁盤上的不同分區,就會導緻一個資料塊被切成多個chunk分多次發給同一個disk drive,這種情況實際上Striped LV并不能提升性能,反而會使性能下降。是以說,Striped LV提升I/O性能的本質是讓多個底層disk drive并行處理I/O請求,而不是表面上的把I/O分散到了多個PV上。
Striped LV主要滿足性能需求,沒有做任何備援,是以沒有容錯能力,如果單個disk損壞,就會導緻資料損壞。
Mirror LV就是各個PV之間做備援,類似于RAID1,通過-m指定備援數量。Mirror LV提供備援能力,可以有效解決磁盤單點故障問題,但是性能方面沒有幫助。Linear LV和Mirror LV直接用lvconvert工具來互相切換,Mirror LV在建立後也可以更改備援數,具體用法請參考man page。
root@hunk-virtual-machine:/home/hunk# lvcreate -l 100 -m1 -n mirrorvol VolGroup1
Logical volume "mirrorvol" created.
root@hunk-virtual-machine:/home/hunk# lvdisplay /dev/VolGroup1/mirrorvol -m
--- Logical volume ---
LV Path /dev/VolGroup1/mirrorvol
LV Name mirrorvol
VG Name VolGroup1
LV UUID YxgfYi-c7nK-wk4v-rlu1-vRdh-MTMb-uVfl2v
LV Write Access read/write
LV Creation host, time hunk-virtual-machine, 2018-11-29 01:39:44 +0800
LV Status available
# open 0
LV Size 400.00 MiB
Current LE 100
Mirrored volumes 2
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:8
--- Segments ---
Logical extents 0 to 99:
Type raid1
Monitoring monitored
Raid Data LV 0
Logical volume mirrorvol_rimage_0
Logical extents 0 to 99
Raid Data LV 1
Logical volume mirrorvol_rimage_1
Raid Metadata LV 0 mirrorvol_rmeta_0
Raid Metadata LV 1 mirrorvol_rmeta_1
測試Linaer/Striped LV
在測試環境中添加了4個virtual disk,size都是10GB。
先用這4個virtual disk建立一個type為linear,size為20GB的Linaer LV,從後面查詢LV的詳情可以看出,這個LV實際上跨了3個PV。
root@hunk-virtual-machine:/home/hunk# pvcreate /dev/sd[cdef]
Physical volume "/dev/sdc" successfully created
Physical volume "/dev/sdd" successfully created
Physical volume "/dev/sde" successfully created
Physical volume "/dev/sdf" successfully created
root@hunk-virtual-machine:/home/hunk# vgcreate VolGroup1 /dev/sd[cdef]
Volume group "VolGroup1" successfully created
root@hunk-virtual-machine:/home/hunk# lvcreate -L 20G -n linnervol VolGroup1
Logical volume "linnervol" created.
root@hunk-virtual-machine:/home/hunk# mkfs.ext4 /dev/VolGroup1/linnervol
root@hunk-virtual-machine:/home/hunk# mount /dev/VolGroup1/linnervol /volumetest
root@hunk-virtual-machine:/home/hunk# df -h |grep linnervol
/dev/mapper/VolGroup1-linnervol 20G 44M 19G 1% /volumetest
現在,用bonnie++來模拟IO,不停地向這個LV中寫入資料。
root@hunk-virtual-machine:/volumetest# bonnie++ -n 0 -u 0 -r `free -m | grep 'Mem:' | awk '{print $2}'` -s $(echo "scale=0;`free -m | grep 'Mem:' | awk '{print $2}'`*2" | bc -l) -f -b -d /volumetest/
Using uid:0, gid:0.
Writing intelligently...
在新視窗用bwm-ng來監控4個磁盤的IO速率,我們發現隻有sdc上面有I/O請求,然而其他disk都很空閑,看着sdc一個家夥在忙。
bwm-ng v0.6 (probing every 0.500s), press 'h' for help
input: disk IO type: rate
\ iface Rx Tx Total
==============================================================================
sdc: 0.00 KB/s 12263.47 KB/s 12263.47 KB/s
sdd: 0.00 KB/s 0.00 KB/s 0.00 KB/s
sde: 0.00 KB/s 0.00 KB/s 0.00 KB/s
sdf: 0.00 KB/s 0.00 KB/s 0.00 KB/s
------------------------------------------------------------------------------
total: 0.00 KB/s 12263.47 KB/s 12263.47 KB/s
我們繼續檢視LV中寫入資料的量,直到寫入資料超過10G時,發現sdc已經不再處理I/O請求了,因為資料已經塞滿了嘛。而sdd開始繼續處理持續的I/O請求。在寫入資料10G多一點的時候,中間實際上有個過度過程,就是sdc和sdf都在處理I/O,這個是因為緩沖造成的。
root@hunk-virtual-machine:/home/hunk# df -h |grep linner
/dev/mapper/VolGroup1-linnervol 20G 11G 8.1G 57% /volumetest
| iface Rx Tx Total
sdc: 0.00 KB/s 0.00 KB/s 0.00 KB/s
sdd: 0.00 KB/s 12263.47 KB/s 12263.47 KB/s
移除前面使用的Linear LV
root@hunk-virtual-machine:/home# lvremove /dev/VolGroup1/linnervol
Do you really want to remove and DISCARD active logical volume linnervol? [y/n]: y
Logical volume "linnervol" successfully removed
建立一個條帶化的LV
root@hunk-virtual-machine:/home# lvcreate -L 20G --stripes 4 --stripesize 256 --name stripevol VolGroup1
WARNING: ext4 signature detected on /dev/VolGroup1/stripevol at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/VolGroup1/stripevol.
Logical volume "stripevol" created.
root@hunk-virtual-machine:/home# lvdisplay /dev/VolGroup1/stripevol -m
LV Path /dev/VolGroup1/stripevol
LV Name stripevol
LV UUID z0MGOg-g6JL-hiE8-9Gt0-RZAJ-K29m-I6tcrS
LV Creation host, time hunk-virtual-machine, 2018-11-27 01:45:41 +0800
LV Size 20.00 GiB
Current LE 5120
- currently set to 4096
Block device 252:6
Logical extents 0 to 5119: #Striped LV映射的PE均勻分布在了4個PV上
Type striped
Stripes 4
Stripe size 256.00 KiB
Stripe 0:
Physical volume /dev/sdc
Physical extents 0 to 1279
Stripe 1:
Physical volume /dev/sdd
Stripe 2:
Physical volume /dev/sde
Stripe 3:
Physical volume /dev/sdf
root@hunk-virtual-machine:/home# mkfs.ext4 /dev/VolGroup1/stripevol
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 5242880 4k blocks and 1310720 inodes
Filesystem UUID: 51dbdea0-48fc-4324-9974-42443e424aa0
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
root@hunk-virtual-machine:/home# mount /dev/VolGroup1/stripevol /volumetest/
root@hunk-virtual-machine:/home# df -h |grep stripe
/dev/mapper/VolGroup1-stripevol 20G 44M 19G 1% /volumetest
用同樣的方法測試這個條帶化的LV,不過這裡的測試比較粗糙,不僅忽略了很多測試要素,前面對Linear LV的測試中bwm的I/O速率是每0.5s一次的采樣值,而這裡Striped LV的取的I/O速率是30s内的均值。不過我們這裡并不是想得到準确的I/O速率,就先不考慮這些因素吧。明顯能看出來4個disk在并行的處理I/O請求,也就是給Striped LV的I/O請求最終被分散到了多個底層disk上面,這樣聚合的I/O效率必然會高出好幾倍。
input: disk IO type: avg (30s) --取30S内采用的均值
/ iface Rx Tx Total
sdc: 0.13 KB/s 10010.92 KB/s 10011.05 KB/s
sdd: 0.00 KB/s 10174.32 KB/s 10174.32 KB/s
sde: 0.00 KB/s 6563.85 KB/s 6563.85 KB/s
sdf: 0.00 KB/s 6113.09 KB/s 6113.09 KB/s
total: 0.13 KB/s 32862.18 KB/s 32862.32 KB/s