Chapter 5. Discarding unused blocks You can perform or schedule discard operations on block devices that support them. The block discard operation communicates to the underlying storage which file system blocks are no longer in use by the mounted file system. Block discard operations allow SSDs to optimize garbage collection routines, and they can inform thinly-provisioned storage to repurpose unused physical blocks. Requirements The block device underlying the file system must support physical discard operations. Physical discard operations are supported if the value in the /sys/block/ <device> /queue/discard_max_bytes file is not zero. 5.1. Types of block discard operations You can run discard operations using different methods: Batch discard Is triggered explicitly by the user and discards all unused blocks in the selected file systems. Online discard Is specified at mount time and triggers in real time without user intervention. Online discard operations discard only blocks that are transitioning from the used to the free state. Periodic discard Are batch operations that are run regularly by a systemd service. All types are supported by the XFS and ext4 file systems. Recommendations Red Hat recommends that you use batch or periodic discard. Use online discard only if: the system's workload is such that batch discard is not feasible, or online discard operations are necessary to maintain performance. 5.2. Performing batch block discard You can perform a batch block discard operation to discard unused blocks on a mounted file system. Prerequisites The file system is mounted. The block device underlying the file system supports physical discard operations. Procedure Use the fstrim utility: To perform discard only on a selected file system, use: To perform discard on all mounted file systems, use: If you execute the fstrim command on: a device that does not support discard operations, or a logical device (LVM or MD) composed of multiple devices, where any one of the device does not support discard operations, the following message displays: Additional resources fstrim(8) man page on your system 5.3. Enabling online block discard You can perform online block discard operations to automatically discard unused blocks on all supported file systems. Procedure Enable online discard at mount time: When mounting a file system manually, add the -o discard mount option: When mounting a file system persistently, add the discard option to the mount entry in the /etc/fstab file. Additional resources mount(8) and fstab(5) man pages on your system 5.4. Enabling online block discard by using the storage RHEL system role You can mount an XFS file system with the online block discard option to automatically discard unused blocks. Prerequisites You have prepared the control node and the managed nodes You are logged in to the control node as a user who can run playbooks on the managed nodes. The account you use to connect to the managed nodes has sudo permissions on them. Procedure Create a playbook file, for example ~/playbook.yml , with the following content: --- - name: Manage local storage hosts: managed-node-01.example.com tasks: - name: Enable online block discard ansible.builtin.include_role: name: rhel-system-roles.storage vars: storage_volumes: - name: barefs type: disk disks: - sdb fs_type: xfs mount_point: /mnt/data mount_options: discard For details about all variables used in the playbook, see the /usr/share/ansible/roles/rhel-system-roles.storage/README.md file on the control node. Validate the playbook syntax: Note that this command only validates the syntax and does not protect against a wrong but valid configuration. Run the playbook: Verification Verify that online block discard option is enabled: Additional resources /usr/share/ansible/roles/rhel-system-roles.storage/README.md file /usr/share/doc/rhel-system-roles/storage/ directory 5.5. Enabling periodic block discard You can enable a systemd timer to regularly discard unused blocks on all supported file systems. Procedure Enable and start the systemd timer: Verification Verify the status of the timer:

Commands:
fstrim mount-point
fstrim --all
fstrim /mnt/non_discard fstrim: /mnt/non_discard : the discard operation is not supported
mount -o discard device mount-point
--- - name: Manage local storage hosts: managed-node-01.example.com tasks: - name: Enable online block discard ansible.builtin.include_role: name: rhel-system-roles.storage vars: storage_volumes: - name: barefs type: disk disks: - sdb fs_type: xfs mount_point: /mnt/data mount_options: discard
ansible-playbook --syntax-check ~/playbook.yml
ansible-playbook ~/playbook.yml
ansible managed-node-01.example.com -m command -a 'findmnt /mnt/data'
systemctl enable --now fstrim.timer Created symlink /etc/systemd/system/timers.target.wants/fstrim.timer /usr/lib/systemd/system/fstrim.timer.
systemctl status fstrim.timer fstrim.timer - Discard unused blocks once a week Loaded: loaded (/usr/lib/systemd/system/fstrim.timer; enabled; vendor preset: disabled) Active: active (waiting) since Wed 2023-05-17 13:24:41 CEST; 3min 15s ago Trigger: Mon 2023-05-22 01:20:46 CEST; 4 days left Docs: man:fstrim May 17 13:24:41 localhost.localdomain systemd[1]: Started Discard unused blocks once a week.