Chapter 19. Discarding unused blocks
Discard operations improve storage performance and lifespan by informing the storage device which blocks are no longer in use. It allows SSDs to optimize wear leveling and enabling thin-provisioned storage to reclaim space.
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_bytesfile is not zero.
19.1. Types of block discard operations
Block discard operations can be performed by using batch, online, or periodic methods, each with specific use cases and performance recommendations.
The following list describes the various discard operations:
- Batch discard
- 
							This type of discard is part of the fstrimcommand. It discards all unused blocks in a file system that match criteria specified by the administrator. Red Hat Enterprise Linux 10 supports batch discard on XFS and ext4 formatted devices that support physical discard operations.
- Online discard
- This type of discard operation is configured at mount time with the discard option, and runs in real time without user intervention. However, it only discards blocks that are transitioning from used to free. Red Hat Enterprise Linux 10 supports online discard on XFS and ext4 formatted devices. - Use batch discard, except when online discard is required to maintain performance, or when batch discard is not feasible for the workload of the system. 
- Periodic discard
- 
							Batch operations that are run regularly by a systemdservice.
All types are supported by the XFS and ext4 file systems.
Recommendations
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.
19.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 - fstrimutility:- To perform discard only on a selected file system, use: - fstrim mount-point - # fstrim mount-point- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- To perform discard on all mounted file systems, use: - fstrim --all - # fstrim --all- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
 
- If you run the - fstrimcommand 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: - fstrim /mnt/non_discard - # fstrim /mnt/non_discard fstrim: /mnt/non_discard: the discard operation is not supported- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
 
19.3. Enabling online block discard
				You can perform online block discard operations to automatically discard unused blocks on all supported file systems. For more information, see the mount(8) and fstab(5) man pages on your system.
			
Procedure
- Enable online discard at mount time: - When mounting a file system manually, add the - -o discardmount option:- mount -o discard device mount-point - # mount -o discard device mount-point- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- 
								When mounting a file system persistently, add the discardoption to the mount entry in the/etc/fstabfile.
 
19.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 sudopermissions on them.
Procedure
- Create a playbook file, for example, - ~/playbook.yml, with the following content:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - For details about all variables used in the playbook, see the - /usr/share/ansible/roles/rhel-system-roles.storage/README.mdfile on the control node.
- Validate the playbook syntax: - ansible-playbook --syntax-check ~/playbook.yml - $ ansible-playbook --syntax-check ~/playbook.yml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Note that this command only validates the syntax and does not protect against a wrong but valid configuration. 
- Run the playbook: - ansible-playbook ~/playbook.yml - $ ansible-playbook ~/playbook.yml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
Verification
- Verify that online block discard option is enabled: - ansible managed-node-01.example.com -m command -a 'findmnt /mnt/data' - # ansible managed-node-01.example.com -m command -a 'findmnt /mnt/data'- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
19.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 - systemdtimer:- systemctl enable --now fstrim.timer - # systemctl enable --now fstrim.timer Created symlink /etc/systemd/system/timers.target.wants/fstrim.timer- /usr/lib/systemd/system/fstrim.timer. - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
Verification
- Verify the status of the timer: - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow