I’ve searched for a solution to reduce the IO impact of btrfs scrub for some time, since ionice doesn’t seem to have much effect. Some time ago, I created a systemd slice for background things like defragmentation, and today I also tried running scrub in that slice. It worked very well, the scrub adhered to the cgroup limits perfectly.
Using systemd resource control, I have created a slice called background.slice, which has
BlockIOReadBandwidth set for slower devices. This is using cgroups v1. For cgroups v2, use the
I then use the following command to start / resume the scrub:
sudo systemd-run --slice background.slice --scope btrfs scrub resume /path/to/mount
I’ve attached a gist containing an example of background.slice