天天看點

Linux LVM條帶化

一、什麼是條帶化

  當多個程序同時通路一個磁盤時,可能會出現磁盤沖突。磁盤系統對通路次數(每秒的IO操作,IOPS)和資料傳輸速率(讀寫速率,TPS)有限制。

當達到這些限制時,後面需要通路磁盤的程序就需要挂起等待,這就是磁盤沖突。避免磁盤沖突是優化I/O性能的一個重要目标。

  條帶化技術是一種自動的将I/0負載均衡到多個實體磁盤上的技術。條帶化技術将一塊連續的資料分成很多小部分,并把他們分别存儲到不同的磁盤上去。

這樣就能使多個程序同時通路資料的多個不同部分而不會造成磁盤沖突,最大化I/0性能。

  LVM的條帶化:為了性能考慮,将資料跨越多個磁盤存儲,即把LV上連續的資料分成大小相同的塊,然後依次存儲在各個磁盤PV上,類似于RAID0的資料存

放形式,實作資料讀寫的并發;管理者一句自己的資料需求,定義資料分塊大小,分布PV磁盤個數資訊,進而實作讀寫性能最佳化。

 粗略來講,條帶化strip就是raid0。

二、示意圖

  如下是條帶化示意圖(raid0)和鏡像的條帶化(raid01)

Linux LVM條帶化

一般以LVM管理的存儲,一個vg中可能會有很多pv,同樣的,一個lv可能跨越多塊pv,為了使硬碟存儲速度加快,就會用到條帶化的技術,即把連續的資料分成大小相同的資料塊,然後依次存儲在各個pv上。類似于RAID0,使存儲速度加快。但并不會使資料像RAID0一樣危險容易丢失,因為在正式使用中,不會像此時做測試一樣沒有任何保障地将多塊硬碟做成一個vg,而是普遍連接配接的背景存儲,在劃分LUN之前,已經在實體硬碟上做好RAID5或RAID1,在RAID5或RAID1的基礎上再劃分出多塊LUN,即系統上的pv,即使pv所在硬碟損壞,但有底層的硬RAID備援,并不會丢失資料。

條帶單元大小:即條帶化的LV中,每一個條帶單元的大小,對應于I/O中資料塊的大小。對于Oracle來講,db_block_size即設定的資料塊大小。而db_file_multiblock_read_count就一次讀取時最多并行的資料塊的個數,db_block_size和db_file_multiblock_read_count相乘即一次總的I/O大小。這個大小不能超過作業系統的最大I/O (max_io_size)值。在ORACLE應用中,lv條帶的大小一般設定為兩倍或兩倍以上的Oracle塊大小,因為假如設定成與Oracle資料塊相同大小,無法保證Oracle資料塊的邊界正好與條帶單元的邊界對應,如果不對應的話,就會出現大量的一個I/O由兩個條帶單元,來處理的情況。

條帶大小的原則:對于高并發并且IO請求小的情況下,一塊實體硬碟處理多個I/O請求,低并發但I/O請求較大時,可能需要多塊硬碟處理一個I/O請求。原則上的要求是一次I/O請求能被一次性處理完成。

大概的條帶化的概念就是這樣。

先看本機中的VG情況,隻有一個vg00,實體硬碟個數是從/dev/sdd到/dev/sdi一共6塊。

将每塊硬碟做為一個PV,先全部執行完成。為了一會做lvextend的測試,先用前三塊硬碟建立vg01

再建立條帶化的lv,下面用到的lvextend的參數如下:

-i:此處寫lv用到的pv的數量,不能超過所在vg的pv數量,一般設定與vg的pv個數相同

-I:條帶單元大小,機關Kb

-L:lv的大小,預設為Mb,可帶機關G,M,K

-l:小寫L,配置設定給lv的LE個數,對應于VG中的PE,在上條vgdisplay的輸出中可看到VG中一共有1533個PE。

-n:自定義lv的名字,預設從lvol0開始往下排。

