给LVM (Thin)添加SSD缓存cache

仅做记录,懒…..

顺便,强烈建议看这个教程,非常详细,为了防止源站关闭文末分享全文:https://www.systutorials.com/docs/linux/man/7-lvmcache/

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
root@JS-2002:~# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdc
  VG Name               R10
  PV Size               7.28 TiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1907199
  Free PE               0
  Allocated PE          1907199
  PV UUID               qPql1T-c6WF-QkN1-1AeM-EQ1r-c0s6-aZwew8
   
  --- Physical volume ---
  PV Name               /dev/sdb3
  VG Name               pve
  PV Size               1.82 TiB / not usable 2.98 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              476671
  Free PE               4096
  Allocated PE          472575
  PV UUID               hon4Me-Jvdj-kwnv-2YDo-IUK4-21ny-Isk4dq
   
root@JS-2002:~# pvcreate /dev/sda
  Physical volume "/dev/sda" successfully created.
root@JS-2002:~# vgextend R10 /dev/sda
  Volume group "R10" successfully extended
root@JS-2002:~# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb3
  VG Name               pve
  PV Size               1.82 TiB / not usable 2.98 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              476671
  Free PE               4096
  Allocated PE          472575
  PV UUID               hon4Me-Jvdj-kwnv-2YDo-IUK4-21ny-Isk4dq
   
  --- Physical volume ---
  PV Name               /dev/sdc
  VG Name               R10
  PV Size               7.28 TiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1907199
  Free PE               0
  Allocated PE          1907199
  PV UUID               qPql1T-c6WF-QkN1-1AeM-EQ1r-c0s6-aZwew8
   
  --- Physical volume ---
  PV Name               /dev/sda
  VG Name               R10
  PV Size               931.51 GiB / not usable 1.71 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              238467
  Free PE               238467
  Allocated PE          0
  PV UUID               Nw1F1C-eNm9-ZWuy-5BaK-Iicf-cVQL-kjzhgs
   
root@JS-2002:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/pve/swap
  LV Name                swap
  VG Name                pve
  LV UUID                JBCaGX-BxRL-ZNfo-wARB-ED2C-HWdD-cGfv6N
  LV Write Access        read/write
  LV Creation host, time proxmox, 2019-05-29 18:10:51 +0800
  LV Status              available
  # open                 2
  LV Size                8.00 GiB
  Current LE             2048
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/pve/root
  LV Name                root
  VG Name                pve
  LV UUID                otXHhw-NRTV-lCup-SFac-5qBg-7HiC-kSkS0v
  LV Write Access        read/write
  LV Creation host, time proxmox, 2019-05-29 18:10:51 +0800
  LV Status              available
  # open                 1
  LV Size                96.00 GiB
  Current LE             24576
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Logical volume ---
  LV Name                data
  VG Name                pve
  LV UUID                31bDzS-XgKA-Bd7R-rf6v-Y2fM-TOY1-EA6csV
  LV Write Access        read/write
  LV Creation host, time proxmox, 2019-05-29 18:10:56 +0800
  LV Pool metadata       data_tmeta
  LV Pool data           data_tdata
  LV Status              available
  # open                 1
  LV Size                1.67 TiB
  Allocated pool data    0.00%
  Allocated metadata     0.05%
  Current LE             437759
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4
   
  --- Logical volume ---
  LV Name                R10
  VG Name                R10
  LV UUID                yF0uLy-b8VJ-Rla8-j94f-4Bp4-zeim-Ze0ZSB
  LV Write Access        read/write
  LV Creation host, time JS-2002, 2019-09-12 18:53:15 +0800
  LV Pool metadata       R10_tmeta
  LV Pool data           R10_tdata
  LV Status              available
  # open                 0
  LV Size                7.28 TiB
  Allocated pool data    0.00%
  Allocated metadata     0.41%
  Current LE             1907139
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:8
   
root@JS-2002:~# lvcreate -n R10Cache -L900G R10 /dev/sda
  Logical volume "R10Cache" created.
