Skip to content

Instantly share code, notes, and snippets.

@kakra
Last active December 17, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakra/5520200 to your computer and use it in GitHub Desktop.
Save kakra/5520200 to your computer and use it in GitHub Desktop.
A very basic ruby script for defragmenting files on btrfs loaded by preload daemon
#!/bin/env ruby
#
# basic concept script for defragmenting files on btrfs loaded by preload daemon
#
# Actually, this idea is working in the spirit of systemd's readahead function which
# can defrag (and relocate?) files on btrfs during boot. Preload takes on where
# readahead stops working, so it should be a great place to continue integrating that
# idea there. This script is actually here, to test the impact of such an idea and
# find first caveats and show-stoppers.
#
# Author: Kai Krakow <hurikhan77@gmail.com>
# License: GPL3
file_already_queued = {}
IO.popen('xargs --no-run-if-empty --max-args=20 --max-procs=3 --null btrfs filesystem defragment -v -clzo', 'w') do |xargs|
File.foreach('/var/lib/preload/preload.state') do |line|
next unless %r{^(EXE|MAP)\s.*?file://(.*)} =~ line
next if file_already_queued[name = $2]
if File.exists?(name)
xargs.print "#{name}\0"
file_already_queued[name] = true
end
end
xargs.flush
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment