Uncategorized

HTGWA: Use bcache for SSD caching on a Raspberry Pi

This is a simple guide, part of a series I’ll call ‘How-To Guide Without Ads’. In it, I’m going to document how I set up bcache on a Raspberry Pi, so I could use an SSD as a cache in front of a RAID array.

Getting bcache

bcache is sometimes used on Linux devices to allow a more efficient SSD cache to run in front of a single or multiple slower hard drives—typically in a storage array.

In my case, I have three SATA hard drives: /dev/sda/dev/sdb, and /dev/sdc. And I have one NVMe SSD: /dev/nvme0n1.

created a RAID5 array with mdadm for the three hard drives, and had the raid device /dev/md0.

I then installed bcache-tools:

$ sudo apt-get install bcache-tools

And used make-bcache to create the backing and cache devices:

$ sudo make-bcache -B /dev/md0
UUID:           eb360a2d-4c62-451d-8549-a68621c633e5
Set UUID:       c8b5c63c-0a44-49f3-bb65-cd4df9b751a0
version:        1
block_size:     1
data_offset:        16

$ sudo make-bcache -C /dev/nvme0n1
UUID:           15bf54e9-be21-4478-b676-a08dad937963
Set UUID:       dea419ba-d795-4566-b01f-bb57fa96eb21
version:        0
nbuckets:       15261770
block_size:     1
bucket_size:        1024
nr_in_set:      1
nr_this_dev:        0
first_bucket:       1

Then I tried to look in /sys/block/md0/bcache/ so I could attach the cache to the backing device, but I realized bcache isn’t loaded into the default Raspberry Pi OS kernel… so I’ll have to compile that in.

Getting bcache on Raspberry Pi OS

cross-compiled the Raspberry Pi Linux kernel, and when I did it, during the menuconfig portion, I selected the following option:

> Device Drivers
  > Multiple devices driver support (RAID and LVM)
    > Block device as cache (BCACHE)

I recompiled the kernel and copied my updated kernel to the Pi, then rebooted.

At this point, I could see the bcache0 device was working:

[email protected]:~ $ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sda           8:0    0  3.6T  0 disk  
└─md0         9:0    0  7.3T  0 raid5 
  └─bcache0 254:0    0  7.3T  0 disk  /mnt
sdb           8:16   0  3.6T  0 disk  
└─md0         9:0    0  7.3T  0 raid5 
  └─bcache0 254:0    0  7.3T  0 disk  /mnt
sdc           8:32   0  3.6T  0 disk  
└─md0         9:0    0  7.3T  0 raid5 
  └─bcache0 254:0    0  7.3T  0 disk  /mnt
mmcblk0     179:0    0 14.8G  0 disk  
├─mmcblk0p1 179:1    0  256M  0 part  /boot
└─mmcblk0p2 179:2    0 14.6G  0 part  /
nvme0n1     259:0    0  7.3T  0 disk  

But if I checked on the status of the cache, it said there was no cache:

[email protected]:~ $ cat /sys/block/bcache0/bcache/state
no cache

Attaching the SSD cache to the backing device

Finally, it’s time to attach the SSD cache to the backing device:

$ sudo su  # switch to the root user
# cd /sys/block/md0/bcache/
# echo dea419ba-d795-4566-b01f-bb57fa96eb21 > attach
# cat state 
clean

The UUID in the echo command above comes from the ‘Set UUID’ output from the make-bcache -C command earlier.

Creating a filesystem and mounting

To actually use the device, I formatted it and mounted it to /mnt:

$ sudo mkfs.ext4 -E lazy_itable_init=0,lazy_journal_init=0 /dev/bcache0
$ sudo mount /dev/bcache0 /mnt

To avoid the initialization when making the filesystem, you can omit the -E option entirely. But for RAID arrays I typically let it go full blast on first initialization, because I don’t like relying on ext4lazyinit on a RAID array—it can take days at its reduced rate, and affect RAID performance that whole time!

Getting stats

You can check the stats from bcache with:

$ tail /sys/block/bcache0/bcache/stats_total/*
==> /sys/block/bcache0/bcache/stats_total/bypassed <==
563.1M

==> /sys/block/bcache0/bcache/stats_total/cache_bypass_hits <==
0

==> /sys/block/bcache0/bcache/stats_total/cache_bypass_misses <==
0

==> /sys/block/bcache0/bcache/stats_total/cache_hit_ratio <==
13

==> /sys/block/bcache0/bcache/stats_total/cache_hits <==
132

==> /sys/block/bcache0/bcache/stats_total/cache_miss_collisions <==
0

==> /sys/block/bcache0/bcache/stats_total/cache_misses <==
849

==> /sys/block/bcache0/bcache/stats_total/cache_readaheads <==
0

Switching the caching mode

There are multiple caching modes, including writebackwritethroughwritearound, and none. The most performant (but most dangerous, especially if you’re using a single SSD and not a set of SSDs in RAID 1 for safety) is writeback, which caches reads, and writes data to the SSD first (considering a write ‘complete’ once written to the SSD), then asynchronously copies that data to the backing device.

 

 

 

Load more 2001
Load more 2002
Load more 2003
Load more 2004
Load more 2005
Load more 2006
Load more 2007
Load more 2008
Load more 2009
Load more 2010
Load more 2011
Load more 2012
Load more 2013
Load more 2014
Load more 2015
Load more 2016
Load more 2017
Load more 2018
Load more 2019
Load more 2020
Load more 2021
Load more 2022
Load more 2023
Load more 2024
Load more 2025
Load more 2026
Load more 2027
Load more 2028
Load more 2029
Load more 2030
Load more 2031
Load more 2032
Load more 2033
Load more 2034
Load more 2035
Load more 2036
Load more 2037
Load more 2038
Load more 2039
Load more 2040
Load more 2041
Load more 2042
Load more 2043
Load more 2044
Load more 2045
Load more 2046
Load more 2047
Load more 2048
Load more 2049
Load more 2050
Load more 2051
Load more 2052
Load more 2053
Load more 2054
Load more 2055
Load more 2056
Load more 2057
Load more 2058
Load more 2059
Load more 2060
Load more 2061
Load more 2062
Load more 2063
Load more 2064
Load more 2065
Load more 2066
Load more 2067
Load more 2068
Load more 2069
Load more 2070
Load more 2071
Load more 2072
Load more 2073
Load more 2074
Load more 2075
Load more 2076
Load more 2077
Load more 2078
Load more 2079
Load more 2080
Load more 2081
Load more 2082
Load more 2083
Load more 2084
Load more 2085
Load more 2086
Load more 2087
Load more 2088
Load more 2089
Load more 2090
Load more 2091
Load more 2092
Load more 2093
Load more 2094
Load more 2095
Load more 2096
Load more 2097
Load more 2098
Load more 2099
Load more 2100
Load more 2101
Load more 2102
Load more 2103
Load more 2104
Load more 2105
Load more 2106
Load more 2107
Load more 2108
Load more 2109
Load more 2110
Load more 2111
Load more 2112
Load more 2113
Load more 2114
Load more 2115
Load more 2116
Load more 2117
Load more 2118
Load more 2119
Load more 2120
Load more 2121
Load more 2122
Load more 2123
Load more 2124
Load more 2125
Load more 2126
Load more 2127
Load more 2128
Load more 2129
Load more 2130
Load more 2131
Load more 2132
Load more 2133
Load more 2134
Load more 2135
Load more 2136
Load more 2137
Load more 2138
Load more 2139
Load more 2140
Load more 2141
Load more 2142
Load more 2143
Load more 2144
Load more 2145
Load more 2146
Load more 2147
Load more 2148
Load more 2149
Load more 2150
Load more 2151
Load more 2152
Load more 2153
Load more 2154
Load more 2155
Load more 2156
Load more 2157
Load more 2158
Load more 2159
Load more 2160
Load more 2161
Load more 2162
Load more 2163
Load more 2164
Load more 2165
Load more 2166
Load more 2167
Load more 2168
Load more 2169
Load more 2170
Load more 2171
Load more 2172
Load more 2173
Load more 2174
Load more 2175
Load more 2176
Load more 2177
Load more 2178
Load more 2179
Load more 2180
Load more 2181
Load more 2182
Load more 2183
Load more 2184
Load more 2185
Load more 2186
Load more 2187
Load more 2188
Load more 2189
Load more 2190
Load more 2191
Load more 2192
Load more 2193
Load more 2194
Load more 2195
Load more 2196
Load more 2197
Load more 2198
Load more 2199
Load more 2200
Load more 2201
Load more 2202
Load more 2203
Load more 2204
Load more 2205
Load more 2206
Load more 2207
Load more 2208
Load more 2209
Load more 2210
Load more 2211
Load more 2212
Load more 2213
Load more 2214
Load more 2215
Load more 2216
Load more 2217
Load more 2218
Load more 2219
Load more 2220
Load more 2221
Load more 2222
Load more 2223
Load more 2224
Load more 2225
Load more 2226
Load more 2227
Load more 2228
Load more 2229
Load more 2230
Load more 2231
Load more 2232
Load more 2233
Load more 2234
Load more 2235
Load more 2236
Load more 2237
Load more 2238
Load more 2239
Load more 2240
Load more 2241
Load more 2242
Load more 2243
Load more 2244
Load more 2245
Load more 2246
Load more 2247
Load more 2248
Load more 2249
Load more 2250
Load more 2251
Load more 2252
Load more 2253
Load more 2254
Load more 2255
Load more 2256
Load more 2257
Load more 2258
Load more 2259
Load more 2260
Load more 2261
Load more 2262
Load more 2263
Load more 2264
Load more 2265
Load more 2266
Load more 2267
Load more 2268
Load more 2269
Load more 2270
Load more 2271
Load more 2272
Load more 2273
Load more 2274
Load more 2275
Load more 2276
Load more 2277
Load more 2278
Load more 2279
Load more 2280
Load more 2281
Load more 2282
Load more 2283
Load more 2284
Load more 2285
Load more 2286
Load more 2287
Load more 2288
Load more 2289
Load more 2290
Load more 2291
Load more 2292
Load more 2293
Load more 2294
Load more 2295
Load more 2296
Load more 2297
Load more 2298
Load more 2299
Load more 2300
Load more 2301
Load more 2302
Load more 2303
Load more 2304
Load more 2305
Load more 2306
Load more 2307
Load more 2308
Load more 2309
Load more 2310
Load more 2311
Load more 2312
Load more 2313
Load more 2314
Load more 2315
Load more 2316
Load more 2317
Load more 2318
Load more 2319
Load more 2320
Load more 2321
Load more 2322
Load more 2323
Load more 2324
Load more 2325
Load more 2326
Load more 2327
Load more 2328
Load more 2329
Load more 2330
Load more 2331
Load more 2332
Load more 2333
Load more 2334
Load more 2335
Load more 2336
Load more 2337
Load more 2338
Load more 2339
Load more 2340
Load more 2341
Load more 2342
Load more 2343
Load more 2344
Load more 2345
Load more 2346
Load more 2347
Load more 2348
Load more 2349
Load more 2350
Load more 2351
Load more 2352
Load more 2353
Load more 2354
Load more 2355
Load more 2356
Load more 2357
Load more 2358
Load more 2359
Load more 2360
Load more 2361
Load more 2362
Load more 2363
Load more 2364
Load more 2365
Load more 2366
Load more 2367
Load more 2368
Load more 2369
Load more 2370
Load more 2371
Load more 2372
Load more 2373
Load more 2374
Load more 2375
Load more 2376
Load more 2377
Load more 2378
Load more 2379
Load more 2380
Load more 2381
Load more 2382
Load more 2383
Load more 2384
Load more 2385
Load more 2386
Load more 2387
Load more 2388
Load more 2389
Load more 2390
Load more 2391
Load more 2392
Load more 2393
Load more 2394
Load more 2395
Load more 2396
Load more 2397
Load more 2398
Load more 2399
Load more 2400
Load more 2401
Load more 2402
Load more 2403
Load more 2404
Load more 2405
Load more 2406
Load more 2407
Load more 2408
Load more 2409
Load more 2410
Load more 2411
Load more 2412
Load more 2413
Load more 2414
Load more 2415
Load more 2416
Load more 2417
Load more 2418
Load more 2419
Load more 2420
Load more 2421
Load more 2422
Load more 2423
Load more 2424
Load more 2425
Load more 2426
Load more 2427
Load more 2428
Load more 2429
Load more 2430
Load more 2431
Load more 2432
Load more 2433
Load more 2434
Load more 2435
Load more 2436
Load more 2437
Load more 2438
Load more 2439
Load more 2440
Load more 2441
Load more 2442
Load more 2443
Load more 2444
Load more 2445
Load more 2446
Load more 2447
Load more 2448
Load more 2449
Load more 2450
Load more 2451
Load more 2452
Load more 2453
Load more 2454
Load more 2455
Load more 2456
Load more 2457
Load more 2458
Load more 2459
Load more 2460
Load more 2461
Load more 2462
Load more 2463
Load more 2464
Load more 2465
Load more 2466
Load more 2467
Load more 2468
Load more 2469
Load more 2470
Load more 2471
Load more 2472
Load more 2473
Load more 2474
Load more 2475
Load more 2476
Load more 2477
Load more 2478
Load more 2479
Load more 2480
Load more 2481
Load more 2482
Load more 2483
Load more 2484
Load more 2485
Load more 2486
Load more 2487
Load more 2488
Load more 2489
Load more 2490
Load more 2491
Load more 2492
Load more 2493
Load more 2494
Load more 2495
Load more 2496
Load more 2497
Load more 2498
Load more 2499
Load more 2500

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button