henrydenhengst

Using Bcache to Soup Up Your SATA Drives

Aug 8th, 2014
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.06 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Most Linux users that employ Solid State Drives (SSDs) build their systems with the smaller SSDs holding the
  4. # operating system and the larger, slower, much cheaper HDD drives holding their data. When you're trying to create
  5. # the fastest system possible, though, you want to get that data drive up to the speeds of your far-faster SSD.
  6. # Problem is, very large SSDs can be cost-prohibitive. So, what can you do? With the help of bcache, you can use a
  7. # smaller SSD as a caching drive for the larger, slower HDD. With this set up, you have the best of both worlds.
  8. #
  9. # A lightning fast SSD housing your operating system.
  10. # A bcache-enabled SSD that caches for your large HDD data drive.
  11. # As of kernel 3.10, bcache is available with minimal installation and work. I will walk you through the process
  12. # of getting the bcache data “drive” up and running (assuming you already have a working Linux system running on
  13. # your faster SSD drive).
  14. #
  15. # Although you can set this up on an existing system (with existing data), I highly recommend you not do so. The
  16. # chance of losing data is high, so let's start out with a fresh SSD (for caching) and a fresh HDD (for storage –
  17. # or “backing”).
  18. #
  19. # I am going to illustrate this process using a working installation of Ubuntu 13.10, with a smaller SSD drive
  20. # (an inexpensive 60 GB drive will work fine) and a much larger HDD drive (as large as necessary) for backing.
  21. # Let's begin.
  22. #
  23. # Install the tools
  24. #
  25. # The first thing you must do is install bcache-tools. This is the tool that will create and register the block
  26. # devices to work with bcache. To install bcache-tools you must first add the repository. This is done with the
  27. # following commands:
  28. #
  29. sudo add-apt-repository ppa:g2p/storage
  30. sudo apt-get update
  31. sudo apt-get install bcache-tools
  32. #
  33. # Create and register the devices
  34. #
  35. # Before you set about creating your bcache devices, format both drives to the ext4 file system
  36. # (I prefer using Gparted for this). Once the devices are formatted, you can create and register the devices
  37. # with the help of bcache-tools. Let's say the caching device (SSD) is located at /dev/sdc and the backing device
  38. # (HDD) is located at /dev/sdb. First, create the backing device with the command:
  39. #
  40. sudo make-bcache -B /dev/sdb1
  41. #
  42. # Now, create the caching device with the command:
  43. #
  44. sudo make-bcache -C /dev/sdc1
  45. #
  46. # If you get an error that there are already non-bcache superblocks on the device(s), you have to remove
  47. # those errors with the wipefs command, as such:
  48. #
  49. sudo wipefs -a /dev/sdb1
  50. #
  51. # You might have to do that for both the caching and backing devices.
  52. #
  53. # With distributions that use udev (such as Ubuntu), you can skip the next two steps.
  54. # If your distribution doesn't use udev, you have to register the devices with the kernel.
  55. # We'll do those one at a time with the commands:
  56. #
  57. sudo echo /dev/sdb1 > /sys/fs/bcache/register
  58. sudo echo /dev/sdc1 > /sys/fs/bcache/register
  59. #
  60. # Once the backing device is registered, it will show up in /dev in the form of /dev/bcacheX
  61. # (Where X is a number – such as /dev/bcache0). The registering can either be done manually
  62. # (you'd have to re-register each time you reboot), or you can set it for auto-register with an init script.
  63. # The contents of that init script would need to be:
  64. #
  65. echo /dev/sd* > /sys/fs/bcache/register_quiet
  66. #
  67. # This will look for and register bcache superblocks and ignore everything else.
  68. #
  69. # Now it's time to mount the newly created file system. This is done with the mount command:
  70. #
  71. sudo mount /dev/bcacheX /path/to/mount/point
  72. #
  73. # Where X is the device to be mounted (most likely bcache0) and /path/to/mount/point is the actual path you want
  74. # to mount the device to. For example, you could create a DATA folder on / and then mount bcache0 there:
  75. #
  76. mount /dev/bcache0 /DATA
  77. #
  78. # NOTE: Make sure the user that will want to write data to the caching device has permission to write to the
  79. # DATA directory.
  80. #
  81. # At this point, the cache set will show up in /sys/fs/bcache/ as a UUID (a UUID will be a long string of characters).
  82. # You will need that UUID for the next command.
  83. #
  84. # Attach the devices
  85. #
  86. # The devices are now created and registered. You now have to attach the caching and backing devices to enable
  87. # the caching feature. Here you will need the UUID (a long string of characters) found in /sys/fs/bcache/
  88. # (enter the command: ls /sys/fs/bcache and you'll see the UUID). To attach the devices, you simply use the
  89. # echo command to add the UUID to the attach file in /sys/block/bcache0/bcache/. The command is:
  90. #
  91. echo UUID > /sys/block/bcache0/bcache/attach
  92. #
  93. # Where UUID is the actual UUID found in /sys/fs/bcache.
  94. #
  95. # Your bcache system is now ready to use. Simply write your data to the /DATA directory  
  96. # and you should enjoy much faster data writes.
  97. #
  98. bcache screen shot
  99. #
  100. # The caching device mounted and ready to use.
  101. #
  102. #
  103. # To check to see if the caching is working, open up a terminal window and issue the command:
  104. #
  105. tail /sys/block/bcache0/bcache/stats_total/*
  106. #
  107. # Enable writeback caching
  108. #
  109. # By default, bcache uses writethrough caching. With writethrough, only reads are cached and writes are written
  110. # directly to the backing drive. You can gain some serious speed by enabling writeback caching. Be warned,
  111. # however, this is not nearly as reliable as the writethrough mode. You can lose data should there be a power
  112. # outtage or if the SSD fails. But if you long for that extra bump of speed, you can enable writeback caching
  113. # by issuing the following command:
  114. #
  115. echo writeback > /sys/block/bcache0/bcache/cache_mode
  116. #
  117. # Again, I will caution you that using writeback mode is not as reliable as writethrough.
  118. #
  119. # If you're looking for a way to squeeze as much speed from your machines as possible, bcache might be exactly
  120. # what you need. With the right hardware, and just a little time, you can get those older, slower HDD drives
  121. # writing data as if they were solid state drives. But do remember, always back up your data before you attempt
  122. # any work of this nature.
  123. #
Advertisement
Add Comment
Please, Sign In to add comment