為了下面測試條帶化下的lvextend,是以将此vg的所有空間都給這個lv,即1533個LE,一共5.99G的可用空間。

執行格式化和挂載

然後測試給此條帶化的lv擴容,先vgextend,再lvextend.

條帶化的lv擴充需要新增pv的時候,有個重要條件,增加的pv數量必須與lv現有的pv數量相同或成倍數關系。想想RAID0的原理就知道了,抽象地說,資料分成大小相同的資料塊,然後依次存儲在每塊硬碟,如果要擴大,必然要每塊硬碟都相應擴大。此處先隻增加一塊硬碟看是否可以。

增加硬碟後,可以看到vg size變大為7.98G,并且PE數量變為2044,pv數量變為4個。

因為有2G的可用空間,此處試着增加100M,但是結果是失敗的,提示不夠用。

然後再以LE的方式增加,一共有511個可用的PE,即最大應該可增加511個LE,此處隻增加10個仍然失敗,提示需要額外的9個。

看起來增加1個應該可以,每次執行也都提示成功。但是每次都提示增加到1534個LE。

然後以lvdisplay檢視,LE的數量仍為1533,并未增加。至于為何會顯示增加1個成功,就不曉得了~~~但從結果知道,其實并沒有增加

再把剩餘的另外2個pv加上,就一共增加了3個pv,正好與vg01的原pv數量相同,成倍數關系。此時vg01的pv個數變成了6個,是原來的2倍。

再用lvextend擴充空間,分别從LE和SIZE的角度擴充,均提示成功。

檢視大小

To increase the size of a logical volume, use the ​<code>​ lvextend​</code>​ command.

When you extend the logical volume, you can indicate how much you want to extend the volume, or how large you want it to be after you extend it.

The following command extends the logical volume ​<code>​ /dev/myvg/homevol​</code>​ to 12 gigabytes.

The following command adds another gigabyte to the logical volume ​<code>​/dev/myvg/homevol​</code>​.

As with the ​<code>​lvcreate​</code>​ command, you can use the ​<code>​-l​</code>​ argument of the ​<code>​lvextend​</code>​ command to specify the number of extents by which to increase the size of the logical volume. You can also use this argument to specify a percentage of the volume group, or a percentage of the remaining free space in the volume group. The following command extends the logical volume called ​<code>​testlv​</code>​ to fill all of the unallocated space in the volume group ​<code>​myvg​</code>​.

After you have extended the logical volume it is necessary to increase the file system size to match.

By default, most file system resizing tools will increase the size of the file system to be the size of the underlying logical volume so you do not need to worry about specifying the same size for each of the two commands.

In order to increase the size of a striped logical volume, there must be enough free space on the underlying physical volumes that make up the volume group to support the stripe. For example, if you have a two-way stripe that that uses up an entire volume group, adding a single physical volume to the volume group will not enable you to extend the stripe. Instead, you must add at least two physical volumes to the volume group.

For example, consider a volume group ​<code>​vg​</code>​ that consists of two underlying physical volumes, as displayed with the following ​<code>​vgs​</code>​ command.

You can create a stripe using the entire amount of space in the volume group.

Note that the volume group now has no more free space.

The following command adds another physical volume to the volume group, which then has 135G of additional space.

At this point you cannot extend the striped logical volume to the full size of the volume group, because two underlying devices are needed in order to stripe the data.

To extend the striped logical volume, add another physical volume and then extend the logical volume. In this example, having added two physical volumes to the volume group we can extend the logical volume to the full size of the volume group.

If you do not have enough underlying physical devices to extend the striped logical volume, it is possible to extend the volume anyway if it does not matter that the extension is not striped, which may result in uneven performance. When adding space to the logical volume, the default operation is to use the same striping parameters of the last segment of the existing logical volume, but you can override those parameters. The following example extends the existing striped logical volume to use the remaining free space after the initial ​<code>​lvextend​</code>​ command fails.