標籤:linux snapshot lvm
1.什麼是LVM
lvm是一個邏輯卷管理工具,它有兩個版本,lvm1 和lvm2. 分別在2.4 kernal和2.6kernal裡原生支援。
LVM提供對磁碟分割更高邏輯層級的管理功能。通過lvm可以重新劃分和移動儲存卷。
LVM可以對邏輯卷自訂命名成development , sales等指定名稱,而不是"sda""sdb"
2.一個案例說明LVM的好處:Joe的PC
As an example: Joe buys a PC with an 8.4 Gigabyte disk on it and installs Linux using the following partitioning system:
/boot /dev/hda1 10 Megabytesswap /dev/hda2 256 Megabytes/ /dev/hda3 2 Gigabytes/home /dev/hda4 6 Gigabytes
當Joe需要安裝office suit時候,root分區的2G空間不夠了。Joe此時只能做如下幾件事情
1.格式化硬碟,重新分區
2.買個新硬碟,建立新分區,挪動資料以完成安裝3.把根分區做個連結,指向到/home,在/home裡完成安裝
如果使用LVM會有什麼不同呢?
/boot /dev/hda1 10 Megabytesswap /dev/vg00/swap 256 Megabytes/ /dev/vg00/root 2 Gigabytes/home /dev/vg00/home 6 Gigabytes
此時當需要安裝office suit,之需要縮小/dev/vg00/home的空間,把空間分配給/root就可以了。
650) this.width=650;" src="http://img.baidu.com/hi/face/i_f42.gif" alt="i_f42.gif" />boot is not included on the LV because bootloaders don‘t understand LVM volumes yet. It‘s possible boot on LVM will work, but you run the risk of having an unbootable system.
Joe的/home目錄滿了,於是他新買了20G的硬碟,需要格式這塊硬碟得到/dev/sdb1 然後把/home裡資料全拷貝過去,接著把新的磁碟掛載成/home,然後把過去的/home中資料做個連結例如/home/joe/old-mp3s
但如果他使用了lvm,就可以直接把磁碟加入存在的卷組,然後直接擴容/home 邏輯卷空間了,不需要做這麼麻煩的事情。
2.LVM的結構(PV VG LV )
hda1 hdc1 (PV:s on partitions or whole disks) \ / \ / diskvg (VG) / | \ / | \ usrlv rootlv varlv (LV:s) | | | ext3 ext4 xfs (filesystems)
VG volume group:
VG 是LVM的最高層級的抽象層面,它把logical volumes和phsical volumes放到一個儲存單元中
PV physical volume:
物理卷,通常是硬碟,或者是看起來像硬碟的raid組
LV logical volume (LV):
類似於磁碟分割,對於系統可視的標準塊裝置,可包含檔案系統例如/home
PE physical extent
每一個物理卷PV被劃分為被稱為PE(Physical Extents)的基本單元,具有唯一編號的PE是可以被LVM定址的最小單元。PE的大小是可配置的,預設為4MB。
logical extent (LE)
LE和PE是對應的定址單位,和同VG裡的PE大小一致。
650) this.width=650;" src="http://img.baidu.com/hi/face/i_f42.gif" alt="i_f42.gif" />一塊硬碟對應LV檔案系統的過程:硬碟→分區→(PV→VG→LV)→格式化(LV為ext檔案系統)→掛載
我們有一個volume group VG1,它的physical extent 大小為 4MB. 在這個 volume group我們引入2個硬碟分區, /dev/hda1 and /dev/hdb1. 這2個分區會變成 physical volumes PV1 and PV2 .PV對應著PE被分割成4MB chunks, 假設PV1分到99個,PV2分到248個。這時我們可以建立邏輯卷。它可以是1-347(248+99)中的任意大小。例如建立了一個邏輯卷LV,實際上做的是LE和PE的映射,例如可以把LE的1映射到PE的51,則意味著對LV的第一個4MB chunk的寫入,實際上是從PV1的第51 chunk開始寫入的。
650) this.width=650;" src="http://img.baidu.com/hi/face/i_f42.gif" alt="i_f42.gif" />映射方式: 兩種
線性映射: 把一組 PE對應到一個地區裡 LE 1 - 99 map to PV1 , LE 100 - 347 map onto PV2
等量對應: 交錯分配
1st chunk of LE[1] -> PV1[1],2nd chunk of LE[1] -> PV2[1],3rd chunk of LE[1] -> PV3[1],4th chunk of LE[1] -> PV1[2],
650) this.width=650;" src="http://img.baidu.com/hi/face/i_f42.gif" alt="i_f42.gif" />Snapshot
快照(snapshot)是LVM所提供的極為美妙的特性。它的原理是“寫時複製(COW - Copy-On-Write)”的技術,複製原始卷的中繼資料(metadata)來建立一個邏輯卷,並沒有複製物理卷上的任何資料, 因此它的建立過程是即時瞬間完成的。快照是特殊類型的邏輯卷,它含有建立時刻所指定的原始邏輯卷的完整資料,您可以操作快照而無需擔心資料的變更令備份失效。
LVM 快照利用一種稱為“寫時複製(COW - Copy-On-Write)”的技術來跟蹤和維持其資料
的一致性。它的原理比較簡單,就是跟蹤原始卷上塊的改變, 在這些資料被改變之前將
其複製到快照自己的預留空間裡(顧名思義稱為寫時複製)。 當對快照進行讀取的時候,
被修改的資料從快照的預留空間中讀取,未修改的資料則重新導向到原始卷上去讀取,因此
在快照的檔案系統與裝置之間多了一層COW裝置。
關於這個快照,雖然看起來還不錯啦,但實際上還有很大的局限性。首要的就是效能
“So a write to a normal LV is just a write, but a write to a snapshotted LV (or an LV snapshot) involves reading the original data, writing it elsewhere and then writing some metadata about it all.
This quite obviously impacts performance, and due to device mapper having a very basic implementation, it is particularly bad. My tests show synchronous sequential writes to a snapshotted LV are around 90% slower than writes to a normal LV.”
多麼痛的領悟!寫入效能下降90%
看個Centos Kernel 2.6.18的測試結果:
#without snapshot:sync; time sh -c "dd if=/dev/zero of=/scratch/moo bs=1M count=1000; sync"1000+0 records in1000+0 records out1048576000 bytes (1.0 GB) copied, 10.3395 seconds, 101 MB/sreal 0m20.285suser 0m0.000ssys 0m1.084s#with snapshot:sync; time sh -c "dd if=/dev/zero of=/scratch/moo bs=1M count=1000; sync"1000+0 records in1000+0 records out1048576000 bytes (1.0 GB) copied, 796.627 seconds, 1.3 MB/sreal 16m44.222suser 0m0.000ssys 0m1.132s
改變chunk size會有一定的效能提升,很多專家建議用8K 16K這些的小單元,減少copy-on-wirte對效能的影響。
出於備份用途,建議不使用活動的快照,而是把快照當成一個凍結點。(對空間比較費)
例如我的資料庫即使在快照時刻也是跑著業務的,那麼我在每天00:01執行一次指令碼,建立一個快照卷,記錄下這個狀態。把快照卷的資料通dd命令或tar進行塊的備份或檔案備份。
可以使用指令碼來實現備份
# vi snapshot_backup.sh; #備份指令碼;------------------------------------------------------------------------------#!/bin/bashtoday=`date "+%Y%m%d"`;lvcreate -L1G -s -n LogVol02s /dev/VolGroup00/LogVol02;mount /dev/VolGroup00/LogVol02s /disk/VolGroup00/LogVol02s;tar -zcvf /tmp/snapshot_backup_$today.tar.gz /disk/VolGroup00/LogVol02s/*;umount /dev/VolGroup00/LogVol02s;lvremove -f /dev/VolGroup00/LogVol02s;------------------------------------------------------------------------------chmod 755 snapshot_backup.sh; #運行使用權限設定;
650) this.width=650;" src="http://img.baidu.com/hi/face/i_f27.gif" alt="i_f27.gif" />
參考文獻:
http://tldp.org/HOWTO/LVM-HOWTO/index.html
https://www.redhat.com/archives/linux-lvm/2013-July/msg00044.html
http://chengkinhung.blogspot.com/2012/09/linuxlvmsnapshot.html
本文出自 “The old artisan” 部落格,轉載請與作者聯絡!
LINUX 邏輯卷管理: LVM