Defrag to improve Synology BTRFS performance
My Synology NAS gets a lot of use at home. Not only is the Synology NAS a central point for my VMware home lab, but all my PCs are connected to it for document, photo, and ISO storage.
In order to keep things as optimized as possible I try to defrag the drives every once in a while. Depending on how fragmented the drives are, this could help improve performance in sequential read and random IOPS.
In this post I’ll show how it easy it is to kick off the task. Just keep in mind you will want to do this during low usage as the defrag task will certainly impact performance while it’s running.
How to defrag Synology NAS using BTRFS
- Log into your Synology DSM.
- Open Storage Manager (found under the “Start Menu”) then click on Volume > Manage.
- Next select Start file system defragmentation and click on Next.
- Now click on Apply.
- File system defragmentation is available on specific models only.
- File system defragmentation can only be performed on volumes in Btrfs file system.
- File system defragmentation can only be performed when the status of the volume is Normal.
- Please do not shut down the system before file system defragmentation is complete. If the system is shut down before completion, file system defragmentation must restart from the beginning.
- Volume usage might increase if the volume contains snapshots of shared folders.
How to schedule the Synology defrag
If you would rather this be an automated process you can schedule the defrag instead.
- Log into your Synology DSM and go to Control Panel.
- Go to Task Scheduler and click on Create > Scheduled Task > User-defined script.
- Under the General tab give the task a name and keep the user set to root. Then under the Schedule tab configure when you want the defrag to run. On the Task Settings tab enter the following command:
/sbin/btrfs filesystem defragment -r /volume1
Finally you’ll be taken back to the Storage Manager and will see a message stating that defrag has started. Depending on how fragmented your drives are this could take several minutes to a couple hours.
Post in the comments below if you’ve noticed any speed improvements as well after a defrag.
Thanks for this :)
Thanks for posting this. It is exactly what I’ve been looking for. However, the script fails with the following errors. Any idea why?
Standard output/error:
ERROR: defrag failed on /volume1/@appstore/Node.js_v4/usr/local/bin/node: Success
ERROR: defrag failed on /volume1/@appstore/SynologyApplicationService/daemon/pgbouncer: Success
ERROR: defrag failed on /volume1/@appstore/SynologyApplicationService/tools/notification_send: Success
ERROR: defrag failed on /volume1/@appstore/ChannelsDVR/channels-dvr/2018.11.20.2224/ffmpeg: Success
ERROR: defrag failed on /volume1/@appstore/ChannelsDVR/channels-dvr/2018.11.20.2224/channels-dvr: Success
ERROR: defrag failed on /volume1/@appstore/CloudSync/sbin/syno-cloud-syncd: Success
ERROR: defrag failed on /volume1/@appstore/Plex Media Server/Plex Media Server: Success
ERROR: defrag failed on /volume1/@appstore/Plex Media Server/Plex Script Host: Success
ERROR: defrag failed on /volume1/@appstore/Plex Media Server/Plex Tuner Service: Success
total 9 failures
Yeah. I’m getting those errors too. Any suggestions?
me too
To get rid of those errors, specify the directories to defrag like the webapp does. Watching ps aux it cycles individually through all shared folders excluding the @ActiveBackup. Throw a -v in there to get verbose and email output from the task. The -b is an exclude. This example would have run in 3 commands but can be combined:
/sbin/btrfs filesystem defragment -v -r -b /volume1/nacho/@ActiveBackup /volume1/nacho -b /volume1/burrito/volume1/burrito/@ActiveBackup /volume1/burrito -b /volume1/taco/@ActiveBackup /volume1/taco
If there are ANY, and I mean ANY Snapshots or deduped data this will cause all that data to expand! DO NOT RUN THIS, infact according to several bits of documentation you SHOULD NOT RUN THIS.
https://www.reddit.com/r/synology/comments/b99ibp/why_did_i_lose_nearly_1tb_of_disk_space_after_a/
Only you know how YOUR NAS is configured and should research how things may impact YOUR configuration. I don’t use snapshots or deduplication on my NAS so neither of these are an issue for me.
I would suggest you read the Synology Knowledgebase, https://www.synology.com/en-us/knowledgebase/DSM/help/DSM/StorageManager/volume_filesystem_defragmentation – at no point does it mention you should NOT run this command. In fact they state “Volume usage might increase if the volume contains snapshots of shared folders.”.
-Michael
Hi, i tried the script and got the following error:
Current status: 1 (Interrupted)
Standard output/error:
ERROR: cannot open /volume1: No such file or directory
total 1 failures
I am only having volume1 in my NAS. If it helps i am running DMS 7 version.
thank you