root@JS-2002:~# lvcreate -n R10CacheMeta -L100M R10 /dev/sda
  Logical volume "R10CacheMeta" created.
 R10/R10CacheMeta R10/R10Cachepe cache-pool --cachemode writeback --poolmetadata 
  Using 960.00 KiB chunk size instead of default 64.00 KiB, so cache pool has less then 1000000 chunks.
  WARNING: Converting logical volume R10/R10Cache and R10/R10CacheMeta to cache pool's data and metadata volumes with metadata wiping.
  THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)
Do you really want to convert R10/R10Cache and R10/R10CacheMeta? [y/n]: y
  Converted R10/R10Cache to cache pool.
root@JS-2002:~#  lvconvert --type cache --cachepool R10/R10Cache R10/R10
Do you want wipe existing metadata of cache pool volume R10/R10Cache? [y/n]: y
  WARNING: Cached thin pool's data cannot be currently resized and require manual uncache before resize!
  Logical volume R10/R10_tdata is now cached.

附上参考原文:

NAME
lvmcache --- LVM caching
 

DESCRIPTION
An lvm(8) cache Logical Volume (LV) uses a small and fast LV to improve the performance of a large and slow LV. It does this by storing the frequently used blocks on the faster LV. LVM refers to the small fast LV as a cache pool LV. The large slow LV is called the origin LV. Due to requirements from dm-cache (the kernel driver), LVM further splits the cache pool LV into two devices - the cache data LV and cache metadata LV. The cache data LV is where copies of data blocks are kept from the origin LV to increase speed. The cache metadata LV holds the accounting information that specifies where data blocks are stored (e.g. on the origin LV or on the cache data LV). Users should be familiar with these LVs if they wish to create the best and most robust cached LVs. All of these associated LVs must be in the same Volume Group (VG).


 

Cache Terms
origin LV           OriginLV      large slow LV
cache data LV       CacheDataLV   small fast LV for cache pool data
cache metadata LV   CacheMetaLV   small fast LV for cache pool metadata
cache pool LV       CachePoolLV   CacheDataLV + CacheMetaLV
cache LV            CacheLV       OriginLV + CachePoolLV
 

Cache Usage
The primary method for using a cache type LV:

 

0. create OriginLV
Create an LV or identify an existing LV to be the origin LV.

lvcreate -n OriginLV -L LargeSize VG SlowPVs

Example 
# lvcreate -n lvol0 -L 100G vg /dev/slow

 

1. create CacheDataLV
Create the cache data LV. This LV will hold data blocks from the OriginLV. The size of this LV is the size of the cache and will be reported as the size of the cache pool LV.

lvcreate -n CacheDataLV -L CacheSize VG FastPVs

Example 
# lvcreate -n cache0 -L 10G vg /dev/fast

 

2. create CacheMetaLV
Create the cache metadata LV. This LV will hold cache pool metadata. The size of this LV should be 1000 times smaller than the cache data LV, with a minimum size of 8MiB.

lvcreate -n CacheMetaLV -L MetaSize VG FastPVs

Example 
# lvcreate -n cache0meta -L 12M vg /dev/fast

# lvs -a vg
  LV         VG   Attr       LSize   Pool Origin
  cache0     vg   -wi-a-----  10.00g                                                    
  cache0meta vg   -wi-a-----  12.00m                                                    
  lvol0      vg   -wi-a----- 100.00g
 

3. create CachePoolLV
Combine the data and metadata LVs into a cache pool LV. The behavior of the cache pool LV can be set in this step. 
CachePoolLV takes the name of CacheDataLV. 
CacheDataLV is renamed CachePoolLV_cdata and becomes hidden. 
CacheMetaLV is renamed CachePoolLV_cmeta and becomes hidden.

lvconvert --type cache-pool --poolmetadata VG/CacheMetaLV

VG/CacheDataLV
Example 
# lvconvert --type cache-pool --poolmetadata vg/cache0meta vg/cache0

# lvs -a vg
  LV              VG   Attr       LSize   Pool Origin
  cache0          vg   Cwi---C---  10.00g
  [cache0_cdata]  vg   Cwi-------  10.00g
  [cache0_cmeta]  vg   ewi-------  12.00m
  lvol0           vg   -wi-a----- 100.00g
 

4. create CacheLV
Create a cache LV by linking the cache pool LV to the origin LV. The user accessible cache LV takes the name of the origin LV, while the origin LV becomes a hidden LV with the name OriginLV_corig. This can be done while the origin LV is in use. 
CacheLV takes the name of OriginLV. 
OriginLV is renamed OriginLV_corig and becomes hidden.

lvconvert --type cache --cachepool VG/CachePoolLV VG/OriginLV

Example 
# lvconvert --type cache --cachepool vg/cache0 vg/lvol0

# lvs -a vg
  LV              VG   Attr       LSize   Pool   Origin
  cache0          vg   Cwi---C---  10.00g                                                             
  [cache0_cdata]  vg   Cwi-ao----  10.00g                                                             
  [cache0_cmeta]  vg   ewi-ao----  12.00m                                                             
  lvol0           vg   Cwi-a-C--- 100.00g cache0 [lvol0_corig]                                        
  [lvol0_corig]   vg   -wi-ao---- 100.00g                                                             
 

Cache Removal
 

Split a cache pool LV off of a cache LV
A cache pool LV can be disconnected from a cache LV, leaving an unused cache pool LV, and an uncached origin LV. This command writes back data from the cache pool to the origin LV when necessary.

lvconvert --splitcache VG/CacheLV

 

Removing a cache pool LV without removing its linked origin LV
This writes back data from the cache pool to the origin LV when necessary, then removes the cache pool LV, leaving the uncached origin LV.

lvremove VG/CachePoolLV

An alternative command that also disconnects the cache pool from the cache LV, and deletes the cache pool:

lvconvert --uncache VG/CacheLV

Example

# lvs vg
  LV     VG   Attr       LSize   Pool   Origin
  cache0 vg   Cwi---C---  10.00g
  lvol0  vg   Cwi-a-C--- 100.00g cache0 [lvol0_corig]

# lvremove vg/cache0

# lvs vg
  LV    VG   Attr       LSize   Pool Origin
  lvol0 vg   -wi-a----- 100.00g
 

Removing a cache LV: both origin LV and the cache pool LV
Removing a cache LV removes both the origin LV and the linked cache pool LV.

lvremove VG/CacheLV

 

Cache Topics
 

Tolerate device failures in a cache pool LV
Users who are concerned about the possibility of failures in their fast devices that could lead to data loss might consider making their cache pool sub-LVs redundant.

Example

0. Create an origin LV we wish to cache
# lvcreate -L 10G -n lv1 vg /dev/slow

1. Create a 2-way RAID1 cache data LV
# lvcreate --type raid1 -m 1 -L 1G -n cache1 vg \
        /dev/fast1 /dev/fast2

2. Create a 2-way RAID1 cache metadata LV
# lvcreate --type raid1 -m 1 -L 8M -n cache1meta vg \
        /dev/fast1 /dev/fast2

3. Create a cache pool LV combining cache data LV and cache metadata LV
# lvconvert --type cache-pool --poolmetadata vg/cache1meta vg/cache1

4. Create a cached LV by combining the cache pool LV and origin LV
# lvconvert --type cache --cachepool vg/cache1 vg/lv1
 

Cache mode
The default cache mode is "writethrough". Writethrough ensures that any data written will be stored both in the cache pool LV and on the origin LV. The loss of a device associated with the cache pool LV in this case would not mean the loss of any data.

A second cache mode is "writeback". Writeback delays writing data blocks from the cache pool back to the origin LV. This mode will increase performance, but the loss of a device associated with the cache pool LV can result in lost data.

With the --cachemode option, the cache mode can be set when creating a cache LV, or changed on an existing cache LV. The current cache mode of a cache LV can be displayed with the cache_mode reporting option:

lvs -o+cache_mode VG/CacheLV

lvm.conf(5) allocation/cache_mode 
defines the default cache mode.

Example

0. Create an origin LV we wish to cache (yours may already exist)
# lvcreate -L 10G -n lv1 vg /dev/slow

1. Create a cache data LV
# lvcreate -L 1G -n cache1 vg /dev/fast

2. Create a cache metadata LV
# lvcreate -L 8M -n cache1meta vg /dev/fast

3. Create a cache pool LV
# lvconvert --type cache-pool --poolmetadata vg/cache1meta vg/cache1

4. Create a cache LV by combining the cache pool LV and origin LV,
   and use the writethrough cache mode.
