ssd disk cache

最後更新: 2016-04-05



Use an SSD as a cache for another blockdevice


* next generation: Bcachefs

* Bcache needs the backing device to be formatted as a bcache block device

* can be attached and detached at runtime, while mounted and in use

* Bcache is meant to be a superior alternative to battery backed up raid controllers, thus it must be reliable even if the power cord is yanked out.

* Detects and bypasses sequential IO (with a configurable threshold, and can be disabled) <= Backups and large file copies should thus entirely bypass the cache.

Operating mode:

read/write cache (in writeback mode)

read cache (writethrough or writearound)






apt-get install bcache-tools


  • /usr/sbin/make-bcache
  • /usr/sbin/bcache-super-show




# wipe partition

# wipe a filesystem signature from a device
# -a = Erase all available signatures.

wipefs -a /dev/sdy1

# Create a backing device

make-bcache -B /dev/sdx1

-B     Create a backing device

UUID:                   47d9e9b6-66dc-44c8-9569-2372589f4b15
Set UUID:               eb3c2c9a-0a22-4d63-b4ed-82a2a78d7c06
version:                1
block_size:             1
data_offset:            16

# Create a cache device

make-bcache -C /dev/sdy1

-C     Create a cache

UUID:                   84a2454f-eebe-416d-bd98-e5211dc890c8
Set UUID:               a77b7bf7-4dd6-4cf0-9f0e-c5ea61cac589
version:                0
nbuckets:               47694
block_size:             1
bucket_size:            1024
nr_in_set:              1
nr_this_dev:            0
first_bucket:           1

# More Info.

# The bucket size should match the erase block size of the caching device (SSD)

# cache hits are counted per bucket (thus a smaller bucket size will give better cache utilization,  but  poorer  write  performance.)

make-bcache -b 512 -C /dev/sdy2

# 當 bcache0 沒有在 /dev 出現時

# Without udev, you can manually register devices like this:

echo /dev/mapper/myvg-admin_data > /sys/fs/bcache/register

# 為 bcache0 加入 cache

# Register the cache device against your backing device
#  find the cache set UUID: bcache-super-show /dev/sdb1 | grep cset.uuid

echo cset.uuid > /sys/block/bcache0/bcache/attach

# 設定 cache mode

# Change your cache mode
# writethrough [writeback] writearound none

echo writeback > /sys/block/bcache0/bcache/cache_mode

# Check status

cat /sys/block/bcache0/bcache/state

Output :

no cache: this means you have not attached a caching device to your backing bcache device

clean: this means everything is ok. The cache is clean.

dirty: this means everything is setup fine and that you have enabled writeback and that the cache is dirty.

inconsistent: you are in trouble because the backing device is not in sync with the caching device


Stop cache


# Stop Cache

echo 1 > /sys/block/bcache0/bcache/stop

# Safely remove the cache device

echo <cache-set-uuid> > /sys/block/bcache0/bcache/detach

# Release backend devices

echo 1 > /sys/fs/bcache/<cache-set-uuid>/stop

# Remark - Checking Device

ll /dev/bcache*


/dev/bcacheN 沒有出現


If a backing device has data in a cache somewhere,
the /dev/bcache# device won't be created until the cache shows up
particularly important if you have writeback caching turned on.

If you're booting up and your cache device is gone and never coming back,
you can force run the backing device:

# disk

echo 1 > /sys/block/sdb/bcache/running

If there was dirty data in the cache, don't expect the filesystem to be recoverable
you will have massive filesystem corruption

# lvm backing device 找不到 cache device

echo 1  > /sys/devices/virtual/block/dm-6/bcache





Writing to this file resets the running total stats (not the day/hour/5 minute decaying versions).


Amount of IO (both reads and writes) that has bypassed the cache




Amount of dirty data is in the cache





Size of readahead that should be performed.  Defaults to 0.

If set to e.g. 1M, it will round cache miss reads up to that size, but without overlapping existing cache entries.


If nonzero, bcache tries to keep around this percentage of the cache dirty by
throttling background writeback and using a PD controller to smoothly adjust the rate.

Remark: dirty data is data in the cache that has not been written to the backing device.

  When dirty data is written to the cache and it previously did not contain
  any, waits some number of seconds before initiating writeback. Defaults to


  Rate in sectors per second - if writeback_percent is nonzero, background
  writeback is throttled to this rate. Continuously adjusted by bcache but may
  also be set by the user.


# 只 Cache 4M sequential IO

echo 4M > /sys/block/bcache0/bcache/sequential_cutoff


Other Opts



Writing to this file forces garbage collection to run.


Boolean; if on a discard/TRIM will be issued to each bucket before it is reused.

Defaults to off, since SATA TRIM is an unqueued command (and thus slow).


Other Usage


# Force flush of cache to backing device

echo 0 > /sys/block/bcache0/bcache/writeback_percent

echo 10 > /sys/block/bcache0/bcache/writeback_percent


Shutdown / Device removal


When the system is shut down the cache stays dirty.

That means that the backing device is not safe to be separated from the caching device unless they are first manually detached

or cache is switched to writethrough.




bcache-super-show /dev/myvg/thunder

sb.magic                ok
sb.first_sector         8 [match]
sb.csum                 8397886606A75AA0 [match]
sb.version              1 [backing device]

dev.label               (empty)
dev.uuid                c669f814-ade5-4955-974e-490156e13200
dev.sectors_per_block   1
dev.sectors_per_bucket  1024   16     0 [writethrough]    0 [detached]

cset.uuid               92820d0f-3ca2-4b6f-bd8b-8511c43daaa0


找出 cache device 及 backend device


# bcache device 指向那 Backend device

ls -l /sys/block/bcache0/bcache

# output

/sys/block/bcache0/bcache -> ../dm-5/bcache

# 那 bcache device 的 Cache  device

ls -l /sys/block/bcache0/bcache/cache

# output

/sys/block/bcache0/bcache/cache -> ../../../../../fs/bcache/a77b7bf7-4dd6-4cf0-9f0e-c5ea61cac589




# 查看系統當前有幾多個 bcache device

lsmod | grep bcache

bcache                217088  1