Linux

如何確定 LVM 快照的最佳大小?

  • April 19, 2013

我想創建一個在備份期間使用 LVM 快照的備份腳本。該腳本將在多台 Linux 機器上執行。我想避免創建太小並且在備份期間已滿的快照。我只有捲大小、該卷上已用空間和可用空間的百分比作為輸入參數。在此卷上備份期間,我沒有平均磁碟 I/O 速度和更改的數據量。有沒有辦法根據這種情況計算卷的最佳快照大小?創建快照的可用空間是有限的。

請記住,您可以隨時調整快照的大小,例如使用 lvextend。因此,您可以給它們一個合理的初始大小,然後在它們變得太滿時將它們增大。

這甚至可以自動完成:使用 dmeventd 並在 lvm.conf 中進行設置:

   # 'snapshot_autoextend_threshold' and 'snapshot_autoextend_percent' define
   # how to handle automatic snapshot extension. The former defines when the
   # snapshot should be extended: when its space usage exceeds this many
   # percent. The latter defines how much extra space should be allocated for
   # the snapshot, in percent of its current size.
   #
   # For example, if you set snapshot_autoextend_threshold to 70 and
   # snapshot_autoextend_percent to 20, whenever a snapshot exceeds 70% usage,
   # it will be extended by another 20%. For a 1G snapshot, using up 700M will
   # trigger a resize to 1.2G. When the usage exceeds 840M, the snapshot will
   # be extended to 1.44G, and so on.
   #
   # Setting snapshot_autoextend_threshold to 100 disables automatic
   # extensions. The minimum value is 50 (A setting below 50 will be treated
   # as 50).

   snapshot_autoextend_threshold = 50
   snapshot_autoextend_percent = 50

自動擴展不會立即起作用,dmeventd 需要幾秒鐘的時間才能做出反應……而且 50% 的填充等級和 50% 的增長非常苛刻,但是對於使用非常小的快照進行測試(從而快速用數據填充快照),它們是需要的。

# lvcreate -n TEST_LV -L 1G /dev/base_vg
 Logical volume "TEST_LV" created
# mke2fs -t ext4 /dev/base_vg/TEST_LV
mke2fs 1.42.5 (29-Jul-2012)
[...]
Writing superblocks and filesystem accounting information: done
# mount /dev/base_vg/TEST_LV /mnt

無需root即可寫入文件

# cd /mnt
# chown USER .
#

$ for i in 1 2 3 4 5 6 7 8 9 10 11 12 ; do 
   dd if=/dev/urandom bs=1024k count=10 > /mnt/File$i
 done
$

# lvcreate -n TEST_LV-SNAP -s /dev/base_vg/TEST_LV -L 25M
 Rounding up size to full physical extent 28.00 MiB
 Logical volume "TEST_LV-SNAP" created
# lvs /dev/base_vg/TEST_LV-SNAP; \
 while true; do 
     lvs /dev/base_vg/TEST_LV-SNAP | 
         grep -v Origin
     sleep 1
 done | uniq 

當它執行時,開始

$ for i in 1 2 3 4 5 6 7 8 9 10 11 12 ; do
     dd if=/dev/urandom bs=1024k count=10 > /mnt/File$i
     sleep 10
 done

寫作中的睡眠需要讓 dmeventd 趕上 — IIRC 它每 10 秒檢查一次。

回到我們的輸出:

 LV                        VG      Attr     LSize  Pool Origin           Data%  Move Log Copy%  Convert
 TEST_LV-SNAP base_vg swi-a-s- 28.00m      TEST_LV   0.04                        
 TEST_LV-SNAP base_vg swi-a-s- 28.00m      TEST_LV   0.04                        
 TEST_LV-SNAP base_vg swi-a-s- 28.00m      TEST_LV  35.90                        
 TEST_LV-SNAP base_vg swi-a-s- 28.00m      TEST_LV  36.01                        
 TEST_LV-SNAP base_vg swi-a-s- 28.00m      TEST_LV  71.86                        
 TEST_LV-SNAP base_vg swi-a-s- 44.00m      TEST_LV  45.82                        
 TEST_LV-SNAP base_vg swi-a-s- 44.00m      TEST_LV  68.63                        
 TEST_LV-SNAP base_vg swi-a-s- 68.00m      TEST_LV  44.46                        
 TEST_LV-SNAP base_vg swi-a-s- 68.00m      TEST_LV  59.22                        
 TEST_LV-SNAP base_vg swi-a-s- 104.00m      TEST_LV  38.75                        
 TEST_LV-SNAP base_vg swi-a-s- 104.00m      TEST_LV  48.40                        
 TEST_LV-SNAP base_vg swi-a-s- 104.00m      TEST_LV  48.43                        
 TEST_LV-SNAP base_vg swi-a-s- 156.00m      TEST_LV  38.74                        
 TEST_LV-SNAP base_vg swi-a-s- 156.00m      TEST_LV  45.17                        
 TEST_LV-SNAP base_vg swi-a-s- 156.00m      TEST_LV  45.19                        
 TEST_LV-SNAP base_vg swi-a-s- 156.00m      TEST_LV  51.63                        
 TEST_LV-SNAP base_vg swi-a-s- 156.00m      TEST_LV  51.65                        
 TEST_LV-SNAP base_vg swi-a-s- 236.00m      TEST_LV  34.14                        
 TEST_LV-SNAP base_vg swi-a-s- 236.00m      TEST_LV  38.39                        
 TEST_LV-SNAP base_vg swi-a-s- 236.00m      TEST_LV  38.40                        
 TEST_LV-SNAP base_vg swi-a-s- 236.00m      TEST_LV  42.66                        
 TEST_LV-SNAP base_vg swi-a-s- 236.00m      TEST_LV  42.67                        
 TEST_LV-SNAP base_vg swi-a-s- 236.00m      TEST_LV  46.92                        
 TEST_LV-SNAP base_vg swi-a-s- 236.00m      TEST_LV  46.94                        
 TEST_LV-SNAP base_vg swi-a-s- 236.00m      TEST_LV  51.19                        
 TEST_LV-SNAP base_vg swi-a-s- 236.00m      TEST_LV  51.20                        
 TEST_LV-SNAP base_vg swi-a-s- 356.00m      TEST_LV  33.94                        

看著它長大…

引用自:https://serverfault.com/questions/497346