# lvconvert --type cache --cachepool vg/cache1 \
        --cachemode writethrough vg/lv1
 

Cache policy
The cache subsystem has additional per-LV parameters: the cache policy to use, and possibly tunable parameters for the cache policy. Three policies are currently available: "smq" is the default policy, "mq" is an older implementation, and "cleaner" is used to force the cache to write back (flush) all cached writes to the origin LV.

The "mq" policy has a number of tunable parameters. The defaults are chosen to be suitable for the majority of systems, but in special circumstances, changing the settings can improve performance.

With the --cachepolicy and --cachesettings options, the cache policy and settings can be set when creating a cache LV, or changed on an existing cache LV (both options can be used together). The current cache policy and settings of a cache LV can be displayed with the cache_policy and cache_settings reporting options:

lvs -o+cache_policy,cache_settings VG/CacheLV

Example

Change the cache policy and settings of an existing cache LV.
# lvchange --cachepolicy mq --cachesettings \
        'migration_threshold=2048 random_threshold=4' vg/lv1
lvm.conf(5) allocation/cache_policy 
defines the default cache policy.

lvm.conf(5) allocation/cache_settings 
defines the default cache settings.

 

Chunk size
The size of data blocks managed by a cache pool can be specified with the --chunksize option when the cache LV is created. The default unit is KiB. The value must be a multiple of 32KiB between 32KiB and 1GiB.

Using a chunk size that is too large can result in wasteful use of the cache, where small reads and writes can cause large sections of an LV to be mapped into the cache. However, choosing a chunk size that is too small can result in more overhead trying to manage the numerous chunks that become mapped into the cache. Overhead can include both excessive CPU time searching for chunks, and excessive memory tracking chunks.

Command to display the cache pool LV chunk size: 
lvs -o+chunksize VG/CacheLV

lvm.conf(5) cache_pool_chunk_size 
controls the default chunk size used when creating a cache LV.

The default value is shown by: 
lvmconfig --type default allocation/cache_pool_chunk_size

 

Spare metadata LV
See lvmthin(7) for a description of the "pool metadata spare" LV. The same concept is used for cache pools.

 

Automatic pool metadata LV
A cache data LV can be converted to cache pool LV without specifying a cache pool metadata LV. LVM will automatically create a metadata LV from the same VG.

lvcreate -n CacheDataLV -L CacheSize VG 
lvconvert --type cache-pool VG/CacheDataLV

 

Create a new cache LV without an existing origin LV
A cache LV can be created using an existing cache pool without an existing origin LV. A new origin LV is created and linked to the cache pool in a single step.

lvcreate --type cache -L LargeSize -n CacheLV

--cachepool VG/CachePoolLV VG SlowPVs
 

Single step cache pool LV creation
A cache pool LV can be created with a single lvcreate command, rather than using lvconvert on existing LVs. This one command creates a cache data LV, a cache metadata LV, and combines the two into a cache pool LV.

lvcreate --type cache-pool -L CacheSize -n CachePoolLV VG FastPVs

 

Convert existing LVs to cache types
When an existing origin LV is converted to a cache LV, the specified cache pool may be a normal LV, rather than a cache pool LV. In this case, lvm will first convert the normal LV to a cache pool LV. A pool metadata LV may optionally be specified.

lvcreate -n OriginLV -L LargeSize VG 
lvcreate -n CacheDataLV -L CacheSize VG 
lvconvert --type cache --cachepool VG/CataDataLV VG/OriginLV

This is equivalent to:

lvcreate -n OriginLV -L LargeSize VG 
lvcreate -n CacheDataLV -L CacheSize VG 
lvconvert --type cache-pool VG/CacheDataLV 
lvconvert --type cache --cachepool VG/CachePoolLV VG/OriginLV

 

Cache metadata formats
There are two disk formats for cache metadata. The metadata format can be specified when a cache pool is created, and cannot be changed. Format 2 has better performance; it is more compact, and stores dirty bits in a separate btree, which improves the speed of shutting down the cache. With auto, lvm selects the best option provided by the current dm-cache kernel target.

lvconvert --type cache-pool --cachemetadataformat auto|1|2

VG/CacheDataLV

 

说点什么

Please Login to comment
avatar
  Subscribe  
提醒